unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Electric indentation sub-optimality and resolution
@ 2014-12-18 16:19 John Yates
  2014-12-18 18:34 ` Óscar Fuentes
  0 siblings, 1 reply; 8+ messages in thread
From: John Yates @ 2014-12-18 16:19 UTC (permalink / raw)
  To: Emacs developers

[-- Attachment #1: Type: text/plain, Size: 1570 bytes --]

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
      (save-excursion
        (beginning-of-line)
        (when (/= (point) my/modified-line)
          (goto-char my/modified-line)
          (end-of-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.

/john

[-- Attachment #2: Type: text/html, Size: 1777 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2014-12-21  0:36 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-18 16:19 Electric indentation sub-optimality and resolution John Yates
2014-12-18 18:34 ` Óscar Fuentes
2014-12-18 19:13   ` Stefan Monnier
2014-12-18 19:40     ` Óscar Fuentes
2014-12-18 20:07   ` Dmitry Gutov
2014-12-19  0:43     ` John Yates
2014-12-19  1:08       ` Óscar Fuentes
2014-12-21  0:36         ` Le Wang

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).