2009-11-20 13 views
19

如標題所示,如何更改emacs轉發字功能的行爲?例如,假設[]是遊標。然後:更改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模式不同。

所以,在上面的例子中,項目1會讓光標在碰到M-f後移動到'a'(以及它的左邊的點)。第2項將允許我將下劃線和標記定義爲單詞字符。

+0

爲下劃線部分,請參見http://計算器.com/questions/1545851/how-to-make-forward-word-backward-word-treat-undercore-as-part-of-word-1545934 – Bahbar 2009-11-20 15:55:36

回答

30

嘗試:

(require 'misc) 

然後使用M-x forward-to-word,看看它是否你想要做什麼。然後,您可以重新綁定M-f

爲了使_一言不分離(即使它成爲一個字組成)對C & 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

+0

是否有(x)Emacs的特定版本,其中' (require'misc)'會將'_'字符移動到語法表中的空白處?我必須做'(modify-syntax-entry?_「 - 」)'才能使它工作。 – sameers 2015-09-25 21:54:59

+0

我現在在OSX上使用GNU Emacs 24.5,我不知道我是否曾經嘗試過這種方式,但添加了對'.emacs'的'modify-syntax-entry'調用不設置表項。當我在迷你緩衝區中輸入它時,它會起作用。任何想法如何使它在所有模式下工作? '(modify-syntax-entry?_「w」standard-syntax-table)'說'standard-syntax-table'是一個void-definition符號。 – sameers 2016-03-11 23:02:17

+0

這會導致'24.5.1'中的嚴重滯後問題。 – Zelphir 2017-02-23 19:26:22

2

另請參閱forward-same-syntax函數。可能這是你需要基於。

+0

非常感謝!我沒有意識到這一點,它效果很好!我發現這捆綁在一起 - 移動/殺死前進/後退:https://github.com/nixme/.emacs.d/blob/master/core/init-keybindings.el – fikovnik 2017-04-06 11:41:17

1

我有一個較小的模式,改變了基於字的命令來操作語法變化(也包括CamelCaseSubwords)。對於某些口味來說它可能有點過於細化,但我發現我基本上已經不再使用單個角色動作了。

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

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)