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