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