DrRacket plugin to remove trailing whitespace on save?

77 views
Skip to first unread message

Jon Zeppieri

unread,
Apr 12, 2015, 6:01:57 PM4/12/15
to Racket Users
Does such a think already exist?

-Jon

Robby Findler

unread,
Apr 12, 2015, 6:14:15 PM4/12/15
to Jon Zeppieri, Racket Users
No but Max changed the way return works so there should be less whitespace added going forward. 

Writing a script to trim whitespace from line-endings would work well if it were to use text% IMO. Use load-file to get a file and then the paragraph methods to find line endings and then delete stuff and ace the file again. 

Robby


On Sunday, April 12, 2015, Jon Zeppieri <zepp...@gmail.com> wrote:
Does such a think already exist?

-Jon

--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jon Zeppieri

unread,
Apr 12, 2015, 7:11:51 PM4/12/15
to Alexis King, Robby Findler, Racket Users
If you mean: "don't remove entire lines that consist of only
whitespace," then I agree.
If you mean: "don't remove any trailing whitespace from lines that
consist only of whitespace," then I do not.

-Jon


On Sun, Apr 12, 2015 at 7:01 PM, Alexis King <lexi....@gmail.com> wrote:
> If you decide to do this, please make it so it doesn’t trim whitespace from
> whitespace-only lines. For some reason a lot of IDEs do this, and it’s a pet
> peeve of mine.
>
> On Apr 12, 2015, at 15:14, Robby Findler <ro...@eecs.northwestern.edu>

Jon Zeppieri

unread,
Apr 13, 2015, 5:29:43 AM4/13/15
to Robby Findler, Racket Users
Robby,

