2001-08-24 22:23:17 +04:00
|
|
|
/* editor initialisation and callback handler.
|
|
|
|
|
2007-09-25 19:33:35 +04:00
|
|
|
Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
|
|
|
|
2007 Free Software Foundation, Inc.
|
2001-08-24 22:23:17 +04:00
|
|
|
|
|
|
|
Authors: 1996, 1997 Paul Sheer
|
|
|
|
|
|
|
|
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
|
2005-05-27 07:35:10 +04:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
02110-1301, USA.
|
2001-08-24 22:23:17 +04:00
|
|
|
*/
|
|
|
|
|
2009-02-07 00:09:50 +03:00
|
|
|
/** \file
|
|
|
|
* \brief Source: editor initialisation and callback handler
|
|
|
|
* \author Paul Sheer
|
|
|
|
* \date 1996, 1997
|
|
|
|
*/
|
|
|
|
|
2001-08-24 22:23:17 +04:00
|
|
|
#include <config.h>
|
2005-02-07 09:31:04 +03:00
|
|
|
|
2005-02-22 20:00:36 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/types.h>
|
2009-02-06 15:44:53 +03:00
|
|
|
#include <unistd.h>
|
2005-02-22 20:00:36 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "../src/global.h"
|
|
|
|
|
2009-05-08 14:01:05 +04:00
|
|
|
#include "../src/tty/tty.h" /* LINES, COLS */
|
|
|
|
#include "../src/tty/key.h" /* is_idle() */
|
|
|
|
|
2009-06-14 19:18:27 +04:00
|
|
|
#include "edit-impl.h"
|
2002-11-29 06:01:06 +03:00
|
|
|
#include "edit-widget.h"
|
2001-08-24 22:23:17 +04:00
|
|
|
|
2009-05-08 14:01:05 +04:00
|
|
|
#include "../src/widget.h" /* buttonbar_redraw() */
|
|
|
|
#include "../src/menu.h" /* menubar_new() */
|
2002-11-14 22:38:26 +03:00
|
|
|
|
2001-08-24 22:23:17 +04:00
|
|
|
WEdit *wedit;
|
2009-05-04 20:11:51 +04:00
|
|
|
struct WMenuBar *edit_menubar;
|
2001-08-24 22:23:17 +04:00
|
|
|
|
|
|
|
int column_highlighting = 0;
|
|
|
|
|
2005-05-23 15:14:31 +04:00
|
|
|
static cb_ret_t edit_callback (Widget *, widget_msg_t msg, int parm);
|
2001-08-24 22:23:17 +04:00
|
|
|
|
2002-11-13 07:32:00 +03:00
|
|
|
static int
|
2009-09-03 21:45:50 +04:00
|
|
|
edit_event (Gpm_Event *event, void *data)
|
2001-08-24 22:23:17 +04:00
|
|
|
{
|
2009-09-03 21:45:50 +04:00
|
|
|
WEdit *edit = (WEdit *) data;
|
2002-09-21 04:16:04 +04:00
|
|
|
|
|
|
|
/* Unknown event type */
|
|
|
|
if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
|
2009-09-03 21:45:50 +04:00
|
|
|
return MOU_NORMAL;
|
|
|
|
|
|
|
|
/* rest of the upper frame, the menu is invisible - call menu */
|
|
|
|
if ((event->type & GPM_DOWN) && (event->y == 1))
|
|
|
|
return edit_menubar->widget.mouse (event, edit_menubar);
|
|
|
|
|
|
|
|
edit_update_curs_row (edit);
|
|
|
|
edit_update_curs_col (edit);
|
|
|
|
|
|
|
|
/* Outside editor window */
|
|
|
|
if (event->y <= 1 || event->x <= 0
|
|
|
|
|| event->x > edit->num_widget_columns
|
|
|
|
|| event->y > edit->num_widget_lines + 1)
|
|
|
|
return MOU_NORMAL;
|
2002-09-21 04:16:04 +04:00
|
|
|
|
|
|
|
/* Wheel events */
|
|
|
|
if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
|
|
|
|
edit_move_up (edit, 2, 1);
|
|
|
|
goto update;
|
|
|
|
}
|
|
|
|
if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
|
|
|
|
edit_move_down (edit, 2, 1);
|
|
|
|
goto update;
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
|
|
|
|
2002-09-21 04:16:04 +04:00
|
|
|
/* A lone up mustn't do anything */
|
|
|
|
if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
|
2009-09-03 21:45:50 +04:00
|
|
|
return MOU_NORMAL;
|
2002-09-21 04:16:04 +04:00
|
|
|
|
|
|
|
if (event->type & (GPM_DOWN | GPM_UP))
|
|
|
|
edit_push_key_press (edit);
|
|
|
|
|
2009-08-17 19:52:43 +04:00
|
|
|
if (option_cursor_beyond_eol) {
|
|
|
|
long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
|
|
|
|
edit_eol(edit, edit->curs1));
|
|
|
|
if ( event->x > line_len ) {
|
2009-08-20 00:44:47 +04:00
|
|
|
edit->over_col = event->x - line_len - option_line_state_width - 1;
|
2009-08-17 19:52:43 +04:00
|
|
|
edit->prev_col = line_len;
|
|
|
|
} else {
|
|
|
|
edit->over_col = 0;
|
2009-08-20 00:44:47 +04:00
|
|
|
edit->prev_col = event->x - option_line_state_width - 1;
|
2009-08-17 19:52:43 +04:00
|
|
|
}
|
2009-08-15 02:51:39 +04:00
|
|
|
} else {
|
2009-08-20 00:44:47 +04:00
|
|
|
edit->prev_col = event->x - edit->start_col - option_line_state_width - 1;
|
2009-08-15 02:51:39 +04:00
|
|
|
}
|
2002-09-21 04:16:04 +04:00
|
|
|
|
|
|
|
if (--event->y > (edit->curs_row + 1))
|
2006-12-11 01:07:28 +03:00
|
|
|
edit_move_down (edit, event->y - (edit->curs_row + 1), 0);
|
|
|
|
else if (event->y < (edit->curs_row + 1))
|
|
|
|
edit_move_up (edit, (edit->curs_row + 1) - event->y, 0);
|
|
|
|
else
|
|
|
|
edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
|
2002-09-21 04:16:04 +04:00
|
|
|
|
|
|
|
if (event->type & GPM_DOWN) {
|
|
|
|
edit_mark_cmd (edit, 1); /* reset */
|
|
|
|
edit->highlight = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(event->type & GPM_DRAG))
|
|
|
|
edit_mark_cmd (edit, 0);
|
|
|
|
|
|
|
|
update:
|
2006-12-11 01:07:28 +03:00
|
|
|
edit_find_bracket (edit);
|
2002-09-21 04:16:04 +04:00
|
|
|
edit->force |= REDRAW_COMPLETELY;
|
|
|
|
edit_update_curs_row (edit);
|
|
|
|
edit_update_curs_col (edit);
|
|
|
|
edit_update_screen (edit);
|
|
|
|
|
2009-09-03 21:45:50 +04:00
|
|
|
return MOU_NORMAL;
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
|
|
|
|
2002-09-24 07:57:36 +04:00
|
|
|
static void
|
2002-11-13 02:35:25 +03:00
|
|
|
edit_adjust_size (Dlg_head *h)
|
2002-09-24 07:57:36 +04:00
|
|
|
{
|
|
|
|
WEdit *edit;
|
|
|
|
WButtonBar *edit_bar;
|
|
|
|
|
2005-05-23 15:14:31 +04:00
|
|
|
edit = (WEdit *) find_widget_type (h, edit_callback);
|
2002-11-13 02:35:25 +03:00
|
|
|
edit_bar = find_buttonbar (h);
|
|
|
|
|
2002-09-24 07:57:36 +04:00
|
|
|
widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
|
2005-06-14 17:00:09 +04:00
|
|
|
widget_set_size ((Widget *) edit_bar, LINES - 1, 0, 1, COLS);
|
2002-09-24 07:57:36 +04:00
|
|
|
widget_set_size (&edit_menubar->widget, 0, 0, 1, COLS);
|
|
|
|
|
|
|
|
#ifdef RESIZABLE_MENUBAR
|
|
|
|
menubar_arrange (edit_menubar);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for the edit dialog */
|
2003-09-08 01:24:01 +04:00
|
|
|
static cb_ret_t
|
|
|
|
edit_dialog_callback (Dlg_head *h, dlg_msg_t msg, int parm)
|
2002-09-24 07:57:36 +04:00
|
|
|
{
|
2003-07-21 07:54:47 +04:00
|
|
|
WEdit *edit;
|
|
|
|
|
2002-09-24 07:57:36 +04:00
|
|
|
switch (msg) {
|
|
|
|
case DLG_RESIZE:
|
|
|
|
edit_adjust_size (h);
|
|
|
|
return MSG_HANDLED;
|
2003-09-08 01:24:01 +04:00
|
|
|
|
2003-07-21 07:54:47 +04:00
|
|
|
case DLG_VALIDATE:
|
2005-05-23 15:14:31 +04:00
|
|
|
edit = (WEdit *) find_widget_type (h, edit_callback);
|
2003-07-21 07:54:47 +04:00
|
|
|
if (!edit_ok_to_exit (edit)) {
|
|
|
|
h->running = 1;
|
|
|
|
}
|
2003-09-08 01:24:01 +04:00
|
|
|
return MSG_HANDLED;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return default_dlg_callback (h, msg, parm);
|
2002-09-24 07:57:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-31 08:33:40 +04:00
|
|
|
int
|
2004-09-19 21:46:39 +04:00
|
|
|
edit_file (const char *_file, int line)
|
2001-08-24 22:23:17 +04:00
|
|
|
{
|
|
|
|
static int made_directory = 0;
|
2002-09-24 19:10:47 +04:00
|
|
|
Dlg_head *edit_dlg;
|
2002-08-22 02:54:27 +04:00
|
|
|
WButtonBar *edit_bar;
|
2001-08-24 22:23:17 +04:00
|
|
|
|
|
|
|
if (!made_directory) {
|
2009-02-06 13:17:03 +03:00
|
|
|
char *dir = concat_dir_and_file (home_dir, EDIT_DIR);
|
2005-02-07 09:31:04 +03:00
|
|
|
made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (dir);
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
2002-12-08 12:10:38 +03:00
|
|
|
|
|
|
|
if (!(wedit = edit_init (NULL, LINES - 2, COLS, _file, line))) {
|
2001-08-24 22:23:17 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new dialog and add it widgets to it */
|
2002-08-31 08:33:40 +04:00
|
|
|
edit_dlg =
|
2002-09-24 07:57:36 +04:00
|
|
|
create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
|
2003-09-13 01:30:29 +04:00
|
|
|
"[Internal File Editor]", NULL, DLG_WANT_TAB);
|
2001-08-24 22:23:17 +04:00
|
|
|
|
|
|
|
init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
|
2009-09-03 21:45:50 +04:00
|
|
|
edit_callback, edit_event);
|
2001-08-24 22:23:17 +04:00
|
|
|
|
|
|
|
widget_want_cursor (wedit->widget, 1);
|
|
|
|
|
|
|
|
edit_bar = buttonbar_new (1);
|
|
|
|
|
2009-05-01 21:10:52 +04:00
|
|
|
edit_menubar = edit_create_menu ();
|
2001-08-24 22:23:17 +04:00
|
|
|
|
|
|
|
add_widget (edit_dlg, edit_bar);
|
2003-09-13 01:30:29 +04:00
|
|
|
add_widget (edit_dlg, wedit);
|
|
|
|
add_widget (edit_dlg, edit_menubar);
|
2001-08-24 22:23:17 +04:00
|
|
|
|
|
|
|
run_dlg (edit_dlg);
|
|
|
|
|
|
|
|
destroy_dlg (edit_dlg);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-06-14 19:18:27 +04:00
|
|
|
const char *
|
|
|
|
edit_get_file_name (const WEdit *edit)
|
|
|
|
{
|
|
|
|
return edit->filename;
|
|
|
|
}
|
|
|
|
|
2004-09-19 19:40:09 +04:00
|
|
|
static void edit_my_define (Dlg_head * h, int idx, const char *text,
|
2001-08-24 22:23:17 +04:00
|
|
|
void (*fn) (WEdit *), WEdit * edit)
|
|
|
|
{
|
2005-07-20 15:56:30 +04:00
|
|
|
text = edit->labels[idx - 1]? edit->labels[idx - 1] : text;
|
2005-05-23 15:14:31 +04:00
|
|
|
/* function-cast ok */
|
2005-02-08 12:50:16 +03:00
|
|
|
buttonbar_set_label_data (h, idx, text, (buttonbarfn) fn, edit);
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void cmd_F1 (WEdit * edit)
|
|
|
|
{
|
2002-11-13 05:56:40 +03:00
|
|
|
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (1));
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_F2 (WEdit * edit)
|
|
|
|
{
|
2002-11-13 05:56:40 +03:00
|
|
|
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (2));
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_F3 (WEdit * edit)
|
|
|
|
{
|
2002-11-13 05:56:40 +03:00
|
|
|
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (3));
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_F4 (WEdit * edit)
|
|
|
|
{
|
2002-11-13 05:56:40 +03:00
|
|
|
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (4));
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_F5 (WEdit * edit)
|
|
|
|
{
|
2002-11-13 05:56:40 +03:00
|
|
|
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (5));
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_F6 (WEdit * edit)
|
|
|
|
{
|
2002-11-13 05:56:40 +03:00
|
|
|
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (6));
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_F7 (WEdit * edit)
|
|
|
|
{
|
2002-11-13 05:56:40 +03:00
|
|
|
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (7));
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_F8 (WEdit * edit)
|
|
|
|
{
|
2002-11-13 05:56:40 +03:00
|
|
|
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (8));
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static void cmd_F9 (WEdit * edit)
|
|
|
|
{
|
2002-11-13 05:56:40 +03:00
|
|
|
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (9));
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void cmd_F10 (WEdit * edit)
|
|
|
|
{
|
2002-11-13 05:56:40 +03:00
|
|
|
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (10));
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
|
|
|
|
2002-11-13 07:32:00 +03:00
|
|
|
static void
|
2002-11-13 02:35:25 +03:00
|
|
|
edit_labels (WEdit *edit)
|
2001-08-24 22:23:17 +04:00
|
|
|
{
|
|
|
|
Dlg_head *h = edit->widget.parent;
|
|
|
|
|
2009-10-17 17:29:37 +04:00
|
|
|
edit_my_define (h, 1, Q_("ButtonBar|Help"), cmd_F1, edit);
|
|
|
|
edit_my_define (h, 2, Q_("ButtonBar|Save"), cmd_F2, edit);
|
|
|
|
edit_my_define (h, 3, Q_("ButtonBar|Mark"), cmd_F3, edit);
|
|
|
|
edit_my_define (h, 4, Q_("ButtonBar|Replac"), cmd_F4, edit);
|
|
|
|
edit_my_define (h, 5, Q_("ButtonBar|Copy"), cmd_F5, edit);
|
|
|
|
edit_my_define (h, 6, Q_("ButtonBar|Move"), cmd_F6, edit);
|
|
|
|
edit_my_define (h, 7, Q_("ButtonBar|Search"), cmd_F7, edit);
|
|
|
|
edit_my_define (h, 8, Q_("ButtonBar|Delete"), cmd_F8, edit);
|
|
|
|
edit_my_define (h, 9, Q_("ButtonBar|PullDn"), edit_menu_cmd, edit);
|
|
|
|
edit_my_define (h, 10, Q_("ButtonBar|Quit"), cmd_F10, edit);
|
2001-08-24 22:23:17 +04:00
|
|
|
|
2005-02-08 12:50:16 +03:00
|
|
|
buttonbar_redraw (h);
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void edit_update_screen (WEdit * e)
|
|
|
|
{
|
|
|
|
edit_scroll_screen_over_cursor (e);
|
|
|
|
|
|
|
|
edit_update_curs_col (e);
|
|
|
|
edit_status (e);
|
|
|
|
|
|
|
|
/* pop all events for this window for internal handling */
|
|
|
|
|
|
|
|
if (!is_idle ()) {
|
|
|
|
e->force |= REDRAW_PAGE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (e->force & REDRAW_COMPLETELY)
|
|
|
|
e->force |= REDRAW_PAGE;
|
|
|
|
edit_render_keypress (e);
|
|
|
|
}
|
|
|
|
|
2003-09-11 02:48:54 +04:00
|
|
|
static cb_ret_t
|
2005-05-23 15:14:31 +04:00
|
|
|
edit_callback (Widget *w, widget_msg_t msg, int parm)
|
2001-08-24 22:23:17 +04:00
|
|
|
{
|
2005-05-23 15:14:31 +04:00
|
|
|
WEdit *e = (WEdit *) w;
|
|
|
|
|
2001-08-24 22:23:17 +04:00
|
|
|
switch (msg) {
|
|
|
|
case WIDGET_INIT:
|
|
|
|
e->force |= REDRAW_COMPLETELY;
|
|
|
|
edit_labels (e);
|
2003-09-11 02:48:54 +04:00
|
|
|
return MSG_HANDLED;
|
|
|
|
|
2001-08-24 22:23:17 +04:00
|
|
|
case WIDGET_DRAW:
|
|
|
|
e->force |= REDRAW_COMPLETELY;
|
|
|
|
e->num_widget_lines = LINES - 2;
|
|
|
|
e->num_widget_columns = COLS;
|
2003-09-11 02:48:54 +04:00
|
|
|
/* fallthrough */
|
|
|
|
|
2001-08-24 22:23:17 +04:00
|
|
|
case WIDGET_FOCUS:
|
|
|
|
edit_update_screen (e);
|
2003-09-11 02:48:54 +04:00
|
|
|
return MSG_HANDLED;
|
|
|
|
|
|
|
|
case WIDGET_KEY:
|
|
|
|
{
|
2001-08-24 22:23:17 +04:00
|
|
|
int cmd, ch;
|
2003-09-11 02:48:54 +04:00
|
|
|
|
2005-07-20 15:56:30 +04:00
|
|
|
/* The user may override the access-keys for the menu bar. */
|
|
|
|
if (edit_translate_key (e, parm, &cmd, &ch)) {
|
|
|
|
edit_execute_key_command (e, cmd, ch);
|
|
|
|
edit_update_screen (e);
|
2003-09-11 02:48:54 +04:00
|
|
|
return MSG_HANDLED;
|
2005-07-20 15:56:30 +04:00
|
|
|
} else if (edit_drop_hotkey_menu (e, parm)) {
|
|
|
|
return MSG_HANDLED;
|
|
|
|
} else {
|
2003-09-11 02:48:54 +04:00
|
|
|
return MSG_NOT_HANDLED;
|
2005-07-20 15:56:30 +04:00
|
|
|
}
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
2003-09-11 02:48:54 +04:00
|
|
|
|
2001-08-24 22:23:17 +04:00
|
|
|
case WIDGET_CURSOR:
|
2003-09-11 02:48:54 +04:00
|
|
|
widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
|
2009-08-17 23:50:35 +04:00
|
|
|
e->curs_col + e->start_col + e->over_col +
|
|
|
|
EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width);
|
2003-09-11 02:48:54 +04:00
|
|
|
return MSG_HANDLED;
|
|
|
|
|
2003-09-10 22:21:40 +04:00
|
|
|
case WIDGET_DESTROY:
|
|
|
|
edit_clean (e);
|
2003-09-11 02:48:54 +04:00
|
|
|
return MSG_HANDLED;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return default_proc (msg, parm);
|
2001-08-24 22:23:17 +04:00
|
|
|
}
|
|
|
|
}
|