diff options
| author | Tom Dalziel <tom_dl@hotmail.com> | 2022-04-24 23:07:01 +0100 |
|---|---|---|
| committer | Tom Dalziel <33435574+tomdl89@users.noreply.github.com> | 2022-04-25 00:28:05 +0200 |
| commit | f75732de0ca5cae70f17dbc4bf7df03cb4ec491f (patch) | |
| tree | a47311e6decdd44f92749b60d2327ccad6bec4ad | |
| parent | 48404a336850a20ed093fcf78539037c17386235 (diff) | |
Add `evil-word-object` & `evil-WORD-object` which stay on line
Fixes #834
| -rw-r--r-- | evil-commands.el | 4 | ||||
| -rw-r--r-- | evil-common.el | 57 | ||||
| -rw-r--r-- | evil-tests.el | 11 |
3 files changed, 55 insertions, 17 deletions
diff --git a/evil-commands.el b/evil-commands.el index 545de06..1d88a76 100644 --- a/evil-commands.el +++ b/evil-commands.el @@ -1277,7 +1277,7 @@ or line COUNT to the top of the window." (evil-define-text-object evil-inner-word (count &optional beg end type) "Select inner word." - (evil-select-inner-object 'evil-word beg end type count)) + (evil-select-inner-object 'evil-word-object beg end type count)) (evil-define-text-object evil-a-WORD (count &optional beg end type) "Select a WORD." @@ -1285,7 +1285,7 @@ or line COUNT to the top of the window." (evil-define-text-object evil-inner-WORD (count &optional beg end type) "Select inner WORD." - (evil-select-inner-object 'evil-WORD beg end type count)) + (evil-select-inner-object 'evil-WORD-object beg end type count)) (evil-define-text-object evil-a-symbol (count &optional beg end type) "Select a symbol." diff --git a/evil-common.el b/evil-common.el index 521ecf7..11acdc5 100644 --- a/evil-common.el +++ b/evil-common.el @@ -1701,26 +1701,47 @@ backwards." "Move forward COUNT whitespace sequences [[:space:]]+." (evil-forward-chars "[:space:]" count)) +(defun evil--forward-word-respect-categories (count) + "Move forward COUNT words. +A word is a sequence of word characters matching [[:word:]] +\(recognized by `forward-word')." + (let ((word-separating-categories evil-cjk-word-separating-categories) + (word-combining-categories evil-cjk-word-combining-categories) + (pnt (point))) + (forward-word count) + (if (= pnt (point)) count 0))) + +(defun evil--forward-non-word-excl-newline (count) + "Move forward COUNT non-words. +A non-word is a sequence of non-whitespace non-word characters." + (evil-forward-chars "^[:word:]\n\r\t\f " count)) + +(defun evil--forward-non-word-incl-newline (count) + "Move forward COUNT non-words. +A non-word is a sequence of non-space, non-tab, non-word characters." + (evil-forward-chars "^[:word:]\t " count)) + (defun forward-evil-word (&optional count) "Move forward COUNT words. Moves point COUNT words forward or (- COUNT) words backward if -COUNT is negative. Point is placed after the end of the word (if -forward) or at the first character of the word (if backward). A +COUNT is negative. Point is placed after the end of the word (if +forward) or at the first character of the word (if backward). A word is a sequence of word characters matching \[[:word:]] (recognized by `forward-word'), a sequence of non-whitespace non-word characters '[^[:word:]\\n\\r\\t\\f ]', or an empty line matching ^$." - (evil-forward-nearest - count - #'(lambda (&optional cnt) - (let ((word-separating-categories evil-cjk-word-separating-categories) - (word-combining-categories evil-cjk-word-combining-categories) - (pnt (point))) - (forward-word cnt) - (if (= pnt (point)) cnt 0))) - #'(lambda (&optional cnt) - (evil-forward-chars "^[:word:]\n\r\t\f " cnt)) - #'forward-evil-empty-line)) + (evil-forward-nearest count + #'evil--forward-word-respect-categories + #'evil--forward-non-word-excl-newline + #'forward-evil-empty-line)) + +(defun forward-evil-word-object (&optional count) + "Move forward COUNT words. +Like `forward-evil-word' but include newline in non-word chars." + (evil-forward-nearest count + #'evil--forward-word-respect-categories + #'evil--forward-non-word-incl-newline + #'forward-evil-empty-line)) (defun forward-evil-WORD (&optional count) "Move forward COUNT \"WORDS\". @@ -1734,6 +1755,14 @@ WORD is a sequence of non-whitespace characters (evil-forward-chars "^\n\r\t\f " cnt)) #'forward-evil-empty-line)) +(defun forward-evil-WORD-object (&optional count) + "Move forward COUNT \"WORDS\". +Like `forward-evil-WORD' but exclude newline in WORD chars." + (evil-forward-nearest count + #'(lambda (&optional cnt) + (evil-forward-chars "^\t " cnt)) + #'forward-evil-empty-line)) + (defun forward-evil-symbol (&optional count) "Move forward COUNT symbols. Moves point COUNT symbols forward or (- COUNT) symbols backward @@ -3107,7 +3136,7 @@ This can be overridden with TYPE." If COUNT is positive, return objects following point; if COUNT is negative, return objects preceding point. If one is unspecified, the other is used with a negative argument. THING is a symbol -understood by thing-at-point. BEG, END and TYPE specify the +understood by `thing-at-point'. BEG, END and TYPE specify the current selection. If LINE is non-nil, the text object should be linewise, otherwise it is character wise." (let* ((count (or count 1)) diff --git a/evil-tests.el b/evil-tests.el index b1ea72f..5b728e9 100644 --- a/evil-tests.el +++ b/evil-tests.el @@ -5936,7 +5936,16 @@ Line 2")) (evil-test-buffer "([a])" ("viw") - "(<[a]>)"))) + "(<[a]>)")) + (ert-info ("Deleting whitespace is confined to the line") + (evil-test-buffer + "foo\n [ ] bar" + ("diw") + "foo\n[b]ar") + (evil-test-buffer + "foo\n [ ] bar" + ("diW") + "foo\n[b]ar"))) (ert-deftest evil-test-word-objects-cjk () "Test `evil-inner-word' and `evil-a-word' on CJK words" |
