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