更新:evil-regexp-range
最近被替换为evil-select-paren
. 这适用于当前的邪恶,并且与旧的用法相同:
(defmacro define-and-bind-text-object (key start-regex end-regex)
(let ((inner-name (make-symbol "inner-name"))
(outer-name (make-symbol "outer-name")))
`(progn
(evil-define-text-object ,inner-name (count &optional beg end type)
(evil-select-paren ,start-regex ,end-regex beg end type count nil))
(evil-define-text-object ,outer-name (count &optional beg end type)
(evil-select-paren ,start-regex ,end-regex beg end type count t))
(define-key evil-inner-text-objects-map ,key (quote ,inner-name))
(define-key evil-outer-text-objects-map ,key (quote ,outer-name)))))
原答案:
如果您最终定义了多个新文本对象,重复可能会很烦人,尤其是如果您想同时绑定内部对象和外部对象。如果你遇到了那个障碍,试试这个:
(defmacro define-and-bind-text-object (key start-regex end-regex)
(let ((inner-name (make-symbol "inner-name"))
(outer-name (make-symbol "outer-name")))
`(progn
(evil-define-text-object ,inner-name (count &optional beg end type)
(evil-regexp-range count beg end type ,start-regex ,end-regex t))
(evil-define-text-object ,outer-name (count &optional beg end type)
(evil-regexp-range count beg end type ,start-regex ,end-regex nil))
(define-key evil-inner-text-objects-map ,key (quote ,inner-name))
(define-key evil-outer-text-objects-map ,key (quote ,outer-name)))))
用法:
; between dollar signs:
(define-and-bind-text-object "$" "\\$" "\\$")
; between pipe characters:
(define-and-bind-text-object "|" "|" "|")
; from regex "b" up to regex "c", bound to k (invoke with "vik" or "vak"):
(define-and-bind-text-object "k" "b" "c")
(这比你想要的要多,但我会把它留在这里以防它帮助某人:)