Skip to content

Commit

Permalink
Merge pull request #1236 from mychris/patch/fix-point-leak-in-vi-mode
Browse files Browse the repository at this point in the history
Fixes point leaks in vi-mode.
  • Loading branch information
cxxxr authored Jan 6, 2024
2 parents e6e9c66 + fe84475 commit 22c2c42
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 40 deletions.
52 changes: 26 additions & 26 deletions extensions/vi-mode/commands.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -194,32 +194,32 @@
(bolp p)))

(define-command vi-forward-word-begin (&optional (n 1)) ("p")
(let ((start-line (line-number-at-point (current-point)))
(origin (copy-point (current-point))))
(dotimes (i n)
(forward-word-begin #'word-char-type))
;; In operator-pending mode, this motion behaves differently.
(when (operator-pending-mode-p)
(with-point ((p (current-point)))
;; Go back to the end of the previous line when the END point is in the next line.
;; For example, when the cursor is at [b],
;; foo [b]ar
;; baz
;; 'dw' deletes only the 'bar', instead of deleting to the beginning of the next word.
(skip-whitespace-backward p t)
(when (and (point< origin p)
(bolp p))
(line-offset p -1)
(line-end p)
(loop while (and (< start-line
(line-number-at-point p))
(on-only-space-line-p p))
do (line-offset p -1)
(line-end p))
;; Skip this line if the previous line is empty
(when (bolp p)
(character-offset p 1))
(move-point (current-point) p))))))
(let ((start-line (line-number-at-point (current-point))))
(with-point ((origin (current-point)))
(dotimes (i n)
(forward-word-begin #'word-char-type))
;; In operator-pending mode, this motion behaves differently.
(when (operator-pending-mode-p)
(with-point ((p (current-point)))
;; Go back to the end of the previous line when the END point is in the next line.
;; For example, when the cursor is at [b],
;; foo [b]ar
;; baz
;; 'dw' deletes only the 'bar', instead of deleting to the beginning of the next word.
(skip-whitespace-backward p t)
(when (and (point< origin p)
(bolp p))
(line-offset p -1)
(line-end p)
(loop while (and (< start-line
(line-number-at-point p))
(on-only-space-line-p p))
do (line-offset p -1)
(line-end p))
;; Skip this line if the previous line is empty
(when (bolp p)
(character-offset p 1))
(move-point (current-point) p)))))))

(define-command vi-backward-word-begin (&optional (n 1)) ("p")
(dotimes (i n)
Expand Down
4 changes: 2 additions & 2 deletions extensions/vi-mode/commands/utils.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
(or (range-type retval) :exclusive)))
(otherwise
(values start
(copy-point (current-point))
(copy-point (current-point) :temporary)
(command-motion-type command)))))))
(command-motion-type (command)
(if (typep command 'vi-motion)
Expand All @@ -162,7 +162,7 @@
(save-excursion
(ignore-some-conditions (end-of-buffer)
(next-logical-line (1- (or uarg 1))))
(values start (copy-point (current-point)) :line))
(values start (copy-point (current-point) :temporary) :line))
;; raise error for invalid commands
(error 'editor-abort :message nil)))
(otherwise
Expand Down
19 changes: 8 additions & 11 deletions extensions/vi-mode/visual.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,18 @@
(typep (current-main-state) 'visual-block))

(defun visual-range ()
(cond
((visual-char-p)
(with-point ((start *start-point*)
(end (current-point)))
(with-point ((start *start-point*)
(end (current-point)))
(cond
((visual-char-p)
(cond ((point<= start end)
(character-offset end 1))
((point< end start)
(character-offset start 1)))
(list start end)))
((visual-block-p)
(list (copy-point *start-point*)
(copy-point (current-point))))
(t
(with-point ((start *start-point*)
(end (current-point)))
(list start end))
((visual-block-p)
(list start end))
(t
(when (point< end start)
(rotatef start end))
(line-start start)
Expand Down
2 changes: 1 addition & 1 deletion extensions/vi-mode/window.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
(window-view-point window))

(defun window-end-point (&optional (window (current-window)))
(let ((point (copy-point (window-start-point window))))
(let ((point (copy-point (window-start-point window) :temporary)))
(move-to-next-virtual-line point (1- (window-height* window)) window)
point))

Expand Down

0 comments on commit 22c2c42

Please sign in to comment.