]> git.gir.st - VimFx.git/blob - extension/packages/commands.coffee
Closes #157. Fix for Firefox 25
[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 # Navigate to the address that is currently stored in the system clipboard
26 command_paste = (vim) ->
27 url = utils.readFromClipboard(vim.window)
28 postData = null
29 if not utils.isURL(url) and submission = utils.browserSearchSubmission(url)
30 url = submission.uri.spec
31 { postData } = submission
32
33 if chromeWindow = utils.getRootWindow(vim.window)
34 chromeWindow.gBrowser.loadURIWithFlags(url, null, null, null, postData)
35
36 # Open new tab and navigate to the address that is currently stored in the system clipboard
37 command_paste_tab = (vim) ->
38 url = utils.readFromClipboard(vim.window)
39 postData = null
40 if not utils.isURL(url) and submission = utils.browserSearchSubmission(url)
41 url = submission.uri.spec
42 { postData } = submission
43
44 if chromeWindow = utils.getRootWindow vim.window
45 chromeWindow.gBrowser.selectedTab = chromeWindow.gBrowser.addTab(url, null, null, postData, null, false)
46
47 # Open new tab and focus the address bar
48 command_open_tab = (vim) ->
49 if chromeWindow = utils.getRootWindow(vim.window)
50 chromeWindow.BrowserOpenTab()
51
52 # Copy element URL to the clipboard
53 command_marker_yank = (vim) ->
54 markers = hints.injectHints(vim.window.document)
55 if markers.length > 0
56 cb = (marker) ->
57 if url = marker.element.href
58 marker.element.focus()
59 utils.writeToClipboard(vim.window, url)
60 else if utils.isTextInputElement(marker.element)
61 utils.writeToClipboard(vim.window, marker.element.value)
62
63 vim.enterHintsMode(markers, cb)
64
65 # Focus element
66 command_marker_focus = (vim) ->
67 markers = hints.injectHints(vim.window.document)
68 if markers.length > 0
69 vim.enterHintsMode(markers, (marker) -> marker.element.focus())
70
71 # Copy current URL to the clipboard
72 command_yank = (vim) ->
73 utils.writeToClipboard(vim.window, vim.window.location.toString())
74
75 # Reload the page, possibly from cache
76 command_reload = (vim) ->
77 vim.window.location.reload(false)
78
79 # Reload the page from the server
80 command_reload_force = (vim) ->
81 vim.window.location.reload(true)
82
83 # Reload the page, possibly from cache
84 command_reload_all = (vim) ->
85 if rootWindow = utils.getRootWindow(vim.window)
86 if tabs = rootWindow.gBrowser.tabContainer
87 for i in [0...tabs.itemCount]
88 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
89 window.location.reload(false)
90
91 # Reload the page from the server
92 command_reload_all_force = (vim) ->
93 if rootWindow = utils.getRootWindow(vim.window)
94 if tabs = rootWindow.gBrowser.tabContainer
95 for i in [0...tabs.itemCount]
96 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
97 window.location.reload(true)
98
99 command_stop = (vim) ->
100 vim.window.stop()
101
102 command_stop_all = (vim) ->
103 if rootWindow = utils.getRootWindow(vim.window)
104 if tabs = rootWindow.gBrowser.tabContainer
105 for i in [0...tabs.itemCount]
106 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
107 window.stop()
108
109 # Scroll to the top of the page
110 command_scroll_to_top = (vim) ->
111 for i in [0...1000]
112 utils.simulateWheel(vim.window, 0, -1, utils.WHEEL_MODE_PAGE)
113
114 # Scroll to the bottom of the page
115 command_scroll_to_bottom = (vim) ->
116 for i in [0...1000]
117 utils.simulateWheel(vim.window, 0, 1, utils.WHEEL_MODE_PAGE)
118
119 # Scroll down a bit
120 command_scroll_down = (vim) ->
121 utils.simulateWheel(vim.window, 0, getPref('scroll_step_lines'), utils.WHEEL_MODE_LINE)
122
123 # Scroll up a bit
124 command_scroll_up = (vim) ->
125 utils.simulateWheel(vim.window, 0, -getPref('scroll_step_lines'), utils.WHEEL_MODE_LINE)
126
127 # Scroll left a bit
128 command_scroll_left = (vim) ->
129 utils.simulateWheel(vim.window, -getPref('scroll_step_lines'), 0, utils.WHEEL_MODE_LINE)
130
131 # Scroll right a bit
132 command_scroll_right = (vim) ->
133 utils.simulateWheel(vim.window, getPref('scroll_step_lines'), 0, utils.WHEEL_MODE_LINE)
134
135 # Scroll down half a page
136 command_scroll_half_page_down = (vim) ->
137 utils.simulateWheel(vim.window, 0, 0.5, utils.WHEEL_MODE_PAGE)
138
139 # Scroll up half a page
140 command_scroll_half_page_up = (vim) ->
141 utils.simulateWheel(vim.window, 0, -0.5, utils.WHEEL_MODE_PAGE)
142
143 # Scroll down full a page
144 command_scroll_page_down = (vim) ->
145 utils.simulateWheel(vim.window, 0, 1, utils.WHEEL_MODE_PAGE)
146
147 # Scroll up full a page
148 command_scroll_page_up = (vim) ->
149 utils.simulateWheel(vim.window, 0, -1, utils.WHEEL_MODE_PAGE)
150
151 # Activate previous tab
152 command_tab_prev = (vim) ->
153 if rootWindow = utils.getRootWindow(vim.window)
154 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true)
155
156 # Activate next tab
157 command_tab_next = (vim) ->
158 if rootWindow = utils.getRootWindow(vim.window)
159 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true)
160
161 command_home = (vim) ->
162 url = getFirefoxPref('browser.startup.homepage')
163 if chromeWindow = utils.getRootWindow(vim.window)
164 chromeWindow.gBrowser.loadURIWithFlags(url, null, null, null, null)
165
166 # Go to the first tab
167 command_tab_first = (vim) ->
168 if rootWindow = utils.getRootWindow(vim.window)
169 rootWindow.gBrowser.tabContainer.selectedIndex = 0
170
171 # Go to the last tab
172 command_tab_last = (vim) ->
173 if rootWindow = utils.getRootWindow(vim.window)
174 itemCount = rootWindow.gBrowser.tabContainer.itemCount
175 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1
176
177 # Go back in history
178 command_back = (vim) ->
179 vim.window.history.back()
180
181 # Go forward in history
182 command_forward = (vim) ->
183 vim.window.history.forward()
184
185 # Close current tab
186 command_close_tab = (vim) ->
187 if rootWindow = utils.getRootWindow(vim.window)
188 unless rootWindow.gBrowser.selectedTab.pinned
189 rootWindow.gBrowser.removeCurrentTab()
190
191 # Restore last closed tab
192 command_restore_tab = (vim) ->
193 if rootWindow = utils.getRootWindow(vim.window)
194 ss = utils.getSessionStore()
195 if ss and ss.getClosedTabCount(rootWindow) > 0
196 ss.undoCloseTab(rootWindow, 0)
197
198 # Follow links with hint markers
199 command_follow = (vim) ->
200 if document = vim.window.document
201 markers = hints.injectHints(document)
202 if markers.length > 0
203 # This callback will be called with the selected marker as argument
204 cb = (marker) ->
205 marker.element.focus()
206 utils.simulateClick(marker.element)
207
208 vim.enterHintsMode(markers, cb)
209
210 # Follow links in a new Tab with hint markers
211 command_follow_in_tab = (vim) ->
212 markers = hints.injectHints(vim.window.document)
213 if markers.length > 0
214 # This callback will be called with the selected marker as argument
215 cb = (marker) ->
216 marker.element.focus()
217 utils.simulateClick(marker.element, { metaKey: true, ctrlKey: true })
218
219 vim.enterHintsMode(markers, cb)
220
221 # Move current tab to the left
222 command_tab_move_left = (vim) ->
223 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
224 if tab = gBrowser.selectedTab
225 index = gBrowser.tabContainer.selectedIndex
226 total = gBrowser.tabContainer.itemCount
227
228 # `total` is added to deal with negative offset
229 gBrowser.moveTabTo(tab, (total + index - 1) % total)
230
231 # Move current tab to the right
232 command_tab_move_right = (vim) ->
233 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
234 if tab = gBrowser.selectedTab
235 index = gBrowser.tabContainer.selectedIndex
236 total = gBrowser.tabContainer.itemCount
237
238 gBrowser.moveTabTo(tab, (index + 1) % total)
239
240 # Display the Help Dialog
241 command_help = (vim) ->
242 help.injectHelp(vim.window.document, commands)
243
244 # Switch into find mode
245 command_find = (vim) ->
246 find.injectFind vim.window.document, (findStr, startFindRng) ->
247 # Reset region and find string if new find stirng has arrived
248 if vim.findStr != findStr
249 [vim.findStr, vim.findRng] = [findStr, startFindRng]
250 # Perform forward find and store found region
251 return vim.findRng = find.find(vim.window, vim.findStr, vim.findRng, find.DIRECTION_FORWARDS)
252
253 # Switch into find mode with highlighting
254 command_find_hl = (vim) ->
255 find.injectFind vim.window.document, (findStr) ->
256 # Reset region and find string if new find stirng has arrived
257 return find.highlight(vim.window, findStr)
258
259 # Search for the last pattern
260 command_find_next = (vim) ->
261 if vim.findStr.length > 0
262 vim.findRng = find.find(vim.window, vim.findStr, vim.findRng, find.DIRECTION_FORWARDS, true)
263
264 # Search for the last pattern backwards
265 command_find_prev = (vim) ->
266 if vim.findStr.length > 0
267 vim.findRng = find.find(vim.window, vim.findStr, vim.findRng, find.DIRECTION_BACKWARDS, true)
268
269 # Close the Help dialog and cancel the pending hint marker action
270 command_Esc = (vim) ->
271 # Blur active element if it's editable. Other elements
272 # aren't blurred - we don't want to interfere with
273 # the browser too much
274 activeElement = vim.window.document.activeElement
275 if utils.isElementEditable(activeElement)
276 activeElement.blur()
277
278 #Remove Find input
279 find.removeFind(vim.window.document)
280
281 # Remove hints
282 hints.removeHints(vim.window.document)
283
284 # Hide help dialog
285 help.removeHelp(vim.window.document)
286
287 # Finally enter normal mode
288 vim.enterNormalMode()
289
290 if not getPref('leave_dt_on_esc')
291 if chromeWindow = utils.getRootWindow(vim.window)
292 chromeWindow.DeveloperToolbar.hide()
293
294 class Command
295 constructor: (@group, @name, @func, keys) ->
296 @defaultKeys = keys
297 if isPrefSet(@prefName('keys'))
298 try @keyValues = JSON.parse(getPref(@prefName('keys')))
299 else
300 @keyValues = keys
301
302 # Check if this command may match given string if more chars are added
303 mayMatch: (value) ->
304 return @keys.reduce(((m, v) -> m or v.indexOf(value) == 0), false)
305
306 # Check is this command matches given string
307 match: (value) ->
308 return @keys.reduce(((m, v) -> m or v == value), false)
309
310 # Name of the preference for a given property
311 prefName: (value) -> "commands.#{ @name }.#{ value }"
312
313 assign: (value) ->
314 @keys = value or @defaultKeys
315 setPref(@prefName('keys'), value and JSON.stringify(value))
316
317 enabled: (value) ->
318 if value is undefined
319 return getPref(@prefName('enabled'), true)
320 else
321 setPref(@prefName('enabled'), !!value)
322
323 keys: (value) ->
324 if value is undefined
325 return @keyValues
326 else
327 @keyValues = value or @defaultKeyValues
328 setPref(@prefName('keys'), value and JSON.stringify(value))
329
330 help: -> _("help_command_#{ @name }")
331
332 commands = [
333 new Command('urls', 'focus', command_focus, ['o'])
334 new Command('urls', 'paste', command_paste, ['p'])
335 new Command('urls', 'paste_tab', command_paste_tab, ['P'])
336 new Command('urls', 'marker_yank', command_marker_yank, ['y,f'])
337 new Command('urls', 'marker_focus', command_marker_focus, ['v,f'])
338 new Command('urls', 'yank', command_yank, ['y,y'])
339 new Command('urls', 'reload', command_reload, ['r'])
340 new Command('urls', 'reload_force', command_reload_force, ['R'])
341 new Command('urls', 'reload_all', command_reload_all, ['a,r'])
342 new Command('urls', 'reload_all_force', command_reload_all_force, ['a,R'])
343 new Command('urls', 'stop', command_stop, ['s'])
344 new Command('urls', 'stop_all', command_stop_all, ['a,s'])
345
346 new Command('nav', 'scroll_to_top', command_scroll_to_top , ['g,g'])
347 new Command('nav', 'scroll_to_bottom', command_scroll_to_bottom, ['G'])
348 new Command('nav', 'scroll_down', command_scroll_down, ['j', 'c-e'])
349 new Command('nav', 'scroll_up', command_scroll_up, ['k', 'c-y'])
350 new Command('nav', 'scroll_left', command_scroll_left, ['h'])
351 new Command('nav', 'scroll_right', command_scroll_right , ['l'])
352 new Command('nav', 'scroll_half_page_down', command_scroll_half_page_down, ['d'])
353 new Command('nav', 'scroll_half_page_up', command_scroll_half_page_up, ['u'])
354 new Command('nav', 'scroll_page_down', command_scroll_page_down, ['c-f'])
355 new Command('nav', 'scroll_page_up', command_scroll_page_up, ['c-b'])
356
357 new Command('tabs', 'open_tab', command_open_tab, ['t'])
358 new Command('tabs', 'tab_prev', command_tab_prev, ['J', 'g,T'])
359 new Command('tabs', 'tab_next', command_tab_next, ['K', 'g,t'])
360 new Command('tabs', 'tab_move_left', command_tab_move_left, ['c-J'])
361 new Command('tabs', 'tab_move_right', command_tab_move_right, ['c-K'])
362 new Command('tabs', 'home', command_home, ['g,h'])
363 new Command('tabs', 'tab_first', command_tab_first, ['g,H', 'g,\^'])
364 new Command('tabs', 'tab_last', command_tab_last, ['g,L', 'g,$'])
365 new Command('tabs', 'close_tab', command_close_tab, ['x'])
366 new Command('tabs', 'restore_tab', command_restore_tab, ['X'])
367
368 new Command('browse', 'follow', command_follow, ['f'])
369 new Command('browse', 'follow_in_tab', command_follow_in_tab, ['F'])
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', 'help', command_help, ['?'])
378 new Command('misc', 'Esc', command_Esc, ['Esc'])
379 new Command('misc', 'dev', command_dev, [':'])
380 ]
381
382 # Called in hints mode. Will process the char, update and hide/show markers
383 hintCharHandler = (vim, keyStr) ->
384 if keyStr
385 # Get char and escape it to avoid problems with String.search
386 key = utils.regexpEscape(keyStr)
387
388 # First do a pre match - count how many markers will match with the new character entered
389 if vim.markers.reduce(((v, marker) -> v or marker.willMatch(key)), false)
390 for marker in vim.markers
391 marker.matchHintChar(key)
392
393 if marker.isMatched()
394 # Add element features to the bloom filter
395 marker.reward()
396 vim.cb(marker)
397 hints.removeHints(vim.window.document)
398 vim.enterNormalMode()
399 break
400
401 findCommand = (keys) ->
402 for i in [0...keys.length]
403 str = keys[i..].join(',')
404 for cmd in commands
405 for key in cmd.keys()
406 if key == str and cmd.enabled()
407 return cmd
408
409 maybeCommand = (keys) ->
410 for i in [0...keys.length]
411 str = keys[i..].join(',')
412 for cmd in commands
413 for key in cmd.keys()
414 if key.indexOf(str) == 0 and cmd.enabled()
415 return true
416
417 exports.hintCharHandler = hintCharHandler
418 exports.findCommand = findCommand
419 exports.maybeCommand = maybeCommand
Imprint / Impressum