[PATCH] Trim trailing whitespace in vim and emacs

Started by David Fetterover 7 years ago12 messages
#1David Fetter
david@fetter.org

Folks,

Here's my attempt to $subject. I've tested with vim, but I'm much less
certain I got the EMACS code right.

Is there any interest in such a feature?

Best,
David.
---
.dir-locals.el | 3 ++-
src/tools/editors/vim.samples | 1 +
2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/.dir-locals.el b/.dir-locals.el
index 9525d6b604..858461e6bd 100644
--- a/.dir-locals.el
+++ b/.dir-locals.el
@@ -4,7 +4,8 @@
             (c-file-style . "bsd")
             (fill-column . 78)
             (indent-tabs-mode . t)
-            (tab-width . 4)))
+            (tab-width . 4)
+			(lambda () (add-to-list 'write-file-functions 'delete-trailing-whitespace)))))
  (dsssl-mode . ((indent-tabs-mode . nil)))
  (nxml-mode . ((indent-tabs-mode . nil)))
  (perl-mode . ((perl-indent-level . 4)
diff --git a/src/tools/editors/vim.samples b/src/tools/editors/vim.samples
index ccbc93f69b..f5e2181bc3 100644
--- a/src/tools/editors/vim.samples
+++ b/src/tools/editors/vim.samples
@@ -11,6 +11,7 @@
 :  set cinoptions=(0
 :  set tabstop=4
 :  set shiftwidth=4
+:  autocmd FileType asm,c,conf,sql,xml,xs,xslt,yacc autocmd BufWritePre <buffer> %s/\s\+$//e

:endif

--
2.17.1

--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

#2Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: David Fetter (#1)
Re: [PATCH] Trim trailing whitespace in vim and emacs

On 2018-Jun-05, David Fetter wrote:

Hi David

Here's my attempt to $subject. I've tested with vim, but I'm much less
certain I got the EMACS code right.

Is there any interest in such a feature?

I'd rather have the editor warn me (highlight) such things rather than
fix them silently (I wonder if it'd cause a mess with regression .out
files for example, which I do edit on occasion).

I once tried to have vim highlight trailing whitespace, spaces before
tabs, and overlength lines (>80 chars), and while I could do each thing
in isolation, I wasn't able to get it to highlight the three of them at
the same time. If you have a recipe for that, I welcome it.

--
�lvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Alvaro Herrera (#2)
Re: [PATCH] Trim trailing whitespace in vim and emacs

Alvaro Herrera <alvherre@2ndquadrant.com> writes:

On 2018-Jun-05, David Fetter wrote:

Is there any interest in such a feature?

I'd rather have the editor warn me (highlight) such things rather than
fix them silently (I wonder if it'd cause a mess with regression .out
files for example, which I do edit on occasion).

Yeah, agreed. FWIW, I'm not that fussed about this for .h/.c files,
since pgindent will fix it sooner or later (or even right away, if the
committer is anal enough to pgindent before committing, as some of us
are). It's a bit more of a problem for other file types.

I note that Peter E. seems to have a recipe for finding such issues,
which I suspect is grounded in some obscure git feature or other.
That might be easier to work with, since you'd only need one fix
not one per editor.

regards, tom lane

#4David Fetter
david@fetter.org
In reply to: Alvaro Herrera (#2)
Re: [PATCH] Trim trailing whitespace in vim and emacs

On Tue, Jun 05, 2018 at 01:28:54PM -0400, Alvaro Herrera wrote:

On 2018-Jun-05, David Fetter wrote:
Hi David

Here's my attempt to $subject. I've tested with vim, but I'm much less
certain I got the EMACS code right.

Is there any interest in such a feature?

I'd rather have the editor warn me (highlight) such things rather than
fix them silently (I wonder if it'd cause a mess with regression .out
files for example, which I do edit on occasion).

For what it's worth, the patch trims trailing whitespace only on
certain types of files in vim and just one (C) in emacs for just this
reason.

I once tried to have vim highlight trailing whitespace, spaces before
tabs, and overlength lines (>80 chars), and while I could do each thing
in isolation, I wasn't able to get it to highlight the three of them at
the same time. If you have a recipe for that, I welcome it.

In vim, one way to do it is:

highlight link sensibleWhitespaceError Error
autocmd Syntax * syntax match sensibleWhitespaceError excludenl /\s\+\%#\@<!$\| \+\ze\t|^.\{81,}/ display containedin=ALL

I'm sure there's something more elegant in emacs, but I'm not sure
what it is.

Best,
David.
--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

#5Teodor Sigaev
teodor@sigaev.ru
In reply to: Alvaro Herrera (#2)
1 attachment(s)
Re: [PATCH] Trim trailing whitespace in vim and emacs

I once tried to have vim highlight trailing whitespace, spaces before
tabs, and overlength lines (>80 chars), and while I could do each thing
in isolation, I wasn't able to get it to highlight the three of them at
the same time. If you have a recipe for that, I welcome it.

I use FileStly plugin to vim [1]https://www.vim.org/scripts/script.php?script_id=5065 -- Teodor Sigaev E-mail: teodor@sigaev.ru WWW: http://www.sigaev.ru/. But I slightly modify it, see in attachment.
And addition in .vimrc:

if expand('%:e') == "c" || expand('%:e') == "h" || expand('%:e') == "cpp" ||
expand('%:e') == "m" || expand('%:e') == "hpp" || expand('%:e') == "pl" ||
expand('%:e') == "pm" || expand('%:e') == "y" || expand('%:e') == "l"
else
let g:filestyle_plugin = 1
endif

[1]: https://www.vim.org/scripts/script.php?script_id=5065 -- Teodor Sigaev E-mail: teodor@sigaev.ru WWW: http://www.sigaev.ru/
--
Teodor Sigaev E-mail: teodor@sigaev.ru
WWW: http://www.sigaev.ru/

Attachments:

filestyle.vimtext/plain; charset=UTF-8; name=filestyle.vimDownload
#6Teodor Sigaev
teodor@sigaev.ru
In reply to: Teodor Sigaev (#5)
Re: [PATCH] Trim trailing whitespace in vim and emacs

I use FileStly plugin to vim [1]. But I slightly modify it,О©╫ see in attachment.

FileStyle, sorry.

--
Teodor Sigaev E-mail: teodor@sigaev.ru
WWW: http://www.sigaev.ru/

#7Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: David Fetter (#1)
Re: [PATCH] Trim trailing whitespace in vim and emacs

On 6/5/18 13:22, David Fetter wrote:

diff --git a/.dir-locals.el b/.dir-locals.el
index 9525d6b604..858461e6bd 100644
--- a/.dir-locals.el
+++ b/.dir-locals.el
@@ -4,7 +4,8 @@
(c-file-style . "bsd")
(fill-column . 78)
(indent-tabs-mode . t)
-            (tab-width . 4)))
+            (tab-width . 4)
+			(lambda () (add-to-list 'write-file-functions 'delete-trailing-whitespace)))))
(dsssl-mode . ((indent-tabs-mode . nil)))
(nxml-mode . ((indent-tabs-mode . nil)))
(perl-mode . ((perl-indent-level . 4)

dir-locals doesn't work this way. It's not a general lisp file.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#8Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Tom Lane (#3)
Re: [PATCH] Trim trailing whitespace in vim and emacs

On 6/5/18 13:37, Tom Lane wrote:

I note that Peter E. seems to have a recipe for finding such issues,
which I suspect is grounded in some obscure git feature or other.
That might be easier to work with, since you'd only need one fix
not one per editor.

I have a git alias:

check-whitespace = !git diff-tree --check $(git hash-object -t tree
/dev/null) HEAD ${1:-$GIT_PREFIX}

Also, in Emacs I use

(global-whitespace-mode 1)
(setq whitespace-style
'(face
trailing empty indentation space-after-tab space-before-tab))

With source code other than PostgreSQL, this will occasionally result in
angry fruit salad.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#9Matthew Woodcraft
matthew@woodcraft.me.uk
In reply to: David Fetter (#1)
Re: [PATCH] Trim trailing whitespace in vim and emacs

On 2018-06-05 18:22, David Fetter wrote:

Folks,

Here's my attempt to $subject. I've tested with vim, but I'm much less
certain I got the EMACS code right.

Is there any interest in such a feature?

A few days ago there was a suggestion on this list to add a
.editorconfig file specifying tab width:
/messages/by-id/87efhuz9be.fsf@news-spur.riddles.org.uk

The .editorconfig format also supports a trim_trailing_whitespace option
(see https://editorconfig.org/ ).

So that might be a simpler way to achieve this (I suppose people who
don't want this behaviour just wouldn't configure their editors to
honour .editorconfig files).

-M-

#10Andrew Gierth
andrew@tao11.riddles.org.uk
In reply to: Peter Eisentraut (#7)
Re: [PATCH] Trim trailing whitespace in vim and emacs

"Peter" == Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:

+ (lambda () (add-to-list 'write-file-functions 'delete-trailing-whitespace)))))

Peter> dir-locals doesn't work this way. It's not a general lisp file.

Right. The correct approach is

+ (eval add-hook 'before-save-hook 'delete-trailing-whitespace nil t)))

(see the value of safe-local-eval-forms for why)

--
Andrew (irc:RhodiumToad)

#11Andrew Gierth
andrew@tao11.riddles.org.uk
In reply to: Matthew Woodcraft (#9)
Re: [PATCH] Trim trailing whitespace in vim and emacs

"Matthew" == Matthew Woodcraft <matthew@woodcraft.me.uk> writes:

Matthew> A few days ago there was a suggestion on this list to add a
Matthew> .editorconfig file specifying tab width:
Matthew> /messages/by-id/87efhuz9be.fsf@news-spur.riddles.org.uk

Matthew> The .editorconfig format also supports a
Matthew> trim_trailing_whitespace option (see https://editorconfig.org/
Matthew> ).

Yes. I was actually planning to ask if any .editorconfig users (of which
I'm not one - my only use for it is to improve display output on github)
would like to contribute the most useful content for such a file.

--
Andrew (irc:RhodiumToad)

#12Andrew Dunstan
andrew.dunstan@2ndquadrant.com
In reply to: Andrew Gierth (#11)
Re: [PATCH] Trim trailing whitespace in vim and emacs

On 06/05/2018 05:33 PM, Andrew Gierth wrote:

"Matthew" == Matthew Woodcraft <matthew@woodcraft.me.uk> writes:

Matthew> A few days ago there was a suggestion on this list to add a
Matthew> .editorconfig file specifying tab width:
Matthew> /messages/by-id/87efhuz9be.fsf@news-spur.riddles.org.uk

Matthew> The .editorconfig format also supports a
Matthew> trim_trailing_whitespace option (see https://editorconfig.org/
Matthew> ).

Yes. I was actually planning to ask if any .editorconfig users (of which
I'm not one - my only use for it is to improve display output on github)
would like to contribute the most useful content for such a file.

I'm not one either (yet!) but it seems to me like something worth doing.
It seems to support an impressively large number of editors and display
mechanisms, and adding a single small file to support it doesn't seem
onerous.

cheers

andrew

--
Andrew Dunstan https://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services