[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Electric indentation sub-optimality and resolution

From: John Yates
Subject: Electric indentation sub-optimality and resolution
Date: Thu, 18 Dec 2014 11:19:17 -0500

Emacs-24's cc-mode has added electric indentation.  I am sympathetic to the basic motivation.  The current implementation leaves me frustrated.  This is because electric indentation introduces lines with trailing whitespace when one adds no actual text to an indented new line.

Disclaimer: I am C++ programmer with minimal elisp experience.  That said, starting from a sketch provided by Alan Mackenzie, I have come up with the following.  It cleans trailing whitespace _only_ from modified lines.

(defvar my/modified-line nil)

(defun my/modified-line-note (beg end old-len)
  (let ((bol (line-beginning-position)))
    (if (/= (point) bol)
        (setq-local my/modified-line bol))))

(defun my/modified-line-cleanup-after-leaving ()
  (if my/modified-line
        (when (/= (point) my/modified-line)
          (goto-char my/modified-line)
          (while (memq (char-before) '(?\  ?\t))
            (delete-char -1))
          (setq-local my/modified-line nil)))))

(defun my/modified-line-first-change ()
  (add-hook 'post-command-hook 'my/modified-line-cleanup-after-leaving t))

(add-hook 'after-change-functions 'my/modified-line-note)
(add-hook 'first-change-hook 'my/modified-line-first-change)

I make no claim that this is a good solution to the problem.  It merely demonstrates that electric indentation can be made more compatible with whitespace hygiene.  Further since I merely tweaked Alan's untested sketch I claim no ownership in the code.  Do with it as you choose.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]