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()
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 vim.window.scrollBy(0, getPref 'scroll_step')
70 command_k_cy = (vim) ->
71 vim.window.scrollBy(0, - getPref 'scroll_step')
75 vim.window.scrollBy(- getPref 'scroll_step', 0)
79 vim.window.scrollBy(getPref 'scroll_step', 0)
83 vim.window.scrollBy(0, vim.window.innerHeight)
87 vim.window.scrollBy(0, -vim.window.innerHeight)
89 # Activate previous tab
90 command_J_gT = (vim) ->
91 if rootWindow = utils.getRootWindow vim.window
92 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true);
95 command_K_gt = (vim) ->
96 if rootWindow = utils.getRootWindow vim.window
97 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true);
100 command_gH_g0 = (vim) ->
101 if rootWindow = utils.getRootWindow vim.window
102 rootWindow.gBrowser.tabContainer.selectedIndex = 0;
105 command_gL_g$ = (vim) ->
106 if rootWindow = utils.getRootWindow vim.window
107 itemCount = rootWindow.gBrowser.tabContainer.itemCount;
108 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1;
112 vim.window.history.back()
114 # Go forward in history
116 vim.window.history.forward()
120 if rootWindow = utils.getRootWindow vim.window
121 rootWindow.gBrowser.removeCurrentTab()
123 # Restore last closed tab
125 if rootWindow = utils.getRootWindow vim.window
126 ss = utils.getSessionStore()
127 if ss and ss.getClosedTabCount(rootWindow) > 0
128 ss.undoCloseTab rootWindow, 0
130 # Follow links with hint markers
132 if document = vim.window.document
133 vim.markers = injectHints document
134 if vim.markers.length > 0
135 # This callback will be called with the selected marker as argument
137 marker.element.focus()
138 utils.simulateClick marker.element
142 # Follow links in a new Tab with hint markers
144 vim.markers = injectHints vim.window.document
145 if vim.markers.length > 0
146 # This callback will be called with the selected marker as argument
148 marker.element.focus()
149 utils.simulateClick marker.element, { metaKey: true, ctrlKey: true }
153 # Move current tab to the left
154 command_cJ = (vim) ->
155 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
156 if tab = gBrowser.selectedTab
157 index = gBrowser.tabContainer.selectedIndex
158 total = gBrowser.tabContainer.itemCount
160 # `total` is added to deal with negative offset
161 console.log index, total, (total + index - 1) % total, 'left'
162 gBrowser.moveTabTo tab, (total + index - 1) % total
164 # Move current tab to the right
165 command_cK = (vim) ->
166 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
167 if tab = gBrowser.selectedTab
168 index = gBrowser.tabContainer.selectedIndex
169 total = gBrowser.tabContainer.itemCount
171 console.log index, total, (index + 1) % total, 'right'
172 gBrowser.moveTabTo tab, (index + 1) % total
174 # Display the Help Dialog
175 command_help = (vim) ->
176 showHelp vim.window.document, commandsHelp
178 # Close the Help dialog and cancel the pending hint marker action
179 command_Esc = (vim) ->
180 # Blur active element if it's editable. Other elements
181 # aren't blurred - we don't want to interfere with
182 # the browser too much
183 activeElement = vim.window.document.activeElement
184 if utils.isElementEditable activeElement
188 removeHints vim.window.document
190 hideHelp vim.window.document
191 # Finally enter normal mode
192 vim.enterNormalMode()
196 'p': [ command_p, "Navigate to the address in the clipboard" ]
197 'P': [ command_P, "Open new tab and navigate to the address in the clipboard" ]
198 'y,f': [ command_yf, "Copy link url to the clipboard" ]
199 'y,y': [ command_yy, "Copy current page link to the clipboard" ]
200 'r': [ command_r, "Reload current page" ]
201 'R': [ command_R, "Reload current page and all the assets (js, css, etc.)" ]
203 'g,g': [ command_gg , "Scroll to the Top of the page" ]
204 'G': [ command_G, "Scroll to the Bottom of the page" ]
205 'j|c-e': [ command_j_ce, "Scroll Down" ]
206 'k|c-y': [ command_k_cy, "Scroll Up" ]
207 'h': [ command_h, "Scroll Left" ]
208 'l': [ command_l , "Scroll Right" ]
209 'd': [ command_d, "Scroll a Page Down" ]
210 'u': [ command_u, "Scroll a Page Up" ]
212 't': [ command_t, "Open New Blank tab" ]
213 'J|g,T': [ command_J_gT, "Go to the Previous tab" ]
214 'K|g,t': [ command_K_gt, "Go to the Next tab" ]
215 'c-J': [ command_cJ, "Move current tab to the left" ]
216 'c-K': [ command_cK, "Move current tab to the right" ]
217 'g,H|g,0': [ command_gH_g0, "Go to the First tab" ]
218 'g,L|g,$': [ command_gL_g$, "Go to the Last tab" ]
219 'x': [ command_x, "Close current tab" ]
220 'X': [ command_X, "Restore last closed tab" ]
222 'f': [ command_f, "Follow a link on the current page" ]
223 'F': [ command_F, "Follow a link on the current page in a new tab" ]
224 'H': [ command_H, "Go Back in history" ]
225 'L': [ command_L, "Go Forward in history" ]
227 '?': [ command_help, "Show Help Dialog" ]
228 'Esc': [ command_Esc, "Close this dialog and cancel hint markers" ]
230 # Merge groups and split command pipes into individual commands
231 commands = do (commandGroups) ->
233 for group, commandsList of commandGroups
234 for keys, command of commandsList
235 for key in keys.split '|'
236 newCommands[key] = command[0]
240 # Extract the help text from the commands preserving groups formation
241 commandsHelp = do (commandGroups) ->
243 for group, commandsList of commandGroups
245 for keys, command of commandsList
246 key = keys.replace(',', '').replace('|', ', ')
247 helpGroup[key] = command[1]
249 help[group] = helpGroup
252 # Called in hints mode. Will process the char, update and hide/show markers
253 hintCharHandler = (vim, char) ->
254 # First count how many markers will match with the new character entered
255 preMatch = vim.markers.reduce ((v, marker) -> v + marker.willMatch char), 0
257 # If prematch is greater than 0, then proceed with matching, else ignore the new char
259 for marker in vim.markers
260 marker.matchHintChar char
262 if marker.isMatched()
264 removeHints vim.window.document
265 vim.enterNormalMode()
268 exports.hintCharHandler = hintCharHandler
269 exports.commands = commands
270 exports.commandsHelp = commandsHelp