]> git.gir.st - VimFx.git/blob - extension/packages/commands.coffee
fix typos and console.log
[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) ->
104 step = getPref('scroll_step_lines')
105 utils.simulateWheel(vim.window, 0, +step, utils.WHEEL_MODE_LINE)
106
107 # Scroll up a bit.
108 command_scroll_up = (vim) ->
109 step = getPref('scroll_step_lines')
110 utils.simulateWheel(vim.window, 0, -step, utils.WHEEL_MODE_LINE)
111
112 # Scroll left a bit.
113 command_scroll_left = (vim) ->
114 step = getPref('scroll_step_lines')
115 utils.simulateWheel(vim.window, -step, 0, utils.WHEEL_MODE_LINE)
116
117 # Scroll right a bit.
118 command_scroll_right = (vim) ->
119 step = getPref('scroll_step_lines')
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) ->
124 utils.simulateWheel(vim.window, 0, +0.5, utils.WHEEL_MODE_PAGE)
125
126 # Scroll up half a page.
127 command_scroll_half_page_up = (vim) ->
128 utils.simulateWheel(vim.window, 0, -0.5, utils.WHEEL_MODE_PAGE)
129
130 # Scroll down full a page.
131 command_scroll_page_down = (vim) ->
132 utils.simulateWheel(vim.window, 0, +1, utils.WHEEL_MODE_PAGE)
133
134 # Scroll up full a page.
135 command_scroll_page_up = (vim) ->
136 utils.simulateWheel(vim.window, 0, -1, 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 # Switch to the previous tab.
143 command_tab_prev = (vim) ->
144 wrap = true
145 vim.rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true) # wrap
146
147 # Switch to the next tab.
148 command_tab_next = (vim) ->
149 wrap = true
150 vim.rootWindow.gBrowser.tabContainer.advanceSelectedTab(+1, true) # wrap
151
152 # Move the current tab backward.
153 command_tab_move_left = (vim) ->
154 { gBrowser } = vim.rootWindow
155 lastIndex = gBrowser.tabContainer.selectedIndex
156 gBrowser.moveTabBackward()
157 if gBrowser.tabContainer.selectedIndex == lastIndex
158 gBrowser.moveTabToEnd()
159
160 # Move the current tab forward.
161 command_tab_move_right = (vim) ->
162 { gBrowser } = vim.rootWindow
163 lastIndex = gBrowser.tabContainer.selectedIndex
164 gBrowser.moveTabForward()
165 if gBrowser.tabContainer.selectedIndex == lastIndex
166 gBrowser.moveTabToStart()
167
168 # Load the home page.
169 command_home = (vim) ->
170 vim.rootWindow.BrowserHome()
171
172 # Switch to the first tab.
173 command_tab_first = (vim) ->
174 vim.rootWindow.gBrowser.selectTabAtIndex(0)
175
176 # Switch to the last tab.
177 command_tab_last = (vim) ->
178 vim.rootWindow.gBrowser.selectTabAtIndex(-1)
179
180 # Toggle Pin Tab.
181 command_toggle_pin_tab = (vim) ->
182 currentTab = vim.rootWindow.gBrowser.selectedTab
183
184 if currentTab.pinned
185 vim.rootWindow.gBrowser.unpinTab(currentTab)
186 else
187 vim.rootWindow.gBrowser.pinTab(currentTab)
188
189 # Duplicate current tab.
190 command_duplicate_tab = (vim) ->
191 { gBrowser } = vim.rootWindow
192 gBrowser.duplicateTab(gBrowser.selectedTab)
193
194 # Close all tabs from current to the end.
195 command_close_tabs_to_end = (vim) ->
196 { gBrowser } = vim.rootWindow
197 gBrowser.removeTabsToTheEndFrom(gBrowser.selectedTab)
198
199 # Close all tabs except the current.
200 command_close_other_tabs = (vim) ->
201 { gBrowser } = vim.rootWindow
202 gBrowser.removeAllTabsBut(gBrowser.selectedTab)
203
204 # Close current tab.
205 command_close_tab = (vim) ->
206 unless vim.rootWindow.gBrowser.selectedTab.pinned
207 vim.rootWindow.gBrowser.removeCurrentTab()
208
209 # Restore last closed tab.
210 command_restore_tab = (vim) ->
211 vim.rootWindow.undoCloseTab()
212
213 helper_follow = ({ inTab, multiple }, vim) ->
214 callback = (matchedMarker, markers) ->
215 matchedMarker.element.focus()
216 utils.simulateClick(matchedMarker.element, {metaKey: inTab, ctrlKey: inTab})
217 isEditable = utils.isElementEditable(matchedMarker.element)
218 if multiple and not isEditable
219 # By not resetting immediately one is able to see the last char being
220 # matched, which gives some nice visual feedback that you've typed the
221 # right char.
222 vim.window.setTimeout((-> marker.reset() for marker in markers), 100)
223 return true
224
225 vim.enterMode('hints', callback)
226
227 # Follow links with hint markers.
228 command_follow = helper_follow.bind(undefined, {inTab: false})
229
230 # Follow links in a new Tab with hint markers.
231 command_follow_in_tab = helper_follow.bind(undefined, {inTab: true})
232
233 # Follow multiple links with hint markers.
234 command_follow_multiple = helper_follow.bind(undefined, {inTab: true, multiple: true})
235
236 helper_follow_pattern = do ->
237 # Search for the prev/next patterns in the following attributes of the
238 # element. `rel` should be kept as the first attribute, since the standard
239 # way of marking up prev/next links (`rel="prev"` and `rel="next"`) should be
240 # favored. Even though some of these attributes only allow a fixed set of
241 # keywords, we pattern-match them anyways since lots of sites don’t follow
242 # the spec and use the attributes arbitrarily.
243 attrs = ['rel', 'role', 'data-tooltip', 'aria-label']
244
245 return (type, vim) ->
246 links = utils.getMarkableElements(vim.window.document, {type: 'action'})
247 .filter(utils.isElementVisible)
248
249 patterns = utils.splitListString(getComplexPref("#{ type }_patterns"))
250
251 if matchingLink = utils.getBestPatternMatch(patterns, attrs, links)
252 utils.simulateClick(matchingLink, {metaKey: false, ctrlKey: false})
253
254 # Follow previous page.
255 command_follow_prev = helper_follow_pattern.bind(undefined, 'prev')
256
257 # Follow next page.
258 command_follow_next = helper_follow_pattern.bind(undefined, 'next')
259
260 # Go up one level in the URL hierarchy.
261 command_go_up_path = (vim) ->
262 path = vim.window.location.pathname
263 vim.window.location.pathname = path.replace(/// / [^/]+ /?$ ///, '')
264
265 # Go up to root of the URL hierarchy.
266 command_go_to_root = (vim) ->
267 vim.window.location.href = vim.window.location.origin
268
269 # Go back in history.
270 command_back = (vim) ->
271 vim.rootWindow.BrowserBack()
272
273 # Go forward in history.
274 command_forward = (vim) ->
275 vim.rootWindow.BrowserForward()
276
277 findStorage = {lastSearchString: ''}
278
279 helper_find = (highlight, vim) ->
280 findBar = vim.rootWindow.gBrowser.getFindBar()
281
282 findBar.onFindCommand()
283 findBar._findField.focus()
284 findBar._findField.select()
285
286 return unless highlightButton = findBar.getElement('highlight')
287 if highlightButton.checked != highlight
288 highlightButton.click()
289
290 # Open the find bar, making sure that hightlighting is off.
291 command_find = helper_find.bind(undefined, false)
292
293 # Open the find bar, making sure that hightlighting is on.
294 command_find_hl = helper_find.bind(undefined, true)
295
296 helper_find_again = (direction, vim) ->
297 findBar = vim.rootWindow.gBrowser.getFindBar()
298 if findStorage.lastSearchString.length > 0
299 findBar._findField.value = findStorage.lastSearchString
300 findBar.onFindAgainCommand(direction)
301
302 # Search for the last pattern.
303 command_find_next = helper_find_again.bind(undefined, false)
304
305 # Search for the last pattern backwards.
306 command_find_prev = helper_find_again.bind(undefined, true)
307
308 # Enter insert mode.
309 command_insert_mode = (vim) ->
310 vim.enterMode('insert')
311
312 # Display the Help Dialog.
313 command_help = (vim) ->
314 help.injectHelp(vim.window.document, commands)
315
316 # Open and select the Developer Toolbar.
317 command_dev = (vim) ->
318 vim.rootWindow.DeveloperToolbar.show(true) # focus
319
320 command_Esc = (vim, event) ->
321 utils.blurActiveElement(vim.window)
322
323 # Blur active XUL control.
324 callback = -> event.originalTarget?.ownerDocument?.activeElement?.blur()
325 vim.window.setTimeout(callback, 0)
326
327 help.removeHelp(vim.window.document)
328
329 vim.rootWindow.DeveloperToolbar.hide()
330
331 vim.rootWindow.gBrowser.getFindBar().close()
332
333 vim.rootWindow.TabView.hide()
334
335
336 class Command
337 constructor: (@group, @name, @func, keys) ->
338 @defaultKeys = keys
339 if isPrefSet(@prefName('keys'))
340 try @keyValues = JSON.parse(getPref(@prefName('keys')))
341 else
342 @keyValues = keys
343
344 # Name of the preference for a given property.
345 prefName: (value) -> "commands.#{ @name }.#{ value }"
346
347 keys: (value) ->
348 if value is undefined
349 return @keyValues
350 else
351 @keyValues = value or @defaultKeyValues
352 setPref(@prefName('keys'), value and JSON.stringify(value))
353
354 help: -> _("help_command_#{ @name }")
355
356 commands = [
357 new Command('urls', 'focus', command_focus, ['o'])
358 new Command('urls', 'focus_search', command_focus_search, ['O'])
359 new Command('urls', 'paste', command_paste, ['p'])
360 new Command('urls', 'paste_tab', command_paste_tab, ['P'])
361 new Command('urls', 'marker_yank', command_marker_yank, ['y,f'])
362 new Command('urls', 'marker_focus', command_marker_focus, ['v,f'])
363 new Command('urls', 'yank', command_yank, ['y,y'])
364 new Command('urls', 'reload', command_reload, ['r'])
365 new Command('urls', 'reload_force', command_reload_force, ['R'])
366 new Command('urls', 'reload_all', command_reload_all, ['a,r'])
367 new Command('urls', 'reload_all_force', command_reload_all_force, ['a,R'])
368 new Command('urls', 'stop', command_stop, ['s'])
369 new Command('urls', 'stop_all', command_stop_all, ['a,s'])
370
371 new Command('nav', 'scroll_to_top', command_scroll_to_top , ['g,g'])
372 new Command('nav', 'scroll_to_bottom', command_scroll_to_bottom, ['G'])
373 new Command('nav', 'scroll_down', command_scroll_down, ['j', 'c-e'])
374 new Command('nav', 'scroll_up', command_scroll_up, ['k', 'c-y'])
375 new Command('nav', 'scroll_left', command_scroll_left, ['h'])
376 new Command('nav', 'scroll_right', command_scroll_right , ['l'])
377 new Command('nav', 'scroll_half_page_down', command_scroll_half_page_down, ['d'])
378 new Command('nav', 'scroll_half_page_up', command_scroll_half_page_up, ['u'])
379 new Command('nav', 'scroll_page_down', command_scroll_page_down, ['c-f'])
380 new Command('nav', 'scroll_page_up', command_scroll_page_up, ['c-b'])
381
382 new Command('tabs', 'open_tab', command_open_tab, ['t'])
383 new Command('tabs', 'tab_prev', command_tab_prev, ['J', 'g,T'])
384 new Command('tabs', 'tab_next', command_tab_next, ['K', 'g,t'])
385 new Command('tabs', 'tab_move_left', command_tab_move_left, ['c-J'])
386 new Command('tabs', 'tab_move_right', command_tab_move_right, ['c-K'])
387 new Command('tabs', 'home', command_home, ['g,h'])
388 new Command('tabs', 'tab_first', command_tab_first, ['g,H', 'g,^'])
389 new Command('tabs', 'tab_last', command_tab_last, ['g,L', 'g,$'])
390 new Command('tabs', 'toggle_pin_tab', command_toggle_pin_tab, [])
391 new Command('tabs', 'duplicate_tab', command_duplicate_tab, [])
392 new Command('tabs', 'close_tabs_to_end', command_close_tabs_to_end, [])
393 new Command('tabs', 'close_other_tabs', command_close_other_tabs, [])
394 new Command('tabs', 'close_tab', command_close_tab, ['x'])
395 new Command('tabs', 'restore_tab', command_restore_tab, ['X'])
396
397 new Command('browse', 'follow', command_follow, ['f'])
398 new Command('browse', 'follow_in_tab', command_follow_in_tab, ['F'])
399 new Command('browse', 'follow_multiple', command_follow_multiple, ['a,f'])
400 new Command('browse', 'follow_previous', command_follow_prev, ['['])
401 new Command('browse', 'follow_next', command_follow_next, [']'])
402 new Command('browse', 'go_up_path', command_go_up_path, ['g,u'])
403 new Command('browse', 'go_to_root', command_go_to_root, ['g,U'])
404 new Command('browse', 'back', command_back, ['H'])
405 new Command('browse', 'forward', command_forward, ['L'])
406
407 new Command('misc', 'find', command_find, ['/'])
408 new Command('misc', 'find_hl', command_find_hl, ['a,/'])
409 new Command('misc', 'find_next', command_find_next, ['n'])
410 new Command('misc', 'find_prev', command_find_prev, ['N'])
411 new Command('misc', 'insert_mode', command_insert_mode, ['i'])
412 new Command('misc', 'help', command_help, ['?'])
413 new Command('misc', 'dev', command_dev, [':'])
414
415 escapeCommand =
416 new Command('misc', 'Esc', command_Esc, ['Esc'])
417 ]
418
419 searchForMatchingCommand = (keys) ->
420 for index in [0...keys.length] by 1
421 str = keys[index..].join(',')
422 for command in commands
423 for key in command.keys()
424 # The following hack is a workaround for the issue where letter `c` is
425 # considered a start of command with control modifier `c-xxx`.
426 if "#{ key },".startsWith("#{ str },")
427 return {match: true, exact: (key == str), command}
428
429 return {match: false}
430
431 isEscCommandKey = (keyStr) -> keyStr in escapeCommand.keys()
432
433 exports.commands = commands
434 exports.searchForMatchingCommand = searchForMatchingCommand
435 exports.isEscCommandKey = isEscCommandKey
436 exports.findStorage = findStorage
Imprint / Impressum