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 vim.window.scrollTo(0, vim.window.document.body.scrollHeight)
65 command_j_ce = (vim) ->
66 vim.window.scrollBy(0, getPref 'scroll_step')
69 command_k_cy = (vim) ->
70 vim.window.scrollBy(0, - getPref 'scroll_step')
74 vim.window.scrollBy(- getPref 'scroll_step', 0)
78 vim.window.scrollBy(getPref 'scroll_step', 0)
82 vim.window.scrollBy(0, vim.window.innerHeight)
86 vim.window.scrollBy(0, -vim.window.innerHeight)
88 # Activate previous tab
89 command_J_gT = (vim) ->
90 if rootWindow = utils.getRootWindow vim.window
91 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true);
94 command_K_gt = (vim) ->
95 if rootWindow = utils.getRootWindow vim.window
96 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true);
99 command_gH_g0 = (vim) ->
100 if rootWindow = utils.getRootWindow vim.window
101 rootWindow.gBrowser.tabContainer.selectedIndex = 0;
104 command_gL_g$ = (vim) ->
105 if rootWindow = utils.getRootWindow vim.window
106 itemCount = rootWindow.gBrowser.tabContainer.itemCount;
107 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1;
111 vim.window.history.back()
113 # Go forward in history
115 vim.window.history.forward()
119 if rootWindow = utils.getRootWindow vim.window
120 rootWindow.gBrowser.removeCurrentTab()
122 # Restore last closed tab
124 if rootWindow = utils.getRootWindow vim.window
125 ss = utils.getSessionStore()
126 if ss and ss.getClosedTabCount(rootWindow) > 0
127 ss.undoCloseTab rootWindow, 0
129 # Follow links with hint markers
131 vim.markers = injectHints vim.window.document
132 if vim.markers.length > 0
133 # This callback will be called with the selected marker as argument
135 marker.element.focus()
136 utils.simulateClick marker.element
140 # Follow links in a new Tab with hint markers
142 vim.markers = injectHints vim.window.document
143 if vim.markers.length > 0
144 # This callback will be called with the selected marker as argument
146 marker.element.focus()
147 utils.simulateClick marker.element, { metaKey: true, ctrlKey: true }
151 # Display the Help Dialog
152 command_help = (vim) ->
153 showHelp vim.window.document, commandsHelp
155 command_Esc = (vim) ->
156 # Blur active element if it's editable. Other elements
157 # aren't blurred - we don't want to interfere with
158 # the browser too much
159 activeElement = vim.window.document.activeElement
160 if utils.isElementEditable activeElement
164 removeHints vim.window.document
166 hideHelp vim.window.document
167 # Finally enter normal mode
168 vim.enterNormalMode()
172 'p': [ command_p, "Navigate to the address in the clipboard" ]
173 'P': [ command_P, "Open new tab and navigate to the address in the clipboard" ]
174 'y,f': [ command_yf, "Copy link url to the clipboard" ]
175 'y,y': [ command_yy, "Copy current page link to the clipboard" ]
176 'r': [ command_r, "Reload current page" ]
177 'R': [ command_R, "Reload current page and all the assets (js, css, etc.)" ]
179 'g,g': [ command_gg , "Scroll to the Top of the page" ]
180 'G': [ command_G, "Scroll to the Bottom of the page" ]
181 'j|c-e': [ command_j_ce, "Scroll Down" ]
182 'k|c-y': [ command_k_cy, "Scroll Up" ]
183 'h': [ command_h, "Scroll Left" ]
184 'l': [ command_l , "Scroll Right" ]
185 'd': [ command_d, "Scroll a Page Down" ]
186 'u': [ command_u, "Scroll a Page Up" ]
188 't': [ command_t, "Open New Blank tab" ]
189 'J|g,T': [ command_J_gT, "Go to the Previous tab" ]
190 'K|g,t': [ command_K_gt, "Go to the Next tab" ]
191 'g,H|g,0': [ command_gH_g0, "Go to the First tab" ]
192 'g,L|g,$': [ command_gL_g$, "Go to the Last tab" ]
193 'x': [ command_x, "Close current tab" ]
194 'X': [ command_X, "Restore last closed tab" ]
196 'f': [ command_f, "Follow a link on the current page" ]
197 'F': [ command_F, "Follow a link on the current page in a new tab" ]
198 'H': [ command_H, "Go Back in history" ]
199 'L': [ command_L, "Go Forward in history" ]
201 '?': [ command_help, "Show Help Dialog" ]
202 'Esc': [ command_Esc, "Close this dialog and cancel hint markers" ]
204 # Merge groups and split command pipes into individual commands
205 commands = do (commandGroups) ->
207 for group, commandsList of commandGroups
208 for keys, command of commandsList
209 for key in keys.split '|'
210 newCommands[key] = command[0]
214 # Extract the help text from the commands preserving groups formation
215 commandsHelp = do (commandGroups) ->
217 for group, commandsList of commandGroups
219 for keys, command of commandsList
220 key = keys.replace(',', '').replace('|', ', ')
221 helpGroup[key] = command[1]
223 help[group] = helpGroup
226 # Called in hints mode. Will process the char, update and hide/show markers
227 hintCharHandler = (vim, char) ->
228 for marker in vim.markers
229 marker.matchHintChar char
231 if marker.isMatched()
233 removeHints vim.window.document
234 vim.enterNormalMode()
237 exports.hintCharHandler = hintCharHandler
238 exports.commands = commands
239 exports.commandsHelp = commandsHelp