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()
31 # Copy current URL to the clipboard
33 vim.markers = injectHints vim.window.document
34 if vim.markers.length > 0
35 # This callback will be called with the selected marker as argument
37 if url = marker.element.href
38 utils.writeToClipboard vim.window, url
42 # Copy current URL to the clipboard
44 utils.writeToClipboard vim.window, vim.window.location.toString()
46 # Reload the page, possibly from cache
48 vim.window.location.reload(false)
50 # Reload the page from the server
52 vim.window.location.reload(true)
54 # Scroll to the top of the page
56 vim.window.scrollTo(0, 0)
58 # Scroll to the bottom of the page
60 vim.window.scrollTo(0, vim.window.document.body.scrollHeight)
63 command_j_ce = (vim) ->
64 vim.window.scrollBy(0, getPref 'scroll_step')
67 command_k_cy = (vim) ->
68 vim.window.scrollBy(0, - getPref 'scroll_step')
72 vim.window.scrollBy(- getPref 'scroll_step', 0)
76 vim.window.scrollBy(getPref 'scroll_step', 0)
79 command_d_cd = (vim) ->
80 vim.window.scrollBy(0, vim.window.innerHeight)
83 command_u_cu = (vim) ->
84 vim.window.scrollBy(0, -vim.window.innerHeight)
86 # Activate previous tab
87 command_J_gT = (vim) ->
88 if rootWindow = utils.getRootWindow vim.window
89 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true);
92 command_K_gt = (vim) ->
93 if rootWindow = utils.getRootWindow vim.window
94 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true);
97 command_gH_g0 = (vim) ->
98 if rootWindow = utils.getRootWindow vim.window
99 rootWindow.gBrowser.tabContainer.selectedIndex = 0;
102 command_gL_g$ = (vim) ->
103 if rootWindow = utils.getRootWindow vim.window
104 itemCount = rootWindow.gBrowser.tabContainer.itemCount;
105 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1;
109 vim.window.history.back()
111 # Go forward in history
113 vim.window.history.forward()
117 if rootWindow = utils.getRootWindow vim.window
118 rootWindow.gBrowser.removeCurrentTab()
120 # Restore last closed tab
122 if rootWindow = utils.getRootWindow vim.window
123 ss = utils.getSessionStore()
124 if ss and ss.getClosedTabCount(rootWindow) > 0
125 ss.undoCloseTab rootWindow, 0
127 # Follow links with hint markers
129 vim.markers = injectHints vim.window.document
130 if vim.markers.length > 0
131 # This callback will be called with the selected marker as argument
133 marker.element.focus()
134 utils.simulateClick marker.element
138 # Follow links in a new Tab with hint markers
140 vim.markers = injectHints vim.window.document
141 if vim.markers.length > 0
142 # This callback will be called with the selected marker as argument
144 marker.element.focus()
145 utils.simulateClick marker.element, { metaKey: true, ctrlKey: true }
149 # Display the Help Dialog
150 command_help = (vim) ->
151 showHelp vim.window.document, commandsHelp
153 command_Esc = (vim) ->
154 # Blur active element if it's editable. Other elements
155 # aren't blurred - we don't want to interfere with
156 # the browser too much
157 activeElement = vim.window.document.activeElement
158 if utils.isElementEditable activeElement
162 removeHints vim.window.document
164 hideHelp vim.window.document
165 # Finally enter normal mode
166 vim.enterNormalMode()
170 'p': [ command_p, "Navigate to the address in the clipboard" ]
171 'P': [ command_P, "Open new tab and navigate to the address in the clipboard" ]
172 'y,f': [ command_yf, "Copy link url to the clipboard" ]
173 'y,y': [ command_yy, "Copy current page link to the clipboard" ]
174 'r': [ command_r, "Reload current page" ]
175 'R': [ command_R, "Reload current page and all the assets (js, css, etc.)" ]
177 'g,g': [ command_gg , "Scroll to the Top of the page" ]
178 'G': [ command_G, "Scroll to the Bottom of the page" ]
179 'j|c-e': [ command_j_ce, "Scroll Left" ]
180 'k|c-y': [ command_k_cy, "Scroll Right" ]
181 'h': [ command_h, "Scroll Down" ]
182 'l': [ command_l , "Scroll Up" ]
183 'd|c-d': [ command_d_cd, "Scroll a Page Down" ]
184 'u|c-u': [ command_u_cu, "Scroll a Page Up" ]
186 't': [ command_t, "Open New Blank tab" ]
187 'J|g,T': [ command_J_gT, "Go to the Previous tab" ]
188 'K|g,t': [ command_K_gt, "Go to the Next tab" ]
189 'g,H|g,0': [ command_gH_g0, "Go to the First tab" ]
190 'g,L|g,$': [ command_gL_g$, "Go to the Last tab" ]
191 'x': [ command_x, "Close current tab" ]
192 'X': [ command_X, "Restore last closed tab" ]
194 'f': [ command_f, "Follow a link on the current page" ]
195 'F': [ command_F, "Follow a link on the current page in a new tab" ]
196 'H': [ command_H, "Go Back in history" ]
197 'L': [ command_L, "Go Forward in history" ]
199 '?': [ command_help, "Show Help Dialog" ]
200 'Esc': [ command_Esc, "Close this dialog and cancel hint markers" ]
202 # Merge groups and split command pipes into individual commands
203 commands = do (commandGroups) ->
205 for group, commandsList of commandGroups
206 for keys, command of commandsList
207 for key in keys.split '|'
208 newCommands[key] = command[0]
212 # Extract the help text from the commands preserving groups formation
213 commandsHelp = do (commandGroups) ->
215 for group, commandsList of commandGroups
217 for keys, command of commandsList
218 key = keys.replace(',', '').replace('|', ', ')
219 helpGroup[key] = command[1]
221 help[group] = helpGroup
224 # Called in hints mode. Will process the char, update and hide/show markers
225 hintCharHandler = (vim, char) ->
226 for marker in vim.markers
227 marker.matchHintChar char
229 if marker.isMatched()
231 removeHints vim.window.document
232 vim.enterNormalMode()
235 exports.hintCharHandler = hintCharHandler
236 exports.commands = commands
237 exports.commandsHelp = commandsHelp