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 new bugs of vi-mode introduced by #952 #964

Merged
merged 4 commits into from
Aug 19, 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
12 changes: 7 additions & 5 deletions extensions/vi-mode/commands.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@

(define-vi-operator vi-delete (start end type) ()
(let ((pos (point-charpos (current-point))))
(with-killring-context (:options (when (eq type :line) :vi-line))
(with-killring-context (:options (when (eq type :line) :vi-line)
:appending (when (eq type :block)
(continue-flag :kill)))
(kill-region start end))
(when (and (eq type :line)
(eq 'vi-delete (command-name (this-command))))
Expand Down Expand Up @@ -447,12 +449,12 @@
(redo n)
(fall-within-line (current-point)))

(defun vi-forward-matching-paren (window point &optional (offset *cursor-offset*))
(declare (ignore window offset))
(defun vi-forward-matching-paren (window point &optional (offset -1))
(declare (ignore window))
(with-point ((point point))
(when (syntax-open-paren-char-p (character-at point))
(when (scan-lists point 1 0 t)
(character-offset point *cursor-offset*)))))
(character-offset point offset)))))

(defun vi-backward-matching-paren (window point &optional (offset -1))
(declare (ignore window offset))
Expand All @@ -463,7 +465,7 @@
(:type :inclusive
:jump t)
(alexandria:when-let ((p (or (vi-backward-matching-paren (current-window) (current-point))
(vi-forward-matching-paren (current-window) (current-point) *cursor-offset*))))
(vi-forward-matching-paren (current-window) (current-point)))))
(move-point (current-point) p)))

(let ((old-forward-matching-paren)
Expand Down
117 changes: 61 additions & 56 deletions extensions/vi-mode/commands/utils.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
(:import-from :lem-vi-mode/visual
:visual-p
:visual-line-p
:visual-block-p
:apply-visual-range
:vi-visual-end)
(:import-from :lem/common/command
Expand Down Expand Up @@ -89,7 +90,7 @@
(t (values arg-list '("P") nil))))

(defmacro define-vi-motion (name arg-list (&key type jump) &body body)
(check-type type (or null (member :inclusive :exclusive :line)))
(check-type type (or null (member :inclusive :exclusive :line :block)))
(check-type jump boolean)
(multiple-value-bind (arg-list arg-descriptor default-n-arg)
(parse-vi-motion-arg-list arg-list)
Expand Down Expand Up @@ -125,63 +126,67 @@
(if (typep command 'vi-motion)
(vi-motion-type command)
:exclusive)))
(if (visual-p)
(let (start end)
(apply-visual-range
(lambda (vstart vend)
(setf start vstart
end vend)))
(values start
end
(if (visual-line-p) :line :exclusive)))
(with-point ((start (current-point)))
(if motion
(let ((command (get-command motion)))
(call-motion command n)
(values
start
(copy-point (current-point))
(command-motion-type command)))
(let* ((uarg (or (read-universal-argument) n))
(command-name (read-command))
(command (get-command command-name)))
(typecase command
(vi-operator
(if (eq command-name (command-name (this-command)))
;; Recursive call of the operator like 'dd', 'cc'
(with-point ((end (current-point)))
(line-offset end (1- (or uarg 1)))
(values start end :line))
;; Ignore an invalid operator (like 'dJ')
nil))
(otherwise
(call-motion command uarg)
(values start
(copy-point (current-point))
(command-motion-type command))))))))))
(with-point ((start (current-point)))
(if motion
(let ((command (get-command motion)))
(call-motion command n)
(values
start
(copy-point (current-point))
(command-motion-type command)))
(let* ((uarg (or (read-universal-argument) n))
(command-name (read-command))
(command (get-command command-name)))
(typecase command
(vi-operator
(if (eq command-name (command-name (this-command)))
;; Recursive call of the operator like 'dd', 'cc'
(with-point ((end (current-point)))
(line-offset end (1- (or uarg 1)))
(values start end :line))
;; Ignore an invalid operator (like 'dJ')
nil))
(otherwise
(call-motion command uarg)
(values start
(copy-point (current-point))
(command-motion-type command)))))))))

(defun call-vi-operator (n fn &key motion keep-visual restore-point)
(with-point ((*vi-origin-point* (current-point)))
(unwind-protect
(if *vi-operator-arguments*
(apply fn *vi-operator-arguments*)
(multiple-value-bind (start end type)
(vi-operator-region n motion)
(when (point< end start)
(rotatef start end))
(ecase type
(:line (unless (visual-p)
(line-start start)
(line-end end)))
(:inclusive (character-offset end 1))
(:exclusive))
(let ((*vi-operator-arguments* (list start end type)))
(funcall fn start end type))))
(when restore-point
(move-point (current-point) *vi-origin-point*))
(unless keep-visual
(when (visual-p)
(vi-visual-end))))))
(flet ((call-with-region (fn start end type)
(when (point< end start)
(rotatef start end))
(ecase type
(:line (unless (visual-p)
(line-start start)
(line-end end)))
(:block)
(:inclusive
(unless (point= start end)
(character-offset end 1)))
(:exclusive))
(let ((*vi-operator-arguments* (list start end type)))
(funcall fn start end type))))
(with-point ((*vi-origin-point* (current-point)))
(unwind-protect
(if *vi-operator-arguments*
(apply fn *vi-operator-arguments*)
(if (visual-p)
(apply-visual-range
(lambda (start end)
(call-with-region fn start end
(cond
((visual-line-p) :line)
((visual-block-p) :block)
(t :exclusive)))))
(multiple-value-bind (start end type)
(vi-operator-region n motion)
(call-with-region fn start end type))))
(when restore-point
(move-point (current-point) *vi-origin-point*))
(unless keep-visual
(when (visual-p)
(vi-visual-end)))))))

(defmacro define-vi-operator (name arg-list (&key motion keep-visual restore-point) &body body)
(with-gensyms (n extra-args)
Expand Down