Andrew Appleton | 9 Feb 18:50

Save and strip whitespace

Are there plans to add an option to strip trailing whitespace on save?

I had this set up in TM1 like this http://blogobaggins.com/2009/03/31/waging-war-on-whitespace.html and it would be great to get ti as a feature of TM2.

_______________________________________________
textmate mailing list
textmate@...
http://lists.macromates.com/listinfo/textmate
Martin Kühl | 9 Feb 20:03
Picon
Gravatar

Re: Save and strip whitespace

On Thu, Feb 9, 2012 at 18:50, Andrew Appleton <andrew <at> mintdigital.com> wrote:
> Are there plans to add an option to strip trailing whitespace on save?
>
> I had this set up in TM1 like
> this http://blogobaggins.com/2009/03/31/waging-war-on-whitespace.html and it
> would be great to get ti as a feature of TM2.

You can basically do the same thing. There's a command for stripping
trailing whitespace in the Text bundle, it's called "Remove Trailing
Spaces in Document / Selection". Just duplicate that and set "Save" to
"Current Document".

HTH,
Martin

_______________________________________________
textmate mailing list
textmate <at> lists.macromates.com
http://lists.macromates.com/listinfo/textmate
Steve King | 9 Feb 20:55

Re: Save and strip whitespace

On 2012-02-09 14:03, Martin Kühl wrote:
> On Thu, Feb 9, 2012 at 18:50, Andrew Appleton<andrew <at> mintdigital.com>  wrote:
>> Are there plans to add an option to strip trailing whitespace on save?
>>
>> I had this set up in TM1 like
>> this http://blogobaggins.com/2009/03/31/waging-war-on-whitespace.html and it
>> would be great to get ti as a feature of TM2.
> You can basically do the same thing. There's a command for stripping
> trailing whitespace in the Text bundle, it's called "Remove Trailing
> Spaces in Document / Selection". Just duplicate that and set "Save" to
> "Current Document".

That doesn't quite work.  The file gets saved before the command is 
applied, not after.  But the file is not marked as changed after 
applying the command!  You can quit TM2 and lose the changes made by the 
command.  It's not a big deal for trailing whitespace, but this behavior 
for other commands could lead to real data loss.

To reproduce:

1) Set the "Remove Trailing Spaces" command to save the current 
document. Be sure to save the bundle.
2) Load a document.
3) Add trailing whitespace to some lines.
4) Save the document.
5) Make a change to the document.  The "modified" indicator in the tab 
shows the file is changed.
6) Run "Remove Trailing Spaces."  The "modified" indicator now shows the 
file is unchanged.
7) Quit TM2. You are not asked to save the document.
8) Start TM2.
9) Load the document.  You will see the change you made in step 5, but 
the trailing whitespace is still there.

--

-- 
Steve King
Sr. Software Engineer
Arbor Networks
+1 734 821 1461
www.arbornetworks.com <http://www.arbornetworks.com/>

_______________________________________________
textmate mailing list
textmate <at> lists.macromates.com
http://lists.macromates.com/listinfo/textmate
Martin Kühl | 10 Feb 11:28
Picon
Gravatar

Re: Save and strip whitespace

On Thu, Feb 9, 2012 at 20:55, Steve King <sking <at> arbor.net> wrote:
> On 2012-02-09 14:03, Martin Kühl wrote:
>>
>> On Thu, Feb 9, 2012 at 18:50, Andrew Appleton<andrew <at> mintdigital.com>
>>  wrote:
>>>
>>> Are there plans to add an option to strip trailing whitespace on save?
>>>
>>> I had this set up in TM1 like
>>> this http://blogobaggins.com/2009/03/31/waging-war-on-whitespace.html and
>>> it
>>> would be great to get ti as a feature of TM2.
>>
>> You can basically do the same thing. There's a command for stripping
>> trailing whitespace in the Text bundle, it's called "Remove Trailing
>> Spaces in Document / Selection". Just duplicate that and set "Save" to
>> "Current Document".
>
>
> That doesn't quite work.  The file gets saved before the command is applied,
> not after.  But the file is not marked as changed after applying the
> command!  You can quit TM2 and lose the changes made by the command.  It's
> not a big deal for trailing whitespace, but this behavior for other commands
> could lead to real data loss.

