1 { classes: Cc, interfaces: Ci, utils: Cu } = Components
3 utils = require 'utils'
4 hints = require 'hints'
10 , getFirefoxPref } = require 'prefs'
12 # Focus the Address Bar
14 if chromeWindow = utils.getRootWindow vim.window
15 chromeWindow.focusAndSelectUrlBar()
17 # Navigate to the address that is currently stored in the system clipboard
19 url = utils.readFromClipboard(vim.window)
21 if not utils.isURL(url) and submission = utils.browserSearchSubmission url
22 url = submission.uri.spec
23 postData = submission.postData
25 if chromeWindow = utils.getRootWindow vim.window
26 chromeWindow.gBrowser.loadURIWithFlags url, null, null, null, postData
28 # Open new tab and navigate to the address that is currently stored in the system clipboard
30 url = utils.readFromClipboard(vim.window)
32 if not utils.isURL(url) and submission = utils.browserSearchSubmission url
33 url = submission.uri.spec
34 postData = submission.postData
36 if chromeWindow = utils.getRootWindow vim.window
37 chromeWindow.gBrowser.selectedTab = chromeWindow.gBrowser.addTab url, null, null, postData, null, false
39 # Open new tab and focus the address bar
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()
49 # Copy current URL to the clipboard
51 markers = hints.injectHints vim.window.document
52 if markers?.length > 0
53 # This callback will be called with the selected marker as argument
55 if url = marker.element.href
56 utils.writeToClipboard vim.window, url
58 vim.enterHintsMode(markers, cb)
60 # Copy current URL to the clipboard
62 utils.writeToClipboard vim.window, vim.window.location.toString()
64 # Reload the page, possibly from cache
66 vim.window.location.reload(false)
68 # Reload the page from the server
70 vim.window.location.reload(true)
72 # Reload the page, possibly from cache
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)
80 # Reload the page from the server
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)
88 # Scroll to the top of the page
90 vim.window.scrollTo(0, 0)
92 # Scroll to the bottom of the page
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))
100 command_j_ce = (vim) ->
101 utils.smoothScroll vim.window, 0, (getPref 'scroll_step'), getPref 'scroll_time'
104 command_k_cy = (vim) ->
105 utils.smoothScroll vim.window, 0, -(getPref 'scroll_step'), getPref 'scroll_time'
109 utils.smoothScroll vim.window, -(getPref 'scroll_step'), 0, getPref 'scroll_time'
113 utils.smoothScroll vim.window, (getPref 'scroll_step'), 0, getPref 'scroll_time'
115 # Scroll down half a page
117 utils.smoothScroll vim.window, 0, vim.window.innerHeight / 2, getPref 'scroll_time'
119 # Scroll up half a page
121 utils.smoothScroll vim.window, 0, -vim.window.innerHeight / 2, getPref 'scroll_time'
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'
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'
133 # Activate previous tab
134 command_J_gT = (vim) ->
135 if rootWindow = utils.getRootWindow vim.window
136 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true);
139 command_K_gt = (vim) ->
140 if rootWindow = utils.getRootWindow vim.window
141 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true);
143 command_gh = (vim) ->
144 homepage_url = getFirefoxPref 'browser.startup.homepage'
145 vim.window.location.assign homepage_url
147 # Go to the first tab
148 command_gH_g0 = (vim) ->
149 if rootWindow = utils.getRootWindow vim.window
150 rootWindow.gBrowser.tabContainer.selectedIndex = 0;
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;
160 vim.window.history.back()
162 # Go forward in history
164 vim.window.history.forward()
168 if rootWindow = utils.getRootWindow vim.window
169 rootWindow.gBrowser.removeCurrentTab()
171 # Restore last closed tab
173 if rootWindow = utils.getRootWindow vim.window
174 ss = utils.getSessionStore()
175 if ss and ss.getClosedTabCount(rootWindow) > 0
176 ss.undoCloseTab rootWindow, 0
178 # Follow links with hint markers
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
185 marker.element.focus()
186 utils.simulateClick marker.element
188 vim.enterHintsMode markers, cb
190 # Follow links in a new Tab with hint markers
192 markers = hints.injectHints vim.window.document
193 if markers?.length > 0
194 # This callback will be called with the selected marker as argument
196 marker.element.focus()
197 utils.simulateClick marker.element, { metaKey: true, ctrlKey: true }
199 vim.enterHintsMode markers, cb
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
208 # `total` is added to deal with negative offset
209 gBrowser.moveTabTo tab, (total + index - 1) % total
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
218 gBrowser.moveTabTo tab, (index + 1) % total
220 # Display the Help Dialog
221 command_help = (vim) ->
222 help.injectHelp vim.window.document, commandsHelp
224 # Switch into find mode
225 command_find = (vim) ->
229 find.injectFind vim.window.document
231 # Search for the last pattern
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)"
237 # Search for the last pattern backwards
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)"
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
253 find.removeFind vim.window.document
256 hints.removeHints vim.window.document
259 help.removeHelp vim.window.document
261 # Finally enter normal mode
262 vim.enterNormalMode()
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') ]
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') ]
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') ]
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') ]
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') ]
313 # Merge groups and split command pipes into individual commands
314 commands = do (commandGroups) ->
316 for group, commandsList of commandGroups
317 for keys, command of commandsList
318 for key in keys.split '|'
319 newCommands[key] = command[0]
323 # Extract the help text from the commands preserving groups formation
324 commandsHelp = do (commandGroups) ->
326 for group, commandsList of commandGroups
328 for keys, command of commandsList
329 helpGroup[keys] = command[1]
331 helpStrings[group] = helpGroup
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
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
345 if marker.isMatched()
347 hints.removeHints vim.window.document
348 vim.enterNormalMode()
351 # Called in find mode. Will process charCode, update find, or close the
352 findCharHandler = (vim, keyStr, charCode) ->
356 find.removeFind vim.window.document
357 vim.enterNormalMode()
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
367 return toNormalMode()
369 vim.findStr += String.fromCharCode(charCode)
373 # Update the interface string
375 # Clear selection to avoid jumping between matching search results
376 vim.window.getSelection().removeAllRanges()
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
383 find.setFindStr vim.window.document, "#{ vim.findStr } (Not Found)"
386 exports.hintCharHandler = hintCharHandler
387 exports.findCharHandler = findCharHandler
388 exports.commands = commands
389 exports.commandsHelp = commandsHelp