9
votes

Emacs Supprimer-Trailing-Whitespace sauf la ligne actuelle

J'ai récemment ajouté EMACS (Supprimer-Trailing-Whitespace) FONCTION À MON 'AVANT-SAVE-SCHOCK Pour certains modes de programmation, mais je trouve cela plutôt frustrant que cela Supprime Whitespace de la ligne Je suis en train de modifier actuellement. Toute suggestion sur la manière de résoudre ce problème?


2 commentaires

Je dois dire que je ne comprends pas pourquoi vous voulez préserver les espaces suivants sur la ligne actuelle.


La justification: quand je suis au milieu d'une modification d'un fichier, je sauvegarde plutôt mon document de manière computive. Si je commence à taper "Imprimer", puis à enregistrer ma mémoire tampon, la ligne rétrécit sur "Imprimer" et le curseur recule, me forçant à taper un autre espace!


4 Réponses :


10
votes

Etant donné que Supprimer-whitespace-WhitSpace CODE> RESPECTEURS RÉACTEURS, une solution consiste à restreindre la mémoire tampon à la partie avant i> la ligne actuelle et appelez-la, puis étroite à la partie après i> la ligne actuelle et appelez-le à nouveau:

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


0 commentaires

4
votes

Ce wrapper pour Supprimer-whitespace-whitpace code> peut être utilisé pour faire ce que vous voulez:

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


0 commentaires

1
votes

J'ai couru dans le même problème et j'ai découvert que WS-Butler le résout parfaitement. Il existe un simple code de configuration d'échantillon: xxx


0 commentaires

0
votes

J'ai simplement un emballage pour faire deux appels à `Supprimer-Trapping-WhitSpace ':

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


0 commentaires