]> git.gir.st - VimFx.git/blob - extension/packages/commands.coffee
Added c-u/c-d back to scroll half a page up/down
[VimFx.git] / extension / packages / commands.coffee
1 { classes: Cc, interfaces: Ci, utils: Cu } = Components
2
3 utils = require 'utils'
4 { getPref } = require 'prefs'
5
6 { handleHintChar
7 , injectHints
8 , removeHints
9 } = require 'hints'
10
11 { showHelp
12 , hideHelp
13 } = require 'help'
14
15 # Navigate to the address that is currently stored in the system clipboard
16 command_p = (vim) ->
17 vim.window.location.assign utils.readFromClipboard(vim.window)
18
19 # Open new tab and navigate to the address that is currently stored in the system clipboard
20 command_P = (vim) ->
21 if chromeWindow = utils.getRootWindow vim.window
22 if gBrowser = chromeWindow.gBrowser
23 gBrowser.selectedTab = gBrowser.addTab utils.readFromClipboard(vim.window)
24
25 # Open new tab and focus the address bar
26 command_t = (vim) ->
27 if chromeWindow = utils.getRootWindow vim.window
28 if gBrowser = chromeWindow.gBrowser
29 gBrowser.selectedTab = chromeWindow.gBrowser.addTab()
30 if urlbar = chromeWindow.document.getElementById('urlbar')
31 urlbar.focus()
32
33 # Copy current URL to the clipboard
34 command_yf = (vim) ->
35 vim.markers = injectHints vim.window.document
36 if vim.markers.length > 0
37 # This callback will be called with the selected marker as argument
38 vim.cb = (marker) ->
39 if url = marker.element.href
40 utils.writeToClipboard vim.window, url
41
42 vim.enterHintsMode()
43
44 # Copy current URL to the clipboard
45 command_yy = (vim) ->
46 utils.writeToClipboard vim.window, vim.window.location.toString()
47
48 # Reload the page, possibly from cache
49 command_r = (vim) ->
50 vim.window.location.reload(false)
51
52 # Reload the page from the server
53 command_R = (vim) ->
54 vim.window.location.reload(true)
55
56 # Scroll to the top of the page
57 command_gg = (vim) ->
58 vim.window.scrollTo(0, 0)
59
60 # Scroll to the bottom of the page
61 command_G = (vim) ->
62 if document = vim.window.document
63 # Workaround the pages where body isn't the scrollable element.
64 # In this case we try to scroll 100k pixels
65 vim.window.scrollTo(0, Math.max(document.body.scrollHeight, 100000))
66
67 # Scroll down a bit
68 command_j_ce = (vim) ->
69 utils.smoothScroll vim.window, 0, (getPref 'scroll_step'), getPref 'scroll_time'
70
71 # Scroll up a bit
72 command_k_cy = (vim) ->
73 utils.smoothScroll vim.window, 0, -(getPref 'scroll_step'), getPref 'scroll_time'
74
75 # Scroll left a bit
76 command_h = (vim) ->
77 utils.smoothScroll vim.window, -(getPref 'scroll_step'), 0, getPref 'scroll_time'
78
79 # Scroll right a bit
80 command_l = (vim) ->
81 utils.smoothScroll vim.window, (getPref 'scroll_step'), 0, getPref 'scroll_time'
82
83 # Scroll down half a page
84 command_d = (vim) ->
85 utils.smoothScroll vim.window, 0, vim.window.innerHeight / 2, getPref 'scroll_time'
86
87 # Scroll up half a page
88 command_u = (vim) ->
89 utils.smoothScroll vim.window, 0, -vim.window.innerHeight / 2, getPref 'scroll_time'
90
91 # Scroll down full a page
92 command_cf = (vim) ->
93 vim.window.scrollByPages(1)
94
95 # Scroll up full a page
96 command_cb = (vim) ->
97 vim.window.scrollByPages(-1)
98
99 # Activate previous tab
100 command_J_gT = (vim) ->
101 if rootWindow = utils.getRootWindow vim.window
102 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true);
103
104 # Activate next tab
105 command_K_gt = (vim) ->
106 if rootWindow = utils.getRootWindow vim.window
107 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true);
108
109 # Go to the first tab
110 command_gH_g0 = (vim) ->
111 if rootWindow = utils.getRootWindow vim.window
112 rootWindow.gBrowser.tabContainer.selectedIndex = 0;
113
114 # Go to the last tab
115 command_gL_g$ = (vim) ->
116 if rootWindow = utils.getRootWindow vim.window
117 itemCount = rootWindow.gBrowser.tabContainer.itemCount;
118 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1;
119
120 # Go back in history
121 command_H = (vim) ->
122 vim.window.history.back()
123
124 # Go forward in history
125 command_L = (vim) ->
126 vim.window.history.forward()
127
128 # Close current tab
129 command_x = (vim) ->
130 if rootWindow = utils.getRootWindow vim.window
131 rootWindow.gBrowser.removeCurrentTab()
132
133 # Restore last closed tab
134 command_X = (vim) ->
135 if rootWindow = utils.getRootWindow vim.window
136 ss = utils.getSessionStore()
137 if ss and ss.getClosedTabCount(rootWindow) > 0
138 ss.undoCloseTab rootWindow, 0
139
140 # Follow links with hint markers
141 command_f = (vim) ->
142 if document = vim.window.document
143 vim.markers = injectHints document
144 if vim.markers.length > 0
145 # This callback will be called with the selected marker as argument
146 vim.cb = (marker) ->
147 marker.element.focus()
148 utils.simulateClick marker.element
149
150 vim.enterHintsMode()
151
152 # Follow links in a new Tab with hint markers
153 command_F = (vim) ->
154 vim.markers = injectHints vim.window.document
155 if vim.markers.length > 0
156 # This callback will be called with the selected marker as argument
157 vim.cb = (marker) ->
158 marker.element.focus()
159 utils.simulateClick marker.element, { metaKey: true, ctrlKey: true }
160
161 vim.enterHintsMode()
162
163 # Move current tab to the left
164 command_cJ = (vim) ->
165 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
166 if tab = gBrowser.selectedTab
167 index = gBrowser.tabContainer.selectedIndex
168 total = gBrowser.tabContainer.itemCount
169
170 # `total` is added to deal with negative offset
171 gBrowser.moveTabTo tab, (total + index - 1) % total
172
173 # Move current tab to the right
174 command_cK = (vim) ->
175 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
176 if tab = gBrowser.selectedTab
177 index = gBrowser.tabContainer.selectedIndex
178 total = gBrowser.tabContainer.itemCount
179
180 gBrowser.moveTabTo tab, (index + 1) % total
181
182 # Display the Help Dialog
183 command_help = (vim) ->
184 showHelp vim.window.document, commandsHelp
185
186 # Close the Help dialog and cancel the pending hint marker action
187 command_Esc = (vim) ->
188 # Blur active element if it's editable. Other elements
189 # aren't blurred - we don't want to interfere with
190 # the browser too much
191 activeElement = vim.window.document.activeElement
192 if utils.isElementEditable activeElement
193 activeElement.blur()
194
195 # Remove hints
196 removeHints vim.window.document
197 # Hide help dialog
198 hideHelp vim.window.document
199 # Finally enter normal mode
200 vim.enterNormalMode()
201
202 commandGroups =
203 'urls':
204 'p': [ command_p, _('help_command_p') ]
205 'P': [ command_P, _('help_command_P') ]
206 'y,f': [ command_yf, _('help_command_yf') ]
207 'y,y': [ command_yy, _('help_command_yy') ]
208 'r': [ command_r, _('help_command_r') ]
209 'R': [ command_R, _('help_command_R') ]
210 'nav':
211 'g,g': [ command_gg , _('help_command_gg') ]
212 'G': [ command_G, _('help_command_G') ]
213 'j|c-e': [ command_j_ce, _('help_command_j_ce') ]
214 'k|c-y': [ command_k_cy, _('help_command_k_cy') ]
215 'h': [ command_h, _('help_command_h') ]
216 'l': [ command_l , _('help_command_l') ]
217 'd|c-d': [ command_d, _('help_command_d') ]
218 'u|c-u': [ command_u, _('help_command_u') ]
219 'c-f': [ command_cf, _('help_command_cf') ]
220 'c-b': [ command_cb, _('help_command_cb') ]
221 'tabs':
222 't': [ command_t, _('help_command_t') ]
223 'J|g,T': [ command_J_gT, _('help_command_J_gT') ]
224 'K|g,t': [ command_K_gt, _('help_command_K_gt') ]
225 'c-J': [ command_cJ, _('help_command_cJ') ]
226 'c-K': [ command_cK, _('help_command_cK') ]
227 'g,H|g,0': [ command_gH_g0, _('help_command_gH_g0') ]
228 'g,L|g,$': [ command_gL_g$, _('help_command_gL_g$') ]
229 'x': [ command_x, _('help_command_x') ]
230 'X': [ command_X, _('help_command_X') ]
231 'browse':
232 'f': [ command_f, _('help_command_f') ]
233 'F': [ command_F, _('help_command_F') ]
234 'H': [ command_H, _('help_command_H') ]
235 'L': [ command_L, _('help_command_L') ]
236 'misc':
237 # `>` is added to help command mapping to hack around russian keyboard layout
238 # See key-utils.coffee for more info
239 '?|>': [ command_help, _('help_command_help') ]
240 'Esc': [ command_Esc, _('help_command_Esc') ]
241
242 # Merge groups and split command pipes into individual commands
243 commands = do (commandGroups) ->
244 newCommands = {}
245 for group, commandsList of commandGroups
246 for keys, command of commandsList
247 for key in keys.split '|'
248 newCommands[key] = command[0]
249
250 return newCommands
251
252 # Extract the help text from the commands preserving groups formation
253 commandsHelp = do (commandGroups) ->
254 help = {}
255 for group, commandsList of commandGroups
256 helpGroup = {}
257 for keys, command of commandsList
258 key = keys.replace(/,/g, '').replace('|', ', ')
259 helpGroup[key] = command[1]
260
261 help[group] = helpGroup
262 return help
263
264 # Called in hints mode. Will process the char, update and hide/show markers
265 hintCharHandler = (vim, char) ->
266 # First count how many markers will match with the new character entered
267 preMatch = vim.markers.reduce ((v, marker) -> v + marker.willMatch char), 0
268
269 # If prematch is greater than 0, then proceed with matching, else ignore the new char
270 if preMatch > 0
271 for marker in vim.markers
272 marker.matchHintChar char
273
274 if marker.isMatched()
275 vim.cb marker
276 removeHints vim.window.document
277 vim.enterNormalMode()
278 break
279
280 exports.hintCharHandler = hintCharHandler
281 exports.commands = commands
282 exports.commandsHelp = commandsHelp
Imprint / Impressum