]> git.gir.st - VimFx.git/blob - extension/lib/commands.coffee
Add default shortcuts to new commands
[VimFx.git] / extension / lib / commands.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
4 # Copyright Wang Zhuochun 2013, 2014.
5 #
6 # This file is part of VimFx.
7 #
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.
12 #
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.
17 #
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/>.
20 ###
21
22 utils = require('./utils')
23 help = require('./help')
24 _ = require('./l10n')
25 { getPref
26 , getComplexPref
27 , setPref
28 , isPrefSet } = require('./prefs')
29
30 { classes: Cc, interfaces: Ci, utils: Cu } = Components
31
32 # “Selecting an element” means “focusing and selecting the text, if any, of an
33 # element”.
34
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()
39
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()
46
47 helper_paste = (vim) ->
48 url = vim.rootWindow.readFromClipboard()
49 postData = null
50 if not utils.isURL(url) and submission = utils.browserSearchSubmission(url)
51 url = submission.uri.spec
52 { postData } = submission
53 return {url, postData}
54
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)
59
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)
65
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)
74
75 vim.enterMode('hints', callback)
76
77 # Focus element.
78 command_marker_focus = (vim) ->
79 callback = (marker) -> marker.element.focus()
80
81 vim.enterMode('hints', callback)
82
83 # Copy the current URL to the system clipboard.
84 command_yank = (vim) ->
85 utils.writeToClipboard(vim.window.location.href)
86
87 # Reload the current tab, possibly from cache.
88 command_reload = (vim) ->
89 vim.rootWindow.BrowserReload()
90
91 # Reload the current tab, skipping cache.
92 command_reload_force = (vim) ->
93 vim.rootWindow.BrowserReloadSkipCache()
94
95 # Reload all tabs, possibly from cache.
96 command_reload_all = (vim) ->
97 vim.rootWindow.gBrowser.reloadAllTabs()
98
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)
104
105 # Stop loading the current tab.
106 command_stop = (vim) ->
107 vim.window.stop()
108
109 # Stop loading all tabs.
110 command_stop_all = (vim) ->
111 for tab in vim.rootWindow.gBrowser.visibleTabs
112 window = tab.linkedBrowser.contentWindow
113 window.stop()
114
115 # Scroll to the top of the page.
116 command_scroll_to_top = (vim) ->
117 vim.rootWindow.goDoCommand('cmd_scrollTop')
118
119 # Scroll to the bottom of the page.
120 command_scroll_to_bottom = (vim) ->
121 vim.rootWindow.goDoCommand('cmd_scrollBottom')
122
123 # Scroll down a bit.
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)
127
128 # Scroll up a bit.
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)
132
133 # Scroll left a bit.
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)
137
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)
142
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)
146
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)
150
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)
154
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)
158
159 # Open a new tab and select the Address Bar.
160 command_open_tab = (vim) ->
161 vim.rootWindow.BrowserOpenTab()
162
163 absoluteTabIndex = (relativeIndex, gBrowser) ->
164 tabs = gBrowser.visibleTabs
165 { selectedTab } = gBrowser
166
167 currentIndex = tabs.indexOf(selectedTab)
168 absoluteIndex = currentIndex + relativeIndex
169 numTabs = tabs.length
170
171 wrap = (Math.abs(relativeIndex) == 1)
172 if wrap
173 absoluteIndex %%= numTabs
174 else
175 absoluteIndex = Math.max(0, absoluteIndex)
176 absoluteIndex = Math.min(absoluteIndex, numTabs - 1)
177
178 return absoluteIndex
179
180 helper_switch_tab = (direction, vim, event, count) ->
181 { gBrowser } = vim.rootWindow
182 gBrowser.selectTabAtIndex(absoluteTabIndex(direction * count, gBrowser))
183
184 # Switch to the previous tab.
185 command_tab_prev = helper_switch_tab.bind(undefined, -1)
186
187 # Switch to the next tab.
188 command_tab_next = helper_switch_tab.bind(undefined, +1)
189
190 helper_move_tab = (direction, vim, event, count) ->
191 { gBrowser } = vim.rootWindow
192 { selectedTab } = gBrowser
193 { pinned } = selectedTab
194
195 index = absoluteTabIndex(direction * count, gBrowser)
196
197 if index < gBrowser._numPinnedTabs
198 gBrowser.pinTab(selectedTab) unless pinned
199 else
200 gBrowser.unpinTab(selectedTab) if pinned
201
202 gBrowser.moveTabTo(selectedTab, index)
203
204 # Move the current tab backward.
205 command_tab_move_left = helper_move_tab.bind(undefined, -1)
206
207 # Move the current tab forward.
208 command_tab_move_right = helper_move_tab.bind(undefined, +1)
209
210 # Load the home page.
211 command_home = (vim) ->
212 vim.rootWindow.BrowserHome()
213
214 # Switch to the first tab.
215 command_tab_first = (vim) ->
216 vim.rootWindow.gBrowser.selectTabAtIndex(0)
217
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)
222
223 # Switch to the last tab.
224 command_tab_last = (vim) ->
225 vim.rootWindow.gBrowser.selectTabAtIndex(-1)
226
227 # Toggle Pin Tab.
228 command_toggle_pin_tab = (vim) ->
229 currentTab = vim.rootWindow.gBrowser.selectedTab
230
231 if currentTab.pinned
232 vim.rootWindow.gBrowser.unpinTab(currentTab)
233 else
234 vim.rootWindow.gBrowser.pinTab(currentTab)
235
236 # Duplicate current tab.
237 command_duplicate_tab = (vim) ->
238 { gBrowser } = vim.rootWindow
239 gBrowser.duplicateTab(gBrowser.selectedTab)
240
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)
245
246 # Close all tabs except the current.
247 command_close_other_tabs = (vim) ->
248 { gBrowser } = vim.rootWindow
249 gBrowser.removeAllTabsBut(gBrowser.selectedTab)
250
251 # Close current tab.
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)
258
259 # Restore last closed tab.
260 command_restore_tab = (vim, event, count) ->
261 vim.rootWindow.undoCloseTab() for [1..count]
262
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 = ''
268
269 matchedMarker.element.focus()
270
271 inTab = if count > 1 then true else inTab
272 utils.simulateClick(matchedMarker.element, {metaKey: inTab, ctrlKey: inTab})
273
274 matchedMarker.element.target = targetReset if targetReset
275
276 count -= 1
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
281 # right char.
282 vim.window.setTimeout((-> marker.reset() for marker in markers), 100)
283 return true
284
285 vim.enterMode('hints', callback)
286
287 # Follow links with hint markers.
288 command_follow = helper_follow.bind(undefined, {inTab: false})
289
290 # Follow links in a new Tab with hint markers.
291 command_follow_in_tab = helper_follow.bind(undefined, {inTab: true})
292
293 # Follow multiple links with hint markers.
294 command_follow_multiple = helper_follow.bind(undefined,
295 {inTab: true, multiple: true})
296
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']
305
306 return (type, vim) ->
307 links = utils.getMarkableElements(vim.window.document, {type: 'action'})
308 .filter(utils.isElementVisible)
309
310 patterns = utils.splitListString(getComplexPref("#{ type }_patterns"))
311
312 if matchingLink = utils.getBestPatternMatch(patterns, attrs, links)
313 utils.simulateClick(matchingLink, {metaKey: false, ctrlKey: false})
314
315 # Follow previous page.
316 command_follow_prev = helper_follow_pattern.bind(undefined, 'prev')
317
318 # Follow next page.
319 command_follow_next = helper_follow_pattern.bind(undefined, 'next')
320
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 }} /?$ ///, ''
326 )
327
328 # Go up to root of the URL hierarchy.
329 command_go_to_root = (vim) ->
330 vim.window.location.href = vim.window.location.origin
331
332 helper_go_history = (num, vim, event, count) ->
333 { index } = vim.rootWindow.getWebNavigation().sessionHistory
334 { history } = vim.window
335 num *= count
336 num = Math.max(num, -index)
337 num = Math.min(num, history.length - 1 - index)
338 return if num == 0
339 history.go(num)
340
341 # Go back in history.
342 command_back = helper_go_history.bind(undefined, -1)
343
344 # Go forward in history.
345 command_forward = helper_go_history.bind(undefined, +1)
346
347 findStorage = {lastSearchString: ''}
348
349 helper_find = (highlight, vim) ->
350 findBar = vim.rootWindow.gBrowser.getFindBar()
351
352 findBar.onFindCommand()
353 findBar._findField.focus()
354 findBar._findField.select()
355
356 return unless highlightButton = findBar.getElement('highlight')
357 if highlightButton.checked != highlight
358 highlightButton.click()
359
360 # Open the find bar, making sure that hightlighting is off.
361 command_find = helper_find.bind(undefined, false)
362
363 # Open the find bar, making sure that hightlighting is on.
364 command_find_hl = helper_find.bind(undefined, true)
365
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)
371
372 # Search for the last pattern.
373 command_find_next = helper_find_again.bind(undefined, false)
374
375 # Search for the last pattern backwards.
376 command_find_prev = helper_find_again.bind(undefined, true)
377
378 # Enter insert mode.
379 command_insert_mode = (vim) ->
380 vim.enterMode('insert')
381
382 # Display the Help Dialog.
383 command_help = (vim) ->
384 help.injectHelp(vim.window.document, commands)
385
386 # Open and select the Developer Toolbar.
387 command_dev = (vim) ->
388 vim.rootWindow.DeveloperToolbar.show(true) # focus
389
390 command_Esc = (vim, event) ->
391 utils.blurActiveElement(vim.window)
392
393 # Blur active XUL control.
394 callback = -> event.originalTarget?.ownerDocument?.activeElement?.blur()
395 vim.window.setTimeout(callback, 0)
396
397 help.removeHelp(vim.window.document)
398
399 vim.rootWindow.DeveloperToolbar.hide()
400
401 vim.rootWindow.gBrowser.getFindBar().close()
402
403 vim.rootWindow.TabView.hide()
404
405
406 class Command
407 constructor: (@group, @name, @func, keys) ->
408 @defaultKeys = keys
409 if isPrefSet(@prefName('keys'))
410 try @keyValues = JSON.parse(getPref(@prefName('keys')))
411 else
412 @keyValues = keys
413
414 # Name of the preference for a given property.
415 prefName: (value) -> "commands.#{ @name }.#{ value }"
416
417 keys: (value) ->
418 if value == undefined
419 return @keyValues
420 else
421 @keyValues = value or @defaultKeyValues
422 setPref(@prefName('keys'), value and JSON.stringify(value))
423
424 help: -> _("help_command_#{ @name }")
425
426 # coffeelint: disable=max_line_length
427 commands = [
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'])
441
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'])
452
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'])
468
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'])
478
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, [':'])
486
487 escapeCommand =
488 new Command('misc', 'Esc', command_Esc, ['Esc'])
489 ]
490 # coffeelint: enable=max_line_length
491
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)
501
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
505
506 count = parseInt(numbers[numbers.length - 1], 10) if numbers
507 count = if count > 1 then count else 1
508
509 return {match: true, exact: (key == str), command, count}
510
511 return {match: false}
512
513 isEscCommandKey = (keyStr) -> keyStr in escapeCommand.keys()
514
515 exports.commands = commands
516 exports.searchForMatchingCommand = searchForMatchingCommand
517 exports.isEscCommandKey = isEscCommandKey
518 exports.findStorage = findStorage
Imprint / Impressum