gpt4 book ai didi

Emacs evil-mode : how to create a new text object to select words with any non-space characters?

转载 作者:行者123 更新时间:2023-12-04 09:11:04 26 4
gpt4 key购买 nike

我正在尝试在 Evil 中创建一个新的文本对象。例如,文本对象 iw 将只选择包含连字符的字符串子集。我希望新的文本对象匹配任何非空格字符的单词。到目前为止我得到的是:

(evil-define-text-object evil-inner-space (count &optional beg end type)
"Select inner space."
:extend-selection nil
(evil-regexp-range count beg end type "[ \n]" " " t))

(define-key evil-inner-text-objects-map " " 'evil-inner-space)

此代码无法正确匹配行尾的单词。由于\n,它适用于行首的单词。

我尝试了很多没有用的东西。对我来说麻烦的是,当某些东西不起作用时,我不知道是由于错误的正则表达式还是邪恶正则表达式范围的限制。例如,使用\s- 会报错,这似乎是 Evil 中的一个限制。

最佳答案

更新 : evil-regexp-range was recently replaced with 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)))))

原答案:

如果您最终定义了多个新文本对象,重复可能会很烦人,尤其是如果您想同时绑定(bind)内部对象和外部对象。如果你遇到了那个障碍,试试这个:
(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")

(这比你想要的要多,但我会把它留在这里以防它帮助某人:)

关于Emacs evil-mode : how to create a new text object to select words with any non-space characters?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18102004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com