更改Emacs前向词行为

时间:2009-11-20 15:14:36

标签: emacs emacs23

正如标题所说,如何改变emacs前向词功能的行为?例如,假设[]是光标。然后:

my $abs_target_path[]= abs_path($target);
<M-f>
my $abs_target_path = abs[_]path($target);

我知道我可以使用M-f M-b,但据我所知,这不应该是必要的,我想改变它。特别是,我想要两件事:

  1. 当我按下M-f时,我想转到下一个单词的第一个字符,无论该点是在一个单词内,在一组空格内还是在其他地方。
  2. 逐个模式自定义单词字符。毕竟,在CPerl模式下移动与TeX模式不同。
  3. 因此,在上面的例子中,第1项将使光标在击中M-f后移动到'a'(以及它左边的点)。第2项允许我将下划线和符号定义为单词字符。

4 个答案:

答案 0 :(得分:31)

尝试:

(require 'misc)

然后使用M-x forward-to-word并查看它是否符合您的要求。然后,您可以重新绑定M-f等。

使_不是一个单词分隔符(即使其成为单词成分),用于C&amp; C ++模式,你会这样做:

(modify-syntax-entry ?_ "w" c-mode-syntax-table)
(modify-syntax-entry ?_ "w" c++-mode-syntax-table)

有关语法表的更多信息,请阅读this wiki page。语法表通常命名为tex-mode-syntax-tablecperl-mode-syntax-table

答案 1 :(得分:3)

请参阅forward-same-syntax函数。可能这是你需要的基础。

答案 2 :(得分:2)

我有一个次要模式,可以更改基于字的命令来操作语法更改(以及CamelCaseSubwords)。对于某些品味来说,它可能有点过于细粒度,但我发现我基本上不再使用单一角色运动了。

https://bitbucket.org/jpkotta/syntax-subword

答案 3 :(得分:0)

我想复制我之前编辑的行为,因此需要更多的控制权,所以这就是我对它的看法:

(defun my-syntax-class (char)
  "Return ?s, ?w or ?p depending or whether CHAR is a white-space, word or punctuation character."
  (pcase (char-syntax char)
      (`?\s ?s)
      (`?w ?w)
      (`?_ ?w)
      (_ ?p)))

(defun my-forward-word (&optional arg)
  "Move point forward a word (simulate behavior of Far Manager's editor).
With prefix argument ARG, do it ARG times if positive, or move backwards ARG times if negative."
  (interactive "^p")
  (or arg (setq arg 1))
  (let* ((backward (< arg 0))
         (count (abs arg))
         (char-next
          (if backward 'char-before 'char-after))
         (skip-syntax
          (if backward 'skip-syntax-backward 'skip-syntax-forward))
         (skip-char
          (if backward 'backward-char 'forward-char))
         prev-char next-char)
    (while (> count 0)
      (setq next-char (funcall char-next))
      (loop
       (if (or                          ; skip one char at a time for whitespace,
            (eql next-char ?\n)         ; in order to stop on newlines
            (eql (char-syntax next-char) ?\s))
           (funcall skip-char)
         (funcall skip-syntax (char-to-string (char-syntax next-char))))
       (setq prev-char next-char)
       (setq next-char (funcall char-next))
       ;; (message (format "Prev: %c %c %c Next: %c %c %c"
       ;;                   prev-char (char-syntax prev-char) (my-syntax-class prev-char)
       ;;                   next-char (char-syntax next-char) (my-syntax-class next-char)))
       (when
           (or
            (eql prev-char ?\n)         ; stop on newlines
            (eql next-char ?\n)
            (and                        ; stop on word -> punctuation
             (eql (my-syntax-class prev-char) ?w)
             (eql (my-syntax-class next-char) ?p))
            (and                        ; stop on word -> whitespace
             this-command-keys-shift-translated ; when selecting
             (eql (my-syntax-class prev-char) ?w)
             (eql (my-syntax-class next-char) ?s))
            (and                        ; stop on whitespace -> non-whitespace
             (not backward)             ; when going forward
             (not this-command-keys-shift-translated) ; and not selecting
             (eql (my-syntax-class prev-char) ?s)
             (not (eql (my-syntax-class next-char) ?s)))
            (and                        ; stop on non-whitespace -> whitespace
             backward                   ; when going backward
             (not this-command-keys-shift-translated) ; and not selecting
             (not (eql (my-syntax-class prev-char) ?s))
             (eql (my-syntax-class next-char) ?s))
            )
         (return))
       )
      (setq count (1- count)))))

(defun delete-word (&optional arg)
  "Delete characters forward until encountering the end of a word.
With argument ARG, do this that many times."
  (interactive "p")
  (delete-region (point) (progn (my-forward-word arg) (point))))

(defun backward-delete-word (arg)
  "Delete characters backward until encountering the beginning of a word.
With argument ARG, do this that many times."
  (interactive "p")
  (delete-word (- arg)))

(defun my-backward-word (&optional arg)
  (interactive "^p")
  (or arg (setq arg 1))
  (my-forward-word (- arg)))

(global-set-key (kbd "C-<left>") 'my-backward-word)
(global-set-key (kbd "C-<right>") 'my-forward-word)
(global-set-key (kbd "C-<delete>") 'delete-word)
(global-set-key (kbd "C-<backspace>") 'backward-delete-word)