2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
4 # Copyright Wang Zhuochun 2013, 2014.
6 # This file is part of VimFx.
8 # VimFx is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # VimFx is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
22 utils = require('./utils')
23 help = require('./help')
28 , isPrefSet } = require('./prefs')
30 { classes: Cc, interfaces: Ci, utils: Cu } = Components
32 # “Selecting an element” means “focusing and selecting the text, if any, of an
35 # Select the Address Bar.
36 command_focus = (vim) ->
37 # This function works even if the Address Bar has been removed.
38 vim.rootWindow.focusAndSelectUrlBar()
40 # Select the Search Bar.
41 command_focus_search = (vim) ->
42 # The `.webSearch()` method opens a search engine in a tab if the Search Bar
43 # has been removed. Therefore we first check if it exists.
44 if vim.rootWindow.BrowserSearch.searchBar
45 vim.rootWindow.BrowserSearch.webSearch()
47 helper_paste = (vim) ->
48 url = vim.rootWindow.readFromClipboard()
50 if not utils.isURL(url) and submission = utils.browserSearchSubmission(url)
51 url = submission.uri.spec
52 { postData } = submission
53 return {url, postData}
55 # Go to or search for the contents of the system clipboard.
56 command_paste = (vim) ->
57 { url, postData } = helper_paste(vim)
58 vim.rootWindow.gBrowser.loadURIWithFlags(url, null, null, null, postData)
60 # Go to or search for the contents of the system clipboard in a new tab.
61 command_paste_tab = (vim) ->
62 { url, postData } = helper_paste(vim)
63 vim.rootWindow.gBrowser.selectedTab =
64 vim.rootWindow.gBrowser.addTab(url, null, null, postData, null, false)
66 # Copy the URL or text of a marker element to the system clipboard.
67 command_marker_yank = (vim) ->
68 callback = (marker) ->
69 if url = marker.element.href
70 marker.element.focus()
71 utils.writeToClipboard(url)
72 else if utils.isTextInputElement(marker.element)
73 utils.writeToClipboard(marker.element.value)
75 vim.enterMode('hints', callback)
78 command_marker_focus = (vim) ->
79 callback = (marker) -> marker.element.focus()
81 vim.enterMode('hints', callback)
83 # Copy the current URL to the system clipboard.
84 command_yank = (vim) ->
85 utils.writeToClipboard(vim.window.location.href)
87 # Reload the current tab, possibly from cache.
88 command_reload = (vim) ->
89 vim.rootWindow.BrowserReload()
91 # Reload the current tab, skipping cache.
92 command_reload_force = (vim) ->
93 vim.rootWindow.BrowserReloadSkipCache()
95 # Reload all tabs, possibly from cache.
96 command_reload_all = (vim) ->
97 vim.rootWindow.gBrowser.reloadAllTabs()
99 # Reload all tabs, skipping cache.
100 command_reload_all_force = (vim) ->
101 for tab in vim.rootWindow.gBrowser.visibleTabs
102 window = tab.linkedBrowser.contentWindow
103 window.location.reload(true)
105 # Stop loading the current tab.
106 command_stop = (vim) ->
109 # Stop loading all tabs.
110 command_stop_all = (vim) ->
111 for tab in vim.rootWindow.gBrowser.visibleTabs
112 window = tab.linkedBrowser.contentWindow
115 # Scroll to the top of the page.
116 command_scroll_to_top = (vim) ->
117 vim.rootWindow.goDoCommand('cmd_scrollTop')
119 # Scroll to the bottom of the page.
120 command_scroll_to_bottom = (vim) ->
121 vim.rootWindow.goDoCommand('cmd_scrollBottom')
124 command_scroll_down = (vim, event, count) ->
125 step = getPref('scroll_step_lines') * count
126 utils.simulateWheel(vim.window, 0, +step, utils.WHEEL_MODE_LINE)
129 command_scroll_up = (vim, event, count) ->
130 step = getPref('scroll_step_lines') * count
131 utils.simulateWheel(vim.window, 0, -step, utils.WHEEL_MODE_LINE)
134 command_scroll_left = (vim, event, count) ->
135 step = getPref('scroll_step_lines') * count
136 utils.simulateWheel(vim.window, -step, 0, utils.WHEEL_MODE_LINE)
138 # Scroll right a bit.
139 command_scroll_right = (vim, event, count) ->
140 step = getPref('scroll_step_lines') * count
141 utils.simulateWheel(vim.window, +step, 0, utils.WHEEL_MODE_LINE)
143 # Scroll down half a page.
144 command_scroll_half_page_down = (vim, event, count) ->
145 utils.simulateWheel(vim.window, 0, +0.5 * count, utils.WHEEL_MODE_PAGE)
147 # Scroll up half a page.
148 command_scroll_half_page_up = (vim, event, count) ->
149 utils.simulateWheel(vim.window, 0, -0.5 * count, utils.WHEEL_MODE_PAGE)
151 # Scroll down full a page.
152 command_scroll_page_down = (vim, event, count) ->
153 utils.simulateWheel(vim.window, 0, +1 * count, utils.WHEEL_MODE_PAGE)
155 # Scroll up full a page.
156 command_scroll_page_up = (vim, event, count) ->
157 utils.simulateWheel(vim.window, 0, -1 * count, utils.WHEEL_MODE_PAGE)
159 # Open a new tab and select the Address Bar.
160 command_open_tab = (vim) ->
161 vim.rootWindow.BrowserOpenTab()
163 absoluteTabIndex = (relativeIndex, gBrowser) ->
164 tabs = gBrowser.visibleTabs
165 { selectedTab } = gBrowser
167 currentIndex = tabs.indexOf(selectedTab)
168 absoluteIndex = currentIndex + relativeIndex
169 numTabs = tabs.length
171 wrap = (Math.abs(relativeIndex) == 1)
173 absoluteIndex %%= numTabs
175 absoluteIndex = Math.max(0, absoluteIndex)
176 absoluteIndex = Math.min(absoluteIndex, numTabs - 1)
180 helper_switch_tab = (direction, vim, event, count) ->
181 { gBrowser } = vim.rootWindow
182 gBrowser.selectTabAtIndex(absoluteTabIndex(direction * count, gBrowser))
184 # Switch to the previous tab.
185 command_tab_prev = helper_switch_tab.bind(undefined, -1)
187 # Switch to the next tab.
188 command_tab_next = helper_switch_tab.bind(undefined, +1)
190 helper_move_tab = (direction, vim, event, count) ->
191 { gBrowser } = vim.rootWindow
192 { selectedTab } = gBrowser
193 { pinned } = selectedTab
195 index = absoluteTabIndex(direction * count, gBrowser)
197 if index < gBrowser._numPinnedTabs
198 gBrowser.pinTab(selectedTab) unless pinned
200 gBrowser.unpinTab(selectedTab) if pinned
202 gBrowser.moveTabTo(selectedTab, index)
204 # Move the current tab backward.
205 command_tab_move_left = helper_move_tab.bind(undefined, -1)
207 # Move the current tab forward.
208 command_tab_move_right = helper_move_tab.bind(undefined, +1)
210 # Load the home page.
211 command_home = (vim) ->
212 vim.rootWindow.BrowserHome()
214 # Switch to the first tab.
215 command_tab_first = (vim) ->
216 vim.rootWindow.gBrowser.selectTabAtIndex(0)
218 # Switch to the first non-pinned tab.
219 command_tab_first_non_pinned = (vim) ->
220 firstNonPinned = vim.rootWindow.gBrowser._numPinnedTabs
221 vim.rootWindow.gBrowser.selectTabAtIndex(firstNonPinned)
223 # Switch to the last tab.
224 command_tab_last = (vim) ->
225 vim.rootWindow.gBrowser.selectTabAtIndex(-1)
228 command_toggle_pin_tab = (vim) ->
229 currentTab = vim.rootWindow.gBrowser.selectedTab
232 vim.rootWindow.gBrowser.unpinTab(currentTab)
234 vim.rootWindow.gBrowser.pinTab(currentTab)
236 # Duplicate current tab.
237 command_duplicate_tab = (vim) ->
238 { gBrowser } = vim.rootWindow
239 gBrowser.duplicateTab(gBrowser.selectedTab)
241 # Close all tabs from current to the end.
242 command_close_tabs_to_end = (vim) ->
243 { gBrowser } = vim.rootWindow
244 gBrowser.removeTabsToTheEndFrom(gBrowser.selectedTab)
246 # Close all tabs except the current.
247 command_close_other_tabs = (vim) ->
248 { gBrowser } = vim.rootWindow
249 gBrowser.removeAllTabsBut(gBrowser.selectedTab)
252 command_close_tab = (vim, event, count) ->
253 { gBrowser } = vim.rootWindow
254 return if gBrowser.selectedTab.pinned
255 currentIndex = gBrowser.visibleTabs.indexOf(gBrowser.selectedTab)
256 for tab in gBrowser.visibleTabs[currentIndex...(currentIndex + count)]
257 gBrowser.removeTab(tab)
259 # Restore last closed tab.
260 command_restore_tab = (vim, event, count) ->
261 vim.rootWindow.undoCloseTab() for [1..count]
263 helper_follow = ({ inTab, multiple }, vim, event, count) ->
264 callback = (matchedMarker, markers) ->
265 if matchedMarker.element.target == '_blank'
266 targetReset = matchedMarker.element.target
267 matchedMarker.element.target = ''
269 matchedMarker.element.focus()
271 inTab = if count > 1 then true else inTab
272 utils.simulateClick(matchedMarker.element, {metaKey: inTab, ctrlKey: inTab})
274 matchedMarker.element.target = targetReset if targetReset
277 isEditable = utils.isElementEditable(matchedMarker.element)
278 if (multiple or count > 0) and not isEditable
279 # By not resetting immediately one is able to see the last char being
280 # matched, which gives some nice visual feedback that you've typed the
282 vim.window.setTimeout((-> marker.reset() for marker in markers), 100)
285 vim.enterMode('hints', callback)
287 # Follow links with hint markers.
288 command_follow = helper_follow.bind(undefined, {inTab: false})
290 # Follow links in a new Tab with hint markers.
291 command_follow_in_tab = helper_follow.bind(undefined, {inTab: true})
293 # Follow multiple links with hint markers.
294 command_follow_multiple = helper_follow.bind(undefined,
295 {inTab: true, multiple: true})
297 helper_follow_pattern = do ->
298 # Search for the prev/next patterns in the following attributes of the
299 # element. `rel` should be kept as the first attribute, since the standard
300 # way of marking up prev/next links (`rel="prev"` and `rel="next"`) should be
301 # favored. Even though some of these attributes only allow a fixed set of
302 # keywords, we pattern-match them anyways since lots of sites don’t follow
303 # the spec and use the attributes arbitrarily.
304 attrs = ['rel', 'role', 'data-tooltip', 'aria-label']
306 return (type, vim) ->
307 links = utils.getMarkableElements(vim.window.document, {type: 'action'})
308 .filter(utils.isElementVisible)
310 patterns = utils.splitListString(getComplexPref("#{ type }_patterns"))
312 if matchingLink = utils.getBestPatternMatch(patterns, attrs, links)
313 utils.simulateClick(matchingLink, {metaKey: false, ctrlKey: false})
315 # Follow previous page.
316 command_follow_prev = helper_follow_pattern.bind(undefined, 'prev')
319 command_follow_next = helper_follow_pattern.bind(undefined, 'next')
321 # Go up one level in the URL hierarchy.
322 command_go_up_path = (vim, event, count) ->
323 { pathname } = vim.window.location
324 vim.window.location.pathname = pathname.replace(
325 /// (?: /[^/]+ ){1,#{ count }} /?$ ///, ''
328 # Go up to root of the URL hierarchy.
329 command_go_to_root = (vim) ->
330 vim.window.location.href = vim.window.location.origin
332 helper_go_history = (num, vim, event, count) ->
333 { index } = vim.rootWindow.getWebNavigation().sessionHistory
334 { history } = vim.window
336 num = Math.max(num, -index)
337 num = Math.min(num, history.length - 1 - index)
341 # Go back in history.
342 command_back = helper_go_history.bind(undefined, -1)
344 # Go forward in history.
345 command_forward = helper_go_history.bind(undefined, +1)
347 findStorage = {lastSearchString: ''}
349 helper_find = (highlight, vim) ->
350 findBar = vim.rootWindow.gBrowser.getFindBar()
352 findBar.onFindCommand()
353 findBar._findField.focus()
354 findBar._findField.select()
356 return unless highlightButton = findBar.getElement('highlight')
357 if highlightButton.checked != highlight
358 highlightButton.click()
360 # Open the find bar, making sure that hightlighting is off.
361 command_find = helper_find.bind(undefined, false)
363 # Open the find bar, making sure that hightlighting is on.
364 command_find_hl = helper_find.bind(undefined, true)
366 helper_find_again = (direction, vim) ->
367 findBar = vim.rootWindow.gBrowser.getFindBar()
368 if findStorage.lastSearchString.length > 0
369 findBar._findField.value = findStorage.lastSearchString
370 findBar.onFindAgainCommand(direction)
372 # Search for the last pattern.
373 command_find_next = helper_find_again.bind(undefined, false)
375 # Search for the last pattern backwards.
376 command_find_prev = helper_find_again.bind(undefined, true)
379 command_insert_mode = (vim) ->
380 vim.enterMode('insert')
382 # Display the Help Dialog.
383 command_help = (vim) ->
384 help.injectHelp(vim.window.document, commands)
386 # Open and select the Developer Toolbar.
387 command_dev = (vim) ->
388 vim.rootWindow.DeveloperToolbar.show(true) # focus
390 command_Esc = (vim, event) ->
391 utils.blurActiveElement(vim.window)
393 # Blur active XUL control.
394 callback = -> event.originalTarget?.ownerDocument?.activeElement?.blur()
395 vim.window.setTimeout(callback, 0)
397 help.removeHelp(vim.window.document)
399 vim.rootWindow.DeveloperToolbar.hide()
401 vim.rootWindow.gBrowser.getFindBar().close()
403 vim.rootWindow.TabView.hide()
407 constructor: (@group, @name, @func, keys) ->
409 if isPrefSet(@prefName('keys'))
410 try @keyValues = JSON.parse(getPref(@prefName('keys')))
414 # Name of the preference for a given property.
415 prefName: (value) -> "commands.#{ @name }.#{ value }"
418 if value == undefined
421 @keyValues = value or @defaultKeyValues
422 setPref(@prefName('keys'), value and JSON.stringify(value))
424 help: -> _("help_command_#{ @name }")
426 # coffeelint: disable=max_line_length
428 new Command('urls', 'focus', command_focus, ['o'])
429 new Command('urls', 'focus_search', command_focus_search, ['O'])
430 new Command('urls', 'paste', command_paste, ['p'])
431 new Command('urls', 'paste_tab', command_paste_tab, ['P'])
432 new Command('urls', 'marker_yank', command_marker_yank, ['y,f'])
433 new Command('urls', 'marker_focus', command_marker_focus, ['v,f'])
434 new Command('urls', 'yank', command_yank, ['y,y'])
435 new Command('urls', 'reload', command_reload, ['r'])
436 new Command('urls', 'reload_force', command_reload_force, ['R'])
437 new Command('urls', 'reload_all', command_reload_all, ['a,r'])
438 new Command('urls', 'reload_all_force', command_reload_all_force, ['a,R'])
439 new Command('urls', 'stop', command_stop, ['s'])
440 new Command('urls', 'stop_all', command_stop_all, ['a,s'])
442 new Command('nav', 'scroll_to_top', command_scroll_to_top , ['g,g'])
443 new Command('nav', 'scroll_to_bottom', command_scroll_to_bottom, ['G'])
444 new Command('nav', 'scroll_down', command_scroll_down, ['j'])
445 new Command('nav', 'scroll_up', command_scroll_up, ['k'])
446 new Command('nav', 'scroll_left', command_scroll_left, ['h'])
447 new Command('nav', 'scroll_right', command_scroll_right , ['l'])
448 new Command('nav', 'scroll_half_page_down', command_scroll_half_page_down, ['d'])
449 new Command('nav', 'scroll_half_page_up', command_scroll_half_page_up, ['u'])
450 new Command('nav', 'scroll_page_down', command_scroll_page_down, ['Space'])
451 new Command('nav', 'scroll_page_up', command_scroll_page_up, ['Shift-Space'])
453 new Command('tabs', 'open_tab', command_open_tab, ['t'])
454 new Command('tabs', 'tab_prev', command_tab_prev, ['J', 'g,T'])
455 new Command('tabs', 'tab_next', command_tab_next, ['K', 'g,t'])
456 new Command('tabs', 'tab_move_left', command_tab_move_left, ['g,J'])
457 new Command('tabs', 'tab_move_right', command_tab_move_right, ['g,K'])
458 new Command('tabs', 'home', command_home, ['g,h'])
459 new Command('tabs', 'tab_first', command_tab_first, ['g,H', 'g,0'])
460 new Command('tabs', 'tab_first_non_pinned', command_tab_first_non_pinned, ['g,^'])
461 new Command('tabs', 'tab_last', command_tab_last, ['g,L', 'g,$'])
462 new Command('tabs', 'toggle_pin_tab', command_toggle_pin_tab, ['g,p'])
463 new Command('tabs', 'duplicate_tab', command_duplicate_tab, ['y,t'])
464 new Command('tabs', 'close_tabs_to_end', command_close_tabs_to_end, ['g,x,$'])
465 new Command('tabs', 'close_other_tabs', command_close_other_tabs, ['g,x,a'])
466 new Command('tabs', 'close_tab', command_close_tab, ['x'])
467 new Command('tabs', 'restore_tab', command_restore_tab, ['X'])
469 new Command('browse', 'follow', command_follow, ['f'])
470 new Command('browse', 'follow_in_tab', command_follow_in_tab, ['F'])
471 new Command('browse', 'follow_multiple', command_follow_multiple, ['a,f'])
472 new Command('browse', 'follow_previous', command_follow_prev, ['['])
473 new Command('browse', 'follow_next', command_follow_next, [']'])
474 new Command('browse', 'go_up_path', command_go_up_path, ['g,u'])
475 new Command('browse', 'go_to_root', command_go_to_root, ['g,U'])
476 new Command('browse', 'back', command_back, ['H'])
477 new Command('browse', 'forward', command_forward, ['L'])
479 new Command('misc', 'find', command_find, ['/'])
480 new Command('misc', 'find_hl', command_find_hl, ['a,/'])
481 new Command('misc', 'find_next', command_find_next, ['n'])
482 new Command('misc', 'find_prev', command_find_prev, ['N'])
483 new Command('misc', 'insert_mode', command_insert_mode, ['i'])
484 new Command('misc', 'help', command_help, ['?'])
485 new Command('misc', 'dev', command_dev, [':'])
488 new Command('misc', 'Esc', command_Esc, ['Esc'])
490 # coffeelint: enable=max_line_length
492 searchForMatchingCommand = (keys) ->
493 for index in [0...keys.length] by 1
494 str = keys[index..].join(',')
495 for command in commands
496 for key in command.keys()
497 # The following hack is a workaround for the issue where letter `c` is
498 # considered a start of command with control modifier `c-xxx`.
499 if "#{ key },".startsWith("#{ str },")
500 numbers = keys[0..index].join('').match(/[1-9]\d*/g)
502 # When letter 0 follows after a number, it is considered as number 0
503 # instead of a valid command.
504 continue if key == '0' and numbers
506 count = parseInt(numbers[numbers.length - 1], 10) if numbers
507 count = if count > 1 then count else 1
509 return {match: true, exact: (key == str), command, count}
511 return {match: false}
513 isEscCommandKey = (keyStr) -> keyStr in escapeCommand.keys()
515 exports.commands = commands
516 exports.searchForMatchingCommand = searchForMatchingCommand
517 exports.isEscCommandKey = isEscCommandKey
518 exports.findStorage = findStorage