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