1 { classes: Cc, interfaces: Ci, utils: Cu } = Components
3 utils = require 'utils'
4 hints = require 'hints'
8 { getPref } = require 'prefs'
10 # Navigate to the address that is currently stored in the system clipboard
12 vim.window.location.assign utils.readFromClipboard(vim.window)
14 # Open new tab and navigate to the address that is currently stored in the system clipboard
16 if chromeWindow = utils.getRootWindow vim.window
17 if gBrowser = chromeWindow.gBrowser
18 gBrowser.selectedTab = gBrowser.addTab utils.readFromClipboard(vim.window)
20 # Open new tab and focus the address bar
22 if chromeWindow = utils.getRootWindow vim.window
23 if gBrowser = chromeWindow.gBrowser
24 # Get the default url for the new tab
25 newtab_url = Services.prefs.getCharPref 'browser.newtab.url'
26 gBrowser.selectedTab = gBrowser.addTab newtab_url
27 # Focus the address bar
28 chromeWindow.focusAndSelectUrlBar()
30 # Copy current URL to the clipboard
32 markers = hints.injectHints vim.window.document
34 # This callback will be called with the selected marker as argument
36 if url = marker.element.href
37 utils.writeToClipboard vim.window, url
39 vim.enterHintsMode(markers, cb)
41 # Copy current URL to the clipboard
43 utils.writeToClipboard vim.window, vim.window.location.toString()
45 # Reload the page, possibly from cache
47 vim.window.location.reload(false)
49 # Reload the page from the server
51 vim.window.location.reload(true)
53 # Reload the page, possibly from cache
55 if rootWindow = utils.getRootWindow vim.window
56 if tabs = rootWindow.gBrowser.tabContainer
57 for i in [0...tabs.itemCount]
58 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
59 window.location.reload(false)
61 # Reload the page from the server
63 if rootWindow = utils.getRootWindow vim.window
64 if tabs = rootWindow.gBrowser.tabContainer
65 for i in [0...tabs.itemCount]
66 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
67 window.location.reload(true)
69 # Scroll to the top of the page
71 vim.window.scrollTo(0, 0)
73 # Scroll to the bottom of the page
75 if document = vim.window.document
76 # Workaround the pages where body isn't the scrollable element.
77 # In this case we try to scroll 100k pixels
78 vim.window.scrollTo(0, Math.max(document.body.scrollHeight, 100000))
81 command_j_ce = (vim) ->
82 utils.smoothScroll vim.window, 0, (getPref 'scroll_step'), getPref 'scroll_time'
85 command_k_cy = (vim) ->
86 utils.smoothScroll vim.window, 0, -(getPref 'scroll_step'), getPref 'scroll_time'
90 utils.smoothScroll vim.window, -(getPref 'scroll_step'), 0, getPref 'scroll_time'
94 utils.smoothScroll vim.window, (getPref 'scroll_step'), 0, getPref 'scroll_time'
96 # Scroll down half a page
98 utils.smoothScroll vim.window, 0, vim.window.innerHeight / 2, getPref 'scroll_time'
100 # Scroll up half a page
102 utils.smoothScroll vim.window, 0, -vim.window.innerHeight / 2, getPref 'scroll_time'
104 # Scroll down full a page
105 command_cf = (vim) ->
106 step = (vim.window.innerHeight - (getPref 'scroll_step'))
107 utils.smoothScroll vim.window, 0, step, getPref 'scroll_time'
109 # Scroll up full a page
110 command_cb = (vim) ->
111 step = - (vim.window.innerHeight - (getPref 'scroll_step'))
112 utils.smoothScroll vim.window, 0, step, getPref 'scroll_time'
114 # Activate previous tab
115 command_J_gT = (vim) ->
116 if rootWindow = utils.getRootWindow vim.window
117 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true);
120 command_K_gt = (vim) ->
121 if rootWindow = utils.getRootWindow vim.window
122 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true);
124 # Go to the first tab
125 command_gH_g0 = (vim) ->
126 if rootWindow = utils.getRootWindow vim.window
127 rootWindow.gBrowser.tabContainer.selectedIndex = 0;
130 command_gL_g$ = (vim) ->
131 if rootWindow = utils.getRootWindow vim.window
132 itemCount = rootWindow.gBrowser.tabContainer.itemCount;
133 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1;
137 vim.window.history.back()
139 # Go forward in history
141 vim.window.history.forward()
145 if rootWindow = utils.getRootWindow vim.window
146 rootWindow.gBrowser.removeCurrentTab()
148 # Restore last closed tab
150 if rootWindow = utils.getRootWindow vim.window
151 ss = utils.getSessionStore()
152 if ss and ss.getClosedTabCount(rootWindow) > 0
153 ss.undoCloseTab rootWindow, 0
155 # Follow links with hint markers
157 if document = vim.window.document
158 markers = hints.injectHints document
159 if markers.length > 0
160 # This callback will be called with the selected marker as argument
162 marker.element.focus()
163 utils.simulateClick marker.element
165 vim.enterHintsMode markers, cb
167 # Follow links in a new Tab with hint markers
169 markers = hints.injectHints vim.window.document
170 if markers.length > 0
171 # This callback will be called with the selected marker as argument
173 marker.element.focus()
174 utils.simulateClick marker.element, { metaKey: true, ctrlKey: true }
176 vim.enterHintsMode markers, cb
178 # Move current tab to the left
179 command_cJ = (vim) ->
180 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
181 if tab = gBrowser.selectedTab
182 index = gBrowser.tabContainer.selectedIndex
183 total = gBrowser.tabContainer.itemCount
185 # `total` is added to deal with negative offset
186 gBrowser.moveTabTo tab, (total + index - 1) % total
188 # Move current tab to the right
189 command_cK = (vim) ->
190 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
191 if tab = gBrowser.selectedTab
192 index = gBrowser.tabContainer.selectedIndex
193 total = gBrowser.tabContainer.itemCount
195 gBrowser.moveTabTo tab, (index + 1) % total
197 # Display the Help Dialog
198 command_help = (vim) ->
199 help.injectHelp vim.window.document, commandsHelp
201 # Switch into find mode
202 command_find = (vim) ->
206 find.injectFind vim.window.document
208 # Search for the last pattern
210 find.find vim.window, vim.findStr, false
212 # Search for the last pattern backwards
214 find.find vim.window, vim.findStr, true
216 # Close the Help dialog and cancel the pending hint marker action
217 command_Esc = (vim) ->
218 # Blur active element if it's editable. Other elements
219 # aren't blurred - we don't want to interfere with
220 # the browser too much
221 activeElement = vim.window.document.activeElement
222 if utils.isElementEditable activeElement
226 find.removeFind vim.window.document
229 hints.removeHints vim.window.document
232 help.removeHelp vim.window.document
234 # Finally enter normal mode
235 vim.enterNormalMode()
239 'p': [ command_p, _('help_command_p') ]
240 'P': [ command_P, _('help_command_P') ]
241 'y,f': [ command_yf, _('help_command_yf') ]
242 'y,y': [ command_yy, _('help_command_yy') ]
243 'r': [ command_r, _('help_command_r') ]
244 'R': [ command_R, _('help_command_R') ]
245 'a,r': [ command_ar, _('help_command_ar') ]
246 'a,R': [ command_aR, _('help_command_aR') ]
248 'g,g': [ command_gg , _('help_command_gg') ]
249 'G': [ command_G, _('help_command_G') ]
250 'j|c-e': [ command_j_ce, _('help_command_j_ce') ]
251 'k|c-y': [ command_k_cy, _('help_command_k_cy') ]
252 'h': [ command_h, _('help_command_h') ]
253 'l': [ command_l , _('help_command_l') ]
254 # Can't use c-u/c-d because it's widely used for viewing sources
255 'd': [ command_d, _('help_command_d') ]
256 'u': [ command_u, _('help_command_u') ]
257 'c-f': [ command_cf, _('help_command_cf') ]
258 'c-b': [ command_cb, _('help_command_cb') ]
260 't': [ command_t, _('help_command_t') ]
261 'J|g,T': [ command_J_gT, _('help_command_J_gT') ]
262 'K|g,t': [ command_K_gt, _('help_command_K_gt') ]
263 'c-J': [ command_cJ, _('help_command_cJ') ]
264 'c-K': [ command_cK, _('help_command_cK') ]
265 "g,H|g,\^": [ command_gH_g0, _('help_command_gH_g0') ]
266 'g,L|g,$': [ command_gL_g$, _('help_command_gL_g$') ]
267 'x': [ command_x, _('help_command_x') ]
268 'X': [ command_X, _('help_command_X') ]
270 'f': [ command_f, _('help_command_f') ]
271 'F': [ command_F, _('help_command_F') ]
272 'H': [ command_H, _('help_command_H') ]
273 'L': [ command_L, _('help_command_L') ]
275 '/': [ command_find, _('help_command_find') ]
276 'n': [ command_n, _('help_command_n') ]
277 'N': [ command_N, _('help_command_N') ]
278 # `>` is added to help command mapping to hack around russian keyboard layout
279 # See key-utils.coffee for more info
280 '?|>': [ command_help, _('help_command_help') ]
281 'Esc': [ command_Esc, _('help_command_Esc') ]
283 # Merge groups and split command pipes into individual commands
284 commands = do (commandGroups) ->
286 for group, commandsList of commandGroups
287 for keys, command of commandsList
288 for key in keys.split '|'
289 newCommands[key] = command[0]
293 # Extract the help text from the commands preserving groups formation
294 commandsHelp = do (commandGroups) ->
296 for group, commandsList of commandGroups
298 for keys, command of commandsList
299 key = keys.replace(/,/g, '').replace('|', ', ')
300 helpGroup[key] = command[1]
302 helpStrings[group] = helpGroup
305 # Called in hints mode. Will process the char, update and hide/show markers
306 hintCharHandler = (vim, keyStr, charCode) ->
308 # Get char and escape it to avoid problems with String.search
309 key = utils.regexpEscape keyStr
311 # First do a pre match - count how many markers will match with the new character entered
312 if vim.markers.reduce ((v, marker) -> v + marker.willMatch key), 0
313 for marker in vim.markers
314 marker.matchHintChar key
316 if marker.isMatched()
318 hints.removeHints vim.window.document
319 vim.enterNormalMode()
322 # Called in find mode. Will process charCode, update find, or close the
323 findCharHandler = (vim, keyStr, charCode) ->
327 find.removeFind vim.window.document
328 vim.enterNormalMode()
330 if keyStr.match(/.*Return/)
331 return toNormalMode()
332 else if keyStr == 'Backspace'
333 # Delete last available character from the query string
334 if vim.findStr.length > 0
335 vim.findStr = vim.findStr.substr(0, vim.findStr.length - 1)
336 # Leave Find Mode the query string is already empty
338 return toNormalMode()
340 vim.findStr += String.fromCharCode(charCode)
344 # Update the interface string
345 find.setFindStr vim.window.document, vim.findStr
347 # Clear selection to avoid jumping between matching search results
348 vim.window.getSelection().removeAllRanges()
350 # Search only if the query string isn't emply.
351 # Otherwise it will pop up Find dialog
352 if vim.findStr.length > 0
353 find.find vim.window, vim.findStr, backwards
357 exports.hintCharHandler = hintCharHandler
358 exports.findCharHandler = findCharHandler
359 exports.commands = commands
360 exports.commandsHelp = commandsHelp