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
25 * \brief Source: editor initialisation and callback handler
34 #include <sys/types.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 /* --------------------------------------------------------------------------------------------- */
75 edit_get_shortcut (unsigned long command
)
78 const char *shortcut
= NULL
;
80 shortcut
= keybind_lookup_keymap_shortcut (editor_map
, command
);
82 return g_strdup (shortcut
);
84 ext_map
= keybind_lookup_keymap_shortcut (editor_map
, CK_ExtendedKeyMap
);
86 shortcut
= keybind_lookup_keymap_shortcut (editor_x_map
, command
);
88 return g_strdup_printf ("%s %s", ext_map
, shortcut
);
93 /* --------------------------------------------------------------------------------------------- */
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
;
104 file_label
= str_term_trim (edit
->filename
, len
- str_term_width1 (_("Edit: ")));
106 return g_strconcat (_("Edit: "), modified
, file_label
, (char *) NULL
);
109 /* --------------------------------------------------------------------------------------------- */
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
)))
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
)
129 if ((event
->type
& (GPM_DOUBLE
| GPM_UP
)) == (GPM_UP
| GPM_DOUBLE
))
131 edit_mark_current_word_cmd (edit
);
136 if ((event
->type
& (GPM_TRIPLE
| GPM_UP
)) == (GPM_UP
| GPM_TRIPLE
))
138 edit_mark_current_line_cmd (edit
);
143 if ((event
->buttons
& GPM_B_UP
) && (event
->type
& GPM_DOWN
))
145 edit_move_up (edit
, 2, 1);
148 if ((event
->buttons
& GPM_B_DOWN
) && (event
->type
& GPM_DOWN
))
150 edit_move_down (edit
, 2, 1);
154 /* A lone up mustn't do anything */
155 if (edit
->mark2
!= -1 && event
->type
& (GPM_UP
| GPM_DRAG
))
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;
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
;
176 edit
->prev_col
= event
->x
- option_line_state_width
- edit
->start_col
- 1;
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);
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 */
194 if (!(event
->type
& GPM_DRAG
))
195 edit_mark_cmd (edit
, 0);
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
);
207 /* --------------------------------------------------------------------------------------------- */
210 edit_command_execute (WEdit
* edit
, unsigned long command
)
212 if (command
== CK_Menu
)
213 edit_menu_cmd (edit
);
216 edit_execute_key_command (edit
, command
, -1);
217 edit_update_screen (edit
);
222 /* --------------------------------------------------------------------------------------------- */
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 */
243 edit_dialog_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
247 WButtonBar
*buttonbar
;
249 edit
= (WEdit
*) find_widget_type (h
, edit_callback
);
250 menubar
= find_menubar (h
);
251 buttonbar
= find_buttonbar (h
);
256 edit_set_buttonbar (edit
, buttonbar
);
260 /* don't use common_dialog_repaint() -- we don't need a frame */
261 tty_setcolor (EDITOR_NORMAL_COLOR
);
266 /* dlg_set_size() is surplus for this case */
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
);
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
);
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
;
289 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
293 /* --------------------------------------------------------------------------------------------- */
296 edit_callback (Widget
* w
, widget_msg_t msg
, int parm
)
298 WEdit
*e
= (WEdit
*) w
;
303 e
->force
|= REDRAW_COMPLETELY
;
307 edit_update_screen (e
);
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
))
318 else if (edit_translate_key (e
, parm
, &cmd
, &ch
))
320 edit_execute_key_command (e
, cmd
, ch
);
321 edit_update_screen (e
);
324 else if (edit_drop_hotkey_menu (e
, parm
))
331 /* command from menubar or buttonbar */
332 return edit_command_execute (e
, parm
);
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
);
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
;
363 char *dir
= concat_dir_and_file (mc_config_get_cache_path (), EDIT_DIR
);
364 made_directory
= (mkdir (dir
, 0700) != -1 || errno
== EEXIST
);
367 dir
= concat_dir_and_file (mc_config_get_path (), EDIT_DIR
);
368 made_directory
= (mkdir (dir
, 0700) != -1 || errno
== EEXIST
);
371 dir
= concat_dir_and_file (mc_config_get_data_path (), EDIT_DIR
);
372 made_directory
= (mkdir (dir
, 0700) != -1 || errno
== EEXIST
);
376 wedit
= edit_init (NULL
, 1, 0, LINES
- 2, COLS
, _file
, line
);
381 /* Create a new dialog and add it widgets to it */
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
));
403 if (edit_dlg
->state
== DLG_CLOSED
)
404 destroy_dlg (edit_dlg
);
409 /* --------------------------------------------------------------------------------------------- */
412 edit_get_file_name (const WEdit
* edit
)
414 return edit
->filename
;
417 /* --------------------------------------------------------------------------------------------- */
420 edit_update_screen (WEdit
* e
)
422 edit_scroll_screen_over_cursor (e
);
424 edit_update_curs_col (e
);
427 /* pop all events for this window for internal handling */
429 e
->force
|= REDRAW_PAGE
;
432 if (e
->force
& REDRAW_COMPLETELY
)
433 e
->force
|= REDRAW_PAGE
;
434 edit_render_keypress (e
);
438 /* --------------------------------------------------------------------------------------------- */