]> git.gir.st - VimFx.git/blob - extension/packages/commands.coffee
Closes #2. Added the Help Dialog, changed the style of the hint markers. Fixed the...
[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
31 # Copy current URL to the clipboard
32 command_yf = (vim) ->
33 vim.markers = injectHints vim.window.document
34 if vim.markers.length > 0
35 # This callback will be called with the selected marker as argument
36 vim.cb = (marker) ->
37 if url = marker.element.href
38 utils.writeToClipboard vim.window, url
39
40 vim.enterHintsMode()
41
42 # Copy current URL to the clipboard
43 command_yy = (vim) ->
44 utils.writeToClipboard vim.window, vim.window.location.toString()
45
46 # Reload the page, possibly from cache
47 command_r = (vim) ->
48 vim.window.location.reload(false)
49
50 # Reload the page from the server
51 command_R = (vim) ->
52 vim.window.location.reload(true)
53
54 # Scroll to the top of the page
55 command_gg = (vim) ->
56 vim.window.scrollTo(0, 0)
57
58 # Scroll to the bottom of the page
59 command_G = (vim) ->
60 vim.window.scrollTo(0, vim.window.document.body.scrollHeight)
61
62 # Scroll down a bit
63 command_j_ce = (vim) ->
64 vim.window.scrollBy(0, getPref 'scroll_step')
65
66 # Scroll up a bit
67 command_k_cy = (vim) ->
68 vim.window.scrollBy(0, - getPref 'scroll_step')
69
70 # Scroll left a bit
71 command_h = (vim) ->
72 vim.window.scrollBy(- getPref 'scroll_step', 0)
73
74 # Scroll right a bit
75 command_l = (vim) ->
76 vim.window.scrollBy(getPref 'scroll_step', 0)
77
78 # Scroll down a page
79 command_d_cd = (vim) ->
80 vim.window.scrollBy(0, vim.window.innerHeight)
81
82 # Scroll up a page
83 command_u_cu = (vim) ->
84 vim.window.scrollBy(0, -vim.window.innerHeight)
85
86 # Activate previous tab
87 command_J_gT = (vim) ->
88 if rootWindow = utils.getRootWindow vim.window
89 rootWindow.gBrowser.tabContainer.advanceSelectedTab(-1, true);
90
91 # Activate next tab
92 command_K_gt = (vim) ->
93 if rootWindow = utils.getRootWindow vim.window
94 rootWindow.gBrowser.tabContainer.advanceSelectedTab(1, true);
95
96 # Go to the first tab
97 command_gH_g0 = (vim) ->
98 if rootWindow = utils.getRootWindow vim.window
99 rootWindow.gBrowser.tabContainer.selectedIndex = 0;
100
101 # Go to the last tab
102 command_gL_g$ = (vim) ->
103 if rootWindow = utils.getRootWindow vim.window
104 itemCount = rootWindow.gBrowser.tabContainer.itemCount;
105 rootWindow.gBrowser.tabContainer.selectedIndex = itemCount - 1;
106
107 # Go back in history
108 command_H = (vim) ->
109 vim.window.history.back()
110
111 # Go forward in history
112 command_L = (vim) ->
113 vim.window.history.forward()
114
115 # Close current tab
116 command_x = (vim) ->
117 if rootWindow = utils.getRootWindow vim.window
118 rootWindow.gBrowser.removeCurrentTab()
119
120 # Restore last closed tab
121 command_X = (vim) ->
122 if rootWindow = utils.getRootWindow vim.window
123 ss = utils.getSessionStore()
124 if ss and ss.getClosedTabCount(rootWindow) > 0
125 ss.undoCloseTab rootWindow, 0
126
127 # Follow links with hint markers
128 command_f = (vim) ->
129 vim.markers = injectHints vim.window.document
130 if vim.markers.length > 0
131 # This callback will be called with the selected marker as argument
132 vim.cb = (marker) ->
133 marker.element.focus()
134 utils.simulateClick marker.element
135
136 vim.enterHintsMode()
137
138 # Follow links in a new Tab with hint markers
139 command_F = (vim) ->
140 vim.markers = injectHints vim.window.document
141 if vim.markers.length > 0
142 # This callback will be called with the selected marker as argument
143 vim.cb = (marker) ->
144 marker.element.focus()
145 utils.simulateClick marker.element, { metaKey: true, ctrlKey: true }
146
147 vim.enterHintsMode()
148
149 # Display the Help Dialog
150 command_help = (vim) ->
151 showHelp vim.window.document, commandsHelp
152
153 command_Esc = (vim) ->
154 # Blur active element if it's editable. Other elements
155 # aren't blurred - we don't want to interfere with
156 # the browser too much
157 activeElement = vim.window.document.activeElement
158 if utils.isElementEditable activeElement
159 activeElement.blur()
160
161 # Remove hints
162 removeHints vim.window.document
163 # Hide help dialog
164 hideHelp vim.window.document
165 # Finally enter normal mode
166 vim.enterNormalMode()
167
168 commandGroups =
169 'urls':
170 'p': [ command_p, "Navigate to the address in the clipboard" ]
171 'P': [ command_P, "Open new tab and navigate to the address in the clipboard" ]
172 'y,f': [ command_yf, "Copy link url to the clipboard" ]
173 'y,y': [ command_yy, "Copy current page link to the clipboard" ]
174 'r': [ command_r, "Reload current page" ]
175 'R': [ command_R, "Reload current page and all the assets (js, css, etc.)" ]
176 'nav':
177 'g,g': [ command_gg , "Scroll to the Top of the page" ]
178 'G': [ command_G, "Scroll to the Bottom of the page" ]
179 'j|c-e': [ command_j_ce, "Scroll Left" ]
180 'k|c-y': [ command_k_cy, "Scroll Right" ]
181 'h': [ command_h, "Scroll Down" ]
182 'l': [ command_l , "Scroll Up" ]
183 'd|c-d': [ command_d_cd, "Scroll a Page Down" ]
184 'u|c-u': [ command_u_cu, "Scroll a Page Up" ]
185 'tabs':
186 't': [ command_t, "Open New Blank tab" ]
187 'J|g,T': [ command_J_gT, "Go to the Previous tab" ]
188 'K|g,t': [ command_K_gt, "Go to the Next tab" ]
189 'g,H|g,0': [ command_gH_g0, "Go to the First tab" ]
190 'g,L|g,$': [ command_gL_g$, "Go to the Last tab" ]
191 'x': [ command_x, "Close current tab" ]
192 'X': [ command_X, "Restore last closed tab" ]
193 'browse':
194 'f': [ command_f, "Follow a link on the current page" ]
195 'F': [ command_F, "Follow a link on the current page in a new tab" ]
196 'H': [ command_H, "Go Back in history" ]
197 'L': [ command_L, "Go Forward in history" ]
198 'misc':
199 '?': [ command_help, "Show Help Dialog" ]
200 'Esc': [ command_Esc, "Close this dialog and cancel hint markers" ]
201
202 # Merge groups and split command pipes into individual commands
203 commands = do (commandGroups) ->
204 newCommands = {}
205 for group, commandsList of commandGroups
206 for keys, command of commandsList
207 for key in keys.split '|'
208 newCommands[key] = command[0]
209
210 return newCommands
211
212 # Extract the help text from the commands preserving groups formation
213 commandsHelp = do (commandGroups) ->
214 help = {}
215 for group, commandsList of commandGroups
216 helpGroup = {}
217 for keys, command of commandsList
218 key = keys.replace(',', '').replace('|', ', ')
219 helpGroup[key] = command[1]
220
221 help[group] = helpGroup
222 return help
223
224 # Called in hints mode. Will process the char, update and hide/show markers
225 hintCharHandler = (vim, char) ->
226 for marker in vim.markers
227 marker.matchHintChar char
228
229 if marker.isMatched()
230 vim.cb marker
231 removeHints vim.window.document
232 vim.enterNormalMode()
233 break
234
235 exports.hintCharHandler = hintCharHandler
236 exports.commands = commands
237 exports.commandsHelp = commandsHelp
Imprint / Impressum