mcedit: move macros-related stuff to editmacros.[ch].

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2021-05-29 19:07:54 +03:00
parent 0df10f488b
commit 5b7b48e1db
7 changed files with 461 additions and 393 deletions

View File

@ -14,6 +14,7 @@ libedit_la_SOURCES = \
editbuffer.c editbuffer.h \
editcmd.c \
editdraw.c \
editmacros.c editmacros.h \
editmenu.c \
editoptions.c \
editsearch.c editsearch.h \

View File

@ -224,20 +224,12 @@ void edit_word_wrap (WEdit * edit);
int edit_sort_cmd (WEdit * edit);
int edit_ext_cmd (WEdit * edit);
int edit_store_macro_cmd (WEdit * edit);
gboolean edit_load_macro_cmd (WEdit * edit);
void edit_delete_macro_cmd (WEdit * edit);
gboolean edit_repeat_macro_cmd (WEdit * edit);
gboolean edit_copy_to_X_buf_cmd (WEdit * edit);
gboolean edit_cut_to_X_buf_cmd (WEdit * edit);
gboolean edit_paste_from_X_buf_cmd (WEdit * edit);
void edit_select_codepage_cmd (WEdit * edit);
void edit_insert_literal_cmd (WEdit * edit);
gboolean edit_execute_macro (WEdit * edit, int hotkey);
void edit_begin_end_macro_cmd (WEdit * edit);
void edit_begin_end_repeat_cmd (WEdit * edit);
void edit_paste_from_history (WEdit * edit);

View File

@ -70,6 +70,7 @@
#include "editwidget.h"
#include "editsearch.h"
#include "editcomplete.h" /* edit_complete_word_cmd() */
#include "editmacros.h"
#include "etags.h" /* edit_get_match_keyword_cmd() */
#ifdef HAVE_ASPELL
#include "spell.h"

View File

