Change file on save event → endless loop?


#1

Hi,

I try to modify a file on save. Of course this triggers a re-save, so that my file is always marked as modified. How can I solve this? E.g. I want to suppress my own event listener while editing the file.

Code looks similar to this (via emissary module):

atom.workspace.eachEditor(function(editor) {
    var buffer = editor.getBuffer();
 
    if (atom.config.get('plugin.onSave')) {
      var events = 'saved';
      plugin.subscribe(buffer, events, modifyMyFile);
    }
  });

Unsubscrubing from the save event inside modifyMyFile doesn’t seem to work :frowning:


Looking for a after file save api
#2

Maybe you want to subscribe to the saving event instead of the saved event? This allows you to modify the file before the save and then the save stores the modified contents. (I think it is called saving, you might want to double check … not in a spot where I can verify.)


#3

I think you want to subscribe to the will-be-saved event. For an example of how that is used check out this line in the whitespace package.


#4

Thank you both, will-be-saved works like a charm! Great community :slight_smile:


#5