替代前向词/后向词以包含符号——例如,*** [英] Alternative to forward-word / backward-word to include symbols -- e.g., ***

查看:16
本文介绍了替代前向词/后向词以包含符号——例如,***的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文档中指示需要注意的位置的首选方法是使用三个星号***.当需要使用 shift+right-wordshift+left-word 选择包含三个星号的区域时,这些函数会跳过三个星号并移至下一个词.当我偷看 bindings.el 时,我看到 left-wordright-wordforward-wordbackward-word,它们可以追溯到 C 源代码中的内置函数.本质上,我正在寻找 left-wordright-word ,其中包括三个星号 *** 等符号.

My preferred method of indicating a location within a document that requires attention is with three asterisks ***. When it comes time to select the region containing the three asterisks using shift+right-word or shift+left-word, those functions skip over the three asterisks and move along to the next word. When I peeked inside bindings.el, I saw that left-word and right-word are adaptations of forward-word and backward-word, that are traceable to built-in functions in the C source code. Essentially, I'm looking for left-word and right-word that includes symbols such as three asterisks ***.

任何人都可以提出一种方法来让函数向左跳转一个单词或符号,向右跳转一个单词或符号.该函数的行为需要与左词和右词相似,以便在多次按下箭头键时我可以选择多个词——例如,left-word-or-symbolscode> 和 right-word-or-symbols.

Can anyone please suggest a way to have a function that jumps a word or symbols to the left, and a word or symbols to the right. The function would need to behave similar to left-word and right-word so that I could select more than one word if the arrow keys are pressed more than one time -- e.g., left-word-or-symbols and right-word-or-symbols.

以下是基于以下有用答案的自定义函数:

The following is a custom function based upon the helpful answer of below:

(defvar lawlist-movement-syntax-table
  (let ((st (make-syntax-table)))
    ;; ` default = punctuation
    ;; ' default = punctuation
    ;; , default = punctuation
    ;; ; default = punctuation
    (modify-syntax-entry ?{ "." st)  ;; { = punctuation
    (modify-syntax-entry ?} "." st)  ;; } = punctuation
    (modify-syntax-entry ?" "." st) ;; " = punctuation
    (modify-syntax-entry ?\ "_" st) ;;  = symbol
    (modify-syntax-entry ?$ "_" st) ;; $ = symbol
    (modify-syntax-entry ?\% "_" st) ;; % = symbol
    st)
  "Syntax table used while executing custom movement functions.")

(defun lawlist-forward-entity ()
"http://stackoverflow.com/q/18675201/2112489"
(interactive "^")
  (with-syntax-table lawlist-movement-syntax-table
    (cond
      ((eolp)
        (forward-char))
      ((and
          (save-excursion (< 0 (skip-chars-forward " 	")))
          (not (region-active-p)))
        (skip-chars-forward " 	"))
      ((and
          (save-excursion (< 0 (skip-chars-forward " 	")))
          (region-active-p))
        (skip-chars-forward " 	")
        (cond
          ((save-excursion (< 0 (skip-syntax-forward "w")))
            (skip-syntax-forward "w"))
          ((save-excursion (< 0 (skip-syntax-forward ".")))
            (skip-syntax-forward "."))
          ((save-excursion (< 0 (skip-syntax-forward "_()")))
            (skip-syntax-forward "_()"))))
      ((save-excursion (< 0 (skip-syntax-forward "w")))
        (skip-syntax-forward "w")
        (if (and
              (not (region-active-p))
              (save-excursion (< 0 (skip-chars-forward " 	"))))
          (skip-chars-forward " 	")))
      ((save-excursion (< 0 (skip-syntax-forward ".")))
        (skip-syntax-forward ".")
        (if (and
              (not (region-active-p))
              (save-excursion (< 0 (skip-chars-forward " 	"))))
          (skip-chars-forward " 	")))
      ((save-excursion (< 0 (skip-syntax-forward "_()")))
        (skip-syntax-forward "_()")
        (if (and
              (not (region-active-p))
              (save-excursion (< 0 (skip-chars-forward " 	"))))
          (skip-chars-forward " 	"))))))

(defun lawlist-backward-entity ()
"http://stackoverflow.com/q/18675201/2112489"
(interactive "^")
  (with-syntax-table lawlist-movement-syntax-table
    (cond
      ((bolp)
        (backward-char))
      ((save-excursion (> 0 (skip-chars-backward " 	")) (bolp))
        (skip-chars-backward " 	"))
      ((save-excursion (> 0 (skip-chars-backward " 	")) (> 0 (skip-syntax-backward "w")))
        (skip-chars-backward " 	")
        (skip-syntax-backward "w"))
      ((save-excursion (> 0 (skip-syntax-backward "w")))
        (skip-syntax-backward "w"))
      ((save-excursion (> 0 (skip-syntax-backward ".")))
        (skip-syntax-backward "."))
      ((save-excursion (> 0 (skip-chars-backward " 	")) (> 0 (skip-syntax-backward ".")))
        (skip-chars-backward " 	")
        (skip-syntax-backward "."))
      ((save-excursion (> 0 (skip-syntax-backward "_()")))
        (skip-syntax-backward "_()"))
      ((save-excursion (> 0 (skip-chars-backward " 	")) (> 0 (skip-syntax-backward "_()")))
        (skip-chars-backward " 	")
        (skip-syntax-backward "_()")))))

推荐答案

添加到模式的模式钩子,你正在编辑文本的地方:

Add to the mode hook of the mode, where you are editing the text:

(modify-syntax-entry ?* "w")

阅读此处更多信息.

请注意,这将使其他函数将星号视为单词组成部分(不过,您可能会想要它,因为您很快就会发现不仅运动命令的行为与您希望它们的行为相反).

Note that this will make other functions treat asterisks as if they were word constituent (though, you'd probably want it, as you'd soon discover that not only the motion command behave contrary to how you want them to).

另一种仅重载这些特定命令的方法是使用 defadvice 通知它们,并仅在函数运行期间临时更改语法表.

Another way to overload only these particular commands would be to advise them with defadvice and to temporarily alter the syntax table just for the duration of the function.

这篇关于替代前向词/后向词以包含符号——例如,***的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