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