Prevent Autocommand Pileup?

107 views
Skip to first unread message

gop...@gmail.com

unread,
Apr 28, 2014, 5:27:59 PM4/28/14
to penta...@googlegroups.com
I have some autocommands in my pentadactylrc, and every time I open a new window, they are added again and executed an extra time. Is there a variable for window number? I was thinking that I could put the autocommands in an if statement so that they are only executed if it is the first window. If not, does anyone know of another way to solve this problem without having to manually add autocommands or source another file every time firefox is started?

gop...@gmail.com

unread,
Jun 3, 2014, 2:52:53 PM6/3/14
to penta...@googlegroups.com, gop...@gmail.com
I thought I'd update on this in case anyone comes across this with the same question. I've just started putting my autocommands in a separate file and sourcing it using "au Enter" to ensure it's only sourced once (when firefox starts). Not as straightforward as in vim, but it works fine.

Lit Wakefield

unread,
Apr 9, 2015, 10:56:55 PM4/9/15
to penta...@googlegroups.com, gop...@gmail.com
I should have posted this earlier, but this doesn't actually work. If it used to work, it doesn't anymore.

wsha...@gmail.com

unread,
Apr 11, 2015, 7:38:28 AM4/11/15
to penta...@googlegroups.com, gop...@gmail.com
You could try a plugin. Change the file ending to your separate autocommands file to .js and wrap each command in commands.execute like commands.execute("au ..."). Then put that file in your $HOME/pentadactyl/plugins directory (create it if it doesn't exist) and do :loadplugins. I think that will only be executed once, but, if not, you could wrap the commands in an if statement that tests the flag that you set with the commands to make sure they are only executed once.

Lit Wakefield

unread,
Apr 11, 2015, 12:03:32 PM4/11/15
to penta...@googlegroups.com, gop...@gmail.com, wsha...@gmail.com
Thanks, that does indeed work. I think I'd still rather have autocommands all in my rc file though. How would you set and test flags?

Lit Wakefield

unread,
Apr 11, 2015, 1:52:24 PM4/11/15
to penta...@googlegroups.com, gop...@gmail.com, wsha...@gmail.com
Once again, my mistake. This doesn't work. I tried putting this in an autocommands.js plugin:

if (typeof downPostAlready === 'undefined') {
    commands
.execute("au DownloadPost ...)
    var downPostAlready = true;
}

That doesn't work either. I think it's probably because the variable is window local (which is the case if I put that in a js <<EOF in my rc and replace "var.." with "window.downPostAlready =..."). How do you get around this?

wsha...@gmail.com

unread,
Apr 12, 2015, 9:34:52 PM4/12/15
to penta...@googlegroups.com, gop...@gmail.com, wsha...@gmail.com
Annoying, yes, everything I tested in Pentadactyl seems to be localized on the window level. The standard way to track variables independently from windows in Firefox add-ons is to use a javascript module (see https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Using), but I couldn't see that Pentadactyl used any in a quick search (cycling through autocomplete after type ":js " in Pentadactyl. If another add-on you use has a module, you could tack the variable on to it. Otherwise, you could write your own Firefox add-on that does nothing but define an empty module for you to tack a variable onto. There must be an easier way to tell if a window is the first opened in Pentadactyl, but I can't think of one so far.

Lit Wakefield

unread,
May 13, 2015, 10:25:34 PM5/13/15
to penta...@googlegroups.com, gop...@gmail.com, wsha...@gmail.com
This doesn't seem to work either unless I'm looking for the wrong thing with :js. I tried using 'dactyl.downPostAlready' instead (dactyl shows up as a module with :js) without success. I also tried a few other things  (like "downloads") that showed up as "[modules <name>]" or that were under the modules heading. I noticed that if I set something under downloads from the pentadactyl commandline (e.g. :js downloads.testVar = true<cr>), it would show up in other open windows or after opening a new window without having to set it again.

wsha...@gmail.com

unread,
May 13, 2015, 11:12:56 PM5/13/15
to penta...@googlegroups.com, gop...@gmail.com
Hmm, I think dactyl is a Module as defined in https://github.com/5digits/dactyl/blob/master/common/modules/main.jsm but not in the Javascript Code Module way that I meant. I don't have time to make a minimal add-on right now that just creates an empty module (though it shouldn't be hard to do), but if you want you could install my add-on Zutilo (available here: https://addons.mozilla.org/en-US/firefox/addon/zutilo-utility-for-zotero/). If you don't use the Zotero add-on, it will complain about Zotero not being installed but you can check a box to suppress the warning in the future. Then Zutilo will basically do nothing other than export an object named Zutilo from its Javascript Code Module. Then you could put this in your autocommands.js plugin:

if (!('commands_loaded' in Zutilo)) {                                          
    Zutilo.commands_loaded = true                                              
    commands.execute(':autocmd PageLoad duckduckgo.com -js alert("test")')        
}

With this, the autocmd is only defined inside the first window and not in subsequent windows. A couple things to keep in mind:

If you close that first window, you will lose your autocommand.
If you rehash Pentadactyl, the Zutilo attribute will already be defined so the autocommand will not be added in the new Pentadactyl session. (You could do ":js delete Zutilo.commands_loaded" and then rehash again to have it defined again).

Lit Wakefield

unread,
May 14, 2015, 12:28:08 AM5/14/15
to penta...@googlegroups.com, wsha...@gmail.com
Thanks! I'd still like it if there was a better way to do this though. I just noticed that that I get duplicates from group.autocmd.get(). Checking to see if the command has already been added seems like a good idea. From the pentadactyl commandline, I can do :echo group.autocmd.get()[0][0] to get the type of the first autocmd (like "PageLoad"), but if I try testing a function in a script that does this I get "TypeError: group.autocmd.get(...)[0] is undefined".

wsha...@gmail.com

unread,
May 15, 2015, 8:51:51 PM5/15/15
to penta...@googlegroups.com, wsha...@gmail.com
That's a good point. I haven't looked into the internals of the autocmd code, but if you look in there at how autocommands are created and deleted you could probably come up with a script that loops over the autocommands and removes any duplicates.
Reply all
Reply to author
Forward
0 new messages