]> git.donarmstrong.com Git - dactyl.git/blob - pentadactyl/content/config.js
Import 1.0b7.1 supporting Firefox up to 8.*
[dactyl.git] / pentadactyl / content / config.js
1 // Copyright (c) 2006-2009 by Martin Stubenschrott <stubenschrott@vimperator.org>
2 // Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com>
3 // Copyright (c) 2008-2011 by Kris Maglione <maglione.k at Gmail>
4 //
5 // This work is licensed for reuse under an MIT license. Details are
6 // given in the LICENSE.txt file included with this file.
7 "use strict";
8
9 var Config = Module("config", ConfigBase, {
10     name: "pentadactyl",
11     appName: "Pentadactyl",
12     idName: "PENTADACTYL",
13     host: "Firefox",
14     hostbin: "firefox",
15
16     commandContainer: "browser-bottombox",
17
18     Local: function Local(dactyl, modules, window)
19         let ({ config } = modules) ({
20
21         completers: Class.memoize(function () update({ sidebar: "sidebar", window: "window" }, this.__proto__.completers)),
22
23         dialogs: {
24             about: ["About Firefox",
25                 function () { window.openDialog("chrome://browser/content/aboutDialog.xul", "_blank", "chrome,dialog,modal,centerscreen"); }],
26             addbookmark: ["Add bookmark for the current page",
27                 function () { window.PlacesCommandHook.bookmarkCurrentPage(true, window.PlacesUtils.bookmarksRootId); }],
28             addons: ["Manage Add-ons",
29                 function () { window.BrowserOpenAddonsMgr(); }],
30             bookmarks: ["List your bookmarks",
31                 function () { window.openDialog("chrome://browser/content/bookmarks/bookmarksPanel.xul", "Bookmarks", "dialog,centerscreen,width=600,height=600"); }],
32             checkupdates: ["Check for updates",
33                 function () { window.checkForUpdates(); },
34                 function () "checkForUpdates" in window],
35             cookies: ["List your cookies",
36                 function () { window.toOpenWindowByType("Browser:Cookies", "chrome://browser/content/preferences/cookies.xul", "chrome,dialog=no,resizable"); }],
37             console: ["JavaScript console",
38                 function () { window.toJavaScriptConsole(); }],
39             customizetoolbar: ["Customize the Toolbar",
40                 function () { window.BrowserCustomizeToolbar(); }],
41             dominspector: ["DOM Inspector",
42                 function () { window.inspectDOMDocument(window.content.document); },
43                 function () "inspectDOMDocument" in window],
44             downloads: ["Manage Downloads",
45                 function () { window.toOpenWindowByType("Download:Manager", "chrome://mozapps/content/downloads/downloads.xul", "chrome,dialog=no,resizable"); }],
46             history: ["List your history",
47                 function () { window.openDialog("chrome://browser/content/history/history-panel.xul", "History", "dialog,centerscreen,width=600,height=600"); }],
48             import: ["Import Preferences, Bookmarks, History, etc. from other browsers",
49                 function () { window.BrowserImport(); }],
50             openfile: ["Open the file selector dialog",
51                 function () { window.BrowserOpenFileWindow(); }],
52             pageinfo: ["Show information about the current page",
53                 function () { window.BrowserPageInfo(); }],
54             pagesource: ["View page source",
55                 function () { window.BrowserViewSourceOfDocument(window.content.document); }],
56             passwords: ["Passwords dialog",
57                 function () { window.openDialog("chrome://passwordmgr/content/passwordManager.xul"); }],
58             places: ["Places Organizer: Manage your bookmarks and history",
59                 function () { window.PlacesCommandHook.showPlacesOrganizer(window.ORGANIZER_ROOT_BOOKMARKS); }],
60             preferences: ["Show Firefox preferences dialog",
61                 function () { window.openPreferences(); }],
62             printpreview: ["Preview the page before printing",
63                 function () { window.PrintUtils.printPreview(window.PrintPreviewListener || window.onEnterPrintPreview, window.onExitPrintPreview); }],
64             printsetup: ["Setup the page size and orientation before printing",
65                 function () { window.PrintUtils.showPageSetup(); }],
66             print: ["Show print dialog",
67                 function () { window.PrintUtils.print(); }],
68             saveframe: ["Save frame to disk",
69                 function () { window.saveFrameDocument(); }],
70             savepage: ["Save page to disk",
71                 function () { window.saveDocument(window.content.document); }],
72             searchengines: ["Manage installed search engines",
73                 function () { window.openDialog("chrome://browser/content/search/engineManager.xul", "_blank", "chrome,dialog,modal,centerscreen"); }],
74             selectionsource: ["View selection source",
75                 function () { modules.buffer.viewSelectionSource(); }],
76             venkman: ["The JavaScript debugger",
77                 function () { dactyl.assert("start_venkman" in window, "Venkman is not installed"); window.start_venkman() },
78                 function () "start_venkman" in window]
79         },
80
81         removeTab: function removeTab(tab) {
82             if (this.tabbrowser.mTabs.length > 1)
83                 this.tabbrowser.removeTab(tab);
84             else {
85                 if (modules.buffer.uri.spec !== "about:blank" || window.getWebNavigation().sessionHistory.count > 0) {
86                     dactyl.open("about:blank", dactyl.NEW_BACKGROUND_TAB);
87                     this.tabbrowser.removeTab(tab);
88                 }
89                 else
90                     dactyl.beep();
91             }
92         },
93
94         get tempFile() {
95             let prefix = this.name;
96             try {
97                 prefix += "-" + window.content.document.location.hostname;
98             }
99             catch (e) {}
100
101             return prefix + ".tmp";
102         }
103     }),
104
105     overlayChrome: ["chrome://browser/content/browser.xul"],
106
107     styleableChrome: ["chrome://browser/content/browser.xul"],
108
109     autocommands: {
110         BookmarkAdd: "Triggered after a page is bookmarked",
111         BookmarkChange: "Triggered after a page's bookmark is changed",
112         BookmarkRemove: "Triggered after a page's bookmark is removed",
113         ColorScheme: "Triggered after a color scheme has been loaded",
114         DOMLoad: "Triggered when a page's DOM content has fully loaded",
115         DownloadPost: "Triggered when a download has completed",
116         Fullscreen: "Triggered when the browser's fullscreen state changes",
117         LocationChange: "Triggered when changing tabs or when navigation to a new location",
118         PageLoadPre: "Triggered after a page load is initiated",
119         PageLoad: "Triggered when a page gets (re)loaded/opened",
120         PrivateMode: "Triggered when private browsing mode is activated or deactivated",
121         Sanitize: "Triggered when a sanitizeable item is cleared",
122         ShellCmdPost: "Triggered after executing a shell command with :!cmd",
123         Enter: "Triggered after Firefox starts",
124         LeavePre: "Triggered before exiting Firefox, just before destroying each module",
125         Leave: "Triggered before exiting Firefox"
126     },
127
128     defaults: {
129         complete: "slf",
130         guioptions: "bCrs",
131         showtabline: "always",
132         titlestring: "Pentadactyl"
133     },
134
135     features: Set([
136         "bookmarks", "hints", "history", "marks", "quickmarks", "sanitizer",
137         "session", "tabs", "tabs_undo", "windows"
138     ]),
139
140     guioptions: {
141         m: ["Menubar",      ["toolbar-menubar"]],
142         T: ["Toolbar",      ["nav-bar"]],
143         B: ["Bookmark bar", ["PersonalToolbar"]]
144     },
145
146     hasTabbrowser: true,
147
148     scripts: [
149         "browser",
150         "bookmarkcache",
151         "bookmarks",
152         "history",
153         "quickmarks",
154         "sanitizer",
155         "tabs"
156     ],
157
158     sidebars: {
159         viewAddons:      ["Add-ons",     "A", "chrome://mozapps/content/extensions/extensions.xul"],
160         viewConsole:     ["Console",     "C", "chrome://global/content/console.xul"],
161         viewDownloads:   ["Downloads",   "D", "chrome://mozapps/content/downloads/downloads.xul"],
162         viewPreferences: ["Preferences", "P", "about:config"]
163     }
164 }, {
165 }, {
166     commands: function (dactyl, modules, window) {
167         const { commands, completion, config } = modules;
168         const { document } = window;
169
170         commands.add(["winon[ly]"],
171             "Close all other windows",
172             function () {
173                 dactyl.windows.forEach(function (win) {
174                     if (win != window)
175                         win.close();
176                 });
177             },
178             { argCount: "0" });
179
180         commands.add(["pref[erences]", "prefs"],
181             "Show " + config.host + " preferences",
182             function (args) {
183                 if (args.bang) // open Firefox settings GUI dialog
184                     dactyl.open("about:config", { from: "prefs" });
185                 else
186                     window.openPreferences();
187             },
188             {
189                 argCount: "0",
190                 bang: true
191             });
192
193         commands.add(["sbcl[ose]"],
194             "Close the sidebar window",
195             function () {
196                 if (!document.getElementById("sidebar-box").hidden)
197                     window.toggleSidebar();
198             },
199             { argCount: "0" });
200
201         commands.add(["sideb[ar]", "sb[ar]", "sbop[en]"],
202             "Open the sidebar window",
203             function (args) {
204                 function compare(a, b) util.compareIgnoreCase(a, b) == 0
205                 let title = document.getElementById("sidebar-title");
206
207                 dactyl.assert(args.length || title.value || args.bang && config.lastSidebar,
208                               _("error.argumentRequired"));
209
210                 if (!args.length)
211                     return window.toggleSidebar(title.value ? null : config.lastSidebar);
212
213                 // focus if the requested sidebar is already open
214                 if (compare(title.value, args[0])) {
215                     if (args.bang)
216                         return window.toggleSidebar();
217                     return dactyl.focus(document.getElementById("sidebar-box"));
218                 }
219
220                 let menu = document.getElementById("viewSidebarMenu");
221
222                 for (let [, panel] in Iterator(menu.childNodes))
223                     if (compare(panel.getAttribute("label"), args[0])) {
224                         let elem = document.getElementById(panel.observes);
225                         if (elem)
226                             elem.doCommand();
227                         return;
228                     }
229
230                 return dactyl.echoerr(_("error.invalidArgument", args[0]));
231             },
232             {
233                 argCount: "?",
234                 bang: true,
235                 completer: function (context) {
236                     context.ignoreCase = true;
237                     return completion.sidebar(context);
238                 },
239                 literal: 0
240             });
241
242         commands.add(["wind[ow]"],
243             "Execute a command and tell it to output in a new window",
244             function (args) {
245                 dactyl.withSavedValues(["forceNewWindow"], function () {
246                     this.forceNewWindow = true;
247                     this.execute(args[0], null, true);
248                 });
249             },
250             {
251                 argCount: "1",
252                 completer: function (context) completion.ex(context),
253                 literal: 0,
254                 subCommand: 0
255             });
256
257         commands.add(["winc[lose]", "wc[lose]"],
258             "Close window",
259             function () { window.close(); },
260             { argCount: "0" });
261
262         commands.add(["wino[pen]", "wo[pen]"],
263             "Open one or more URLs in a new window",
264             function (args) {
265                 if (args[0])
266                     dactyl.open(args[0], dactyl.NEW_WINDOW);
267                 else
268                     dactyl.open("about:blank", dactyl.NEW_WINDOW);
269             },
270             {
271                 completer: function (context) completion.url(context),
272                 domains: function (args) commands.get("open").domains(args),
273                 literal: 0,
274                 privateData: true
275             });
276     },
277     completion: function (dactyl, modules, window) {
278         const { CompletionContext, bookmarkcache, completion } = modules;
279         const { document } = window;
280
281         var searchRunning = null; // only until Firefox fixes https://bugzilla.mozilla.org/show_bug.cgi?id=510589
282         completion.location = function location(context) {
283             if (!services.autoCompleteSearch)
284                 return;
285
286             if (searchRunning) {
287                 searchRunning.completions = searchRunning.completions;
288                 searchRunning.cancel();
289             }
290
291             context.anchored = false;
292             context.compare = CompletionContext.Sort.unsorted;
293             context.filterFunc = null;
294
295             let words = context.filter.toLowerCase().split(/\s+/g);
296             context.hasItems = true;
297             context.completions = context.completions.filter(function ({ url, title })
298                 words.every(function (w) (url + " " + title).toLowerCase().indexOf(w) >= 0))
299             context.incomplete = true;
300
301             context.format = modules.bookmarks.format;
302             context.keys.extra = function (item) (bookmarkcache.get(item.url) || {}).extra;
303             context.title = ["Smart Completions"];
304
305             context.cancel = function () {
306                 this.incomplete = false;
307                 if (searchRunning === this) {
308                     services.autoCompleteSearch.stopSearch();
309                     searchRunning = null;
310                 }
311             };
312
313             services.autoCompleteSearch.startSearch(context.filter, "", context.result, {
314                 onSearchResult: function onSearchResult(search, result) {
315                     if (result.searchResult <= result.RESULT_SUCCESS)
316                         searchRunning = null;
317
318                     context.incomplete = result.searchResult >= result.RESULT_NOMATCH_ONGOING;
319                     context.completions = [
320                         { url: result.getValueAt(i), title: result.getCommentAt(i), icon: result.getImageAt(i) }
321                         for (i in util.range(0, result.matchCount))
322                     ];
323                 },
324                 get onUpdateSearchResult() this.onSearchResult
325             });
326             searchRunning = context;
327         };
328
329         completion.addUrlCompleter("l",
330             "Firefox location bar entries (bookmarks and history sorted in an intelligent way)",
331             completion.location);
332
333         completion.sidebar = function sidebar(context) {
334             let menu = document.getElementById("viewSidebarMenu");
335             context.title = ["Sidebar Panel"];
336             context.completions = Array.map(menu.childNodes, function (n) [n.getAttribute("label"), ""]);
337         };
338     },
339     events: function (dactyl, modules, window) {
340         modules.events.listen(window, "SidebarFocused", function (event) {
341             modules.config.lastSidebar = window.document.getElementById("sidebar-box")
342                                                .getAttribute("sidebarcommand");
343         }, false);
344     },
345     mappings: function initMappings(dactyl, modules, window) {
346         const { Events, mappings, modes } = modules;
347         mappings.add([modes.NORMAL],
348                      ["<Return>", "<Space>", "<Up>", "<Down>"],
349                      "Handled by " + config.host,
350                      function () Events.PASS_THROUGH);
351     },
352     modes: function (dactyl, modules, window) {
353         const { modes } = modules;
354         config.modes.forEach(function (mode) { modes.addMode.apply(this, mode); });
355     },
356     options: function (dactyl, modules, window) {
357         modules.options.add(["online"],
358             "Set the 'work offline' option",
359             "boolean", true,
360             {
361                 setter: function (value) {
362                     if (services.io.offline == value)
363                         window.BrowserOffline.toggleOfflineStatus();
364                     return value;
365                 },
366                 getter: function () !services.io.offline
367             });
368     }
369 });
370
371 // vim: set fdm=marker sw=4 ts=4 et: