From d5439d0bdc7886fdc913963dfb5d316254e0fe0f Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Fri, 5 Dec 2008 08:45:43 -0500 Subject: [PATCH] Complete :bmarkgit diff properly --- common/content/commands.js | 8 ++++++-- common/content/completion.js | 19 +++++++++++++---- vimperator/content/bookmarks.js | 36 ++++++++++++++++++++++++++++++--- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/common/content/commands.js b/common/content/commands.js index 7c521729..c5bc166c 100644 --- a/common/content/commands.js +++ b/common/content/commands.js @@ -143,7 +143,7 @@ Command.prototype = { return false; }, - parseArgs: function (args, complete) commands.parseArgs(args, this.options, this.argCount, false, this.literal, complete) + parseArgs: function (args, complete, extra) commands.parseArgs(args, this.options, this.argCount, false, this.literal, complete, extra) }; //}}} @@ -319,7 +319,7 @@ function Commands() //{{{ // @param allowUnknownOptions: -foo won't result in an error, if -foo isn't // specified in "options" // TODO: should it handle comments? - parseArgs: function (str, options, argCount, allowUnknownOptions, literal, complete) + parseArgs: function (str, options, argCount, allowUnknownOptions, literal, complete, extra) { // returns [count, parsed_argument] function getNextArg(str) @@ -421,6 +421,10 @@ function Commands() //{{{ args.string = str; // for access to the unparsed string args.literalArg = ""; + // FIXME! + for (let [k, v] in Iterator(extra || [])) + args[k] = v; + var invalid = false; // FIXME: best way to specify these requirements? var onlyArgumentsRemaining = allowUnknownOptions || options.length == 0 || false; // after a -- has been found diff --git a/common/content/completion.js b/common/content/completion.js index 4a4a19d9..60a5bd3d 100644 --- a/common/content/completion.js +++ b/common/content/completion.js @@ -452,9 +452,9 @@ CompletionContext.prototype = { catch (e) {} }, - match: function match(str) + // FIXME + _match: function _match(filter, str) { - let filter = this.filter; if (this.ignoreCase) { filter = filter.toLowerCase(); @@ -465,6 +465,11 @@ CompletionContext.prototype = { return str.indexOf(filter) > -1; }, + match: function match(str) + { + return this._match(this.filter, str); + }, + reset: function reset() { let self = this; @@ -1172,10 +1177,16 @@ function Completion() //{{{ context.completions = config.autocommands; }, - bookmark: function bookmark(context, tags) + bookmark: function bookmark(context, tags, extra) { context.title = ["Bookmark", "Title"]; context.format = bookmarks.format; + for (let val in Iterator(extra || [])) + { + let [k, v] = val; // Need let block here for closure. + if (v) + context.filters.push(function (item) this._match(v, this.getKey(item, k))); + } // Need to make a copy because set completions() checks instanceof Array, // and this may be an Array from another window. context.completions = Array.slice(storage["bookmark-cache"].bookmarks); @@ -1279,7 +1290,7 @@ function Completion() //{{{ [prefix] = context.filter.match(/^(?:\w*[\s!]|!)\s*/); let cmdContext = context.fork(cmd, prefix.length); let argContext = context.fork("args", prefix.length); - args = command.parseArgs(cmdContext.filter, argContext); + args = command.parseArgs(cmdContext.filter, argContext, { count: count, bang: bang }); if (args) { // FIXME: Move to parseCommand diff --git a/vimperator/content/bookmarks.js b/vimperator/content/bookmarks.js index 49cef5f9..9c6b2651 100644 --- a/vimperator/content/bookmarks.js +++ b/vimperator/content/bookmarks.js @@ -278,16 +278,37 @@ function Bookmarks() //{{{ }, { argCount: "0" }); + // TODO: Clean this up. function tags(context, args) { let filter = context.filter; let have = filter.split(","); + args.completeFilter = have.pop(); + let prefix = filter.substr(0, filter.length - args.completeFilter.length); let tags = util.Array.uniq(util.Array.flatten([b.tags for ([k, b] in Iterator(cache.bookmarks))])); + return [[prefix + tag, tag] for ([i, tag] in Iterator(tags)) if (have.indexOf(tag) < 0)]; } + function title(context, args) + { + if (!args.bang) + return [[content.document.title, "Current Page Title"]]; + context.keys.text = "title"; + context.keys.description = "url"; + return bookmarks.get(args.join(" "), args["-tags"], null, { keyword: args["-keyword"], title: context.filter }); + } + + function keyword(context, args) + { + if (!args.bang) + return []; + context.keys.text = "keyword"; + return bookmarks.get(args.join(" "), args["-tags"], null, { keyword: context.filter, title: args["-title"] }); + } + commands.add(["bma[rk]"], "Add a bookmark", function (args) @@ -308,7 +329,16 @@ function Bookmarks() //{{{ { argCount: "?", bang: true, - options: [[["-title", "-t"], commands.OPTION_STRING, null, function () [[content.document.title, "Current Page Title"]]], + completer: function (context, args) + { + if (!args.bang) + { + context.completions = [[content.document.documentURI, "Current Location"]]; + return + } + completion.bookmark(context, args["-tags"], { keyword: args["-keyword"], title: args["-title"] }); + }, + options: [[["-title", "-t"], commands.OPTION_STRING, null, title], [["-tags", "-T"], commands.OPTION_LIST, null, tags], [["-keyword", "-k"], commands.OPTION_STRING, function (arg) /\w/.test(arg)]] }); @@ -361,9 +391,9 @@ function Bookmarks() //{{{ // if "bypassCache" is true, it will force a reload of the bookmarks database // on my PC, it takes about 1ms for each bookmark to load, so loading 1000 bookmarks // takes about 1 sec - get: function get(filter, tags, maxItems) + get: function get(filter, tags, maxItems, extra) { - return completion.runCompleter("bookmark", filter, maxItems, tags); + return completion.runCompleter("bookmark", filter, maxItems, tags, extra); }, // if starOnly = true it is saved in the unfiledBookmarksFolder, otherwise in the bookmarksMenuFolder