<commit_before>// Copyright (c) 2006-2008 by <NAME> <<EMAIL>>
// Copyright (c) 2007-2011 by <NAME> <<EMAIL>>
// Copyright (c) 2008-2011 by <NAME> <<EMAIL>>
//
// 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", {
    get format() bookmarks.format,

    get service() services.history,

    get: function get(filter, maxItems, order) {
        // no query parameters will get all history
        let query = services.history.getNewQuery();
        let options = services.history.getNewQueryOptions();

        if (typeof filter == "string")
            filter = { searchTerms: filter };
        for (let [k, v] in Iterator(filter))
            query[k] = v;

        let _order = /^([+-])(.+)/.exec(order || "+date");
        dactyl.assert(_order, _("error.invalidSort", order));

        _order = "SORT_BY_" + _order[2].toUpperCase() + "_" +
                    (_order[1] == "+" ? "ASCENDING" : "DESCENDING");
        dactyl.assert(_order in options, _("error.invalidSort", order));

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

        let root = services.history.executeQuery(query, options).root;
        root.containerOpen = true;
        let 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 : DEFAULT_FAVICON
            };
        }).toArray();
        root.containerOpen = false;

        return items;
    },

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

        let obj = [];
        obj.__defineGetter__("index", function () sh.index);
        obj.__defineSetter__("index", function (val) { webNav.gotoIndex(val) });
        obj.__iterator__ = function () array.iterItems(this);

        for (let item in iter(sh.SHistoryEnumerator, Ci.nsIHistoryEntry))
            obj.push(update(Object.create(item), {
                index: obj.length,
                icon: Class.Memoize(function () 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 (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.<commit_msg>Make :tab :back/<C-t>H, and so forth, work.<commit_after>// Copyright (c) 2006-2008 by <NAME> <<EMAIL>>
// Copyright (c) 2007-2011 by <NAME> <<EMAIL>>
// Copyright (c) 2008-2011 by <NAME> <<EMAIL>>
//
// 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", {
    get format() bookmarks.format,

    get service() services.history,

    get: function get(filter, maxItems, order) {
        // no query parameters will get all history
        let query = services.history.getNewQuery();
        let options = services.history.getNewQueryOptions();

        if (typeof filter == "string")
            filter = { searchTerms: filter };
        for (let [k, v] in Iterator(filter))
            query[k] = v;

        let _order = /^([+-])(.+)/.exec(order || "+date");
        dactyl.assert(_order, _("error.invalidSort", order));

        _order = "SORT_BY_" + _order[2].toUpperCase() + "_" +
                    (_order[1] == "+" ? "ASCENDING" : "DESCENDING");
        dactyl.assert(_order in options, _("error.invalidSort", order));

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

        let root = services.history.executeQuery(query, options).root;
        root.containerOpen = true;
        let 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 : DEFAULT_FAVICON
            };
        }).toArray();
        root.containerOpen = false;

        return items;
    },

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

        let obj = [];
        obj.__defineGetter__("index", function () sh.index);
        obj.__defineSetter__("index", function (val) { webNav.gotoIndex(val) });
        obj.__iterator__ = function () array.iterItems(this);

        for (let item in iter(sh.SHistoryEnumerator, Ci.nsIHistoryEntry))
            obj.push(update(Object.create(item), {
                index: obj.length,
                icon: Class.Memoize(function () 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.
{'fraction_non_alphanumeric': 0.13275397063230446, 'fraction_numerical': 0.010488462691039856, 'mean_word_length': 2.0960111317254175, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6478987', 'n_tokens_mistral': 2061, 'n_tokens_neox': 1939, 'n_words': 536}