]> git.gir.st - VimFx.git/blob - extension/packages/commands.coffee
Workaround for command shortcuts that start with letter c
[VimFx.git] / extension / packages / commands.coffee
1 utils = require 'utils'
2 help = require 'help'
3 find_link = require 'find-link'
4 { _ } = require 'l10n'
5 { getPref
6 , getComplexPref
7 , setPref
8 , isPrefSet
9 , getFirefoxPref } = require 'prefs'
10
11 { classes: Cc, interfaces: Ci, utils: Cu } = Components
12
13 # Open developer toolbar (Default shotrcut: Shift-F2)
14 command_dev = (vim) ->
15 if chromeWindow = utils.getRootWindow vim.window
16 chromeWindow.DeveloperToolbar.show(true)
17 chromeWindow.DeveloperToolbar.focus()
18
19 # Focus the Address Bar
20 command_focus = (vim) ->
21 if chromeWindow = utils.getRootWindow(vim.window)
22 chromeWindow.focusAndSelectUrlBar()
23
24 # Focus the Search Bar
25 command_focus_search = (vim) ->
26 if chromeWindow = utils.getRootWindow(vim.window)
27 if searchBar = chromeWindow.document.getElementById("searchbar")
28 searchBar.select()
29
30 # Navigate to the address that is currently stored in the system clipboard
31 command_paste = (vim) ->
32 url = utils.readFromClipboard(vim.window)
33 postData = null
34 if not utils.isURL(url) and submission = utils.browserSearchSubmission(url)
35 url = submission.uri.spec
36 { postData } = submission
37
38 if chromeWindow = utils.getRootWindow(vim.window)
39 chromeWindow.gBrowser.loadURIWithFlags(url, null, null, null, postData)
40
41 # Open new tab and navigate to the address that is currently stored in the system clipboard
42 command_paste_tab = (vim) ->
43 url = utils.readFromClipboard(vim.window)
44 postData = null
45 if not utils.isURL(url) and submission = utils.browserSearchSubmission(url)
46 url = submission.uri.spec
47 { postData } = submission
48
49 if chromeWindow = utils.getRootWindow vim.window
50 chromeWindow.gBrowser.selectedTab = chromeWindow.gBrowser.addTab(url, null, null, postData, null, false)
51
52 # Open new tab and focus the address bar
53 command_open_tab = (vim) ->
54 if chromeWindow = utils.getRootWindow(vim.window)
55 chromeWindow.BrowserOpenTab()
56
57 # Copy element URL to the clipboard
58 command_marker_yank = (vim) ->
59 callback = (marker) ->
60 if url = marker.element.href
61 marker.element.focus()
62 utils.writeToClipboard(vim.window, url)
63 else if utils.isTextInputElement(marker.element)
64 utils.writeToClipboard(vim.window, marker.element.value)
65
66 vim.enterMode('hints', callback)
67
68 # Focus element
69 command_marker_focus = (vim) ->
70 callback = (marker) -> marker.element.focus()
71
72 vim.enterMode('hints', callback)
73
74 # Copy current URL to the clipboard
75 command_yank = (vim) ->
76 utils.writeToClipboard(vim.window, vim.window.location.toString())
77
78 # Reload the page, possibly from cache
79 command_reload = (vim) ->
80 vim.window.location.reload(false)
81
82 # Reload the page from the server
83 command_reload_force = (vim) ->
84 vim.window.location.reload(true)
85
86 # Reload the page, possibly from cache
87 command_reload_all = (vim) ->
88 if rootWindow = utils.getRootWindow(vim.window)
89 if tabs = rootWindow.gBrowser.tabContainer
90 for i in [0...tabs.itemCount]
91 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
92 window.location.reload(false)
93
94 # Reload the page from the server
95 command_reload_all_force = (vim) ->
96 if rootWindow = utils.getRootWindow(vim.window)
97 if tabs = rootWindow.gBrowser.tabContainer
98 for i in [0...tabs.itemCount]
99 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
100 window.location.reload(true)
101
102 command_stop = (vim) ->
103 vim.window.stop()
104
105 command_stop_all = (vim) ->
106 if rootWindow = utils.getRootWindow(vim.window)
107 if tabs = rootWindow.gBrowser.tabContainer
108 for i in [0...tabs.itemCount]
109 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
110 window.stop()
111
112 # Scroll to the top of the page
113 command_scroll_to_top = (vim) ->
114 for i in [0...1000]
115 utils.simulateWheel(vim.window, 0, -1, utils.WHEEL_MODE_PAGE)
116
117 # Scroll to the bottom of the page
118 command_scroll_to_bottom = (vim) ->
119 for i in [0...1000]
120 utils.simulateWheel(vim.window, 0, 1, utils.WHEEL_MODE_PAGE)
121
122 # Scroll down a bit
123 command_scroll_down = (vim) ->
124 utils.simulateWheel(vim.window, 0, getPref('scroll_step_lines'), utils.WHEEL_MODE_LINE)
125
126 # Scroll up a bit
127 command_scroll_up = (vim) ->
128 utils.simulateWheel(vim.window, 0, -getPref('scroll_step_lines'), utils.WHEEL_MODE_LINE)
129
130 # Scroll left a bit
131 command_scroll_left = (vim) ->
132 utils.simulateWheel(vim.window, -getPref('scroll_step_lines'), 0, utils.WHEEL_MODE_LINE)
133
134 # Scroll right a bit
135 command_scroll_right = (vim) ->
136 utils.simulateWheel(vim.window, getPref('scroll_step_lines'), 0, utils.WHEEL_MODE_LINE)
137
138 # Scroll down half a page
139 command_scroll_half_page_down = (vim) ->
140 utils.simulateWheel(vim.window, 0, 0.5, utils.WHEEL_MODE_PAGE)
141
142 # Scroll up half a page
143 command_scroll_half_page_up = (vim) ->
144 utils.simulateWheel(vim.window, 0, -0.5, utils.WHEEL_MODE_PAGE)
145
146 # Scroll down full a page
147 command_scroll_page_down = (vim) ->
148 utils.simulateWheel(vim.window, 0, 1, utils.WHEEL_MODE_PAGE)
149
150 # Scroll up full a page
151 command_scroll_page_up = (vim) ->
152 utils.simulateWheel(vim.window, 0, -1, utils.WHEEL_MODE_PAGE)
153
154 # Activate previous tab
155 command_tab_prev = (vim) ->
156 if rootWindow = utils.getRootWindow(vim.window)
157 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true)
158
159 # Activate next tab
160 command_tab_next = (vim) ->
161 if rootWindow = utils.getRootWindow(vim.window)
162 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true)
163
164 command_home = (vim) ->
165 url = getFirefoxPref('browser.startup.homepage')
166 if chromeWindow = utils.getRootWindow(vim.window)
167 chromeWindow.gBrowser.loadURIWithFlags(url, null, null, null, null)
168
169 # Go to the first tab
170 command_tab_first = (vim) ->
171 if rootWindow = utils.getRootWindow(vim.window)
172 rootWindow.gBrowser.tabContainer.selectedIndex = 0
173
174 # Go to the last tab
175 command_tab_last = (vim) ->
176 if rootWindow = utils.getRootWindow(vim.window)
177 itemCount = rootWindow.gBrowser.tabContainer.itemCount
178 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1
179
180 # Go back in history
181 command_back = (vim) ->
182 vim.window.history.back()
183
184 # Go forward in history
185 command_forward = (vim) ->
186 vim.window.history.forward()
187
188 # Close current tab
189 command_close_tab = (vim) ->
190 if rootWindow = utils.getRootWindow(vim.window)
191 unless rootWindow.gBrowser.selectedTab.pinned
192 rootWindow.gBrowser.removeCurrentTab()
193
194 # Restore last closed tab
195 command_restore_tab = (vim) ->
196 if rootWindow = utils.getRootWindow(vim.window)
197 ss = utils.getSessionStore()
198 if ss and ss.getClosedTabCount(rootWindow) > 0
199 ss.undoCloseTab(rootWindow, 0)
200
201 helper_follow = ({ inTab, multiple }, vim) ->
202 callback = (matchedMarker, markers) ->
203 matchedMarker.element.focus()
204 utils.simulateClick(matchedMarker.element, {metaKey: inTab, ctrlKey: inTab})
205 isEditable = utils.isElementEditable(matchedMarker.element)
206 if multiple and not isEditable
207 # By not resetting immediately one is able to see the last char being matched, which gives
208 # some nice visual feedback that you've typed the right char.
209 vim.window.setTimeout((-> marker.reset() for marker in markers), 100)
210 return true
211
212 vim.enterMode('hints', callback)
213
214 # Follow links with hint markers
215 command_follow = helper_follow.bind(undefined, {inTab: false})
216
217 # Follow links in a new Tab with hint markers
218 command_follow_in_tab = helper_follow.bind(undefined, {inTab: true})
219
220 # Follow multiple links with hint markers
221 command_follow_multiple = helper_follow.bind(undefined, {inTab: true, multiple: true})
222
223 helper_follow_link = ({ type }, vim) ->
224 patterns = getComplexPref("#{ type }_patterns").split(',')
225 for link in find_link.find(vim.window.document, patterns)
226 # Only process links that have less than 15 characters of content.
227 # Otherwise it will match many wrong links whose content
228 # contains words link `more`, `next`, etc.
229 if link.textContent.length < 15
230 utils.simulateClick(link, {metaKey: false, ctrlKey: false})
231 return
232
233 # Follow previous page
234 command_follow_prev = helper_follow_link.bind(undefined, {type: 'prev'})
235
236 # Follow next page
237 command_follow_next = helper_follow_link.bind(undefined, {type: 'next'})
238
239 # Go up one level in the URL hierarchy
240 command_go_up_path = (vim) ->
241 path = vim.window.location.pathname
242 vim.window.location.pathname = path.replace(/\/[^\/]*?(\/)?$/, '')
243
244 # Go up to root of the URL hierarchy
245 command_go_to_root = (vim) ->
246 vim.window.location.href = vim.window.location.origin
247
248 # Move current tab to the left
249 command_tab_move_left = (vim) ->
250 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
251 if tab = gBrowser.selectedTab
252 index = gBrowser.tabContainer.selectedIndex
253 total = gBrowser.tabContainer.itemCount
254
255 # `total` is added to deal with negative offset
256 gBrowser.moveTabTo(tab, (total + index - 1) % total)
257
258 # Move current tab to the right
259 command_tab_move_right = (vim) ->
260 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
261 if tab = gBrowser.selectedTab
262 index = gBrowser.tabContainer.selectedIndex
263 total = gBrowser.tabContainer.itemCount
264
265 gBrowser.moveTabTo(tab, (index + 1) % total)
266
267 # Display the Help Dialog
268 command_help = (vim) ->
269 help.injectHelp(vim.window.document, commands)
270
271 findStorage = { lastSearchString: '' }
272
273 # Switch into find mode
274 command_find = (vim, storage) ->
275 vim.enterMode('find', { highlight: false })
276
277 # Switch into find mode with highlighting
278 command_find_hl = (vim, storage) ->
279 vim.enterMode('find', { highlight: true })
280
281 # Search for the last pattern
282 command_find_next = (vim, storage) ->
283 if findBar = utils.getRootWindow(vim.window).gBrowser.getFindBar()
284 if findStorage.lastSearchString.length > 0
285 findBar._findField.value = findStorage.lastSearchString
286 findBar.onFindAgainCommand(false)
287
288 # Search for the last pattern backwards
289 command_find_prev = (vim, storage) ->
290 if findBar = utils.getRootWindow(vim.window).gBrowser.getFindBar()
291 if findStorage.lastSearchString.length > 0
292 findBar._findField.value = findStorage.lastSearchString
293 findBar.onFindAgainCommand(true)
294
295 command_insert_mode = (vim) ->
296 vim.enterMode('insert')
297
298 command_Esc = (vim, storage, event) ->
299 utils.blurActiveElement(vim.window)
300
301 # Blur active XUL control
302 callback = -> event.originalTarget?.ownerDocument?.activeElement?.blur()
303 vim.window.setTimeout(callback, 0)
304
305 help.removeHelp(vim.window.document)
306
307 return unless rootWindow = utils.getRootWindow(vim.window)
308
309 rootWindow.DeveloperToolbar.hide()
310
311 rootWindow.gBrowser.getFindBar()?.close()
312
313
314 class Command
315 constructor: (@group, @name, @func, keys) ->
316 @defaultKeys = keys
317 if isPrefSet(@prefName('keys'))
318 try @keyValues = JSON.parse(getPref(@prefName('keys')))
319 else
320 @keyValues = keys
321
322 # Name of the preference for a given property
323 prefName: (value) -> "commands.#{ @name }.#{ value }"
324
325 keys: (value) ->
326 if value is undefined
327 return @keyValues
328 else
329 @keyValues = value or @defaultKeyValues
330 setPref(@prefName('keys'), value and JSON.stringify(value))
331
332 help: -> _("help_command_#{ @name }")
333
334 commands = [
335 new Command('urls', 'focus', command_focus, ['o'])
336 new Command('urls', 'focus_search', command_focus_search, ['O'])
337 new Command('urls', 'paste', command_paste, ['p'])
338 new Command('urls', 'paste_tab', command_paste_tab, ['P'])
339 new Command('urls', 'marker_yank', command_marker_yank, ['y,f'])
340 new Command('urls', 'marker_focus', command_marker_focus, ['v,f'])
341 new Command('urls', 'yank', command_yank, ['y,y'])
342 new Command('urls', 'reload', command_reload, ['r'])
343 new Command('urls', 'reload_force', command_reload_force, ['R'])
344 new Command('urls', 'reload_all', command_reload_all, ['a,r'])
345 new Command('urls', 'reload_all_force', command_reload_all_force, ['a,R'])
346 new Command('urls', 'stop', command_stop, ['s'])
347 new Command('urls', 'stop_all', command_stop_all, ['a,s'])
348
349 new Command('nav', 'scroll_to_top', command_scroll_to_top , ['g,g'])
350 new Command('nav', 'scroll_to_bottom', command_scroll_to_bottom, ['G'])
351 new Command('nav', 'scroll_down', command_scroll_down, ['j', 'c-e'])
352 new Command('nav', 'scroll_up', command_scroll_up, ['k', 'c-y'])
353 new Command('nav', 'scroll_left', command_scroll_left, ['h'])
354 new Command('nav', 'scroll_right', command_scroll_right , ['l'])
355 new Command('nav', 'scroll_half_page_down', command_scroll_half_page_down, ['d'])
356 new Command('nav', 'scroll_half_page_up', command_scroll_half_page_up, ['u'])
357 new Command('nav', 'scroll_page_down', command_scroll_page_down, ['c-f'])
358 new Command('nav', 'scroll_page_up', command_scroll_page_up, ['c-b'])
359
360 new Command('tabs', 'open_tab', command_open_tab, ['t'])
361 new Command('tabs', 'tab_prev', command_tab_prev, ['J', 'g,T'])
362 new Command('tabs', 'tab_next', command_tab_next, ['K', 'g,t'])
363 new Command('tabs', 'tab_move_left', command_tab_move_left, ['c-J'])
364 new Command('tabs', 'tab_move_right', command_tab_move_right, ['c-K'])
365 new Command('tabs', 'home', command_home, ['g,h'])
366 new Command('tabs', 'tab_first', command_tab_first, ['g,H', 'g,^'])
367 new Command('tabs', 'tab_last', command_tab_last, ['g,L', 'g,$'])
368 new Command('tabs', 'close_tab', command_close_tab, ['x'])
369 new Command('tabs', 'restore_tab', command_restore_tab, ['X'])
370
371 new Command('browse', 'follow', command_follow, ['f'])
372 new Command('browse', 'follow_in_tab', command_follow_in_tab, ['F'])
373 new Command('browse', 'follow_multiple', command_follow_multiple, ['a,f'])
374 new Command('browse', 'follow_previous', command_follow_prev, ['['])
375 new Command('browse', 'follow_next', command_follow_next, [']'])
376 new Command('browse', 'go_up_path', command_go_up_path, ['g,u'])
377 new Command('browse', 'go_to_root', command_go_to_root, ['g,U'])
378 new Command('browse', 'back', command_back, ['H'])
379 new Command('browse', 'forward', command_forward, ['L'])
380
381 new Command('misc', 'find', command_find, ['/'])
382 new Command('misc', 'find_hl', command_find_hl, ['a,/'])
383 new Command('misc', 'find_next', command_find_next, ['n'])
384 new Command('misc', 'find_prev', command_find_prev, ['N'])
385 new Command('misc', 'insert_mode', command_insert_mode, ['i'])
386 new Command('misc', 'help', command_help, ['?'])
387 new Command('misc', 'dev', command_dev, [':'])
388
389 escapeCommand =
390 new Command('misc', 'Esc', command_Esc, ['Esc'])
391 ]
392
393 searchForMatchingCommand = (keys) ->
394 for index in [0...keys.length] by 1
395 str = keys[index..].join(',')
396 for command in commands
397 for key in command.keys()
398 # The following hack is a workaround for the issue where # letter `c`
399 # is considered a start of command with control modifier `c-xxx`
400 if "#{key},".startsWith("#{str},")
401 return {match: true, exact: (key == str), command}
402
403 return {match: false}
404
405 isEscCommandKey = (keyStr) -> keyStr in escapeCommand.keys()
406
407 isReturnCommandKey = (keyStr) -> keyStr.contains('Return')
408
409 exports.commands = commands
410 exports.searchForMatchingCommand = searchForMatchingCommand
411 exports.isEscCommandKey = isEscCommandKey
412 exports.isReturnCommandKey = isReturnCommandKey
413 exports.findStorage = findStorage
Imprint / Impressum