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