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