This is my first crack at a DrRacket tool (not to mention an uncommon
foray into Racket's class system), so what I'm doing may not be...
sane. But if you're willing to take a look:
[https://github.com/97jaz/drwhitespace]. At any rate, it isn't a lot
of code. I based the general structure on Asumu Takikawa's
drracket-vim-tool.

It's supposed to have the same effect as emacs's `delete-trailing-whitespace`.
I still need to add a preference setting to turn it on or off. (Maybe
that should be based on the editor's mode?)

-Jon

'John Clements' via users-redirect

unread,
Apr 13, 2015, 1:06:26 PM4/13/15
to Jon Zeppieri, Robby Findler, Racket Users

On Apr 13, 2015, at 2:29 AM, Jon Zeppieri <zepp...@gmail.com> wrote:

> Robby,
>
> This is my first crack at a DrRacket tool (not to mention an uncommon
> foray into Racket's class system), so what I'm doing may not be...
> sane. But if you're willing to take a look:
> [https://github.com/97jaz/drwhitespace]. At any rate, it isn't a lot
> of code. I based the general structure on Asumu Takikawa's
> drracket-vim-tool.
>
> It's supposed to have the same effect as emacs's `delete-trailing-whitespace`.
> I still need to add a preference setting to turn it on or off. (Maybe
> that should be based on the editor's mode?)

I’d really like to use this tool.

Here’s one problem that I see: there’s a hidden invariant in DrRacket (and all editors) that when a buffer is unchanged, saving it won’t change the file on disk. Actually, I think you can state this in a bunch of different ways. I forsee hard-to-understand errors arising from a silent change-on-save (unless I’m misreading your code?). Personally, I think I’d be more likely to use a tool that requests permission to scrub trailing whitespace when saving a file. I also think that this should probably be limited to racket and scribble files, and I’m guessing that the easiest way to distinguish these would be to use the filename extension.

Please don’t let me stop you from doing this, though, this is something I’ve wanted for quite a while!

Many thanks,

John

Robby Findler

unread,
Apr 13, 2015, 1:26:05 PM4/13/15
to John Clements, Jon Zeppieri, Racket Users
You could just make delete-trailing-whitespace a keyboard shortcut instead.

Another approach would be to add a mode that colors trailing
whitespace in that ugly greeny/yellowy color that DrRacket uses for
the "your code is more than 102 columns" warning.

Robby

Jon Zeppieri

unread,
Apr 13, 2015, 1:26:09 PM4/13/15
to John Clements, Robby Findler, Racket Users
On Mon, Apr 13, 2015 at 1:06 PM, John Clements
<clem...@brinckerhoff.org> wrote:
>
> On Apr 13, 2015, at 2:29 AM, Jon Zeppieri <zepp...@gmail.com> wrote:
>
>>
>> It's supposed to have the same effect as emacs's `delete-trailing-whitespace`.
>> I still need to add a preference setting to turn it on or off. (Maybe
>> that should be based on the editor's mode?)
>
> I’d really like to use this tool.
>
> Here’s one problem that I see: there’s a hidden invariant in DrRacket (and all editors) that when a buffer is unchanged, saving it won’t change the file on disk. Actually, I think you can state this in a bunch of different ways. I forsee hard-to-understand errors arising from a silent change-on-save (unless I’m misreading your code?). Personally, I think I’d be more likely to use a tool that requests permission to scrub trailing whitespace when saving a file. I also think that this should probably be limited to racket and scribble files, and I’m guessing that the easiest way to distinguish these would be to use the filename extension.
>
> Please don’t let me stop you from doing this, though, this is something I’ve wanted for quite a while!
>
> Many thanks,
>
> John

Good point. `delete-trailing-whitespace` is just a normal function in
emacs, and if you want this to happen when saving a file, you use a
before-save-hook. (This is a pretty common thing to do, though.)

Exposing this as a method on text% is simple, I think. Then you could
bind a keyboard shortcut to it or bring up a model on save (though
personally I would find the latter pretty irritating). But, yes,
different modes of use are important, I think. So I need to add a way
of setting preferences for it.

-Jon

Robby Findler

unread,
Apr 13, 2015, 1:27:43 PM4/13/15
to Jon Zeppieri, John Clements, Racket Users

Jon Zeppieri

unread,
Apr 13, 2015, 1:34:25 PM4/13/15
to Robby Findler, John Clements, Racket Users
On Mon, Apr 13, 2015 at 1:27 PM, Robby Findler
<ro...@eecs.northwestern.edu> wrote:
> Preferences are here for the prefs library:
>
> http://docs.racket-lang.org/framework/Preferences__Textual.html
>
> and here for adding check boxes to the prefs dialog:
>
> http://docs.racket-lang.org/framework/Preferences.html?q=editor-add#%28def._%28%28lib._framework%2Fmain..rkt%29._preferences~3aadd-to-editor-checkbox-panel%29%29
>
> hth,
> Robby
>

Thanks!

-Jon

'John Clements' via users-redirect

unread,
Apr 13, 2015, 1:51:03 PM4/13/15
to Robby Findler, Jon Zeppieri, Racket Users

On Apr 13, 2015, at 10:26 AM, Robby Findler <ro...@eecs.northwestern.edu> wrote:

> You could just make delete-trailing-whitespace a keyboard shortcut instead.
>
> Another approach would be to add a mode that colors trailing
> whitespace in that ugly greeny/yellowy color that DrRacket uses for
> the "your code is more than 102 columns" warning.

+1 to this. I think I’d definitely like this independent of a mechanism to trim on save.

John

Benjamin Greenman

unread,
Apr 13, 2015, 5:01:20 PM4/13/15
to John Clements, Robby Findler, Jon Zeppieri, Racket Users
On Mon, Apr 13, 2015 at 1:51 PM, 'John Clements' via users-redirect <us...@plt-scheme.org> wrote:

On Apr 13, 2015, at 10:26 AM, Robby Findler <ro...@eecs.northwestern.edu> wrote:

> You could just make delete-trailing-whitespace a keyboard shortcut instead.
>
> Another approach would be to add a mode that colors trailing
> whitespace in that ugly greeny/yellowy color that DrRacket uses for
> the "your code is more than 102 columns" warning.

+1 to this. I think I’d definitely like this independent of a mechanism to trim on save.

+1

Greg Hendershott

unread,
Apr 13, 2015, 8:39:12 PM4/13/15
to Racket Users
>> > Another approach would be to add a mode that colors trailing
>> > whitespace in that ugly greeny/yellowy color that DrRacket uses for
>> > the "your code is more than 102 columns" warning.
>>
>> +1 to this. I think I’d definitely like this independent of a mechanism to
>> trim on save.
>
> +1

Also +1. This is like Emacs' `show-trailing-whitespace', which I
enable for most prog modes.

Things like trailing whitespace, or files not ending with a newline,
cause Git noise. It's nice to clean them up. However I've found it
problematic to change them automagically (and especially on save), as
opposed to me doing it deliberately and with review.

For example from using `show-trailing-whitespace' I can report there's
a lot of it in Racket sources. But it might be nicer to have commits
dedicated to removing it (maybe even for whole repos at once), as
opposed to the cleanups being sprinkled among many commits intended to
accomplish other things? Just a thought.

Neil Van Dyke

unread,
Apr 13, 2015, 9:13:54 PM4/13/15
to Racket Users
After editing a Racket source file in DrRacket, I usually end up going
into Emacs to clean up formatting of the code and comments. This is a
combination of `quack-tidy-buffer` (see docs below), semi-automated
re-filling of any messy comment/docs, adjusting of line breaks in ways
that are easier to do with Emacs's richer editing controls, and any
checking and moving stuff around and such that's easier to
see/manipulate in an Emacs window than a DrRacket window.

It *might* be nice if DrRacket did some no-brainer whitespace cleanup on
manual file saves (e.g., remove trailing whitespace at end of lines and
end of file, force newline at end of file). But, in my case, I'm going
to have to clean up the file in Emacs anyway, so I'm indifferent.

An DrRacket editor indicator of irrelevant whitespace at end of lines
would probably be a distraction that I would disable.

> quack-tidy-buffer is an interactive compiled Lisp function in `quack.el'.
>
> (quack-tidy-buffer)
>
> Tidy the formatting of the current Scheme buffer.
>
> This reindents, converts tabs to spaces, removes trailing whitespace
> on lines,
> removes formfeed characters, removes extraneous blank lines, and makes
> sure
> the buffer ends with a newline.
>
> This can conceivably corrupt multi-line string literals, but not in
> any way
> they wouldn't be corrupted by Usenet, various mailers, typesetting for
> print,
> etc.
>
> This may also result in large diffs when the tidied file is commited
> back to a
> version control or configuration management system. Consider making a
> VC or CM
> delta that consists only of changes made by `quack-tidy-buffer'.

Neil V.

Jon Zeppieri

unread,
Apr 13, 2015, 10:23:03 PM4/13/15
to Neil Van Dyke, Racket Users
So, just to be clear, because there have been a number of +1s for a
whitespace-highlighting feature: that's not what I'm doing. A bunch of
people want that, so someone should write it, but I'm not going in
that direction. (In fact, one of my emacs configurations does this in
ruby-mode, and I keep meaning to find the setting to turn it off.
Whenever the highlight appears, I compulsively save the buffer just to
strip the whitespace and get rid of the highlight.)

-Jon
Reply all
Reply to author
Forward
0 new messages