@ -65,7 +65,6 @@
#ifdef HAVE_CHARSET
#include "src/selcodepage.h"
#endif
#include "src/keymap.h"
#include "src/util.h" /* check_for_default() */
#include "edit-impl.h"
@ -735,89 +734,6 @@ edit_insert_column_of_text (WEdit * edit, unsigned char *data, off_t size, long
edit_cursor_move (edit, cursor - edit->buffer.curs1);
}
/* --------------------------------------------------------------------------------------------- */
static int
edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
{
const macros_t *m1 = (const macros_t *) macro1;
const macros_t *m2 = (const macros_t *) macro2;
return m1->hotkey - m2->hotkey;
}
/* --------------------------------------------------------------------------------------------- */
static void
edit_macro_sort_by_hotkey (void)
{
if (macros_list != NULL && macros_list->len != 0)
g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
}
/* --------------------------------------------------------------------------------------------- */
static int
edit_get_macro (WEdit * edit, int hotkey)
{
macros_t *array_start;
macros_t *result;
macros_t search_macro = {
.hotkey = hotkey
};
(void) edit;
result = bsearch (&search_macro, macros_list->data, macros_list->len,
sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
if (result == NULL || result->macro == NULL)
return (-1);
array_start = &g_array_index (macros_list, struct macros_t, 0);
return (int) (result - array_start);
}
/* --------------------------------------------------------------------------------------------- */
/** returns FALSE on error */
static gboolean
edit_delete_macro (WEdit * edit, int hotkey)
{
mc_config_t *macros_config = NULL;
const char *section_name = "editor";
gchar *macros_fname;
int indx;
char *skeyname;
/* clear array of actions for current hotkey */
while ((indx = edit_get_macro (edit, hotkey) != -1))
{
macros_t *macros;
macros = &g_array_index (macros_list, struct macros_t, indx);
g_array_free (macros->macro, TRUE);
g_array_remove_index (macros_list, indx);
edit_macro_sort_by_hotkey ();
}
macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
macros_config = mc_config_init (macros_fname, FALSE);
g_free (macros_fname);
if (macros_config == NULL)
return FALSE;
skeyname = lookup_key_by_code (hotkey);
while (mc_config_del_key (macros_config, section_name, skeyname))
;
g_free (skeyname);
mc_config_save_file (macros_config, NULL);
mc_config_deinit (macros_config);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Callback for the iteration of objects in the 'editors' array.
@ -1098,279 +1014,6 @@ edit_save_as_cmd (WEdit * edit)
return ret;
}
/* {{{ Macro stuff starts here */
/* --------------------------------------------------------------------------------------------- */
void
edit_delete_macro_cmd (WEdit * edit)
{
int hotkey;
hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
}
/* --------------------------------------------------------------------------------------------- */
/** returns FALSE on error */
gboolean
edit_execute_macro (WEdit * edit, int hotkey)
{
gboolean res = FALSE;
if (hotkey != 0)
{
int indx;
indx = edit_get_macro (edit, hotkey);
if (indx != -1)
{
const macros_t *macros;
macros = &g_array_index (macros_list, struct macros_t, indx);
if (macros->macro->len != 0)
{
guint i;
edit->force |= REDRAW_PAGE;
for (i = 0; i < macros->macro->len; i++)
{
const macro_action_t *m_act;
m_act = &g_array_index (macros->macro, struct macro_action_t, i);
edit_execute_cmd (edit, m_act->action, m_act->ch);
res = TRUE;
}
}
}
}
return res;
}
/* --------------------------------------------------------------------------------------------- */
/** returns FALSE on error */
gboolean
edit_store_macro_cmd (WEdit * edit)
{
int i;
int hotkey;
GString *macros_string = NULL;
const char *section_name = "editor";
gchar *macros_fname;
GArray *macros = NULL;
int tmp_act;
mc_config_t *macros_config;
char *skeyname;
hotkey =
editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
if (hotkey == ESC_CHAR)
return FALSE;
tmp_act = keybind_lookup_keymap_command (WIDGET (edit)->keymap, hotkey);
/* return FALSE if try assign macro into restricted hotkeys */
if (tmp_act == CK_MacroStartRecord
|| tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
return FALSE;
edit_delete_macro (edit, hotkey);
macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
macros_config = mc_config_init (macros_fname, FALSE);
g_free (macros_fname);
if (macros_config == NULL)
return FALSE;
edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
skeyname = lookup_key_by_code (hotkey);
for (i = 0; i < macro_index; i++)
{
macro_action_t m_act;
const char *action_name;
action_name = keybind_lookup_actionname (record_macro_buf[i].action);
if (action_name == NULL)
break;
if (macros == NULL)
{
macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
macros_string = g_string_sized_new (250);
}
m_act.action = record_macro_buf[i].action;
m_act.ch = record_macro_buf[i].ch;
g_array_append_val (macros, m_act);
g_string_append_printf (macros_string, "%s:%i;", action_name, (int) record_macro_buf[i].ch);
}
if (macros == NULL)
mc_config_del_key (macros_config, section_name, skeyname);
else
{
macros_t macro;
macro.hotkey = hotkey;
macro.macro = macros;
g_array_append_val (macros_list, macro);
mc_config_set_string (macros_config, section_name, skeyname, macros_string->str);
}
g_free (skeyname);
edit_macro_sort_by_hotkey ();
if (macros_string != NULL)
g_string_free (macros_string, TRUE);
mc_config_save_file (macros_config, NULL);
mc_config_deinit (macros_config);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
gboolean
edit_repeat_macro_cmd (WEdit * edit)
{
gboolean ok;
char *f;
long count_repeat = 0;
f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL,
INPUT_COMPLETE_NONE);
ok = (f != NULL && *f != '\0');
if (ok)
{
char *error = NULL;
count_repeat = strtol (f, &error, 0);
ok = (*error == '\0');
}
g_free (f);
if (ok)
{
int i, j;
edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
edit->force |= REDRAW_PAGE;
for (j = 0; j < count_repeat; j++)
for (i = 0; i < macro_index; i++)
edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
edit_update_screen (edit);
}
return ok;
}
/* --------------------------------------------------------------------------------------------- */
/** return FALSE on error */
gboolean
edit_load_macro_cmd (WEdit * edit)
{
mc_config_t *macros_config = NULL;
gchar **profile_keys, **keys;
gchar **values, **curr_values;
const char *section_name = "editor";
gchar *macros_fname;
(void) edit;
if (macros_list == NULL || macros_list->len != 0)
return FALSE;
macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
macros_config = mc_config_init (macros_fname, TRUE);
g_free (macros_fname);
if (macros_config == NULL)
return FALSE;
keys = mc_config_get_keys (macros_config, section_name, NULL);
for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
{
int hotkey;
GArray *macros = NULL;
macros_t macro;
values = mc_config_get_string_list (macros_config, section_name, *profile_keys, NULL);
hotkey = lookup_key (*profile_keys, NULL);
for (curr_values = values; *curr_values != NULL && *curr_values[0] != '\0'; curr_values++)
{
char **macro_pair;
macro_pair = g_strsplit (*curr_values, ":", 2);
if (macro_pair != NULL)
{
macro_action_t m_act = {
.action = 0,
.ch = -1
};
if (macro_pair[0] != NULL && macro_pair[0][0] != '\0')
m_act.action = keybind_lookup_action (macro_pair[0]);
if (macro_pair[1] != NULL && macro_pair[1][0] != '\0')
m_act.ch = strtol (macro_pair[1], NULL, 0);
if (m_act.action != 0)
{
/* a shell command */
if ((m_act.action / CK_PipeBlock (0)) == 1)
{
m_act.action = CK_PipeBlock (0);
if (m_act.ch > 0)
m_act.action += m_act.ch;
m_act.ch = -1;
}
if (macros == NULL)
macros = g_array_new (TRUE, FALSE, sizeof (m_act));
g_array_append_val (macros, m_act);
}
g_strfreev (macro_pair);
}
}
if (macros != NULL)
{
macro.hotkey = hotkey;
macro.macro = macros;
g_array_append_val (macros_list, macro);
}
g_strfreev (values);
}
g_strfreev (keys);
mc_config_deinit (macros_config);
edit_macro_sort_by_hotkey ();
return TRUE;
}
/* }}} Macro stuff end here */
/* --------------------------------------------------------------------------------------------- */
/** returns TRUE on success */
@ -2363,34 +2006,6 @@ edit_insert_literal_cmd (WEdit * edit)
/* --------------------------------------------------------------------------------------------- */
void
edit_begin_end_macro_cmd (WEdit * edit)
{
/* edit is a pointer to the widget */
if (edit != NULL)
{
long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
edit_execute_key_command (edit, command, -1);
}
}
/* --------------------------------------------------------------------------------------------- */
void
edit_begin_end_repeat_cmd (WEdit * edit)
{
/* edit is a pointer to the widget */
if (edit != NULL)
{
long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
edit_execute_key_command (edit, command, -1);
}
}
/* --------------------------------------------------------------------------------------------- */
gboolean
edit_load_forward_cmd (WEdit * edit)
{

434
src/editor/editmacros.c Normal file
View File

@ -0,0 +1,434 @@
/*
Editor macros engine
Copyright (C) 2001-2021
Free Software Foundation, Inc.
This file is part of the Midnight Commander.
The Midnight Commander is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
The Midnight Commander is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdlib.h>
#include "lib/global.h"
#include "lib/mcconfig.h"
#include "lib/tty/key.h" /* lookup_key*() */
#include "lib/keybind.h" /* keybind_lookup_actionname() */
#include "lib/fileloc.h"
#include "src/setup.h" /* macro_action_t */
#include "src/history.h" /* MC_HISTORY_EDIT_REPEAT */
#include "editwidget.h"
#include "editmacros.h"
/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
static int
edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
{
const macros_t *m1 = (const macros_t *) macro1;
const macros_t *m2 = (const macros_t *) macro2;
return m1->hotkey - m2->hotkey;
}
/* --------------------------------------------------------------------------------------------- */
static void
edit_macro_sort_by_hotkey (void)
{
if (macros_list != NULL && macros_list->len != 0)
g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
}
/* --------------------------------------------------------------------------------------------- */
static int
edit_get_macro (WEdit * edit, int hotkey)
{
macros_t *array_start;
macros_t *result;
macros_t search_macro = {
.hotkey = hotkey
};
(void) edit;
result = bsearch (&search_macro, macros_list->data, macros_list->len,
sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
if (result == NULL || result->macro == NULL)
return (-1);
array_start = &g_array_index (macros_list, struct macros_t, 0);
return (int) (result - array_start);
}
/* --------------------------------------------------------------------------------------------- */
/** returns FALSE on error */
static gboolean
edit_delete_macro (WEdit * edit, int hotkey)
{
mc_config_t *macros_config = NULL;
const char *section_name = "editor";
gchar *macros_fname;
int indx;
char *skeyname;
/* clear array of actions for current hotkey */
while ((indx = edit_get_macro (edit, hotkey) != -1))
{
macros_t *macros;
macros = &g_array_index (macros_list, struct macros_t, indx);
g_array_free (macros->macro, TRUE);
g_array_remove_index (macros_list, indx);
edit_macro_sort_by_hotkey ();
}
macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
macros_config = mc_config_init (macros_fname, FALSE);
g_free (macros_fname);
if (macros_config == NULL)
return FALSE;
skeyname = lookup_key_by_code (hotkey);
while (mc_config_del_key (macros_config, section_name, skeyname))
;
g_free (skeyname);
mc_config_save_file (macros_config, NULL);
mc_config_deinit (macros_config);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/** returns FALSE on error */
gboolean
edit_store_macro_cmd (WEdit * edit)
{
int i;
int hotkey;
GString *macros_string = NULL;
const char *section_name = "editor";
gchar *macros_fname;
GArray *macros = NULL;
int tmp_act;
mc_config_t *macros_config;
char *skeyname;
hotkey =
editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
if (hotkey == ESC_CHAR)
return FALSE;
tmp_act = keybind_lookup_keymap_command (WIDGET (edit)->keymap, hotkey);
/* return FALSE if try assign macro into restricted hotkeys */
if (tmp_act == CK_MacroStartRecord
|| tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
return FALSE;
edit_delete_macro (edit, hotkey);
macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
macros_config = mc_config_init (macros_fname, FALSE);
g_free (macros_fname);
if (macros_config == NULL)
return FALSE;
edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
skeyname = lookup_key_by_code (hotkey);
for (i = 0; i < macro_index; i++)
{
macro_action_t m_act;
const char *action_name;
action_name = keybind_lookup_actionname (record_macro_buf[i].action);
if (action_name == NULL)
break;
if (macros == NULL)
{
macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
macros_string = g_string_sized_new (250);
}
m_act.action = record_macro_buf[i].action;
m_act.ch = record_macro_buf[i].ch;
g_array_append_val (macros, m_act);
g_string_append_printf (macros_string, "%s:%i;", action_name, (int) record_macro_buf[i].ch);
}
if (macros == NULL)
mc_config_del_key (macros_config, section_name, skeyname);
else
{
macros_t macro;
macro.hotkey = hotkey;
macro.macro = macros;
g_array_append_val (macros_list, macro);
mc_config_set_string (macros_config, section_name, skeyname, macros_string->str);
}
g_free (skeyname);
edit_macro_sort_by_hotkey ();
if (macros_string != NULL)
g_string_free (macros_string, TRUE);
mc_config_save_file (macros_config, NULL);
mc_config_deinit (macros_config);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
/** return FALSE on error */
gboolean
edit_load_macro_cmd (WEdit * edit)
{
mc_config_t *macros_config = NULL;
gchar **profile_keys, **keys;
gchar **values, **curr_values;
const char *section_name = "editor";
gchar *macros_fname;
(void) edit;
if (macros_list == NULL || macros_list->len != 0)
return FALSE;
macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
macros_config = mc_config_init (macros_fname, TRUE);
g_free (macros_fname);
if (macros_config == NULL)
return FALSE;
keys = mc_config_get_keys (macros_config, section_name, NULL);
for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
{
int hotkey;
GArray *macros = NULL;
macros_t macro;
values = mc_config_get_string_list (macros_config, section_name, *profile_keys, NULL);
hotkey = lookup_key (*profile_keys, NULL);
for (curr_values = values; *curr_values != NULL && *curr_values[0] != '\0'; curr_values++)
{
char **macro_pair;
macro_pair = g_strsplit (*curr_values, ":", 2);
if (macro_pair != NULL)
{
macro_action_t m_act = {
.action = 0,
.ch = -1
};
if (macro_pair[0] != NULL && macro_pair[0][0] != '\0')
m_act.action = keybind_lookup_action (macro_pair[0]);
if (macro_pair[1] != NULL && macro_pair[1][0] != '\0')
m_act.ch = strtol (macro_pair[1], NULL, 0);
if (m_act.action != 0)
{
/* a shell command */
if ((m_act.action / CK_PipeBlock (0)) == 1)
{
m_act.action = CK_PipeBlock (0);
if (m_act.ch > 0)
m_act.action += m_act.ch;
m_act.ch = -1;
}
if (macros == NULL)
macros = g_array_new (TRUE, FALSE, sizeof (m_act));
g_array_append_val (macros, m_act);
}
g_strfreev (macro_pair);
}
}
if (macros != NULL)
{
macro.hotkey = hotkey;
macro.macro = macros;
g_array_append_val (macros_list, macro);
}
g_strfreev (values);
}
g_strfreev (keys);
mc_config_deinit (macros_config);
edit_macro_sort_by_hotkey ();
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
void
edit_delete_macro_cmd (WEdit * edit)
{
int hotkey;
hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
}
/* --------------------------------------------------------------------------------------------- */
gboolean
edit_repeat_macro_cmd (WEdit * edit)
{
gboolean ok;
char *f;
long count_repeat = 0;
f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL,
INPUT_COMPLETE_NONE);
ok = (f != NULL && *f != '\0');
if (ok)
{
char *error = NULL;
count_repeat = strtol (f, &error, 0);
ok = (*error == '\0');
}
g_free (f);
if (ok)
{
int i, j;
edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
edit->force |= REDRAW_PAGE;
for (j = 0; j < count_repeat; j++)
for (i = 0; i < macro_index; i++)
edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
edit_update_screen (edit);
}
return ok;
}
/* --------------------------------------------------------------------------------------------- */
/** returns FALSE on error */
gboolean
edit_execute_macro (WEdit * edit, int hotkey)
{
gboolean res = FALSE;
if (hotkey != 0)
{
int indx;
indx = edit_get_macro (edit, hotkey);
if (indx != -1)
{
const macros_t *macros;
macros = &g_array_index (macros_list, struct macros_t, indx);
if (macros->macro->len != 0)
{
guint i;
edit->force |= REDRAW_PAGE;
for (i = 0; i < macros->macro->len; i++)
{
const macro_action_t *m_act;
m_act = &g_array_index (macros->macro, struct macro_action_t, i);
edit_execute_cmd (edit, m_act->action, m_act->ch);
res = TRUE;
}
}
}
}
return res;
}
/* --------------------------------------------------------------------------------------------- */
void
edit_begin_end_macro_cmd (WEdit * edit)
{
/* edit is a pointer to the widget */
if (edit != NULL)
{
long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
edit_execute_key_command (edit, command, -1);
}
}
/* --------------------------------------------------------------------------------------------- */
void
edit_begin_end_repeat_cmd (WEdit * edit)
{
/* edit is a pointer to the widget */
if (edit != NULL)
{
long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
edit_execute_key_command (edit, command, -1);
}
}
/* --------------------------------------------------------------------------------------------- */

24
src/editor/editmacros.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef MC__EDIT_MACROS_H
#define MC__EDIT_MACROS_H 1
/*** typedefs(not structures) and defined constants **********************************************/
/*** enums ***************************************************************************************/
/*** structures declarations (and typedefs of structures)*****************************************/
/*** global variables defined in .c file *********************************************************/
/*** declarations of public functions ************************************************************/
int edit_store_macro_cmd (WEdit * edit);
gboolean edit_load_macro_cmd (WEdit * edit);
void edit_delete_macro_cmd (WEdit * edit);
gboolean edit_repeat_macro_cmd (WEdit * edit);
gboolean edit_execute_macro (WEdit * edit, int hotkey);
void edit_begin_end_macro_cmd (WEdit * edit);
void edit_begin_end_repeat_cmd (WEdit * edit);
/*** inline functions ****************************************************************************/
#endif /* MC__EDIT_MACROS_H */

View File

@ -64,6 +64,7 @@
#include "edit-impl.h"
#include "editwidget.h"
#include "editmacros.h" /* edit_execute_macro() */
#ifdef HAVE_ASPELL
#include "spell.h"
#endif