Skip to content

Commit

Permalink
Add evil-word-object & evil-WORD-object which stay on line
Browse files Browse the repository at this point in the history
Fixes #834
  • Loading branch information
tomdl89 committed Apr 24, 2022
1 parent 48404a3 commit f75732d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
4 changes: 2 additions & 2 deletions evil-commands.el
Original file line number Diff line number Diff line change
Expand Up @@ -1277,15 +1277,15 @@ 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."
(evil-select-an-object 'evil-WORD beg end type count))

(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."
Expand Down
57 changes: 43 additions & 14 deletions evil-common.el
Original file line number Diff line number Diff line change
Expand Up @@ -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\".
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down
11 changes: 10 additions & 1 deletion evil-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit f75732d

Please sign in to comment.