<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 */

/** @scope modules */

var AutoCommand = Struct("event", "filter", "command");
update(AutoCommand.prototype, {
    eventName: Class.Memoize(function () this.event.toLowerCase()),

    match: function (event, pattern) {
        return (!event || this.eventName == event.toLowerCase()) && (!pattern || String(this.filter) === String(pattern));
    }
});

var AutoCmdHive = Class("AutoCmdHive", Contexts.Hive, {
    init: function init(group) {
        init.supercall(this, group);
        this._store = [];
    },

    __iterator__: function () array.iterValues(this._store),

    /**
     * Adds a new autocommand. *cmd* will be executed when one of the specified
     * *events* occurs and the URL of the applicable buffer matches *regexp*.
     *
     * @param {Array} events The array of event names for which this
     *     autocommand should be executed.
     * @param {string} pattern The URL pattern to match against the buffer URL.
     * @param {string} cmd The Ex command to run.
     */
    add: function (events, pattern, cmd) {
        if (!callable(pattern))
            pattern = Group.compileFilter(pattern);

        for (let event in values(events))
            this._store.push(AutoCommand(event, pattern, cmd));
    },

    /**
     * Returns all autocommands with a matching *event* and *filter*.
     *
     * @param {string} event The event name filter.
     * @param {string} filter The URL pattern filter.
     * @returns {[AutoCommand]}
     */
    get: function (event, filter) {
        filter = filter && String(Group.compileFilter(filter));
        return this._store.filter(function (autoCmd) autoCmd.match(event, filter));
    },

    /**
     * Deletes all autocommands with a matching *event* and *filter*.
     *
     * @param {string} event The event name filter.
     * @param {string} filter The URL pattern filter.
     */
    remove: function (event, filter) {
        filter = filter && String(Group.compileFilter(filter));
        this._store = this._store.filter(function (autoCmd) !autoCmd.match(event, filter));
    },
});

/**
 * @instance autocommands
 */
