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