]> git.gir.st - VimFx.git/blob - extension/lib/commands.coffee
Configure coffeelint and fix lint errors
[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 = utils.readFromClipboard(vim.window)
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 # Close current tab.
228 command_close_tab = (vim, event, count) ->
229 { gBrowser } = vim.rootWindow
230 return if gBrowser.selectedTab.pinned
231 currentIndex = gBrowser.visibleTabs.indexOf(gBrowser.selectedTab)
232 for tab in gBrowser.visibleTabs[currentIndex...(currentIndex + count)]
233 gBrowser.removeTab(tab)
234
235 # Restore last closed tab.
236 command_restore_tab = (vim, event, count) ->
237 vim.rootWindow.undoCloseTab() for [1..count]
238
239 helper_follow = ({ inTab, multiple }, vim, event, count) ->
240 callback = (matchedMarker, markers) ->
241 if matchedMarker.element.target == '_blank'
242 targetReset = matchedMarker.element.target
243 matchedMarker.element.target = ''
244
245 matchedMarker.element.focus()
246
247 inTab = if count > 1 then true else inTab
248 utils.simulateClick(matchedMarker.element, {metaKey: inTab, ctrlKey: inTab})
249
250 matchedMarker.element.target = targetReset if targetReset
251
252 count -= 1
253 isEditable = utils.isElementEditable(matchedMarker.element)
254 if (multiple or count > 0) and not isEditable
255 # By not resetting immediately one is able to see the last char being
256 # matched, which gives some nice visual feedback that you've typed the
257 # right char.
258 vim.window.setTimeout((-> marker.reset() for marker in markers), 100)
259 return true
260
261 vim.enterMode('hints', callback)
262
263 # Follow links with hint markers.
264 command_follow = helper_follow.bind(undefined, {inTab: false})
265
266 # Follow links in a new Tab with hint markers.
267 command_follow_in_tab = helper_follow.bind(undefined, {inTab: true})
268
269 # Follow multiple links with hint markers.
270 command_follow_multiple = helper_follow.bind(undefined,
271 {inTab: true, multiple: true})
272
273 helper_follow_pattern = do ->
274 # Search for the prev/next patterns in the following attributes of the
275 # element. `rel` should be kept as the first attribute, since the standard
276 # way of marking up prev/next links (`rel="prev"` and `rel="next"`) should be
277 # favored. Even though some of these attributes only allow a fixed set of
278 # keywords, we pattern-match them anyways since lots of sites don’t follow
279 # the spec and use the attributes arbitrarily.
280 attrs = ['rel', 'role', 'data-tooltip', 'aria-label']
281
282 return (type, vim) ->
283 links = utils.getMarkableElements(vim.window.document, {type: 'action'})
284 .filter(utils.isElementVisible)
285
286 patterns = utils.splitListString(getComplexPref("#{ type }_patterns"))
287
288 if matchingLink = utils.getBestPatternMatch(patterns, attrs, links)
289 utils.simulateClick(matchingLink, {metaKey: false, ctrlKey: false})
290
291 # Follow previous page.
292 command_follow_prev = helper_follow_pattern.bind(undefined, 'prev')
293
294 # Follow next page.
295 command_follow_next = helper_follow_pattern.bind(undefined, 'next')
296
297 # Go up one level in the URL hierarchy.
298 command_go_up_path = (vim, event, count) ->
299 { pathname } = vim.window.location
300 vim.window.location.pathname = pathname.replace(
301 /// (?: /[^/]+ ){1,#{ count }} /?$ ///, ''
302 )
303
304 # Go up to root of the URL hierarchy.
305 command_go_to_root = (vim) ->
306 vim.window.location.href = vim.window.location.origin
307
308 helper_go_history = (num, vim, event, count) ->
309 { index } = vim.rootWindow.getWebNavigation().sessionHistory
310 { history } = vim.window
311 num *= count
312 num = Math.max(num, -index)
313 num = Math.min(num, history.length - 1 - index)
314 return if num == 0
315 history.go(num)
316
317 # Go back in history.
318 command_back = helper_go_history.bind(undefined, -1)
319
320 # Go forward in history.
321 command_forward = helper_go_history.bind(undefined, +1)
322
323 findStorage = {lastSearchString: ''}
324
325 helper_find = (highlight, vim) ->
326 findBar = vim.rootWindow.gBrowser.getFindBar()
327
328 findBar.onFindCommand()
329 findBar._findField.focus()
330 findBar._findField.select()
331
332 return unless highlightButton = findBar.getElement('highlight')
333 if highlightButton.checked != highlight
334 highlightButton.click()
335
336 # Open the find bar, making sure that hightlighting is off.
337 command_find = helper_find.bind(undefined, false)
338
339 # Open the find bar, making sure that hightlighting is on.
340 command_find_hl = helper_find.bind(undefined, true)
341
342 helper_find_again = (direction, vim) ->
343 findBar = vim.rootWindow.gBrowser.getFindBar()
344 if findStorage.lastSearchString.length > 0
345 findBar._findField.value = findStorage.lastSearchString
346 findBar.onFindAgainCommand(direction)
347
348 # Search for the last pattern.
349 command_find_next = helper_find_again.bind(undefined, false)
350
351 # Search for the last pattern backwards.
352 command_find_prev = helper_find_again.bind(undefined, true)
353
354 # Enter insert mode.
355 command_insert_mode = (vim) ->
356 vim.enterMode('insert')
357
358 # Display the Help Dialog.
359 command_help = (vim) ->
360 help.injectHelp(vim.window.document, commands)
361
362 # Open and select the Developer Toolbar.
363 command_dev = (vim) ->
364 vim.rootWindow.DeveloperToolbar.show(true) # focus
365
366 command_Esc = (vim, event) ->
367 utils.blurActiveElement(vim.window)
368
369 # Blur active XUL control.
370 callback = -> event.originalTarget?.ownerDocument?.activeElement?.blur()
371 vim.window.setTimeout(callback, 0)
372
373 help.removeHelp(vim.window.document)
374
375 vim.rootWindow.DeveloperToolbar.hide()
376
377 vim.rootWindow.gBrowser.getFindBar().close()
378
379 vim.rootWindow.TabView.hide()
380
381
382 class Command
383 constructor: (@group, @name, @func, keys) ->
384 @defaultKeys = keys
385 if isPrefSet(@prefName('keys'))
386 try @keyValues = JSON.parse(getPref(@prefName('keys')))
387 else
388 @keyValues = keys
389
390 # Name of the preference for a given property.
391 prefName: (value) -> "commands.#{ @name }.#{ value }"
392
393 keys: (value) ->
394 if value == undefined
395 return @keyValues
396 else
397 @keyValues = value or @defaultKeyValues
398 setPref(@prefName('keys'), value and JSON.stringify(value))
399
400 help: -> _("help_command_#{ @name }")
401
402 # coffeelint: disable=max_line_length
403 commands = [
404 new Command('urls', 'focus', command_focus, ['o'])
405 new Command('urls', 'focus_search', command_focus_search, ['O'])
406 new Command('urls', 'paste', command_paste, ['p'])
407 new Command('urls', 'paste_tab', command_paste_tab, ['P'])
408 new Command('urls', 'marker_yank', command_marker_yank, ['y,f'])
409 new Command('urls', 'marker_focus', command_marker_focus, ['v,f'])
410 new Command('urls', 'yank', command_yank, ['y,y'])
411 new Command('urls', 'reload', command_reload, ['r'])
412 new Command('urls', 'reload_force', command_reload_force, ['R'])
413 new Command('urls', 'reload_all', command_reload_all, ['a,r'])
414 new Command('urls', 'reload_all_force', command_reload_all_force, ['a,R'])
415 new Command('urls', 'stop', command_stop, ['s'])
416 new Command('urls', 'stop_all', command_stop_all, ['a,s'])
417
418 new Command('nav', 'scroll_to_top', command_scroll_to_top , ['g,g'])
419 new Command('nav', 'scroll_to_bottom', command_scroll_to_bottom, ['G'])
420 new Command('nav', 'scroll_down', command_scroll_down, ['j'])
421 new Command('nav', 'scroll_up', command_scroll_up, ['k'])
422 new Command('nav', 'scroll_left', command_scroll_left, ['h'])
423 new Command('nav', 'scroll_right', command_scroll_right , ['l'])
424 new Command('nav', 'scroll_half_page_down', command_scroll_half_page_down, ['d'])
425 new Command('nav', 'scroll_half_page_up', command_scroll_half_page_up, ['u'])
426 new Command('nav', 'scroll_page_down', command_scroll_page_down, ['Space'])
427 new Command('nav', 'scroll_page_up', command_scroll_page_up, ['Shift-Space'])
428
429 new Command('tabs', 'open_tab', command_open_tab, ['t'])
430 new Command('tabs', 'tab_prev', command_tab_prev, ['J', 'g,T'])
431 new Command('tabs', 'tab_next', command_tab_next, ['K', 'g,t'])
432 new Command('tabs', 'tab_move_left', command_tab_move_left, ['g,J'])
433 new Command('tabs', 'tab_move_right', command_tab_move_right, ['g,K'])
434 new Command('tabs', 'home', command_home, ['g,h'])
435 new Command('tabs', 'tab_first', command_tab_first, ['g,H', 'g,0'])
436 new Command('tabs', 'tab_first_non_pinned', command_tab_first_non_pinned, ['g,^'])
437 new Command('tabs', 'tab_last', command_tab_last, ['g,L', 'g,$'])
438 new Command('tabs', 'close_tab', command_close_tab, ['x'])
439 new Command('tabs', 'restore_tab', command_restore_tab, ['X'])
440
441 new Command('browse', 'follow', command_follow, ['f'])
442 new Command('browse', 'follow_in_tab', command_follow_in_tab, ['F'])
443 new Command('browse', 'follow_multiple', command_follow_multiple, ['a,f'])
444 new Command('browse', 'follow_previous', command_follow_prev, ['['])
445 new Command('browse', 'follow_next', command_follow_next, [']'])
446 new Command('browse', 'go_up_path', command_go_up_path, ['g,u'])
447 new Command('browse', 'go_to_root', command_go_to_root, ['g,U'])
448 new Command('browse', 'back', command_back, ['H'])
449 new Command('browse', 'forward', command_forward, ['L'])
450
451 new Command('misc', 'find', command_find, ['/'])
452 new Command('misc', 'find_hl', command_find_hl, ['a,/'])
453 new Command('misc', 'find_next', command_find_next, ['n'])
454 new Command('misc', 'find_prev', command_find_prev, ['N'])
455 new Command('misc', 'insert_mode', command_insert_mode, ['i'])
456 new Command('misc', 'help', command_help, ['?'])
457 new Command('misc', 'dev', command_dev, [':'])
458
459 escapeCommand =
460 new Command('misc', 'Esc', command_Esc, ['Esc'])
461 ]
462 # coffeelint: enable=max_line_length
463
464 searchForMatchingCommand = (keys) ->
465 for index in [0...keys.length] by 1
466 str = keys[index..].join(',')
467 for command in commands
468 for key in command.keys()
469 # The following hack is a workaround for the issue where letter `c` is
470 # considered a start of command with control modifier `c-xxx`.
471 if "#{ key },".startsWith("#{ str },")
472 numbers = keys[0..index].join('').match(/[1-9]\d*/g)
473
474 # When letter 0 follows after a number, it is considered as number 0
475 # instead of a valid command.
476 continue if key == '0' and numbers
477
478 count = parseInt(numbers[numbers.length - 1], 10) if numbers
479 count = if count > 1 then count else 1
480
481 return {match: true, exact: (key == str), command, count}
482
483 return {match: false}
484
485 isEscCommandKey = (keyStr) -> keyStr in escapeCommand.keys()
486
487 exports.commands = commands
488 exports.searchForMatchingCommand = searchForMatchingCommand
489 exports.isEscCommandKey = isEscCommandKey
490 exports.findStorage = findStorage
Imprint / Impressum