2010-08-20 11 views
7

Recientemente agregué la función Emacs (delete-trailing-whitespace) a mi 'before-save-hook para algunos modos de programación, pero me resulta bastante frustrante que elimine los espacios en blanco de la línea que estoy editando actualmente. ¿Alguna sugerencia sobre cómo solucionar este problema?emacs delete-trailing-whitespace excepto la línea actual

+0

hay que decir que no entiendo por qué desea conservar espacios en blanco al final de la línea actual. – offby1

+0

La justificación: cuando estoy en el medio de editar un archivo, guardo mi documento de forma compulsiva. Si empiezo a escribir "print" y luego guardo mi buffer, la línea se contrae para "imprimir" y el cursor retrocede, ¡forzándome a escribir otro espacio! – pariser

Respuesta

9

Desde delete-trailing-whitespace aspectos estrechamiento, una solución es reducir la memoria intermedia a la parte antes de la línea actual y llamarlo, entonces estrecha a la parte después de la línea actual y llamar de nuevo:

(defun delete-trailing-whitespace-except-current-line() 
    (interactive) 
    (let ((begin (line-beginning-position)) 
     (end (line-end-position))) 
    (save-excursion 
     (when (< (point-min) begin) 
     (save-restriction 
      (narrow-to-region (point-min) (1- begin)) 
      (delete-trailing-whitespace))) 
     (when (> (point-max) end) 
     (save-restriction 
      (narrow-to-region (1+ end) (point-max)) 
      (delete-trailing-whitespace)))))) 

Ponga esta función en su before-save-hook en lugar de delete-trailing-whitespace.

+0

Funciona perfectamente, gracias! – pariser

3

Esta envoltura para delete-trailing-whitespace se puede utilizar para hacer lo que quiera:

(defun delete-trailing-whitespace-except-current-line() 
    "do delete-trailing-whitespace, except preserve whitespace of current line" 
    (interactive) 
    (let ((current-line (buffer-substring (line-beginning-position) (line-end-position))) 
     (backward (- (line-end-position) (point)))) 
    (delete-trailing-whitespace) 
    (when (not (string-equal (buffer-substring (line-beginning-position) (line-end-position)) 
          current-line)) 
     (delete-region (line-beginning-position) (line-end-position)) 
     (insert current-line) 
     (backward-char backward)))) 
1

me encontré con el mismo problema, y ​​descubrí que ws-butler resuelve perfectamente ella. No es un simple código de ejemplo de configuración:

;; autoload ws-butler on file open 
(add-hook 'find-file-hook #'ws-butler-global-mode) 
(setq require-final-newline t) 
0

Simplemente tengo un envoltorio para hacer dos llamadas a `delete-trailing-white-space ':

(defun modi/delete-trailing-whitespace-buffer() 
    "Delete trailing whitespace in the whole buffer, except on the current line. 
The current line exception is because we do want to remove any whitespace 
on the current line on saving the file (`before-save-hook') while we are 
in-between typing something. 

Do not do anything if `do-not-delete-trailing-whitespace' is non-nil." 
    (interactive) 
    (when (not (bound-and-true-p do-not-delete-trailing-whitespace)) 
    (delete-trailing-whitespace (point-min) (line-beginning-position)) 
    (delete-trailing-whitespace (line-end-position) (point-max)))) 
(add-hook 'before-save-hook #'modi/delete-trailing-whitespace-buffer) 
Cuestiones relacionadas