* 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
* Re: Electric indentation sub-optimality and resolution
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 20:07 ` Dmitry Gutov
0 siblings, 2 replies; 8+ messages in thread
From: Óscar Fuentes @ 2014-12-18 18:34 UTC (permalink / raw)
To: emacs-devel
John Yates <john@yates-sheets.org> writes:
> 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.
Annoying indeed. For me this was fixed by ws-butler:
https://github.com/lewang/ws-butler
It has the advantage of cleaning up whitespace no matter how it was
created, but only on the lines you changed or added.
[snip]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Electric indentation sub-optimality and resolution
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
1 sibling, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2014-12-18 19:13 UTC (permalink / raw)
To: Óscar Fuentes; +Cc: emacs-devel
> Annoying indeed. For me this was fixed by ws-butler:
> https://github.com/lewang/ws-butler
> It has the advantage of cleaning up whitespace no matter how it was
> created, but only on the lines you changed or added.
AFAICT the code that John posted has the same property, more or less.
In any case, I'd be happy to integrate something like that in Emacs.
Stefan
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Electric indentation sub-optimality and resolution
2014-12-18 19:13 ` Stefan Monnier
@ 2014-12-18 19:40 ` Óscar Fuentes
0 siblings, 0 replies; 8+ messages in thread
From: Óscar Fuentes @ 2014-12-18 19:40 UTC (permalink / raw)
To: emacs-devel
Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> Annoying indeed. For me this was fixed by ws-butler:
>> https://github.com/lewang/ws-butler
>> It has the advantage of cleaning up whitespace no matter how it was
>> created, but only on the lines you changed or added.
>
> AFAICT the code that John posted has the same property, more or less.
The author of ws-butler experimented with a post-command-hook approach
and experienced some undesirable side effects. There is a commentary
about this on the posted URL.
> In any case, I'd be happy to integrate something like that in Emacs.
Ok, I'll pass the message to Le Wang.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Electric indentation sub-optimality and resolution
2014-12-18 18:34 ` Óscar Fuentes
2014-12-18 19:13 ` Stefan Monnier
@ 2014-12-18 20:07 ` Dmitry Gutov
2014-12-19 0:43 ` John Yates
1 sibling, 1 reply; 8+ messages in thread
From: Dmitry Gutov @ 2014-12-18 20:07 UTC (permalink / raw)
To: Óscar Fuentes; +Cc: emacs-devel
Óscar Fuentes <ofv@wanadoo.es> writes:
> Annoying indeed. For me this was fixed by ws-butler:
>
> https://github.com/lewang/ws-butler
Personally, I like this lighter-weight approach:
https://github.com/purcell/whitespace-cleanup-mode
Admittedly, it's only a good solution when the fraction of files with
pre-existing trailing whitespace that you have to edit is small.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Electric indentation sub-optimality and resolution
2014-12-18 20:07 ` Dmitry Gutov
@ 2014-12-19 0:43 ` John Yates
2014-12-19 1:08 ` Óscar Fuentes
0 siblings, 1 reply; 8+ messages in thread
From: John Yates @ 2014-12-19 0:43 UTC (permalink / raw)
To: Dmitry Gutov; +Cc: Óscar Fuentes, Emacs developers
[-- Attachment #1: Type: text/plain, Size: 1197 bytes --]
On Thu, Dec 18, 2014 at 3:07 PM, Dmitry Gutov <dgutov@yandex.ru> wrote:
>
> Personally, I like this lighter-weight approach:
>
> https://github.com/purcell/whitespace-cleanup-mode
In my case I have show-trailing-whitespace set and my trailing-whitespace
face is "red3". That made it very painful to insert blank lines in the
presence of cc-mode's new electric indentation, even when some other
mechanism / package / mode ultimately was going to clean things up
(assuming I saved the file). Until I did a save my new electric whitespace
messes were "in my face".
Steve Purcell's approach works only if one's file is pristine. Prior to
the introduction of electric indentation I had no need of such a package: I
was careful not to introduce inappropriate whitespace. But since I
regularly have to work on files where other people have been less
fastidious Steve's package would not clean these electric whitespace messes.
I actually really like the behavior of the code I posted. It has an added
small bonus. Normally it does not cleanup pre-existing whitespace. When I
modify a line with trailing whitespace it does cleanup that one line,
something my projects' guidelines allow.
/john
[-- Attachment #2: Type: text/html, Size: 1734 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Electric indentation sub-optimality and resolution
2014-12-19 0:43 ` John Yates
@ 2014-12-19 1:08 ` Óscar Fuentes
2014-12-21 0:36 ` Le Wang
0 siblings, 1 reply; 8+ messages in thread
From: Óscar Fuentes @ 2014-12-19 1:08 UTC (permalink / raw)
To: emacs-devel
John Yates <john@yates-sheets.org> writes:
> On Thu, Dec 18, 2014 at 3:07 PM, Dmitry Gutov <dgutov@yandex.ru> wrote:
>>
>> Personally, I like this lighter-weight approach:
>>
>> https://github.com/purcell/whitespace-cleanup-mode
>
>
> In my case I have show-trailing-whitespace set and my trailing-whitespace
> face is "red3". That made it very painful to insert blank lines in the
> presence of cc-mode's new electric indentation, even when some other
> mechanism / package / mode ultimately was going to clean things up
> (assuming I saved the file). Until I did a save my new electric whitespace
> messes were "in my face".
I had a similar setup here, but after using ws-butler it made no sense
anymore, since that package ensures that the file does not end with
newly added trailing whitespace.
> Steve Purcell's approach works only if one's file is pristine.
Yes, that disqualifies his package for me.
> Prior to
> the introduction of electric indentation I had no need of such a package: I
> was careful not to introduce inappropriate whitespace. But since I
> regularly have to work on files where other people have been less
> fastidious Steve's package would not clean these electric whitespace messes.
>
> I actually really like the behavior of the code I posted. It has an added
> small bonus. Normally it does not cleanup pre-existing whitespace. When I
> modify a line with trailing whitespace it does cleanup that one line,
> something my projects' guidelines allow.
That's what ws-butler does.
OTOH, your code is ok for adding it to the user's personal .emacs, but
not for incorporation into Emacs. The code should be made into a minor
mode or an optional feature of electric-indent-mode. Creating a new
minor mode makes more sense, IMO.
Anyways, we need to hear the opinion of the author of ws-butler package.
AFAIK he tried a similar approach to yours and found problems with it.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Electric indentation sub-optimality and resolution
2014-12-19 1:08 ` Óscar Fuentes
@ 2014-12-21 0:36 ` Le Wang
0 siblings, 0 replies; 8+ messages in thread
From: Le Wang @ 2014-12-21 0:36 UTC (permalink / raw)
To: Óscar Fuentes; +Cc: emacs-devel
[-- Attachment #1: Type: text/plain, Size: 1889 bytes --]
On Thu, Dec 18, 2014 at 8:08 PM, Óscar Fuentes <ofv@wanadoo.es> wrote:
>
> > I actually really like the behavior of the code I posted. It has an
> added
> > small bonus. Normally it does not cleanup pre-existing whitespace.
> When I
> > modify a line with trailing whitespace it does cleanup that one line,
> > something my projects' guidelines allow.
>
If you work on large projects with a mishmash of participants, this feature
is essential. It's really distracting from the actual work to always have
your diffs show unrelated edits.
That's what ws-butler does.
>
Indeed.
OTOH, your code is ok for adding it to the user's personal .emacs, but
> not for incorporation into Emacs. The code should be made into a minor
> mode or an optional feature of electric-indent-mode. Creating a new
> minor mode makes more sense, IMO.
>
"ws-trim" also takes the post-command-hook approach, and it's a full
minor-mode. I found this more active whitespace cleanup distracting. For
example, if I page-up to read some contextual code and page-down, my
indentation is gone if I was on a blank line.
> Anyways, we need to hear the opinion of the author of ws-butler package.
> AFAIK he tried a similar approach to yours and found problems with it.
>
Let's get some historical context by reading this thread:
https://lists.gnu.org/archive/html/emacs-devel/2003-02/msg00018.html
ws-trim was proposed there, and apparently it didn't go forward.
ws-butler was made as a quick experiment to see if less obtrusive
whitespace management was possible by reusing the highlight-changes-mode
mechanisms. If, it was to be included in Emacs, maybe the feature should
be rewritten to independent of highlight-changes-mode. As it stands, one
glaring deficiency is that you cannot use highlight-changes-mode when
using ws-butler-mode.
--
Le
[-- Attachment #2: Type: text/html, Size: 3198 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).