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