1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-21 16:17:57 +01:00

keyword support for bookmarks

This commit is contained in:
Martin Stubenschrott
2007-05-04 18:04:11 +00:00
parent 2284afec3a
commit c6d67fe8fe
12 changed files with 221 additions and 102 deletions

View File

@@ -1,4 +1,9 @@
<pre>
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

View File

@@ -5,6 +5,8 @@
* Ben Klemens
* Sjoerd Siebinga
* Cillian de Roiste
* Miron Tewfik
* Robert Heckel
I want to say a big <b>THANK YOU</b> for all people which supported this project in this way.
</pre>

View File

@@ -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

View File

@@ -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:

View File

@@ -239,11 +239,11 @@ var g_commands = [/*{{{*/
"Open one ore more URLs in the current tab",
"Multiple URLs can be separated with the | character.<br/>" +
"Each |-separated token is analayzed and in this order:<br/>"+
"<ol><li>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 (<code class=command>:open wiki linus torvalds</code> will open the wikipedia entry for linux torvalds).</li>"+
"<ol><li>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 (<code class=command>:open wikipedia linus torvalds</code> will open the wikipedia entry for linux torvalds).</li>"+
" <li>Transformed to a relative URL of the current location if it starts with . or .. or ...;<br/>... is special and moves up the directory hierarchy as far as possible.<br/>"+
" <li>Opened with the default search engine or keyword (specified with the <code class=setting>'defsearch'</code> setting) if the first word is no search engine (<code>:open linus torvalds</code> will open a google search for linux torvalds).</li>"+
"<ul><li><code class=command>:open ...</code> with current location <code>\"http://www.example.com/dir1/dir2/file.html\"</code> will open <code>\"http://www.example.com\"</code></li></li>"+
"<li><code class=command>:open ./foo.html</code> with current location <code>\"http://www.example.com/dir1/dir2/file.html\"</code> will open <code>\"http://www.example.com/dir1/dir2/foo.html\"</code></li></ul></li>"+
" <li>Opened with the default search engine if the first word is no search engine (<code>:open linus torvalds</code> will open a google search for linux torvalds).</li>"+
" <li>Passed directly to Firefox in all other cases (<code class=command>:open www.osnews.com | www.slashdot.org</code> will open OSNews in the current, and Slashdot in a new background tab).</li></ol>"+
"You WILL be able to use <code class=command>:open [-T \"linux\"] torvalds&lt;Tab&gt;</code> 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.<br/>"+
"The items which are completed on <code>&lt;Tab&gt;</code> are specified in the <code>'complete'</code> option.<br/>"+
@@ -564,7 +564,7 @@ var g_mappings = [/*{{{*/
["p", "<MiddleMouse>"],
["p", "<MiddleMouse>"],
"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 <code class=mapping>p</code>",
"You can also just select some non-URL text, and search for it with the default search engine or keyword (specified by the <code class=setting>'defsearch'</code> setting) with <code class=mapping>p</code>",
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)
{

View File

@@ -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])

Binary file not shown.

View File

@@ -63,7 +63,7 @@ var g_settings = [/*{{{*/
["complete", "cpt"],
"Items which are completed at the :[tab]open prompt",
"Available items:<br>"+
"<ul><li><b>s</b>: Search machines</li><li>"+
"<ul><li><b>s</b>: Search machines and keyword URLs</li><li>"+
"<b>f</b>: Local files</li><li>"+
"<b>b</b>: Bookmarks</li><li>"+
"<b>h</b>: History</li></ul>"+
@@ -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
],

View File

@@ -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

View File

@@ -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");

50
vimperator.vim Normal file
View File

@@ -0,0 +1,50 @@
" Vim syntax file
" Language: VIMperator configuration file
" Maintainer: Doug Kearns <dougkearns@gmail.com>
" 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="\<set\=\>" 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:

View File

@@ -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: