/*

    Minimum Profit 5.x
    A Programmer's Text Editor

    Templates.

    Copyright (C) 1991-2012 Angel Ortega <angel@triptico.com>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    http://triptico.com

*/

/** editor actions **/

mp_doc.actions['insert_template'] = sub(d) {
    local l;

    /* no local templates? do nothing */
    if ((l = d->long_op(d.read_templates_file)) != NULL) {

        /* select one */
        local t = mp.form(
            [
                {
                    label:  L("Template to insert:"),
                    type:   'list',
                    list:   l
                }
            ]
        );

        if (t != NULL) {
            t = t[0];

            d->store_undo();

            /* get the template content */
            local c = mp.templates[l[t]];

            /* does it start with #!? execute as MPSL */
            if (regex(l[t], "/^ *#!/")) {
                c = eval(c);

                if (ERROR)
                    mp.alert(ERROR);

                ERROR = NULL;
            }

            if (c)
                d->insert(c);
        }
    }
};

mp.actions['open_templates_file'] = sub {
    local templates = glob(HOMEDIR ~ "/.mp_templates/*"); /* Treat as a dir */

    if (size(templates) == 0) {
        mp.open(HOMEDIR ~ ".mp_templates");
    }
    else {
        if ((t = mp.form(
                [
                    {
                        label:  L("Template file to edit:"),
                        type:   'list',
                        list:   templates
                    }
                ]
            )) == NULL)
            return;

        mp.open(templates[t[0]]);
    }
};

/** default key bindings **/

/** action descriptions **/

mp.actdesc['insert_template']       = LL("Insert template...");
mp.actdesc['open_templates_file']   = LL("Edit templates file");

/** data **/

mp.templates = {};

/** code **/

sub mp_doc.read_templates_file(d)
/* reads the $HOME/.mp_templates or $HOME/.mp_templates/{global,syntax.name} file into mp.templates */
{
    local f, l, k, v, n;

    local read_template = sub(filename) {
        if ((f = open(filename, "r")) == NULL)
            return NULL;

        while (l = read(f)) {
            if (regex(l, "/^%%/")) {
                /* new template: store previous, if any */
                if (k && v) {
                    push(n, k);
                    mp.templates[k] = v;
                }

                /* strip prefix */
                k = sregex(mp.chomp(l), "/^%%/");
                v = NULL;
            }
            else {
                /* add to v */
                v = v ~ l;
            }
        }

        close(f);
    };

    k = NULL;
    v = NULL;
    n = [];

    /* reset */
    mp.templates = {};

    read_template(HOMEDIR ~ "/.mp_templates");
    read_template(HOMEDIR ~ "/.mp_templates/global");
    if (d.syntax != NULL) {
        read_template(HOMEDIR ~ "/.mp_templates/" ~ d.syntax.id);
    }

    /* store last value */
    if (k && v) {
        push(n, k);
        mp.templates[k] = v;
    }

    /* returns keys(mp.templates), but in its original order */
    return n;
}
