]> git.gir.st - VimFx.git/blob - extension/packages/commands.coffee
Enabled Shift-space to rotate hint markers backwards
[VimFx.git] / extension / packages / commands.coffee
1 utils = require 'utils'
2 hints = require 'hints'
3 help = require 'help'
4 find = require 'find'
5
6 { _ } = require 'l10n'
7 { getPref
8 , setPref
9 , isPrefSet
10 , getFirefoxPref } = require 'prefs'
11
12 { classes: Cc, interfaces: Ci, utils: Cu } = Components
13
14 # Opens developer toolbar (Default shotrcut: Shift-F2)
15 command_dev = (vim) ->
16 if chromeWindow = utils.getRootWindow vim.window
17 chromeWindow.DeveloperToolbar.show(true)
18 chromeWindow.DeveloperToolbar.focus()
19
20 # Focus the Address Bar
21 command_focus = (vim) ->
22 if chromeWindow = utils.getRootWindow(vim.window)
23 chromeWindow.focusAndSelectUrlBar()
24 #
25 # Focus the Search Bar
26 command_focus_search = (vim) ->
27 if chromeWindow = utils.getRootWindow(vim.window)
28 if searchBar = chromeWindow.document.getElementById("searchbar")
29 searchBar.select()
30
31 # Navigate to the address that is currently stored in the system clipboard
32 command_paste = (vim) ->
33 url = utils.readFromClipboard(vim.window)
34 postData = null
35 if not utils.isURL(url) and submission = utils.browserSearchSubmission(url)
36 url = submission.uri.spec
37 { postData } = submission
38
39 if chromeWindow = utils.getRootWindow(vim.window)
40 chromeWindow.gBrowser.loadURIWithFlags(url, null, null, null, postData)
41
42 # Open new tab and navigate to the address that is currently stored in the system clipboard
43 command_paste_tab = (vim) ->
44 url = utils.readFromClipboard(vim.window)
45 postData = null
46 if not utils.isURL(url) and submission = utils.browserSearchSubmission(url)
47 url = submission.uri.spec
48 { postData } = submission
49
50 if chromeWindow = utils.getRootWindow vim.window
51 chromeWindow.gBrowser.selectedTab = chromeWindow.gBrowser.addTab(url, null, null, postData, null, false)
52
53 # Open new tab and focus the address bar
54 command_open_tab = (vim) ->
55 if chromeWindow = utils.getRootWindow(vim.window)
56 chromeWindow.BrowserOpenTab()
57
58 # Copy element URL to the clipboard
59 command_marker_yank = (vim) ->
60 markers = hints.injectHints(vim.window.document)
61 if markers.length > 0
62 cb = (marker) ->
63 if url = marker.element.href
64 marker.element.focus()
65 utils.writeToClipboard(vim.window, url)
66 else if utils.isTextInputElement(marker.element)
67 utils.writeToClipboard(vim.window, marker.element.value)
68
69 vim.enterHintsMode(markers, cb)
70
71 # Focus element
72 command_marker_focus = (vim) ->
73 markers = hints.injectHints(vim.window.document)
74 if markers.length > 0
75 vim.enterHintsMode(markers, (marker) -> marker.element.focus())
76
77 # Copy current URL to the clipboard
78 command_yank = (vim) ->
79 utils.writeToClipboard(vim.window, vim.window.location.toString())
80
81 # Reload the page, possibly from cache
82 command_reload = (vim) ->
83 vim.window.location.reload(false)
84
85 # Reload the page from the server
86 command_reload_force = (vim) ->
87 vim.window.location.reload(true)
88
89 # Reload the page, possibly from cache
90 command_reload_all = (vim) ->
91 if rootWindow = utils.getRootWindow(vim.window)
92 if tabs = rootWindow.gBrowser.tabContainer
93 for i in [0...tabs.itemCount]
94 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
95 window.location.reload(false)
96
97 # Reload the page from the server
98 command_reload_all_force = (vim) ->
99 if rootWindow = utils.getRootWindow(vim.window)
100 if tabs = rootWindow.gBrowser.tabContainer
101 for i in [0...tabs.itemCount]
102 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
103 window.location.reload(true)
104
105 command_stop = (vim) ->
106 vim.window.stop()
107
108 command_stop_all = (vim) ->
109 if rootWindow = utils.getRootWindow(vim.window)
110 if tabs = rootWindow.gBrowser.tabContainer
111 for i in [0...tabs.itemCount]
112 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
113 window.stop()
114
115 # Scroll to the top of the page
116 command_scroll_to_top = (vim) ->
117 for i in [0...1000]
118 utils.simulateWheel(vim.window, 0, -1, utils.WHEEL_MODE_PAGE)
119
120 # Scroll to the bottom of the page
121 command_scroll_to_bottom = (vim) ->
122 for i in [0...1000]
123 utils.simulateWheel(vim.window, 0, 1, utils.WHEEL_MODE_PAGE)
124
125 # Scroll down a bit
126 command_scroll_down = (vim) ->
127 utils.simulateWheel(vim.window, 0, getPref('scroll_step_lines'), utils.WHEEL_MODE_LINE)
128
129 # Scroll up a bit
130 command_scroll_up = (vim) ->
131 utils.simulateWheel(vim.window, 0, -getPref('scroll_step_lines'), utils.WHEEL_MODE_LINE)
132
133 # Scroll left a bit
134 command_scroll_left = (vim) ->
135 utils.simulateWheel(vim.window, -getPref('scroll_step_lines'), 0, utils.WHEEL_MODE_LINE)
136
137 # Scroll right a bit
138 command_scroll_right = (vim) ->
139 utils.simulateWheel(vim.window, getPref('scroll_step_lines'), 0, utils.WHEEL_MODE_LINE)
140
141 # Scroll down half a page
142 command_scroll_half_page_down = (vim) ->
143 utils.simulateWheel(vim.window, 0, 0.5, utils.WHEEL_MODE_PAGE)
144
145 # Scroll up half a page
146 command_scroll_half_page_up = (vim) ->
147 utils.simulateWheel(vim.window, 0, -0.5, utils.WHEEL_MODE_PAGE)
148
149 # Scroll down full a page
150 command_scroll_page_down = (vim) ->
151 utils.simulateWheel(vim.window, 0, 1, utils.WHEEL_MODE_PAGE)
152
153 # Scroll up full a page
154 command_scroll_page_up = (vim) ->
155 utils.simulateWheel(vim.window, 0, -1, utils.WHEEL_MODE_PAGE)
156
157 # Activate previous tab
158 command_tab_prev = (vim) ->
159 if rootWindow = utils.getRootWindow(vim.window)
160 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true)
161
162 # Activate next tab
163 command_tab_next = (vim) ->
164 if rootWindow = utils.getRootWindow(vim.window)
165 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true)
166
167 command_home = (vim) ->
168 url = getFirefoxPref('browser.startup.homepage')
169 if chromeWindow = utils.getRootWindow(vim.window)
170 chromeWindow.gBrowser.loadURIWithFlags(url, null, null, null, null)
171
172 # Go to the first tab
173 command_tab_first = (vim) ->
174 if rootWindow = utils.getRootWindow(vim.window)
175 rootWindow.gBrowser.tabContainer.selectedIndex = 0
176
177 # Go to the last tab
178 command_tab_last = (vim) ->
179 if rootWindow = utils.getRootWindow(vim.window)
180 itemCount = rootWindow.gBrowser.tabContainer.itemCount
181 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1
182
183 # Go back in history
184 command_back = (vim) ->
185 vim.window.history.back()
186
187 # Go forward in history
188 command_forward = (vim) ->
189 vim.window.history.forward()
190
191 # Close current tab
192 command_close_tab = (vim) ->
193 if rootWindow = utils.getRootWindow(vim.window)
194 unless rootWindow.gBrowser.selectedTab.pinned
195 rootWindow.gBrowser.removeCurrentTab()
196
197 # Restore last closed tab
198 command_restore_tab = (vim) ->
199 if rootWindow = utils.getRootWindow(vim.window)
200 ss = utils.getSessionStore()
201 if ss and ss.getClosedTabCount(rootWindow) > 0
202 ss.undoCloseTab(rootWindow, 0)
203
204 # Follow links with hint markers
205 command_follow = (vim) ->
206 if document = vim.window.document
207 markers = hints.injectHints(document)
208 if markers.length > 0
209 # This callback will be called with the selected marker as argument
210 cb = (marker) ->
211 marker.element.focus()
212 utils.simulateClick(marker.element)
213
214 vim.enterHintsMode(markers, cb)
215
216 # Follow links in a new Tab with hint markers
217 command_follow_in_tab = (vim) ->
218 markers = hints.injectHints(vim.window.document)
219 if markers.length > 0
220 # This callback will be called with the selected marker as argument
221 cb = (marker) ->
222 marker.element.focus()
223 utils.simulateClick(marker.element, { metaKey: true, ctrlKey: true })
224
225 vim.enterHintsMode(markers, cb)
226
227 # Move current tab to the left
228 command_tab_move_left = (vim) ->
229 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
230 if tab = gBrowser.selectedTab
231 index = gBrowser.tabContainer.selectedIndex
232 total = gBrowser.tabContainer.itemCount
233
234 # `total` is added to deal with negative offset
235 gBrowser.moveTabTo(tab, (total + index - 1) % total)
236
237 # Move current tab to the right
238 command_tab_move_right = (vim) ->
239 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
240 if tab = gBrowser.selectedTab
241 index = gBrowser.tabContainer.selectedIndex
242 total = gBrowser.tabContainer.itemCount
243
244 gBrowser.moveTabTo(tab, (index + 1) % total)
245
246 # Display the Help Dialog
247 command_help = (vim) ->
248 help.injectHelp(vim.window.document, commands)
249
250 # Switch into find mode
251 command_find = (vim) ->
252 find.injectFind vim.window.document, (findStr, startFindRng) ->
253 # Reset region and find string if new find stirng has arrived
254 if vim.findStr != findStr
255 [vim.findStr, vim.findRng] = [findStr, startFindRng]
256 # Perform forward find and store found region
257 return vim.findRng = find.find(vim.window, vim.findStr, vim.findRng, find.DIRECTION_FORWARDS)
258
259 # Switch into find mode with highlighting
260 command_find_hl = (vim) ->
261 find.injectFind vim.window.document, (findStr) ->
262 # Reset region and find string if new find stirng has arrived
263 return find.highlight(vim.window, findStr)
264
265 # Search for the last pattern
266 command_find_next = (vim) ->
267 if vim.findStr.length > 0
268 vim.findRng = find.find(vim.window, vim.findStr, vim.findRng, find.DIRECTION_FORWARDS, true)
269
270 # Search for the last pattern backwards
271 command_find_prev = (vim) ->
272 if vim.findStr.length > 0
273 vim.findRng = find.find(vim.window, vim.findStr, vim.findRng, find.DIRECTION_BACKWARDS, true)
274
275 # Close the Help dialog and cancel the pending hint marker action
276 command_Esc = (vim) ->
277 # Blur active element if it's editable. Other elements
278 # aren't blurred - we don't want to interfere with
279 # the browser too much
280 activeElement = vim.window.document.activeElement
281 if utils.isElementEditable(activeElement)
282 activeElement.blur()
283
284 #Remove Find input
285 find.removeFind(vim.window.document)
286
287 # Remove hints
288 hints.removeHints(vim.window.document)
289
290 # Hide help dialog
291 help.removeHelp(vim.window.document)
292
293 # Finally enter normal mode
294 vim.enterNormalMode()
295
296 if not getPref('leave_dt_on_esc')
297 if chromeWindow = utils.getRootWindow(vim.window)
298 chromeWindow.DeveloperToolbar.hide()
299
300 class Command
301 constructor: (@group, @name, @func, keys) ->
302 @defaultKeys = keys
303 if isPrefSet(@prefName('keys'))
304 try @keyValues = JSON.parse(getPref(@prefName('keys')))
305 else
306 @keyValues = keys
307
308 # Check if this command may match given string if more chars are added
309 mayMatch: (value) ->
310 return @keys.reduce(((m, v) -> m or v.indexOf(value) == 0), false)
311
312 # Check is this command matches given string
313 match: (value) ->
314 return @keys.reduce(((m, v) -> m or v == value), false)
315
316 # Name of the preference for a given property
317 prefName: (value) -> "commands.#{ @name }.#{ value }"
318
319 assign: (value) ->
320 @keys = value or @defaultKeys
321 setPref(@prefName('keys'), value and JSON.stringify(value))
322
323 enabled: (value) ->
324 if value is undefined
325 return getPref(@prefName('enabled'), true)
326 else
327 setPref(@prefName('enabled'), !!value)
328
329 keys: (value) ->
330 if value is undefined
331 return @keyValues
332 else
333 @keyValues = value or @defaultKeyValues
334 setPref(@prefName('keys'), value and JSON.stringify(value))
335
336 help: -> _("help_command_#{ @name }")
337
338 commands = [
339 new Command('urls', 'focus', command_focus, ['o'])
340 new Command('urls', 'focus_search', command_focus_search, ['O'])
341 new Command('urls', 'paste', command_paste, ['p'])
342 new Command('urls', 'paste_tab', command_paste_tab, ['P'])
343 new Command('urls', 'marker_yank', command_marker_yank, ['y,f'])
344 new Command('urls', 'marker_focus', command_marker_focus, ['v,f'])
345 new Command('urls', 'yank', command_yank, ['y,y'])
346 new Command('urls', 'reload', command_reload, ['r'])
347 new Command('urls', 'reload_force', command_reload_force, ['R'])
348 new Command('urls', 'reload_all', command_reload_all, ['a,r'])
349 new Command('urls', 'reload_all_force', command_reload_all_force, ['a,R'])
350 new Command('urls', 'stop', command_stop, ['s'])
351 new Command('urls', 'stop_all', command_stop_all, ['a,s'])
352
353 new Command('nav', 'scroll_to_top', command_scroll_to_top , ['g,g'])
354 new Command('nav', 'scroll_to_bottom', command_scroll_to_bottom, ['G'])
355 new Command('nav', 'scroll_down', command_scroll_down, ['j', 'c-e'])
356 new Command('nav', 'scroll_up', command_scroll_up, ['k', 'c-y'])
357 new Command('nav', 'scroll_left', command_scroll_left, ['h'])
358 new Command('nav', 'scroll_right', command_scroll_right , ['l'])
359 new Command('nav', 'scroll_half_page_down', command_scroll_half_page_down, ['d'])
360 new Command('nav', 'scroll_half_page_up', command_scroll_half_page_up, ['u'])
361 new Command('nav', 'scroll_page_down', command_scroll_page_down, ['c-f'])
362 new Command('nav', 'scroll_page_up', command_scroll_page_up, ['c-b'])
363
364 new Command('tabs', 'open_tab', command_open_tab, ['t'])
365 new Command('tabs', 'tab_prev', command_tab_prev, ['J', 'g,T'])
366 new Command('tabs', 'tab_next', command_tab_next, ['K', 'g,t'])
367 new Command('tabs', 'tab_move_left', command_tab_move_left, ['c-J'])
368 new Command('tabs', 'tab_move_right', command_tab_move_right, ['c-K'])
369 new Command('tabs', 'home', command_home, ['g,h'])
370 new Command('tabs', 'tab_first', command_tab_first, ['g,H', 'g,\^'])
371 new Command('tabs', 'tab_last', command_tab_last, ['g,L', 'g,$'])
372 new Command('tabs', 'close_tab', command_close_tab, ['x'])
373 new Command('tabs', 'restore_tab', command_restore_tab, ['X'])
374
375 new Command('browse', 'follow', command_follow, ['f'])
376 new Command('browse', 'follow_in_tab', command_follow_in_tab, ['F'])
377 new Command('browse', 'back', command_back, ['H'])
378 new Command('browse', 'forward', command_forward, ['L'])
379
380 new Command('misc', 'find', command_find, ['/'])
381 new Command('misc', 'find_hl', command_find_hl, ['a,/'])
382 new Command('misc', 'find_next', command_find_next, ['n'])
383 new Command('misc', 'find_prev', command_find_prev, ['N'])
384 new Command('misc', 'help', command_help, ['?'])
385 new Command('misc', 'Esc', command_Esc, ['Esc'])
386 new Command('misc', 'dev', command_dev, [':'])
387 ]
388
389 # Called in hints mode. Will process the char, update and hide/show markers
390 hintCharHandler = (vim, keyStr) ->
391 if keyStr
392 if keyStr == 'Space'
393 rotateOverlappingMarkers(vim.markers, true)
394 return
395 else if keyStr == 'Shift-Space'
396 rotateOverlappingMarkers(vim.markers, false)
397 return
398
399 # Get char and escape it to avoid problems with String.search
400 key = utils.regexpEscape(keyStr)
401
402 # First do a pre match - count how many markers will match with the new character entered
403 if vim.markers.reduce(((v, marker) -> v or marker.willMatch(key)), false)
404 for marker in vim.markers
405 marker.matchHintChar(key)
406
407 if marker.isMatched()
408 # Add element features to the bloom filter
409 marker.reward()
410 vim.cb(marker)
411 hints.removeHints(vim.window.document)
412 vim.enterNormalMode()
413 break
414
415 findCommand = (keys) ->
416 for i in [0...keys.length]
417 str = keys[i..].join(',')
418 for cmd in commands
419 for key in cmd.keys()
420 if key == str and cmd.enabled()
421 return cmd
422
423 maybeCommand = (keys) ->
424 for i in [0...keys.length]
425 str = keys[i..].join(',')
426 for cmd in commands
427 for key in cmd.keys()
428 if key.indexOf(str) == 0 and cmd.enabled()
429 return true
430
431 # Finds all stacks of markers that overlap each other (by using `getStackFor`) (#1), and rotates
432 # their `z-index`:es (#2), thus alternating which markers are visible.
433 rotateOverlappingMarkers = (originalMarkers, forward) ->
434 # Shallow working copy. This is necessary since `markers` will be mutated and eventually empty.
435 markers = originalMarkers[..]
436
437 # (#1)
438 stacks = (getStackFor(markers.pop(), markers) while markers.length > 0)
439
440 # (#2)
441 # Stacks of length 1 don't participate in any overlapping, and can therefore be skipped.
442 for stack in stacks when stack.length > 1
443 # This sort is not required, but makes the rotation more predictable.
444 stack.sort((a, b) -> a.markerElement.style.zIndex - b.markerElement.style.zIndex)
445
446 # Array of z indices
447 indexStack = (m.markerElement.style.zIndex for m in stack)
448 # Shift the array of indices one item forward or back
449 if forward
450 indexStack.unshift(indexStack.pop())
451 else
452 indexStack.push(indexStack.shift())
453
454 for marker, index in stack
455 marker.markerElement.style.setProperty('z-index', indexStack[index], 'important')
456
457 # Get an array containing `marker` and all markers that overlap `marker`, if any, which is called a
458 # "stack". All markers in the returned stack are spliced out from `markers`, thus mutating it.
459 getStackFor = (marker, markers) ->
460 stack = [marker]
461
462 { top, bottom, left, right } = marker.position
463
464 index = 0
465 while index < markers.length
466 nextMarker = markers[index]
467
468 { top: nextTop, bottom: nextBottom, left: nextLeft, right: nextRight } = nextMarker.position
469 overlapsVertically = (nextBottom >= top and nextTop <= bottom)
470 overlapsHorizontally = (nextRight >= left and nextLeft <= right)
471
472 if overlapsVertically and overlapsHorizontally
473 # Also get all markers overlapping this one
474 markers.splice(index, 1)
475 stack = stack.concat(getStackFor(nextMarker, markers))
476 else
477 # Continue the search
478 index++
479
480 return stack
481
482 exports.hintCharHandler = hintCharHandler
483 exports.findCommand = findCommand
484 exports.maybeCommand = maybeCommand
485 exports.commands = commands
Imprint / Impressum