2 Editor initialisation and callback handler.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
6 The Free Software Foundation, Inc.
10 Andrew Borodin <aborodin@vmail.ru> 2012
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * \brief Source: editor initialisation and callback handler
38 #include <sys/types.h>
46 #include "lib/global.h"
48 #include "lib/tty/tty.h" /* LINES, COLS */
49 #include "lib/tty/key.h" /* is_idle() */
50 #include "lib/tty/color.h" /* tty_setcolor() */
52 #include "lib/strutil.h" /* str_term_trim() */
53 #include "lib/util.h" /* mc_build_filename() */
54 #include "lib/widget.h"
55 #include "lib/mcconfig.h"
56 #include "lib/event.h" /* mc_event_raise() */
58 #include "src/keybind-defaults.h"
59 #include "src/setup.h" /* home_dir */
60 #include "src/filemanager/cmd.h" /* view_other_cmd(), save_setup_cmd() */
61 #include "src/learn.h" /* learn_keys() */
62 #include "src/args.h" /* mcedit_arg_t */
64 #include "edit-impl.h"
65 #include "editwidget.h"
70 /*** global variables ****************************************************************************/
72 char *edit_window_state_char
= NULL
;
73 char *edit_window_close_char
= NULL
;
75 /*** file scope macro definitions ****************************************************************/
77 #define WINDOW_MIN_LINES (2 + 2)
78 #define WINDOW_MIN_COLS (2 + LINE_STATE_WIDTH + 2)
80 /*** file scope type declarations ****************************************************************/
82 /*** file scope variables ************************************************************************/
83 static unsigned int edit_dlg_init_refcounter
= 0;
85 /*** file scope functions ************************************************************************/
87 static cb_ret_t
edit_callback (Widget
* w
, widget_msg_t msg
, int parm
);
88 static cb_ret_t
edit_dialog_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
,
91 /* --------------------------------------------------------------------------------------------- */
93 * Init the 'edit' subsystem
99 if (edit_dlg_init_refcounter
== 0)
101 edit_window_state_char
= mc_skin_get ("editor", "window-state-char", "*");
102 edit_window_close_char
= mc_skin_get ("editor", "window-close-char", "X");
109 edit_dlg_init_refcounter
++;
112 /* --------------------------------------------------------------------------------------------- */
114 * Deinit the 'edit' subsystem
118 edit_dlg_deinit (void)
120 if (edit_dlg_init_refcounter
!= 0)
121 edit_dlg_init_refcounter
--;
123 if (edit_dlg_init_refcounter
== 0)
125 g_free (edit_window_state_char
);
126 g_free (edit_window_close_char
);
134 /* --------------------------------------------------------------------------------------------- */
136 * Show info about editor
142 const char *header
= N_("About");
143 const char *button_name
= N_("&OK");
144 const char *const version
= "MCEdit " VERSION
;
145 char text
[BUF_LARGE
];
147 int win_len
, version_len
, button_len
;
154 button_name
= _(button_name
);
157 button_len
= str_term_width1 (button_name
) + 5;
158 version_len
= str_term_width1 (version
);
160 g_snprintf (text
, sizeof (text
),
161 _("Copyright (C) 1996-2012 the Free Software Foundation\n\n"
162 " A user friendly text editor\n"
163 " written for the Midnight Commander"));
165 win_len
= str_term_width1 (header
);
166 win_len
= max (win_len
, version_len
);
167 win_len
= max (win_len
, button_len
);
169 /* count width and height of text */
170 str_msg_term_size (text
, &lines
, &cols
);
172 cols
= max (win_len
, cols
) + 6;
175 about_dlg
= create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, NULL
, NULL
,
176 "[Internal File Editor]", header
, DLG_CENTER
| DLG_TRYUP
);
178 add_widget (about_dlg
, label_new (3, (cols
- version_len
) / 2, version
));
179 add_widget (about_dlg
, label_new (5, 3, text
));
180 add_widget (about_dlg
, button_new (lines
- 3, (cols
- button_len
) / 2,
181 B_ENTER
, NORMAL_BUTTON
, button_name
, NULL
));
184 destroy_dlg (about_dlg
);
187 /* --------------------------------------------------------------------------------------------- */
195 ev_help_t event_data
= { NULL
, "[Internal File Editor]" };
196 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
199 /* --------------------------------------------------------------------------------------------- */
201 * Callback for the iteration of objects in the 'editors' array.
202 * Resize the editor window.
204 * @param data probably WEdit object
205 * @param user_data unused
209 edit_dialog_resize_cb (void *data
, void *user_data
)
211 Widget
*w
= WIDGET (data
);
215 if (edit_widget_is_editor (w
) && ((WEdit
*) w
)->fullscreen
)
217 Widget
*wh
= WIDGET (w
->owner
);
219 w
->lines
= wh
->lines
- 2;
224 /* --------------------------------------------------------------------------------------------- */
226 * Restore saved window size.
228 * @param edit editor object
232 edit_restore_size (WEdit
* edit
)
234 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
235 widget_set_size (WIDGET (edit
), edit
->y_prev
, edit
->x_prev
, edit
->lines_prev
, edit
->cols_prev
);
236 dlg_redraw (WIDGET (edit
)->owner
);
239 /* --------------------------------------------------------------------------------------------- */
241 * Move window by one row or column in any direction.
243 * @param edit editor object
244 * @param command direction (CK_Up, CK_Down, CK_Left, CK_Right)
248 edit_window_move (WEdit
* edit
, unsigned long command
)
250 Widget
*w
= WIDGET (edit
);
251 Widget
*wh
= WIDGET (w
->owner
);
256 if (w
->y
> wh
->y
+ 1) /* menubar */
260 if (w
->y
< wh
->y
+ wh
->lines
- 2) /* buttonbar */
264 if (w
->x
+ w
->cols
> wh
->x
)
268 if (w
->x
< wh
->x
+ wh
->cols
)
275 edit
->force
|= REDRAW_PAGE
;
276 dlg_redraw (w
->owner
);
279 /* --------------------------------------------------------------------------------------------- */
281 * Resize window by one row or column in any direction.
283 * @param edit editor object
284 * @param command direction (CK_Up, CK_Down, CK_Left, CK_Right)
288 edit_window_resize (WEdit
* edit
, unsigned long command
)
290 Widget
*w
= WIDGET (edit
);
291 Widget
*wh
= WIDGET (w
->owner
);
296 if (w
->lines
> WINDOW_MIN_LINES
)
300 if (w
->y
+ w
->lines
< wh
->y
+ wh
->lines
- 1) /* buttonbar */
304 if (w
->cols
> WINDOW_MIN_COLS
)
308 if (w
->x
+ w
->cols
< wh
->x
+ wh
->cols
)
315 edit
->force
|= REDRAW_COMPLETELY
;
316 dlg_redraw (w
->owner
);
319 /* --------------------------------------------------------------------------------------------- */
321 * Get hotkey by number.
330 return (n
<= 9) ? '0' + n
: 'a' + n
- 10;
333 /* --------------------------------------------------------------------------------------------- */
336 edit_window_list (const Dlg_head
* h
)
338 const size_t offset
= 2; /* skip menu and buttonbar */
339 const size_t dlg_num
= g_list_length (h
->widgets
) - offset
;
346 lines
= min ((size_t) (LINES
* 2 / 3), dlg_num
);
349 listbox
= create_listbox_window (lines
, cols
, _("Open files"), "[Open files]");
351 for (w
= h
->widgets
; w
!= NULL
; w
= g_list_next (w
))
352 if (edit_widget_is_editor (WIDGET (w
->data
)))
354 WEdit
*e
= (WEdit
*) w
->data
;
357 if (e
->filename_vpath
== NULL
)
358 fname
= g_strdup_printf ("%c [%s]", e
->modified
? '*' : ' ', _("NoName"));
363 fname2
= vfs_path_to_str (e
->filename_vpath
);
364 fname
= g_strdup_printf ("%c%s", e
->modified
? '*' : ' ', fname2
);
368 listbox_add_item (listbox
->list
, LISTBOX_APPEND_AT_END
, get_hotkey (i
++),
369 str_term_trim (fname
, WIDGET (listbox
->list
)->cols
- 2), NULL
);
373 rv
= g_list_position (h
->widgets
, h
->current
) - offset
;
374 listbox_select_entry (listbox
->list
, rv
);
375 rv
= run_listbox (listbox
);
378 w
= g_list_nth (h
->widgets
, rv
+ offset
);
379 dlg_set_top_widget (w
->data
);
383 /* --------------------------------------------------------------------------------------------- */
386 edit_get_shortcut (unsigned long command
)
389 const char *shortcut
= NULL
;
391 shortcut
= keybind_lookup_keymap_shortcut (editor_map
, command
);
392 if (shortcut
!= NULL
)
393 return g_strdup (shortcut
);
395 ext_map
= keybind_lookup_keymap_shortcut (editor_map
, CK_ExtendedKeyMap
);
397 shortcut
= keybind_lookup_keymap_shortcut (editor_x_map
, command
);
398 if (shortcut
!= NULL
)
399 return g_strdup_printf ("%s %s", ext_map
, shortcut
);
404 /* --------------------------------------------------------------------------------------------- */
407 edit_get_title (const Dlg_head
* h
, size_t len
)
409 const WEdit
*edit
= find_editor (h
);
410 const char *modified
= edit
->modified
? "(*) " : " ";
411 const char *file_label
;
416 filename
= vfs_path_to_str (edit
->filename_vpath
);
417 if (filename
== NULL
)
418 filename
= g_strdup (_("[NoName]"));
419 file_label
= str_term_trim (filename
, len
- str_term_width1 (_("Edit: ")));
422 return g_strconcat (_("Edit: "), modified
, file_label
, (char *) NULL
);
425 /* --------------------------------------------------------------------------------------------- */
427 * Handle mouse events of editor window
429 * @param event mouse event
430 * @param data editor window
431 * @return MOU_NORMAL if event was handled, MOU_UNHANDLED otherwise
435 edit_event (Gpm_Event
* event
, void *data
)
437 WEdit
*edit
= (WEdit
*) data
;
438 Widget
*w
= WIDGET (data
);
441 if (!mouse_global_in_widget (event
, w
))
442 return MOU_UNHANDLED
;
444 local
= mouse_get_local (event
, w
);
446 /* Unknown event type */
447 if ((event
->type
& (GPM_DOWN
| GPM_DRAG
| GPM_UP
)) == 0)
450 dlg_set_top_widget (w
);
452 edit_update_curs_row (edit
);
453 edit_update_curs_col (edit
);
455 if (edit
->fullscreen
|| (local
.buttons
& GPM_B_LEFT
) == 0 || (local
.type
& GPM_UP
) != 0)
456 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
457 else if (local
.y
== 1 && edit
->drag_state
!= MCEDIT_DRAG_RESIZE
)
459 /* click on the top line (move) */
460 int dx
= edit
->fullscreen
? 0 : 2;
462 if (local
.x
== w
->cols
- dx
- 1)
464 edit_dialog_callback (w
->owner
, NULL
, DLG_ACTION
, CK_Close
, NULL
);
468 if (local
.x
== w
->cols
- dx
- 4)
470 edit_toggle_fullscreen (edit
);
474 if ((local
.type
& (GPM_DOWN
| GPM_DRAG
)) != 0)
476 /* move if not fullscreen */
477 edit
->drag_state_start
= local
.x
;
478 edit
->drag_state
= MCEDIT_DRAG_MOVE
;
479 edit
->force
|= REDRAW_COMPLETELY
;
480 edit_update_screen (edit
);
483 else if (!edit
->fullscreen
&& local
.y
== w
->lines
&& local
.x
== w
->cols
)
485 /* click on bottom-right corner (resize) */
486 if ((local
.type
& (GPM_DOWN
| GPM_DRAG
)) != 0)
488 edit
->drag_state
= MCEDIT_DRAG_RESIZE
;
489 edit
->force
|= REDRAW_COMPLETELY
;
490 edit_update_screen (edit
);
494 if (edit
->drag_state
== MCEDIT_DRAG_NORMAL
)
496 gboolean done
= TRUE
;
499 if ((local
.type
& (GPM_DOUBLE
| GPM_UP
)) == (GPM_UP
| GPM_DOUBLE
))
501 edit_mark_current_word_cmd (edit
);
506 if ((local
.type
& (GPM_TRIPLE
| GPM_UP
)) == (GPM_UP
| GPM_TRIPLE
))
508 edit_mark_current_line_cmd (edit
);
513 if ((local
.buttons
& GPM_B_UP
) != 0 && (local
.type
& GPM_DOWN
) != 0)
515 edit_move_up (edit
, 2, 1);
518 if ((local
.buttons
& GPM_B_DOWN
) != 0 && (local
.type
& GPM_DOWN
) != 0)
520 edit_move_down (edit
, 2, 1);
524 /* continue handle current event */
527 /* handle DRAG mouse event, don't use standard way to continue
528 * event handling outside of widget */
533 c
= tty_get_event (event
, FALSE
, TRUE
);
534 if (c
== EV_NONE
|| c
!= EV_MOUSE
)
537 local
= mouse_get_local (event
, w
);
540 /* A lone up mustn't do anything */
541 if (edit
->mark2
!= -1 && (local
.type
& (GPM_UP
| GPM_DRAG
)) != 0)
544 if ((local
.type
& (GPM_DOWN
| GPM_UP
)) != 0)
545 edit_push_key_press (edit
);
547 if (!edit
->fullscreen
)
549 if (!option_cursor_beyond_eol
)
550 edit
->prev_col
= local
.x
- edit
->start_col
- option_line_state_width
- 1;
553 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
554 edit_eol (edit
, edit
->curs1
));
556 if (local
.x
> line_len
)
559 local
.x
- line_len
- edit
->start_col
- option_line_state_width
- 1;
560 edit
->prev_col
= line_len
;
565 edit
->prev_col
= local
.x
- option_line_state_width
- edit
->start_col
- 1;
569 if (!edit
->fullscreen
)
571 if (local
.y
> (edit
->curs_row
+ 1))
572 edit_move_down (edit
, local
.y
- (edit
->curs_row
+ 1), 0);
573 else if (local
.y
< (edit
->curs_row
+ 1))
574 edit_move_up (edit
, (edit
->curs_row
+ 1) - local
.y
, 0);
576 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
578 if ((local
.type
& GPM_DOWN
) != 0)
580 edit_mark_cmd (edit
, TRUE
); /* reset */
584 done
= (local
.type
& GPM_DRAG
) == 0;
586 edit_mark_cmd (edit
, FALSE
);
589 edit_find_bracket (edit
);
590 edit
->force
|= REDRAW_COMPLETELY
;
591 edit_update_curs_row (edit
);
592 edit_update_curs_col (edit
);
593 edit_update_screen (edit
);
595 while (!edit
->fullscreen
&& !done
);
598 while (edit
->drag_state
!= MCEDIT_DRAG_NORMAL
)
603 c
= tty_get_event (event
, FALSE
, TRUE
);
606 if (c
== EV_NONE
|| c
!= EV_MOUSE
)
609 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
610 edit
->force
|= REDRAW_COMPLETELY
;
611 edit_update_screen (edit
);
613 else if (y
== w
->y
&& (event
->type
& (GPM_DOUBLE
| GPM_UP
)) == (GPM_DOUBLE
| GPM_UP
))
615 /* double click on top line (toggle fullscreen) */
616 edit_toggle_fullscreen (edit
);
617 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
618 edit
->force
|= REDRAW_COMPLETELY
;
619 edit_update_screen (edit
);
621 else if ((event
->type
& (GPM_DRAG
| GPM_DOWN
)) == 0)
624 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
625 edit
->force
|= REDRAW_COMPLETELY
;
626 edit_update_screen (edit
);
628 else if (!edit
->fullscreen
)
630 Widget
*h
= WIDGET (w
->owner
);
632 if (edit
->drag_state
== MCEDIT_DRAG_MOVE
)
634 int x
= event
->x
- 1;
636 y
= max (y
, h
->y
+ 1); /* status line */
637 y
= min (y
, h
->y
+ h
->lines
- 2); /* buttonbar */
639 x
= min (x
, h
->x
+ h
->cols
- 1);
640 /* don't use widget_set_size() here to avoid double draw */
642 w
->x
= x
- edit
->drag_state_start
;
643 edit
->force
|= REDRAW_COMPLETELY
;
645 else if (edit
->drag_state
== MCEDIT_DRAG_RESIZE
)
647 event
->y
= min (event
->y
, h
->y
+ h
->lines
- 1); /* buttonbar */
648 event
->x
= min (event
->x
, h
->x
+ h
->cols
);
649 local
= mouse_get_local (event
, w
);
651 /* don't use widget_set_size() here to avoid double draw */
652 w
->lines
= max (WINDOW_MIN_LINES
, local
.y
);
653 w
->cols
= max (WINDOW_MIN_COLS
, local
.x
);
654 edit
->force
|= REDRAW_COMPLETELY
;
657 dlg_redraw (w
->owner
);
664 /* --------------------------------------------------------------------------------------------- */
666 * Handle mouse events of editor screen.
668 * @param event mouse event
669 * @param data editor screen
670 * @return MOU_NORMAL if event was handled, MOU_UNHANDLED otherwise
674 edit_dialog_event (Gpm_Event
* event
, void *data
)
676 Dlg_head
*h
= (Dlg_head
*) data
;
678 Widget
*wh
= WIDGET (h
);
679 int ret
= MOU_UNHANDLED
;
681 w
= WIDGET (find_menubar (h
));
683 if (event
->y
== wh
->y
+ 1 && (event
->type
& GPM_DOWN
) != 0 && !((WMenuBar
*) w
)->is_active
)
691 /* Try find top fullscreen window */
692 for (l
= h
->widgets
; l
!= NULL
; l
= g_list_next (l
))
693 if (edit_widget_is_editor (WIDGET (l
->data
)) && ((WEdit
*) l
->data
)->fullscreen
)
696 /* Handle fullscreen/close buttons in the top line */
697 x
= wh
->x
+ wh
->cols
+ 1 - 6;
699 if (top
!= NULL
&& event
->x
>= x
)
703 e
= (WEdit
*) top
->data
;
706 if (top
!= h
->current
)
708 /* Window is not active. Activate it */
709 dlg_set_top_widget (e
);
714 edit_toggle_fullscreen (e
);
716 edit_dialog_callback (h
, NULL
, DLG_ACTION
, CK_Close
, NULL
);
721 if (ret
== MOU_UNHANDLED
)
722 dlg_select_widget (w
);
728 /* --------------------------------------------------------------------------------------------- */
731 edit_dialog_command_execute (Dlg_head
* h
, unsigned long command
)
733 Widget
*wh
= WIDGET (h
);
734 gboolean ret
= MSG_HANDLED
;
739 edit_add_window (h
, wh
->y
+ 1, wh
->x
, wh
->lines
- 2, wh
->cols
, NULL
, 0);
744 case CK_EditSyntaxFile
:
745 edit_load_syntax_file (h
);
747 case CK_EditUserMenu
:
748 edit_load_menu_file (h
);
751 /* if there are no opened files anymore, close MC editor */
752 if (edit_widget_is_editor (WIDGET (h
->current
->data
)) &&
753 edit_close_cmd ((WEdit
*) h
->current
->data
) && find_editor (h
) == NULL
)
758 /* edit->force |= REDRAW_COMPLETELY; */
766 Widget
*w
= WIDGET (h
->current
->data
);
768 if (!edit_widget_is_editor (w
) || ((WEdit
*) w
)->drag_state
== MCEDIT_DRAG_NORMAL
)
771 edit_restore_size ((WEdit
*) w
);
778 edit_syntax_onoff_cmd (h
);
781 edit_show_tabs_tws_cmd (h
);
784 edit_show_margin_cmd (h
);
787 edit_show_numbers_cmd (h
);
799 case CK_WindowResize
:
800 if (edit_widget_is_editor (WIDGET (h
->current
->data
)))
801 edit_handle_move_resize ((WEdit
*) h
->current
->data
, command
);
804 edit_window_list (h
);
808 dlg_set_top_widget (h
->current
->data
);
812 dlg_set_top_widget (h
->current
->data
);
815 edit_options_dialog (h
);
817 case CK_OptionsSaveMode
:
818 edit_save_mode_cmd ();
824 ret
= MSG_NOT_HANDLED
;
831 /* --------------------------------------------------------------------------------------------- */
834 edit_quit (Dlg_head
* h
)
839 h
->state
= DLG_ACTIVE
; /* don't stop the dialog before final decision */
841 for (l
= h
->widgets
; l
!= NULL
; l
= g_list_next (l
))
842 if (edit_widget_is_editor (WIDGET (l
->data
)))
844 e
= (WEdit
*) l
->data
;
846 if (e
->drag_state
!= MCEDIT_DRAG_NORMAL
)
848 edit_restore_size (e
);
854 dlg_select_widget (e
);
856 if (!edit_ok_to_exit (e
))
861 /* no editors in dialog at all or no any file required to be saved */
862 if (e
== NULL
|| l
== NULL
)
863 h
->state
= DLG_CLOSED
;
866 /* --------------------------------------------------------------------------------------------- */
869 edit_set_buttonbar (WEdit
* edit
, WButtonBar
* bb
)
871 buttonbar_set_label (bb
, 1, Q_ ("ButtonBar|Help"), editor_map
, NULL
);
872 buttonbar_set_label (bb
, 2, Q_ ("ButtonBar|Save"), editor_map
, WIDGET (edit
));
873 buttonbar_set_label (bb
, 3, Q_ ("ButtonBar|Mark"), editor_map
, WIDGET (edit
));
874 buttonbar_set_label (bb
, 4, Q_ ("ButtonBar|Replac"), editor_map
, WIDGET (edit
));
875 buttonbar_set_label (bb
, 5, Q_ ("ButtonBar|Copy"), editor_map
, WIDGET (edit
));
876 buttonbar_set_label (bb
, 6, Q_ ("ButtonBar|Move"), editor_map
, WIDGET (edit
));
877 buttonbar_set_label (bb
, 7, Q_ ("ButtonBar|Search"), editor_map
, WIDGET (edit
));
878 buttonbar_set_label (bb
, 8, Q_ ("ButtonBar|Delete"), editor_map
, WIDGET (edit
));
879 buttonbar_set_label (bb
, 9, Q_ ("ButtonBar|PullDn"), editor_map
, NULL
);
880 buttonbar_set_label (bb
, 10, Q_ ("ButtonBar|Quit"), editor_map
, NULL
);
883 /* --------------------------------------------------------------------------------------------- */
884 /** Callback for the edit dialog */
887 edit_dialog_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
890 WButtonBar
*buttonbar
;
891 Widget
*wh
= WIDGET (h
);
900 /* don't use common_dialog_repaint() -- we don't need a frame */
901 tty_setcolor (EDITOR_BACKGROUND
);
906 menubar
= find_menubar (h
);
907 buttonbar
= find_buttonbar (h
);
908 /* dlg_set_size() is surplus for this case */
911 widget_set_size (WIDGET (buttonbar
), wh
->lines
- 1, wh
->x
, 1, wh
->cols
);
912 widget_set_size (WIDGET (menubar
), wh
->y
, wh
->x
, 1, wh
->cols
);
913 menubar_arrange (menubar
);
914 g_list_foreach (h
->widgets
, (GFunc
) edit_dialog_resize_cb
, NULL
);
920 return edit_dialog_command_execute (h
, parm
);
921 /* message from menu */
922 menubar
= find_menubar (h
);
923 if (sender
== WIDGET (menubar
))
925 if (edit_dialog_command_execute (h
, parm
) == MSG_HANDLED
)
927 /* try send command to the current window */
928 return send_message (WIDGET (h
->current
->data
), WIDGET_COMMAND
, parm
);
930 /* message from buttonbar */
931 buttonbar
= find_buttonbar (h
);
932 if (sender
== WIDGET (buttonbar
))
935 return send_message (WIDGET (data
), WIDGET_COMMAND
, parm
);
936 return edit_dialog_command_execute (h
, parm
);
938 return MSG_NOT_HANDLED
;
942 Widget
*w
= h
->current
->data
;
943 cb_ret_t ret
= MSG_NOT_HANDLED
;
945 if (edit_widget_is_editor (w
))
947 WEdit
*e
= (WEdit
*) w
;
948 unsigned long command
;
951 command
= keybind_lookup_keymap_command (editor_map
, parm
);
955 command
= keybind_lookup_keymap_command (editor_x_map
, parm
);
958 if (command
!= CK_IgnoreKey
)
959 ret
= edit_dialog_command_execute (h
, command
);
965 /* hardcoded menu hotkeys (see edit_drop_hotkey_menu) */
966 case DLG_UNHANDLED_KEY
:
967 return edit_drop_hotkey_menu (h
, parm
) ? MSG_HANDLED
: MSG_NOT_HANDLED
;
978 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
982 /* --------------------------------------------------------------------------------------------- */
985 edit_callback (Widget
* w
, widget_msg_t msg
, int parm
)
987 WEdit
*e
= (WEdit
*) w
;
992 edit_set_buttonbar (e
, find_buttonbar (w
->owner
));
996 e
->force
|= REDRAW_COMPLETELY
;
997 edit_update_screen (e
);
1000 case WIDGET_UNFOCUS
:
1001 /* redraw frame and status */
1002 edit_status (e
, FALSE
);
1008 cb_ret_t ret
= MSG_NOT_HANDLED
;
1010 /* The user may override the access-keys for the menu bar. */
1011 if (macro_index
== -1 && edit_execute_macro (e
, parm
))
1013 edit_update_screen (e
);
1016 else if (edit_translate_key (e
, parm
, &cmd
, &ch
))
1018 edit_execute_key_command (e
, cmd
, ch
);
1019 edit_update_screen (e
);
1026 case WIDGET_COMMAND
:
1027 /* command from menubar or buttonbar */
1028 edit_execute_key_command (e
, parm
, -1);
1029 edit_update_screen (e
);
1036 y
= (e
->fullscreen
? 0 : 1) + EDIT_TEXT_VERTICAL_OFFSET
+ e
->curs_row
;
1037 x
= (e
->fullscreen
? 0 : 1) + EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
+
1038 e
->curs_col
+ e
->start_col
+ e
->over_col
;
1040 widget_move (w
, y
, x
);
1044 case WIDGET_DESTROY
:
1049 return default_proc (msg
, parm
);
1053 /* --------------------------------------------------------------------------------------------- */
1054 /*** public functions ****************************************************************************/
1055 /* --------------------------------------------------------------------------------------------- */
1059 * @param file_vpath file object
1060 * @param line line number
1061 * @return TRUE if no errors was occured, FALSE otherwise
1065 edit_file (const vfs_path_t
* file_vpath
, long line
)
1067 mcedit_arg_t arg
= { (vfs_path_t
*) file_vpath
, line
};
1071 files
= g_list_prepend (NULL
, &arg
);
1072 ok
= edit_files (files
);
1073 g_list_free (files
);
1078 /* --------------------------------------------------------------------------------------------- */
1081 edit_files (const GList
* files
)
1083 static gboolean made_directory
= FALSE
;
1087 gboolean ok
= FALSE
;
1089 if (!made_directory
)
1093 dir
= mc_build_filename (mc_config_get_cache_path (), EDIT_DIR
, NULL
);
1094 made_directory
= (mkdir (dir
, 0700) != -1 || errno
== EEXIST
);
1097 dir
= mc_build_filename (mc_config_get_path (), EDIT_DIR
, NULL
);
1098 made_directory
= (mkdir (dir
, 0700) != -1 || errno
== EEXIST
);
1101 dir
= mc_build_filename (mc_config_get_data_path (), EDIT_DIR
, NULL
);
1102 made_directory
= (mkdir (dir
, 0700) != -1 || errno
== EEXIST
);
1106 /* Create a new dialog and add it widgets to it */
1108 create_dlg (FALSE
, 0, 0, LINES
, COLS
, NULL
, edit_dialog_callback
, edit_dialog_event
,
1109 "[Internal File Editor]", NULL
, DLG_WANT_TAB
);
1111 edit_dlg
->get_shortcut
= edit_get_shortcut
;
1112 edit_dlg
->get_title
= edit_get_title
;
1114 menubar
= menubar_new (0, 0, COLS
, NULL
);
1115 add_widget (edit_dlg
, menubar
);
1116 edit_init_menu (menubar
);
1118 add_widget (edit_dlg
, buttonbar_new (TRUE
));
1120 for (file
= files
; file
!= NULL
; file
= g_list_next (file
))
1122 Widget
*w
= WIDGET (edit_dlg
);
1123 mcedit_arg_t
*f
= (mcedit_arg_t
*) file
->data
;
1126 f_ok
= edit_add_window (edit_dlg
, w
->y
+ 1, w
->x
, w
->lines
- 2, w
->cols
, f
->file_vpath
,
1128 /* at least one file has been opened succefully */
1135 if (!ok
|| edit_dlg
->state
== DLG_CLOSED
)
1136 destroy_dlg (edit_dlg
);
1141 /* --------------------------------------------------------------------------------------------- */
1144 edit_get_file_name (const WEdit
* edit
)
1146 return vfs_path_to_str (edit
->filename_vpath
);
1149 /* --------------------------------------------------------------------------------------------- */
1152 find_editor (const Dlg_head
* h
)
1154 if (edit_widget_is_editor (WIDGET (h
->current
->data
)))
1155 return (WEdit
*) h
->current
->data
;
1156 return (WEdit
*) find_widget_type (h
, edit_callback
);
1159 /* --------------------------------------------------------------------------------------------- */
1161 * Check if widget is an WEdit class.
1163 * @param w probably editor object
1164 * @return TRUE if widget is an WEdit class, FALSE otherwise
1168 edit_widget_is_editor (const Widget
* w
)
1170 return (w
!= NULL
&& w
->callback
== edit_callback
);
1173 /* --------------------------------------------------------------------------------------------- */
1176 edit_update_screen (WEdit
* e
)
1178 Dlg_head
*h
= WIDGET (e
)->owner
;
1180 edit_scroll_screen_over_cursor (e
);
1181 edit_update_curs_col (e
);
1183 edit_status (e
, (e
->force
& REDRAW_COMPLETELY
) != 0 && (void *) e
== h
->current
->data
);
1185 /* pop all events for this window for internal handling */
1187 e
->force
|= REDRAW_PAGE
;
1190 if ((e
->force
& REDRAW_COMPLETELY
) != 0)
1191 e
->force
|= REDRAW_PAGE
;
1192 edit_render_keypress (e
);
1195 buttonbar_redraw (find_buttonbar (h
));
1198 /* --------------------------------------------------------------------------------------------- */
1200 * Save current window size.
1202 * @param edit editor object
1206 edit_save_size (WEdit
* edit
)
1208 Widget
*w
= WIDGET (edit
);
1210 edit
->y_prev
= w
->y
;
1211 edit
->x_prev
= w
->x
;
1212 edit
->lines_prev
= w
->lines
;
1213 edit
->cols_prev
= w
->cols
;
1216 /* --------------------------------------------------------------------------------------------- */
1218 * Create new editor window and insert it into editor screen.
1220 * @param h editor dialog (screen)
1221 * @param y y coordinate
1222 * @param x x coordinate
1223 * @param lines window height
1224 * @param cols window width
1225 * @param f file object
1226 * @param fline line number in file
1227 * @return TRUE if new window was successfully created and inserted into editor screen,
1232 edit_add_window (Dlg_head
* h
, int y
, int x
, int lines
, int cols
, const vfs_path_t
* f
, long fline
)
1237 edit
= edit_init (NULL
, y
, x
, lines
, cols
, f
, fline
);
1242 w
->callback
= edit_callback
;
1243 w
->mouse
= edit_event
;
1244 widget_want_cursor (w
, TRUE
);
1252 /* --------------------------------------------------------------------------------------------- */
1254 * Handle move/resize events.
1256 * @param edit editor object
1257 * @param command action id
1258 * @return TRUE if mouse actions was handled, FALSE otherwise
1262 edit_handle_move_resize (WEdit
* edit
, unsigned long command
)
1264 gboolean ret
= FALSE
;
1266 if (edit
->fullscreen
)
1268 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
1272 switch (edit
->drag_state
)
1274 case MCEDIT_DRAG_NORMAL
:
1275 /* possible start move/resize */
1279 edit
->drag_state
= MCEDIT_DRAG_MOVE
;
1280 edit_save_size (edit
);
1283 case CK_WindowResize
:
1284 edit
->drag_state
= MCEDIT_DRAG_RESIZE
;
1285 edit_save_size (edit
);
1293 case MCEDIT_DRAG_MOVE
:
1296 case CK_WindowResize
:
1297 edit
->drag_state
= MCEDIT_DRAG_RESIZE
;
1304 edit_window_move (edit
, command
);
1309 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
1310 /* redraw frame and status */
1311 edit_status (edit
, TRUE
);
1318 case MCEDIT_DRAG_RESIZE
:
1322 edit
->drag_state
= MCEDIT_DRAG_MOVE
;
1329 edit_window_resize (edit
, command
);
1333 case CK_WindowResize
:
1334 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
1335 /* redraw frame and status */
1336 edit_status (edit
, TRUE
);
1347 /* --------------------------------------------------------------------------------------------- */
1349 * Toggle window fuulscreen mode.
1351 * @param edit editor object
1355 edit_toggle_fullscreen (WEdit
* edit
)
1357 edit
->fullscreen
= !edit
->fullscreen
;
1358 edit
->force
= REDRAW_COMPLETELY
;
1360 if (!edit
->fullscreen
)
1361 edit_restore_size (edit
);
1364 Widget
*w
= WIDGET (edit
);
1365 Widget
*h
= WIDGET (w
->owner
);
1367 edit_save_size (edit
);
1368 widget_set_size (w
, h
->y
+ 1, h
->x
, h
->lines
- 2, h
->cols
);
1369 edit
->force
|= REDRAW_PAGE
;
1370 edit_update_screen (edit
);
1374 /* --------------------------------------------------------------------------------------------- */