1 { classes: Cc, interfaces: Ci, utils: Cu } = Components
3 utils = require 'utils'
4 { getPref } = require 'prefs'
15 # Navigate to the address that is currently stored in the system clipboard
17 vim.window.location.assign utils.readFromClipboard(vim.window)
19 # Open new tab and navigate to the address that is currently stored in the system clipboard
21 if chromeWindow = utils.getRootWindow vim.window
22 if gBrowser = chromeWindow.gBrowser
23 gBrowser.selectedTab = gBrowser.addTab utils.readFromClipboard(vim.window)
25 # Open new tab and focus the address bar
27 if chromeWindow = utils.getRootWindow vim.window
28 if gBrowser = chromeWindow.gBrowser
29 # Get the default url for the new tab
30 newtab_url = Services.prefs.getCharPref 'browser.newtab.url'
31 gBrowser.selectedTab = gBrowser.addTab newtab_url
32 # Focus the address bar
33 chromeWindow.focusAndSelectUrlBar()
35 # Copy current URL to the clipboard
37 vim.markers = injectHints vim.window.document
38 if vim.markers.length > 0
39 # This callback will be called with the selected marker as argument
41 if url = marker.element.href
42 utils.writeToClipboard vim.window, url
46 # Copy current URL to the clipboard
48 utils.writeToClipboard vim.window, vim.window.location.toString()
50 # Reload the page, possibly from cache
52 vim.window.location.reload(false)
54 # Reload the page from the server
56 vim.window.location.reload(true)
58 # Reload the page, possibly from cache
60 if rootWindow = utils.getRootWindow vim.window
61 if tabs = rootWindow.gBrowser.tabContainer
62 for i in [0...tabs.itemCount]
63 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
64 window.location.reload(false)
66 # Reload the page from the server
68 if rootWindow = utils.getRootWindow vim.window
69 if tabs = rootWindow.gBrowser.tabContainer
70 for i in [0...tabs.itemCount]
71 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
72 window.location.reload(true)
74 # Scroll to the top of the page
76 vim.window.scrollTo(0, 0)
78 # Scroll to the bottom of the page
80 if document = vim.window.document
81 # Workaround the pages where body isn't the scrollable element.
82 # In this case we try to scroll 100k pixels
83 vim.window.scrollTo(0, Math.max(document.body.scrollHeight, 100000))
86 command_j_ce = (vim) ->
87 utils.smoothScroll vim.window, 0, (getPref 'scroll_step'), getPref 'scroll_time'
90 command_k_cy = (vim) ->
91 utils.smoothScroll vim.window, 0, -(getPref 'scroll_step'), getPref 'scroll_time'
95 utils.smoothScroll vim.window, -(getPref 'scroll_step'), 0, getPref 'scroll_time'
99 utils.smoothScroll vim.window, (getPref 'scroll_step'), 0, getPref 'scroll_time'
101 # Scroll down half a page
103 utils.smoothScroll vim.window, 0, vim.window.innerHeight / 2, getPref 'scroll_time'
105 # Scroll up half a page
107 utils.smoothScroll vim.window, 0, -vim.window.innerHeight / 2, getPref 'scroll_time'
109 # Scroll down full a page
110 command_cf = (vim) ->
111 vim.window.scrollByPages(1)
113 # Scroll up full a page
114 command_cb = (vim) ->
115 vim.window.scrollByPages(-1)
117 # Activate previous tab
118 command_J_gT = (vim) ->
119 if rootWindow = utils.getRootWindow vim.window
120 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true);
123 command_K_gt = (vim) ->
124 if rootWindow = utils.getRootWindow vim.window
125 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true);
127 # Go to the first tab
128 command_gH_g0 = (vim) ->
129 if rootWindow = utils.getRootWindow vim.window
130 rootWindow.gBrowser.tabContainer.selectedIndex = 0;
133 command_gL_g$ = (vim) ->
134 if rootWindow = utils.getRootWindow vim.window
135 itemCount = rootWindow.gBrowser.tabContainer.itemCount;
136 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1;
140 vim.window.history.back()
142 # Go forward in history
144 vim.window.history.forward()
148 if rootWindow = utils.getRootWindow vim.window
149 rootWindow.gBrowser.removeCurrentTab()
151 # Restore last closed tab
153 if rootWindow = utils.getRootWindow vim.window
154 ss = utils.getSessionStore()
155 if ss and ss.getClosedTabCount(rootWindow) > 0
156 ss.undoCloseTab rootWindow, 0
158 # Follow links with hint markers
160 if document = vim.window.document
161 vim.markers = injectHints document
162 if vim.markers.length > 0
163 # This callback will be called with the selected marker as argument
165 marker.element.focus()
166 utils.simulateClick marker.element
170 # Follow links in a new Tab with hint markers
172 vim.markers = injectHints vim.window.document
173 if vim.markers.length > 0
174 # This callback will be called with the selected marker as argument
176 marker.element.focus()
177 utils.simulateClick marker.element, { metaKey: true, ctrlKey: true }
181 # Move current tab to the left
182 command_cJ = (vim) ->
183 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
184 if tab = gBrowser.selectedTab
185 index = gBrowser.tabContainer.selectedIndex
186 total = gBrowser.tabContainer.itemCount
188 # `total` is added to deal with negative offset
189 gBrowser.moveTabTo tab, (total + index - 1) % total
191 # Move current tab to the right
192 command_cK = (vim) ->
193 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
194 if tab = gBrowser.selectedTab
195 index = gBrowser.tabContainer.selectedIndex
196 total = gBrowser.tabContainer.itemCount
198 gBrowser.moveTabTo tab, (index + 1) % total
200 # Display the Help Dialog
201 command_help = (vim) ->
202 showHelp vim.window.document, commandsHelp
204 # Close the Help dialog and cancel the pending hint marker action
205 command_Esc = (vim) ->
206 # Blur active element if it's editable. Other elements
207 # aren't blurred - we don't want to interfere with
208 # the browser too much
209 activeElement = vim.window.document.activeElement
210 if utils.isElementEditable activeElement
214 removeHints vim.window.document
216 hideHelp vim.window.document
217 # Finally enter normal mode
218 vim.enterNormalMode()
222 'p': [ command_p, _('help_command_p') ]
223 'P': [ command_P, _('help_command_P') ]
224 'y,f': [ command_yf, _('help_command_yf') ]
225 'y,y': [ command_yy, _('help_command_yy') ]
226 'r': [ command_r, _('help_command_r') ]
227 'R': [ command_R, _('help_command_R') ]
228 'a,r': [ command_ar, _('help_command_ar') ]
229 'a,R': [ command_aR, _('help_command_aR') ]
231 'g,g': [ command_gg , _('help_command_gg') ]
232 'G': [ command_G, _('help_command_G') ]
233 'j|c-e': [ command_j_ce, _('help_command_j_ce') ]
234 'k|c-y': [ command_k_cy, _('help_command_k_cy') ]
235 'h': [ command_h, _('help_command_h') ]
236 'l': [ command_l , _('help_command_l') ]
238 # Can't use c-u/c-d because it's generally used for viewing sources
239 'd': [ command_d, _('help_command_d') ]
240 'u': [ command_u, _('help_command_u') ]
242 # Can't use c-f because it's generally used for viewing sources
243 #'c-f': [ command_cf, _('help_command_cf') ]
244 #'c-b': [ command_cb, _('help_command_cb') ]
246 't': [ command_t, _('help_command_t') ]
247 'J|g,T': [ command_J_gT, _('help_command_J_gT') ]
248 'K|g,t': [ command_K_gt, _('help_command_K_gt') ]
249 'c-J': [ command_cJ, _('help_command_cJ') ]
250 'c-K': [ command_cK, _('help_command_cK') ]
251 'g,H|g,0': [ command_gH_g0, _('help_command_gH_g0') ]
252 'g,L|g,$': [ command_gL_g$, _('help_command_gL_g$') ]
253 'x': [ command_x, _('help_command_x') ]
254 'X': [ command_X, _('help_command_X') ]
256 'f': [ command_f, _('help_command_f') ]
257 'F': [ command_F, _('help_command_F') ]
258 'H': [ command_H, _('help_command_H') ]
259 'L': [ command_L, _('help_command_L') ]
261 # `>` is added to help command mapping to hack around russian keyboard layout
262 # See key-utils.coffee for more info
263 '?|>': [ command_help, _('help_command_help') ]
264 'Esc': [ command_Esc, _('help_command_Esc') ]
266 # Merge groups and split command pipes into individual commands
267 commands = do (commandGroups) ->
269 for group, commandsList of commandGroups
270 for keys, command of commandsList
271 for key in keys.split '|'
272 newCommands[key] = command[0]
276 # Extract the help text from the commands preserving groups formation
277 commandsHelp = do (commandGroups) ->
279 for group, commandsList of commandGroups
281 for keys, command of commandsList
282 key = keys.replace(/,/g, '').replace('|', ', ')
283 helpGroup[key] = command[1]
285 help[group] = helpGroup
288 # Called in hints mode. Will process the char, update and hide/show markers
289 hintCharHandler = (vim, char) ->
290 # First count how many markers will match with the new character entered
291 preMatch = vim.markers.reduce ((v, marker) -> v + marker.willMatch char), 0
293 # If prematch is greater than 0, then proceed with matching, else ignore the new char
295 for marker in vim.markers
296 marker.matchHintChar char
298 if marker.isMatched()
300 removeHints vim.window.document
301 vim.enterNormalMode()
304 exports.hintCharHandler = hintCharHandler
305 exports.commands = commands
306 exports.commandsHelp = commandsHelp