From 1c33972ee5c142091afa5e7d43cb946670571e06 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Thu, 28 Jul 2011 12:53:34 +0400 Subject: [PATCH] Add CK_Close action to close current open file. Signed-off-by: Andrew Borodin --- lib/keybind.c | 1 + lib/keybind.h | 1 + misc/mc.keymap.default | 1 + misc/mc.keymap.emacs | 1 + src/editor/edit-impl.h | 2 ++ src/editor/editcmd.c | 39 +++++++++++++++++++++++++++++++++++++++ src/editor/editmenu.c | 7 ++++--- src/editor/editwidget.c | 23 ++++++++++++++++++++--- 8 files changed, 69 insertions(+), 6 deletions(-) diff --git a/lib/keybind.c b/lib/keybind.c index 8f554b480..b47b82b4d 100644 --- a/lib/keybind.c +++ b/lib/keybind.c @@ -250,6 +250,7 @@ static name_keymap_t command_names[] = { #endif #ifdef USE_INTERNAL_EDIT + {"Close", CK_Close}, {"Tab", CK_Tab}, {"Undo", CK_Undo}, {"ScrollUp", CK_ScrollUp}, diff --git a/lib/keybind.h b/lib/keybind.h index d30012848..939f2e56c 100644 --- a/lib/keybind.h +++ b/lib/keybind.h @@ -234,6 +234,7 @@ enum CK_EditFile, CK_InsertFile, CK_EditSyntaxFile, + CK_Close, /* block commands */ CK_BlockSave, CK_BlockShiftLeft, diff --git a/misc/mc.keymap.default b/misc/mc.keymap.default index afc70161e..630c3767d 100644 --- a/misc/mc.keymap.default +++ b/misc/mc.keymap.default @@ -254,6 +254,7 @@ Save = f2 EditFile = EditNew = ctrl-n SaveAs = f12; ctrl-f2 +Close = Mark = f3 Copy = f5 Move = f6 diff --git a/misc/mc.keymap.emacs b/misc/mc.keymap.emacs index 635e1981b..1b57b6e7e 100644 --- a/misc/mc.keymap.emacs +++ b/misc/mc.keymap.emacs @@ -254,6 +254,7 @@ ParagraphDown = Save = f2 EditFile = SaveAs = f12; ctrl-f2 +Close = Mark = f3; ctrl-at Copy = f5 Move = f6 diff --git a/src/editor/edit-impl.h b/src/editor/edit-impl.h index e28104dc2..f46ebd75f 100644 --- a/src/editor/edit-impl.h +++ b/src/editor/edit-impl.h @@ -178,6 +178,7 @@ extern gboolean search_create_bookmark; /*** declarations of public functions ************************************************************/ +WEdit *find_editor (const Dlg_head * h); gboolean edit_widget_is_editor (const Widget * w); gboolean edit_drop_hotkey_menu (Dlg_head * h, int key); void edit_menu_cmd (Dlg_head * h); @@ -233,6 +234,7 @@ gboolean edit_ok_to_exit (WEdit * edit); gboolean edit_renew (WEdit * edit); gboolean edit_new_cmd (WEdit * edit); gboolean edit_load_cmd (WEdit * edit, edit_current_file_t what); +gboolean edit_close_cmd (WEdit * edit); void edit_mark_cmd (WEdit * edit, int unmark); void edit_mark_current_word_cmd (WEdit * edit); void edit_mark_current_line_cmd (WEdit * edit); diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c index 84b1116b6..13fbfad52 100644 --- a/src/editor/editcmd.c +++ b/src/editor/editcmd.c @@ -2140,6 +2140,45 @@ edit_load_cmd (WEdit * edit, edit_current_file_t what) /* --------------------------------------------------------------------------------------------- */ /** + * Close window with opened file. + * + * @returns TRUE if file was closed. + */ + +gboolean +edit_close_cmd (WEdit * edit) +{ + gboolean ret; + + ret = (edit != NULL) && edit_ok_to_exit (edit); + + if (ret) + { + Dlg_head *h = ((Widget *) edit)->owner; + + if (edit->locked != 0) + unlock_file (edit->filename_vpath); + + del_widget (edit); + + if (edit_widget_is_editor ((Widget *) h->current->data)) + edit = (WEdit *) h->current->data; + else + { + edit = find_editor (h); + if (edit != NULL) + dlg_set_top_widget (edit); + } + } + + if (edit != NULL) + edit->force |= REDRAW_COMPLETELY; + + return ret; +} + +/* --------------------------------------------------------------------------------------------- */ +/** if mark2 is -1 then marking is from mark1 to the cursor. Otherwise its between the markers. This handles this. Returns 1 if no text is marked. diff --git a/src/editor/editmenu.c b/src/editor/editmenu.c index c9a8c1a89..24a46d3e6 100644 --- a/src/editor/editmenu.c +++ b/src/editor/editmenu.c @@ -72,6 +72,7 @@ create_file_menu (void) entries = g_list_prepend (entries, menu_entry_create (_("&Open file..."), CK_EditFile)); entries = g_list_prepend (entries, menu_entry_create (_("&New"), CK_EditNew)); + entries = g_list_prepend (entries, menu_entry_create (_("&Close"), CK_Close)); entries = g_list_prepend (entries, menu_separator_create ()); entries = g_list_prepend (entries, menu_entry_create (_("&Save"), CK_Save)); entries = g_list_prepend (entries, menu_entry_create (_("Save &as..."), CK_SaveAs)); @@ -213,10 +214,10 @@ create_window_menu (void) { GList *entries = NULL; - entries = g_list_append (entries, menu_entry_create (_("&Move"), CK_WindowMove)); - entries = g_list_append (entries, menu_entry_create (_("&Resize"), CK_WindowResize)); + entries = g_list_prepend (entries, menu_entry_create (_("&Move"), CK_WindowMove)); + entries = g_list_prepend (entries, menu_entry_create (_("&Resize"), CK_WindowResize)); - return entries; + return g_list_reverse (entries); } /* --------------------------------------------------------------------------------------------- */ diff --git a/src/editor/editwidget.c b/src/editor/editwidget.c index d5b10e70e..38571b5d7 100644 --- a/src/editor/editwidget.c +++ b/src/editor/editwidget.c @@ -264,7 +264,7 @@ edit_get_shortcut (unsigned long command) static char * edit_get_title (const Dlg_head * h, size_t len) { - const WEdit *edit = (const WEdit *) find_widget_type (h, edit_callback); + const WEdit *edit = find_editor (h); const char *modified = edit->modified ? "(*) " : " "; const char *file_label; char *filename; @@ -482,6 +482,12 @@ edit_dialog_command_execute (Dlg_head * h, unsigned long command) switch (command) { + case CK_Close: + /* if there are no opened files anymore, close MC editor */ + if (edit_widget_is_editor ((Widget *) h->current->data) && + edit_close_cmd ((WEdit *) h->current->data) && find_editor (h) == NULL) + dlg_stop (h); + break; case CK_Help: edit_help (); /* edit->force |= REDRAW_COMPLETELY; */ @@ -555,13 +561,13 @@ edit_dialog_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, vo WMenuBar *menubar; WButtonBar *buttonbar; - edit = (WEdit *) find_widget_type (h, edit_callback); menubar = find_menubar (h); buttonbar = find_buttonbar (h); switch (msg) { case DLG_INIT: + edit = find_editor (h); edit_set_buttonbar (edit, buttonbar); return MSG_HANDLED; @@ -632,7 +638,10 @@ edit_dialog_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, vo case DLG_VALIDATE: h->state = DLG_ACTIVE; /* don't stop the dialog before final decision */ - if (edit->drag_state != MCEDIT_DRAG_NORMAL) + edit = find_editor (h); + if (edit == NULL) + h->state = DLG_CLOSED; + else if (edit->drag_state != MCEDIT_DRAG_NORMAL) edit_restore_size (edit); else if (edit_ok_to_exit (edit)) h->state = DLG_CLOSED; @@ -780,6 +789,14 @@ edit_get_file_name (const WEdit * edit) * @return TRUE if widget is an WEdit class, FALSE otherwise */ +WEdit * +find_editor (const Dlg_head * h) +{ + return (WEdit *) find_widget_type (h, edit_callback); +} + +/* --------------------------------------------------------------------------------------------- */ + gboolean edit_widget_is_editor (const Widget * w) { -- 2.11.4.GIT