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 gBrowser.selectedTab = chromeWindow.gBrowser.addTab('about:blank')
30 if urlbar = chromeWindow.document.getElementById('urlbar')
33 # Copy current URL to the clipboard
35 vim.markers = injectHints vim.window.document
36 if vim.markers.length > 0
37 # This callback will be called with the selected marker as argument
39 if url = marker.element.href
40 utils.writeToClipboard vim.window, url
44 # Copy current URL to the clipboard
46 utils.writeToClipboard vim.window, vim.window.location.toString()
48 # Reload the page, possibly from cache
50 vim.window.location.reload(false)
52 # Reload the page from the server
54 vim.window.location.reload(true)
56 # Scroll to the top of the page
58 vim.window.scrollTo(0, 0)
60 # Scroll to the bottom of the page
62 if document = vim.window.document
63 vim.window.scrollTo(0, document.body.scrollHeight)
66 command_j_ce = (vim) ->
67 utils.smoothScroll vim.window, 0, (getPref 'scroll_step'), getPref 'scroll_time'
70 command_k_cy = (vim) ->
71 utils.smoothScroll vim.window, 0, -(getPref 'scroll_step'), getPref 'scroll_time'
75 vim.window.scrollBy -(getPref 'scroll_step'), 0
79 vim.window.scrollBy (getPref 'scroll_step'), 0
81 # Scroll down half a page
83 vim.window.scrollBy(0, vim.window.innerHeight / 2)
85 # Scroll up half a page
87 vim.window.scrollBy(0, -vim.window.innerHeight / 2)
89 # Scroll down full a page
91 vim.window.scrollBy(0, vim.window.innerHeight)
93 # Scroll up full a page
95 vim.window.scrollBy(0, -vim.window.innerHeight)
97 # Activate previous tab
98 command_J_gT = (vim) ->
99 if rootWindow = utils.getRootWindow vim.window
100 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true);
103 command_K_gt = (vim) ->
104 if rootWindow = utils.getRootWindow vim.window
105 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true);
107 # Go to the first tab
108 command_gH_g0 = (vim) ->
109 if rootWindow = utils.getRootWindow vim.window
110 rootWindow.gBrowser.tabContainer.selectedIndex = 0;
113 command_gL_g$ = (vim) ->
114 if rootWindow = utils.getRootWindow vim.window
115 itemCount = rootWindow.gBrowser.tabContainer.itemCount;
116 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1;
120 vim.window.history.back()
122 # Go forward in history
124 vim.window.history.forward()
128 if rootWindow = utils.getRootWindow vim.window
129 rootWindow.gBrowser.removeCurrentTab()
131 # Restore last closed tab
133 if rootWindow = utils.getRootWindow vim.window
134 ss = utils.getSessionStore()
135 if ss and ss.getClosedTabCount(rootWindow) > 0
136 ss.undoCloseTab rootWindow, 0
138 # Follow links with hint markers
140 if document = vim.window.document
141 vim.markers = injectHints document
142 if vim.markers.length > 0
143 # This callback will be called with the selected marker as argument
145 marker.element.focus()
146 utils.simulateClick marker.element
150 # Follow links in a new Tab with hint markers
152 vim.markers = injectHints vim.window.document
153 if vim.markers.length > 0
154 # This callback will be called with the selected marker as argument
156 marker.element.focus()
157 utils.simulateClick marker.element, { metaKey: true, ctrlKey: true }
161 # Move current tab to the left
162 command_cJ = (vim) ->
163 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
164 if tab = gBrowser.selectedTab
165 index = gBrowser.tabContainer.selectedIndex
166 total = gBrowser.tabContainer.itemCount
168 # `total` is added to deal with negative offset
169 console.log index, total, (total + index - 1) % total, 'left'
170 gBrowser.moveTabTo tab, (total + index - 1) % total
172 # Move current tab to the right
173 command_cK = (vim) ->
174 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
175 if tab = gBrowser.selectedTab
176 index = gBrowser.tabContainer.selectedIndex
177 total = gBrowser.tabContainer.itemCount
179 console.log index, total, (index + 1) % total, 'right'
180 gBrowser.moveTabTo tab, (index + 1) % total
182 # Display the Help Dialog
183 command_help = (vim) ->
184 showHelp vim.window.document, commandsHelp
186 # Close the Help dialog and cancel the pending hint marker action
187 command_Esc = (vim) ->
188 # Blur active element if it's editable. Other elements
189 # aren't blurred - we don't want to interfere with
190 # the browser too much
191 activeElement = vim.window.document.activeElement
192 if utils.isElementEditable activeElement
196 removeHints vim.window.document
198 hideHelp vim.window.document
199 # Finally enter normal mode
200 vim.enterNormalMode()
204 'p': [ command_p, _('help_command_p') ]
205 'P': [ command_P, _('help_command_P') ]
206 'y,f': [ command_yf, _('help_command_yf') ]
207 'y,y': [ command_yy, _('help_command_yy') ]
208 'r': [ command_r, _('help_command_r') ]
209 'R': [ command_R, _('help_command_R') ]
211 'g,g': [ command_gg , _('help_command_gg') ]
212 'G': [ command_G, _('help_command_G') ]
213 'j|c-e': [ command_j_ce, _('help_command_j_ce') ]
214 'k|c-y': [ command_k_cy, _('help_command_k_cy') ]
215 'h': [ command_h, _('help_command_h') ]
216 'l': [ command_l , _('help_command_l') ]
217 'd': [ command_d, _('help_command_d') ]
218 'u': [ command_u, _('help_command_u') ]
219 'c-f': [ command_cf, _('help_command_cf') ]
220 'c-b': [ command_cb, _('help_command_cb') ]
222 't': [ command_t, _('help_command_t') ]
223 'J|g,T': [ command_J_gT, _('help_command_J_gT') ]
224 'K|g,t': [ command_K_gt, _('help_command_K_gt') ]
225 'c-J': [ command_cJ, _('help_command_cJ') ]
226 'c-K': [ command_cK, _('help_command_cK') ]
227 'g,H|g,0': [ command_gH_g0, _('help_command_gH_g0') ]
228 'g,L|g,$': [ command_gL_g$, _('help_command_gL_g$') ]
229 'x': [ command_x, _('help_command_x') ]
230 'X': [ command_X, _('help_command_X') ]
232 'f': [ command_f, _('help_command_f') ]
233 'F': [ command_F, _('help_command_F') ]
234 'H': [ command_H, _('help_command_H') ]
235 'L': [ command_L, _('help_command_L') ]
237 '?': [ command_help, _('help_command_help') ]
238 'Esc': [ command_Esc, _('help_command_Esc') ]
240 # Merge groups and split command pipes into individual commands
241 commands = do (commandGroups) ->
243 for group, commandsList of commandGroups
244 for keys, command of commandsList
245 for key in keys.split '|'
246 newCommands[key] = command[0]
250 # Extract the help text from the commands preserving groups formation
251 commandsHelp = do (commandGroups) ->
253 for group, commandsList of commandGroups
255 for keys, command of commandsList
256 key = keys.replace(',', '').replace('|', ', ')
257 helpGroup[key] = command[1]
259 help[group] = helpGroup
262 # Called in hints mode. Will process the char, update and hide/show markers
263 hintCharHandler = (vim, char) ->
264 # First count how many markers will match with the new character entered
265 preMatch = vim.markers.reduce ((v, marker) -> v + marker.willMatch char), 0
267 # If prematch is greater than 0, then proceed with matching, else ignore the new char
269 for marker in vim.markers
270 marker.matchHintChar char
272 if marker.isMatched()
274 removeHints vim.window.document
275 vim.enterNormalMode()
278 exports.hintCharHandler = hintCharHandler
279 exports.commands = commands
280 exports.commandsHelp = commandsHelp