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