]> git.gir.st - VimFx.git/blob - extension/packages/commands.coffee
Merge branch 'hints-z-index' of git://github.com/lydell/VimFx into lydell-hints-z...
[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 link = find_link.find(vim.window.document, patterns)
226 utils.simulateClick(link, {metaKey: false, ctrlKey: false}) if link
227
228 # Follow previous page
229 command_follow_prev = helper_follow_link.bind(undefined, {type: 'prev'})
230
231 # Follow next page
232 command_follow_next = helper_follow_link.bind(undefined, {type: 'next'})
233
234 # Go up one level in the URL hierarchy
235 command_go_up_path = (vim) ->
236 path = vim.window.location.pathname
237 vim.window.location.pathname = path.replace(/\/[^\/]*?(\/)?$/, '')
238
239 # Go up to root of the URL hierarchy
240 command_go_to_root = (vim) ->
241 vim.window.location.href = vim.window.location.origin
242
243 # Move current tab to the left
244 command_tab_move_left = (vim) ->
245 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
246 if tab = gBrowser.selectedTab
247 index = gBrowser.tabContainer.selectedIndex
248 total = gBrowser.tabContainer.itemCount
249
250 # `total` is added to deal with negative offset
251 gBrowser.moveTabTo(tab, (total + index - 1) % total)
252
253 # Move current tab to the right
254 command_tab_move_right = (vim) ->
255 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
256 if tab = gBrowser.selectedTab
257 index = gBrowser.tabContainer.selectedIndex
258 total = gBrowser.tabContainer.itemCount
259
260 gBrowser.moveTabTo(tab, (index + 1) % total)
261
262 # Display the Help Dialog
263 command_help = (vim) ->
264 help.injectHelp(vim.window.document, commands)
265
266 findStorage = { lastSearchString: '' }
267
268 # Switch into find mode
269 command_find = (vim, storage) ->
270 vim.enterMode('find', { highlight: false })
271
272 # Switch into find mode with highlighting
273 command_find_hl = (vim, storage) ->
274 vim.enterMode('find', { highlight: true })
275
276 # Search for the last pattern
277 command_find_next = (vim, storage) ->
278 if findBar = utils.getRootWindow(vim.window).gBrowser.getFindBar()
279 if findStorage.lastSearchString.length > 0
280 findBar._findField.value = findStorage.lastSearchString
281 findBar.onFindAgainCommand(false)
282
283 # Search for the last pattern backwards
284 command_find_prev = (vim, storage) ->
285 if findBar = utils.getRootWindow(vim.window).gBrowser.getFindBar()
286 if findStorage.lastSearchString.length > 0
287 findBar._findField.value = findStorage.lastSearchString
288 findBar.onFindAgainCommand(true)
289
290 command_insert_mode = (vim) ->
291 vim.enterMode('insert')
292
293 command_Esc = (vim, storage, event) ->
294 utils.blurActiveElement(vim.window)
295
296 # Blur active XUL control
297 callback = -> event.originalTarget?.ownerDocument?.activeElement?.blur()
298 vim.window.setTimeout(callback, 0)
299
300 help.removeHelp(vim.window.document)
301
302 return unless rootWindow = utils.getRootWindow(vim.window)
303
304 rootWindow.DeveloperToolbar.hide()
305
306 rootWindow.gBrowser.getFindBar()?.close()
307
308
309 class Command
310 constructor: (@group, @name, @func, keys) ->
311 @defaultKeys = keys
312 if isPrefSet(@prefName('keys'))
313 try @keyValues = JSON.parse(getPref(@prefName('keys')))
314 else
315 @keyValues = keys
316
317 # Name of the preference for a given property
318 prefName: (value) -> "commands.#{ @name }.#{ value }"
319
320 keys: (value) ->
321 if value is undefined
322 return @keyValues
323 else
324 @keyValues = value or @defaultKeyValues
325 setPref(@prefName('keys'), value and JSON.stringify(value))
326
327 help: -> _("help_command_#{ @name }")
328
329 commands = [
330 new Command('urls', 'focus', command_focus, ['o'])
331 new Command('urls', 'focus_search', command_focus_search, ['O'])
332 new Command('urls', 'paste', command_paste, ['p'])
333 new Command('urls', 'paste_tab', command_paste_tab, ['P'])
334 new Command('urls', 'marker_yank', command_marker_yank, ['y,f'])
335 new Command('urls', 'marker_focus', command_marker_focus, ['v,f'])
336 new Command('urls', 'yank', command_yank, ['y,y'])
337 new Command('urls', 'reload', command_reload, ['r'])
338 new Command('urls', 'reload_force', command_reload_force, ['R'])
339 new Command('urls', 'reload_all', command_reload_all, ['a,r'])
340 new Command('urls', 'reload_all_force', command_reload_all_force, ['a,R'])
341 new Command('urls', 'stop', command_stop, ['s'])
342 new Command('urls', 'stop_all', command_stop_all, ['a,s'])
343
344 new Command('nav', 'scroll_to_top', command_scroll_to_top , ['g,g'])
345 new Command('nav', 'scroll_to_bottom', command_scroll_to_bottom, ['G'])
346 new Command('nav', 'scroll_down', command_scroll_down, ['j', 'c-e'])
347 new Command('nav', 'scroll_up', command_scroll_up, ['k', 'c-y'])
348 new Command('nav', 'scroll_left', command_scroll_left, ['h'])
349 new Command('nav', 'scroll_right', command_scroll_right , ['l'])
350 new Command('nav', 'scroll_half_page_down', command_scroll_half_page_down, ['d'])
351 new Command('nav', 'scroll_half_page_up', command_scroll_half_page_up, ['u'])
352 new Command('nav', 'scroll_page_down', command_scroll_page_down, ['c-f'])
353 new Command('nav', 'scroll_page_up', command_scroll_page_up, ['c-b'])
354
355 new Command('tabs', 'open_tab', command_open_tab, ['t'])
356 new Command('tabs', 'tab_prev', command_tab_prev, ['J', 'g,T'])
357 new Command('tabs', 'tab_next', command_tab_next, ['K', 'g,t'])
358 new Command('tabs', 'tab_move_left', command_tab_move_left, ['c-J'])
359 new Command('tabs', 'tab_move_right', command_tab_move_right, ['c-K'])
360 new Command('tabs', 'home', command_home, ['g,h'])
361 new Command('tabs', 'tab_first', command_tab_first, ['g,H', 'g,^'])
362 new Command('tabs', 'tab_last', command_tab_last, ['g,L', 'g,$'])
363 new Command('tabs', 'close_tab', command_close_tab, ['x'])
364 new Command('tabs', 'restore_tab', command_restore_tab, ['X'])
365
366 new Command('browse', 'follow', command_follow, ['f'])
367 new Command('browse', 'follow_in_tab', command_follow_in_tab, ['F'])
368 new Command('browse', 'follow_multiple', command_follow_multiple, ['a,f'])
369 new Command('browse', 'follow_previous', command_follow_prev, ['['])
370 new Command('browse', 'follow_next', command_follow_next, [']'])
371 new Command('browse', 'go_up_path', command_go_up_path, ['g,u'])
372 new Command('browse', 'go_to_root', command_go_to_root, ['g,U'])
373 new Command('browse', 'back', command_back, ['H'])
374 new Command('browse', 'forward', command_forward, ['L'])
375
376 new Command('misc', 'find', command_find, ['/'])
377 new Command('misc', 'find_hl', command_find_hl, ['a,/'])
378 new Command('misc', 'find_next', command_find_next, ['n'])
379 new Command('misc', 'find_prev', command_find_prev, ['N'])
380 new Command('misc', 'insert_mode', command_insert_mode, ['i'])
381 new Command('misc', 'help', command_help, ['?'])
382 new Command('misc', 'dev', command_dev, [':'])
383
384 escapeCommand =
385 new Command('misc', 'Esc', command_Esc, ['Esc'])
386 ]
387
388 searchForMatchingCommand = (keys) ->
389 for index in [0...keys.length] by 1
390 str = keys[index..].join(',')
391 for command in commands
392 for key in command.keys()
393 if key.startsWith(str)
394 return {match: true, exact: (key == str), command}
395
396 return {match: false}
397
398 isEscCommandKey = (keyStr) -> keyStr in escapeCommand.keys()
399
400 isReturnCommandKey = (keyStr) -> keyStr.contains('Return')
401
402 exports.commands = commands
403 exports.searchForMatchingCommand = searchForMatchingCommand
404 exports.isEscCommandKey = isEscCommandKey
405 exports.isReturnCommandKey = isReturnCommandKey
406 exports.findStorage = findStorage
Imprint / Impressum