]> git.gir.st - VimFx.git/blob - extension/packages/commands.coffee
#16, Closes #19. Changed u/d to scroll half a page, added c-f/c-b to scroll full...
[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 vim.window.scrollTo(0, document.body.scrollHeight)
64
65 # Scroll down a bit
66 command_j_ce = (vim) ->
67 vim.window.scrollBy(0, getPref 'scroll_step')
68
69 # Scroll up a bit
70 command_k_cy = (vim) ->
71 vim.window.scrollBy(0, - getPref 'scroll_step')
72
73 # Scroll left a bit
74 command_h = (vim) ->
75 vim.window.scrollBy(- getPref 'scroll_step', 0)
76
77 # Scroll right a bit
78 command_l = (vim) ->
79 vim.window.scrollBy(getPref 'scroll_step', 0)
80
81 # Scroll down half a page
82 command_d = (vim) ->
83 vim.window.scrollBy(0, vim.window.innerHeight / 2)
84
85 # Scroll up half a page
86 command_u = (vim) ->
87 vim.window.scrollBy(0, -vim.window.innerHeight / 2)
88 #
89 # Scroll down full a page
90 command_cf = (vim) ->
91 vim.window.scrollBy(0, vim.window.innerHeight)
92
93 # Scroll up full a page
94 command_cb = (vim) ->
95 vim.window.scrollBy(0, -vim.window.innerHeight)
96
97 # Activate previous tab
98 command_J_gT = (vim) ->
99 if rootWindow = utils.getRootWindow vim.window
100 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true);
101
102 # Activate next tab
103 command_K_gt = (vim) ->
104 if rootWindow = utils.getRootWindow vim.window
105 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true);
106
107 # Go to the first tab
108 command_gH_g0 = (vim) ->
109 if rootWindow = utils.getRootWindow vim.window
110 rootWindow.gBrowser.tabContainer.selectedIndex = 0;
111
112 # Go to the last tab
113 command_gL_g$ = (vim) ->
114 if rootWindow = utils.getRootWindow vim.window
115 itemCount = rootWindow.gBrowser.tabContainer.itemCount;
116 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1;
117
118 # Go back in history
119 command_H = (vim) ->
120 vim.window.history.back()
121
122 # Go forward in history
123 command_L = (vim) ->
124 vim.window.history.forward()
125
126 # Close current tab
127 command_x = (vim) ->
128 if rootWindow = utils.getRootWindow vim.window
129 rootWindow.gBrowser.removeCurrentTab()
130
131 # Restore last closed tab
132 command_X = (vim) ->
133 if rootWindow = utils.getRootWindow vim.window
134 ss = utils.getSessionStore()
135 if ss and ss.getClosedTabCount(rootWindow) > 0
136 ss.undoCloseTab rootWindow, 0
137
138 # Follow links with hint markers
139 command_f = (vim) ->
140 if document = vim.window.document
141 vim.markers = injectHints document
142 if vim.markers.length > 0
143 # This callback will be called with the selected marker as argument
144 vim.cb = (marker) ->
145 marker.element.focus()
146 utils.simulateClick marker.element
147
148 vim.enterHintsMode()
149
150 # Follow links in a new Tab with hint markers
151 command_F = (vim) ->
152 vim.markers = injectHints vim.window.document
153 if vim.markers.length > 0
154 # This callback will be called with the selected marker as argument
155 vim.cb = (marker) ->
156 marker.element.focus()
157 utils.simulateClick marker.element, { metaKey: true, ctrlKey: true }
158
159 vim.enterHintsMode()
160
161 # Move current tab to the left
162 command_cJ = (vim) ->
163 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
164 if tab = gBrowser.selectedTab
165 index = gBrowser.tabContainer.selectedIndex
166 total = gBrowser.tabContainer.itemCount
167
168 # `total` is added to deal with negative offset
169 console.log index, total, (total + index - 1) % total, 'left'
170 gBrowser.moveTabTo tab, (total + index - 1) % total
171
172 # Move current tab to the right
173 command_cK = (vim) ->
174 if gBrowser = utils.getRootWindow(vim.window)?.gBrowser
175 if tab = gBrowser.selectedTab
176 index = gBrowser.tabContainer.selectedIndex
177 total = gBrowser.tabContainer.itemCount
178
179 console.log index, total, (index + 1) % total, 'right'
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, "Navigate to the address in the clipboard" ]
205 'P': [ command_P, "Open new tab and navigate to the address in the clipboard" ]
206 'y,f': [ command_yf, "Copy link url to the clipboard" ]
207 'y,y': [ command_yy, "Copy current page link to the clipboard" ]
208 'r': [ command_r, "Reload current page" ]
209 'R': [ command_R, "Reload current page and all the assets (js, css, etc.)" ]
210 'nav':
211 'g,g': [ command_gg , "Scroll to the Top of the page" ]
212 'G': [ command_G, "Scroll to the Bottom of the page" ]
213 'j|c-e': [ command_j_ce, "Scroll Down" ]
214 'k|c-y': [ command_k_cy, "Scroll Up" ]
215 'h': [ command_h, "Scroll Left" ]
216 'l': [ command_l , "Scroll Right" ]
217 'd': [ command_d, "Scroll half a Page Down" ]
218 'u': [ command_u, "Scroll half a Page Up" ]
219 'c-f': [ command_cf, "Scroll a full Page Down" ]
220 'c-b': [ command_cb, "Scroll a full Page Up" ]
221 'tabs':
222 't': [ command_t, "Open New Blank tab" ]
223 'J|g,T': [ command_J_gT, "Go to the Previous tab" ]
224 'K|g,t': [ command_K_gt, "Go to the Next tab" ]
225 'c-J': [ command_cJ, "Move current tab to the left" ]
226 'c-K': [ command_cK, "Move current tab to the right" ]
227 'g,H|g,0': [ command_gH_g0, "Go to the First tab" ]
228 'g,L|g,$': [ command_gL_g$, "Go to the Last tab" ]
229 'x': [ command_x, "Close current tab" ]
230 'X': [ command_X, "Restore last closed tab" ]
231 'browse':
232 'f': [ command_f, "Follow a link on the current page" ]
233 'F': [ command_F, "Follow a link on the current page in a new tab" ]
234 'H': [ command_H, "Go Back in history" ]
235 'L': [ command_L, "Go Forward in history" ]
236 'misc':
237 '?': [ command_help, "Show Help Dialog" ]
238 'Esc': [ command_Esc, "Close this dialog and cancel hint markers" ]
239
240 # Merge groups and split command pipes into individual commands
241 commands = do (commandGroups) ->
242 newCommands = {}
243 for group, commandsList of commandGroups
244 for keys, command of commandsList
245 for key in keys.split '|'
246 newCommands[key] = command[0]
247
248 return newCommands
249
250 # Extract the help text from the commands preserving groups formation
251 commandsHelp = do (commandGroups) ->
252 help = {}
253 for group, commandsList of commandGroups
254 helpGroup = {}
255 for keys, command of commandsList
256 key = keys.replace(',', '').replace('|', ', ')
257 helpGroup[key] = command[1]
258
259 help[group] = helpGroup
260 return help
261
262 # Called in hints mode. Will process the char, update and hide/show markers
263 hintCharHandler = (vim, char) ->
264 # First count how many markers will match with the new character entered
265 preMatch = vim.markers.reduce ((v, marker) -> v + marker.willMatch char), 0
266
267 # If prematch is greater than 0, then proceed with matching, else ignore the new char
268 if preMatch > 0
269 for marker in vim.markers
270 marker.matchHintChar char
271
272 if marker.isMatched()
273 vim.cb marker
274 removeHints vim.window.document
275 vim.enterNormalMode()
276 break
277
278 exports.hintCharHandler = hintCharHandler
279 exports.commands = commands
280 exports.commandsHelp = commandsHelp
Imprint / Impressum