Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix vi-mode search feature to work similar to Vim #996

Merged
merged 6 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions extensions/vi-mode/commands.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -435,22 +435,53 @@
(add-hook *enable-hook* 'on-matching-paren)
(add-hook *disable-hook* 'off-matching-paren)

(defvar *last-search-direction* nil)

(define-command vi-search-forward () ()
(setf *last-search-direction* :forward)
(with-jump-motion
(lem/isearch:isearch-forward-regexp "/")))
(lem/isearch::isearch-start "/"
(lambda (point string)
(alexandria:when-let (p (lem/isearch::search-forward-regexp
(copy-point lem/isearch::*isearch-start-point* :temporary)
string))
(character-offset p (- (length string)))
(move-point point p)))
#'lem/isearch::search-forward-regexp
#'lem/isearch::search-backward-regexp
"")))

(define-command vi-search-backward () ()
(setf *last-search-direction* :backward)
(with-jump-motion
(lem/isearch:isearch-backward-regexp "?")))

(define-command vi-search-next (n) ("p")
(defun vi-search-repeat-forward (n)
(with-jump-motion
(dotimes (i n) (lem/isearch:isearch-next))))

(define-command vi-search-previous (n) ("p")
(with-point ((p (current-point)))
(vi-forward-char (length lem/isearch::*isearch-string*))
(loop repeat n
for found = (lem/isearch:isearch-next)
unless found
do (move-point (current-point) p)
(return)
finally
(vi-backward-char (length lem/isearch::*isearch-string*))))))

(defun vi-search-repeat-backward (n)
(with-jump-motion
(dotimes (i n) (lem/isearch:isearch-prev))))

(define-command vi-search-next (n) ("p")
(case *last-search-direction*
(:forward (vi-search-repeat-forward n))
(:backward (vi-search-repeat-backward n))))

(define-command vi-search-previous (n) ("p")
(case *last-search-direction*
(:forward (vi-search-repeat-backward n))
(:backward (vi-search-repeat-forward n))))

(define-command vi-search-forward-symbol-at-point () ()
(with-jump-motion
(lem/isearch:isearch-forward-symbol-at-point)
Expand Down
3 changes: 2 additions & 1 deletion extensions/vi-mode/jump-motions.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
(,p (copy-point (current-point)) ; leak?
))
(prog1 (progn ,@body)
(push ,p *prev-jump-points*)
(unless (point= ,p (current-point))
(push ,p *prev-jump-points*))
(setf *current-point* nil))))))

(defun jump-back ()
Expand Down
21 changes: 21 additions & 0 deletions extensions/vi-mode/tests/motion.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,24 @@
(ok (buf= "foo-bar-Ba[z]z"))
(cmd "$2;")
(ok (buf= "foo-ba[r]-Bazz")))))

