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