forked from philc/vimium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.js
239 lines (192 loc) · 8.02 KB
/
commands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
var availableCommands = {};
var keyToCommandRegistry = {};
/*
* Registers a command, making it available to be optionally bound to a key.
* options:
* - background: whether this command needs to be run against the background page.
* - passCountToFunction: true if this command should have any digits which were typed prior to the
* command passed to it. This is used to implement e.g. "closing of 3 tabs".
*/
function addCommand(command, description, options) {
if (availableCommands[command]) {
console.log(command, "is already defined! Check commands.js for duplicates.");
return;
}
options = options || {};
availableCommands[command] = { description: description,
isBackgroundCommand: options.background,
passCountToFunction: options.passCountToFunction
};
}
function mapKeyToCommand(key, command) {
if (!availableCommands[command]) {
console.log(command, "doesn't exist!");
return;
}
keyToCommandRegistry[key] = { command: command,
isBackgroundCommand: availableCommands[command].isBackgroundCommand,
passCountToFunction: availableCommands[command].passCountToFunction
};
}
function unmapKey(key) { delete keyToCommandRegistry[key]; }
/* Lower-case the appropriate portions of named keys.
*
* A key name is one of three forms exemplified by <c-a> <left> or <c-f12>
* (prefixed normal key, named key, or prefixed named key). Internally, for
* simplicity, we would like prefixes and key names to be lowercase, though
* humans may prefer other forms <Left> or <C-a>.
* On the other hand, <c-a> and <c-A> are different named keys - for one of
* them you have to press "shift" as well.
*/
function normalizeKey(key) {
return key.replace(/<[acm]-/ig, function(match){ return match.toLowerCase(); })
.replace(/<([acm]-)?([a-zA-Z0-9]{2,5})>/g, function(match, optionalPrefix, keyName){
return "<" + ( optionalPrefix ? optionalPrefix : "") + keyName.toLowerCase() + ">";
});
}
function parseCustomKeyMappings(customKeyMappings) {
lines = customKeyMappings.split("\n");
for (var i = 0; i < lines.length; i++) {
if (lines[i][0] == "\"" || lines[i][0] == "#") { continue }
split_line = lines[i].split(/\s+/);
var lineCommand = split_line[0];
if (lineCommand == "map") {
if (split_line.length != 3) { continue; }
var key = normalizeKey(split_line[1]);
var vimiumCommand = split_line[2];
if (!availableCommands[vimiumCommand]) { continue }
console.log("Mapping", key, "to", vimiumCommand);
mapKeyToCommand(key, vimiumCommand);
}
else if (lineCommand == "unmap") {
if (split_line.length != 2) { continue; }
var key = normalizeKey(split_line[1]);
console.log("Unmapping", key);
unmapKey(key);
}
else if (lineCommand == "unmapAll") {
keyToCommandRegistry = {};
}
}
}
function clearKeyMappingsAndSetDefaults() {
keyToCommandRegistry = {};
var defaultKeyMappings = {
"?": "showHelp",
"j": "scrollDown",
"k": "scrollUp",
"h": "scrollLeft",
"l": "scrollRight",
"gg": "scrollToTop",
"G": "scrollToBottom",
"zH": "scrollToLeft",
"zL": "scrollToRight",
"<c-e>": "scrollDown",
"<c-y>": "scrollUp",
"d": "scrollPageDown",
"u": "scrollPageUp",
"r": "reload",
"gs": "toggleViewSource",
"i": "enterInsertMode",
"H": "goBack",
"L": "goForward",
"gu": "goUp",
"gi": "focusInput",
"f": "linkHints.activateMode",
"F": "linkHints.activateModeToOpenInNewTab",
"<a-f>": "linkHints.activateModeWithQueue",
"/": "enterFindMode",
"n": "performFind",
"N": "performBackwardsFind",
"[[": "goPrevious",
"]]": "goNext",
"yy": "copyCurrentUrl",
"yf": "linkHints.activateModeToCopyLinkUrl",
"K": "nextTab",
"J": "previousTab",
"gt": "nextTab",
"gT": "previousTab",
"t": "createTab",
"x": "removeTab",
"X": "restoreTab",
"b": "activateBookmarkFindMode",
"B": "activateBookmarkFindModeToOpenInNewTab",
"gf": "nextFrame"
};
for (var key in defaultKeyMappings)
mapKeyToCommand(key, defaultKeyMappings[key]);
}
// This is a mapping of: commandIdentifier => [description, options].
var commandDescriptions = {
// Navigating the current page
showHelp: ["Show help", { background: true }],
scrollDown: ["Scroll down"],
scrollUp: ["Scroll up"],
scrollLeft: ["Scroll left"],
scrollRight: ["Scroll right"],
scrollToTop: ["Scroll to the top of the page"],
scrollToBottom: ["Scroll to the bottom of the page"],
scrollToLeft: ["Scroll all the way to the left"],
scrollToRight: ["Scroll all the way to the right"],
scrollPageDown: ["Scroll a page down"],
scrollPageUp: ["Scroll a page up"],
scrollFullPageDown: ["Scroll a full page down"],
scrollFullPageUp: ["Scroll a full page up"],
reload: ["Reload the page"],
toggleViewSource: ["View page source"],
copyCurrentUrl: ["Copy the current URL to the clipboard"],
'linkHints.activateModeToCopyLinkUrl': ["Copy a link URL to the clipboard"],
enterInsertMode: ["Enter insert mode"],
focusInput: ["Focus the first (or n-th) text box on the page", { passCountToFunction: true }],
'linkHints.activateMode': ["Open a link in the current tab"],
'linkHints.activateModeToOpenInNewTab': ["Open a link in a new tab"],
'linkHints.activateModeWithQueue': ["Open multiple links in a new tab"],
enterFindMode: ["Enter find mode"],
performFind: ["Cycle forward to the next find match"],
performBackwardsFind: ["Cycle backward to the previous find match"],
goPrevious: ["Follow the link labeled previous or <"],
goNext: ["Follow the link labeled next or >"],
// Navigating your history
goBack: ["Go back in history"],
goForward: ["Go forward in history"],
// Navigating the URL hierarchy
goUp: ["Go up the URL hierarchy", { passCountToFunction: true }],
// Manipulating tabs
nextTab: ["Go one tab right", { background: true }],
previousTab: ["Go one tab left", { background: true }],
createTab: ["Create new tab", { background: true }],
removeTab: ["Close current tab", { background: true }],
restoreTab: ["Restore closed tab", { background: true }],
activateBookmarkFindMode: ["Open a bookmark in the current tab"],
activateBookmarkFindModeToOpenInNewTab: ["Open a bookmark in a new tab"],
nextFrame: ["Cycle forward to the next frame on the page", { background: true, passCountToFunction: true }]
};
for (var command in commandDescriptions)
addCommand(command, commandDescriptions[command][0], commandDescriptions[command][1]);
// An ordered listing of all available commands, grouped by type. This is the order they will
// be shown in the help page.
var commandGroups = {
pageNavigation:
["scrollDown", "scrollUp", "scrollLeft", "scrollRight",
"scrollToTop", "scrollToBottom", "scrollToLeft", "scrollToRight", "scrollPageDown",
"scrollPageUp", "scrollFullPageUp", "scrollFullPageDown",
"reload", "toggleViewSource", "copyCurrentUrl", "linkHints.activateModeToCopyLinkUrl", "goUp",
"enterInsertMode", "focusInput",
"linkHints.activateMode", "linkHints.activateModeToOpenInNewTab", "linkHints.activateModeWithQueue",
"activateBookmarkFindMode", "activateBookmarkFindModeToOpenInNewTab",
"goPrevious", "goNext", "nextFrame"],
findCommands: ["enterFindMode", "performFind", "performBackwardsFind"],
historyNavigation:
["goBack", "goForward"],
tabManipulation:
["nextTab", "previousTab", "createTab", "removeTab", "restoreTab"],
misc:
["showHelp"]
};
// Rarely used commands are not shown by default in the help dialog or in the README. The goal is to present
// a focused, high-signal set of commands to the new and casual user. Only those truly hungry for more power
// from Vimium will uncover these gems.
var advancedCommands = [
"scrollToLeft", "scrollToRight",
"goUp", "focusInput", "linkHints.activateModeWithQueue",
"goPrevious", "goNext"];