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