summaryrefslogtreecommitdiff
path: root/common/content/history.js
blob: fe410d734766480b00d921321fd74c325b9a081c (plain)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
// Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com>
// Copyright (c) 2008-2014 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";

var History = Module("history", {
    SORT_DEFAULT: "-date",

    get format() { return bookmarks.format; },

    get service() { return services.history; },

    get: function get(filter, maxItems, sort=this.SORT_DEFAULT) {
        if (isString(filter))
            filter = { searchTerms: filter };

        // no query parameters will get all history
        let query = services.history.getNewQuery();
        let options = services.history.getNewQueryOptions();

        for (let [k, v] of iter(filter))
            query[k] = v;

        let res = /^([+-])(.+)/.exec(sort);
        dactyl.assert(res, _("error.invalidSort", sort));

        let [, dir, field] = res;
        let _sort = "SORT_BY_" + field.toUpperCase() + "_" +
                    { "+": "ASCENDING", "-": "DESCENDING" }[dir];

        dactyl.assert(_sort in options,
                      _("error.invalidSort", sort));

        options.sortingMode = options[_sort];
        options.resultType = options.RESULTS_AS_URI;
        if (maxItems > 0)
            options.maxResults = maxItems;

        let root = services.history.executeQuery(query, options).root;
        root.containerOpen = true;
        try {
            var items = iter(util.range(0, root.childCount)).map(function (i) {
                let node = root.getChild(i);
                return {
                    url: node.uri,
                    title: node.title,
                    icon: node.icon ? node.icon : BookmarkCache.DEFAULT_FAVICON
                };
            }).toArray();
        }
        finally {
            root.containerOpen = false;
        }

        return items;
    },

    get session() {
        let webNav = window.getWebNavigation();
        let sh = webNav.sessionHistory;

        let obj = [];
        obj.__defineGetter__("index", () => sh.index);
        obj.__defineSetter__("index", val => { webNav.gotoIndex(val); });
        obj[Symbol.iterator] = function () { return this.entries(); };

        for (let item of iter(sh.SHistoryEnumerator, Ci.nsISHEntry))
            obj.push(update(Object.create(item), {
                index: obj.length,
                icon: Class.Memoize(function () {
                    return services.favicon
                                   .getFaviconImageForPage(this.URI)
                                   .spec;
                })
            }));
        return obj;
    },

    /**
     * Step to the given offset in the history stack.
     *
     * @param {number} steps The possibly negative number of steps to
     *      step.
     * @param {boolean} jumps If true, take into account jumps in the
     *      marks stack. @optional
     */
    stepTo: function stepTo(steps, jumps) {
        if (dactyl.forceOpen.target == dactyl.NEW_TAB)
            tabs.cloneTab(tabs.getTab(), true);

        if (jumps)
            steps -= marks.jump(steps);
        if (steps == 0)
            return;

        let sh = this.session;
        dactyl.assert(steps > 0 && sh.index < sh.length - 1 || steps < 0 && sh.index > 0);

        try {
            sh.index = Math.constrain(sh.index + steps, 0, sh.length - 1);
        }
        catch (e if e.result == Cr.NS_ERROR_FILE_NOT_FOUND) {}
    },

    /**
     * Search for the *steps*th next *item* in the history list.
     *
     * @param {string} item The nebulously defined item to search for.
     * @param {number} steps The number of steps to step.
     */
    search: function search(item, steps) {
        var ctxt;
        var filter = item => true;
        if (item == "domain")
            var filter = item => {
                let res = item.URI.hostPort != ctxt;
                ctxt = item.URI.hostPort;
                return res;
            };

        let sh = this.session;
        let idx;
        let sign = steps / Math.abs(steps);

        filter(sh[sh.index]);
        for (let i = sh.index + sign; steps && i >= 0 && i < sh.length; i += sign)
            if (filter(sh[i])) {
                idx = i;
                steps -= sign;
            }

        dactyl.assert(idx != null);
        sh.index = idx;
    },

    goToStart: function goToStart() {
        let index = window.getWebNavigation().sessionHistory.index;

        if (index > 0)
            window.getWebNavigation().gotoIndex(0);
        else
            dactyl.beep();

    },

    goToEnd: function goToEnd() {
        let sh = window.getWebNavigation().sessionHistory;
        let max = sh.count - 1;

        if (sh.index < max)
            window.getWebNavigation().gotoIndex(max);
        else
            dactyl.beep();

    },

    // if openItems is true, open the matching history items in tabs rather than display
    list: function list(filter, openItems, maxItems, sort) {
        // FIXME: returning here doesn't make sense
        //   Why the hell doesn't it make sense? --Kris
        // See comment at bookmarks.list --djk
        if (!openItems)
            return completion.listCompleter("history", filter, maxItems, maxItems, sort);
        let items = completion.runCompleter("history", filter, maxItems, maxItems, sort);

        if (items.length)
            return dactyl.open(items.map(i => i.url), dactyl.NEW_TAB);

        if (filter.length > 0)
            dactyl.echoerr(_("history.noMatching", JSON.stringify(filter)));
        else
            dactyl.echoerr(_("history.none"));
        return null;
    }
}, {
}, {
    commands: function initCommands() {
        commands.add(["ba[ck]"],
            "Go back in the browser history",
            function (args) {
                let url = args[0];

                if (args.bang)
                    history.goToStart();
                else {
                    if (url) {
                        let sh = history.session;
                        if (/^\d+(:|$)/.test(url) && sh.index - parseInt(url) in sh)
                            return void window.getWebNavigation().gotoIndex(sh.index - parseInt(url));

                        for (let [i, ent] of iter(sh.slice(0, sh.index).reverse()))
                            if (ent.URI.spec == url)
                                return void window.getWebNavigation().gotoIndex(i);
                        dactyl.echoerr(_("history.noURL"));
                    }
                    else
                        history.stepTo(-Math.max(args.count, 1));
                }
                return null;
            },
            {
                argCount: "?",
                bang: true,
                completer: function completer(context) {
                    let sh = history.session;

                    context.anchored = false;
                    context.compare = CompletionContext.Sort.unsorted;
                    context.filters = [CompletionContext.Filter.textDescription];
                    context.completions = sh.slice(0, sh.index).reverse();
                    context.keys = { text: function (item) (sh.index - item.index) + ": " + item.URI.spec,
                                     description: "title",
                                     icon: "icon" };
                },
                count: true,
                literal: 0,
                privateData: true
            });

        commands.add(["fo[rward]", "fw"],
            "Go forward in the browser history",
            function (args) {
                let url = args.literalArg;

                if (args.bang)
                    history.goToEnd();
                else {
                    if (url) {
                        let sh = history.session;
                        if (/^\d+(:|$)/.test(url) && sh.index + parseInt(url) in sh)
                            return void window.getWebNavigation().gotoIndex(sh.index + parseInt(url));

                        for (let [i, ent] of iter(sh.slice(sh.index + 1)))
                            if (ent.URI.spec == url)
                                return void window.getWebNavigation().gotoIndex(i);
                        dactyl.echoerr(_("history.noURL"));
                    }
                    else
                        history.stepTo(Math.max(args.count, 1));
                }
                return null;
            },
            {
                argCount: "?",
                bang: true,
                completer: function completer(context) {
                    let sh = history.session;

                    context.anchored = false;
                    context.compare = CompletionContext.Sort.unsorted;
                    context.filters = [CompletionContext.Filter.textDescription];
                    context.completions = sh.slice(sh.index + 1);
                    context.keys = { text: function (item) (item.index - sh.index) + ": " + item.URI.spec,
                                     description: "title",
                                     icon: "icon" };
                },
                count: true,
                literal: 0,
                privateData: true
            });

        commands.add(["hist[ory]", "hs"],
            "Show recently visited URLs",
            function (args) { history.list(args.join(" "), args.bang, args["-max"], args["-sort"]); }, {
                bang: true,
                completer: function (context, args) completion.history(context, args["-max"], args["-sort"]),
                options: [
                    {
                        names: ["-max", "-m"],
                        description: "The maximum number of items to list",
                        default: 1000,
                        type: CommandOption.INT
                    },
                    {
                        names: ["-sort", "-s"],
                        type: CommandOption.STRING,
                        description: "The sort order of the results",
                        completer: function (context) {
                            context.compare = CompletionContext.Sort.unsorted;
                            return Ary.flatten([
                                "annotation",
                                "date",
                                "date added",
                                "keyword",
                                "last modified",
                                "tags",
                                "title",
                                "uri",
                                "visitcount"
                            ].map(order => [
                                  ["+" + order.replace(" ", ""), /*L*/"Sort by " + order + " ascending"],
                                  ["-" + order.replace(" ", ""), /*L*/"Sort by " + order + " descending"]
                            ]));
                        }
                    }
                ],
                privateData: true
            });

        commands.add(["ju[mps]"],
            "Show jumplist",
            function () {
                let sh = history.session;
                let index = sh.index;

                let jumps = marks.jumps;
                if (jumps.index < 0)
                    jumps = [sh[sh.index]];
                else {
                    index += jumps.index;
                    jumps = jumps.locations.map(l => ({
                        __proto__: l,
                        title: buffer.title,
                        get URI() { return util.newURI(this.location); }
                    }));
                }

                let list = sh.slice(0, sh.index)
                             .concat(jumps)
                             .concat(sh.slice(sh.index + 1));

                commandline.commandOutput(template.jumps(index, list));
            },
            { argCount: "0" });

    },
    completion: function initCompletion() {
        completion.domain = context => {
            context.anchored = false;
            context.compare = (a, b) => String.localeCompare(a.key, b.key);
            context.keys = { text: util.identity, description: util.identity,
                key: function (host) host.split(".").reverse().join(".") };

            // FIXME: Schema-specific
            context.generate = () => [
                Array.slice(row.rev_host).reverse().join("").slice(1)
                for (row of iter(services.history.DBConnection
                                         .createStatement("SELECT DISTINCT rev_host FROM moz_places WHERE rev_host IS NOT NULL;")))
            ].slice(2);
        };

        completion.history = function _history(context, maxItems, sort) {
            context.format = history.format;
            context.title = ["History"];
            context.compare = CompletionContext.Sort.unsorted;
            //context.background = true;
            if (maxItems == null)
                context.maxItems = maxItems;
            if (maxItems && context.maxItems == null)
                context.maxItems = 100;
            context.regenerate = true;
            context.generate = function () {
                return history.get(context.filter, this.maxItems, sort);
            };
        };

        completion.addUrlCompleter("history", "History", completion.history);
    },
    mappings: function initMappings() {
        function bind(...args) {
            return apply(mappings, "add", [config.browserModes].concat(args));
        }

        bind(["<C-o>"], "Go to an older position in the jump list",
             function ({ count }) { history.stepTo(-Math.max(count, 1), true); },
             { count: true });

        bind(["<C-i>"], "Go to a newer position in the jump list",
             function ({ count }) { history.stepTo(Math.max(count, 1), true); },
             { count: true });

        bind(["H", "<A-Left>", "<M-Left>"], "Go back in the browser history",
             function ({ count }) { history.stepTo(-Math.max(count, 1)); },
             { count: true });

        bind(["L", "<A-Right>", "<M-Right>"], "Go forward in the browser history",
             function ({ count }) { history.stepTo(Math.max(count, 1)); },
             { count: true });

        bind(["[d"], "Go back to the previous domain in the browser history",
             function ({ count }) { history.search("domain", -Math.max(count, 1)); },
             { count: true });

        bind(["]d"], "Go forward to the next domain in the browser history",
             function ({ count }) { history.search("domain", Math.max(count, 1)); },
             { count: true });
    }
});

// vim: set fdm=marker sw=4 sts=4 ts=8 et: