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