keymap files: unification of Fxx keys: move to lower case.
[midnight-commander.git] / src / editor / editwidget.c
blob9486b11b3782c23f5214b6862119a7c154c4eaba
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() */
46 #include "lib/tty/color.h" /* tty_setcolor() */
47 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
48 #include "lib/strutil.h" /* str_term_trim() */
49 #include "lib/util.h" /* concat_dir_and_file() */
50 #include "lib/widget.h"
51 #include "lib/mcconfig.h"
53 #include "src/keybind-defaults.h"
54 #include "src/main.h" /* home_dir */
56 #include "edit-impl.h"
57 #include "edit-widget.h"
59 /*** global variables ****************************************************************************/
61 /*** file scope macro definitions ****************************************************************/
63 /*** file scope type declarations ****************************************************************/
65 /*** file scope variables ************************************************************************/
67 /*** file scope functions ************************************************************************/
69 static cb_ret_t edit_callback (Widget * w, widget_msg_t msg, int parm);
72 /* --------------------------------------------------------------------------------------------- */
74 static char *
75 edit_get_shortcut (unsigned long command)
77 const char *ext_map;
78 const char *shortcut = NULL;
80 shortcut = keybind_lookup_keymap_shortcut (editor_map, command);
81 if (shortcut != NULL)
82 return g_strdup (shortcut);
84 ext_map = keybind_lookup_keymap_shortcut (editor_map, CK_ExtendedKeyMap);
85 if (ext_map != NULL)
86 shortcut = keybind_lookup_keymap_shortcut (editor_x_map, command);
87 if (shortcut != NULL)
88 return g_strdup_printf ("%s %s", ext_map, shortcut);
90 return NULL;
93 /* --------------------------------------------------------------------------------------------- */
95 static char *
96 edit_get_title (const Dlg_head * h, size_t len)
98 const WEdit *edit = (const WEdit *) find_widget_type (h, edit_callback);
99 const char *modified = edit->modified ? "(*) " : " ";
100 const char *file_label;
102 len -= 4;
104 file_label = str_term_trim (edit->filename, len - str_term_width1 (_("Edit: ")));
106 return g_strconcat (_("Edit: "), modified, file_label, (char *) NULL);
109 /* --------------------------------------------------------------------------------------------- */
111 static int
112 edit_event (Gpm_Event * event, void *data)
114 WEdit *edit = (WEdit *) data;
116 /* Unknown event type */
117 if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
118 return MOU_NORMAL;
120 edit_update_curs_row (edit);
121 edit_update_curs_col (edit);
123 /* Outside editor window */
124 if (event->y < 1 || event->x < 1
125 || event->x > edit->widget.cols || event->y > edit->widget.lines)
126 return MOU_NORMAL;
128 /* Double click */
129 if ((event->type & (GPM_DOUBLE | GPM_UP)) == (GPM_UP | GPM_DOUBLE))
131 edit_mark_current_word_cmd (edit);
132 goto update;
134 #if 0
135 /* Triple click */
136 if ((event->type & (GPM_TRIPLE | GPM_UP)) == (GPM_UP | GPM_TRIPLE))
138 edit_mark_current_line_cmd (edit);
139 goto update;
141 #endif
142 /* Wheel events */
143 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN))
145 edit_move_up (edit, 2, 1);
146 goto update;
148 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN))
150 edit_move_down (edit, 2, 1);
151 goto update;
154 /* A lone up mustn't do anything */
155 if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
156 return MOU_NORMAL;
158 if (event->type & (GPM_DOWN | GPM_UP))
159 edit_push_key_press (edit);
161 if (!option_cursor_beyond_eol)
162 edit->prev_col = event->x - edit->start_col - option_line_state_width - 1;
163 else
165 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
166 edit_eol (edit, edit->curs1));
168 if (event->x > line_len)
170 edit->over_col = event->x - line_len - edit->start_col - option_line_state_width - 1;
171 edit->prev_col = line_len;
173 else
175 edit->over_col = 0;
176 edit->prev_col = event->x - option_line_state_width - edit->start_col - 1;
180 --event->y;
181 if (event->y > edit->curs_row)
182 edit_move_down (edit, event->y - edit->curs_row, 0);
183 else if (event->y < edit->curs_row)
184 edit_move_up (edit, edit->curs_row - event->y, 0);
185 else
186 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
188 if (event->type & GPM_DOWN)
190 edit_mark_cmd (edit, 1); /* reset */
191 edit->highlight = 0;
194 if (!(event->type & GPM_DRAG))
195 edit_mark_cmd (edit, 0);
197 update:
198 edit_find_bracket (edit);
199 edit->force |= REDRAW_COMPLETELY;
200 edit_update_curs_row (edit);
201 edit_update_curs_col (edit);
202 edit_update_screen (edit);
204 return MOU_NORMAL;
207 /* --------------------------------------------------------------------------------------------- */
209 static cb_ret_t
210 edit_command_execute (WEdit * edit, unsigned long command)
212 if (command == CK_Menu)
213 edit_menu_cmd (edit);
214 else
216 edit_execute_key_command (edit, command, -1);
217 edit_update_screen (edit);
219 return MSG_HANDLED;
222 /* --------------------------------------------------------------------------------------------- */
224 static inline void
225 edit_set_buttonbar (WEdit * edit, WButtonBar * bb)
227 buttonbar_set_label (bb, 1, Q_ ("ButtonBar|Help"), editor_map, (Widget *) edit);
228 buttonbar_set_label (bb, 2, Q_ ("ButtonBar|Save"), editor_map, (Widget *) edit);
229 buttonbar_set_label (bb, 3, Q_ ("ButtonBar|Mark"), editor_map, (Widget *) edit);
230 buttonbar_set_label (bb, 4, Q_ ("ButtonBar|Replac"), editor_map, (Widget *) edit);
231 buttonbar_set_label (bb, 5, Q_ ("ButtonBar|Copy"), editor_map, (Widget *) edit);
232 buttonbar_set_label (bb, 6, Q_ ("ButtonBar|Move"), editor_map, (Widget *) edit);
233 buttonbar_set_label (bb, 7, Q_ ("ButtonBar|Search"), editor_map, (Widget *) edit);
234 buttonbar_set_label (bb, 8, Q_ ("ButtonBar|Delete"), editor_map, (Widget *) edit);
235 buttonbar_set_label (bb, 9, Q_ ("ButtonBar|PullDn"), editor_map, (Widget *) edit);
236 buttonbar_set_label (bb, 10, Q_ ("ButtonBar|Quit"), editor_map, (Widget *) edit);
239 /* --------------------------------------------------------------------------------------------- */
240 /** Callback for the edit dialog */
242 static cb_ret_t
243 edit_dialog_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
245 WEdit *edit;
246 WMenuBar *menubar;
247 WButtonBar *buttonbar;
249 edit = (WEdit *) find_widget_type (h, edit_callback);
250 menubar = find_menubar (h);
251 buttonbar = find_buttonbar (h);
253 switch (msg)
255 case DLG_INIT:
256 edit_set_buttonbar (edit, buttonbar);
257 return MSG_HANDLED;
259 case DLG_DRAW:
260 /* don't use common_dialog_repaint() -- we don't need a frame */
261 tty_setcolor (EDITOR_NORMAL_COLOR);
262 dlg_erase (h);
263 return MSG_HANDLED;
265 case DLG_RESIZE:
266 /* dlg_set_size() is surplus for this case */
267 h->lines = LINES;
268 h->cols = COLS;
269 widget_set_size (&buttonbar->widget, h->lines - 1, h->x, 1, h->cols);
270 widget_set_size (&menubar->widget, h->y, h->x, 1, h->cols);
271 menubar_arrange (menubar);
272 widget_set_size (&edit->widget, h->y + 1, h->x, h->lines - 2, h->cols);
273 return MSG_HANDLED;
275 case DLG_ACTION:
276 if (sender == (Widget *) menubar)
277 return send_message ((Widget *) edit, WIDGET_COMMAND, parm);
278 if (sender == (Widget *) buttonbar)
279 return send_message ((Widget *) edit, WIDGET_COMMAND, parm);
280 return MSG_HANDLED;
282 case DLG_VALIDATE:
283 h->state = DLG_ACTIVE; /* don't stop the dialog before final decision */
284 if (edit_ok_to_exit (edit))
285 h->state = DLG_CLOSED;
286 return MSG_HANDLED;
288 default:
289 return default_dlg_callback (h, sender, msg, parm, data);
293 /* --------------------------------------------------------------------------------------------- */
295 static cb_ret_t
296 edit_callback (Widget * w, widget_msg_t msg, int parm)
298 WEdit *e = (WEdit *) w;
300 switch (msg)
302 case WIDGET_DRAW:
303 e->force |= REDRAW_COMPLETELY;
304 /* fallthrough */
306 case WIDGET_FOCUS:
307 edit_update_screen (e);
308 return MSG_HANDLED;
310 case WIDGET_KEY:
312 int cmd, ch;
313 cb_ret_t ret = MSG_NOT_HANDLED;
315 /* The user may override the access-keys for the menu bar. */
316 if (macro_index == -1 && edit_execute_macro (e, parm))
317 ret = MSG_HANDLED;
318 else if (edit_translate_key (e, parm, &cmd, &ch))
320 edit_execute_key_command (e, cmd, ch);
321 edit_update_screen (e);
322 ret = MSG_HANDLED;
324 else if (edit_drop_hotkey_menu (e, parm))
325 ret = MSG_HANDLED;
327 return ret;
330 case WIDGET_COMMAND:
331 /* command from menubar or buttonbar */
332 return edit_command_execute (e, parm);
334 case WIDGET_CURSOR:
335 widget_move (w, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
336 e->curs_col + e->start_col + e->over_col +
337 EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width);
338 return MSG_HANDLED;
340 case WIDGET_DESTROY:
341 edit_clean (e);
342 return MSG_HANDLED;
344 default:
345 return default_proc (msg, parm);
349 /* --------------------------------------------------------------------------------------------- */
350 /*** public functions ****************************************************************************/
351 /* --------------------------------------------------------------------------------------------- */
354 edit_file (const char *_file, int line)
356 static gboolean made_directory = FALSE;
357 Dlg_head *edit_dlg;
358 WEdit *wedit;
359 WMenuBar *menubar;
361 if (!made_directory)
363 char *dir = concat_dir_and_file (mc_config_get_cache_path (), EDIT_DIR);
364 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
365 g_free (dir);
367 dir = concat_dir_and_file (mc_config_get_path (), EDIT_DIR);
368 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
369 g_free (dir);
371 dir = concat_dir_and_file (mc_config_get_data_path (), EDIT_DIR);
372 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
373 g_free (dir);
376 wedit = edit_init (NULL, 1, 0, LINES - 2, COLS, _file, line);
378 if (wedit == NULL)
379 return 0;
381 /* Create a new dialog and add it widgets to it */
382 edit_dlg =
383 create_dlg (FALSE, 0, 0, LINES, COLS, NULL, edit_dialog_callback,
384 "[Internal File Editor]", NULL, DLG_WANT_TAB);
386 edit_dlg->get_shortcut = edit_get_shortcut;
387 edit_dlg->get_title = edit_get_title;
389 menubar = menubar_new (0, 0, COLS, NULL);
390 add_widget (edit_dlg, menubar);
391 edit_init_menu (menubar);
393 init_widget (&wedit->widget, wedit->widget.y, wedit->widget.x,
394 wedit->widget.lines, wedit->widget.cols, edit_callback, edit_event);
395 widget_want_cursor (wedit->widget, TRUE);
397 add_widget (edit_dlg, wedit);
399 add_widget (edit_dlg, buttonbar_new (TRUE));
401 run_dlg (edit_dlg);
403 if (edit_dlg->state == DLG_CLOSED)
404 destroy_dlg (edit_dlg);
406 return 1;
409 /* --------------------------------------------------------------------------------------------- */
411 const char *
412 edit_get_file_name (const WEdit * edit)
414 return edit->filename;
417 /* --------------------------------------------------------------------------------------------- */
419 void
420 edit_update_screen (WEdit * e)
422 edit_scroll_screen_over_cursor (e);
424 edit_update_curs_col (e);
425 edit_status (e);
427 /* pop all events for this window for internal handling */
428 if (!is_idle ())
429 e->force |= REDRAW_PAGE;
430 else
432 if (e->force & REDRAW_COMPLETELY)
433 e->force |= REDRAW_PAGE;
434 edit_render_keypress (e);
438 /* --------------------------------------------------------------------------------------------- */