2010-02-12 15 views

Odpowiedz

12

Usuwa 4 spacje z przodu bieżącej linii (pod warunkiem, że istnieją spacje).

(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 ""))))) 

Jeśli zmienna tab-width dzieje się 4 (i to, co chcesz „cofnąć”), a następnie można wymienić (looking-at "^ ") z czymś bardziej ogólnym jak (concat "^" (make-string tab-width ?\)).

Ponadto kod przekształci zakładki z przodu wiersza w spacje, używając untabify.

+1

Nice! Ale użyłbym WHEN zamiast IF, ponieważ nie ma drugiej klauzuli. – Ken

+1

dla mnie nie działa z wybranym regionem. Tylko z pierwszą linią: -/ – Ismael

+0

Dla mnie to w ogóle nie działa. :( –

5

Zrobiłem pewne funkcje tabbing region na cztery przestrzenie w lewo lub w prawo w zależności od przypadku korzystania z karty lub Shift + Tab:

(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: Kod Added Maven do sprawdź, czy w minibuforze, jak również w przypadku uzupełniania kart w określonych buforach (na przykład powłoka).

+1

Przyjemnie, zaakceptowana odpowiedź działa tylko dla 1 linii, a nie dla regionów –

+1

Idealny. Działa dla regionów i linii Replikuje inne wspólne funkcje edytora. – mythicalcoder

1

Ulepszyłem odpowiedź Stanleya Baka. Dla mnie to powiązanie z kluczem globalnym komplikowało proces uzupełniania minibufów.

Tak więc włączyłem także walizkę do minibufora.

Edytuj: Zmienianie nazwy indent-region ->indent-region-custom.

indent-region koliduje z istniejącym poleceniem i podaje błąd wcięcia przy składowaniu (przechwyceniu przed zapisaniem) i kilku innych kombinacjach klawiszy.

(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) 
Powiązane problemy