]> git.gir.st - VimFx.git/blob - extension/packages/commands.coffee
Closes #61. Added commands to stop loading current page and all pages
[VimFx.git] / extension / packages / commands.coffee
1 utils = require 'utils'
2 hints = require 'hints'
3 help = require 'help'
4 find = require 'find'
5
6 { _ } = require 'l10n'
7 { getPref
8 , setPref
9 , getFirefoxPref } = require 'prefs'
10
11 { console } = require 'console'
12
13 { classes: Cc, interfaces: Ci, utils: Cu } = Components
14
15 # Opens developer toolbar (Default shotrcut: Shift-F2)
16 command_dev = (vim) ->
17 if chromeWindow = utils.getRootWindow vim.window
18 chromeWindow.DeveloperToolbar.show(true)
19 chromeWindow.DeveloperToolbar.focus()
20
21 # Focus the Address Bar
22 command_o = (vim) ->
23 if chromeWindow = utils.getRootWindow(vim.window)
24 chromeWindow.focusAndSelectUrlBar()
25
26 # Navigate to the address that is currently stored in the system clipboard
27 command_p = (vim) ->
28 url = utils.readFromClipboard(vim.window)
29 postData = null
30 if not utils.isURL(url) and submission = utils.browserSearchSubmission(url)
31 url = submission.uri.spec
32 { postData } = submission
33
34 if chromeWindow = utils.getRootWindow(vim.window)
35 chromeWindow.gBrowser.loadURIWithFlags(url, null, null, null, postData)
36
37 # Open new tab and navigate to the address that is currently stored in the system clipboard
38 command_P = (vim) ->
39 url = utils.readFromClipboard(vim.window)
40 postData = null
41 if not utils.isURL(url) and submission = utils.browserSearchSubmission(url)
42 url = submission.uri.spec
43 { postData } = submission
44
45 if chromeWindow = utils.getRootWindow vim.window
46 chromeWindow.gBrowser.selectedTab = chromeWindow.gBrowser.addTab(url, null, null, postData, null, false)
47
48 # Open new tab and focus the address bar
49 command_t = (vim) ->
50 if chromeWindow = utils.getRootWindow(vim.window)
51 chromeWindow.BrowserOpenTab()
52
53 # Copy element URL to the clipboard
54 command_yf = (vim) ->
55 markers = hints.injectHints(vim.window.document)
56 if markers?.length > 0
57 cb = (marker) ->
58 if url = marker.element.href
59 marker.element.focus()
60 utils.writeToClipboard(vim.window, url)
61 else if utils.isTextInputElement(marker.element)
62 utils.writeToClipboard(vim.window, marker.element.value)
63
64 vim.enterHintsMode(markers, cb)
65
66 # Focus element
67 command_vf = (vim) ->
68 markers = hints.injectHints(vim.window.document)
69 if markers?.length > 0
70 vim.enterHintsMode(markers, (marker) -> marker.element.focus())
71
72 # Copy current URL to the clipboard
73 command_yy = (vim) ->
74 utils.writeToClipboard(vim.window, vim.window.location.toString())
75
76 # Reload the page, possibly from cache
77 command_r = (vim) ->
78 vim.window.location.reload(false)
79
80 # Reload the page from the server
81 command_R = (vim) ->
82 vim.window.location.reload(true)
83
84 # Reload the page, possibly from cache
85 command_ar = (vim) ->
86 if rootWindow = utils.getRootWindow(vim.window)
87 if tabs = rootWindow.gBrowser.tabContainer
88 for i in [0...tabs.itemCount]
89 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
90 window.location.reload(false)
91
92 # Reload the page from the server
93 command_aR = (vim) ->
94 if rootWindow = utils.getRootWindow(vim.window)
95 if tabs = rootWindow.gBrowser.tabContainer
96 for i in [0...tabs.itemCount]
97 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
98 window.location.reload(true)
99
100 command_s = (vim) ->
101 vim.window.stop()
102
103 command_as = (vim) ->
104 if rootWindow = utils.getRootWindow(vim.window)
105 if tabs = rootWindow.gBrowser.tabContainer
106 for i in [0...tabs.itemCount]
107 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
108 window.stop()
109
110 # Scroll to the top of the page
111 command_gg = (vim) ->
112 for i in [0...1000]
113 utils.simulateWheel(vim.window, 0, -1, utils.WHEEL_MODE_PAGE)
114
115 # Scroll to the bottom of the page
116 command_G = (vim) ->
117 for i in [0...1000]
118 utils.simulateWheel(vim.window, 0, 1, utils.WHEEL_MODE_PAGE)
119
120 # Scroll down a bit
121 command_j_ce = (vim) ->
122 utils.simulateWheel(vim.window, 0, getPref('scroll_step_lines'), utils.WHEEL_MODE_PIXEL)
123
124 # Scroll up a bit
125 command_k_cy = (vim) ->
126 utils.simulateWheel(vim.window, 0, -getPref('scroll_step_lines'), utils.WHEEL_MODE_PIXEL)
127
128 # Scroll left a bit
129 command_h = (vim) ->
130 utils.simulateWheel(vim.window, -getPref('scroll_step_lines'), 0, utils.WHEEL_MODE_LINE)
131
132 # Scroll right a bit
133 command_l = (vim) ->
134 utils.simulateWheel(vim.window, getPref('scroll_step_lines'), 0, utils.WHEEL_MODE_LINE)
135
136 # Scroll down half a page
137 command_d = (vim) ->
138 utils.simulateWheel(vim.window, 0, 0.5, utils.WHEEL_MODE_PAGE)
139
140 # Scroll up half a page
141 command_u = (vim) ->
142 utils.simulateWheel(vim.window, 0, -0.5, utils.WHEEL_MODE_PAGE)
143
144 # Scroll down full a page
145 command_cf = (vim) ->
146 utils.simulateWheel(vim.window, 0, 1, utils.WHEEL_MODE_PAGE)
147
148 # Scroll up full a page
149 command_cb = (vim) ->
150 utils.simulateWheel(vim.window, 0, -1, utils.WHEEL_MODE_PAGE)
151
152 # Activate previous tab
153 command_J_gT = (vim) ->
154 if rootWindow = utils.getRootWindow(vim.window)
155 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true)
156
157 # Activate next tab
158 command_K_gt = (vim) ->
159 if rootWindow = utils.getRootWindow(vim.window)
160 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true)
161
162 command_gh = (vim) ->
163 url = getFirefoxPref('browser.startup.homepage')
164 if chromeWindow = utils.getRootWindow(vim.window)
165 chromeWindow.gBrowser.loadURIWithFlags(url, null, null, null, null)
166
167 # Go to the first tab
168 command_gH_g0 = (vim) ->
169 if rootWindow = utils.getRootWindow(vim.window)
170 rootWindow.gBrowser.tabContainer.selectedIndex = 0
171
172 # Go to the last tab
173 command_gL_g$ = (vim) ->
174 if rootWindow = utils.getRootWindow(vim.window)
175 itemCount = rootWindow.gBrowser.tabContainer.itemCount
176 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1
177
178 # Go back in history
179 command_H = (vim) ->
180 vim.window.history.back()
181
182 # Go forward in history
183 command_L = (vim) ->
184 vim.window.history.forward()
185
186 # Close current tab
187 command_x = (vim) ->
188 if rootWindow = utils.getRootWindow(vim.window)
189 unless rootWindow.gBrowser.selectedTab.pinned
190 rootWindow.gBrowser.removeCurrentTab()
191
192 # Restore last closed tab
193 command_X = (vim) ->
194 if rootWindow = utils.getRootWindow(vim.window)
195 ss = utils.getSessionStore()
196 if ss and ss.getClosedTabCount(rootWindow) > 0
197 ss.undoCloseTab(rootWindow, 0)
198
199 # Follow links with hint markers
200 command_f = (vim) ->
201 if document = vim.window.document
202 markers = hints.injectHints(document)
203 if markers?.length > 0
204 # This callback will be called with the selected marker as argument
205 cb = (marker) ->
206 marker.element.focus()
207 utils.simulateClick(marker.element)
208
209 vim.enterHintsMode(markers, cb)
210
211 # Follow links in a new Tab with hint markers
212 command_F = (vim) ->
213 markers = hints.injectHints(vim.window.document)
214 if markers?.length > 0
215 # This callback will be called with the selected marker as argument
216 cb = (marker) ->
217 marker.element.focus()
218 utils.simulateClick(marker.element, { metaKey: true, ctrlKey: true })
219
220 vim.enterHintsMode(markers, cb)
221
222 # Move current tab to the left
223 command_cJ = (vim) ->
224 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
225 if tab = gBrowser.selectedTab
226 index = gBrowser.tabContainer.selectedIndex
227 total = gBrowser.tabContainer.itemCount
228
229 # `total` is added to deal with negative offset
230 gBrowser.moveTabTo(tab, (total + index - 1) % total)
231
232 # Move current tab to the right
233 command_cK = (vim) ->
234 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
235 if tab = gBrowser.selectedTab
236 index = gBrowser.tabContainer.selectedIndex
237 total = gBrowser.tabContainer.itemCount
238
239 gBrowser.moveTabTo(tab, (index + 1) % total)
240
241 # Display the Help Dialog
242 command_help = (vim) ->
243 help.injectHelp(vim.window.document, commandsHelp)
244
245 # Switch into find mode
246 command_find = (vim) ->
247 find.injectFind vim.window.document, (findStr, startFindRng) ->
248 # Reset region and find string if new find stirng has arrived
249 if vim.findStr != findStr
250 [vim.findStr, vim.findRng] = [findStr, startFindRng]
251 # Perform forward find and store found region
252 return vim.findRng = find.find(vim.window, vim.findStr, vim.findRng, find.DIRECTION_FORWARDS)
253
254 # Switch into find mode with highlighting
255 command_find_hl = (vim) ->
256 find.injectFind vim.window.document, (findStr) ->
257 # Reset region and find string if new find stirng has arrived
258 return find.highlight(vim.window, findStr)
259
260 # Search for the last pattern
261 command_n = (vim) ->
262 if vim.findStr.length > 0
263 vim.findRng = find.find(vim.window, vim.findStr, vim.findRng, find.DIRECTION_FORWARDS, true)
264
265 # Search for the last pattern backwards
266 command_N = (vim) ->
267 if vim.findStr.length > 0
268 vim.findRng = find.find(vim.window, vim.findStr, vim.findRng, find.DIRECTION_BACKWARDS, true)
269
270 # Close the Help dialog and cancel the pending hint marker action
271 command_Esc = (vim) ->
272 # Blur active element if it's editable. Other elements
273 # aren't blurred - we don't want to interfere with
274 # the browser too much
275 activeElement = vim.window.document.activeElement
276 if utils.isElementEditable(activeElement)
277 activeElement.blur()
278
279 #Remove Find input
280 find.removeFind(vim.window.document)
281
282 # Remove hints
283 hints.removeHints(vim.window.document)
284
285 # Hide help dialog
286 help.removeHelp(vim.window.document)
287
288 # Finally enter normal mode
289 vim.enterNormalMode()
290
291 if not getPref('leave_dt_on_esc')
292 if chromeWindow = utils.getRootWindow(vim.window)
293 chromeWindow.DeveloperToolbar.hide()
294
295 commandGroups =
296 'urls':
297 'o': [ command_o, _('help_command_o') ]
298 'p': [ command_p, _('help_command_p') ]
299 'P': [ command_P, _('help_command_P') ]
300 'y,f': [ command_yf, _('help_command_yf') ]
301 'v,f': [ command_vf, _('help_command_vf') ]
302 'y,y': [ command_yy, _('help_command_yy') ]
303 'r': [ command_r, _('help_command_r') ]
304 'R': [ command_R, _('help_command_R') ]
305 'a,r': [ command_ar, _('help_command_ar') ]
306 'a,R': [ command_aR, _('help_command_aR') ]
307 's': [ command_s, _('help_command_s') ]
308 'a,s': [ command_as, _('help_command_as') ]
309 'nav':
310 'g,g': [ command_gg , _('help_command_gg') ]
311 'G': [ command_G, _('help_command_G') ]
312 'j|c-e': [ command_j_ce, _('help_command_j_ce') ]
313 'k|c-y': [ command_k_cy, _('help_command_k_cy') ]
314 'h': [ command_h, _('help_command_h') ]
315 'l': [ command_l , _('help_command_l') ]
316 # Can't use c-u/c-d because c-u is widely used for viewing sources
317 'd': [ command_d, _('help_command_d') ]
318 'u': [ command_u, _('help_command_u') ]
319 'c-f': [ command_cf, _('help_command_cf') ]
320 'c-b': [ command_cb, _('help_command_cb') ]
321 'tabs':
322 't': [ command_t, _('help_command_t') ]
323 'J|g,T': [ command_J_gT, _('help_command_J_gT') ]
324 'K|g,t': [ command_K_gt, _('help_command_K_gt') ]
325 'c-J': [ command_cJ, _('help_command_cJ') ]
326 'c-K': [ command_cK, _('help_command_cK') ]
327 'g,h': [ command_gh, _('help_command_gh') ]
328 'g,H|g,\^': [ command_gH_g0, _('help_command_gH_g0') ]
329 'g,L|g,$': [ command_gL_g$, _('help_command_gL_g$') ]
330 'x': [ command_x, _('help_command_x') ]
331 'X': [ command_X, _('help_command_X') ]
332 'browse':
333 'f': [ command_f, _('help_command_f') ]
334 'F': [ command_F, _('help_command_F') ]
335 'H': [ command_H, _('help_command_H') ]
336 'L': [ command_L, _('help_command_L') ]
337 'misc':
338 # `.` is added to find command mapping to hack around Russian keyboard layout
339 '\.|/': [ command_find, _('help_command_find') ]
340 'a,\.|a,/': [ command_find_hl,_('help_command_find_hl') ]
341 'n': [ command_n, _('help_command_n') ]
342 'N': [ command_N, _('help_command_N') ]
343 # `>` is added to help command mapping to hack around Russian keyboard layout
344 # See key-utils.coffee for more info
345 '?|>': [ command_help, _('help_command_help') ]
346 'Esc': [ command_Esc, _('help_command_Esc') ]
347 ':': [ command_dev, _('help_command_dev') ]
348
349 # Merge groups and split command pipes into individual commands
350 commands = do (commandGroups) ->
351 newCommands = {}
352 for group, commandsList of commandGroups
353 for keys, command of commandsList
354 for key in keys.split('|')
355 newCommands[key] = command[0]
356
357 return newCommands
358
359 # Extract the help text from the commands preserving groups formation
360 commandsHelp = do (commandGroups) ->
361 helpStrings = {}
362 for group, commandsList of commandGroups
363 helpGroup = {}
364 for keys, command of commandsList
365 helpGroup[keys] = command[1]
366
367 helpStrings[group] = helpGroup
368 return helpStrings
369
370 # Called in hints mode. Will process the char, update and hide/show markers
371 hintCharHandler = (vim, keyStr, charCode) ->
372 if keyStr and charCode > 0
373 # Get char and escape it to avoid problems with String.search
374 key = utils.regexpEscape(keyStr)
375
376 # First do a pre match - count how many markers will match with the new character entered
377 if vim.markers.reduce(((v, marker) -> v or marker.willMatch(key)), false)
378 for marker in vim.markers
379 marker.matchHintChar(key)
380
381 if marker.isMatched()
382 vim.cb(marker)
383 hints.removeHints(vim.window.document)
384 vim.enterNormalMode()
385 break
386
387 exports.hintCharHandler = hintCharHandler
388 exports.commands = commands
389 exports.commandsHelp = commandsHelp
Imprint / Impressum