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