]> git.gir.st - VimFx.git/blob - extension/packages/commands.coffee
Closes #14. Now if the newly entered character doesn't match any markers then it...
[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 # Display the Help Dialog
154 command_help = (vim) ->
155 showHelp vim.window.document, commandsHelp
156
157 command_Esc = (vim) ->
158 # Blur active element if it's editable. Other elements
159 # aren't blurred - we don't want to interfere with
160 # the browser too much
161 activeElement = vim.window.document.activeElement
162 if utils.isElementEditable activeElement
163 activeElement.blur()
164
165 # Remove hints
166 removeHints vim.window.document
167 # Hide help dialog
168 hideHelp vim.window.document
169 # Finally enter normal mode
170 vim.enterNormalMode()
171
172 commandGroups =
173 'urls':
174 'p': [ command_p, "Navigate to the address in the clipboard" ]
175 'P': [ command_P, "Open new tab and navigate to the address in the clipboard" ]
176 'y,f': [ command_yf, "Copy link url to the clipboard" ]
177 'y,y': [ command_yy, "Copy current page link to the clipboard" ]
178 'r': [ command_r, "Reload current page" ]
179 'R': [ command_R, "Reload current page and all the assets (js, css, etc.)" ]
180 'nav':
181 'g,g': [ command_gg , "Scroll to the Top of the page" ]
182 'G': [ command_G, "Scroll to the Bottom of the page" ]
183 'j|c-e': [ command_j_ce, "Scroll Down" ]
184 'k|c-y': [ command_k_cy, "Scroll Up" ]
185 'h': [ command_h, "Scroll Left" ]
186 'l': [ command_l , "Scroll Right" ]
187 'd': [ command_d, "Scroll a Page Down" ]
188 'u': [ command_u, "Scroll a Page Up" ]
189 'tabs':
190 't': [ command_t, "Open New Blank tab" ]
191 'J|g,T': [ command_J_gT, "Go to the Previous tab" ]
192 'K|g,t': [ command_K_gt, "Go to the Next tab" ]
193 'g,H|g,0': [ command_gH_g0, "Go to the First tab" ]
194 'g,L|g,$': [ command_gL_g$, "Go to the Last tab" ]
195 'x': [ command_x, "Close current tab" ]
196 'X': [ command_X, "Restore last closed tab" ]
197 'browse':
198 'f': [ command_f, "Follow a link on the current page" ]
199 'F': [ command_F, "Follow a link on the current page in a new tab" ]
200 'H': [ command_H, "Go Back in history" ]
201 'L': [ command_L, "Go Forward in history" ]
202 'misc':
203 '?': [ command_help, "Show Help Dialog" ]
204 'Esc': [ command_Esc, "Close this dialog and cancel hint markers" ]
205
206 # Merge groups and split command pipes into individual commands
207 commands = do (commandGroups) ->
208 newCommands = {}
209 for group, commandsList of commandGroups
210 for keys, command of commandsList
211 for key in keys.split '|'
212 newCommands[key] = command[0]
213
214 return newCommands
215
216 # Extract the help text from the commands preserving groups formation
217 commandsHelp = do (commandGroups) ->
218 help = {}
219 for group, commandsList of commandGroups
220 helpGroup = {}
221 for keys, command of commandsList
222 key = keys.replace(',', '').replace('|', ', ')
223 helpGroup[key] = command[1]
224
225 help[group] = helpGroup
226 return help
227
228 # Called in hints mode. Will process the char, update and hide/show markers
229 hintCharHandler = (vim, char) ->
230 # First count how many markers will match with the new character entered
231 preMatch = vim.markers.reduce ((v, marker) -> v + marker.willMatch char), 0
232
233 # If prematch is greater than 0, then proceed with matching, else ignore the new char
234 if preMatch > 0
235 for marker in vim.markers
236 marker.matchHintChar char
237
238 if marker.isMatched()
239 vim.cb marker
240 removeHints vim.window.document
241 vim.enterNormalMode()
242 break
243
244 exports.hintCharHandler = hintCharHandler
245 exports.commands = commands
246 exports.commandsHelp = commandsHelp
Imprint / Impressum