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