Darn, you're right. We could try a command that saves, then modifies the
contents of $TM_FILEPATH, but that sounds rather brittle and assumes
TM_FILEPATH is even set _before_ the command script runs.

Given that, I second the original feature request. :-)

Martin

_______________________________________________
textmate mailing list
textmate <at> lists.macromates.com
http://lists.macromates.com/listinfo/textmate
John Yeates | 10 Feb 09:48
Picon

Re: Save and strip whitespace

On Thursday February 09 2012, Martin Kühl <martin.kuehl@...> wrote:
> 
> You can basically do the same thing. There's a command for stripping
> trailing whitespace in the Text bundle, it's called "Remove Trailing
> Spaces in Document / Selection". Just duplicate that and set "Save" to
> "Current Document".

Doesn't that save the current document when the command is run, rather than running the command when the
document is saved, though?

What I'm looking for is a way of hooking the Save command so that the whitespace stripper is run just before
saving, regardless of whether I pressed Cmd-S, chose Save from the File menu, or selected Save in the
dialog when closing a document with unsaved changes.

I'm sure I read something on a blog which suggested it was possible, but the article unfortunately assumed a
greater familiarity with bundle writing than I have :)

--

-- 
John Yeates

_______________________________________________
textmate mailing list
textmate@...
http://lists.macromates.com/listinfo/textmate

Martin Kühl | 10 Feb 11:33
Picon
Gravatar

Re: Save and strip whitespace

On Fri, Feb 10, 2012 at 09:48, John Yeates <textmate2 <at> warmvomit.co.uk> wrote:
> On Thursday February 09 2012, Martin Kühl <martin.kuehl <at> gmail.com> wrote:
>>
>> You can basically do the same thing. There's a command for stripping
>> trailing whitespace in the Text bundle, it's called "Remove Trailing
>> Spaces in Document / Selection". Just duplicate that and set "Save" to
>> "Current Document".
>
> Doesn't that save the current document when the command is run, rather
> than running the command when the document is saved, though?

That's correct. The idea is to replace default ⌘S behaviour does with
the command.

> What I'm looking for is a way of hooking the Save command so that the
> whitespace stripper is run just before saving, regardless of whether I
> pressed Cmd-S, chose Save from the File menu, or selected Save in the
> dialog when closing a document with unsaved changes.

I see. Those are definitely not possible with TM1; maybe Allan will
consider adding the necessary hooks to TM2, but designing a good hooking
system takes time.

Martin

_______________________________________________
textmate mailing list
textmate <at> lists.macromates.com
http://lists.macromates.com/listinfo/textmate
Martin Kühl | 12 Feb 09:36
Picon
Gravatar

Re: Save and strip whitespace

On Fri, Feb 10, 2012 at 11:33, Martin Kühl <martin.kuehl <at> gmail.com> wrote:
> I see. Those are definitely not possible with TM1; maybe Allan will
> consider adding the necessary hooks to TM2, but designing a good hooking
> system takes time.

Heh, of course the system is already in place. Actions with the semantic
class "callback.document.did-save" are run on save; maybe there'll be
another class "callback.document.will-save" for things like whitespace
trimming.

Martin

_______________________________________________
textmate mailing list
textmate <at> lists.macromates.com
http://lists.macromates.com/listinfo/textmate
Allan Odgaard | 14 Feb 12:18
Favicon

Re: Save and strip whitespace

On 12/02/2012, at 15.36, Martin Kühl wrote:

> On Fri, Feb 10, 2012 at 11:33, Martin Kühl <martin.kuehl@...> wrote:
>> I see. Those are definitely not possible with TM1; maybe Allan will
>> consider adding the necessary hooks to TM2, but designing a good hooking
>> system takes time.
> 
> Heh, of course the system is already in place. Actions with the semantic
> class "callback.document.did-save" are run on save; maybe there'll be
> another class "callback.document.will-save" for things like whitespace
> trimming.

