From b145689c14e6abd73c5e0725e739aa6db07aeb5a Mon Sep 17 00:00:00 2001 From: Doug Kearns Date: Thu, 14 Aug 2008 05:01:18 +0000 Subject: [PATCH] add :emenu for accessing menubar menu items from the command line --- NEWS | 1 + content/liberator.js | 60 ++++++++++++++++++++++++++++++++++++++++ locale/en-US/various.txt | 10 +++++++ 3 files changed, 71 insertions(+) diff --git a/NEWS b/NEWS index 8ead2b7f..8a53a0e6 100644 --- a/NEWS +++ b/NEWS @@ -15,6 +15,7 @@ generous donation which made this behavior possible) * IMPORTANT: ctrl-x/a never take possible negative URLs into account, it was just too unpredictable + * add :emenu for accessing the Firefox main menu items from the command line * add 'shell' and 'shellcmdflag' options * :tabprevious, :bprevious, :tabnext, :bnext and friends now accept a prefix count * add :comclear and :delcommand diff --git a/content/liberator.js b/content/liberator.js index b8866547..608bd9ba 100644 --- a/content/liberator.js +++ b/content/liberator.js @@ -152,6 +152,66 @@ const liberator = (function () //{{{ } }); + // TODO: move this + function getMenuItems() + { + var menubar = document.getElementById("main-menubar"); + var items = []; + + for (var i = 0; i < menubar.childNodes.length; i++) + { + (function (item, path) + { + if (item.childNodes.length == 0 && item.localName == "menuitem" + && !/rdf:http:/.test(item.label)) // FIXME + { + item.fullMenuPath = path += item.label; + items.push(item); + } + else + { + if (item.localName == "menu") + path += item.label + "."; + + for (var j = 0; j < item.childNodes.length; j++) + arguments.callee(item.childNodes[j], path); + } + })(menubar.childNodes[i], ""); + } + + return items; + } + + liberator.commands.addUserCommand(["em[enu]"], + "Execute the specified menu item from the command line", + function (args) + { + var item = args.string; + var items = getMenuItems(); + + if (!items.some(function (i) { return i.fullMenuPath == item; })) + { + liberator.echoerr("E334: Menu not found: " + item); + return; + } + + for (var i = 0; i < items.length; i++) + { + if (items[i].fullMenuPath == item) + items[i].doCommand(); + } + }, + { + argCount: "+", // NOTE: single arg may contain unescaped whitespace + completer: function (filter) + { + var completions = getMenuItems().map(function (item) { + return [item.fullMenuPath, item.label]; + }); + return [0, liberator.completion.filter(completions, filter)]; + } + }); + liberator.commands.add(["exe[cute]"], "Execute the argument as an Ex command", function (args) { liberator.execute(args); }); diff --git a/locale/en-US/various.txt b/locale/en-US/various.txt index 9a207caf..c5b64bd2 100644 --- a/locale/en-US/various.txt +++ b/locale/en-US/various.txt @@ -116,6 +116,16 @@ Play a system beep. ________________________________________________________________________________ +|:emenu| + +||:emenu {menu}|| +________________________________________________________________________________ +Execute {menu} from the command line. This command provides command line +access to all menu items available from the main Firefox menubar. {menu} is a +hierachical path to the menu item with each submenu separated by a period. +E.g. :emenu File.Open File... +________________________________________________________________________________ + + |:dia| |:dialog| ||:dia[log] [firefox-dialog]|| + ________________________________________________________________________________