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