aboutsummaryrefslogtreecommitdiff
path: root/evil-commands.el
diff options
context:
space:
mode:
authorJustin Burkett <justin@burkett.cc>2019-08-08 21:50:50 -0400
committerEivind Fonn <evfonn@gmail.com>2019-11-19 11:34:20 +0100
commit5365e4d79f7f9c9e04d8d4f9b02b087791c18db2 (patch)
treeea047fe8d9012bf3e60a00d431b9d9a1ee0bb6ae /evil-commands.el
parent92102f3b30d03d1d87b34ae2c01e4bc2013d5129 (diff)
Make more commands support visual-line-mode
when evil-respect-visual-line-mode is non-nil, including evil-change-line, evil-delete-line, and evil-yank-line. Most of the work is done through the motions evil-line-or-visual-line and evil-end-of-line-or-visual-line. These motions use visual lines when visual-line-mode and evil-respect-visual-line-mode are non-nil, and revert back to standard lines otherwise. Visual selection via visual lines in the sense of visual-line-mode is supported by adding a new screen-line type (named to avoid confusion with visual state).
Diffstat (limited to 'evil-commands.el')
-rw-r--r--evil-commands.el64
1 files changed, 51 insertions, 13 deletions
diff --git a/evil-commands.el b/evil-commands.el
index 7d87d6b..6efa2cd 100644
--- a/evil-commands.el
+++ b/evil-commands.el
@@ -162,6 +162,18 @@ of the line or the buffer; just return nil."
(evil-line-move (1- (or count 1)))
((beginning-of-buffer end-of-buffer)))))
+(evil-define-motion evil-line-or-visual-line (count)
+ "Move COUNT - 1 lines down."
+ :type screen-line
+ (let ((line-move-visual (and evil-respect-visual-line-mode
+ visual-line-mode)))
+ ;; Catch bob and eob errors. These are caused when not moving
+ ;; point starting in the first or last line, respectively. In this
+ ;; case the current line should be selected.
+ (condition-case err
+ (evil-line-move (1- (or count 1)))
+ ((beginning-of-buffer end-of-buffer)))))
+
(evil-define-motion evil-beginning-of-line ()
"Move the cursor to the beginning of the current line."
:type exclusive
@@ -196,6 +208,18 @@ If COUNT is given, move COUNT - 1 screen lines downward first."
(end-of-visual-line count)
(end-of-line count)))
+(evil-define-motion evil-end-of-line-or-visual-line (count)
+ "Move the cursor to the last character of the current screen
+line if `visual-line-mode' is active and
+`evil-respect-visual-line-mode' is non-nil. If COUNT is given,
+move COUNT - 1 screen lines downward first."
+ :type inclusive
+ (if (and (fboundp 'end-of-visual-line)
+ evil-respect-visual-line-mode
+ visual-line-mode)
+ (end-of-visual-line count)
+ (evil-end-of-line count)))
+
(evil-define-motion evil-middle-of-visual-line ()
"Move the cursor to the middle of the current visual line."
:type exclusive
@@ -1344,12 +1368,16 @@ or line COUNT to the top of the window."
(evil-define-operator evil-yank-line (beg end type register)
"Saves whole lines into the kill-ring."
- :motion evil-line
+ :motion evil-line-or-visual-line
:move-point nil
(interactive "<R><x>")
(when (evil-visual-state-p)
- (unless (memq type '(line block))
- (let ((range (evil-expand beg end 'line)))
+ (unless (memq type '(line block screen-line))
+ (let ((range (evil-expand beg end
+ (if (and evil-respect-visual-line-mode
+ visual-line-mode)
+ 'screen-line
+ 'line))))
(setq beg (evil-range-beginning range)
end (evil-range-end range)
type (evil-type range))))
@@ -1391,10 +1419,20 @@ Save in REGISTER or in the kill-ring with YANK-HANDLER."
(interactive "<R><x>")
;; act linewise in Visual state
(let* ((beg (or beg (point)))
- (end (or end beg)))
+ (end (or end beg))
+ (visual-line-mode (and evil-respect-visual-line-mode
+ visual-line-mode))
+ (line-end (if visual-line-mode
+ (save-excursion
+ (end-of-visual-line)
+ (point))
+ (line-end-position))))
(when (evil-visual-state-p)
- (unless (memq type '(line block))
- (let ((range (evil-expand beg end 'line)))
+ (unless (memq type '(line screen-line block))
+ (let ((range (evil-expand beg end
+ (if visual-line-mode
+ 'screen-line
+ 'line))))
(setq beg (evil-range-beginning range)
end (evil-range-end range)
type (evil-type range))))
@@ -1408,15 +1446,15 @@ Save in REGISTER or in the kill-ring with YANK-HANDLER."
(let ((temporary-goal-column most-positive-fixnum)
(last-command 'next-line))
(evil-delete beg end 'block register yank-handler)))
- ((eq type 'line)
+ ((memq type '(line screen-line))
(evil-delete beg end type register yank-handler))
(t
- (evil-delete beg (line-end-position) type register yank-handler)))))
+ (evil-delete beg line-end type register yank-handler)))))
(evil-define-operator evil-delete-whole-line
(beg end type register yank-handler)
"Delete whole line."
- :motion evil-line
+ :motion evil-line-or-visual-line
(interactive "<R><x>")
(evil-delete beg end type register yank-handler))
@@ -1515,20 +1553,20 @@ of the block."
(evil-define-operator evil-change-line (beg end type register yank-handler)
"Change to end of line."
- :motion evil-end-of-line
+ :motion evil-end-of-line-or-visual-line
(interactive "<R><x><y>")
(evil-change beg end type register yank-handler #'evil-delete-line))
(evil-define-operator evil-change-whole-line
(beg end type register yank-handler)
"Change whole line."
- :motion evil-line
+ :motion evil-line-or-visual-line
(interactive "<R><x>")
(evil-change beg end type register yank-handler #'evil-delete-whole-line))
(evil-define-command evil-copy (beg end address)
"Copy lines in BEG END below line given by ADDRESS."
- :motion evil-line
+ :motion evil-line-or-visual-line
(interactive "<r><addr>")
(goto-char (point-min))
(forward-line address)
@@ -1543,7 +1581,7 @@ of the block."
(evil-define-command evil-move (beg end address)
"Move lines in BEG END below line given by ADDRESS."
- :motion evil-line
+ :motion evil-line-or-visual-line
(interactive "<r><addr>")
(goto-char (point-min))
(forward-line address)