]> git.gir.st - VimFx.git/blob - extension/packages/commands.coffee
Closes #114, closes #115.
[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 vim.window.scrollTo(0, 0)
103
104 # Scroll to the bottom of the page
105 command_G = (vim) ->
106 if document = vim.window.document
107 # Workaround the pages where body isn't the scrollable element.
108 # In this case we try to scroll 100k pixels
109 vim.window.scrollTo(0, Math.max(document.body.scrollHeight, 100000))
110
111 # Scroll down a bit
112 command_j_ce = (vim) ->
113 utils.smoothScroll(vim.window, 0, getPref('scroll_step'), getPref('scroll_time'))
114
115 # Scroll up a bit
116 command_k_cy = (vim) ->
117 utils.smoothScroll(vim.window, 0, -getPref('scroll_step'), getPref('scroll_time'))
118
119 # Scroll left a bit
120 command_h = (vim) ->
121 utils.smoothScroll(vim.window, -getPref('scroll_step'), 0, getPref('scroll_time'))
122
123 # Scroll right a bit
124 command_l = (vim) ->
125 utils.smoothScroll(vim.window, getPref('scroll_step'), 0, getPref('scroll_time'))
126
127 # Scroll down half a page
128 command_d = (vim) ->
129 utils.smoothScroll(vim.window, 0, vim.window.innerHeight / 2, getPref('scroll_time'))
130
131 # Scroll up half a page
132 command_u = (vim) ->
133 utils.smoothScroll(vim.window, 0, -vim.window.innerHeight / 2, getPref('scroll_time'))
134
135 # Scroll down full a page
136 command_cf = (vim) ->
137 step = (vim.window.innerHeight - getPref('scroll_step'))
138 utils.smoothScroll(vim.window, 0, step, getPref('scroll_time'))
139
140 # Scroll up full a page
141 command_cb = (vim) ->
142 step = -(vim.window.innerHeight - getPref('scroll_step'))
143 utils.smoothScroll(vim.window, 0, step, getPref('scroll_time'))
144
145 # Activate previous tab
146 command_J_gT = (vim) ->
147 if rootWindow = utils.getRootWindow(vim.window)
148 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true)
149
150 # Activate next tab
151 command_K_gt = (vim) ->
152 if rootWindow = utils.getRootWindow(vim.window)
153 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true)
154
155 command_gh = (vim) ->
156 url = getFirefoxPref('browser.startup.homepage')
157 if chromeWindow = utils.getRootWindow(vim.window)
158 chromeWindow.gBrowser.loadURIWithFlags(url, null, null, null, null)
159
160 # Go to the first tab
161 command_gH_g0 = (vim) ->
162 if rootWindow = utils.getRootWindow(vim.window)
163 rootWindow.gBrowser.tabContainer.selectedIndex = 0
164
165 # Go to the last tab
166 command_gL_g$ = (vim) ->
167 if rootWindow = utils.getRootWindow(vim.window)
168 itemCount = rootWindow.gBrowser.tabContainer.itemCount
169 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1
170
171 # Go back in history
172 command_H = (vim) ->
173 vim.window.history.back()
174
175 # Go forward in history
176 command_L = (vim) ->
177 vim.window.history.forward()
178
179 # Close current tab
180 command_x = (vim) ->
181 if rootWindow = utils.getRootWindow(vim.window)
182 unless rootWindow.gBrowser.selectedTab.pinned
183 rootWindow.gBrowser.removeCurrentTab()
184
185 # Restore last closed tab
186 command_X = (vim) ->
187 if rootWindow = utils.getRootWindow(vim.window)
188 ss = utils.getSessionStore()
189 if ss and ss.getClosedTabCount(rootWindow) > 0
190 ss.undoCloseTab(rootWindow, 0)
191
192 # Follow links with hint markers
193 command_f = (vim) ->
194 if document = vim.window.document
195 markers = hints.injectHints(document)
196 if markers?.length > 0
197 # This callback will be called with the selected marker as argument
198 cb = (marker) ->
199 marker.element.focus()
200 utils.simulateClick(marker.element)
201
202 vim.enterHintsMode(markers, cb)
203
204 # Follow links in a new Tab with hint markers
205 command_F = (vim) ->
206 markers = hints.injectHints(vim.window.document)
207 if markers?.length > 0
208 # This callback will be called with the selected marker as argument
209 cb = (marker) ->
210 marker.element.focus()
211 utils.simulateClick(marker.element, { metaKey: true, ctrlKey: true })
212
213 vim.enterHintsMode(markers, cb)
214
215 # Move current tab to the left
216 command_cJ = (vim) ->
217 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
218 if tab = gBrowser.selectedTab
219 index = gBrowser.tabContainer.selectedIndex
220 total = gBrowser.tabContainer.itemCount
221
222 # `total` is added to deal with negative offset
223 gBrowser.moveTabTo(tab, (total + index - 1) % total)
224
225 # Move current tab to the right
226 command_cK = (vim) ->
227 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
228 if tab = gBrowser.selectedTab
229 index = gBrowser.tabContainer.selectedIndex
230 total = gBrowser.tabContainer.itemCount
231
232 gBrowser.moveTabTo(tab, (index + 1) % total)
233
234 # Display the Help Dialog
235 command_help = (vim) ->
236 help.injectHelp(vim.window.document, commandsHelp)
237
238 # Switch into find mode
239 command_find = (vim) ->
240 find.injectFind vim.window.document, (findStr, startFindRng) ->
241 # Reset region and find string if new find stirng has arrived
242 if vim.findStr != findStr
243 [vim.findStr, vim.findRng] = [findStr, startFindRng]
244 # Perform forward find and store found region
245 return vim.findRng = find.find(vim.window, vim.findStr, vim.findRng, find.DIRECTION_FORWARDS)
246
247 # Switch into find mode with highlighting
248 command_find_hl = (vim) ->
249 find.injectFind vim.window.document, (findStr) ->
250 # Reset region and find string if new find stirng has arrived
251 return find.highlight(vim.window, findStr)
252
253 # Search for the last pattern
254 command_n = (vim) ->
255 if vim.findStr.length > 0
256 vim.findRng = find.find(vim.window, vim.findStr, vim.findRng, find.DIRECTION_FORWARDS, true)
257
258 # Search for the last pattern backwards
259 command_N = (vim) ->
260 if vim.findStr.length > 0
261 vim.findRng = find.find(vim.window, vim.findStr, vim.findRng, find.DIRECTION_BACKWARDS, true)
262
263 # Close the Help dialog and cancel the pending hint marker action
264 command_Esc = (vim) ->
265 # Blur active element if it's editable. Other elements
266 # aren't blurred - we don't want to interfere with
267 # the browser too much
268 activeElement = vim.window.document.activeElement
269 if utils.isElementEditable(activeElement)
270 activeElement.blur()
271
272 #Remove Find input
273 find.removeFind(vim.window.document)
274
275 # Remove hints
276 hints.removeHints(vim.window.document)
277
278 # Hide help dialog
279 help.removeHelp(vim.window.document)
280
281 # Finally enter normal mode
282 vim.enterNormalMode()
283
284 if not getPref('leave_dt_on_esc')
285 if chromeWindow = utils.getRootWindow(vim.window)
286 chromeWindow.DeveloperToolbar.hide()
287
288 commandGroups =
289 'urls':
290 'o': [ command_o, _('help_command_o') ]
291 'p': [ command_p, _('help_command_p') ]
292 'P': [ command_P, _('help_command_P') ]
293 'y,f': [ command_yf, _('help_command_yf') ]
294 'v,f': [ command_vf, _('help_command_vf') ]
295 'y,y': [ command_yy, _('help_command_yy') ]
296 'r': [ command_r, _('help_command_r') ]
297 'R': [ command_R, _('help_command_R') ]
298 'a,r': [ command_ar, _('help_command_ar') ]
299 'a,R': [ command_aR, _('help_command_aR') ]
300 'nav':
301 'g,g': [ command_gg , _('help_command_gg') ]
302 'G': [ command_G, _('help_command_G') ]
303 'j|c-e': [ command_j_ce, _('help_command_j_ce') ]
304 'k|c-y': [ command_k_cy, _('help_command_k_cy') ]
305 'h': [ command_h, _('help_command_h') ]
306 'l': [ command_l , _('help_command_l') ]
307 # Can't use c-u/c-d because c-u is widely used for viewing sources
308 'd': [ command_d, _('help_command_d') ]
309 'u': [ command_u, _('help_command_u') ]
310 'c-f': [ command_cf, _('help_command_cf') ]
311 'c-b': [ command_cb, _('help_command_cb') ]
312 'tabs':
313 't': [ command_t, _('help_command_t') ]
314 'J|g,T': [ command_J_gT, _('help_command_J_gT') ]
315 'K|g,t': [ command_K_gt, _('help_command_K_gt') ]
316 'c-J': [ command_cJ, _('help_command_cJ') ]
317 'c-K': [ command_cK, _('help_command_cK') ]
318 'g,h': [ command_gh, _('help_command_gh') ]
319 'g,H|g,\^': [ command_gH_g0, _('help_command_gH_g0') ]
320 'g,L|g,$': [ command_gL_g$, _('help_command_gL_g$') ]
321 'x': [ command_x, _('help_command_x') ]
322 'X': [ command_X, _('help_command_X') ]
323 'browse':
324 'f': [ command_f, _('help_command_f') ]
325 'F': [ command_F, _('help_command_F') ]
326 'H': [ command_H, _('help_command_H') ]
327 'L': [ command_L, _('help_command_L') ]
328 'misc':
329 # `.` is added to find command mapping to hack around Russian keyboard layout
330 '\.|/': [ command_find, _('help_command_find') ]
331 'a,\.|a,/': [ command_find_hl,_('help_command_find_hl') ]
332 'n': [ command_n, _('help_command_n') ]
333 'N': [ command_N, _('help_command_N') ]
334 # `>` is added to help command mapping to hack around Russian keyboard layout
335 # See key-utils.coffee for more info
336 '?|>': [ command_help, _('help_command_help') ]
337 'Esc': [ command_Esc, _('help_command_Esc') ]
338 ':': [ command_dev, _('help_command_dev') ]
339
340 # Merge groups and split command pipes into individual commands
341 commands = do (commandGroups) ->
342 newCommands = {}
343 for group, commandsList of commandGroups
344 for keys, command of commandsList
345 for key in keys.split('|')
346 newCommands[key] = command[0]
347
348 return newCommands
349
350 # Extract the help text from the commands preserving groups formation
351 commandsHelp = do (commandGroups) ->
352 helpStrings = {}
353 for group, commandsList of commandGroups
354 helpGroup = {}
355 for keys, command of commandsList
356 helpGroup[keys] = command[1]
357
358 helpStrings[group] = helpGroup
359 return helpStrings
360
361 # Called in hints mode. Will process the char, update and hide/show markers
362 hintCharHandler = (vim, keyStr, charCode) ->
363 if keyStr and charCode > 0
364 # Get char and escape it to avoid problems with String.search
365 key = utils.regexpEscape(keyStr)
366
367 # First do a pre match - count how many markers will match with the new character entered
368 if vim.markers.reduce(((v, marker) -> v or marker.willMatch(key)), false)
369 for marker in vim.markers
370 marker.matchHintChar(key)
371
372 if marker.isMatched()
373 vim.cb(marker)
374 hints.removeHints(vim.window.document)
375 vim.enterNormalMode()
376 break
377
378 exports.hintCharHandler = hintCharHandler
379 exports.commands = commands
380 exports.commandsHelp = commandsHelp
Imprint / Impressum