(deftest vi-search-forward-backward
(with-fake-interface ()
(with-vi-buffer (#?"[f]oo-bar-baz\nThis bar is great for gathering.\nCandy bars are my favorite food.\n")
(cmd "/bar<Return>")
(ok (buf= #?"foo-[b]ar-baz\nThis bar is great for gathering.\nCandy bars are my favorite food.\n"))
(cmd "n")
(ok (buf= #?"foo-bar-baz\nThis [b]ar is great for gathering.\nCandy bars are my favorite food.\n"))
(cmd "N")
(ok (buf= #?"foo-[b]ar-baz\nThis bar is great for gathering.\nCandy bars are my favorite food.\n"))
(cmd "2n")
(ok (buf= #?"foo-bar-baz\nThis bar is great for gathering.\nCandy [b]ars are my favorite food.\n"))
(cmd "/apple<Return>")
(ok (buf= #?"foo-bar-baz\nThis bar is great for gathering.\nCandy [b]ars are my favorite food.\n")))
(with-vi-buffer (#?"foo-bar-baz\nThis bar is great for gathering.\nCandy bars are my favorite food[.]\n")
(cmd "?bar<Return>")
(ok (buf= #?"foo-bar-baz\nThis bar is great for gathering.\nCandy [b]ars are my favorite food.\n"))
(cmd "n")
(ok (buf= #?"foo-bar-baz\nThis [b]ar is great for gathering.\nCandy bars are my favorite food.\n"))
(cmd "N")
(ok (buf= #?"foo-bar-baz\nThis bar is great for gathering.\nCandy [b]ars are my favorite food.\n")))))
9 changes: 6 additions & 3 deletions extensions/vi-mode/tests/utils.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,12 @@
(appendf key-args '(:shift t)))))
(let ((sym-str (ppcre:scan-to-strings "[^<-]+(?=>$)" key-str)))
(apply #'make-key :sym (if-let (char (name-char sym-str))
(if (char= char #\Esc)
"Escape"
(string char))
(case char
(#\Esc "Escape")
(#\Return "Return")
(#\Space "Space")
(#\Tab "Tab")
(otherwise (string char)))
sym-str)
key-args))))
keys))
Expand Down
54 changes: 33 additions & 21 deletions src/ext/isearch.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -347,30 +347,42 @@
(when (string= "" *isearch-string*)
(setq *isearch-string* (isearch-default-string)))
(setf (variable-value 'isearch-prev-last :buffer) nil)
(cond ((variable-value 'isearch-next-last :buffer)
(setf (variable-value 'isearch-next-last :buffer) nil)
(with-point ((p (current-point)))
(buffer-start p)
(when (funcall *isearch-search-forward-function* p *isearch-string*)
(move-point (current-point) p))))
((not (funcall *isearch-search-forward-function* (current-point) *isearch-string*))
(setf (variable-value 'isearch-next-last :buffer) t)))
(isearch-update-display)))
(prog1
(cond ((variable-value 'isearch-next-last :buffer)
(setf (variable-value 'isearch-next-last :buffer) nil)
(with-point ((p (current-point)))
(buffer-start p)
(if (funcall *isearch-search-forward-function* p *isearch-string*)
(progn
(move-point (current-point) p)
t)
nil)))
((not (funcall *isearch-search-forward-function* (current-point) *isearch-string*))
(setf (variable-value 'isearch-next-last :buffer) t)
nil)
(t))
(isearch-update-display))))
Comment on lines +350 to +364
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to return a boolean value to tell whether the search is successful.


(define-command isearch-prev () ()
(when (boundp '*isearch-string*)
(when (string= "" *isearch-string*)
(setq *isearch-string* (isearch-default-string)))
(setf (variable-value 'isearch-next-last :buffer) nil)
(cond ((variable-value 'isearch-prev-last :buffer)
(setf (variable-value 'isearch-prev-last :buffer) nil)
(with-point ((p (current-point)))
(buffer-end p)
(when (funcall *isearch-search-backward-function* p *isearch-string*)
(move-point (current-point) p))))
((not (funcall *isearch-search-backward-function* (current-point) *isearch-string*))
(setf (variable-value 'isearch-prev-last :buffer) t)))
(isearch-update-display)))
(prog1
(cond ((variable-value 'isearch-prev-last :buffer)
(setf (variable-value 'isearch-prev-last :buffer) nil)
(with-point ((p (current-point)))
(buffer-end p)
(if (funcall *isearch-search-backward-function* p *isearch-string*)
(progn
(move-point (current-point) p)
t)
nil)))
((not (funcall *isearch-search-backward-function* (current-point) *isearch-string*))
(setf (variable-value 'isearch-prev-last :buffer) t)
nil)
(t))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns T. Is this intended?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I changed this to return a boolean value as I did to isearch-next.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I understand now 👍

(isearch-update-display))))

(define-command isearch-yank () ()
(let ((str (yank-from-clipboard-or-killring)))
Expand All @@ -383,9 +395,9 @@
(concatenate 'string
*isearch-string*
(string c)))
(with-point ((start-point (current-point)))
(unless (funcall *isearch-search-function* (current-point) *isearch-string*)
(move-point (current-point) start-point)))
(with-point ((point (current-point)))
(unless (funcall *isearch-search-function* point *isearch-string*)
(move-point (current-point) point)))
(isearch-update-display)
t)

Expand Down
Loading