]> git.gir.st - VimFx.git/blob - extension/packages/commands.coffee
Merge branch 'develop' into huffman
[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 { classes: Cc, interfaces: Ci, utils: Cu } = Components
12
13 # Opens developer toolbar (Default shotrcut: Shift-F2)
14 command_dev = (vim) ->
15 if chromeWindow = utils.getRootWindow vim.window
16 chromeWindow.DeveloperToolbar.show(true)
17 chromeWindow.DeveloperToolbar.focus()
18
19 # Focus the Address Bar
20 command_focus = (vim) ->
21 if chromeWindow = utils.getRootWindow(vim.window)
22 chromeWindow.focusAndSelectUrlBar()
23
24 # Navigate to the address that is currently stored in the system clipboard
25 command_paste = (vim) ->
26 url = utils.readFromClipboard(vim.window)
27 postData = null
28 if not utils.isURL(url) and submission = utils.browserSearchSubmission(url)
29 url = submission.uri.spec
30 { postData } = submission
31
32 if chromeWindow = utils.getRootWindow(vim.window)
33 chromeWindow.gBrowser.loadURIWithFlags(url, null, null, null, postData)
34
35 # Open new tab and navigate to the address that is currently stored in the system clipboard
36 command_paste_tab = (vim) ->
37 url = utils.readFromClipboard(vim.window)
38 postData = null
39 if not utils.isURL(url) and submission = utils.browserSearchSubmission(url)
40 url = submission.uri.spec
41 { postData } = submission
42
43 if chromeWindow = utils.getRootWindow vim.window
44 chromeWindow.gBrowser.selectedTab = chromeWindow.gBrowser.addTab(url, null, null, postData, null, false)
45
46 # Open new tab and focus the address bar
47 command_open_tab = (vim) ->
48 if chromeWindow = utils.getRootWindow(vim.window)
49 chromeWindow.BrowserOpenTab()
50
51 # Copy element URL to the clipboard
52 command_marker_yank = (vim) ->
53 markers = hints.injectHints(vim.window.document)
54 if markers.length > 0
55 cb = (marker) ->
56 if url = marker.element.href
57 marker.element.focus()
58 utils.writeToClipboard(vim.window, url)
59 else if utils.isTextInputElement(marker.element)
60 utils.writeToClipboard(vim.window, marker.element.value)
61
62 vim.enterHintsMode(markers, cb)
63
64 # Focus element
65 command_marker_focus = (vim) ->
66 markers = hints.injectHints(vim.window.document)
67 if markers.length > 0
68 vim.enterHintsMode(markers, (marker) -> marker.element.focus())
69
70 # Copy current URL to the clipboard
71 command_yank = (vim) ->
72 utils.writeToClipboard(vim.window, vim.window.location.toString())
73
74 # Reload the page, possibly from cache
75 command_reload = (vim) ->
76 vim.window.location.reload(false)
77
78 # Reload the page from the server
79 command_reload_force = (vim) ->
80 vim.window.location.reload(true)
81
82 # Reload the page, possibly from cache
83 command_reload_all = (vim) ->
84 if rootWindow = utils.getRootWindow(vim.window)
85 if tabs = rootWindow.gBrowser.tabContainer
86 for i in [0...tabs.itemCount]
87 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
88 window.location.reload(false)
89
90 # Reload the page from the server
91 command_reload_all_force = (vim) ->
92 if rootWindow = utils.getRootWindow(vim.window)
93 if tabs = rootWindow.gBrowser.tabContainer
94 for i in [0...tabs.itemCount]
95 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
96 window.location.reload(true)
97
98 command_stop = (vim) ->
99 vim.window.stop()
100
101 command_stop_all = (vim) ->
102 if rootWindow = utils.getRootWindow(vim.window)
103 if tabs = rootWindow.gBrowser.tabContainer
104 for i in [0...tabs.itemCount]
105 window = tabs.getItemAtIndex(i).linkedBrowser.contentWindow
106 window.stop()
107
108 # Scroll to the top of the page
109 command_scroll_to_top = (vim) ->
110 for i in [0...1000]
111 utils.simulateWheel(vim.window, 0, -1, utils.WHEEL_MODE_PAGE)
112
113 # Scroll to the bottom of the page
114 command_scroll_to_bottom = (vim) ->
115 for i in [0...1000]
116 utils.simulateWheel(vim.window, 0, 1, utils.WHEEL_MODE_PAGE)
117
118 # Scroll down a bit
119 command_scroll_down = (vim) ->
120 utils.simulateWheel(vim.window, 0, getPref('scroll_step_lines'), utils.WHEEL_MODE_LINE)
121
122 # Scroll up a bit
123 command_scroll_up = (vim) ->
124 utils.simulateWheel(vim.window, 0, -getPref('scroll_step_lines'), utils.WHEEL_MODE_LINE)
125
126 # Scroll left a bit
127 command_scroll_left = (vim) ->
128 utils.simulateWheel(vim.window, -getPref('scroll_step_lines'), 0, utils.WHEEL_MODE_LINE)
129
130 # Scroll right a bit
131 command_scroll_right = (vim) ->
132 utils.simulateWheel(vim.window, getPref('scroll_step_lines'), 0, utils.WHEEL_MODE_LINE)
133
134 # Scroll down half a page
135 command_scroll_half_page_down = (vim) ->
136 utils.simulateWheel(vim.window, 0, 0.5, utils.WHEEL_MODE_PAGE)
137
138 # Scroll up half a page
139 command_scroll_half_page_up = (vim) ->
140 utils.simulateWheel(vim.window, 0, -0.5, utils.WHEEL_MODE_PAGE)
141
142 # Scroll down full a page
143 command_scroll_page_down = (vim) ->
144 utils.simulateWheel(vim.window, 0, 1, utils.WHEEL_MODE_PAGE)
145
146 # Scroll up full a page
147 command_scroll_page_up = (vim) ->
148 utils.simulateWheel(vim.window, 0, -1, utils.WHEEL_MODE_PAGE)
149
150 # Activate previous tab
151 command_tab_prev = (vim) ->
152 if rootWindow = utils.getRootWindow(vim.window)
153 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true)
154
155 # Activate next tab
156 command_tab_next = (vim) ->
157 if rootWindow = utils.getRootWindow(vim.window)
158 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true)
159
160 command_home = (vim) ->
161 url = getFirefoxPref('browser.startup.homepage')
162 if chromeWindow = utils.getRootWindow(vim.window)
163 chromeWindow.gBrowser.loadURIWithFlags(url, null, null, null, null)
164
165 # Go to the first tab
166 command_tab_first = (vim) ->
167 if rootWindow = utils.getRootWindow(vim.window)
168 rootWindow.gBrowser.tabContainer.selectedIndex = 0
169
170 # Go to the last tab
171 command_tab_last = (vim) ->
172 if rootWindow = utils.getRootWindow(vim.window)
173 itemCount = rootWindow.gBrowser.tabContainer.itemCount
174 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1
175
176 # Go back in history
177 command_back = (vim) ->
178 vim.window.history.back()
179
180 # Go forward in history
181 command_forward = (vim) ->
182 vim.window.history.forward()
183
184 # Close current tab
185 command_close_tab = (vim) ->
186 if rootWindow = utils.getRootWindow(vim.window)
187 unless rootWindow.gBrowser.selectedTab.pinned
188 rootWindow.gBrowser.removeCurrentTab()
189
190 # Restore last closed tab
191 command_reload_tab = (vim) ->
192 if rootWindow = utils.getRootWindow(vim.window)
193 ss = utils.getSessionStore()
194 if ss and ss.getClosedTabCount(rootWindow) > 0
195 ss.undoCloseTab(rootWindow, 0)
196
197 # Follow links with hint markers
198 command_follow = (vim) ->
199 if document = vim.window.document
200 markers = hints.injectHints(document)
201 if markers.length > 0
202 # This callback will be called with the selected marker as argument
203 cb = (marker) ->
204 marker.element.focus()
205 utils.simulateClick(marker.element)
206
207 vim.enterHintsMode(markers, cb)
208
209 # Follow links in a new Tab with hint markers
210 command_follow_in_tab = (vim) ->
211 markers = hints.injectHints(vim.window.document)
212 if markers.length > 0
213 # This callback will be called with the selected marker as argument
214 cb = (marker) ->
215 marker.element.focus()
216 utils.simulateClick(marker.element, { metaKey: true, ctrlKey: true })
217
218 vim.enterHintsMode(markers, cb)
219
220 # Move current tab to the left
221 command_tab_move_left = (vim) ->
222 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
223 if tab = gBrowser.selectedTab
224 index = gBrowser.tabContainer.selectedIndex
225 total = gBrowser.tabContainer.itemCount
226
227 # `total` is added to deal with negative offset
228 gBrowser.moveTabTo(tab, (total + index - 1) % total)
229
230 # Move current tab to the right
231 command_tab_move_right = (vim) ->
232 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
233 if tab = gBrowser.selectedTab
234 index = gBrowser.tabContainer.selectedIndex
235 total = gBrowser.tabContainer.itemCount
236
237 gBrowser.moveTabTo(tab, (index + 1) % total)
238
239 # Display the Help Dialog
240 command_help = (vim) ->
241 help.injectHelp(vim.window.document, commands)
242
243 # Switch into find mode
244 command_find = (vim) ->
245 find.injectFind vim.window.document, (findStr, startFindRng) ->
246 # Reset region and find string if new find stirng has arrived
247 if vim.findStr != findStr
248 [vim.findStr, vim.findRng] = [findStr, startFindRng]
249 # Perform forward find and store found region
250 return vim.findRng = find.find(vim.window, vim.findStr, vim.findRng, find.DIRECTION_FORWARDS)
251
252 # Switch into find mode with highlighting
253 command_find_hl = (vim) ->
254 find.injectFind vim.window.document, (findStr) ->
255 # Reset region and find string if new find stirng has arrived
256 return find.highlight(vim.window, findStr)
257
258 # Search for the last pattern
259 command_find_next = (vim) ->
260 if vim.findStr.length > 0
261 vim.findRng = find.find(vim.window, vim.findStr, vim.findRng, find.DIRECTION_FORWARDS, true)
262
263 # Search for the last pattern backwards
264 command_find_prev = (vim) ->
265 if vim.findStr.length > 0
266 vim.findRng = find.find(vim.window, vim.findStr, vim.findRng, find.DIRECTION_BACKWARDS, true)
267
268 # Close the Help dialog and cancel the pending hint marker action
269 command_Esc = (vim) ->
270 # Blur active element if it's editable. Other elements
271 # aren't blurred - we don't want to interfere with
272 # the browser too much
273 activeElement = vim.window.document.activeElement
274 if utils.isElementEditable(activeElement)
275 activeElement.blur()
276
277 #Remove Find input
278 find.removeFind(vim.window.document)
279
280 # Remove hints
281 hints.removeHints(vim.window.document)
282
283 # Hide help dialog
284 help.removeHelp(vim.window.document)
285
286 # Finally enter normal mode
287 vim.enterNormalMode()
288
289 if not getPref('leave_dt_on_esc')
290 if chromeWindow = utils.getRootWindow(vim.window)
291 chromeWindow.DeveloperToolbar.hide()
292
293 class Command
294 constructor: (@group, @name, @func, @keys) ->
295 @defaultKeys = @keys
296 try
297 @keys = JSON.parse(getPref(@prefName('keys')))
298 catch err
299 @keys = @defaultKeys
300
301 # Check if this command may match given string if more chars are added
302 mayMatch: (value) ->
303 return @keys.reduce(((m, v) -> m or v.indexOf(value) == 0), false)
304
305 # Check is this command matches given string
306 match: (value) ->
307 return @keys.reduce(((m, v) -> m or v == value), false)
308
309 # Name of the preference for a given property
310 prefName: (value) -> "commands.#{ @name }.#{ value }"
311
312 assign: (value) ->
313 @keys = value or @defaultKeys
314 setPref(@prefName('keys'), value and JSON.stringify(value))
315
316 enabled: (value) ->
317 if value is undefined
318 return getPref(@prefName('enabled'), true)
319 else
320 setPref(@prefName('enabled'), !!value)
321
322 help: -> _("help_command_#{ @name }")
323
324 commands = [
325 new Command('urls', 'focus', command_focus, ['o'])
326 new Command('urls', 'paste', command_paste, ['p'])
327 new Command('urls', 'paste_tab', command_paste_tab, ['P'])
328 new Command('urls', 'marker_yank', command_marker_yank, ['y,f'])
329 new Command('urls', 'marker_focus', command_marker_focus, ['v,f'])
330 new Command('urls', 'yank', command_yank, ['y,y'])
331 new Command('urls', 'reload', command_reload, ['r'])
332 new Command('urls', 'reload_force', command_reload_force, ['R'])
333 new Command('urls', 'reload_all', command_reload_all, ['a,r'])
334 new Command('urls', 'reload_all_force', command_reload_all_force, ['a,R'])
335 new Command('urls', 'stop', command_stop, ['s'])
336 new Command('urls', 'stop_all', command_stop_all, ['a,s'])
337
338 new Command('nav', 'scroll_to_top', command_scroll_to_top , ['g,g'])
339 new Command('nav', 'scroll_to_bottom', command_scroll_to_bottom, ['G'])
340 new Command('nav', 'scroll_down', command_scroll_down, ['j', 'c-e'])
341 new Command('nav', 'scroll_up', command_scroll_up, ['k', 'c-y'])
342 new Command('nav', 'scroll_left', command_scroll_left, ['h'])
343 new Command('nav', 'scroll_right ', command_scroll_right , ['l'])
344 new Command('nav', 'scroll_half_page_down', command_scroll_half_page_down, ['d'])
345 new Command('nav', 'scroll_half_page_up', command_scroll_half_page_up, ['u'])
346 new Command('nav', 'scroll_page_down', command_scroll_page_down, ['c-f'])
347 new Command('nav', 'scroll_page_up', command_scroll_page_up, ['c-b'])
348
349 new Command('tabs', 'open_tab', command_open_tab, ['t'])
350 new Command('tabs', 'tab_prev', command_tab_prev, ['J', 'g,T'])
351 new Command('tabs', 'tab_next', command_tab_next, ['K', 'g,t'])
352 new Command('tabs', 'tab_move_left', command_tab_move_left, ['c-J'])
353 new Command('tabs', 'tab_move_right', command_tab_move_right, ['c-K'])
354 new Command('tabs', 'home', command_home, ['g,h'])
355 new Command('tabs', 'tab_first', command_tab_first, ['g,H', 'g,\^'])
356 new Command('tabs', 'tab_last', command_tab_last, ['g,L', 'g,$'])
357 new Command('tabs', 'close_tab', command_close_tab, ['x'])
358 new Command('tabs', 'reload_tab', command_reload_tab, ['X'])
359
360 new Command('browse', 'follow', command_follow, ['f'])
361 new Command('browse', 'follow_in_tab', command_follow_in_tab, ['F'])
362 new Command('browse', 'back', command_back, ['H'])
363 new Command('browse', 'forward', command_forward, ['L'])
364
365 new Command('misc', 'find', command_find, ['\.', '/'])
366 new Command('misc', 'find_hl', command_find_hl, ['a,\.', 'a,/'])
367 new Command('misc', 'find_next', command_find_next, ['n'])
368 new Command('misc', 'find_prev', command_find_prev, ['N'])
369 new Command('misc', 'help', command_help, ['?', '>'])
370 new Command('misc', 'Esc', command_Esc, ['Esc'])
371 new Command('misc', 'dev', command_dev, [':'])
372 ]
373
374 # Called in hints mode. Will process the char, update and hide/show markers
375 hintCharHandler = (vim, keyStr, charCode) ->
376 if keyStr and charCode > 0
377 # Get char and escape it to avoid problems with String.search
378 key = utils.regexpEscape(keyStr)
379
380 # First do a pre match - count how many markers will match with the new character entered
381 if vim.markers.reduce(((v, marker) -> v or marker.willMatch(key)), false)
382 for marker in vim.markers
383 marker.matchHintChar(key)
384
385 if marker.isMatched()
386 # Add element features to the bloom filter
387 marker.reward()
388 vim.cb(marker)
389 hints.removeHints(vim.window.document)
390 vim.enterNormalMode()
391 break
392
393 findCommand = (keys) ->
394 for i in [0...keys.length]
395 str = keys[i..].join(',')
396 for cmd in commands
397 for key in cmd.keys
398 if key == str and cmd.enabled()
399 return cmd
400
401 maybeCommand = (keys) ->
402 for i in [0...keys.length]
403 str = keys[i..].join(',')
404 for cmd in commands
405 for key in cmd.keys
406 if key.indexOf(str) == 0 and cmd.enabled()
407 return true
408
409 exports.hintCharHandler = hintCharHandler
410 exports.findCommand = findCommand
411 exports.maybeCommand = maybeCommand
Imprint / Impressum