all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: John Yates <john@yates-sheets.org>
To: Emacs developers <emacs-devel@gnu.org>
Subject: Electric indentation sub-optimality and resolution
Date: Thu, 18 Dec 2014 11:19:17 -0500	[thread overview]
Message-ID: <CAJnXXogGnKuCQXZJmujHcv0WqC+kCJmc4zBbJ7ecBrzGTw6v5A@mail.gmail.com> (raw)

[-- 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 --]

             reply	other threads:[~2014-12-18 16:19 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-18 16:19 John Yates [this message]
2014-12-18 18:34 ` Electric indentation sub-optimality and resolution Ó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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAJnXXogGnKuCQXZJmujHcv0WqC+kCJmc4zBbJ7ecBrzGTw6v5A@mail.gmail.com \
    --to=john@yates-sheets.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.