1 utils = require 'utils'
8 , getFirefoxPref } = require 'prefs'
10 { classes: Cc, interfaces: Ci, utils: Cu } = Components
12 # “Selecting an element” means “focusing and selecting the text, if any, of an
15 # Select the Address Bar
16 command_focus = (vim) ->
17 # This function works even if the Address Bar has been removed.
18 vim.rootWindow.focusAndSelectUrlBar()
20 # Select the Search Bar
21 command_focus_search = (vim) ->
22 # The `.webSearch()` method opens a search engine in a tab if the Search Bar
23 # has been removed. Therefore we first check if it exists.
24 if vim.rootWindow.BrowserSearch.searchBar
25 vim.rootWindow.BrowserSearch.webSearch()
27 helper_paste = (vim) ->
28 url = utils.readFromClipboard(vim.window)
30 if not utils.isURL(url) and submission = utils.browserSearchSubmission(url)
31 url = submission.uri.spec
32 { postData } = submission
33 return { url, postData }
35 # Go to or search for the contents of the system clipboard
36 command_paste = (vim) ->
37 { url, postData } = helper_paste(vim)
38 vim.rootWindow.gBrowser.loadURIWithFlags(url, null, null, null, postData)
40 # Go to or search for the contents of the system clipboard in a new tab
41 command_paste_tab = (vim) ->
42 { url, postData } = helper_paste(vim)
43 vim.rootWindow.gBrowser.selectedTab = vim.rootWindow.gBrowser.addTab(url, null, null, postData, null, false)
45 # Copy the URL or text of a marker element to the system clipboard
46 command_marker_yank = (vim) ->
47 callback = (marker) ->
48 if url = marker.element.href
49 marker.element.focus()
50 utils.writeToClipboard(url)
51 else if utils.isTextInputElement(marker.element)
52 utils.writeToClipboard(marker.element.value)
54 vim.enterMode('hints', callback)
57 command_marker_focus = (vim) ->
58 callback = (marker) -> marker.element.focus()
60 vim.enterMode('hints', callback)
62 # Copy the current URL to the system clipboard
63 command_yank = (vim) ->
64 utils.writeToClipboard(vim.window.location.href)
66 # Reload the current tab, possibly from cache
67 command_reload = (vim) ->
68 vim.rootWindow.BrowserReload()
70 # Reload the current tab, skipping cache
71 command_reload_force = (vim) ->
72 vim.rootWindow.BrowserReloadSkipCache()
74 # Reload all tabs, possibly from cache
75 command_reload_all = (vim) ->
76 vim.rootWindow.gBrowser.reloadAllTabs()
78 # Reload all tabs, skipping cache
79 command_reload_all_force = (vim) ->
80 for tab in vim.rootWindow.gBrowser.visibleTabs
81 window = tab.linkedBrowser.contentWindow
82 window.location.reload(true)
84 # Stop loading the current tab
85 command_stop = (vim) ->
88 # Stop loading all tabs
89 command_stop_all = (vim) ->
90 for tab in vim.rootWindow.gBrowser.visibleTabs
91 window = tab.linkedBrowser.contentWindow
94 # Scroll to the top of the page
95 command_scroll_to_top = (vim) ->
96 vim.rootWindow.goDoCommand('cmd_scrollTop')
98 # Scroll to the bottom of the page
99 command_scroll_to_bottom = (vim) ->
100 vim.rootWindow.goDoCommand('cmd_scrollBottom')
103 command_scroll_down = (vim) ->
104 utils.simulateWheel(vim.window, 0, getPref('scroll_step_lines'), utils.WHEEL_MODE_LINE)
107 command_scroll_up = (vim) ->
108 utils.simulateWheel(vim.window, 0, -getPref('scroll_step_lines'), utils.WHEEL_MODE_LINE)
111 command_scroll_left = (vim) ->
112 utils.simulateWheel(vim.window, -getPref('scroll_step_lines'), 0, utils.WHEEL_MODE_LINE)
115 command_scroll_right = (vim) ->
116 utils.simulateWheel(vim.window, getPref('scroll_step_lines'), 0, utils.WHEEL_MODE_LINE)
118 # Scroll down half a page
119 command_scroll_half_page_down = (vim) ->
120 utils.simulateWheel(vim.window, 0, 0.5, utils.WHEEL_MODE_PAGE)
122 # Scroll up half a page
123 command_scroll_half_page_up = (vim) ->
124 utils.simulateWheel(vim.window, 0, -0.5, utils.WHEEL_MODE_PAGE)
126 # Scroll down full a page
127 command_scroll_page_down = (vim) ->
128 utils.simulateWheel(vim.window, 0, 1, utils.WHEEL_MODE_PAGE)
130 # Scroll up full a page
131 command_scroll_page_up = (vim) ->
132 utils.simulateWheel(vim.window, 0, -1, utils.WHEEL_MODE_PAGE)
134 # Open a new tab and select the Address Bar
135 command_open_tab = (vim) ->
136 vim.rootWindow.BrowserOpenTab()
138 # Switch to the previous tab
139 command_tab_prev = (vim) ->
140 vim.rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true) # `true` to allow wrapping
142 # Switch to the next tab
143 command_tab_next = (vim) ->
144 vim.rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true) # `true` to allow wrapping
146 # Move the current tab backward
147 command_tab_move_left = (vim) ->
148 { gBrowser } = vim.rootWindow
149 lastIndex = gBrowser.tabContainer.selectedIndex
150 gBrowser.moveTabBackward()
151 if gBrowser.tabContainer.selectedIndex == lastIndex
152 gBrowser.moveTabToEnd()
154 # Move the current tab forward
155 command_tab_move_right = (vim) ->
156 { gBrowser } = vim.rootWindow
157 lastIndex = gBrowser.tabContainer.selectedIndex
158 gBrowser.moveTabForward()
159 if gBrowser.tabContainer.selectedIndex == lastIndex
160 gBrowser.moveTabToStart()
163 command_home = (vim) ->
164 vim.rootWindow.BrowserHome()
166 # Switch to the first tab
167 command_tab_first = (vim) ->
168 vim.rootWindow.gBrowser.selectTabAtIndex(0)
170 # Switch to the last tab
171 command_tab_last = (vim) ->
172 vim.rootWindow.gBrowser.selectTabAtIndex(-1)
175 command_close_tab = (vim) ->
176 unless vim.rootWindow.gBrowser.selectedTab.pinned
177 vim.rootWindow.gBrowser.removeCurrentTab()
179 # Restore last closed tab
180 command_restore_tab = (vim) ->
181 vim.rootWindow.undoCloseTab()
183 helper_follow = ({ inTab, multiple }, vim) ->
184 callback = (matchedMarker, markers) ->
185 matchedMarker.element.focus()
186 utils.simulateClick(matchedMarker.element, {metaKey: inTab, ctrlKey: inTab})
187 isEditable = utils.isElementEditable(matchedMarker.element)
188 if multiple and not isEditable
189 # By not resetting immediately one is able to see the last char being matched, which gives
190 # some nice visual feedback that you've typed the right char.
191 vim.window.setTimeout((-> marker.reset() for marker in markers), 100)
194 vim.enterMode('hints', callback)
196 # Follow links with hint markers
197 command_follow = helper_follow.bind(undefined, {inTab: false})
199 # Follow links in a new Tab with hint markers
200 command_follow_in_tab = helper_follow.bind(undefined, {inTab: true})
202 # Follow multiple links with hint markers
203 command_follow_multiple = helper_follow.bind(undefined, {inTab: true, multiple: true})
205 helper_follow_pattern = do ->
206 # Search for the prev/next patterns in the following attributes of the element.
207 # `rel` should be kept as the first attribute, since the standard way of
208 # marking up prev/next links (`rel="prev"` and `rel="next"`) should be
209 # favored. Even though some of these attributes only allow a fixed set of
210 # keywords, we pattern-match them anyways since lots of sites don’t follow
211 # the spec and use the attributes arbitrarily.
212 attrs = [ 'rel', 'role', 'data-tooltip', 'aria-label' ]
214 return (type, vim) ->
215 links = utils.getMarkableElements(vim.window.document, {type: 'action'})
216 .filter(utils.isElementVisible)
218 patterns = utils.splitListString(getComplexPref("#{ type }_patterns"))
220 if matchingLink = utils.getBestPatternMatch(patterns, attrs, links)
221 utils.simulateClick(matchingLink, {metaKey: false, ctrlKey: false})
223 # Follow previous page
224 command_follow_prev = helper_follow_pattern.bind(undefined, 'prev')
227 command_follow_next = helper_follow_pattern.bind(undefined, 'next')
229 # Go up one level in the URL hierarchy
230 command_go_up_path = (vim) ->
231 path = vim.window.location.pathname
232 vim.window.location.pathname = path.replace(/// / [^/]+ /?$ ///, '')
234 # Go up to root of the URL hierarchy
235 command_go_to_root = (vim) ->
236 vim.window.location.href = vim.window.location.origin
239 command_back = (vim) ->
240 vim.rootWindow.BrowserBack()
242 # Go forward in history
243 command_forward = (vim) ->
244 vim.rootWindow.BrowserForward()
246 findStorage = { lastSearchString: '' }
248 # Switch into find mode
249 command_find = (vim) ->
250 vim.enterMode('find', { highlight: false })
252 # Switch into find mode with highlighting
253 command_find_hl = (vim) ->
254 vim.enterMode('find', { highlight: true })
256 # Search for the last pattern
257 command_find_next = (vim) ->
258 if findBar = vim.rootWindow.gBrowser.getFindBar()
259 if findStorage.lastSearchString.length > 0
260 findBar._findField.value = findStorage.lastSearchString
261 findBar.onFindAgainCommand(false)
263 # Search for the last pattern backwards
264 command_find_prev = (vim) ->
265 if findBar = vim.rootWindow.gBrowser.getFindBar()
266 if findStorage.lastSearchString.length > 0
267 findBar._findField.value = findStorage.lastSearchString
268 findBar.onFindAgainCommand(true)
271 command_insert_mode = (vim) ->
272 vim.enterMode('insert')
274 # Display the Help Dialog
275 command_help = (vim) ->
276 help.injectHelp(vim.window.document, commands)
278 # Open and select the Developer Toolbar
279 command_dev = (vim) ->
280 vim.rootWindow.DeveloperToolbar.show(true)
281 vim.rootWindow.DeveloperToolbar.focus()
283 command_Esc = (vim, event) ->
284 utils.blurActiveElement(vim.window)
286 # Blur active XUL control
287 callback = -> event.originalTarget?.ownerDocument?.activeElement?.blur()
288 vim.window.setTimeout(callback, 0)
290 help.removeHelp(vim.window.document)
292 vim.rootWindow.DeveloperToolbar.hide()
294 vim.rootWindow.gBrowser.getFindBar()?.close()
296 vim.rootWindow.TabView.hide()
300 constructor: (@group, @name, @func, keys) ->
302 if isPrefSet(@prefName('keys'))
303 try @keyValues = JSON.parse(getPref(@prefName('keys')))
307 # Name of the preference for a given property
308 prefName: (value) -> "commands.#{ @name }.#{ value }"
311 if value is undefined
314 @keyValues = value or @defaultKeyValues
315 setPref(@prefName('keys'), value and JSON.stringify(value))
317 help: -> _("help_command_#{ @name }")
320 new Command('urls', 'focus', command_focus, ['o'])
321 new Command('urls', 'focus_search', command_focus_search, ['O'])
322 new Command('urls', 'paste', command_paste, ['p'])
323 new Command('urls', 'paste_tab', command_paste_tab, ['P'])
324 new Command('urls', 'marker_yank', command_marker_yank, ['y,f'])
325 new Command('urls', 'marker_focus', command_marker_focus, ['v,f'])
326 new Command('urls', 'yank', command_yank, ['y,y'])
327 new Command('urls', 'reload', command_reload, ['r'])
328 new Command('urls', 'reload_force', command_reload_force, ['R'])
329 new Command('urls', 'reload_all', command_reload_all, ['a,r'])
330 new Command('urls', 'reload_all_force', command_reload_all_force, ['a,R'])
331 new Command('urls', 'stop', command_stop, ['s'])
332 new Command('urls', 'stop_all', command_stop_all, ['a,s'])
334 new Command('nav', 'scroll_to_top', command_scroll_to_top , ['g,g'])
335 new Command('nav', 'scroll_to_bottom', command_scroll_to_bottom, ['G'])
336 new Command('nav', 'scroll_down', command_scroll_down, ['j', 'c-e'])
337 new Command('nav', 'scroll_up', command_scroll_up, ['k', 'c-y'])
338 new Command('nav', 'scroll_left', command_scroll_left, ['h'])
339 new Command('nav', 'scroll_right', command_scroll_right , ['l'])
340 new Command('nav', 'scroll_half_page_down', command_scroll_half_page_down, ['d'])
341 new Command('nav', 'scroll_half_page_up', command_scroll_half_page_up, ['u'])
342 new Command('nav', 'scroll_page_down', command_scroll_page_down, ['c-f'])
343 new Command('nav', 'scroll_page_up', command_scroll_page_up, ['c-b'])
345 new Command('tabs', 'open_tab', command_open_tab, ['t'])
346 new Command('tabs', 'tab_prev', command_tab_prev, ['J', 'g,T'])
347 new Command('tabs', 'tab_next', command_tab_next, ['K', 'g,t'])
348 new Command('tabs', 'tab_move_left', command_tab_move_left, ['c-J'])
349 new Command('tabs', 'tab_move_right', command_tab_move_right, ['c-K'])
350 new Command('tabs', 'home', command_home, ['g,h'])
351 new Command('tabs', 'tab_first', command_tab_first, ['g,H', 'g,^'])
352 new Command('tabs', 'tab_last', command_tab_last, ['g,L', 'g,$'])
353 new Command('tabs', 'close_tab', command_close_tab, ['x'])
354 new Command('tabs', 'restore_tab', command_restore_tab, ['X'])
356 new Command('browse', 'follow', command_follow, ['f'])
357 new Command('browse', 'follow_in_tab', command_follow_in_tab, ['F'])
358 new Command('browse', 'follow_multiple', command_follow_multiple, ['a,f'])
359 new Command('browse', 'follow_previous', command_follow_prev, ['['])
360 new Command('browse', 'follow_next', command_follow_next, [']'])
361 new Command('browse', 'go_up_path', command_go_up_path, ['g,u'])
362 new Command('browse', 'go_to_root', command_go_to_root, ['g,U'])
363 new Command('browse', 'back', command_back, ['H'])
364 new Command('browse', 'forward', command_forward, ['L'])
366 new Command('misc', 'find', command_find, ['/'])
367 new Command('misc', 'find_hl', command_find_hl, ['a,/'])
368 new Command('misc', 'find_next', command_find_next, ['n'])
369 new Command('misc', 'find_prev', command_find_prev, ['N'])
370 new Command('misc', 'insert_mode', command_insert_mode, ['i'])
371 new Command('misc', 'help', command_help, ['?'])
372 new Command('misc', 'dev', command_dev, [':'])
375 new Command('misc', 'Esc', command_Esc, ['Esc'])
378 searchForMatchingCommand = (keys) ->
379 for index in [0...keys.length] by 1
380 str = keys[index..].join(',')
381 for command in commands
382 for key in command.keys()
383 # The following hack is a workaround for the issue where # letter `c`
384 # is considered a start of command with control modifier `c-xxx`
385 if "#{key},".startsWith("#{str},")
386 return {match: true, exact: (key == str), command}
388 return {match: false}
390 isEscCommandKey = (keyStr) -> keyStr in escapeCommand.keys()
392 isReturnCommandKey = (keyStr) -> keyStr.contains('Return')
394 exports.commands = commands
395 exports.searchForMatchingCommand = searchForMatchingCommand
396 exports.isEscCommandKey = isEscCommandKey
397 exports.isReturnCommandKey = isReturnCommandKey
398 exports.findStorage = findStorage