Skip to content

Commit 52b7f94

Browse files
amlitzermaxauthority
authored andcommitted
Canonicalize commandline history during matching. (#587)
This allows finding all commands regardless of which command name was used. E.g. :js <Up> matches on :js, :javas and :javascript. We special case :open/:tabopen/:winopen, which which are treated the same. Allowing e.g. :open retrieve URLs that were previously used with :tabopen.
1 parent c045269 commit 52b7f94

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

common/content/browser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ const Browser = Module("browser", {
238238
else
239239
liberator.open("");
240240
}, {
241+
canonicalize: function (cmd) cmd.replace(/^(op?|open?)\b/, 'open'),
241242
completer: function (context) completion.url(context),
242243
literal: 0,
243244
privateData: true

common/content/commandline.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,21 @@ const CommandLine = Module("commandline", {
10871087
this.store.push({ value: str, timestamp: Date.now(), privateData: this.checkPrivate(str) });
10881088
this.store.truncate(options.history, true);
10891089
},
1090+
/**
1091+
* @property {function} Returns a canonical form of the argument that
1092+
* can be used for comparison.
1093+
*/
1094+
canonicalize: function (str) {
1095+
// Not really the ideal place for this check.
1096+
if (this.mode != "command")
1097+
return str;
1098+
1099+
let cmd = (commands.get(commands.parseCommand(str)[1]) || {});
1100+
if (cmd.canonicalize != undefined)
1101+
return cmd.canonicalize(str);
1102+
1103+
return str;
1104+
},
10901105
/**
10911106
* @property {function} Returns whether a data item should be
10921107
* considered private.
@@ -1140,6 +1155,7 @@ const CommandLine = Module("commandline", {
11401155

11411156
// search the this._history for the first item matching the current
11421157
// commandline string
1158+
let canonical_original = this.canonicalize(this.original);
11431159
while (true) {
11441160
this.index += diff;
11451161
if (this.index < 0 || this.index > this.store.length) {
@@ -1162,8 +1178,10 @@ const CommandLine = Module("commandline", {
11621178
else
11631179
hist = (hist.value || hist);
11641180

1165-
if (!matchCurrent || hist.substr(0, this.original.length) == this.original) {
1166-
this.replace(hist);
1181+
let canonical_hist = this.canonicalize(hist);
1182+
if (!matchCurrent ||
1183+
canonical_hist.substr(0, canonical_original.length) == canonical_original) {
1184+
this.replace(this.original + canonical_hist.substr(canonical_original.length));
11671185
break;
11681186
}
11691187
}

common/content/commands.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@
2222
* @param {function} action The action invoked by this command when executed.
2323
* @param {Object} extraInfo An optional extra configuration hash. The
2424
* following properties are supported.
25-
* argCount - see {@link Command#argCount}
26-
* bang - see {@link Command#bang}
27-
* completer - see {@link Command#completer}
28-
* count - see {@link Command#count}
29-
* heredoc - see {@link Command#heredoc}
30-
* literal - see {@link Command#literal}
31-
* options - see {@link Command#options}
32-
* serial - see {@link Command#serial}
33-
* privateData - see {@link Command#privateData}
25+
* argCount - see {@link Command#argCount}
26+
* bang - see {@link Command#bang}
27+
* canonicalize - see {@link Command#canonicalize}
28+
* completer - see {@link Command#completer}
29+
* count - see {@link Command#count}
30+
* heredoc - see {@link Command#heredoc}
31+
* literal - see {@link Command#literal}
32+
* options - see {@link Command#options}
33+
* serial - see {@link Command#serial}
34+
* privateData - see {@link Command#privateData}
3435
* @optional
3536
* @private
3637
*/
@@ -158,6 +159,13 @@ const Command = Class("Command", {
158159
* @see Commands#parseArgs
159160
*/
160161
argCount: 0,
162+
/**
163+
* @property {function (string)} Transforms command line string to its
164+
* canonical form.
165+
*/
166+
canonicalize: function(cmd) {
167+
return cmd.replace(new RegExp('^(' + this.names.join('|') + ')\\b'), this.name);
168+
},
161169
/**
162170
* @property {function (CompletionContext, Args)} This command's completer.
163171
* @see CompletionContext

common/content/tabs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,7 @@ const Tabs = Module("tabs", {
879879
liberator.open("", { where: where });
880880
}, {
881881
bang: true,
882+
canonicalize: function (cmd) cmd.replace(/^(to?|tope?|topen|tabopen|tabnew)\b/, 'open'),
882883
completer: function (context) completion.url(context),
883884
literal: 0,
884885
privateData: true

vimperator/content/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ var Config = Module("config", ConfigBase, {
249249
options: [
250250
[["-private", "-p"], commands.OPTION_NOARG],
251251
],
252+
canonicalize: function (cmd) cmd.replace(/^(winop?|winopen?)\b/, 'open'),
252253
completer: function (context) completion.url(context),
253254
literal: 0,
254255
privateData: true

0 commit comments

Comments
 (0)