It can presently be done as an export command, i.e.: callback.document.export

This will however only result in the data on disk getting whitespace stripped.

The will-save makes sense, but I might not add it right away, as I have some refactoring planned with respect
to these things, so prefer not to add more complexity prior to that.

_______________________________________________
textmate mailing list
textmate@...
http://lists.macromates.com/listinfo/textmate

Ale Muñoz | 16 Feb 11:29
Picon

Re: Save and strip whitespace

Not sure if this is what you're looking for, but I have a "Strip Trailing Whitespace on Save" bundle on
Github: https://github.com/bomberstudios/Whitespace.tmbundle

a!

_______________________________________________
textmate mailing list
textmate@...
http://lists.macromates.com/listinfo/textmate

Ale Muñoz | 16 Feb 11:32
Picon

Re: Save and strip whitespace

On 16/02/2012, at 11:29, Ale Muñoz wrote:

> Not sure if this is what you're looking for, but I have a "Strip Trailing Whitespace on Save" bundle on
Github: https://github.com/bomberstudios/Whitespace.tmbundle

BTW, I use it with my "Save on Focus Lost" bundle
(https://github.com/bomberstudios/Autosave.tmbundle) and everything works like magic (sort of
like a "Strip Trailing Whitespace When Focus is Lost" option :)

a!

_______________________________________________
textmate mailing list
textmate@...
http://lists.macromates.com/listinfo/textmate

John Yeates | 17 Feb 12:45
Picon

Re: Save and strip whitespace

On Thursday February 16 2012, Ale Muñoz <bomberstudios@...> wrote:
> 
> Not sure if this is what you're looking for, but I have a "Strip Trailing Whitespace on Save" bundle on Github:https://github.com/bomberstudios/Whitespace.tmbundle

Exactly what I was looking for — thanks!

(I did have to rename it as I also have the Whitespace bundle from Mads Hartmann Jensen, which highlights
trailing spaces and mixed space/tab indents. Guess it's a popular name :)

--

-- 
John Yeates

_______________________________________________
textmate mailing list
textmate@...
http://lists.macromates.com/listinfo/textmate

John Yeates | 1 Apr 15:48
Picon

Re: Save and strip whitespace

On Thursday February 16 2012, Ale Muñoz <bomberstudios@...> wrote:
> 
> Not sure if this is what you're looking for, but I have a "Strip Trailing Whitespace on Save" bundle on Github:https://github.com/bomberstudios/Whitespace.tmbundle

This bundle seems to have stopped working in build 9110: it doesn’t strip space in the buffer or saved file
any more. Has anything changed that would cause the callback.document.did-save hook to stop working?

--

-- 
John Yeates

_______________________________________________
textmate mailing list
textmate@...
http://lists.macromates.com/listinfo/textmate

Ale Muñoz | 2 Apr 11:50
Picon

Re: Save and strip whitespace

On 01/04/2012, at 15:48, John Yeates wrote:

> This bundle seems to have stopped working in build 9110: it doesn’t strip space in the buffer or saved
file any more. Has anything changed that would cause the callback.document.did-save hook to stop working?

I'm starting to think it never actually worked properly...

Here's how the bundle works on my end (with 9110, on 10.7.3):

- Open a document
- Add some trailing whitespace
- Hit 'Save'
- The trailing whitespace is removed, and the file *seems* to be saved
  I say "seems", because the document icon goes from semitransparent to opaque, but what TextMate is saving
is actually the file *with* the whitespace.

The command's semantic class is 'callback.document.did-save' and is set to Save: 'Current Document'
(see attached screenshot)

It looks like it first saves the document, and then it runs the command (makes sense, but then we need a new
semantic class: 'callback.document.before-save'). Still, there is a bug with TM2, as it doesn't show
the document as modified after running the command.

I've gotten used to hitting Command + S two times in a row to remove whitespace and have it saved on disk, but
it's a pretty crappy solution and doesn't actually solve the problem when using the 'Save on Focus Lost' bundle.


_______________________________________________
textmate mailing list
textmate@...
http://lists.macromates.com/listinfo/textmate

Gmane