2009-11-20 13 views
19

Come dice il titolo, come si modifica il comportamento della funzione forward-word di emacs? Ad esempio, supponiamo che [] sia il cursore. Poi:Modifica comportamento Emacs Forward-Word

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

So che potrei semplicemente usare M-f M-b, ma per quanto mi riguarda, che non dovrebbe essere necessario, e mi piacerebbe cambiarlo. In particolare, voglio due cose:

  1. Quando premo M-f, voglio andare al primo carattere della parola successiva indipendentemente dal fatto che il punto è all'interno di una parola, all'interno di un gruppo di spazi o da qualche altra parte.
  2. Personalizza i caratteri di parola in base alla modalità. Dopotutto, spostarsi in modalità CPerl è diverso da, ad esempio, la modalità TeX.

Quindi, nell'esempio precedente, l'elemento 1 dovrebbe spostare il cursore su 'a' (e il punto verso sinistra) dopo aver premuto M-f. L'elemento 2 mi consentirà di definire caratteri di sottolineatura e sigilli come caratteri di parole.

+0

per la parte di sottolineatura, vedere http: // StackOverflow .com/questions/1545851/how-to-make-forward-word-backward-word-treat-underscore-as-part-of-a-word/1545934 – Bahbar

risposta

30

Prova:

(require 'misc) 

Quindi utilizzare M-x forward-to-word e vedere se fa ciò che si vuole. È quindi possibile associare nuovamente M-f, ecc

Per rendere la non un separatore _ parola (cioè lo rendono una parola costituente) per C & modalità C++, si dovrebbe fare questo:

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

Per maggiori informazioni su tabelle di sintassi, leggere this wiki page. Le tabelle di sintassi sono generalmente denominate come tex-mode-syntax-table e cperl-mode-syntax-table.

+0

Esiste una versione specifica di (x) Emacs in cui ' (richiedere 'misc) 'sposta il carattere' _' in uno spazio bianco nella tabella della sintassi? Ho dovuto fare '(modifica-sintassi-entry? _" - ")' per farlo funzionare. – sameers

+0

Ora sto usando GNU Emacs 24.5 su OSX, e non so se l'ho provato prima, ma aggiungendo che la chiamata 'modify-syntax-entry' al mio' .emacs' non imposta la voce della tabella. Funziona quando lo digito nel mini-buffer. Qualche idea su come farlo funzionare in tutte le modalità? '(modifica-sintassi-entry? _" w "standard-syntax-table)' dice che 'standard-syntax-table' è un simbolo di definizione del vuoto. – sameers

+0

Ciò può causare seri problemi di ritardo in '24.5.1'. – Zelphir

2

Vedere anche la funzione della sintassi in avanti. probabilmente questo è necessario per basarsi.

+0

Grazie mille! Non ero a conoscenza di questo e funziona benissimo! Ho trovato questo che raggruppa tutti insieme: sposta/uccidi avanti/indietro: https://github.com/nixme/.emacs.d/blob/master/core/init-keybindings.el – fikovnik

1

Ho una modalità secondaria che modifica i comandi basati su parole per operare sulle modifiche della sintassi (e anche su CamelCaseSubwords). Potrebbe essere un po 'troppo raffinato per alcuni gusti, ma trovo che praticamente mai più il movimento di un singolo personaggio.

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

0

ho voluto copiare il comportamento di mio precedente editore quindi bisogno di un po 'più di controllo, ecco il mio prendere su di esso:

(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) 
Problemi correlati