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