]> git.gir.st - VimFx.git/blob - extension/lib/commands.coffee
Implement simple marks
[VimFx.git] / extension / lib / commands.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014, 2015.
4 # Copyright Wang Zhuochun 2013, 2014.
5 #
6 # This file is part of VimFx.
7 #
8 # VimFx is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # VimFx is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
20 ###
21
22 # This file defines all Normal mode commands. Commands that need to interact
23 # with web page content do so by running `vim._run(name)`, which invokes `name`
24 # in commands-frame.coffee.
25
26 # NOTE: Most tab related commands need to do their actual tab manipulations in
27 # the next tick (`utils.nextTick`) to work around bug 1200334.
28
29 help = require('./help')
30 hints = require('./hints')
31 prefs = require('./prefs')
32 utils = require('./utils')
33
34 commands = {}
35
36
37
38 commands.focus_location_bar = ({vim}) ->
39 # This function works even if the Address Bar has been removed.
40 vim.window.focusAndSelectUrlBar()
41
42 commands.focus_search_bar = ({vim, count}) ->
43 # The `.webSearch()` method opens a search engine in a tab if the Search Bar
44 # has been removed. Therefore we first check if it exists.
45 if vim.window.BrowserSearch.searchBar
46 vim.window.BrowserSearch.webSearch()
47
48 helper_paste_and_go = (props, {vim}) ->
49 {gURLBar} = vim.window
50 gURLBar.value = vim.window.readFromClipboard()
51 gURLBar.handleCommand(new vim.window.KeyboardEvent('keydown', props))
52
53 commands.paste_and_go = helper_paste_and_go.bind(null, null)
54
55 commands.paste_and_go_in_tab = helper_paste_and_go.bind(null, {altKey: true})
56
57 commands.copy_current_url = ({vim}) ->
58 utils.writeToClipboard(vim.window.gBrowser.currentURI.spec)
59
60 commands.go_up_path = ({vim, count}) ->
61 vim._run('go_up_path', {count})
62
63 # Go up to root of the URL hierarchy.
64 commands.go_to_root = ({vim}) ->
65 vim._run('go_to_root')
66
67 commands.go_home = ({vim}) ->
68 vim.window.BrowserHome()
69
70 helper_go_history = (num, {vim, count = 1}) ->
71 {SessionStore, gBrowser} = vim.window
72
73 # TODO: When Firefox 43 is released, only use the `.getSessionHistory`
74 # version and bump the minimut Firefox version.
75 if SessionStore.getSessionHistory
76 SessionStore.getSessionHistory(gBrowser.selectedTab, (sessionHistory) ->
77 {index} = sessionHistory
78 newIndex = index + num * count
79 newIndex = Math.max(newIndex, 0)
80 newIndex = Math.min(newIndex, sessionHistory.entries.length - 1)
81 gBrowser.gotoIndex(newIndex) unless newIndex == index
82 )
83 else
84 # Until then, fall back to a no-count version.
85 if num < 0 then gBrowser.goBack() else gBrowser.goForward()
86
87 commands.history_back = helper_go_history.bind(null, -1)
88
89 commands.history_forward = helper_go_history.bind(null, +1)
90
91 commands.reload = ({vim}) ->
92 vim.window.BrowserReload()
93
94 commands.reload_force = ({vim}) ->
95 vim.window.BrowserReloadSkipCache()
96
97 commands.reload_all = ({vim}) ->
98 vim.window.gBrowser.reloadAllTabs()
99
100 commands.reload_all_force = ({vim}) ->
101 for tab in vim.window.gBrowser.visibleTabs
102 gBrowser = tab.linkedBrowser
103 consts = gBrowser.webNavigation
104 flags = consts.LOAD_FLAGS_BYPASS_PROXY | consts.LOAD_FLAGS_BYPASS_CACHE
105 gBrowser.reload(flags)
106 return
107
108 commands.stop = ({vim}) ->
109 vim.window.BrowserStop()
110
111 commands.stop_all = ({vim}) ->
112 for tab in vim.window.gBrowser.visibleTabs
113 tab.linkedBrowser.stop()
114 return
115
116
117
118 helper_scroll = (vim, args...) ->
119 [method, type, directions, amounts, properties = null, name = 'scroll'] = args
120 options = {
121 method, type, directions, amounts, properties
122 smooth: (prefs.root.get('general.smoothScroll') and
123 prefs.root.get("general.smoothScroll.#{type}"))
124 }
125 reset = prefs.root.tmp(
126 'layout.css.scroll-behavior.spring-constant',
127 vim.options["smoothScroll.#{type}.spring-constant"]
128 )
129 vim._run(name, options, reset)
130
131 helper_scrollByLinesX = (amount, {vim, count = 1}) ->
132 distance = prefs.root.get('toolkit.scrollbox.horizontalScrollDistance')
133 helper_scroll(vim, 'scrollBy', 'lines', ['left'],
134 [amount * distance * count * 5])
135
136 helper_scrollByLinesY = (amount, {vim, count = 1}) ->
137 distance = prefs.root.get('toolkit.scrollbox.verticalScrollDistance')
138 helper_scroll(vim, 'scrollBy', 'lines', ['top'],
139 [amount * distance * count * 20])
140
141 helper_scrollByPagesY = (amount, {vim, count = 1}) ->
142 helper_scroll(vim, 'scrollBy', 'pages', ['top'],
143 [amount * count], ['clientHeight'])
144
145 helper_scrollToX = (amount, {vim}) ->
146 helper_scroll(vim, 'scrollTo', 'other', ['left'], [amount], ['scrollLeftMax'])
147
148 helper_scrollToY = (amount, {vim}) ->
149 helper_scroll(vim, 'scrollTo', 'other', ['top'], [amount], ['scrollTopMax'])
150
151 commands.scroll_left = helper_scrollByLinesX.bind(null, -1)
152 commands.scroll_right = helper_scrollByLinesX.bind(null, +1)
153 commands.scroll_down = helper_scrollByLinesY.bind(null, +1)
154 commands.scroll_up = helper_scrollByLinesY.bind(null, -1)
155 commands.scroll_page_down = helper_scrollByPagesY.bind(null, +1)
156 commands.scroll_page_up = helper_scrollByPagesY.bind(null, -1)
157 commands.scroll_half_page_down = helper_scrollByPagesY.bind(null, +0.5)
158 commands.scroll_half_page_up = helper_scrollByPagesY.bind(null, -0.5)
159 commands.scroll_to_top = helper_scrollToY.bind(null, 0)
160 commands.scroll_to_bottom = helper_scrollToY.bind(null, Infinity)
161 commands.scroll_to_left = helper_scrollToX.bind(null, 0)
162 commands.scroll_to_right = helper_scrollToX.bind(null, Infinity)
163
164 commands.mark_scroll_position = ({vim}) ->
165 vim.enterMode('marks', (keyStr) -> vim._run('mark_scroll_position', {keyStr}))
166
167 commands.scroll_to_mark = ({vim}) ->
168 vim.enterMode('marks', (keyStr) ->
169 helper_scroll(vim, 'scrollTo', 'other', ['top', 'left'], keyStr,
170 ['scrollTopMax', 'scrollLeftMax'], 'scroll_to_mark')
171 )
172
173
174
175 commands.tab_new = ({vim}) ->
176 utils.nextTick(vim.window, ->
177 vim.window.BrowserOpenTab()
178 )
179
180 commands.tab_duplicate = ({vim}) ->
181 {gBrowser} = vim.window
182 utils.nextTick(vim.window, ->
183 gBrowser.duplicateTab(gBrowser.selectedTab)
184 )
185
186 absoluteTabIndex = (relativeIndex, gBrowser, {pinnedSeparate}) ->
187 tabs = gBrowser.visibleTabs
188 {selectedTab} = gBrowser
189
190 currentIndex = tabs.indexOf(selectedTab)
191 absoluteIndex = currentIndex + relativeIndex
192 numTabsTotal = tabs.length
193 numPinnedTabs = gBrowser._numPinnedTabs
194
195 [numTabs, min] = switch
196 when not pinnedSeparate then [numTabsTotal, 0]
197 when selectedTab.pinned then [numPinnedTabs, 0]
198 else [numTabsTotal - numPinnedTabs, numPinnedTabs]
199
200 # Wrap _once_ if at one of the ends of the tab bar and cannot move in the
201 # current direction.
202 if (relativeIndex < 0 and currentIndex == min) or
203 (relativeIndex > 0 and currentIndex == min + numTabs - 1)
204 if absoluteIndex < min
205 absoluteIndex += numTabs
206 else if absoluteIndex >= min + numTabs
207 absoluteIndex -= numTabs
208
209 absoluteIndex = Math.max(min, absoluteIndex)
210 absoluteIndex = Math.min(absoluteIndex, min + numTabs - 1)
211
212 return absoluteIndex
213
214 helper_switch_tab = (direction, {vim, count = 1}) ->
215 {gBrowser} = vim.window
216 index = absoluteTabIndex(direction * count, gBrowser, {pinnedSeparate: false})
217 utils.nextTick(vim.window, ->
218 gBrowser.selectTabAtIndex(index)
219 )
220
221 commands.tab_select_previous = helper_switch_tab.bind(null, -1)
222
223 commands.tab_select_next = helper_switch_tab.bind(null, +1)
224
225 helper_move_tab = (direction, {vim, count = 1}) ->
226 {gBrowser} = vim.window
227 index = absoluteTabIndex(direction * count, gBrowser, {pinnedSeparate: true})
228 utils.nextTick(vim.window, ->
229 gBrowser.moveTabTo(gBrowser.selectedTab, index)
230 )
231
232 commands.tab_move_backward = helper_move_tab.bind(null, -1)
233
234 commands.tab_move_forward = helper_move_tab.bind(null, +1)
235
236 commands.tab_select_first = ({vim, count = 1}) ->
237 utils.nextTick(vim.window, ->
238 vim.window.gBrowser.selectTabAtIndex(count - 1)
239 )
240
241 commands.tab_select_first_non_pinned = ({vim, count = 1}) ->
242 firstNonPinned = vim.window.gBrowser._numPinnedTabs
243 utils.nextTick(vim.window, ->
244 vim.window.gBrowser.selectTabAtIndex(firstNonPinned + count - 1)
245 )
246
247 commands.tab_select_last = ({vim, count = 1}) ->
248 utils.nextTick(vim.window, ->
249 vim.window.gBrowser.selectTabAtIndex(-count)
250 )
251
252 commands.tab_toggle_pinned = ({vim}) ->
253 currentTab = vim.window.gBrowser.selectedTab
254 if currentTab.pinned
255 vim.window.gBrowser.unpinTab(currentTab)
256 else
257 vim.window.gBrowser.pinTab(currentTab)
258
259 commands.tab_close = ({vim, count = 1}) ->
260 {gBrowser} = vim.window
261 return if gBrowser.selectedTab.pinned
262 currentIndex = gBrowser.visibleTabs.indexOf(gBrowser.selectedTab)
263 utils.nextTick(vim.window, ->
264 for tab in gBrowser.visibleTabs[currentIndex...(currentIndex + count)]
265 gBrowser.removeTab(tab)
266 return
267 )
268
269 commands.tab_restore = ({vim, count = 1}) ->
270 utils.nextTick(vim.window, ->
271 vim.window.undoCloseTab() for [1..count] by 1
272 return
273 )
274
275 commands.tab_close_to_end = ({vim}) ->
276 {gBrowser} = vim.window
277 gBrowser.removeTabsToTheEndFrom(gBrowser.selectedTab)
278
279 commands.tab_close_other = ({vim}) ->
280 {gBrowser} = vim.window
281 gBrowser.removeAllTabsBut(gBrowser.selectedTab)
282
283
284
285 helper_follow = (name, vim, callback, count = null) ->
286 vim.markPageInteraction()
287
288 # Enter hints mode immediately, with an empty set of markers. The user might
289 # press keys before the `vim._run` callback is invoked. Those key presses
290 # should be handled in hints mode, not normal mode.
291 initialMarkers = []
292 storage = vim.enterMode('hints', initialMarkers, callback, count)
293
294 vim._run(name, null, ({wrappers, viewport}) ->
295 # The user might have exited hints mode (and perhaps even entered it again)
296 # before this callback is invoked. If so, `storage.markers` has been
297 # cleared, or set to a new value. Only proceed if it is unchanged.
298 return unless storage.markers == initialMarkers
299
300 if wrappers.length > 0
301 markers = hints.injectHints(vim.window, wrappers, viewport, vim.options)
302 storage.markers = markers
303 else
304 vim.enterMode('normal')
305 )
306
307 helper_follow_clickable = ({inTab, inBackground}, {vim, count = 1}) ->
308 callback = (marker, timesLeft, keyStr) ->
309 {type, elementIndex} = marker.wrapper
310 isLast = (timesLeft == 1)
311 isLink = (type == 'link')
312
313 switch
314 when keyStr.startsWith(vim.options.hints_toggle_in_tab)
315 inTab = not inTab
316 when keyStr.startsWith(vim.options.hints_toggle_in_background)
317 inTab = true
318 inBackground = not inBackground
319 else
320 unless isLast
321 inTab = true
322 inBackground = true
323
324 inTab = false unless isLink
325
326 if type == 'text' or (isLink and not (inTab and inBackground))
327 isLast = true
328
329 vim._focusMarkerElement(elementIndex)
330
331 if inTab
332 utils.nextTick(vim.window, ->
333 utils.openTab(vim.window, marker.wrapper.href, {
334 inBackground
335 relatedToCurrent: true
336 })
337 )
338 else
339 vim._run('click_marker_element', {
340 elementIndex, type
341 preventTargetBlank: vim.options.prevent_target_blank
342 })
343
344 return not isLast
345
346 name = if inTab then 'follow_in_tab' else 'follow'
347 helper_follow(name, vim, callback, count)
348
349 # Follow links, focus text inputs and click buttons with hint markers.
350 commands.follow =
351 helper_follow_clickable.bind(null, {inTab: false, inBackground: true})
352
353 # Follow links in a new background tab with hint markers.
354 commands.follow_in_tab =
355 helper_follow_clickable.bind(null, {inTab: true, inBackground: true})
356
357 # Follow links in a new foreground tab with hint markers.
358 commands.follow_in_focused_tab =
359 helper_follow_clickable.bind(null, {inTab: true, inBackground: false})
360
361 # Like command_follow but multiple times.
362 commands.follow_multiple = (args) ->
363 args.count = Infinity
364 commands.follow(args)
365
366 # Copy the URL or text of a markable element to the system clipboard.
367 commands.follow_copy = ({vim}) ->
368 callback = (marker) ->
369 {elementIndex} = marker.wrapper
370 property = switch marker.wrapper.type
371 when 'link' then 'href'
372 when 'text' then 'value'
373 when 'contenteditable' then 'textContent'
374 vim._run('copy_marker_element', {elementIndex, property})
375 helper_follow('follow_copy', vim, callback)
376
377 # Focus element with hint markers.
378 commands.follow_focus = ({vim}) ->
379 callback = (marker) ->
380 vim._focusMarkerElement(marker.wrapper.elementIndex, {select: true})
381 return helper_follow('follow_focus', vim, callback)
382
383 helper_follow_pattern = (type, {vim}) ->
384 options =
385 pattern_selector: vim.options.pattern_selector
386 pattern_attrs: vim.options.pattern_attrs
387 patterns: vim.options["#{type}_patterns"]
388 vim._run('follow_pattern', {type, options})
389
390 commands.follow_previous = helper_follow_pattern.bind(null, 'prev')
391
392 commands.follow_next = helper_follow_pattern.bind(null, 'next')
393
394 # Focus last focused or first text input.
395 commands.focus_text_input = ({vim, count}) ->
396 vim.markPageInteraction()
397 vim._run('focus_text_input', {count})
398
399
400
401 findStorage = {lastSearchString: ''}
402
403 helper_find = (highlight, {vim}) ->
404 findBar = vim.window.gBrowser.getFindBar()
405
406 findBar.onFindCommand()
407 utils.focusElement(findBar._findField, {select: true})
408
409 return unless highlightButton = findBar.getElement('highlight')
410 if highlightButton.checked != highlight
411 highlightButton.click()
412
413 # Open the find bar, making sure that hightlighting is off.
414 commands.find = helper_find.bind(null, false)
415
416 # Open the find bar, making sure that hightlighting is on.
417 commands.find_highlight_all = helper_find.bind(null, true)
418
419 helper_find_again = (direction, {vim}) ->
420 findBar = vim.window.gBrowser.getFindBar()
421 if findStorage.lastSearchString.length > 0
422 findBar._findField.value = findStorage.lastSearchString
423 findBar.onFindAgainCommand(direction)
424 message = findBar._findStatusDesc.textContent
425 vim.notify(message) if message
426
427 commands.find_next = helper_find_again.bind(null, false)
428
429 commands.find_previous = helper_find_again.bind(null, true)
430
431
432
433 commands.enter_mode_ignore = ({vim}) ->
434 vim.enterMode('ignore')
435
436 # Quote next keypress (pass it through to the page).
437 commands.quote = ({vim, count = 1}) ->
438 vim.enterMode('ignore', count)
439
440 # Display the Help Dialog.
441 commands.help = ({vim}) ->
442 help.injectHelp(vim.window, vim._parent)
443
444 # Open and focus the Developer Toolbar.
445 commands.dev = ({vim}) ->
446 vim.window.DeveloperToolbar.show(true) # `true` to focus.
447
448 commands.esc = ({vim}) ->
449 vim._run('esc')
450 utils.blurActiveBrowserElement(vim)
451 help.removeHelp(vim.window)
452 vim.window.DeveloperToolbar.hide()
453 vim.window.gBrowser.getFindBar().close()
454 # TODO: Remove when Tab Groups have been removed.
455 vim.window.TabView?.hide()
456 hints.removeHints(vim.window) # Better safe than sorry.
457
458
459
460 module.exports = {
461 commands
462 findStorage
463 }
Imprint / Impressum