J'ai récemment ajouté la fonction Emacs (delete-trailing-whitespace)
à mon 'before-save-hook
pour certains modes de programmation, mais je trouve plutôt frustrant de supprimer des espaces de la ligne que je suis en train d'éditer. Des suggestions sur la façon de résoudre ce problème?emacs delete-trailing-whitespace sauf la ligne actuelle
Répondre
Depuis delete-trailing-whitespace
égards rétrécissement, une solution consiste à réduire la mémoire tampon à la partie avant la ligne courante et l'appeler, puis étroite à la partie après la ligne en cours et d'appeler à 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))))))
Mettez cette fonction sur votre before-save-hook
au lieu de delete-trailing-whitespace
.
Fonctionne parfaitement, merci! – pariser
Cette enveloppe pour delete-trailing-whitespace
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))))
je suis tombé sur le même problème, et a trouvé que ws-butler résout parfaitement ce. Il est un simple exemple de code de configuration:
;; autoload ws-butler on file open
(add-hook 'find-file-hook #'ws-butler-global-mode)
(setq require-final-newline t)
J'ai simplement un wrapper pour faire deux appels à `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)
Je dois dire que je ne comprends pas pourquoi vous vouloir préserver les espaces de fin sur la ligne courante. – offby1
La justification: quand je suis en train d'éditer un fichier, je sauvegarde mon document plutôt compulsivement. Si je commence à taper "print" et ensuite enregistrer mon tampon, la ligne se rétrécit à "print" et le curseur se rétracte, me forçant à taper un autre espace! – pariser