/*

    Minimum Profit 5.x
    A Programmer's Text Editor

    Miscellaneous editor actions.

    Copyright (C) 1991-2007 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://www.triptico.com

*/

/* editor actions */

mp.actions['eval']	= sub (d) { 
	local t = mp.form( [
		{ 'label' => L("Code to execute:"),
		  'type' => 'text',
		  'history' => 'eval' }
		]);

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

		if (! regex('/;\s*$/', t))
			t = t ~ ';';

		t = eval(t);

		if (ERROR != NULL) {
			mp.alert(L("Error: ") ~ ERROR);
			ERROR = NULL;
		}
		else
		if (t != NULL)
			mp.alert(L("Exit value:\n") ~ t);
	}
};

mp.actions['eval_doc']	= sub (d) {
	local t = join("\n", d.txt.lines);

	if (t != NULL) {
		t = eval(t);

		if (ERROR != NULL) {
			mp.alert(L("Error: ") ~ ERROR);

			/* try to move the cursor to the line
			   where the error was */
			local l;

			if ((l = regex( [ '/, line /', '/[0-9]+/' ], ERROR)) != NULL)
				mp.set_y(d, l[1]);

			ERROR = NULL;
		}
	}
};

mp.actions['encoding']	= sub (d) {
	local t = mp.form( [
		{ 'label' => L("Encoding (utf-8, iso8859-1, etc.; empty, current locale)") ~ ':',
		  'type' => 'text',
		  'history' => 'encoding' }
		]);

	if (t != NULL)
		if (encoding(t[0]) == -1)
			mp.alert(L("Invalid encoding ") ~ t[0]);
};

mp.actions['zoom_in'] 	= sub (d) {

	mp.config.font_size++;
	mp.update_ui();
};

mp.actions['zoom_out'] 	= sub (d) {

	if (mp.config.font_size > 4) {
		mp.config.font_size--;
		mp.update_ui();
	}
};

mp.actions['word_count'] = sub (d) {
	local w = 0;
	local a = mp.get_active_area(d);

	mp.busy(1);
	foreach (l, a)
		w += size(mp.split_by_words(l));
	mp.busy(0);

	mp.alert(sprintf(L("Lines: %d Words: %d"), size(a), w));
};

mp.actions['about'] = sub (d) {
	local msg = L(
	"\nMinimum Profit %s - Programmer Text Editor\n\n"\
	"Components: MPDM %s, MPSL %s\n\n"\
	"Copyright (C) 1991-2007 Angel Ortega <angel@triptico.com>\n"\
	"\n"\
	"This program is free software; you can redistribute it and/or\n"\
	"modify it under the terms of the GNU General Public License\n"\
	"as published by the Free Software Foundation; either version 2\n"\
	"of the License, or (at your option) any later version.\n"\
	"\n"\
	"This program is distributed in the hope that it will be useful,\n"\
	"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"\
	"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"\
	"See the GNU General Public License for more details.\n"\
	"\n"\
	"You should have received a copy of the GNU General Public License\n"\
	"along with this program; if not, write to the Free Software Foundation,\n"\
	"Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\n"\
	"\n"\
	"Home page: http://www.triptico.com/software/mp.html\n"\
	"Mailing list: mp-subscribe@lists.triptico.com\n");

	local mpdm = MPDM();
	msg = sprintf(msg, mp.VERSION, mpdm.version, MPSL.VERSION);

	d = mp.find_file_by_name("<about>");

	if (d != -1) {
		mp.active_i = d;
		d = mp.active();
	}
	else
		d = mp.new("<about>", split("\n", msg));
};

/* default key bindings */

mp.keycodes['escape']		= 'eval';
mp.keycodes['f12']		= 'zoom_in';
mp.keycodes['ctrl-kp-plus' ]	= 'zoom_in';
mp.keycodes['f11']		= 'zoom_out';
mp.keycodes['ctrl-kp-minus' ]	= 'zoom_out';

/* action descriptions */

mp.actdesc['eval']		= LL("Execute MPSL code...");
mp.actdesc['eval_doc']		= LL("Execute document as MPSL");
mp.actdesc['encoding']		= LL("Set charset encoding...");
mp.actdesc['zoom_in']		= LL("Bigger font");
mp.actdesc['zoom_out']		= LL("Smaller font");
mp.actdesc['word_count']	= LL("Count words");
mp.actdesc['about']		= LL("About...");

/* code */

sub dump(v)
/* overwrite of the MPSL dump() function, dumping over a text document */
{
	local d = mp.open("<dump>");

	mp.move_eof(d);
	mp.insert(d, dumper(v));
	d.txt.mod = 0;
}
