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