/*

    Minimum Profit 5.x
    A Programmer's Text Editor

    A simple IDE-like build system.

    Copyright (C) 1991-2013 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

*/

mp.config.make_opts = '';

/** editor actions **/

mp.actions['build']	= sub {
    local t, target;

    if ((t = mp.build_get_targets()) == NULL)
        mp.alert(L("No build targets found."));
    else {
        /* more than one target? ask user to pick one */
        if (size(t) > 1) {
            local r = mp.form(
                [
                    {
                        label:  L("Build target") ~ ':',
                        type:   'list',
                        list:   t,
                        value:  mp.makefile_target
                    }
                ]
            );

            if (r != NULL) {
                mp.makefile_target = r[0];
                target = t[mp.makefile_target];
            }
        }

        if (target != NULL)
            mp.long_op(mp.build_execute, target);
    }
};


/** Default key bindings **/

mp.keycodes['f2'] = "build";

/** action descriptions **/

mp.actdesc['build']             = LL("Build project...");


/** code **/

sub mp.build_get_targets()
{
    local r, f;
    local m = 'make';

    if (mp.config.make_opts)
        m ~= ' ' ~ mp.config.make_opts;

    if ((f = open('Makefile', 'r')) != NULL) {
        local l;

        r = [ m ];

        while (l = read(f)) {
            local t;

            if (t = regex(l, '/^[A-Za-z0-9_\.-]+:/'))
                push(r, m ~ ' ' ~ sregex(t, '/:/', ''));
        }

        close(f);
    }
    else
    if ((f = mp.active.syntax.build_targets) != NULL) {
        r = f->map(sub (e) { sprintf(e, mp.active.name); });
    }

    return r;
}


sub mp.build_execute(target)
{
    local log = mp.open('<build output>');

    log.txt.lines = [];
    log.txt.y = 0;

    /* pipe through make */
    local p;
    if ((p = popen(target, 'r')) != NULL) {
        local l;

        while (l = read(p))
            log->insert(mp.chomp(l) ~ "\n");

        pclose(p);

        log.txt.mod = 0;
        log.read_only = 1;

        log.syntax = mp.syntax.make_output;

        log->move_bof();

        /* set the last search regex to match file:line strings,
            so that calling seek-next and seek-prev moves there */
        mp.last_search = '/^[a-z\.\_0-9\/-]+:[0-9]+:/m';

        mp.redraw();
    }
    else
        mp.alert(sprintf(L("Error executing '%s'"), target));
}
