From c6d67fe8fedd3281dda19f640018aeee78230820 Mon Sep 17 00:00:00 2001 From: Martin Stubenschrott Date: Fri, 4 May 2007 18:04:11 +0000 Subject: [PATCH] keyword support for bookmarks --- ChangeLog | 5 ++ Donators | 2 + Makefile | 8 +- chrome/content/vimperator/bookmarks.js | 111 ++++++++++++++++-------- chrome/content/vimperator/commands.js | 91 ++++++++----------- chrome/content/vimperator/completion.js | 28 ++++-- chrome/content/vimperator/help.js | Bin 13143 -> 13145 bytes chrome/content/vimperator/settings.js | 6 +- chrome/content/vimperator/tags | 6 +- chrome/content/vimperator/vimperator.js | 14 +++ vimperator.vim | 50 +++++++++++ vimperatorrc.example | 2 +- 12 files changed, 221 insertions(+), 102 deletions(-) create mode 100644 vimperator.vim diff --git a/ChangeLog b/ChangeLog index b8a2a547..1e2817ee 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,9 @@
+2007-05-02:
+	* version ???
+	* Added keyword support for bookmarks to the :[tab]open commands
+	* many small bug fixes
+
 2007-05-02:
 	* version 0.4.1
 	* Fixed bug that :open google.com/mail opened ".com/mail" in google search
diff --git a/Donators b/Donators
index c5ff869f..d36eb2b8 100644
--- a/Donators
+++ b/Donators
@@ -5,6 +5,8 @@
 * Ben Klemens
 * Sjoerd Siebinga
 * Cillian de Roiste
+* Miron Tewfik
+* Robert Heckel
 
 I want to say a big THANK YOU for all people which supported this project in this way.
 
