1 utils = require 'utils'
2 hints = require 'hints'
10 , getFirefoxPref } = require 'prefs'
12 { classes: Cc, interfaces: Ci, utils: Cu } = Components
14 # Opens developer toolbar (Default shotrcut: Shift-F2)
15 command_dev = (vim) ->
16 if chromeWindow = utils.getRootWindow vim.window
17 chromeWindow.DeveloperToolbar.show(true)
18 chromeWindow.DeveloperToolbar.focus()
20 # Focus the Address Bar
21 command_focus = (vim) ->
22 if chromeWindow = utils.getRootWindow(vim.window)
23 chromeWindow.focusAndSelectUrlBar()
25 # Focus the Search Bar
26 command_focus_search = (vim) ->
27 if chromeWindow = utils.getRootWindow(vim.window)
28 if searchBar = chromeWindow.document.getElementById("searchbar")
31 # Navigate to the address that is currently stored in the system clipboard
32 command_paste = (vim) ->
33 url = utils.readFromClipboard(vim.window)
35 if not utils.isURL(url) and submission = utils.browserSearchSubmission(url)
36 url = submission.uri.spec
37 { postData } = submission
39 if chromeWindow = utils.getRootWindow(vim.window)
40 chromeWindow.gBrowser.loadURIWithFlags(url, null, null, null, postData)
42 # Open new tab and navigate to the address that is currently stored in the system clipboard
43 command_paste_tab = (vim) ->
44 url = utils.readFromClipboard(vim.window)
46 if not utils.isURL(url) and submission = utils.browserSearchSubmission(url)
47 url = submission.uri.spec
48 { postData } = submission
50 if chromeWindow = utils.getRootWindow vim.window
51 chromeWindow.gBrowser.selectedTab = chromeWindow.gBrowser.addTab(url, null, null, postData, null, false)
53 # Open new tab and focus the address bar
54 command_open_tab = (vim) ->
55 if chromeWindow = utils.getRootWindow(vim.window)
56 chromeWindow.BrowserOpenTab()
58 # Copy element URL to the clipboard
59 command_marker_yank = (vim) ->
60 callback = (marker) ->
61 if url = marker.element.href
62 marker.element.focus()
63 utils.writeToClipboard(vim.window, url)
64 else if utils.isTextInputElement(marker.element)
65 utils.writeToClipboard(vim.window, marker.element.value)
67 vim.enterMode('hints', [callback])
70 command_marker_focus = (vim) ->
71 callback = (marker) -> marker.element.focus()
73 vim.enterMode('hints', [callback])
75 # Copy current URL to the clipboard
76 command_yank = (vim) ->
77 utils.writeToClipboard(vim.window, vim.window.location.toString())
79 # Reload the page, possibly from cache
80 command_reload = (vim) ->
81 vim.window.location.reload(false)
83 # Reload the page from the server
84 command_reload_force = (vim) ->
85 vim.window.location.reload(true)
87 # Reload the page, possibly from cache
88 command_reload_all = (vim) ->
89 if rootWindow = utils.getRootWindow(vim.window)
90 if tabs = rootWindow.gBrowser.tabContainer
91 for i in [0...tabs.itemCount]
92 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
93 window.location.reload(false)
95 # Reload the page from the server
96 command_reload_all_force = (vim) ->
97 if rootWindow = utils.getRootWindow(vim.window)
98 if tabs = rootWindow.gBrowser.tabContainer
99 for i in [0...tabs.itemCount]
100 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
101 window.location.reload(true)
103 command_stop = (vim) ->
106 command_stop_all = (vim) ->
107 if rootWindow = utils.getRootWindow(vim.window)
108 if tabs = rootWindow.gBrowser.tabContainer
109 for i in [0...tabs.itemCount]
110 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
113 # Scroll to the top of the page
114 command_scroll_to_top = (vim) ->
116 utils.simulateWheel(vim.window, 0, -1, utils.WHEEL_MODE_PAGE)
118 # Scroll to the bottom of the page
119 command_scroll_to_bottom = (vim) ->
121 utils.simulateWheel(vim.window, 0, 1, utils.WHEEL_MODE_PAGE)
124 command_scroll_down = (vim) ->
125 utils.simulateWheel(vim.window, 0, getPref('scroll_step_lines'), utils.WHEEL_MODE_LINE)
128 command_scroll_up = (vim) ->
129 utils.simulateWheel(vim.window, 0, -getPref('scroll_step_lines'), utils.WHEEL_MODE_LINE)
132 command_scroll_left = (vim) ->
133 utils.simulateWheel(vim.window, -getPref('scroll_step_lines'), 0, utils.WHEEL_MODE_LINE)
136 command_scroll_right = (vim) ->
137 utils.simulateWheel(vim.window, getPref('scroll_step_lines'), 0, utils.WHEEL_MODE_LINE)
139 # Scroll down half a page
140 command_scroll_half_page_down = (vim) ->
141 utils.simulateWheel(vim.window, 0, 0.5, utils.WHEEL_MODE_PAGE)
143 # Scroll up half a page
144 command_scroll_half_page_up = (vim) ->
145 utils.simulateWheel(vim.window, 0, -0.5, utils.WHEEL_MODE_PAGE)
147 # Scroll down full a page
148 command_scroll_page_down = (vim) ->
149 utils.simulateWheel(vim.window, 0, 1, utils.WHEEL_MODE_PAGE)
151 # Scroll up full a page
152 command_scroll_page_up = (vim) ->
153 utils.simulateWheel(vim.window, 0, -1, utils.WHEEL_MODE_PAGE)
155 # Activate previous tab
156 command_tab_prev = (vim) ->
157 if rootWindow = utils.getRootWindow(vim.window)
158 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true)
161 command_tab_next = (vim) ->
162 if rootWindow = utils.getRootWindow(vim.window)
163 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true)
165 command_home = (vim) ->
166 url = getFirefoxPref('browser.startup.homepage')
167 if chromeWindow = utils.getRootWindow(vim.window)
168 chromeWindow.gBrowser.loadURIWithFlags(url, null, null, null, null)
170 # Go to the first tab
171 command_tab_first = (vim) ->
172 if rootWindow = utils.getRootWindow(vim.window)
173 rootWindow.gBrowser.tabContainer.selectedIndex = 0
176 command_tab_last = (vim) ->
177 if rootWindow = utils.getRootWindow(vim.window)
178 itemCount = rootWindow.gBrowser.tabContainer.itemCount
179 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1
182 command_back = (vim) ->
183 vim.window.history.back()
185 # Go forward in history
186 command_forward = (vim) ->
187 vim.window.history.forward()
190 command_close_tab = (vim) ->
191 if rootWindow = utils.getRootWindow(vim.window)
192 unless rootWindow.gBrowser.selectedTab.pinned
193 rootWindow.gBrowser.removeCurrentTab()
195 # Restore last closed tab
196 command_restore_tab = (vim) ->
197 if rootWindow = utils.getRootWindow(vim.window)
198 ss = utils.getSessionStore()
199 if ss and ss.getClosedTabCount(rootWindow) > 0
200 ss.undoCloseTab(rootWindow, 0)
202 # Follow links with hint markers
203 command_follow = (vim) ->
204 callback = (marker) ->
205 marker.element.focus()
206 utils.simulateClick(marker.element)
208 vim.enterMode('hints', [callback])
210 # Follow links in a new Tab with hint markers
211 command_follow_in_tab = (vim) ->
212 callback = (marker) ->
213 marker.element.focus()
214 utils.simulateClick(marker.element, { metaKey: true, ctrlKey: true })
216 vim.enterMode('hints', [callback])
218 # Move current tab to the left
219 command_tab_move_left = (vim) ->
220 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
221 if tab = gBrowser.selectedTab
222 index = gBrowser.tabContainer.selectedIndex
223 total = gBrowser.tabContainer.itemCount
225 # `total` is added to deal with negative offset
226 gBrowser.moveTabTo(tab, (total + index - 1) % total)
228 # Move current tab to the right
229 command_tab_move_right = (vim) ->
230 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
231 if tab = gBrowser.selectedTab
232 index = gBrowser.tabContainer.selectedIndex
233 total = gBrowser.tabContainer.itemCount
235 gBrowser.moveTabTo(tab, (index + 1) % total)
237 # Display the Help Dialog
238 command_help = (vim) ->
239 help.injectHelp(vim.window.document, commands)
243 # Switch into find mode
244 command_find = (vim, storage) ->
245 find.injectFind vim.window.document, (findStr, startFindRng) ->
246 # Reset region and find string if new find stirng has arrived
247 if find.findStr != findStr
248 [find.findStr, storage.findRng] = [findStr, startFindRng]
249 # Perform forward find and store found region
250 return storage.findRng = find.find(vim.window, find.findStr, storage.findRng, find.DIRECTION_FORWARDS)
252 # Switch into find mode with highlighting
253 command_find_hl = (vim, storage) ->
254 find.injectFind vim.window.document, (findStr) ->
255 # Reset region and find string if new find stirng has arrived
256 return find.highlight(vim.window, findStr)
258 # Search for the last pattern
259 command_find_next = (vim, storage) ->
260 if find.findStr.length > 0
261 storage.findRng = find.find(vim.window, find.findStr, storage.findRng, find.DIRECTION_FORWARDS, true)
263 # Search for the last pattern backwards
264 command_find_prev = (vim, storage) ->
265 if find.findStr.length > 0
266 storage.findRng = find.find(vim.window, find.findStr, storage.findRng, find.DIRECTION_BACKWARDS, true)
268 # Close the Help dialog and cancel the pending hint marker action
269 command_Esc = (vim) ->
270 # Blur active element if it's editable. Other elements
271 # aren't blurred - we don't want to interfere with
272 # the browser too much
273 activeElement = vim.window.document.activeElement
274 if utils.isElementEditable(activeElement)
278 find.removeFind(vim.window.document)
281 hints.removeHints(vim.window.document)
284 help.removeHelp(vim.window.document)
286 # Finally enter normal mode
287 vim.enterNormalMode()
289 if not getPref('leave_dt_on_esc')
290 if chromeWindow = utils.getRootWindow(vim.window)
291 chromeWindow.DeveloperToolbar.hide()
295 constructor: (@group, @name, @func, keys) ->
297 if isPrefSet(@prefName('keys'))
298 try @keyValues = JSON.parse(getPref(@prefName('keys')))
302 # Check if this command may match given string if more chars are added
304 return @keys.reduce(((m, v) -> m or v.indexOf(value) == 0), false)
306 # Check is this command matches given string
308 return @keys.reduce(((m, v) -> m or v == value), false)
310 # Name of the preference for a given property
311 prefName: (value) -> "commands.#{ @name }.#{ value }"
314 @keys = value or @defaultKeys
315 setPref(@prefName('keys'), value and JSON.stringify(value))
318 if value is undefined
319 return getPref(@prefName('enabled'), true)
321 setPref(@prefName('enabled'), !!value)
324 if value is undefined
327 @keyValues = value or @defaultKeyValues
328 setPref(@prefName('keys'), value and JSON.stringify(value))
330 help: -> _("help_command_#{ @name }")
333 new Command('urls', 'focus', command_focus, ['o'])
334 new Command('urls', 'focus_search', command_focus_search, ['O'])
335 new Command('urls', 'paste', command_paste, ['p'])
336 new Command('urls', 'paste_tab', command_paste_tab, ['P'])
337 new Command('urls', 'marker_yank', command_marker_yank, ['y,f'])
338 new Command('urls', 'marker_focus', command_marker_focus, ['v,f'])
339 new Command('urls', 'yank', command_yank, ['y,y'])
340 new Command('urls', 'reload', command_reload, ['r'])
341 new Command('urls', 'reload_force', command_reload_force, ['R'])
342 new Command('urls', 'reload_all', command_reload_all, ['a,r'])
343 new Command('urls', 'reload_all_force', command_reload_all_force, ['a,R'])
344 new Command('urls', 'stop', command_stop, ['s'])
345 new Command('urls', 'stop_all', command_stop_all, ['a,s'])
347 new Command('nav', 'scroll_to_top', command_scroll_to_top , ['g,g'])
348 new Command('nav', 'scroll_to_bottom', command_scroll_to_bottom, ['G'])
349 new Command('nav', 'scroll_down', command_scroll_down, ['j', 'c-e'])
350 new Command('nav', 'scroll_up', command_scroll_up, ['k', 'c-y'])
351 new Command('nav', 'scroll_left', command_scroll_left, ['h'])
352 new Command('nav', 'scroll_right', command_scroll_right , ['l'])
353 new Command('nav', 'scroll_half_page_down', command_scroll_half_page_down, ['d'])
354 new Command('nav', 'scroll_half_page_up', command_scroll_half_page_up, ['u'])
355 new Command('nav', 'scroll_page_down', command_scroll_page_down, ['c-f'])
356 new Command('nav', 'scroll_page_up', command_scroll_page_up, ['c-b'])
358 new Command('tabs', 'open_tab', command_open_tab, ['t'])
359 new Command('tabs', 'tab_prev', command_tab_prev, ['J', 'g,T'])
360 new Command('tabs', 'tab_next', command_tab_next, ['K', 'g,t'])
361 new Command('tabs', 'tab_move_left', command_tab_move_left, ['c-J'])
362 new Command('tabs', 'tab_move_right', command_tab_move_right, ['c-K'])
363 new Command('tabs', 'home', command_home, ['g,h'])
364 new Command('tabs', 'tab_first', command_tab_first, ['g,H', 'g,\^'])
365 new Command('tabs', 'tab_last', command_tab_last, ['g,L', 'g,$'])
366 new Command('tabs', 'close_tab', command_close_tab, ['x'])
367 new Command('tabs', 'restore_tab', command_restore_tab, ['X'])
369 new Command('browse', 'follow', command_follow, ['f'])
370 new Command('browse', 'follow_in_tab', command_follow_in_tab, ['F'])
371 new Command('browse', 'back', command_back, ['H'])
372 new Command('browse', 'forward', command_forward, ['L'])
374 new Command('misc', 'find', command_find, ['/'])
375 new Command('misc', 'find_hl', command_find_hl, ['a,/'])
376 new Command('misc', 'find_next', command_find_next, ['n'])
377 new Command('misc', 'find_prev', command_find_prev, ['N'])
378 new Command('misc', 'help', command_help, ['?'])
379 new Command('misc', 'Esc', command_Esc, ['Esc'])
380 new Command('misc', 'dev', command_dev, [':'])
383 exports.commands = commands