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.
On Thu, Feb 9, 2012 at 18:50, Andrew Appleton andrew@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%C2%A0and 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
On 2012-02-09 14:03, Martin Kühl wrote:
On Thu, Feb 9, 2012 at 18:50, Andrew Appletonandrew@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.
On Thursday February 09 2012, Martin Kühl martin.kuehl@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?
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 :)
On Thu, Feb 9, 2012 at 20:55, Steve King sking@arbor.net wrote:
On 2012-02-09 14:03, Martin Kühl wrote:
On Thu, Feb 9, 2012 at 18:50, Andrew Appletonandrew@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
On Fri, Feb 10, 2012 at 09:48, John Yeates textmate2@warmvomit.co.uk wrote:
On Thursday February 09 2012, Martin Kühl martin.kuehl@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
On Fri, Feb 10, 2012 at 11:33, Martin Kühl martin.kuehl@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
On 12/02/2012, at 15.36, Martin Kühl wrote:
On Fri, Feb 10, 2012 at 11:33, Martin Kühl martin.kuehl@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.
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.
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!
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!
On Thursday February 16 2012, Ale Muñoz bomberstudios@gmail.com 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 :)
On Thursday February 16 2012, Ale Muñoz bomberstudios@gmail.com 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?
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.