Ticket #2127: updated file extension for "sh"
[midnight-commander.git] / src / editor / editwidget.c
blob0407ada0c9c4d7320cdf6bb2e05ccf80e444fab3
1 /* editor initialisation and callback handler.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
24 /** \file
25 * \brief Source: editor initialisation and callback handler
26 * \author Paul Sheer
27 * \date 1996, 1997
30 #include <config.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <sys/stat.h>
40 #include <stdlib.h>
42 #include "lib/global.h"
44 #include "lib/tty/tty.h" /* LINES, COLS */
45 #include "lib/tty/key.h" /* is_idle() */
47 #include "edit-impl.h"
48 #include "edit-widget.h"
50 #include "src/dialog.h"
51 #include "src/widget.h" /* ButtonBar */
52 #include "src/menu.h" /* menubar_new() */
53 #include "src/cmddef.h"
55 int column_highlighting = 0;
57 static cb_ret_t edit_callback (Widget *, widget_msg_t msg, int parm);
59 static char *
60 edit_get_shortcut (unsigned long command)
62 const char *ext_map;
63 const char *shortcut = NULL;
65 shortcut = lookup_keymap_shortcut (editor_map, command);
66 if (shortcut != NULL)
67 return g_strdup (shortcut);
69 ext_map = lookup_keymap_shortcut (editor_map, CK_Ext_Mode);
70 if (ext_map != NULL)
71 shortcut = lookup_keymap_shortcut (editor_x_map, command);
72 if (shortcut != NULL)
73 return g_strdup_printf ("%s %s", ext_map, shortcut);
75 return NULL;
78 static int
79 edit_event (Gpm_Event * event, void *data)
81 WEdit *edit = (WEdit *) data;
83 /* Unknown event type */
84 if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
85 return MOU_NORMAL;
87 /* rest of the upper frame, the menu is invisible - call menu */
88 if ((event->type & GPM_DOWN) && (event->y == 1))
90 WMenuBar *menubar;
92 menubar = find_menubar (edit->widget.parent);
94 return menubar->widget.mouse (event, menubar);
97 edit_update_curs_row (edit);
98 edit_update_curs_col (edit);
100 /* Outside editor window */
101 if (event->y <= 1 || event->x <= 0
102 || event->x > edit->num_widget_columns || event->y > edit->num_widget_lines + 1)
103 return MOU_NORMAL;
105 /* Wheel events */
106 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN))
108 edit_move_up (edit, 2, 1);
109 goto update;
111 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN))
113 edit_move_down (edit, 2, 1);
114 goto update;
117 /* A lone up mustn't do anything */
118 if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
119 return MOU_NORMAL;
121 if (event->type & (GPM_DOWN | GPM_UP))
122 edit_push_key_press (edit);
124 if (option_cursor_beyond_eol)
126 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
127 edit_eol (edit, edit->curs1));
129 if (event->x > line_len)
131 edit->over_col = event->x - line_len - edit->start_col - option_line_state_width - 1;
132 edit->prev_col = line_len;
134 else
136 edit->over_col = 0;
137 edit->prev_col = event->x - option_line_state_width - edit->start_col - 1;
140 else
142 edit->prev_col = event->x - edit->start_col - option_line_state_width - 1;
145 if (--event->y > (edit->curs_row + 1))
146 edit_move_down (edit, event->y - (edit->curs_row + 1), 0);
147 else if (event->y < (edit->curs_row + 1))
148 edit_move_up (edit, (edit->curs_row + 1) - event->y, 0);
149 else
150 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
152 if (event->type & GPM_DOWN)
154 edit_mark_cmd (edit, 1); /* reset */
155 edit->highlight = 0;
158 if (!(event->type & GPM_DRAG))
159 edit_mark_cmd (edit, 0);
161 update:
162 edit_find_bracket (edit);
163 edit->force |= REDRAW_COMPLETELY;
164 edit_update_curs_row (edit);
165 edit_update_curs_col (edit);
166 edit_update_screen (edit);
168 return MOU_NORMAL;
171 static cb_ret_t
172 edit_command_execute (WEdit * edit, unsigned long command)
174 if (command == CK_Menu)
175 edit_menu_cmd (edit);
176 else
178 edit_execute_key_command (edit, command, -1);
179 edit_update_screen (edit);
181 return MSG_HANDLED;
184 static inline void
185 edit_set_buttonbar (WEdit * edit, WButtonBar * bb)
187 buttonbar_set_label (bb, 1, Q_ ("ButtonBar|Help"), editor_map, (Widget *) edit);
188 buttonbar_set_label (bb, 2, Q_ ("ButtonBar|Save"), editor_map, (Widget *) edit);
189 buttonbar_set_label (bb, 3, Q_ ("ButtonBar|Mark"), editor_map, (Widget *) edit);
190 buttonbar_set_label (bb, 4, Q_ ("ButtonBar|Replac"), editor_map, (Widget *) edit);
191 buttonbar_set_label (bb, 5, Q_ ("ButtonBar|Copy"), editor_map, (Widget *) edit);
192 buttonbar_set_label (bb, 6, Q_ ("ButtonBar|Move"), editor_map, (Widget *) edit);
193 buttonbar_set_label (bb, 7, Q_ ("ButtonBar|Search"), editor_map, (Widget *) edit);
194 buttonbar_set_label (bb, 8, Q_ ("ButtonBar|Delete"), editor_map, (Widget *) edit);
195 buttonbar_set_label (bb, 9, Q_ ("ButtonBar|PullDn"), editor_map, (Widget *) edit);
196 buttonbar_set_label (bb, 10, Q_ ("ButtonBar|Quit"), editor_map, (Widget *) edit);
199 /* Callback for the edit dialog */
200 static cb_ret_t
201 edit_dialog_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
203 WEdit *edit;
204 WMenuBar *menubar;
205 WButtonBar *buttonbar;
207 edit = (WEdit *) find_widget_type (h, edit_callback);
208 menubar = find_menubar (h);
209 buttonbar = find_buttonbar (h);
211 switch (msg)
213 case DLG_INIT:
214 edit_set_buttonbar (edit, buttonbar);
215 return MSG_HANDLED;
217 case DLG_RESIZE:
218 widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
219 widget_set_size (&buttonbar->widget, LINES - 1, 0, 1, COLS);
220 widget_set_size (&menubar->widget, 0, 0, 1, COLS);
221 menubar_arrange (menubar);
222 return MSG_HANDLED;
224 case DLG_ACTION:
225 if (sender == (Widget *) menubar)
226 return send_message ((Widget *) edit, WIDGET_COMMAND, parm);
227 if (sender == (Widget *) buttonbar)
228 return send_message ((Widget *) edit, WIDGET_COMMAND, parm);
229 return MSG_HANDLED;
231 case DLG_VALIDATE:
232 if (!edit_ok_to_exit (edit))
233 h->running = 1;
234 return MSG_HANDLED;
236 default:
237 return default_dlg_callback (h, sender, msg, parm, data);
242 edit_file (const char *_file, int line)
244 static gboolean made_directory = FALSE;
245 Dlg_head *edit_dlg;
246 WEdit *wedit;
247 WMenuBar *menubar;
249 if (!made_directory)
251 char *dir = concat_dir_and_file (home_dir, EDIT_DIR);
252 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
253 g_free (dir);
256 wedit = edit_init (NULL, LINES - 2, COLS, _file, line);
258 if (wedit == NULL)
259 return 0;
261 /* Create a new dialog and add it widgets to it */
262 edit_dlg =
263 create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
264 "[Internal File Editor]", NULL, DLG_WANT_TAB);
266 edit_dlg->get_shortcut = edit_get_shortcut;
267 menubar = menubar_new (0, 0, COLS, NULL);
268 add_widget (edit_dlg, menubar);
269 edit_init_menu (menubar);
271 init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS, edit_callback, edit_event);
272 widget_want_cursor (wedit->widget, 1);
274 add_widget (edit_dlg, wedit);
276 add_widget (edit_dlg, buttonbar_new (TRUE));
278 run_dlg (edit_dlg);
280 destroy_dlg (edit_dlg);
282 return 1;
285 const char *
286 edit_get_file_name (const WEdit * edit)
288 return edit->filename;
291 void
292 edit_update_screen (WEdit * e)
294 edit_scroll_screen_over_cursor (e);
296 edit_update_curs_col (e);
297 edit_status (e);
299 /* pop all events for this window for internal handling */
300 if (!is_idle ())
301 e->force |= REDRAW_PAGE;
302 else
304 if (e->force & REDRAW_COMPLETELY)
305 e->force |= REDRAW_PAGE;
306 edit_render_keypress (e);
310 static cb_ret_t
311 edit_callback (Widget * w, widget_msg_t msg, int parm)
313 WEdit *e = (WEdit *) w;
315 switch (msg)
317 case WIDGET_DRAW:
318 e->force |= REDRAW_COMPLETELY;
319 e->num_widget_lines = LINES - 2;
320 e->num_widget_columns = COLS;
321 /* fallthrough */
323 case WIDGET_FOCUS:
324 edit_update_screen (e);
325 return MSG_HANDLED;
327 case WIDGET_KEY:
329 int cmd, ch;
330 cb_ret_t ret = MSG_NOT_HANDLED;
332 /* The user may override the access-keys for the menu bar. */
333 if (edit_translate_key (e, parm, &cmd, &ch))
335 edit_execute_key_command (e, cmd, ch);
336 edit_update_screen (e);
337 ret = MSG_HANDLED;
339 else if (edit_drop_hotkey_menu (e, parm))
340 ret = MSG_HANDLED;
342 return ret;
345 case WIDGET_COMMAND:
346 /* command from menubar or buttonbar */
347 return edit_command_execute (e, parm);
349 case WIDGET_CURSOR:
350 widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
351 e->curs_col + e->start_col + e->over_col +
352 EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width);
353 return MSG_HANDLED;
355 case WIDGET_DESTROY:
356 edit_clean (e);
357 return MSG_HANDLED;
359 default:
360 return default_proc (msg, parm);