[Vimperator] Search engine aliases

Nikolai Weibull now at bitwi.se
Thu Apr 3 01:35:05 PDT 2008


On Thu, Mar 27, 2008 at 3:57 PM, Dmitry Teslenko <dteslenko at gmail.com> wrote:
> I get used to invoking search engines via vimperator command line.

Here's a plugin that I wrote for 0.5.*:

(function () {

const oldGetSearchURL = vimperator.bookmarks.getSearchURL;
const keywords = {
  mark: 'http://del.icio.us/post?v=4;url=%h;title=%t',
  del: 'http://del.icio.us/search?all=%s',
  "'": 'http://del.icio.us/now/%s',
  '#': 'http://hitta.se/default.aspx?UCSB%%3aTextBoxWho=%s&SearchType=4&UCSB%%3aWflWhite=1a1b&UCSB%%3aWflPink=4a',
  addr: 'http://hitta.se/SearchCombi.aspx?var=%s',
  movie: 'http://imdb.com/find?q=%s',
  posix: 'http://opengroup.org/cgi-bin/kman2?value=%s',
  wps: 'http://en.wikipedia.org/wiki/Special:Search?search=%s&fulltext=Search',
  wp: 'http://en.wikipedia.org/wiki/%Ws',
  '/': 'http://google.com/search?q=%s',
  archived: 'http://web.archive.org/web/*/%h',
  cite: 'http://citeseer.ist.psu.edu/cis?q=%s&submit=Search+Documents',
  cookies: "javascript:alert('Cookies stored by this host or
domain:\\n\\n' + document.cookie.replace(/; /g, '\\n'))",
  d: 'http://discogs.com/search?type=all&q=%s',
  '/d': 'http://google.com/search?q=%s+site:discogs.com',
  dict: 'http://wordnet.princeton.edu/perl/webwn?s=%s',
  login: 'http://bugmenot.com/view.php?mode=bookmarklet&url=%h',
  math: 'http://planetmath.org/?op=search&term=%s',
  q: 'http://answers.com/%s',
  related: 'http://google.com/search?related:%Lh',
  '/s': 'http://google.com/search?q=%s+site:%Lh',
  '/c': 'http://google.com/codesearch?q=%s'
};

vimperator.bookmarks.getSearchURL = function(text, keyword) {
  text = (text == null ? "" : text);
  var url = keywords[keyword];
  if (url) {
    var words = text.split(/\s+/)
    var expanded = [];
    var add_javascript = false;

    for (var i = 0; i < url.length; i++) {
      switch (url[i]) {
      case '%':
        var position = [];
        if (url[i + 1].match(/^\d$/)) {
          while (url[i + 1].match(/^\d$/))
            position.push(url[++i]);
          position = parseInt(position.join("")) - 1;
        } else {
          position = "";
        }

        var modifier = "";
        if (url[i + 1].match(/^[ULCW]$/))
          modifier = url[++i];

        var quote = false;
        switch (url[++i]) {
        case 's':
          var component = "";
          if (position === "")
            component = text;
          else
            component = (position < words.length ? words[position] : "");

          switch (modifier) {
          case 'U':
            component = component.toUpperCase();
            break;
          case 'L':
            component = component.toLowerCase();
            break;
          case 'C':
            component = component.split(/\s+/).map(function(s){ if
(s.length == 0) return ""; return s[0].toUpperCase() +
s.slice(1).toLowerCase(); }).join(' ');
            break;
          case 'W':
            component = component.split(/\s+/).map(function(s){ if
(s.length == 0) return ""; return s[0].toUpperCase() +
s.slice(1).toLowerCase(); }).join('_');
            break;
          }

          expanded.push(encodeURIComponent(component));
          break;
        case 'h':
          add_javascript = true;
          quote = true;
          if (modifier == 'L')
            expanded.push("'+encodeURIComponent(location.href.replace(/^(?:http:\\/\\/)?([^\\/]+).*/,
'$1'))");
          else
            expanded.push("'+encodeURIComponent(location.href)");
          break;
        case 't':
          add_javascript = true;
          quote = true;
          expanded.push("'+encodeURIComponent(document.title)");
          break;
        case '%':
          expanded.push('%');
          break;
        default:
          expanded.push('%');
          expanded.push(position + 1);
          expanded.push(modifier);
          expanded.push(url[i]);
          break;
        }
        if (quote && i < url.length - 1)
          expanded.push("+'");
        break;
      default:
        expanded.push(url[i]);
        break;
      }
    }

    if (add_javascript)
      expanded.unshift("javascript:location.href='");

    expanded = expanded.join("");
    return expanded;
  }

  return oldGetSearchURL(text, keyword);
}

function add(keyword, url) {
  keywords[keyword] = url;
}

})();

It can surely be made more efficient and the definition of keywords
should be a command that you can run in your .vimperatorrc.  Also, I
have no idea if this works with 0.6.*.

Here's some documentation:

%[<integer>][ULCW]s

  Substitute with the <integer>:th word.  If <integer> isn't given,
get whole text.
  The following modifiers may also be specified:

  U

    Uppercase the substitution

  L

    Lowercase the substitution

  C

    Capitalize the substitution

  W

    Wikify the substitution, that is, capitalize each word and join
them with lowlines

%t

  Substitute with the title of the current document.

%[L]h

  Substitute with the current document's location.
  The following modifiers may also be specified:

  L

    Substitute with only the domain name of the current document's location


More information about the Vimperator mailing list