2010-02-12 7 views

risposta

12

Rimuove 4 spazi dalla parte anteriore della riga corrente (purché gli spazi esistano).

(global-set-key (kbd "<S-tab>") 'un-indent-by-removing-4-spaces) 
(defun un-indent-by-removing-4-spaces() 
    "remove 4 spaces from beginning of of line" 
    (interactive) 
    (save-excursion 
    (save-match-data 
     (beginning-of-line) 
     ;; get rid of tabs at beginning of line 
     (when (looking-at "^\\s-+") 
     (untabify (match-beginning 0) (match-end 0))) 
     (when (looking-at "^ ") 
     (replace-match ""))))) 

Se la variabile tab-width capita di essere 4 (e questo è ciò che si vuole "annullare"), allora si può sostituire il (looking-at "^ ") con qualcosa di più generale come (concat "^" (make-string tab-width ?\)).

Inoltre, il codice convertirà le schede nella parte anteriore della linea in spazi utilizzando untabify.

+1

Bello! Ma userei QUANDO invece di IF, poiché non c'è altra clausola. – Ken

+1

per me, non funziona con una regione selezionata. Solo con la prima riga: -/ – Ismael

+0

Per me, non funziona affatto. :( –

5

ho fatto alcune funzioni per la tabulazione una regione su quattro spazi a destra oa sinistra a seconda se scheda o MAIUSC + TAB si utilizza:

(defun indent-region-custom(numSpaces) 
    (progn 
     ; default to start and end of current line 
     (setq regionStart (line-beginning-position)) 
     (setq regionEnd (line-end-position)) 

     ; if there's a selection, use that instead of the current line 
     (when (use-region-p) 
      (setq regionStart (region-beginning)) 
      (setq regionEnd (region-end)) 
     ) 

     (save-excursion ; restore the position afterwards    
      (goto-char regionStart) ; go to the start of region 
      (setq start (line-beginning-position)) ; save the start of the line 
      (goto-char regionEnd) ; go to the end of region 
      (setq end (line-end-position)) ; save the end of the line 

      (indent-rigidly start end numSpaces) ; indent between start and end 
      (setq deactivate-mark nil) ; restore the selected region 
     ) 
    ) 
) 

(defun untab-region (N) 
    (interactive "p") 
    (indent-region-custom -4) 
) 

(defun tab-region (N) 
    (interactive "p") 
    (if (active-minibuffer-window) 
     (minibuffer-complete) ; tab is pressed in minibuffer window -> do completion 
    ; else 
    (if (string= (buffer-name) "*shell*") 
     (comint-dynamic-complete) ; in a shell, use tab completion 
    ; else 
    (if (use-region-p) ; tab is pressed is any other buffer -> execute with space insertion 
     (indent-region-custom 4) ; region was selected, call indent-region 
     (insert " ") ; else insert four spaces as expected 
    ))) 
) 

(global-set-key (kbd "<backtab>") 'untab-region) 
(global-set-key (kbd "<tab>") 'tab-region) 

Edit: il codice di Aggiunto Maven per controllare se in minibuffer, nonché per il completamento scheda in buffer specifici (shell, ad esempio).

+1

Nice, la risposta accettata funzionava solo per 1 riga, non per le regioni –

+1

Perfetto. Funziona per regioni e linee. Replica le funzionalità di altri editor comuni. – mythicalcoder

1

Ho migliorato la risposta di Stanley Bak. Per me, questa associazione a chiave globale stava rovinando il completamento del minibuffer.

Quindi ho incluso anche una custodia per il minibuffer.

Modifica: Rinomina indent-region ->indent-region-custom.

indent-region si scontrano con il comando esistente e restituisce un errore per il rientro su salvataggio (gancio per il salvataggio precedente) e alcune altre combinazioni di tasti.

(defun indent-region-custom(numSpaces) 
    (progn 
    ;; default to start and end of current line 
    (setq regionStart (line-beginning-position)) 
    (setq regionEnd (line-end-position)) 
    ;; if there's a selection, use that instead of the current line 
    (when (use-region-p) 
     (setq regionStart (region-beginning)) 
     (setq regionEnd (region-end)) 
    ) 

    (save-excursion ; restore the position afterwards 
     (goto-char regionStart) ; go to the start of region 
     (setq start (line-beginning-position)) ; save the start of the line 
     (goto-char regionEnd) ; go to the end of region 
     (setq end (line-end-position)) ; save the end of the line 

     (indent-rigidly start end numSpaces) ; indent between start and end 
     (setq deactivate-mark nil) ; restore the selected region 
    ) 
    ) 
) 

(defun untab-region (N) 
    (interactive "p") 
    (indent-region-custom -4) 
) 

(defun tab-region (N) 
    (interactive "p") 
    (if (active-minibuffer-window) 
     (minibuffer-complete) ; tab is pressed in minibuffer window -> do completion 
    (if (use-region-p) ; tab is pressed is any other buffer -> execute with space insertion 
     (indent-region-custom 4) ; region was selected, call indent-region-custom 
     (insert " ") ; else insert four spaces as expected 
    ) 
    ) 
) 

(global-set-key (kbd "<backtab>") 'untab-region) 
(global-set-key (kbd "<tab>") 'tab-region) 
Problemi correlati