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