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