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 # Display the Help Dialog
154 command_help = (vim) ->
155 showHelp vim.window.document, commandsHelp
157 command_Esc = (vim) ->
158 # Blur active element if it's editable. Other elements
159 # aren't blurred - we don't want to interfere with
160 # the browser too much
161 activeElement = vim.window.document.activeElement
162 if utils.isElementEditable activeElement
166 removeHints vim.window.document
168 hideHelp vim.window.document
169 # Finally enter normal mode
170 vim.enterNormalMode()
174 'p': [ command_p, "Navigate to the address in the clipboard" ]
175 'P': [ command_P, "Open new tab and navigate to the address in the clipboard" ]
176 'y,f': [ command_yf, "Copy link url to the clipboard" ]
177 'y,y': [ command_yy, "Copy current page link to the clipboard" ]
178 'r': [ command_r, "Reload current page" ]
179 'R': [ command_R, "Reload current page and all the assets (js, css, etc.)" ]
181 'g,g': [ command_gg , "Scroll to the Top of the page" ]
182 'G': [ command_G, "Scroll to the Bottom of the page" ]
183 'j|c-e': [ command_j_ce, "Scroll Down" ]
184 'k|c-y': [ command_k_cy, "Scroll Up" ]
185 'h': [ command_h, "Scroll Left" ]
186 'l': [ command_l , "Scroll Right" ]
187 'd': [ command_d, "Scroll a Page Down" ]
188 'u': [ command_u, "Scroll a Page Up" ]
190 't': [ command_t, "Open New Blank tab" ]
191 'J|g,T': [ command_J_gT, "Go to the Previous tab" ]
192 'K|g,t': [ command_K_gt, "Go to the Next tab" ]
193 'g,H|g,0': [ command_gH_g0, "Go to the First tab" ]
194 'g,L|g,$': [ command_gL_g$, "Go to the Last tab" ]
195 'x': [ command_x, "Close current tab" ]
196 'X': [ command_X, "Restore last closed tab" ]
198 'f': [ command_f, "Follow a link on the current page" ]
199 'F': [ command_F, "Follow a link on the current page in a new tab" ]
200 'H': [ command_H, "Go Back in history" ]
201 'L': [ command_L, "Go Forward in history" ]
203 '?': [ command_help, "Show Help Dialog" ]
204 'Esc': [ command_Esc, "Close this dialog and cancel hint markers" ]
206 # Merge groups and split command pipes into individual commands
207 commands = do (commandGroups) ->
209 for group, commandsList of commandGroups
210 for keys, command of commandsList
211 for key in keys.split '|'
212 newCommands[key] = command[0]
216 # Extract the help text from the commands preserving groups formation
217 commandsHelp = do (commandGroups) ->
219 for group, commandsList of commandGroups
221 for keys, command of commandsList
222 key = keys.replace(',', '').replace('|', ', ')
223 helpGroup[key] = command[1]
225 help[group] = helpGroup
228 # Called in hints mode. Will process the char, update and hide/show markers
229 hintCharHandler = (vim, char) ->
230 for marker in vim.markers
231 marker.matchHintChar char
233 if marker.isMatched()
235 removeHints vim.window.document
236 vim.enterNormalMode()
239 exports.hintCharHandler = hintCharHandler
240 exports.commands = commands
241 exports.commandsHelp = commandsHelp