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