diff --git a/chrome/content/vimperator/bookmarks.js b/chrome/content/vimperator/bookmarks.js index 726b7621..59c90d5a 100644 --- a/chrome/content/vimperator/bookmarks.js +++ b/chrome/content/vimperator/bookmarks.js @@ -1,84 +1,3 @@ -/* - * low-level BOOKMARK and HISTORY handling - * - * these commands try to be generic and don't use any GUI handling code - * for higher-level functions look into commands.js - */ - -function getProperty( aInput, aArc, DS ) -{ - var node; - node = DS.GetTarget( aInput, aArc, true ); - if( node instanceof Components.interfaces.nsIRDFResource ) { - return node.Value; - } - if( node instanceof Components.interfaces.nsIRDFLiteral ) { - return node.Value; - } - return ""; -} - -function addBookmark(title, uri) -{ - folder = RDF.GetResource("NC:BookmarksRoot"); - var rSource = BookmarksUtils.createBookmark(title, uri, null, title); - var selection = BookmarksUtils.getSelectionFromResource(rSource); - var target = BookmarksUtils.getTargetFromFolder(folder); - BookmarksUtils.insertAndCheckSelection("newbookmark", selection, target); - - //also update bookmark cache - g_bookmarks.unshift([uri, title]); -} - -/* no idea what it does, it Just Works (TM) - * - * returns number of deleted bookmarks - */ -function deleteBookmark(url) -{ - var deleted = 0; - - // gNC_NS for trunk, NC_NS for 1.X - try {var pNC_NS; pNC_NS = gNC_NS;} catch (err) { pNC_NS = NC_NS;} - if(! BMSVC || ! BMDS || ! RDF || ! pNC_NS ) return null; - if ( !url) return null; // just in case - - var curfolder = RDF.GetResource("NC:BookmarksRoot"); - var urlArc = RDF.GetResource(pNC_NS+"URL"); - var urlLiteral = RDF.GetLiteral(url); - if (BMDS.hasArcIn(urlLiteral, urlArc)) { - var bmResources, bmResource, title, uri, type, ptype; - bmResources = BMSVC.GetSources(urlArc, urlLiteral, true); - while (bmResources.hasMoreElements()) { - bmResource = bmResources.getNext(); - type = BookmarksUtils.resolveType(bmResource); - if (type != "ImmutableBookmark") { - ptype = BookmarksUtils.resolveType(BMSVC.getParent(bmResource)); -// alert(type); -// if ( type == "Folder") // store the current folder -// curfolder = bmResource; - if ( (type == "Bookmark" || type == "IEFavorite") && ptype != "Livemark") { - title = BookmarksUtils.getProperty(bmResource, pNC_NS+"Name"); - uri = BookmarksUtils.getProperty(bmResource, pNC_NS+"URL"); - - if (uri == url) - { - RDFC.Init(BMDS, BMSVC.getParent(bmResource)); - RDFC.RemoveElement(bmResource, true); - deleted++; - } - } - } - } - } - - // also update bookmark cache, if we removed at least one bookmark - if(deleted > 0) - bookmarks_loaded = false; - - return deleted; -} - /* call the function like this: var res = new Object(); parseBookmarkString("-t tag1,tag2 -T title http://www.orf.at", res); @@ -147,20 +66,140 @@ function parseBookmarkString(str, res) } } } - return true; } - +/* + * also includes methods for dealing with + * keywords and search engines + */ function Bookmarks() { + //////////////////////////////////////////////////////////////////////////////// + ////////////////////// PRIVATE SECTION ///////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////// const search_service = Components.classes["@mozilla.org/browser/search-service;1"]. getService(Components.interfaces.nsIBrowserSearchService); + const rdf_service = Components.classes["@mozilla.org/rdf/rdf-service;1"]. + getService( Components.interfaces.nsIRDFService ); + var bookmarks = null; + var keywords = null; + + if(get_pref("preload")) + setTimeout(function() { load(); } , 100); + + function load() + { + // update our bookmark cache + var root = rdf_service.GetResource("NC:BookmarksRoot"); + bookmarks = new Array(); // also clear our bookmark cache + keywords = new Array(); + + var bmarks = []; // here getAllChildren will store the bookmarks + BookmarksUtils.getAllChildren(root, bmarks); + for(var bm in bmarks) + { + if (bmarks[bm][0] && bmarks[bm][1]) + bookmarks.push([bmarks[bm][1].Value, bmarks[bm][0].Value ]); + + // keyword + if(bmarks[bm][1] && bmarks[bm][2]) + keywords.push([bmarks[bm][2].Value, bmarks[bm][0].Value, bmarks[bm][1].Value]); + } + } + + //////////////////////////////////////////////////////////////////////////////// + ////////////////////// PUBLIC SECTION ////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////// + /* + * @return a new Array() of our bookmarks + */ + this.get = function() + { + if (!bookmarks) + load(); + + return bookmarks; + } + + /** + * @TODO: keyword support + */ + this.add = function (title, uri, keyword) + { + if(!bookmarks) + load(); + + folder = rdf_service.GetResource("NC:BookmarksRoot"); + var rSource = BookmarksUtils.createBookmark(title, uri, keyword, title); + var selection = BookmarksUtils.getSelectionFromResource(rSource); + var target = BookmarksUtils.getTargetFromFolder(folder); + BookmarksUtils.insertAndCheckSelection("newbookmark", selection, target); + + //also update bookmark cache + bookmarks.unshift([uri, title]); + return true; + } + + /* no idea what it does, it Just Works (TM) + * + * @returns number of deleted bookmarks + */ + this.remove = function(url) + { + var deleted = 0; + if(!url) + return 0; + + // gNC_NS for trunk, NC_NS for 1.X + //try { var pNC_NS; pNC_NS = gNC_NS;} catch (err) { pNC_NS = NC_NS;} + if (!BMSVC || !BMDS || !RDF || !gNC_NS) // defined from firefox + return 0; + + var curfolder = RDF.GetResource("NC:BookmarksRoot"); + var urlArc = RDF.GetResource(gNC_NS + "URL"); + var urlLiteral = RDF.GetLiteral(url); + if (BMDS.hasArcIn(urlLiteral, urlArc)) + { + var bmResources, bmResource, title, uri, type, ptype; + bmResources = BMSVC.GetSources(urlArc, urlLiteral, true); + while (bmResources.hasMoreElements()) + { + bmResource = bmResources.getNext(); + type = BookmarksUtils.resolveType(bmResource); + if (type != "ImmutableBookmark") { + ptype = BookmarksUtils.resolveType(BMSVC.getParent(bmResource)); + // alert(type); + // if ( type == "Folder") // store the current folder + // curfolder = bmResource; + if ( (type == "Bookmark" || type == "IEFavorite") && ptype != "Livemark") + { + title = BookmarksUtils.getProperty(bmResource, gNC_NS + "Name"); + uri = BookmarksUtils.getProperty(bmResource, gNC_NS + "URL"); + + if (uri == url) + { + RDFC.Init(BMDS, BMSVC.getParent(bmResource)); + RDFC.RemoveElement(bmResource, true); + deleted++; + } + } + } + } + } + + // also update bookmark cache, if we removed at least one bookmark + if(deleted > 0) + load(); + + return deleted; + } + /* also ensures that each search engine has a vimperator-friendly alias */ this.getSearchEngines = function() { - var search_engines = []; + var search_engines = new Array(); var firefox_engines = search_service.getVisibleEngines({ }); for(var i in firefox_engines) { @@ -175,35 +214,18 @@ function Bookmarks() return search_engines; } - // 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() { - return g_keywords; + if(!keywords) + load(); + + return 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 + // @returns the url for the search string this.getSearchURL = function(text, engine_name) { var url = null; @@ -221,27 +243,114 @@ function Bookmarks() } else // check for keyword urls { - for (var i in g_keywords) + if(!keywords) + load(); + + for (var i in keywords) { - if(g_keywords[i][0] == engine_name) + if(keywords[i][0] == engine_name) { if (text == null) text = ""; - url = g_keywords[i][2].replace(/%s/g, encodeURIComponent(text)); + url = keywords[i][2].replace(/%s/g, encodeURIComponent(text)); break; } } } - // if we came here, the engine_name is neither + // if we came here, the engine_name is neither a search engine or URL return url; } - logMessage("Bookmarks initialized."); + logMessage("Bookmarks initialized"); } -var bookmarks = new Bookmarks(); // FIXME, must it really be here? doesn't work in vimperator.js +function History() +{ + const rdf_service = Components.classes["@mozilla.org/rdf/rdf-service;1"]. + getService( Components.interfaces.nsIRDFService ); + const global_history_service = Components.classes["@mozilla.org/browser/global-history;2"]. + getService(Components.interfaces.nsIRDFDataSource); + var history = null; + if(get_pref("preload")) + setTimeout(function() { load(); } , 100); + + function load() + { + history = new Array(); + + var historytree = document.getElementById("hiddenHistoryTree"); + if (!historytree) + return; + + if (historytree.hidden) + { + historytree.hidden = false; + historytree.database.AddDataSource(global_history_service); + } + + if (!historytree.ref) + historytree.ref = "NC:HistoryRoot"; + + var nameResource = rdf_service.GetResource(gNC_NS + "Name"); + var builder = historytree.builder.QueryInterface(Components.interfaces.nsIXULTreeBuilder); + + var count = historytree.view.rowCount; + for (var i = count-1; i >= 0; i--) + { + var res = builder.getResourceAtIndex(i); + var url = res.Value; + var title; + var titleRes = historytree.database.GetTarget(res, nameResource, true); + if (!titleRes) + continue; + + var titleLiteral = titleRes.QueryInterface(Components.interfaces.nsIRDFLiteral); + if(titleLiteral) + title = titleLiteral.Value; + else + title = ""; + + history.push([url, title]); + } + } + //////////////////////////////////////////////////////////////////////////////// + ////////////////////// PUBLIC SECTION ////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////// + /* + * @return a new Array() of our bookmarks + */ + this.get = function() + { + if (!history) + load(); + + return history; + } + + this.add = function (uri, title) + { + if(!history) + load(); + + // XXX: check if fast enough + history = history.filter(function(elem) { + return elem[0] != uri; + }); +// for(var i in history) +// { +// if(g_history[i][0] == url) +// return; +// } +// g_history.unshift([url, title]); + + history.unshift([uri, title]); + return true; + } + + logMessage("History initialized"); +} Vimperator.prototype.quickmarks = new function() { diff --git a/chrome/content/vimperator/commands.js b/chrome/content/vimperator/commands.js index 4cd7326d..b1faa969 100644 --- a/chrome/content/vimperator/commands.js +++ b/chrome/content/vimperator/commands.js @@ -1346,7 +1346,7 @@ function stringToURLs(str) if (matches && matches[3] && matches[3].length >= 1) text = matches[3]; - var search_url = bookmarks.getSearchURL(text, alias); + var search_url = vimperator.bookmarks.getSearchURL(text, alias); if (search_url && search_url.length >= 1) { urls[url] = search_url; @@ -1354,7 +1354,7 @@ function stringToURLs(str) } 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); + search_url = vimperator.bookmarks.getSearchURL(urls[url], null); if (search_url && search_url.length >= 1) { urls[url] = search_url; @@ -1515,11 +1515,11 @@ function bmadd(str) if(res.title == null) // title could still be null res.title = res.url; - addBookmark(res.title, res.url); - echo("Bookmark `" + res.title + "' added with url `" + res.url + "'"); + vimperator.bookmarks.add(res.title, res.url); + vimperator.echo("Bookmark `" + res.title + "' added with url `" + res.url + "'"); } else - echo("Usage: :bmadd [-t \"My Title\"] [-T tag1,tag2] "); + vimperator.echo("Usage: :bmadd [-t \"My Title\"] [-T tag1,tag2] "); } function bmdel(str) @@ -1530,11 +1530,11 @@ function bmdel(str) if(res.url == null) res.url = getCurrentLocation(); - var del = deleteBookmark(res.url); - echo(del + " bookmark(s) with url `" + res.url + "' deleted"); + var del = vimperator.bookmarks.remove(res.url); + vimperator.echo(del + " bookmark(s) with url `" + res.url + "' deleted"); } else - echo("Usage: :bmdel "); + vimperator.echo("Usage: :bmdel "); } function bmshow(filter, fullmode) @@ -1543,7 +1543,7 @@ function bmshow(filter, fullmode) openURLsInNewTab("chrome://browser/content/bookmarks/bookmarksPanel.xul", true); else { - var items = get_bookmark_completions(filter); + var items = vimperator.bookmarks.get(filter); vimperator.previewwindow.show(items); } } @@ -1553,7 +1553,7 @@ function hsshow(filter, fullmode) openURLsInNewTab("chrome://browser/content/history/history-panel.xul", true); else { - var items = get_history_completions(filter); + var items = vimperator.history.get(filter); vimperator.previewwindow.show(items); } } diff --git a/chrome/content/vimperator/completion.js b/chrome/content/vimperator/completion.js index 9fd54095..bdd47ad2 100644 --- a/chrome/content/vimperator/completion.js +++ b/chrome/content/vimperator/completion.js @@ -5,15 +5,15 @@ */ // array of all our bookmarks -var g_bookmarks = []; -var bookmarks_loaded = false; +// var g_bookmarks = []; +// var bookmarks_loaded = false; // array of all our history items -var g_history = []; -var history_loaded = false; +// var g_history = []; +// var history_loaded = false; // array of our bookmark keywords -var g_keywords = []; +// var g_keywords = []; // 2 dimensional: 1st element: what to complete // 2nd element: help description @@ -211,9 +211,7 @@ function filter_url_array(urls, filter)/*{{{*/ function get_search_completions(filter)/*{{{*/ { - //var engines = bookmarks.getSearchEngines();//.concat(bookmarks.getKeywords()); - //var engines = bokmarks.getKeywords();//.concat(bookmarks.getKeywords()); - var engines = bookmarks.getSearchEngines().concat(bookmarks.getKeywords()); + var engines = vimperator.bookmarks.getSearchEngines().concat(vimperator.bookmarks.getKeywords()); if (!filter) return engines.map(function($_) { return [$_[0], $_[1]]; @@ -224,102 +222,17 @@ function get_search_completions(filter)/*{{{*/ return build_longest_common_substring(mapped, filter); }/*}}}*/ -function get_history_completions(filter)/*{{{*/ +function get_history_completions(filter) { - var history = document.getElementById("hiddenHistoryTree"); - if (!history) - return []; + var items = vimperator.history.get(); + return filter_url_array(items, filter); +} - // build our history cache - if (history_loaded == false) - { - if (history.hidden) - { - history.hidden = false; - var globalHistory = Components.classes["@mozilla.org/browser/global-history;2"].getService(Components.interfaces.nsIRDFDataSource); - history.database.AddDataSource(globalHistory); - g_history = []; - } - - if (!history.ref) - history.ref = "NC:HistoryRoot"; - - const NC_NS = "http://home.netscape.com/NC-rdf#"; - if (!gRDF) - gRDF = Components.classes["@mozilla.org/rdf/rdf-service;1"] - .getService(Components.interfaces.nsIRDFService); - - var nameResource = gRDF.GetResource(NC_NS + "Name"); - var builder = history.builder.QueryInterface(Components.interfaces.nsIXULTreeBuilder); - - var count = history.view.rowCount; - for (var i = count-1; i >= 0; i--) - { - var res = builder.getResourceAtIndex(i); - var url = res.Value; - // var col = history.columns["Name"]; - //var title = history.view.getCellText(i, col); - var title; - var titleRes = history.database.GetTarget(res, nameResource, true); - if (!titleRes) - continue; - - var titleLiteral = titleRes.QueryInterface(Components.interfaces.nsIRDFLiteral); - if(titleLiteral) - title = titleLiteral.Value; - else - title = ""; - - g_history.push([url, title]); - } - history_loaded = true; - } - return filter_url_array(g_history, filter); -}/*}}}*/ - -function get_bookmark_completions(filter)/*{{{*/ +function get_bookmark_completions(filter) { - if (!bookmarks_loaded) - { - // update our bookmark cache - var RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService( Components.interfaces.nsIRDFService ); - var root = RDF.GetResource( "NC:BookmarksRoot" ); - var bmarks = []; // here getAllChildren will store the bookmarks - g_bookmarks = []; // also clear our bookmark cache - // FIXME: wrong location - g_keywords = []; - - BookmarksUtils.getAllChildren(root, bmarks); -// alert(bookmarks[0].length); - for(var i = 0; i < bmarks.length; i++) - { - if (bmarks[i][0] && bmarks[i][1]) - { - g_bookmarks.push([bmarks[i][1].Value, bmarks[i][0].Value ]); - } - // for(var j=0; j < bookmarks[i].length; j++) - // { - // 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]) -// alert("4: " + bookmarks[i][4].Value); -// if(bookmarks[i][5]) -// alert("5: " + bookmarks[i][5].Value); - //alert("0: " + bookmarks[i][0].Value + " - 1: " + bookmarks[i][1].Value + "- 2:" + bookmarks[i][2].Value);// + "- 3:"+ bookmarks[i][3].Value + "- 4:" + bookmarks[i][4].Value);// + "- 5:" + bookmarks[i][5].Value); - //} - } - bookmarks_loaded = true; - } - - - return filter_url_array(g_bookmarks, filter); -}/*}}}*/ + var bookmarks = vimperator.bookmarks.get(); + return filter_url_array(bookmarks, filter); +} function get_file_completions(filter)/*{{{*/ { @@ -509,8 +422,6 @@ function get_buffer_completions(filter)/*{{{*/ return build_longest_common_substring(items, filter); }/*}}}*/ - -// return [startindex, [[itemtext, itemhelp],...]] function exTabCompletion(str) { var [count, cmd, special, args] = tokenize_ex(str); diff --git a/chrome/content/vimperator/ui.js b/chrome/content/vimperator/ui.js index 2ca09fed..74c22c7a 100644 --- a/chrome/content/vimperator/ui.js +++ b/chrome/content/vimperator/ui.js @@ -66,7 +66,7 @@ function CommandLine () // for the example command "open sometext| othertext" (| is the cursor pos) var completion_start_index = 0; // will be 5 because we want to complete arguments for the :open command var completion_prefix = "" // will be: "open sometext" - var completion_postfix = ""; // will be: " othertext" + var completion_postfix = ""; // will be: " othertext" var wild_index = 0; // keep track how often we press in a row var completion_index = UNINITIALIZED; @@ -158,6 +158,10 @@ function CommandLine () this.echo = function(str) { + var focused = document.commandDispatcher.focusedElement; + if (focused && focused == command_widget.inputField) + return; + setNormalStyle(); setPrompt(""); setCommand(str); @@ -165,6 +169,10 @@ function CommandLine () this.echoErr = function(str) { + var focused = document.commandDispatcher.focusedElement; + if (focused && focused == command_widget.inputField) + return; + setErrorStyle(); setPrompt(""); setCommand(str); diff --git a/chrome/content/vimperator/vimperator.js b/chrome/content/vimperator/vimperator.js index 58975438..55fecb20 100644 --- a/chrome/content/vimperator/vimperator.js +++ b/chrome/content/vimperator/vimperator.js @@ -167,20 +167,22 @@ window.addEventListener("load", init, false); //////////////////////////////////////////////////////////////////////// // init/uninit //////////////////////////////////////////////////// {{{1 //////////////////////////////////////////////////////////////////////// +//function moo() { return ["moo", "x"];}; +// return [startindex, [[itemtext, itemhelp],...]] + function init() { // init the main object vimperator = new Vimperator; - // these inner classes are only created here, because outside the init() + // these inner classes are created here, because outside the init() // function, the chrome:// is not ready + Vimperator.prototype.bookmarks = new Bookmarks; + Vimperator.prototype.history = new History; Vimperator.prototype.qm = new QM; -// alert("ini3"); // Vimperator.prototype.commandline = new CommandLine; Vimperator.prototype.search = new Search; -// alert("ini4"); Vimperator.prototype.previewwindow = new InformationList("vimperator-preview-window", { incremental_fill: false, max_items: 10 }); -// alert("ini5"); Vimperator.prototype.bufferwindow = new InformationList("vimperator-buffer-window", { incremental_fill: false, max_items: 10 }); Vimperator.prototype.statusline = new StatusLine(); Vimperator.prototype.tabs = new Tabs(); @@ -188,6 +190,7 @@ function init() // XXX: move elsewhere vimperator.registerCallback("submit", vimperator.modes.EX, function(command) { /*vimperator.*/execute(command); } ); vimperator.registerCallback("complete", vimperator.modes.EX, function(str) { return exTabCompletion(str); } ); + //vimperator.registerCallback("complete", vimperator.modes.EX, function(str) { return moo();; } ); //status_line = document.getElementById("vim-statusbar"); command_line = document.getElementById("vim-commandbar"); @@ -337,10 +340,6 @@ function init() gURLBar.blur(); vimperator.focusContent(); - // everything important is done, register a preload handler to speed up first time history cache - if(get_pref("preload")) - setTimeout(function() { get_url_completions(""); } , 100); - // firefox preferences which we need to be changed to work well with vimperator set_firefox_pref("browser.startup.page", 3); // start with saved session @@ -421,13 +420,8 @@ function addEventListeners() if (!event.persisted) // only if not bypassing cache { var url = getCurrentLocation(); - var title = document.title; - for(var i=0; i