diff options
| author | Frank Fischer <frank-fischer@shadow-soft.de> | 2014-01-02 13:38:38 +0100 |
|---|---|---|
| committer | Frank Fischer <frank-fischer@shadow-soft.de> | 2014-01-02 13:38:38 +0100 |
| commit | 1bafbb69ae4797018918167d2a255e45e1439306 (patch) | |
| tree | 1c3bbdcddf7bd98f8a57277441deaffae86901b9 | |
| parent | 31b38b011caf24a0d41ded56dab16064cb4e8363 (diff) | |
add `evil-select-inner-object` for normal inner text object selection
This functions fulfils the same purpose as `evil-inner-object-range`
but is based on thing-at-point.
Inner word, WORD, sentence and paragraph text objects are implemented
in terms of this function.
This addresses #364.
| -rw-r--r-- | evil-commands.el | 8 | ||||
| -rw-r--r-- | evil-common.el | 39 | ||||
| -rw-r--r-- | evil-tests.el | 5 |
3 files changed, 45 insertions, 7 deletions
diff --git a/evil-commands.el b/evil-commands.el index 12a584d..6b09564 100644 --- a/evil-commands.el +++ b/evil-commands.el @@ -1054,7 +1054,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-inner-object-range count beg end type #'evil-move-word)) + (evil-select-inner-object 'evil-word beg end type count)) (evil-define-text-object evil-a-WORD (count &optional beg end type) "Select a WORD." @@ -1062,7 +1062,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-inner-object-range count beg end type #'evil-move-WORD)) + (evil-select-inner-object 'evil-WORD beg end type count)) (evil-define-text-object evil-a-sentence (count &optional beg end type) "Select a sentence." @@ -1070,7 +1070,7 @@ or line COUNT to the top of the window." (evil-define-text-object evil-inner-sentence (count &optional beg end type) "Select inner sentence." - (evil-inner-object-range count beg end type #'evil-move-sentence)) + (evil-select-inner-object 'evil-sentence beg end type count)) (evil-define-text-object evil-a-paragraph (count &optional beg end type) "Select a paragraph." @@ -1080,7 +1080,7 @@ or line COUNT to the top of the window." (evil-define-text-object evil-inner-paragraph (count &optional beg end type) "Select inner paragraph." :type line - (evil-inner-object-range count beg end type #'evil-move-paragraph)) + (evil-select-inner-object 'evil-paragraph beg end type count t)) (evil-define-text-object evil-a-paren (count &optional beg end type) "Select a parenthesis." diff --git a/evil-common.el b/evil-common.el index 2c5cb06..43b9824 100644 --- a/evil-common.el +++ b/evil-common.el @@ -2775,6 +2775,45 @@ Returns t if RANGE was successfully adjusted and nil otherwise." (evil-set-range range nil (line-end-position 0))) (not (evil-subrange-p orig range))))) +(defun evil-select-inner-object (thing beg end type &optional count line) + "Return an inner text object range of COUNT objects. +If COUNT is positive, return objects following point; if COUNT is +negative, return objects preceding point. FORWARD is a function +which moves to the end of an object, and BACKWARD is a function +which moves to the beginning. 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 current +selection. If LINE is non-nil, the text object should be +linewise, otherwise it is character wise." + (let* ((count (or count 1)) + (bnd (bounds-of-thing-at-point thing))) + ;; if there is no current thing select space in between + (when (or (not bnd) (= (cdr bnd) (point))) + (setq bnd + (cons (save-excursion + (forward-thing thing -1) + (or (cdr-safe (bounds-of-thing-at-point thing)) + (point-min))) + (save-excursion + (forward-thing thing) + (or (car-safe (bounds-of-thing-at-point thing)) + (point-max)))))) + ;; check if current object is selected + (when (or (not beg) (not end) + (> beg (car bnd)) + (< end (cdr bnd))) + (when (or (not beg) (< (car bnd) beg)) (setq beg (car bnd))) + (when (or (not end) (> (cdr bnd) end)) (setq end (cdr bnd))) + (setq count (if (> count 0) (1- count) (1+ count)))) + (goto-char (if (< count 0) beg end)) + (evil-forward-nearest count + #'(lambda (cnt) (forward-thing thing cnt)) + #'(lambda (cnt) (evil-forward-not-thing thing cnt))) + (evil-range (if (>= count 0) beg (point)) + (if (< count 0) end (point)) + (if line 'line type) + :expanded t))) + (defun evil-inner-object-range (count beg end type forward &optional backward range-type) "Return an inner text object range (BEG END) of COUNT objects. If COUNT is positive, return objects following point; diff --git a/evil-tests.el b/evil-tests.el index 850d0ae..953c88a 100644 --- a/evil-tests.el +++ b/evil-tests.el @@ -6043,7 +6043,7 @@ Below some empty line.")) ;; and for Lisp evaluation." ("vip") "<;; This buffer is for notes, -;; and for Lisp evaluation[.] +;; and for Lisp evaluation.[] > ;; This buffer is for notes, ;; and for Lisp evaluation.") @@ -6055,11 +6055,10 @@ Below some empty line.")) ;; and for Lisp evaluation." ("vip") "<;; This buffer is for notes, -;; and for Lisp evaluation[.] +;; and for Lisp evaluation.[] > ;; This buffer is for notes, ;; and for Lisp evaluation.") - (evil-test-buffer ";; This buffer is for notes, ;; and for Lisp evaluation. |
