1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-22 11:07:58 +01:00

allow :marks to take an argument and sort its output properly

This commit is contained in:
Doug Kearns
2007-07-02 07:40:42 +00:00
parent cfa515f62e
commit 3372fee9e9
2 changed files with 115 additions and 64 deletions

View File

@@ -7,6 +7,7 @@ function Bookmarks() //{{{
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
////////////////////// PRIVATE SECTION ///////////////////////////////////////// ////////////////////// PRIVATE SECTION /////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
const search_service = Components.classes["@mozilla.org/browser/search-service;1"]. const search_service = Components.classes["@mozilla.org/browser/search-service;1"].
getService(Components.interfaces.nsIBrowserSearchService); getService(Components.interfaces.nsIBrowserSearchService);
const rdf_service = Components.classes["@mozilla.org/rdf/rdf-service;1"]. const rdf_service = Components.classes["@mozilla.org/rdf/rdf-service;1"].
@@ -41,6 +42,7 @@ function Bookmarks() //{{{
/////////////////////////////////////////////////////////////////////////////}}} /////////////////////////////////////////////////////////////////////////////}}}
////////////////////// PUBLIC SECTION ////////////////////////////////////////// ////////////////////// PUBLIC SECTION //////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
/* /*
* @return a new Array() of our bookmarks * @return a new Array() of our bookmarks
*/ */
@@ -269,6 +271,7 @@ function History() //{{{
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
////////////////////// PRIVATE SECTION ///////////////////////////////////////// ////////////////////// PRIVATE SECTION /////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
const rdf_service = Components.classes["@mozilla.org/rdf/rdf-service;1"]. const rdf_service = Components.classes["@mozilla.org/rdf/rdf-service;1"].
getService( Components.interfaces.nsIRDFService ); getService( Components.interfaces.nsIRDFService );
const global_history_service = Components.classes["@mozilla.org/browser/global-history;2"]. const global_history_service = Components.classes["@mozilla.org/browser/global-history;2"].
@@ -318,9 +321,11 @@ function History() //{{{
history.push([url, title]); history.push([url, title]);
} }
} }
/////////////////////////////////////////////////////////////////////////////}}} /////////////////////////////////////////////////////////////////////////////}}}
////////////////////// PUBLIC SECTION ////////////////////////////////////////// ////////////////////// PUBLIC SECTION //////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
/* /*
* @return a new Array() of our bookmarks * @return a new Array() of our bookmarks
*/ */
@@ -384,7 +389,7 @@ function Marks() //{{{
{ {
if (local_marks[mark][i].location == win.location.href) if (local_marks[mark][i].location == win.location.href)
{ {
vimperator.log("Deleting local mark: " + mark + " | " + local_marks[mark][i].location + " | (" + local_marks[mark][i].position.x + ", " + local_marks[mark][i].position.y + ") | tab: " + vimperator.tabs.index(local_marks[mark][i].tab)); vimperator.log("Deleting local mark: " + mark + " | " + local_marks[mark][i].location + " | (" + local_marks[mark][i].position.x + ", " + local_marks[mark][i].position.y + ") | tab: " + vimperator.tabs.index(local_marks[mark][i].tab), 5);
local_marks[mark].splice(i, 1); local_marks[mark].splice(i, 1);
if (local_marks[mark].length == 0) if (local_marks[mark].length == 0)
delete local_marks[mark]; delete local_marks[mark];
@@ -398,7 +403,7 @@ function Marks() //{{{
{ {
if (mark in url_marks) if (mark in url_marks)
{ {
vimperator.log("Deleting URL mark: " + mark + " | " + url_marks[mark].location + " | (" + url_marks[mark].position.x + ", " + url_marks[mark].position.y + ") | tab: " + vimperator.tabs.index(url_marks[mark].tab)); vimperator.log("Deleting URL mark: " + mark + " | " + url_marks[mark].location + " | (" + url_marks[mark].position.x + ", " + url_marks[mark].position.y + ") | tab: " + vimperator.tabs.index(url_marks[mark].tab), 5);
delete url_marks[mark]; delete url_marks[mark];
} }
} }
@@ -413,6 +418,37 @@ function Marks() //{{{
return /^[A-Z0-9]$/.test(mark); return /^[A-Z0-9]$/.test(mark);
} }
function getSortedMarks()
{
// local marks
var lmarks = [];
for (var mark in local_marks)
{
for (var i = 0; i < local_marks[mark].length; i++)
{
if (local_marks[mark][i].location == window.content.location.href)
lmarks.push([mark, local_marks[mark][i]]);
}
}
lmarks.sort();
// URL marks
var umarks = [];
for (var mark in url_marks)
umarks.push([mark, url_marks[mark]]);
// FIXME: why does umarks.sort() cause a "Component is not available =
// NS_ERROR_NOT_AVAILABLE" exception when used here?
umarks.sort(function(a, b) {
if (a[0] < b[0])
return -1
else if (a[0] > b[0])
return 1;
return 0
});
return lmarks.concat(umarks);
}
/////////////////////////////////////////////////////////////////////////////}}} /////////////////////////////////////////////////////////////////////////////}}}
////////////////////// PUBLIC SECTION ////////////////////////////////////////// ////////////////////// PUBLIC SECTION //////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
@@ -433,7 +469,7 @@ function Marks() //{{{
var position = { x: x, y: y }; var position = { x: x, y: y };
if (isURLMark(mark)) if (isURLMark(mark))
{ {
vimperator.log("Adding URL mark: " + mark + " | " + win.location.href + " | (" + position.x + ", " + position.y + ") | tab: " + vimperator.tabs.index(vimperator.tabs.getTab())); vimperator.log("Adding URL mark: " + mark + " | " + win.location.href + " | (" + position.x + ", " + position.y + ") | tab: " + vimperator.tabs.index(vimperator.tabs.getTab()), 5);
url_marks[mark] = { location: win.location.href, position: position, tab: vimperator.tabs.getTab() }; url_marks[mark] = { location: win.location.href, position: position, tab: vimperator.tabs.getTab() };
} }
else if (isLocalMark(mark)) else if (isLocalMark(mark))
@@ -442,12 +478,12 @@ function Marks() //{{{
removeLocalMark(mark); removeLocalMark(mark);
if (!local_marks[mark]) if (!local_marks[mark])
local_marks[mark] = []; local_marks[mark] = [];
vimperator.log("Adding local mark: " + mark + " | " + win.location.href + " | (" + position.x + ", " + position.y + ")"); vimperator.log("Adding local mark: " + mark + " | " + win.location.href + " | (" + position.x + ", " + position.y + ")", 5);
local_marks[mark].push({ location: win.location.href, position: position }); local_marks[mark].push({ location: win.location.href, position: position });
} }
} }
this.remove = function(marks_str, special) this.remove = function(filter, special)
{ {
if (special) if (special)
{ {
@@ -457,7 +493,7 @@ function Marks() //{{{
} }
else else
{ {
var pattern = new RegExp("[" + marks_str.replace(/\s+/g, '') + "]"); var pattern = new RegExp("[" + filter.replace(/\s+/g, '') + "]");
for (var mark in url_marks) for (var mark in url_marks)
{ {
if (pattern.test(mark)) if (pattern.test(mark))
@@ -498,7 +534,7 @@ function Marks() //{{{
win.location.href = slice.location; win.location.href = slice.location;
return; return;
} }
vimperator.log("Jumping to URL mark: " + mark + " | " + slice.location + " | (" + slice.position.x + ", " + slice.position.y + ") | tab: " + vimperator.tabs.index(slice.tab)); vimperator.log("Jumping to URL mark: " + mark + " | " + slice.location + " | (" + slice.position.x + ", " + slice.position.y + ") | tab: " + vimperator.tabs.index(slice.tab), 5);
win.scrollTo(slice.position.x * win.scrollMaxX, slice.position.y * win.scrollMaxY); win.scrollTo(slice.position.x * win.scrollMaxX, slice.position.y * win.scrollMaxY);
ok = true; ok = true;
} }
@@ -512,7 +548,7 @@ function Marks() //{{{
{ {
if (win.location.href == slice[i].location) if (win.location.href == slice[i].location)
{ {
vimperator.log("Jumping to local mark: " + mark + " | " + slice[i].location + " | (" + slice[i].position.x + ", " + slice[i].position.y + ")"); vimperator.log("Jumping to local mark: " + mark + " | " + slice[i].location + " | (" + slice[i].position.x + ", " + slice[i].position.y + ")", 5);
win.scrollTo(slice[i].position.x * win.scrollMaxX, slice[i].position.y * win.scrollMaxY); win.scrollTo(slice[i].position.x * win.scrollMaxX, slice[i].position.y * win.scrollMaxY);
ok = true; ok = true;
} }
@@ -523,32 +559,41 @@ function Marks() //{{{
vimperator.echoerr("E20: Mark not set"); // FIXME: move up? vimperator.echoerr("E20: Mark not set"); // FIXME: move up?
} }
// TODO: show marks like vim does (when the multiline echo impl is done) or in the preview window right now this.list = function(filter)
this.list = function()
{ {
// FIXME: hashes don't have a .length property --mst var marks = getSortedMarks();
// if (local_marks.length + url_marks.length < 1)
// { if (marks.length == 0)
// vimperator.echoerr("No marks defined"); {
// return; vimperator.echoerr("No marks set");
// } return;
}
if (filter.length > 0)
{
marks = marks.filter(function(mark) {
if (filter.indexOf(mark[0]) > -1)
return mark;
});
if (marks.length == 0)
{
vimperator.echoerr("E283: No marks matching \"" + filter + "\"");
return;
}
}
var list = "<table><tr style=\"color: magenta\"><td>mark</td><td>line</td><td>col</td><td>file</td></tr>"; var list = "<table><tr style=\"color: magenta\"><td>mark</td><td>line</td><td>col</td><td>file</td></tr>";
for (var i in local_marks) for (var i = 0; i < marks.length; i++)
{ {
list += "<tr><td>&nbsp;" + i + "</td><td align=\"right\">" list += "<tr>"
+ Math.round(local_marks[i][0].position.y *100)+ "%</td><td align=\"right\">" + "<td>&nbsp;" + marks[i][0] + "</td>"
+ Math.round(local_marks[i][0].position.x *100)+ "%</td><td>" + "<td align=\"right\">" + Math.round(marks[i][1].position.y * 100) + "%</td>"
+ local_marks[i][0].location + "</td></tr>"; + "<td align=\"right\">" + Math.round(marks[i][1].position.x * 100) + "%</td>"
} + "<td>" + marks[i][1].location + "</td>"
for (var j in url_marks) + "</tr>";
{
list += "<tr><td>&nbsp;" + j + "</td><td align=\"right\">"
+ Math.round(url_marks[j].position.y *100)+ "%</td><td align=\"right\">"
+ Math.round(url_marks[j].position.x *100)+ "%</td><td>"
+ url_marks[j].location + "</td></tr>";
} }
list += "</table>"; list += "</table>";
vimperator.commandline.echo(list, true); // TODO: force of multiline widget a better way vimperator.commandline.echo(list, true); // TODO: force of multiline widget a better way
} }
//}}} //}}}
@@ -561,7 +606,7 @@ function QuickMarks() //{{{
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
var marks = {}; var marks = {};
// load the commandline history -- TODO: change to sqlite // load the saved quickmarks -- TODO: change to sqlite
var saved_marks = Options.getPref("quickmarks", "").split("\n"); var saved_marks = Options.getPref("quickmarks", "").split("\n");
for (var i = 0; i < saved_marks.length - 1; i += 2) for (var i = 0; i < saved_marks.length - 1; i += 2)
{ {
@@ -577,9 +622,9 @@ function QuickMarks() //{{{
marks[mark] = location; marks[mark] = location;
} }
this.remove = function(marks_str) this.remove = function(filter)
{ {
var pattern = new RegExp("[" + marks_str.replace(/\s+/g, '') + "]"); var pattern = new RegExp("[" + filter.replace(/\s+/g, '') + "]");
for (var mark in marks) for (var mark in marks)
{ {
if (pattern.test(mark)) if (pattern.test(mark))
@@ -603,21 +648,20 @@ function QuickMarks() //{{{
this.list = function() this.list = function()
{ {
// FIXME: hashes don't have a .length property --mst var is_empty = true;
// if (local_marks.length + url_marks.length < 1)
// {
// vimperator.echoerr("No marks defined");
// return;
// }
var list = "<table><tr style=\"color: magenta\"><td>mark</td><td>URL</td></tr>"; var list = "<table><tr style=\"color: magenta\"><td>mark</td><td>URL</td></tr>";
for (var i in marks) for (var i in marks)
{ {
is_empty = false;
list += "<tr><td>&nbsp;" + i + "</td><td>" list += "<tr><td>&nbsp;" + i + "</td><td>"
+ marks[i] + "</td></tr>"; + marks[i] + "</td></tr>";
} }
list += "</table>"; list += "</table>";
if (!is_empty)
vimperator.commandline.echo(list, true); // TODO: force of multiline widget a better way vimperator.commandline.echo(list, true); // TODO: force of multiline widget a better way
else
vimperator.echoerr("No quickmarks defined");
} }
this.destroy = function() this.destroy = function()
@@ -633,4 +677,5 @@ function QuickMarks() //{{{
} }
//}}} //}}}
} //}}} } //}}}
// vim: set fdm=marker sw=4 ts=4 et: // vim: set fdm=marker sw=4 ts=4 et:

View File

@@ -515,7 +515,13 @@ function Commands() //{{{
)); ));
addDefaultCommand(new Command(["marks"], addDefaultCommand(new Command(["marks"],
function(args) { function(args) {
vimperator.marks.list(args) if (args.length > 1 && !/[a-zA-Z]/.test(args))
{
vimperator.echoerr("E283: No marks matching \"" + args + "\"");
return;
}
var filter = args.replace(/[^a-zA-Z]/g, '');
vimperator.marks.list(filter)
}, },
{ {
usage: ["marks {arg}"], usage: ["marks {arg}"],