[Vimperator] Addon search keywords

Kipling Inscore k at bijna.net
Wed Jun 25 09:43:44 PDT 2008


New patch below. because getShortcutOrURI() takes only one string
argument, I modified getSearchURL() so that the engine name and search
terms don't have to be split and the second argument is a boolean to
use defsearch. I modified stringToURLArray() (the only function I
found that calls getSearchURL()) accordingly and moved the file check
to after the calls to getSearchURL(), as I don't think the comment
about speed still applies.
I think I handle post data correctly now but I don't have any search
aliases that use post data. Can someone who does test or give me (a
link to download/install) a search alias with post data?

No modifications (yet) to suggestions. I think alias/keyword handling
should be moved as much as possible (without changing vim bar
behavior) to the Firefox location bar handlers, as that's what I see
being emulated now, but I haven't yet found how Firefox creates its
suggestion list. Maybe with these changes vimperator won't need to
have its own arrays of aliases and keywords.

Index: src/content/bookmarks.js
===================================================================
RCS file: /cvs/vimperator/src/content/bookmarks.js,v
retrieving revision 1.52
diff -u -r1.52 bookmarks.js
--- src/content/bookmarks.js    17 Jun 2008 00:31:38 -0000      1.52
+++ src/content/bookmarks.js    25 Jun 2008 16:18:29 -0000
@@ -359,50 +359,18 @@
         // if @param engineName is null, it uses the default search engine
         // @returns the url for the search string
         //          if the search also requires a postData, [url,
postData] is returned
-        getSearchURL: function (text, engineName)
+        getSearchURL: function (text, useDefsearch)
         {
             var url = null;
-            var postData = null;
-            if (!engineName)
-                engineName = liberator.options["defsearch"];
-
-            // we need to make sure our custom alias have been set,
even if the user
-            // did not :open <tab> once before
-            this.getSearchEngines();
-
-            // first checks the search engines for a match
-            var engine = searchService.getEngineByAlias(engineName);
-            if (engine)
-            {
-                if (text)
-                {
-                    var submission = engine.getSubmission(text, null);
-                    url = submission.uri.spec;
-                    postData = submission.postData;
-                }
-                else
-                    url = engine.searchForm;
-            }
-            else // check for keyword urls
-            {
-                if (!keywords)
-                    load();
+            var aPostDataRef = null;
+            var searchString = (useDefsearch?
liberator.options["defsearch"] + " " : "") + text;

-                for (var i in keywords)
-                {
-                    if (keywords[i][0] == engineName)
-                    {
-                        if (text == null)
-                            text = "";
-                        url = keywords[i][2].replace(/%s/g,
encodeURIComponent(text));
-                        break;
-                    }
-                }
-            }
+            url = getShortcutOrURI(searchString, aPostDataRef);
+            if (url == searchString)
+                url = null;

-            // if we came here, the engineName is neither a search
engine or URL
-            if (postData)
-                return [url, postData];
+            if (aPostDataRef && aPostDataRef.value)
+                return [url, aPostDataRef.value];
             else
                 return url; // can be null
         },
Index: src/content/util.js
===================================================================
RCS file: /cvs/vimperator/src/content/util.js,v
retrieving revision 1.37
diff -u -r1.37 util.js
--- src/content/util.js 6 Jun 2008 11:36:51 -0000       1.37
+++ src/content/util.js 25 Jun 2008 16:11:41 -0000
@@ -285,40 +285,27 @@
             // strip each 'URL' - makes things simpler later on
             urls[url] = urls[url].replace(/^\s+/, "").replace(/\s+$/, "");

-            // first check if it is an existing local file but NOT a
search url/keyword
-            // NOTE: the test for being a file is done first, because
it's faster than getSearchURL
-            var file = liberator.io.getFile(urls[url]);
-            if (file.exists() && file.isReadable() &&
!liberator.bookmarks.getSearchURL("", urls[url]))
-            {
-                urls[url] = file.path;
-                continue;
-            }
-
             // if the string doesn't look like a valid URL (i.e.
contains a space
             // or does not contain any of: .:/) try opening it with a
search engine
             // or keyword bookmark
-            if (liberator.has("bookmarks") && (/\s/.test(urls[url])
|| !/[.:\/]/.test(urls[url])))
+            if (/\s/.test(urls[url]) || !/:\/\/]/.test(urls[url]))
             {
-                var matches = urls[url].match(/^(\S+)(?:\s+(.+))?$/);
-                var alias = matches[1];
-                var text = matches[2] || null;
-
                 // TODO: it would be clearer if the appropriate call to
                 // getSearchURL was made based on whether or not the
first word was
                 // indeed an SE alias rather than seeing if getSearchURL can
                 // process the call usefully and trying again if it
fails - much
                 // like the comments below ;-)

-                // check if the first word is a search engine
-                var searchURL = liberator.bookmarks.getSearchURL(text, alias);
+                // check for a search engine match in the string
+                var searchURL =
liberator.bookmarks.getSearchURL(urls[url], false);
                 if (searchURL)
                 {
                     urls[url] = searchURL;
                     continue;
                 }
-                else // the first word was not a search engine,
search for the whole string in the default engine
+                else // no search engine match, search for the whole
string in the default engine
                 {
-                    searchURL =
liberator.bookmarks.getSearchURL(urls[url], null);
+                    searchURL =
liberator.bookmarks.getSearchURL(urls[url], true);
                     if (searchURL)
                     {
                         urls[url] = searchURL;
@@ -327,6 +314,13 @@
                 }
             }

+            var file = liberator.io.getFile(urls[url]);
+            if (file.exists() && file.isReadable())
+            {
+                urls[url] = file.path;
+                continue;
+            }
+
             // if we are here let Firefox handle the url and hope it does
             // something useful with it :)
         }


More information about the Vimperator mailing list