]> git.gir.st - VimFx.git/blob - extension/packages/commands.coffee
Developer Toolbar shortcut hotfix
[VimFx.git] / extension / packages / commands.coffee
1 { classes: Cc, interfaces: Ci, utils: Cu } = Components
2
3 utils = require 'utils'
4 hints = require 'hints'
5 help = require 'help'
6 find = require 'find'
7
8 { getPref
9 , setPref
10 , getFirefoxPref } = require 'prefs'
11
12 # Opens developer toolbar (Default shotrcut: Shift-F2)
13 command_dev = (vim) ->
14 if chromeWindow = utils.getRootWindow vim.window
15 chromeWindow.DeveloperToolbar.show(true)
16 chromeWindow.DeveloperToolbar.focus()
17
18 # Focus the Address Bar
19 command_o = (vim) ->
20 if chromeWindow = utils.getRootWindow vim.window
21 chromeWindow.focusAndSelectUrlBar()
22
23 # Navigate to the address that is currently stored in the system clipboard
24 command_p = (vim) ->
25 url = utils.readFromClipboard(vim.window)
26 postData = null
27 if not utils.isURL(url) and submission = utils.browserSearchSubmission url
28 url = submission.uri.spec
29 postData = submission.postData
30
31 if chromeWindow = utils.getRootWindow vim.window
32 chromeWindow.gBrowser.loadURIWithFlags url, null, null, null, postData
33
34 # Open new tab and navigate to the address that is currently stored in the system clipboard
35 command_P = (vim) ->
36 url = utils.readFromClipboard(vim.window)
37 postData = null
38 if not utils.isURL(url) and submission = utils.browserSearchSubmission url
39 url = submission.uri.spec
40 postData = submission.postData
41
42 if chromeWindow = utils.getRootWindow vim.window
43 chromeWindow.gBrowser.selectedTab = chromeWindow.gBrowser.addTab url, null, null, postData, null, false
44
45 # Open new tab and focus the address bar
46 command_t = (vim) ->
47 if chromeWindow = utils.getRootWindow vim.window
48 if gBrowser = chromeWindow.gBrowser
49 # Get the default url for the new tab
50 newtab_url = getFirefoxPref 'browser.newtab.url'
51 gBrowser.selectedTab = gBrowser.addTab newtab_url
52 # Focus the address bar
53 chromeWindow.focusAndSelectUrlBar()
54
55 # Copy current URL to the clipboard
56 command_yf = (vim) ->
57 markers = hints.injectHints vim.window.document
58 if markers?.length > 0
59 # This callback will be called with the selected marker as argument
60 cb = (marker) ->
61 if url = marker.element.href
62 utils.writeToClipboard vim.window, url
63
64 vim.enterHintsMode(markers, cb)
65
66 # Copy current URL to the clipboard
67 command_yy = (vim) ->
68 utils.writeToClipboard vim.window, vim.window.location.toString()
69
70 # Reload the page, possibly from cache
71 command_r = (vim) ->
72 vim.window.location.reload(false)
73
74 # Reload the page from the server
75 command_R = (vim) ->
76 vim.window.location.reload(true)
77
78 # Reload the page, possibly from cache
79 command_ar = (vim) ->
80 if rootWindow = utils.getRootWindow vim.window
81 if tabs = rootWindow.gBrowser.tabContainer
82 for i in [0...tabs.itemCount]
83 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
84 window.location.reload(false)
85
86 # Reload the page from the server
87 command_aR = (vim) ->
88 if rootWindow = utils.getRootWindow vim.window
89 if tabs = rootWindow.gBrowser.tabContainer
90 for i in [0...tabs.itemCount]
91 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
92 window.location.reload(true)
93
94 # Scroll to the top of the page
95 command_gg = (vim) ->
96 vim.window.scrollTo(0, 0)
97
98 # Scroll to the bottom of the page
99 command_G = (vim) ->
100 if document = vim.window.document
101 # Workaround the pages where body isn't the scrollable element.
102 # In this case we try to scroll 100k pixels
103 vim.window.scrollTo(0, Math.max(document.body.scrollHeight, 100000))
104
105 # Scroll down a bit
106 command_j_ce = (vim) ->
107 utils.smoothScroll vim.window, 0, (getPref 'scroll_step'), getPref 'scroll_time'
108
109 # Scroll up a bit
110 command_k_cy = (vim) ->
111 utils.smoothScroll vim.window, 0, -(getPref 'scroll_step'), getPref 'scroll_time'
112
113 # Scroll left a bit
114 command_h = (vim) ->
115 utils.smoothScroll vim.window, -(getPref 'scroll_step'), 0, getPref 'scroll_time'
116
117 # Scroll right a bit
118 command_l = (vim) ->
119 utils.smoothScroll vim.window, (getPref 'scroll_step'), 0, getPref 'scroll_time'
120
121 # Scroll down half a page
122 command_d = (vim) ->
123 utils.smoothScroll vim.window, 0, vim.window.innerHeight / 2, getPref 'scroll_time'
124
125 # Scroll up half a page
126 command_u = (vim) ->
127 utils.smoothScroll vim.window, 0, -vim.window.innerHeight / 2, getPref 'scroll_time'
128
129 # Scroll down full a page
130 command_cf = (vim) ->
131 step = (vim.window.innerHeight - (getPref 'scroll_step'))
132 utils.smoothScroll vim.window, 0, step, getPref 'scroll_time'
133
134 # Scroll up full a page
135 command_cb = (vim) ->
136 step = - (vim.window.innerHeight - (getPref 'scroll_step'))
137 utils.smoothScroll vim.window, 0, step, getPref 'scroll_time'
138
139 # Activate previous tab
140 command_J_gT = (vim) ->
141 if rootWindow = utils.getRootWindow vim.window
142 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true);
143
144 # Activate next tab
145 command_K_gt = (vim) ->
146 if rootWindow = utils.getRootWindow vim.window
147 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true);
148
149 command_gh = (vim) ->
150 homepage_url = getFirefoxPref 'browser.startup.homepage'
151 vim.window.location.assign homepage_url
152
153 # Go to the first tab
154 command_gH_g0 = (vim) ->
155 if rootWindow = utils.getRootWindow vim.window
156 rootWindow.gBrowser.tabContainer.selectedIndex = 0;
157
158 # Go to the last tab
159 command_gL_g$ = (vim) ->
160 if rootWindow = utils.getRootWindow vim.window
161 itemCount = rootWindow.gBrowser.tabContainer.itemCount;
162 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1;
163
164 # Go back in history
165 command_H = (vim) ->
166 vim.window.history.back()
167
168 # Go forward in history
169 command_L = (vim) ->
170 vim.window.history.forward()
171
172 # Close current tab
173 command_x = (vim) ->
174 if rootWindow = utils.getRootWindow vim.window
175 unless rootWindow.gBrowser.selectedTab.pinned
176 rootWindow.gBrowser.removeCurrentTab()
177
178 # Restore last closed tab
179 command_X = (vim) ->
180 if rootWindow = utils.getRootWindow vim.window
181 ss = utils.getSessionStore()
182 if ss and ss.getClosedTabCount(rootWindow) > 0
183 ss.undoCloseTab rootWindow, 0
184
185 # Follow links with hint markers
186 command_f = (vim) ->
187 if document = vim.window.document
188 markers = hints.injectHints document
189 if markers?.length > 0
190 # This callback will be called with the selected marker as argument
191 cb = (marker) ->
192 marker.element.focus()
193 utils.simulateClick marker.element
194
195 vim.enterHintsMode markers, cb
196
197 # Follow links in a new Tab with hint markers
198 command_F = (vim) ->
199 markers = hints.injectHints vim.window.document
200 if markers?.length > 0
201 # This callback will be called with the selected marker as argument
202 cb = (marker) ->
203 marker.element.focus()
204 utils.simulateClick marker.element, { metaKey: true, ctrlKey: true }
205
206 vim.enterHintsMode markers, cb
207
208 # Move current tab to the left
209 command_cJ = (vim) ->
210 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
211 if tab = gBrowser.selectedTab
212 index = gBrowser.tabContainer.selectedIndex
213 total = gBrowser.tabContainer.itemCount
214
215 # `total` is added to deal with negative offset
216 gBrowser.moveTabTo tab, (total + index - 1) % total
217
218 # Move current tab to the right
219 command_cK = (vim) ->
220 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
221 if tab = gBrowser.selectedTab
222 index = gBrowser.tabContainer.selectedIndex
223 total = gBrowser.tabContainer.itemCount
224
225 gBrowser.moveTabTo tab, (index + 1) % total
226
227 # Display the Help Dialog
228 command_help = (vim) ->
229 help.injectHelp vim.window.document, commandsHelp
230
231 # Switch into find mode
232 command_find = (vim) ->
233 vim.enterFindMode()
234 vim.findStr = ""
235
236 find.injectFind vim.window.document
237
238 # Search for the last pattern
239 command_n = (vim) ->
240 if vim.findStr.length > 0
241 if not find.find vim.window, vim.findStr, false
242 find.flashFind vim.window.document, "#{ vim.findStr } (Not Found)"
243
244 # Search for the last pattern backwards
245 command_N = (vim) ->
246 if vim.findStr.length > 0
247 if not find.find vim.window, vim.findStr, true
248 find.flashFind vim.window.document, "#{ vim.findStr } (Not Found)"
249
250 # Close the Help dialog and cancel the pending hint marker action
251 command_Esc = (vim) ->
252 # Blur active element if it's editable. Other elements
253 # aren't blurred - we don't want to interfere with
254 # the browser too much
255 activeElement = vim.window.document.activeElement
256 if utils.isElementEditable activeElement
257 activeElement.blur()
258
259 #Remove Find input
260 find.removeFind vim.window.document
261
262 # Remove hints
263 hints.removeHints vim.window.document
264
265 # Hide help dialog
266 help.removeHelp vim.window.document
267
268 # Finally enter normal mode
269 vim.enterNormalMode()
270
271 if not getPref('leave_dt_on_esc')
272 if chromeWindow = utils.getRootWindow vim.window
273 chromeWindow.DeveloperToolbar.hide()
274
275 commandGroups =
276 'urls':
277 'o': [ command_o, _('help_command_o') ]
278 'p': [ command_p, _('help_command_p') ]
279 'P': [ command_P, _('help_command_P') ]
280 'y,f': [ command_yf, _('help_command_yf') ]
281 'y,y': [ command_yy, _('help_command_yy') ]
282 'r': [ command_r, _('help_command_r') ]
283 'R': [ command_R, _('help_command_R') ]
284 'a,r': [ command_ar, _('help_command_ar') ]
285 'a,R': [ command_aR, _('help_command_aR') ]
286 'nav':
287 'g,g': [ command_gg , _('help_command_gg') ]
288 'G': [ command_G, _('help_command_G') ]
289 'j|c-e': [ command_j_ce, _('help_command_j_ce') ]
290 'k|c-y': [ command_k_cy, _('help_command_k_cy') ]
291 'h': [ command_h, _('help_command_h') ]
292 'l': [ command_l , _('help_command_l') ]
293 # Can't use c-u/c-d because c-u is widely used for viewing sources
294 'd': [ command_d, _('help_command_d') ]
295 'u': [ command_u, _('help_command_u') ]
296 'c-f': [ command_cf, _('help_command_cf') ]
297 'c-b': [ command_cb, _('help_command_cb') ]
298 'tabs':
299 't': [ command_t, _('help_command_t') ]
300 'J|g,T': [ command_J_gT, _('help_command_J_gT') ]
301 'K|g,t': [ command_K_gt, _('help_command_K_gt') ]
302 'c-J': [ command_cJ, _('help_command_cJ') ]
303 'c-K': [ command_cK, _('help_command_cK') ]
304 'g,h': [ command_gh, _('help_command_gh') ]
305 "g,H|g,\^": [ command_gH_g0, _('help_command_gH_g0') ]
306 'g,L|g,$': [ command_gL_g$, _('help_command_gL_g$') ]
307 'x': [ command_x, _('help_command_x') ]
308 'X': [ command_X, _('help_command_X') ]
309 'browse':
310 'f': [ command_f, _('help_command_f') ]
311 'F': [ command_F, _('help_command_F') ]
312 'H': [ command_H, _('help_command_H') ]
313 'L': [ command_L, _('help_command_L') ]
314 'misc':
315 # `.` is added to find command mapping to hack around Russian keyboard layout
316 '\.|/': [ command_find, _('help_command_find') ]
317 'n': [ command_n, _('help_command_n') ]
318 'N': [ command_N, _('help_command_N') ]
319 # `>` is added to help command mapping to hack around Russian keyboard layout
320 # See key-utils.coffee for more info
321 '?|>': [ command_help, _('help_command_help') ]
322 'Esc': [ command_Esc, _('help_command_Esc') ]
323 ':': [ command_dev, _('help_command_dev') ]
324
325 # Merge groups and split command pipes into individual commands
326 commands = do (commandGroups) ->
327 newCommands = {}
328 for group, commandsList of commandGroups
329 for keys, command of commandsList
330 for key in keys.split '|'
331 newCommands[key] = command[0]
332
333 return newCommands
334
335 # Extract the help text from the commands preserving groups formation
336 commandsHelp = do (commandGroups) ->
337 helpStrings = {}
338 for group, commandsList of commandGroups
339 helpGroup = {}
340 for keys, command of commandsList
341 helpGroup[keys] = command[1]
342
343 helpStrings[group] = helpGroup
344 return helpStrings
345
346 # Called in hints mode. Will process the char, update and hide/show markers
347 hintCharHandler = (vim, keyStr, charCode) ->
348 if keyStr and charCode > 0
349 # Get char and escape it to avoid problems with String.search
350 key = regexpEscape keyStr
351
352 # First do a pre match - count how many markers will match with the new character entered
353 if vim.markers.reduce ((v, marker) -> v or marker.willMatch key), false
354 for marker in vim.markers
355 marker.matchHintChar key
356
357 if marker.isMatched()
358 vim.cb marker
359 hints.removeHints vim.window.document
360 vim.enterNormalMode()
361 break
362
363 # Called in find mode. Will process charCode, update find, or close the
364 findCharHandler = (vim, keyStr, charCode) ->
365 backwards = false
366
367 toNormalMode = ->
368 find.removeFind vim.window.document
369 vim.enterNormalMode()
370
371 if keyStr and keyStr.match(/.*Return/)
372 return toNormalMode()
373 else if keyStr == 'Backspace'
374 # Delete last available character from the query string
375 if vim.findStr.length > 0
376 vim.findStr = vim.findStr.substr(0, vim.findStr.length - 1)
377 # Leave Find Mode the query string is already empty
378 else
379 return toNormalMode()
380 else if charCode > 0
381 vim.findStr += String.fromCharCode(charCode)
382 else
383 return
384
385 # Update the interface string
386
387 # Clear selection to avoid jumping between matching search results
388 vim.window.getSelection().removeAllRanges()
389
390 # Search only if the query string isn't emply.
391 # Otherwise it will pop up Find dialog
392 if find.find vim.window, vim.findStr, backwards
393 find.setFindStr vim.window.document, vim.findStr
394 else
395 find.setFindStr vim.window.document, "#{ vim.findStr } (Not Found)"
396
397
398 exports.hintCharHandler = hintCharHandler
399 exports.findCharHandler = findCharHandler
400 exports.commands = commands
401 exports.commandsHelp = commandsHelp
Imprint / Impressum