mceditor: refactoring.

* (mcedit_arg_t): rename to edit_arg_t and move to src/editor/edit.h.
  * (mcedit_arg_free): rename to edit_arg_free() and move to src/editor/edit.c.
  * (mcedit_arg_vpath_new): rename to edit_arg_vpath_new(), move to src/editor/edit.c
  and make public.
  * (mcedit_arg_new): rename to edit_arg_new(), move to src/editor/edit.c
  and make public.
  * src/editor/editwidget.c: sync with new editor APIs.
  * src/main.c: likewise.
  * src/args.c: likewise.
  * (mc_args_parse): show error in case of run MC as "mc -e" if MC is built
    without builtin editor.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2024-02-23 12:36:18 +03:00
parent f9a2038991
commit dae33eb229
6 changed files with 99 additions and 74 deletions

View File

@ -36,6 +36,10 @@
#include "src/textconf.h"
#ifdef USE_INTERNAL_EDIT
#include "editor/edit.h" /* edit_arg_t */
#endif
#include "src/args.h"
/*** external variables **************************************************************************/
@ -376,9 +380,11 @@ mc_args_add_usage_info (void)
switch (mc_global.mc_run_mode)
{
#ifdef USE_INTERNAL_EDIT
case MC_RUN_EDITOR:
s = g_strdup_printf ("%s\n", _("[+lineno] file1[:lineno] [file2[:lineno]...]"));
break;
#endif /* USE_INTERNAL_EDIT */
case MC_RUN_VIEWER:
s = g_strdup_printf ("%s\n", _("file"));
break;
@ -471,48 +477,14 @@ parse_mc_v_argument (const gchar * option_name, const gchar * value, gpointer da
}
/* --------------------------------------------------------------------------------------------- */
/**
* Create mcedit_arg_t object from vfs_path_t object and the line number.
*
* @param file_vpath file path object
* @param line_number line number. If value is 0, try to restore saved position.
* @return mcedit_arg_t object
*/
static mcedit_arg_t *
mcedit_arg_vpath_new (vfs_path_t * file_vpath, long line_number)
{
mcedit_arg_t *arg;
arg = g_new (mcedit_arg_t, 1);
arg->file_vpath = file_vpath;
arg->line_number = line_number;
return arg;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Create mcedit_arg_t object from file name and the line number.
*
* @param file_name file name
* @param line_number line number. If value is 0, try to restore saved position.
* @return mcedit_arg_t object
*/
static mcedit_arg_t *
mcedit_arg_new (const char *file_name, long line_number)
{
return mcedit_arg_vpath_new (vfs_path_from_str (file_name), line_number);
}
/* --------------------------------------------------------------------------------------------- */
#ifdef USE_INTERNAL_EDIT
/**
* Get list of filenames (and line numbers) from command line, when mc called as editor
*
* @param argc count of all arguments
* @param argv array of strings, contains arguments
* @return list of mcedit_arg_t objects
* @return list of edit_arg_t objects
*/
static GList *
@ -526,7 +498,7 @@ parse_mcedit_arguments (int argc, char **argv)
{
char *tmp;
char *end, *p;
mcedit_arg_t *arg;
edit_arg_t *arg;
tmp = argv[i];
@ -579,36 +551,37 @@ parse_mcedit_arguments (int argc, char **argv)
*/
if (mc_stat (tmp_vpath, &st) == -1 && mc_stat (fname_vpath, &st) != -1)
{
arg = mcedit_arg_vpath_new (fname_vpath, atoi (p));
arg = edit_arg_vpath_new (fname_vpath, atoi (p));
vfs_path_free (tmp_vpath, TRUE);
}
else
{
arg = mcedit_arg_vpath_new (tmp_vpath, 0);
arg = edit_arg_vpath_new (tmp_vpath, 0);
vfs_path_free (fname_vpath, TRUE);
}
g_free (fname);
}
else
arg = mcedit_arg_new (tmp, 0);
arg = edit_arg_new (tmp, 0);
flist = g_list_prepend (flist, arg);
}
if (flist == NULL)
flist = g_list_prepend (flist, mcedit_arg_new (NULL, 0));
flist = g_list_prepend (flist, edit_arg_new (NULL, 0));
else if (first_line_number != -1)
{
/* overwrite line number for first file */
GList *l;
l = g_list_last (flist);
((mcedit_arg_t *) l->data)->line_number = first_line_number;
((edit_arg_t *) l->data)->line_number = first_line_number;
}
return flist;
}
#endif /* USE_INTERNAL_EDIT */
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
@ -621,16 +594,18 @@ mc_setup_run_mode (char **argv)
base = x_basename (argv[0]);
if (strncmp (base, "mce", 3) == 0 || strcmp (base, "vi") == 0)
{
/* mce* or vi is link to mc */
mc_global.mc_run_mode = MC_RUN_EDITOR;
}
else if (strncmp (base, "mcv", 3) == 0 || strcmp (base, "view") == 0)
if (strncmp (base, "mcv", 3) == 0 || strcmp (base, "view") == 0)
{
/* mcv* or view is link to mc */
mc_global.mc_run_mode = MC_RUN_VIEWER;
}
#ifdef USE_INTERNAL_EDIT
else if (strncmp (base, "mce", 3) == 0 || strcmp (base, "vi") == 0)
{
/* mce* or vi is link to mc */
mc_global.mc_run_mode = MC_RUN_EDITOR;
}
#endif
#ifdef USE_DIFF_VIEW
else if (strncmp (base, "mcd", 3) == 0 || strcmp (base, "diff") == 0)
{
@ -640,6 +615,8 @@ mc_setup_run_mode (char **argv)
#endif /* USE_DIFF_VIEW */
}
/* --------------------------------------------------------------------------------------------- */
gboolean
mc_args_parse (int *argc, char ***argv, const char *translation_domain, GError ** mcerror)
{
@ -787,8 +764,13 @@ mc_setup_by_args (int argc, char **argv, GError ** mcerror)
switch (mc_global.mc_run_mode)
{
case MC_RUN_EDITOR:
#ifdef USE_INTERNAL_EDIT
mc_run_param0 = parse_mcedit_arguments (argc - 1, &argv[1]);
break;
#else
mc_propagate_error (mcerror, 0, "%s\n", _("MC is built without builtin editor."));
return FALSE;
#endif
case MC_RUN_VIEWER:
if (tmp == NULL)
@ -829,17 +811,3 @@ mc_setup_by_args (int argc, char **argv, GError ** mcerror)
}
/* --------------------------------------------------------------------------------------------- */
/**
* Free the mcedit_arg_t object.
*
* @param arg mcedit_arg_t object
*/
void
mcedit_arg_free (mcedit_arg_t * arg)
{
vfs_path_free (arg->file_vpath, TRUE);
g_free (arg);
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -10,12 +10,6 @@
/*** structures declarations (and typedefs of structures)*****************************************/
typedef struct
{
vfs_path_t *file_vpath;
long line_number;
} mcedit_arg_t;
/*** global variables defined in .c file *********************************************************/
extern gboolean mc_args__force_xterm;
@ -48,8 +42,6 @@ gboolean mc_args_parse (int *argc, char ***argv, const char *translation_domain,
gboolean mc_args_show_info (void);
gboolean mc_setup_by_args (int argc, char **argv, GError ** mcerror);
void mcedit_arg_free (mcedit_arg_t * arg);
/*** inline functions ****************************************************************************/
#endif /* MC__ARGS_H */

View File

@ -4171,3 +4171,53 @@ edit_move_down (WEdit * edit, long i, gboolean do_scroll)
}
/* --------------------------------------------------------------------------------------------- */
/**
* Create edit_arg_t object from vfs_path_t object and the line number.
*
* @param file_vpath file path object
* @param line_number line number. If value is 0, try to restore saved position.
* @return edit_arg_t object
*/
edit_arg_t *
edit_arg_vpath_new (vfs_path_t * file_vpath, long line_number)
{
edit_arg_t *arg;
arg = g_new (edit_arg_t, 1);
arg->file_vpath = file_vpath;
arg->line_number = line_number;
return arg;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Create edit_arg_t object from file name and the line number.
*
* @param file_name file name
* @param line_number line number. If value is 0, try to restore saved position.
* @return edit_arg_t object
*/
edit_arg_t *
edit_arg_new (const char *file_name, long line_number)
{
return edit_arg_vpath_new (vfs_path_from_str (file_name), line_number);
}
/* --------------------------------------------------------------------------------------------- */
/**
* Free the edit_arg_t object.
*
* @param arg edit_arg_t object
*/
void
edit_arg_free (edit_arg_t * arg)
{
vfs_path_free (arg->file_vpath, TRUE);
g_free (arg);
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -62,6 +62,12 @@ typedef struct
gboolean check_nl_at_eof;
} edit_options_t;
typedef struct
{
vfs_path_t *file_vpath;
long line_number;
} edit_arg_t;
/*** global variables defined in .c file *********************************************************/
extern edit_options_t edit_options;
@ -75,6 +81,10 @@ void edit_stack_free (void);
gboolean edit_file (const vfs_path_t * file_vpath, long line);
gboolean edit_files (const GList * files);
edit_arg_t *edit_arg_vpath_new (vfs_path_t * file_vpath, long line_number);
edit_arg_t *edit_arg_new (const char *file_name, long line_number);
void edit_arg_free (edit_arg_t * arg);
const char *edit_get_file_name (const WEdit * edit);
off_t edit_get_cursor_offset (const WEdit * edit);
long edit_get_curs_col (const WEdit * edit);

View File

@ -60,7 +60,6 @@
#include "src/execute.h" /* toggle_subshell() */
#include "src/filemanager/cmd.h" /* save_setup_cmd() */
#include "src/learn.h" /* learn_keys() */
#include "src/args.h" /* mcedit_arg_t */
#include "edit-impl.h"
#include "editwidget.h"
@ -1199,7 +1198,7 @@ edit_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
gboolean
edit_file (const vfs_path_t * file_vpath, long line)
{
mcedit_arg_t arg = { (vfs_path_t *) file_vpath, line };
edit_arg_t arg = { (vfs_path_t *) file_vpath, line };
GList *files;
gboolean ok;
@ -1270,7 +1269,7 @@ edit_files (const GList * files)
for (file = files; file != NULL; file = g_list_next (file))
{
mcedit_arg_t *f = (mcedit_arg_t *) file->data;
edit_arg_t *f = (edit_arg_t *) file->data;
gboolean f_ok;
f_ok = edit_load_file_from_filename (edit_dlg, f->file_vpath, f->line_number);

View File

@ -63,6 +63,10 @@
#include "filemanager/command.h" /* cmdline */
#include "filemanager/panel.h" /* panalized_panel */
#ifdef USE_INTERNAL_EDIT
#include "editor/edit.h" /* edit_arg_free() */
#endif
#include "vfs/plugins_init.h"
#include "events_init.h"
@ -532,8 +536,10 @@ main (int argc, char *argv[])
if (mc_global.mc_run_mode != MC_RUN_EDITOR)
g_free (mc_run_param0);
#ifdef USE_INTERNAL_EDIT
else
g_list_free_full ((GList *) mc_run_param0, (GDestroyNotify) mcedit_arg_free);
g_list_free_full ((GList *) mc_run_param0, (GDestroyNotify) edit_arg_free);
#endif /* USE_INTERNAL_EDIT */
g_free (mc_run_param1);
g_free (saved_other_dir);