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