]> git.gir.st - VimFx.git/blob - extension/lib/commands.coffee
Merge pull request #438 from lydell/vim-like-keys
[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 notation = require('vim-like-key-notation')
23 legacy = require('./legacy')
24 utils = require('./utils')
25 help = require('./help')
26 _ = require('./l10n')
27 { getPref
28 , getComplexPref
29 , setPref
30 , isPrefSet } = require('./prefs')
31
32 { classes: Cc, interfaces: Ci, utils: Cu } = Components
33
34 # “Selecting an element” means “focusing and selecting the text, if any, of an
35 # element”.
36
37 # Select the Address Bar.
38 command_focus = (vim) ->
39 # This function works even if the Address Bar has been removed.
40 vim.rootWindow.focusAndSelectUrlBar()
41
42 # Select the Search Bar.
43 command_focus_search = (vim) ->
44 # The `.webSearch()` method opens a search engine in a tab if the Search Bar
45 # has been removed. Therefore we first check if it exists.
46 if vim.rootWindow.BrowserSearch.searchBar
47 vim.rootWindow.BrowserSearch.webSearch()
48
49 helper_paste = (vim) ->
50 url = vim.rootWindow.readFromClipboard()
51 postData = null
52 if not utils.isURL(url) and submission = utils.browserSearchSubmission(url)
53 url = submission.uri.spec
54 { postData } = submission
55 return {url, postData}
56
57 # Go to or search for the contents of the system clipboard.
58 command_paste = (vim) ->
59 { url, postData } = helper_paste(vim)
60 vim.rootWindow.gBrowser.loadURIWithFlags(url, null, null, null, postData)
61
62 # Go to or search for the contents of the system clipboard in a new tab.
63 command_paste_tab = (vim) ->
64 { url, postData } = helper_paste(vim)
65 vim.rootWindow.gBrowser.selectedTab =
66 vim.rootWindow.gBrowser.addTab(url, null, null, postData, null, false)
67
68 # Copy the URL or text of a marker element to the system clipboard.
69 command_marker_yank = (vim) ->
70 callback = (marker) ->
71 if url = marker.element.href
72 marker.element.focus()
73 utils.writeToClipboard(url)
74 else if utils.isTextInputElement(marker.element)
75 utils.writeToClipboard(marker.element.value)
76
77 vim.enterMode('hints', callback)
78
79 # Focus element.
80 command_marker_focus = (vim) ->
81 callback = (marker) -> marker.element.focus()
82
83 vim.enterMode('hints', callback)
84
85 # Copy the current URL to the system clipboard.
86 command_yank = (vim) ->
87 utils.writeToClipboard(vim.window.location.href)
88
89 # Reload the current tab, possibly from cache.
90 command_reload = (vim) ->
91 vim.rootWindow.BrowserReload()
92
93 # Reload the current tab, skipping cache.
94 command_reload_force = (vim) ->
95 vim.rootWindow.BrowserReloadSkipCache()
96
97 # Reload all tabs, possibly from cache.
98 command_reload_all = (vim) ->
99 vim.rootWindow.gBrowser.reloadAllTabs()
100
101 # Reload all tabs, skipping cache.
102 command_reload_all_force = (vim) ->
103 for tab in vim.rootWindow.gBrowser.visibleTabs
104 window = tab.linkedBrowser.contentWindow
105 window.location.reload(true)
106
107 # Stop loading the current tab.
108 command_stop = (vim) ->
109 vim.window.stop()
110
111 # Stop loading all tabs.
112 command_stop_all = (vim) ->
113 for tab in vim.rootWindow.gBrowser.visibleTabs
114 window = tab.linkedBrowser.contentWindow
115 window.stop()
116
117 # Scroll to the top of the page.
118 command_scroll_to_top = (vim) ->
119 vim.rootWindow.goDoCommand('cmd_scrollTop')
120
121 # Scroll to the bottom of the page.
122 command_scroll_to_bottom = (vim) ->
123 vim.rootWindow.goDoCommand('cmd_scrollBottom')
124
125 # Scroll down a bit.
126 command_scroll_down = (vim, event, count) ->
127 step = getPref('scroll_step_lines') * count
128 utils.simulateWheel(vim.window, 0, +step, utils.WHEEL_MODE_LINE)
129
130 # Scroll up a bit.
131 command_scroll_up = (vim, event, count) ->
132 step = getPref('scroll_step_lines') * count
133 utils.simulateWheel(vim.window, 0, -step, utils.WHEEL_MODE_LINE)
134
135 # Scroll left a bit.
136 command_scroll_left = (vim, event, count) ->
137 step = getPref('scroll_step_lines') * count
138 utils.simulateWheel(vim.window, -step, 0, utils.WHEEL_MODE_LINE)
139
140 # Scroll right a bit.
141 command_scroll_right = (vim, event, count) ->
142 step = getPref('scroll_step_lines') * count
143 utils.simulateWheel(vim.window, +step, 0, utils.WHEEL_MODE_LINE)
144
145 # Scroll down half a page.
146 command_scroll_half_page_down = (vim, event, count) ->
147 utils.simulateWheel(vim.window, 0, +0.5 * count, utils.WHEEL_MODE_PAGE)
148
149 # Scroll up half a page.
150 command_scroll_half_page_up = (vim, event, count) ->
151 utils.simulateWheel(vim.window, 0, -0.5 * count, utils.WHEEL_MODE_PAGE)
152
153 # Scroll down full a page.
154 command_scroll_page_down = (vim, event, count) ->
155 utils.simulateWheel(vim.window, 0, +1 * count, utils.WHEEL_MODE_PAGE)
156
157 # Scroll up full a page.
158 command_scroll_page_up = (vim, event, count) ->
159 utils.simulateWheel(vim.window, 0, -1 * count, utils.WHEEL_MODE_PAGE)
160
161 # Open a new tab and select the Address Bar.
162 command_open_tab = (vim) ->
163 vim.rootWindow.BrowserOpenTab()
164
165 absoluteTabIndex = (relativeIndex, gBrowser) ->
166 tabs = gBrowser.visibleTabs
167 { selectedTab } = gBrowser
168
169 currentIndex = tabs.indexOf(selectedTab)
170 absoluteIndex = currentIndex + relativeIndex
171 numTabs = tabs.length
172
173 wrap = (Math.abs(relativeIndex) == 1)
174 if wrap
175 absoluteIndex %%= numTabs
176 else
177 absoluteIndex = Math.max(0, absoluteIndex)
178 absoluteIndex = Math.min(absoluteIndex, numTabs - 1)
179
180 return absoluteIndex
181
182 helper_switch_tab = (direction, vim, event, count) ->
183 { gBrowser } = vim.rootWindow
184 gBrowser.selectTabAtIndex(absoluteTabIndex(direction * count, gBrowser))
185
186 # Switch to the previous tab.
187 command_tab_prev = helper_switch_tab.bind(undefined, -1)
188
189 # Switch to the next tab.
190 command_tab_next = helper_switch_tab.bind(undefined, +1)
191
192 helper_move_tab = (direction, vim, event, count) ->
193 { gBrowser } = vim.rootWindow
194 { selectedTab } = gBrowser
195 { pinned } = selectedTab
196
197 index = absoluteTabIndex(direction * count, gBrowser)
198
199 if index < gBrowser._numPinnedTabs
200 gBrowser.pinTab(selectedTab) unless pinned
201 else
202 gBrowser.unpinTab(selectedTab) if pinned
203
204 gBrowser.moveTabTo(selectedTab, index)
205
206 # Move the current tab backward.
207 command_tab_move_left = helper_move_tab.bind(undefined, -1)
208
209 # Move the current tab forward.
210 command_tab_move_right = helper_move_tab.bind(undefined, +1)
211
212 # Load the home page.
213 command_home = (vim) ->
214 vim.rootWindow.BrowserHome()
215
216 # Switch to the first tab.
217 command_tab_first = (vim) ->
218 vim.rootWindow.gBrowser.selectTabAtIndex(0)
219
220 # Switch to the first non-pinned tab.
221 command_tab_first_non_pinned = (vim) ->
222 firstNonPinned = vim.rootWindow.gBrowser._numPinnedTabs
223 vim.rootWindow.gBrowser.selectTabAtIndex(firstNonPinned)
224
225 # Switch to the last tab.
226 command_tab_last = (vim) ->
227 vim.rootWindow.gBrowser.selectTabAtIndex(-1)
228
229 # Toggle Pin Tab.
230 command_toggle_pin_tab = (vim) ->
231 currentTab = vim.rootWindow.gBrowser.selectedTab
232
233 if currentTab.pinned
234 vim.rootWindow.gBrowser.unpinTab(currentTab)
235 else
236 vim.rootWindow.gBrowser.pinTab(currentTab)
237
238 # Duplicate current tab.
239 command_duplicate_tab = (vim) ->
240 { gBrowser } = vim.rootWindow
241 gBrowser.duplicateTab(gBrowser.selectedTab)
242
243 # Close all tabs from current to the end.
244 command_close_tabs_to_end = (vim) ->
245 { gBrowser } = vim.rootWindow
246 gBrowser.removeTabsToTheEndFrom(gBrowser.selectedTab)
247
248 # Close all tabs except the current.
249 command_close_other_tabs = (vim) ->
250 { gBrowser } = vim.rootWindow
251 gBrowser.removeAllTabsBut(gBrowser.selectedTab)
252
253 # Close current tab.
254 command_close_tab = (vim, event, count) ->
255 { gBrowser } = vim.rootWindow
256 return if gBrowser.selectedTab.pinned
257 currentIndex = gBrowser.visibleTabs.indexOf(gBrowser.selectedTab)
258 for tab in gBrowser.visibleTabs[currentIndex...(currentIndex + count)]
259 gBrowser.removeTab(tab)
260
261 # Restore last closed tab.
262 command_restore_tab = (vim, event, count) ->
263 vim.rootWindow.undoCloseTab() for [1..count]
264
265 helper_follow = ({ inTab, multiple }, vim, event, count) ->
266 callback = (matchedMarker, markers) ->
267 if matchedMarker.element.target == '_blank'
268 targetReset = matchedMarker.element.target
269 matchedMarker.element.target = ''
270
271 matchedMarker.element.focus()
272
273 inTab = if count > 1 then true else inTab
274 utils.simulateClick(matchedMarker.element, {metaKey: inTab, ctrlKey: inTab})
275
276 matchedMarker.element.target = targetReset if targetReset
277
278 count -= 1
279 isEditable = utils.isElementEditable(matchedMarker.element)
280 if (multiple or count > 0) and not isEditable
281 # By not resetting immediately one is able to see the last char being
282 # matched, which gives some nice visual feedback that you've typed the
283 # right char.
284 vim.window.setTimeout((-> marker.reset() for marker in markers), 100)
285 return true
286
287 vim.enterMode('hints', callback)
288
289 # Follow links with hint markers.
290 command_follow = helper_follow.bind(undefined, {inTab: false})
291
292 # Follow links in a new Tab with hint markers.
293 command_follow_in_tab = helper_follow.bind(undefined, {inTab: true})
294
295 # Follow multiple links with hint markers.
296 command_follow_multiple = helper_follow.bind(undefined,
297 {inTab: true, multiple: true})
298
299 helper_follow_pattern = do ->
300 # Search for the prev/next patterns in the following attributes of the
301 # element. `rel` should be kept as the first attribute, since the standard
302 # way of marking up prev/next links (`rel="prev"` and `rel="next"`) should be
303 # favored. Even though some of these attributes only allow a fixed set of
304 # keywords, we pattern-match them anyways since lots of sites don’t follow
305 # the spec and use the attributes arbitrarily.
306 attrs = ['rel', 'role', 'data-tooltip', 'aria-label']
307
308 return (type, vim) ->
309 links = utils.getMarkableElements(vim.window.document, {type: 'action'})
310 .filter(utils.isElementVisible)
311
312 patterns = utils.splitListString(getComplexPref("#{ type }_patterns"))
313
314 if matchingLink = utils.getBestPatternMatch(patterns, attrs, links)
315 utils.simulateClick(matchingLink, {metaKey: false, ctrlKey: false})
316
317 # Follow previous page.
318 command_follow_prev = helper_follow_pattern.bind(undefined, 'prev')
319
320 # Follow next page.
321 command_follow_next = helper_follow_pattern.bind(undefined, 'next')
322
323 # Go up one level in the URL hierarchy.
324 command_go_up_path = (vim, event, count) ->
325 { pathname } = vim.window.location
326 vim.window.location.pathname = pathname.replace(
327 /// (?: /[^/]+ ){1,#{ count }} /?$ ///, ''
328 )
329
330 # Go up to root of the URL hierarchy.
331 command_go_to_root = (vim) ->
332 vim.window.location.href = vim.window.location.origin
333
334 helper_go_history = (num, vim, event, count) ->
335 { index } = vim.rootWindow.getWebNavigation().sessionHistory
336 { history } = vim.window
337 num *= count
338 num = Math.max(num, -index)
339 num = Math.min(num, history.length - 1 - index)
340 return if num == 0
341 history.go(num)
342
343 # Go back in history.
344 command_back = helper_go_history.bind(undefined, -1)
345
346 # Go forward in history.
347 command_forward = helper_go_history.bind(undefined, +1)
348
349 findStorage = {lastSearchString: ''}
350
351 helper_find = (highlight, vim) ->
352 findBar = vim.rootWindow.gBrowser.getFindBar()
353
354 findBar.onFindCommand()
355 findBar._findField.focus()
356 findBar._findField.select()
357
358 return unless highlightButton = findBar.getElement('highlight')
359 if highlightButton.checked != highlight
360 highlightButton.click()
361
362 # Open the find bar, making sure that hightlighting is off.
363 command_find = helper_find.bind(undefined, false)
364
365 # Open the find bar, making sure that hightlighting is on.
366 command_find_hl = helper_find.bind(undefined, true)
367
368 helper_find_again = (direction, vim) ->
369 findBar = vim.rootWindow.gBrowser.getFindBar()
370 if findStorage.lastSearchString.length > 0
371 findBar._findField.value = findStorage.lastSearchString
372 findBar.onFindAgainCommand(direction)
373
374 # Search for the last pattern.
375 command_find_next = helper_find_again.bind(undefined, false)
376
377 # Search for the last pattern backwards.
378 command_find_prev = helper_find_again.bind(undefined, true)
379
380 # Enter insert mode.
381 command_insert_mode = (vim) ->
382 vim.enterMode('insert')
383
384 # Display the Help Dialog.
385 command_help = (vim) ->
386 help.injectHelp(vim.window.document, commands)
387
388 # Open and select the Developer Toolbar.
389 command_dev = (vim) ->
390 vim.rootWindow.DeveloperToolbar.show(true) # focus
391
392 command_Esc = (vim, event) ->
393 utils.blurActiveElement(vim.window)
394
395 # Blur active XUL control.
396 callback = -> event.originalTarget?.ownerDocument?.activeElement?.blur()
397 vim.window.setTimeout(callback, 0)
398
399 help.removeHelp(vim.window.document)
400
401 vim.rootWindow.DeveloperToolbar.hide()
402
403 vim.rootWindow.gBrowser.getFindBar().close()
404
405 vim.rootWindow.TabView.hide()
406
407
408 class Command
409 constructor: (@group, @name, @func, keys) ->
410 @defaultKeys = keys
411 if isPrefSet(@prefName('keys'))
412 try @keyValues = JSON.parse(getPref(@prefName('keys')))
413 else
414 @keyValues = keys
415 for key, index in @keyValues when typeof key == 'string'
416 @keyValues[index] = legacy.convertKey(key)
417
418 # Name of the preference for a given property.
419 prefName: (value) -> "commands.#{ @name }.#{ value }"
420
421 keys: (value) ->
422 if value == undefined
423 return @keyValues
424 else
425 @keyValues = value or @defaultKeyValues
426 setPref(@prefName('keys'), value and JSON.stringify(value))
427
428 help: -> _("help_command_#{ @name }")
429
430 # coffeelint: disable=max_line_length
431 commands = [
432 new Command('urls', 'focus', command_focus, [['o']])
433 new Command('urls', 'focus_search', command_focus_search, [['O']])
434 new Command('urls', 'paste', command_paste, [['p']])
435 new Command('urls', 'paste_tab', command_paste_tab, [['P']])
436 new Command('urls', 'marker_yank', command_marker_yank, [['y', 'f']])
437 new Command('urls', 'marker_focus', command_marker_focus, [['v', 'f']])
438 new Command('urls', 'yank', command_yank, [['y', 'y']])
439 new Command('urls', 'reload', command_reload, [['r']])
440 new Command('urls', 'reload_force', command_reload_force, [['R']])
441 new Command('urls', 'reload_all', command_reload_all, [['a', 'r']])
442 new Command('urls', 'reload_all_force', command_reload_all_force, [['a', 'R']])
443 new Command('urls', 'stop', command_stop, [['s']])
444 new Command('urls', 'stop_all', command_stop_all, [['a', 's']])
445
446 new Command('nav', 'scroll_to_top', command_scroll_to_top , [['g', 'g']])
447 new Command('nav', 'scroll_to_bottom', command_scroll_to_bottom, [['G']])
448 new Command('nav', 'scroll_down', command_scroll_down, [['j']])
449 new Command('nav', 'scroll_up', command_scroll_up, [['k']])
450 new Command('nav', 'scroll_left', command_scroll_left, [['h']])
451 new Command('nav', 'scroll_right', command_scroll_right , [['l']])
452 new Command('nav', 'scroll_half_page_down', command_scroll_half_page_down, [['d']])
453 new Command('nav', 'scroll_half_page_up', command_scroll_half_page_up, [['u']])
454 new Command('nav', 'scroll_page_down', command_scroll_page_down, [['<Space>']])
455 new Command('nav', 'scroll_page_up', command_scroll_page_up, [['<s-Space>']])
456
457 new Command('tabs', 'open_tab', command_open_tab, [['t']])
458 new Command('tabs', 'tab_prev', command_tab_prev, [['J'], ['g', 'T']])
459 new Command('tabs', 'tab_next', command_tab_next, [['K'], ['g', 't']])
460 new Command('tabs', 'tab_move_left', command_tab_move_left, [['g', 'J']])
461 new Command('tabs', 'tab_move_right', command_tab_move_right, [['g', 'K']])
462 new Command('tabs', 'home', command_home, [['g', 'h']])
463 new Command('tabs', 'tab_first', command_tab_first, [['g', 'H'], ['g', '0']])
464 new Command('tabs', 'tab_first_non_pinned', command_tab_first_non_pinned, [['g', '^']])
465 new Command('tabs', 'tab_last', command_tab_last, [['g', 'L'], ['g', '$']])
466 new Command('tabs', 'toggle_pin_tab', command_toggle_pin_tab, [['g', 'p']])
467 new Command('tabs', 'duplicate_tab', command_duplicate_tab, [['y', 't']])
468 new Command('tabs', 'close_tabs_to_end', command_close_tabs_to_end, [['g', 'x', '$']])
469 new Command('tabs', 'close_other_tabs', command_close_other_tabs, [['g', 'x', 'a']])
470 new Command('tabs', 'close_tab', command_close_tab, [['x']])
471 new Command('tabs', 'restore_tab', command_restore_tab, [['X']])
472
473 new Command('browse', 'follow', command_follow, [['f']])
474 new Command('browse', 'follow_in_tab', command_follow_in_tab, [['F']])
475 new Command('browse', 'follow_multiple', command_follow_multiple, [['a', 'f']])
476 new Command('browse', 'follow_previous', command_follow_prev, [['[']])
477 new Command('browse', 'follow_next', command_follow_next, [[']']])
478 new Command('browse', 'go_up_path', command_go_up_path, [['g', 'u']])
479 new Command('browse', 'go_to_root', command_go_to_root, [['g', 'U']])
480 new Command('browse', 'back', command_back, [['H']])
481 new Command('browse', 'forward', command_forward, [['L']])
482
483 new Command('misc', 'find', command_find, [['/']])
484 new Command('misc', 'find_hl', command_find_hl, [['a', '/']])
485 new Command('misc', 'find_next', command_find_next, [['n']])
486 new Command('misc', 'find_prev', command_find_prev, [['N']])
487 new Command('misc', 'insert_mode', command_insert_mode, [['i']])
488 new Command('misc', 'help', command_help, [['?']])
489 new Command('misc', 'dev', command_dev, [[':']])
490
491 escapeCommand =
492 new Command('misc', 'Esc', command_Esc, [['<Esc>']])
493 ]
494 # coffeelint: enable=max_line_length
495
496 searchForMatchingCommand = (keys) ->
497 for index in [0...keys.length] by 1
498 str = keys[index..].join('')
499 for command in commands
500 for key in command.keys()
501 key = utils.normalizedKey(key)
502 if key.startsWith(str)
503 numbers = keys[0..index].join('').match(/[1-9]\d*/g)
504
505 # When letter 0 follows after a number, it is considered as number 0
506 # instead of a valid command.
507 continue if key == '0' and numbers
508
509 count = parseInt(numbers[numbers.length - 1], 10) if numbers
510 count = if count > 1 then count else 1
511
512 return {match: true, exact: (key == str), command, count}
513
514 return {match: false}
515
516 isEscCommandKey = (keyStr) ->
517 for key in escapeCommand.keys()
518 if keyStr == utils.normalizedKey(key)
519 return true
520 return false
521
522 exports.commands = commands
523 exports.searchForMatchingCommand = searchForMatchingCommand
524 exports.isEscCommandKey = isEscCommandKey
525 exports.findStorage = findStorage
Imprint / Impressum