diff --git a/Makefile b/Makefile index 65aa8194..cab6cf1f 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,13 @@ VERSION = 0.4.1 OS = $(shell uname -s) BUILD_DATE = $(shell date "+%Y/%m/%d %H:%M:%S") -JAR_FILES = ${shell find chrome/content -type f -a ! -path '*CVS*' ! -name 'tags'} chrome.manifest +JAR_FILES = ${shell find chrome/content -type f \ + -a ! -path '*CVS*' \ + -a \( -path '*.js' \ + -o -path '*.css' \ + -o -path '*.xul' \ + -o -path '*.rdf' \ + \) } chrome.manifest JAR_DIRS = $(foreach f,${JAR_FILES},$(dir $f)) JAR = chrome/vimperator.jar diff --git a/chrome/content/vimperator/bookmarks.js b/chrome/content/vimperator/bookmarks.js index 6054a846..f8c2c173 100644 --- a/chrome/content/vimperator/bookmarks.js +++ b/chrome/content/vimperator/bookmarks.js @@ -151,50 +151,93 @@ function parseBookmarkString(str, res) return true; } -/* also ensures that each search engine has a vimperator-friendly alias */ -function getSearchEngines() -{ - var search_engines = []; - const nsSS = Components.classes["@mozilla.org/browser/search-service;1"]. - getService(Components.interfaces.nsIBrowserSearchService); - var firefox_engines = nsSS.getVisibleEngines({ }); - for(var i in firefox_engines) - { - if (!firefox_engines[i].alias || !firefox_engines[i].alias.match(/^[a-z0-9_]+$/)) - { - var alias = firefox_engines[i].name.replace(/^\W*(\w+).*/, "$1").toLowerCase(); - firefox_engines[i].alias = alias; - } - search_engines.push([firefox_engines[i].alias, firefox_engines[i].description]); - } - return search_engines; -} - -function Search() +function Bookmarks() { const search_service = Components.classes["@mozilla.org/browser/search-service;1"]. - getService(Components.interfaces.nsIBrowserSearchService); + getService(Components.interfaces.nsIBrowserSearchService); - this.getDefaultEngine = function() + /* also ensures that each search engine has a vimperator-friendly alias */ + this.getSearchEngines = function() { - return search_service.currentEngine; + var search_engines = []; + var firefox_engines = search_service.getVisibleEngines({ }); + for(var i in firefox_engines) + { + if (!firefox_engines[i].alias || !firefox_engines[i].alias.match(/^[a-z0-9_]+$/)) + { + var alias = firefox_engines[i].name.replace(/^\W*(\w+).*/, "$1").toLowerCase(); + firefox_engines[i].alias = alias; + } + search_engines.push([firefox_engines[i].alias, firefox_engines[i].description]); + } + + return search_engines; } - this.setDefaultEngine = function(alias) + // FIXME: for now g_keywords is generated by get_bookmarks_completion, WILL HAVE TO CHANGE + // format of returned array: + // [keyword, helptext, url] + this.getKeywords = function() { - var engine = search_service.getEngineByAlias(alias); + return g_keywords; + } + + // xxx: probably remove these functions + // this.getDefaultEngine = function() + // { + // return search_service.currentEngine; + // } + // this.setDefaultEngine = function(alias) + // { + // var engine = search_service.getEngineByAlias(alias); + // if(engine) + // search_service.currentEngine = engine; + // else + // echoerr("Error: Search engine with alias '" + alias + "' does not exist"); + // } + // this.getEngine = function(alias) + // { + // var engine = search_service.getEngineByAlias(alias); + // return engine; + // } + + // if the engine name is null, it uses the default search engine + // returns a url for the search string + this.getSearchURL = function(text, engine_name) + { + var url = null; + if(!engine_name || engine_name == "") + engine_name = get_pref("defsearch", "google"); + + // first checks the search engines for a match + var engine = search_service.getEngineByAlias(engine_name); if(engine) - search_service.currentEngine = engine; - else - echoerr("Error: Search engine with alias '" + alias + "' does not exist"); - } + { + if(text) + url = engine.getSubmission(text, null).uri.spec; + else + url = engine.searchForm; + } + else // check for keyword urls + { + for (var i in g_keywords) + { + if(g_keywords[i][0] == engine_name) + { + if (text == null) + text = ""; + url = g_keywords[i][2].replace(/%s/g, encodeURIComponent(text)); + break; + } + } + } - this.getEngine = function(alias) - { - var engine = search_service.getEngineByAlias(alias); - return engine; + // if we came here, the engine_name is neither + return url; } + logMessage("Bookmarks initialized."); } -var search = new Search(); // FIXME, must it really be here? doesn't work in vimperator.js + +var bookmarks = new Bookmarks(); // FIXME, must it really be here? doesn't work in vimperator.js // vim: set fdm=marker sw=4 ts=4 et: diff --git a/chrome/content/vimperator/commands.js b/chrome/content/vimperator/commands.js index d7e5e8d7..c2bb16eb 100644 --- a/chrome/content/vimperator/commands.js +++ b/chrome/content/vimperator/commands.js @@ -239,11 +239,11 @@ var g_commands = [/*{{{*/ "Open one ore more URLs in the current tab", "Multiple URLs can be separated with the | character.
" + "Each |-separated token is analayzed and in this order:
"+ - "
  1. Opened with the specified search engine if the token looks like a search string and the first word of the token is the name of a search engine (:open wiki linus torvalds will open the wikipedia entry for linux torvalds).
  2. "+ + "
    1. Opened with the specified search engine if the token looks like a search string and the first word of the token is the name of a search engine (:open wikipedia linus torvalds will open the wikipedia entry for linux torvalds).
    2. "+ "
    3. Transformed to a relative URL of the current location if it starts with . or .. or ...;
      ... is special and moves up the directory hierarchy as far as possible.
      "+ + "
    4. Opened with the default search engine or keyword (specified with the 'defsearch' setting) if the first word is no search engine (:open linus torvalds will open a google search for linux torvalds).
    5. "+ "
      • :open ... with current location \"http://www.example.com/dir1/dir2/file.html\" will open \"http://www.example.com\"
      • "+ "
      • :open ./foo.html with current location \"http://www.example.com/dir1/dir2/file.html\" will open \"http://www.example.com/dir1/dir2/foo.html\"
      "+ - "
    6. Opened with the default search engine if the first word is no search engine (:open linus torvalds will open a google search for linux torvalds).
    7. "+ "
    8. Passed directly to Firefox in all other cases (:open www.osnews.com | www.slashdot.org will open OSNews in the current, and Slashdot in a new background tab).
    "+ "You WILL be able to use :open [-T \"linux\"] torvalds<Tab> to complete bookmarks with tag \"linux\" and which contain \"torvalds\". Note that -T support is only available for tab completion, not for the actual command.
    "+ "The items which are completed on <Tab> are specified in the 'complete' option.
    "+ @@ -564,7 +564,7 @@ var g_mappings = [/*{{{*/ ["p", ""], ["p", ""], "Open (put) an URL based on the current clipboard contents in the current buffer", - "You can also just select some non-URL text, and search for it with the default search engine with p", + "You can also just select some non-URL text, and search for it with the default search engine or keyword (specified by the 'defsearch' setting) with p", function(count) { openURLs(readFromClipboard()); } ], [ @@ -1301,55 +1301,6 @@ function stringToURLs(str) var urls = str.split(/\s*\|\s*/); begin: for(var url=0; url < urls.length; url++) { - /*for(var i=0; i < g_searchengines.length; i++) - { - var regex = new RegExp("^" + g_searchengines[i][0] + "\\s+" + "(.+)"); - matches = urls[url].match(regex); - if(matches != null) - { - urls[url] = g_searchengines[i][1].replace(/%s/, encodeURIComponent(matches[1])); - break begin; - } - }*/ - - // first check if the first word is a search engine - var matches = urls[url].match(/^\s*(\w+)(\s+|$)(.*)/); - var alias = null; - var text = null; - if (matches && matches[1]) - alias = matches[1]; - if (matches && matches[3] && matches[3].length >= 1) - text = matches[3]; - - if (alias) - { - var engine = search.getEngine(alias); - if (engine) - { - if(text) - urls[url] = engine.getSubmission(text, null).uri.spec; - else - urls[url] = engine.searchForm; - - continue; - } - } - - /* if the string contains a space or does not contain any of: .:/ - * open it with default search engine */ - if (urls[url].match(/\s+/) || urls[url].match(/\.|:|\//) == null) - { - // defaultEngine is always the same (Google), therefor let's use the currentEngine - //var default_engine = search_service.currentEngine; - var default_engine = search.getDefaultEngine(); - if (default_engine) - { - urls[url] = default_engine.getSubmission(urls[url], null).uri.spec; - continue; - } - } - - // check for ./ and ../ (or even .../) to go to a file in the upper directory if (urls[url].match(/^(\.$|\.\/\S*)/)) { @@ -1359,6 +1310,7 @@ function stringToURLs(str) newLocation += urls[url].replace(/^\.(\/\S+)/, "$1"); urls[url] = newLocation; + continue; } else if (urls[url].match(/^(\.\.$|\.\.\/[\S]*)/)) { @@ -1368,6 +1320,7 @@ function stringToURLs(str) newLocation += urls[url].replace(/^\.\.\/(\S+)/, "$1"); urls[url] = newLocation; + continue; } else if (urls[url].match(/^(\.\.\.$|\.\.\.\/[\S]*)/)) { @@ -1377,15 +1330,47 @@ function stringToURLs(str) newLocation += urls[url].replace(/^\.\.\.\/(\S+)/, "$1"); urls[url] = newLocation; + continue; } + /* if the string contains a space or does not contain any of: .:/ + * open it with default search engine */ + if (urls[url].match(/\s+/) || urls[url].match(/\.|:|\//) == null) + { + // check if the first word is a search engine + var matches = urls[url].match(/^\s*(.*?)(\s+|$)(.*)/); + var alias = null; + var text = null; + if (matches && matches[1]) + alias = matches[1]; + if (matches && matches[3] && matches[3].length >= 1) + text = matches[3]; + + var search_url = bookmarks.getSearchURL(text, alias); + if (search_url && search_url.length >= 1) + { + urls[url] = search_url; + continue; + } + else // the first word was not a search engine, search for the whole string in the default engine + { + search_url = bookmarks.getSearchURL(urls[url], null); + if (search_url && search_url.length >= 1) + { + urls[url] = search_url; + continue; + } + } + } + + // if we are here let Firefox handle the url and hope it does + // something useful with it :) } return urls; } /* returns true if the currently loaded URI is * a directory or false if it is a file - * if passed 'url' is null, use current directory */ function isDirectory(url) { diff --git a/chrome/content/vimperator/completion.js b/chrome/content/vimperator/completion.js index 8f9fc6ee..f03e48f8 100644 --- a/chrome/content/vimperator/completion.js +++ b/chrome/content/vimperator/completion.js @@ -12,6 +12,9 @@ var bookmarks_loaded = false; var g_history = []; var history_loaded = false; +// array of our bookmark keywords +var g_keywords = []; + // variables for the tab completion and command history: // -1: filled, but no selection made // >= 0: index of current item in the g_completions array @@ -390,7 +393,9 @@ function filter_url_array(urls, filter)/*{{{*/ function get_search_completions(filter)/*{{{*/ { - var engines = getSearchEngines(); + //var engines = bookmarks.getSearchEngines();//.concat(bookmarks.getKeywords()); + //var engines = bokmarks.getKeywords();//.concat(bookmarks.getKeywords()); + var engines = bookmarks.getSearchEngines().concat(bookmarks.getKeywords()); if (!filter) return engines.map(function($_) { return [$_[0], $_[1]]; @@ -459,20 +464,27 @@ function get_bookmark_completions(filter)/*{{{*/ // update our bookmark cache var RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService( Components.interfaces.nsIRDFService ); var root = RDF.GetResource( "NC:BookmarksRoot" ); - bookmarks = []; // here getAllChildren will store the bookmarks + var bmarks = []; // here getAllChildren will store the bookmarks g_bookmarks = []; // also clear our bookmark cache - BookmarksUtils.getAllChildren(root, bookmarks); + // FIXME: wrong location + g_keywords = []; + + BookmarksUtils.getAllChildren(root, bmarks); // alert(bookmarks[0].length); - for(var i = 0; i < bookmarks.length; i++) + for(var i = 0; i < bmarks.length; i++) { - if (bookmarks[i][0] && bookmarks[i][1]) + if (bmarks[i][0] && bmarks[i][1]) { - g_bookmarks.push([bookmarks[i][1].Value, bookmarks[i][0].Value ]); + g_bookmarks.push([bmarks[i][1].Value, bmarks[i][0].Value ]); } // for(var j=0; j < bookmarks[i].length; j++) // { -// if(bookmarks[i][2]) -// alert("2: " + bookmarks[i][2].Value); + // keyword + if(bmarks[i][1] && bmarks[i][2]) + g_keywords.push([bmarks[i][2].Value, bmarks[i][0].Value, bmarks[i][1].Value]); + //g_keywords.push([bookmarks[i][2].Value, bookmarks[i][0].Value + " (" + bookmarks[i][1].Value + ")"]); + //g_keywords.push([[bookmarks[i][2].Value, bookmarks[i][1].Value], bookmarks[i][0].Value]); + //alert("2: " + bookmarks[i][2].Value); // if(bookmarks[i][3]) // alert("3: " + bookmarks[i][3].Value); // if(bookmarks[i][4]) diff --git a/chrome/content/vimperator/help.js b/chrome/content/vimperator/help.js index e0605a2b9a8ed75e052ce14599a1b1f6dd39527f..2b62e7d1d2a6c141da1809a852b2f5c40178f8f9 100644 GIT binary patch delta 21 ccmcbfb~9~(FjGw4W=3IeSw`#4J_g*(09yP8qyPW_ delta 18 Zcmcbac0Fx@FeAfeCSh;c&3*>l%m70Q1)u-` diff --git a/chrome/content/vimperator/settings.js b/chrome/content/vimperator/settings.js index d1a3e19e..d8fdab22 100644 --- a/chrome/content/vimperator/settings.js +++ b/chrome/content/vimperator/settings.js @@ -63,7 +63,7 @@ var g_settings = [/*{{{*/ ["complete", "cpt"], "Items which are completed at the :[tab]open prompt", "Available items:
    "+ - "
    • s: Search machines
    • "+ + "
      • s: Search machines and keyword URLs
      • "+ "f: Local files
      • "+ "b: Bookmarks
      • "+ "h: History
      "+ @@ -84,8 +84,8 @@ var g_settings = [/*{{{*/ "if [arg] neither looks like a URL or like a specified search engine/keyword.", "string", function() { return [["foo", "bar"], ["shit", "blub"]]; }, - function(value) { search.setDefaultEngine(value); }, - function() { return search.getDefaultEngine().alias; }, + function(value) { set_pref("defsearch", value); }, + function() { return get_pref("defsearch", "google"); }, "google", null ], diff --git a/chrome/content/vimperator/tags b/chrome/content/vimperator/tags index e9aea5d6..8dac41a5 100644 --- a/chrome/content/vimperator/tags +++ b/chrome/content/vimperator/tags @@ -13,6 +13,7 @@ beep commands.js /^function beep()$/;" f bmadd commands.js /^function bmadd(str)$/;" f bmdel commands.js /^function bmdel(str)$/;" f bmshow commands.js /^function bmshow(filter, fullmode)$/;" f +Bookmarks bookmarks.js /^function Bookmarks()$/;" f buffer_preview_toggle commands.js /^function buffer_preview_toggle()$/;" f buffer_preview_update commands.js /^function buffer_preview_update(event)$/;" f buffer_switch commands.js /^function buffer_switch(string)$/;" f @@ -55,7 +56,6 @@ getHintById hints.js /^ function getHintById(id, win)$/;" f getLinkNodes vimperator.js /^function getLinkNodes(doc)$/;" f getPageLinkNodes vimperator.js /^function getPageLinkNodes()$/;" f getProperty bookmarks.js /^function getProperty( aInput, aArc, DS )$/;" f -getSearchEngines bookmarks.js /^function getSearchEngines()$/;" f get_bookmark_completions completion.js /^function get_bookmark_completions(filter)\/*{{{*\/$/;" f get_buffer_completions completion.js /^function get_buffer_completions(filter)\/*{{{*\/$/;" f get_command commands.js /^function get_command(cmd) \/\/ {{{$/;" f @@ -113,8 +113,8 @@ removeHints hints.js /^ function removeHints(win)$/;" f removeMode commands.js /^function removeMode(mode)$/;" f restart commands.js /^function restart()$/;" f save_history completion.js /^function save_history()$/;" f +scrollBufferAbsolute commands.js /^function scrollBufferAbsolute(horizontal, vertical)$/;" f scrollBufferRelative commands.js /^function scrollBufferRelative(right, down)$/;" f -Search bookmarks.js /^function Search()$/;" f searcher find.js /^function searcher () {$/;" f selectInput commands.js /^function selectInput()$/;" f set commands.js /^function set(args, special)$/;" f @@ -146,3 +146,5 @@ tokenize_ex commands.js /^function tokenize_ex(string, tag)$/;" f unload vimperator.js /^function unload()$/;" f updateStatusbar vimperator.js /^function updateStatusbar(message)$/;" f yankCurrentLocation commands.js /^function yankCurrentLocation()$/;" f +zoom_in commands.js /^function zoom_in(factor)$/;" f +zoom_to commands.js /^function zoom_to(value)$/;" f diff --git a/chrome/content/vimperator/vimperator.js b/chrome/content/vimperator/vimperator.js index 5e369163..20ef8a79 100644 --- a/chrome/content/vimperator/vimperator.js +++ b/chrome/content/vimperator/vimperator.js @@ -180,11 +180,25 @@ nsBrowserStatusHandler.prototype = window.addEventListener("load", init, false); + +// the global vimperator object, quit empty right now +// add functions with vimperator.prototype.func = ... +// var vimperator = null; +// var Vimperator = function() { +// this.keywordsLoaded = false; +// this.keywords = []; +// this.searchEngines = []; +// this.bookmarks = new Bookmarks(); +// }; + + //////////////////////////////////////////////////////////////////////// // init/uninit //////////////////////////////////////////////////// {{{1 //////////////////////////////////////////////////////////////////////// function init() { +// vimperator = new Vimperator; + preview_window = document.getElementById("vim-preview_window"); status_line = document.getElementById("vim-statusbar"); completion_list = document.getElementById("vim-completion"); diff --git a/vimperator.vim b/vimperator.vim new file mode 100644 index 00000000..e5c782b1 --- /dev/null +++ b/vimperator.vim @@ -0,0 +1,50 @@ +" Vim syntax file +" Language: VIMperator configuration file +" Maintainer: Doug Kearns +" Latest Revision: 2007 May 03 + +if exists("b:current_syntax") + finish +endif + +let s:cpo_save = &cpo +set cpo&vim + +syn include @javascriptTop syntax/javascript.vim +unlet b:current_syntax + +syn keyword vimperatorTodo FIXME NOTE TODO XXX contained +syn match vimperatorComment +".*$+ contains=vimperatorTodo,@Spell + +syn keyword vimperatorCommand addons ba[ck] bd[elete] bw[ipeout] bun[load] tabc[lose] beep bmadd bmdel bookmarks bm b[uffer] + \ buffers files ls downloads dl ec[ho] echoe[rr] exe[cute] forward fw ha[rdcopy] h[elp] history hs javascript js ma[rk] + \ marks o[pen] e[dit] pc[lose] preferences prefs q[uit] quita[ll] qa[ll] re[load] restart restart sav[eas] se[t] so[urce] + \ st[op] tab tabn[ext] tn[ext] tabopen t to topen tabnew tabe[dit] tp[revious] tN[ext] tabp[revious] tabN[ext] u[ndo] + \ qmarkadd qmadd qmarkdel qmdel qmarks qms ve[rsion] w wo[pen] wine[dit] win[open] wq wqa[ll] xa[ll] zo[om] + \ contained + +" FIXME +syn match vimperatorCommandWrapper "\<\h\w*\>" contains=vimperatorCommand + +syn region vimperatorSet matchgroup=vimperatorCommand start="\" end="$" keepend oneline contains=vimperatorOption +syn keyword vimperatorOption activate beep nobeep beep complete cpt defsearch ds extendedhinttags eht focusedhintstyle fhs + \ fullscreen fs nofullscreen nofs guioptions go hintchars hc hintstyle hs hinttags maxhints mh preload nopreload + \ previewheight pvh showtabline stal usermode um nousermode noum wildmode wim wildoptions wop + \ contained + +syn region vimperatorJavascript start="\%(^\s*\%(javascript\|js\)\s\+\)\@<=" end="$" contains=@javascriptTop keepend oneline +syn region vimperatorJavascript matchgroup=vimperatorJavascriptDelimiter + \ start="\%(^\s*\%(javascript\|js\)\s\+\)\@<=<<\z(\h\w*\)"hs=s+2 end="^\z1$" contains=@javascriptTop fold + +" Note: match vim.vim highlighting groups +hi def link vimperatorCommand Statement +hi def link vimperatorComment Comment +hi def link vimperatorJavascriptDelimiter Delimiter +hi def link vimperatorOption PreProc + +let b:current_syntax = "vimperator" + +let &cpo = s:cpo_save +unlet s:cpo_save + +" vim: tw=130: diff --git a/vimperatorrc.example b/vimperatorrc.example index 4e880f51..4839bd63 100644 --- a/vimperatorrc.example +++ b/vimperatorrc.example @@ -73,4 +73,4 @@ EOF javascript define_map("h","h","previous tab",function(){tab_go(-1);}); javascript define_map("l","l","next tab",function(){tab_go(0);}); -" vim: set syntax=javascript : +" vim: set syntax=vimperator: