Jump to last saved file

I would like to propose a new addition to already wonderful functions of:

  • Go back one location
  • Go forward one location
  • General: Most Recently Viewed File

which would be: Jump to last saved file

Usually in my workflow I have dozens of files that are open for reference and copy-from jobs, practically making their usage read-only, while there is always one file where I’m working on, which gets saved every once in a while. So, a function like that would help me always jump back to the file I’m pasting to, since that one would always be the last one saved.

I was informed that this could be achieved with a macro, but I have no idea where to start.
Any help on building such a macro would be greatly appreciated.

Thanks.

hey @AtmanActive,

You could also use the Switch to Tab #1 command. Open commando (ctrl + shift + o), hit the command scope then search Switch. ALL of those commands can be mapped to a keybinding in your preferences. So that’s probably the easiest route but it will require that said file stay in one spot. Easy enough if you just leave it in position 1.

This should be easy enough in a macro as well. You’ll want the Komodo Extension Developer for the Javascript shell.

Once you’ve got that you’ll want the require("ko/views") SDK. Soon you’ll be able to reference it online in the new Komodo docs but that won’t be updated until 9.3 release. For now you can tab through results in the JS shell. eg. require("ko/views").|//press tab twice at the bar. Note a view is the entire editor Mozilla UI element which wraps a the file object inside itself as koDoc.

Some functions you’ll be interested in.

  • To get all open file objects:

    require(“ko/views”).all(); //returns an array
    var view1 = require(“ko/views”).all()[0];

  • Get current view:

require("ko/views").current().get();
  • Once you have a view, get it’s path:
var view =  require("ko/views").current().get();
view.getURI();
  • Get the views filename:
`view.koDoc.baseName`
// filename.py

That should get you started. There are MANY functions wrapped up in there so have a look and please do ask questions.

Oh one last function you’ll want: view.focus() //:wink:

  • Carey

Here’s a simple userscript (macro) I quickly threw together:

if (extensions.goToLastSaved)
{
    window.removeEventListener("file_saved", extensions.goToLastSaved.onFileSave);
    require("ko/commands").unregister("goto_lastsaved");
}

(function() {
    
    var lastView = null;
    
    this.onFileSave = function()
    {
        lastView = ko.views.manager.currentView;
    }
    
    this.onGoto = function()
    {
        var v = ko.views.manager.currentView;
        if (lastView && v != lastView)
            xtk.domutils.fireEvent(lastView.parentNode._tab, 'click');
    }
    
    require("ko/commands").register("goto_lastsaved", this.onGoto, {label: "Go to Last Saved"});
    window.addEventListener("file_saved", extensions.goToLastSaved.onFileSave);
    
}).apply(extensions.goToLastSaved)

You’ll want to have that trigger on startup. And you’ll want to define a key for the command using the usual keybinding interface.

@careyh sorry, it seems we were both working on this :stuck_out_tongue:

They compliment each other! Excellent!

  • Carey

Guys, thank you very much for your quick help and macro ideas.

@nathanr, however I can’t make that code to work.
I don’t understand what is wrong with it and I get weird errors from Komodo that I don’t understand.
First time I put it in a macro and run it I got ‘type error extensions.goToLastSaved is undefined’.
Then I tried to remove the first IF part, and now I get “undefined: The command ‘undefined’ is already in use”

Please help.

Thanks.

Try changing the first part to:

if ("goToLastSaved" in extensions)

Just noticed it also needs the following at the very top:

window.extensions = window.extensions || {}

TypeError: extensions.goToLastSaved is undefined

Did you restart Komodo @AtmanActive? Or make sure to rerun the macro?

  • Carey

Yup.

When I first start Komodo and execute the macro, I get:

TypeError: extensions.goToLastSaved is undefined

After that, all subsequent attempts to execute the macro end up with:

undefined: The command ‘undefined’ is already in use

Here is the macro source (so far):

window.extensions = window.extensions || {};

if ("goToLastSaved" in extensions)
{
    window.removeEventListener("file_saved", extensions.goToLastSaved.onFileSave);
    require("ko/commands").unregister("goto_lastsaved");
}

(function() {
    
    var lastView = null;
    
    this.onFileSave = function()
    {
        lastView = ko.views.manager.currentView;
    }
    
    this.onGoto = function()
    {
        var v = ko.views.manager.currentView;
        if (lastView && v != lastView)
            xtk.domutils.fireEvent(lastView.parentNode._tab, 'click');
    }
    
    require("ko/commands").register("goto_lastsaved", this.onGoto, {label: "Go to Last Saved"});
    window.addEventListener("file_saved", extensions.goToLastSaved.onFileSave);
    
}).apply(extensions.goToLastSaved)

@AtmanActive, thanks for confirming. At line 27 goToLastSaved isn’t defined. This information was in the logs but not in the alert window which isn’t as helpful. I’m going to file a bug on that.

I added an extra line below line 1:

window.extensions = window.extensions || {};
window.extensions.goToLastSaved = window.extensions.goToLastSaved || {}

I haven’t tested if that gets the functionality you want but it no longer errors out.

  • Carey

This one should work:

window.extensions = window.extensions || {};
if (extensions.goToLastSaved)
{
    window.removeEventListener("file_saved", extensions.goToLastSaved.onFileSave);
    require("ko/commands").unregister("goto_lastsaved");
}

extensions.goToLastSaved = {};
(function() {
    
    var lastView = null;
    
    this.onFileSave = function()
    {
        lastView = ko.views.manager.currentView;
    }
    
    this.onGoto = function()
    {
        var v = ko.views.manager.currentView;
        if (lastView && v != lastView)
            xtk.domutils.fireEvent(lastView.parentNode._tab, 'click');
    }
    
    require("ko/commands").register("goto_lastsaved", this.onGoto, {label: "Go to Last Saved"});
    window.addEventListener("file_saved", extensions.goToLastSaved.onFileSave);
    
}).apply(extensions.goToLastSaved)

Update the one you added with these contents then restart Komodo.

Yes, yes!

This one works.

You guys rock!

Just to clarify for someone reading this later:
The macro above should be set to autorun on Komodo start. That’s in macro’s properties->triggers.
So, this macro runs once on startup to initialize the functionality and that’s it.
Next, create another new macro to actually invoke this function. The one that can be included in the toolbar:

komodo.assertMacroVersion(3);
if (komodo.view) { komodo.view.setFocus(); }
ko.commands.doCommand('cmd_goto_lastsaved')
ko.commands.doCommand('cmd_focusEditor')

1 Like

You do not need to create another macro to invoke this one. You can just set the keybinding under Preferences > Key Bindings.

Glad you like it :slight_smile:

I like to click :slight_smile:

Unless he wants a tool bar item :wink:

Ahh right :stuck_out_tongue: