1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
// Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
// Copyright (c) 2007-2009 by Doug Kearns <dougkearns@gmail.com>
// Copyright (c) 2008-2010 by Kris Maglione <maglione.k@gmail.com>
//
// This work is licensed for reuse under an MIT license. Details are
// given in the LICENSE.txt file included with this file.
"use strict";
/**
* @scope modules
* @instance marks
*/
const Marks = Module("marks", {
init: function init() {
function replacer(key, val) val instanceof Ci.nsISupports ? null : val;
this._localMarks = storage.newMap("local-marks", { privateData: true, replacer: replacer, store: true });
this._urlMarks = storage.newMap("url-marks", { privateData: true, replacer: replacer, store: true });
try {
if (isArray(Iterator(this._localMarks).next()[1]))
this._localMarks.clear();
}
catch (e) {}
this._pendingJumps = [];
},
/**
* @property {Array} Returns all marks, both local and URL, in a sorted
* array.
*/
get all() {
let lmarks = array(Iterator(this._localMarks.get(this.localURI) || {}));
let umarks = array(Iterator(this._urlMarks)).array;
return lmarks.concat(umarks).sort(function (a, b) String.localeCompare(a[0], b[0]));
},
get localURI() buffer.focusedFrame.document.documentURI,
/**
* Add a named mark for the current buffer, at its current position.
* If mark matches [A-Z], it's considered a URL mark, and will jump to
* the same position at the same URL no matter what buffer it's
* selected from. If it matches [a-z'"], it's a local mark, and can
* only be recalled from a buffer with a matching URL.
*
* @param {string} mark The mark name.
* @param {boolean} silent Whether to output error messages.
*/
add: function (mark, silent) {
let win = buffer.focusedFrame;
let doc = win.document;
let x = win.scrollMaxX ? win.pageXOffset / win.scrollMaxX : 0;
let y = win.scrollMaxY ? win.pageYOffset / win.scrollMaxY : 0;
let position = { x: x, y: y };
if (Marks.isURLMark(mark)) {
let res = this._urlMarks.set(mark, { location: doc.URL, position: position, tab: Cu.getWeakReference(tabs.getTab()), timestamp: Date.now()*1000 });
if (!silent)
dactyl.log("Adding URL mark: " + Marks.markToString(mark, res), 5);
}
else if (Marks.isLocalMark(mark)) {
let marks = this._localMarks.get(doc.documentURI, {});
marks[mark] = { location: doc.documentURI, position: position, timestamp: Date.now()*1000 };
this._localMarks.changed();
if (!silent)
dactyl.log("Adding local mark: " + Marks.markToString(mark, marks[mark]), 5);
}
},
/**
* Remove all marks matching <b>filter</b>. If <b>special</b> is
* given, removes all local marks.
*
* @param {string} filter A string containing one character for each
* mark to be removed.
* @param {boolean} special Whether to delete all local marks.
*/
remove: function (filter, special) {
if (special)
this._localMarks.remove(this.localURI);
else {
let local = this._localMarks.get(this.localURI);
Array.forEach(filter, function (mark) {
delete local[mark];
this.urlMarks.remove(mark);
});
try {
Iterator(local).next();
this._localMarks.changed();
}
catch (e) {
this._localMarks.remove(this.localURI);
}
}
},
/**
* Jumps to the named mark. See {@link #add}
*
* @param {string} mark The mark to jump to.
*/
jumpTo: function (mark) {
let ok = false;
if (Marks.isURLMark(mark)) {
let slice = this._urlMarks.get(mark);
let tab = slice && slice.tab && slice.tab.get();
if (tab && tab.linkedBrowser) {
if (tab.parentNode != config.browser.tabContainer) {
this._pendingJumps.push(slice);
// NOTE: this obviously won't work on generated pages using
// non-unique URLs :(
dactyl.open(slice.location, dactyl.NEW_TAB);
return;
}
let index = tabs.index(tab);
if (index != -1) {
tabs.select(index);
let win = tab.linkedBrowser.contentWindow;
if (win.location.href != slice.location) {
this._pendingJumps.push(slice);
win.location.href = slice.location;
return;
}
dactyl.log("Jumping to URL mark: " + Marks.markToString(mark, slice), 5);
buffer.scrollToPercent(slice.position.x * 100, slice.position.y * 100);
ok = true;
}
}
}
else if (Marks.isLocalMark(mark)) {
ok = (this._localMarks.get(this.localURI) || {})[mark];
if (ok) {
dactyl.log("Jumping to local mark: " + Marks.markToString(mark, ok), 5);
buffer.scrollToPercent(ok.position.x * 100, ok.position.y * 100);
}
}
if (!ok)
dactyl.echoerr("E20: Mark not set");
},
/**
* List all marks matching <b>filter</b>.
*
* @param {string} filter
*/
list: function (filter) {
let marks = this.all;
dactyl.assert(marks.length > 0, "No marks set");
if (filter.length > 0) {
marks = marks.filter(function (mark) filter.indexOf(mark[0]) >= 0);
dactyl.assert(marks.length > 0, "E283: No marks matching " + filter.quote());
}
commandline.commandOutput(
template.tabular(
["Mark", "Line", "Column", "File"],
["", "text-align: right", "text-align: right", "color: green"],
([mark[0],
Math.round(mark[1].position.x * 100) + "%",
Math.round(mark[1].position.y * 100) + "%",
mark[1].location]
for ([, mark] in Iterator(marks)))));
},
_onPageLoad: function _onPageLoad(event) {
let win = event.originalTarget.defaultView;
for (let [i, mark] in Iterator(this._pendingJumps)) {
if (win && win.location.href == mark.location) {
buffer.scrollToPercent(mark.position.x * 100, mark.position.y * 100);
this._pendingJumps.splice(i, 1);
return;
}
}
},
}, {
markToString: function markToString(name, mark) {
let tab = mark.tab && mark.tab.get();
return name + ", " + mark.location +
", (" + Math.round(mark.position.x * 100) +
"%, " + Math.round(mark.position.y * 100) + "%)" +
(tab ? ", tab: " + tabs.index(tab) : "");
},
isLocalMark: function isLocalMark(mark) /^['`a-z]$/.test(mark),
isURLMark: function isURLMark(mark) /^[A-Z0-9]$/.test(mark)
}, {
events: function () {
let appContent = document.getElementById("appcontent");
if (appContent)
events.addSessionListener(appContent, "load", this.closure._onPageLoad, true);
},
mappings: function () {
var myModes = config.browserModes;
mappings.add(myModes,
["m"], "Set mark at the cursor position",
function (arg) {
dactyl.assert(/^[a-zA-Z]$/.test(arg));
marks.add(arg);
},
{ arg: true });
mappings.add(myModes,
["'", "`"], "Jump to the mark in the current buffer",
function (arg) { marks.jumpTo(arg); },
{ arg: true });
},
commands: function () {
commands.add(["delm[arks]"],
"Delete the specified marks",
function (args) {
let special = args.bang;
args = args[0] || "";
// assert(special ^ args)
dactyl.assert( special || args, "E471: Argument required");
dactyl.assert(!special || !args, "E474: Invalid argument");
let matches = args.match(/(?:(?:^|[^a-zA-Z0-9])-|-(?:$|[^a-zA-Z0-9])|[^a-zA-Z0-9 -]).*/);
// NOTE: this currently differs from Vim's behavior which
// deletes any valid marks in the arg list, up to the first
// invalid arg, as well as giving the error message.
dactyl.assert(!matches, "E475: Invalid argument: " + (matches && matches[0]));
// check for illegal ranges - only allow a-z A-Z 0-9
if ((matches = args.match(/[a-zA-Z0-9]-[a-zA-Z0-9]/g))) {
for (let match in values(matches))
dactyl.assert(/[a-z]-[a-z]|[A-Z]-[A-Z]|[0-9]-[0-9]/.test(match) &&
match[0] <= match[2],
"E475: Invalid argument: " + args.match(match + ".*")[0]);
}
marks.remove(args, special);
},
{
bang: true,
completer: function (context) completion.mark(context),
literal: 0
});
commands.add(["ma[rk]"],
"Mark current location within the web page",
function (args) {
let mark = args[0] || "";
dactyl.assert(mark.length <= 1, "E488: Trailing characters");
dactyl.assert(/[a-zA-Z]/.test(mark),
"E191: Argument must be a letter or forward/backward quote");
marks.add(mark);
},
{ argCount: "1" });
commands.add(["marks"],
"Show all location marks of current web page",
function (args) {
args = args[0] || "";
// ignore invalid mark characters unless there are no valid mark chars
dactyl.assert(!args || /[a-zA-Z]/.test(args),
"E283: No marks matching " + args.quote());
let filter = args.replace(/[^a-zA-Z]/g, "");
marks.list(filter);
}, {
literal: 0
});
},
completion: function () {
completion.mark = function mark(context) {
function percent(i) Math.round(i * 100);
// FIXME: Line/Column doesn't make sense with %
context.title = ["Mark", "Line Column File"];
context.keys.description = function ([, m]) percent(m.position.y) + "% " + percent(m.position.x) + "% " + m.location;
context.completions = marks.all;
};
},
sanitizer: function () {
sanitizer.addItem("marks", {
description: "Local and URL marks",
persistent: true,
contains: ["history"],
action: function (timespan, host) {
function matchhost(url) !host || util.isDomainURL(url, host);
function match(marks) (k for ([k, v] in Iterator(marks)) if (timespan.contains(v.timestamp) && matchhost(v.location)));
for (let [url, local] in storage["local-marks"])
if (matchhost(url)) {
for (let key in match(local))
delete local[key];
if (!Object.keys(local).length)
storage["local-marks"].remove(url);
}
storage["local-marks"].changed();
for (let key in match(storage["url-marks"]))
storage["url-marks"].remove(key);
}
});
}
});
// vim: set fdm=marker sw=4 ts=4 et:
|