var AutoCommands = Module("autocommands", {
    init: function () {
    },

    get activeHives() contexts.allGroups.autocmd.filter(function (h) h._store.length),

    add: deprecated("group.autocmd.add", { get: function add() autocommands.user.closure.add }),
    get: deprecated("group.autocmd.get", { get: function get() autocommands.user.closure.get }),
    remove: deprecated("group.autocmd.remove", { get: function remove() autocommands.user.closure.remove }),

    /**
     * Lists all autocommands with a matching *event*, *regexp* and optionally
     * *hives*.
     *
     * @param {string} event The event name filter.
     * @param {string} regexp The URL pattern filter.
     * @param {[Hive]} hives List of hives.
     * @optional
     */
    list: function (event, regexp, hives) {

        let hives = hives || this.activeHives;

        function cmds(hive) {
            let cmds = {};
            hive._store.forEach(function (autoCmd) {
                if (autoCmd.match(event, regexp)) {
                    cmds[autoCmd.event] = cmds[autoCmd.event] || [];
                    cmds[autoCmd.event].push(autoCmd);
                }
            });
            return cmds;
        }

        XML.prettyPrinting = XML.ignoreWhitespace = false;
        commandline.commandOutput(
            <table>
                <tr highlight="Title">
                    <td colspan="3">----- Auto Commands -----</td>
                </tr>
                {
                    template.map(hives, function (hive)
                        <tr>
                            <td colspan="3"><span highlight="Title">{hive.name}</span>
                                            {hive.filter}</td>
                        </tr> +
                        <tr style="height: .5ex;"/> +
                        template.map(cmds(hive), function ([event, items])
                            <tr style="height: .5ex;"/> +
                            template.map(items, function (item, i)
                                <tr>
                                    <td highlight="Title" style="padding-left: 1em; padding-right: 1em;">{i == 0 ? event : ""}</td>
                                    <td>{item.filter.toXML ? item.filter.toXML() : item.filter}</td>
                                    <td>{item.command}</td>
                                </tr>) +
                            <tr style="height: .5ex;"/>) +
                        <tr style="height: .5ex;"/>)
                }
            </table>);
    },

    /**
     * Triggers the execution of all autocommands registered for *event*. A map
     * of *args* is passed to each autocommand when it is being executed.
     *
     * @param {string} event The event to fire.
     * @param {Object} args The args to pass to each autocommand.
     */<commit_msg>Death to E4X and stuff.<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 */

/** @scope modules */

var AutoCommand = Struct("event", "filter", "command");
update(AutoCommand.prototype, {
    eventName: Class.Memoize(function () this.event.toLowerCase()),

    match: function (event, pattern) {
        return (!event || this.eventName == event.toLowerCase()) && (!pattern || String(this.filter) === String(pattern));
    }
});

var AutoCmdHive = Class("AutoCmdHive", Contexts.Hive, {
    init: function init(group) {
        init.supercall(this, group);
        this._store = [];
    },

    __iterator__: function () array.iterValues(this._store),

    /**
     * Adds a new autocommand. *cmd* will be executed when one of the specified
     * *events* occurs and the URL of the applicable buffer matches *regexp*.
     *
     * @param {Array} events The array of event names for which this
     *     autocommand should be executed.
     * @param {string} pattern The URL pattern to match against the buffer URL.
     * @param {string} cmd The Ex command to run.
     */
    add: function (events, pattern, cmd) {
        if (!callable(pattern))
            pattern = Group.compileFilter(pattern);

        for (let event in values(events))
            this._store.push(AutoCommand(event, pattern, cmd));
    },

    /**
     * Returns all autocommands with a matching *event* and *filter*.
     *
     * @param {string} event The event name filter.
     * @param {string} filter The URL pattern filter.
     * @returns {[AutoCommand]}
     */
    get: function (event, filter) {
        filter = filter && String(Group.compileFilter(filter));
        return this._store.filter(function (autoCmd) autoCmd.match(event, filter));
    },

    /**
     * Deletes all autocommands with a matching *event* and *filter*.
     *
     * @param {string} event The event name filter.
     * @param {string} filter The URL pattern filter.
     */
    remove: function (event, filter) {
        filter = filter && String(Group.compileFilter(filter));
        this._store = this._store.filter(function (autoCmd) !autoCmd.match(event, filter));
    },
});

/**
 * @instance autocommands
 */
var AutoCommands = Module("autocommands", {
    init: function () {
    },

    get activeHives() contexts.allGroups.autocmd.filter(function (h) h._store.length),

    add: deprecated("group.autocmd.add", { get: function add() autocommands.user.closure.add }),
    get: deprecated("group.autocmd.get", { get: function get() autocommands.user.closure.get }),
    remove: deprecated("group.autocmd.remove", { get: function remove() autocommands.user.closure.remove }),

    /**
     * Lists all autocommands with a matching *event*, *regexp* and optionally
     * *hives*.
     *
     * @param {string} event The event name filter.
     * @param {string} regexp The URL pattern filter.
     * @param {[Hive]} hives List of hives.
     * @optional
     */
    list: function (event, regexp, hives) {

        let hives = hives || this.activeHives;

        function cmds(hive) {
            let cmds = {};
            hive._store.forEach(function (autoCmd) {
                if (autoCmd.match(event, regexp)) {
                    cmds[autoCmd.event] = cmds[autoCmd.event] || [];
                    cmds[autoCmd.event].push(autoCmd);
                }
            });
            return cmds;
        }

        XML.prettyPrinting = XML.ignoreWhitespace = false;
        let table = (
            ["table", {},
                ["tr", { highlight: "Title" },
                    ["td", { colspan: "3" }, "----- Auto Commands -----"]],
                hives.map(function (hive) [
                    ["tr", {},
                        ["td", { colspan: "3" },
                            ["span", { highlight: "Title" }, hive.name],
                            " ", /* E4X-FIXME: Filter is formatted E4X. */ String(hive.filter)]],
                    ["tr", { style: "height: .5ex;" }],
                    iter(cmds(hive)).map(function ([event, items]) [
                        ["tr", { style: "height: .5ex;" }],
                        items.map(function (item, i)
                            ["tr", {},
                                ["td", { highlight: "Title", style: "padding-left: 1em; padding-right: 1em;" },
                                    i == 0 ? event : ""],
                                // E4X-FIXME: ["td", {}, item.filter.toXML ? item.filter.toXML() : String(item.filter)],
                                ["td", {}, String(item.filter)],
                                ["td", {}, String(item.command)]]),
                        ["tr", { style: "height: .5ex;" }]]).toArray(),
                    ["tr", { style: "height: .5ex;" }],
                ])]);
        commandline.commandOutput(table);
    },

    /**
     * Triggers the execution of all autocommands registered for *event*. A map
     * of *args* is passed to each autocommand when it is being executed.
     *
     * @param {string} event The event to fire.
     * @param {Object} args The args to pass to each autocommand.
     */
{'fraction_non_alphanumeric': 0.14237063664743166, 'fraction_numerical': 0.006621890076624728, 'mean_word_length': 1.9555493430248811, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1053083', 'n_tokens_mistral': 3080, 'n_tokens_neox': 2957, 'n_words': 837}