Merge branch '2968_viewer_next_prev_mode'

* 2968_viewer_next_prev_mode:
  Fix mode switch after CK_FileNext/CK_FilePrev.
  (regex_command_for): new function to use regex_command in already created viewer/editor
  (exec_extension): rename variable.
  (exec_extension_view): refactoring: remove temporary file in exec_extension().
  Ticket #2968: mcviewer: broken Raw/Parse and Format/UnformatRaw/Parse and Format/Unformat
This commit is contained in:
Andrew Borodin 2013-04-12 09:14:12 +04:00
commit 02265113cd
9 changed files with 112 additions and 52 deletions

View File

@ -98,9 +98,22 @@ static gboolean is_cd = FALSE;
static gboolean written_nonspace = FALSE; static gboolean written_nonspace = FALSE;
static gboolean do_local_copy = FALSE; static gboolean do_local_copy = FALSE;
/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/ /*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static void
exec_cleanup_script (vfs_path_t * script_vpath)
{
if (script_vpath != NULL)
{
(void) mc_unlink (script_vpath);
vfs_path_free (script_vpath);
}
}
/* --------------------------------------------------------------------------------------------- */
static void static void
exec_cleanup_file_name (const vfs_path_t * filename_vpath, gboolean has_changed) exec_cleanup_file_name (const vfs_path_t * filename_vpath, gboolean has_changed)
{ {
@ -142,6 +155,7 @@ exec_get_file_name (const vfs_path_t * filename_vpath)
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static char * static char *
exec_expand_format (char symbol, gboolean is_result_quoted) exec_expand_format (char symbol, gboolean is_result_quoted)
{ {
@ -337,8 +351,7 @@ exec_make_shell_string (const char *lc_data, const vfs_path_t * filename_vpath)
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static void static void
exec_extension_view (char *cmd, const vfs_path_t * filename_vpath, int start_line, exec_extension_view (void *target, char *cmd, const vfs_path_t * filename_vpath, int start_line)
vfs_path_t * temp_file_name_vpath)
{ {
int def_hex_mode = mcview_default_hex_mode, changed_hex_mode = 0; int def_hex_mode = mcview_default_hex_mode, changed_hex_mode = 0;
int def_nroff_flag = mcview_default_nroff_flag, changed_nroff_flag = 0; int def_nroff_flag = mcview_default_nroff_flag, changed_nroff_flag = 0;
@ -350,16 +363,16 @@ exec_extension_view (char *cmd, const vfs_path_t * filename_vpath, int start_lin
if (def_nroff_flag != mcview_default_nroff_flag) if (def_nroff_flag != mcview_default_nroff_flag)
changed_nroff_flag = 1; changed_nroff_flag = 1;
/* If we've written whitespace only, then just load filename if (target == NULL)
* into view
*/
if (written_nonspace)
{
mcview_viewer (cmd, filename_vpath, start_line); mcview_viewer (cmd, filename_vpath, start_line);
mc_unlink (temp_file_name_vpath);
}
else else
mcview_viewer (NULL, filename_vpath, start_line); {
char *file_name;
file_name = vfs_path_to_str (filename_vpath);
mcview_load ((mcview_t *) target, cmd, file_name, start_line);
g_free (file_name);
}
if (changed_hex_mode && !mcview_altered_hex_mode) if (changed_hex_mode && !mcview_altered_hex_mode)
mcview_default_hex_mode = def_hex_mode; mcview_default_hex_mode = def_hex_mode;
@ -397,16 +410,16 @@ exec_extension_cd (void)
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static void static vfs_path_t *
exec_extension (const vfs_path_t * filename_vpath, const char *lc_data, int start_line) exec_extension (void *target, const vfs_path_t * filename_vpath, const char *lc_data, int start_line)
{ {
char *shell_string, *export_variables; char *shell_string, *export_variables;
vfs_path_t *temp_file_name_vpath = NULL; vfs_path_t *script_vpath = NULL;
int cmd_file_fd; int cmd_file_fd;
FILE *cmd_file; FILE *cmd_file;
char *cmd = NULL; char *cmd = NULL;
g_return_if_fail (lc_data != NULL); g_return_val_if_fail (lc_data != NULL, NULL);
pbuffer = NULL; pbuffer = NULL;
localmtime = 0; localmtime = 0;
@ -436,7 +449,7 @@ exec_extension (const vfs_path_t * filename_vpath, const char *lc_data, int star
* Sometimes it's not needed (e.g. for %cd and %view commands), * Sometimes it's not needed (e.g. for %cd and %view commands),
* but it's easier to create it anyway. * but it's easier to create it anyway.
*/ */
cmd_file_fd = mc_mkstemps (&temp_file_name_vpath, "mcext", SCRIPT_SUFFIX); cmd_file_fd = mc_mkstemps (&script_vpath, "mcext", SCRIPT_SUFFIX);
if (cmd_file_fd == -1) if (cmd_file_fd == -1)
{ {
@ -467,7 +480,7 @@ exec_extension (const vfs_path_t * filename_vpath, const char *lc_data, int star
{ {
char *file_name; char *file_name;
file_name = vfs_path_to_str (temp_file_name_vpath); file_name = vfs_path_to_str (script_vpath);
fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name); fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
g_free (file_name); g_free (file_name);
} }
@ -476,24 +489,29 @@ exec_extension (const vfs_path_t * filename_vpath, const char *lc_data, int star
if ((run_view && !written_nonspace) || is_cd) if ((run_view && !written_nonspace) || is_cd)
{ {
mc_unlink (temp_file_name_vpath); exec_cleanup_script (script_vpath);
vfs_path_free (temp_file_name_vpath); script_vpath = NULL;
temp_file_name_vpath = NULL;
} }
else else
{ {
char *file_name; char *file_name;
file_name = vfs_path_to_str (temp_file_name_vpath); file_name = vfs_path_to_str (script_vpath);
/* Set executable flag on the command file ... */ /* Set executable flag on the command file ... */
mc_chmod (temp_file_name_vpath, S_IRWXU); mc_chmod (script_vpath, S_IRWXU);
/* ... but don't rely on it - run /bin/sh explicitly */ /* ... but don't rely on it - run /bin/sh explicitly */
cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL); cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
g_free (file_name); g_free (file_name);
} }
if (run_view) if (run_view)
exec_extension_view (cmd, filename_vpath, start_line, temp_file_name_vpath); {
/* If we've written whitespace only, then just load filename into view */
if (!written_nonspace)
exec_extension_view (target, NULL, filename_vpath, start_line);
else
exec_extension_view (target, cmd, filename_vpath, start_line);
}
else else
{ {
shell_execute (cmd, EXECUTE_INTERNAL); shell_execute (cmd, EXECUTE_INTERNAL);
@ -511,7 +529,7 @@ exec_extension (const vfs_path_t * filename_vpath, const char *lc_data, int star
exec_cleanup_file_name (filename_vpath, TRUE); exec_cleanup_file_name (filename_vpath, TRUE);
ret: ret:
vfs_path_free (temp_file_name_vpath); return script_vpath;
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -756,6 +774,7 @@ flush_extension_file (void)
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
/** /**
* The second argument is action, i.e. Open, View or Edit * The second argument is action, i.e. Open, View or Edit
* Use target object to open file in.
* *
* This function returns: * This function returns:
* *
@ -768,7 +787,8 @@ flush_extension_file (void)
*/ */
int int
regex_command (const vfs_path_t * filename_vpath, const char *action) regex_command_for (void *target, const vfs_path_t * filename_vpath, const char *action,
vfs_path_t ** script_vpath)
{ {
char *filename, *p, *q, *r, c; char *filename, *p, *q, *r, c;
size_t file_len; size_t file_len;
@ -784,6 +804,9 @@ regex_command (const vfs_path_t * filename_vpath, const char *action)
if (filename_vpath == NULL) if (filename_vpath == NULL)
return 0; return 0;
if (script_vpath != NULL)
*script_vpath = NULL;
/* Check for the special View:%d parameter */ /* Check for the special View:%d parameter */
if (strncmp (action, "View:", 5) == 0) if (strncmp (action, "View:", 5) == 0)
{ {
@ -1022,7 +1045,14 @@ regex_command (const vfs_path_t * filename_vpath, const char *action)
*/ */
if (p < q) if (p < q)
{ {
exec_extension (filename_vpath, r + 1, view_at_line_number); vfs_path_t *sv;
sv = exec_extension (target, filename_vpath, r + 1, view_at_line_number);
if (script_vpath != NULL)
*script_vpath = sv;
else
exec_cleanup_script (sv);
ret = 1; ret = 1;
} }
break; break;

View File

@ -14,7 +14,8 @@
/*** declarations of public functions ************************************************************/ /*** declarations of public functions ************************************************************/
int regex_command (const vfs_path_t * filename_vpath, const char *action); int regex_command_for (void *target, const vfs_path_t * filename_vpath, const char *action,
vfs_path_t ** script_vpath);
/* Call it after the user has edited the mc.ext file, /* Call it after the user has edited the mc.ext file,
* to flush the cached mc.ext file * to flush the cached mc.ext file
@ -22,4 +23,11 @@ int regex_command (const vfs_path_t * filename_vpath, const char *action);
void flush_extension_file (void); void flush_extension_file (void);
/*** inline functions ****************************************************************************/ /*** inline functions ****************************************************************************/
static inline int
regex_command (const vfs_path_t * filename_vpath, const char *action)
{
return regex_command_for (NULL, filename_vpath, action, NULL);
}
#endif /* MC__EXT_H */ #endif /* MC__EXT_H */

View File

@ -3,7 +3,7 @@
Callback function for some actions (hotkeys, menu) Callback function for some actions (hotkeys, menu)
Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2009, 2011 2004, 2005, 2006, 2007, 2009, 2011, 2013
The Free Software Foundation, Inc. The Free Software Foundation, Inc.
Written by: Written by:
@ -15,7 +15,7 @@
Pavel Machek, 1998 Pavel Machek, 1998
Roland Illig <roland.illig@gmx.de>, 2004, 2005 Roland Illig <roland.illig@gmx.de>, 2004, 2005
Slava Zanko <slavazanko@google.com>, 2009 Slava Zanko <slavazanko@google.com>, 2009
Andrew Borodin <aborodin@vmail.ru>, 2009 Andrew Borodin <aborodin@vmail.ru>, 2009, 2013
Ilia Maslakov <il.smind@gmail.com>, 2009 Ilia Maslakov <il.smind@gmail.com>, 2009
This file is part of the Midnight Commander. This file is part of the Midnight Commander.
@ -63,13 +63,13 @@
#include "src/filemanager/layout.h" #include "src/filemanager/layout.h"
#include "src/filemanager/cmd.h" #include "src/filemanager/cmd.h"
#include "src/filemanager/midnight.h" /* current_panel */ #include "src/filemanager/midnight.h" /* current_panel */
#include "src/filemanager/ext.h" /* regex_command_for() */
#include "src/history.h" #include "src/history.h"
#include "src/execute.h" #include "src/execute.h"
#include "src/keybind-defaults.h" #include "src/keybind-defaults.h"
#include "internal.h" #include "internal.h"
#include "mcviewer.h"
/*** global variables ****************************************************************************/ /*** global variables ****************************************************************************/
@ -79,9 +79,23 @@
/*** file scope variables ************************************************************************/ /*** file scope variables ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/ /*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static void
mcview_remove_ext_script (mcview_t * view)
{
if (view->ext_script != NULL)
{
mc_unlink (view->ext_script);
vfs_path_free (view->ext_script);
view->ext_script = NULL;
}
}
/* --------------------------------------------------------------------------------------------- */
/* Both views */ /* Both views */
static void static void
mcview_search (mcview_t * view) mcview_search (mcview_t * view)
@ -321,7 +335,7 @@ mcview_load_next_prev (mcview_t * view, int direction)
dir_list *dir; dir_list *dir;
int *dir_count, *dir_idx; int *dir_count, *dir_idx;
vfs_path_t *vfile; vfs_path_t *vfile;
char *file; vfs_path_t *ext_script = NULL;
mcview_load_next_prev_init (view); mcview_load_next_prev_init (view);
mcview_scan_for_file (view, direction); mcview_scan_for_file (view, direction);
@ -334,15 +348,22 @@ mcview_load_next_prev (mcview_t * view, int direction)
view->dir_count = NULL; view->dir_count = NULL;
view->dir_idx = NULL; view->dir_idx = NULL;
vfile = vfs_path_append_new (view->workdir_vpath, dir->list[*dir_idx].fname, (char *) NULL); vfile = vfs_path_append_new (view->workdir_vpath, dir->list[*dir_idx].fname, (char *) NULL);
file = vfs_path_to_str (vfile);
vfs_path_free (vfile);
mcview_done (view); mcview_done (view);
mcview_remove_ext_script (view);
mcview_init (view); mcview_init (view);
if (regex_command_for (view, vfile, "View", &ext_script) == 0)
{
char *file;
file = vfs_path_to_str (vfile);
mcview_load (view, NULL, file, 0); mcview_load (view, NULL, file, 0);
g_free (file); g_free (file);
}
vfs_path_free (vfile);
view->dir = dir; view->dir = dir;
view->dir_count = dir_count; view->dir_count = dir_count;
view->dir_idx = dir_idx; view->dir_idx = dir_idx;
view->ext_script = ext_script;
view->dpy_bbar_dirty = FALSE; /* FIXME */ view->dpy_bbar_dirty = FALSE; /* FIXME */
view->dirty++; view->dirty++;
@ -623,6 +644,7 @@ mcview_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *
mcview_ok_to_quit (view); mcview_ok_to_quit (view);
} }
mcview_done (view); mcview_done (view);
mcview_remove_ext_script (view);
return MSG_HANDLED; return MSG_HANDLED;
default: default:

View File

@ -3,7 +3,7 @@
Function for whow info on display Function for whow info on display
Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2009, 2011 2004, 2005, 2006, 2007, 2009, 2011, 2013
The Free Software Foundation, Inc. The Free Software Foundation, Inc.
Written by: Written by:
@ -15,7 +15,7 @@
Pavel Machek, 1998 Pavel Machek, 1998
Roland Illig <roland.illig@gmx.de>, 2004, 2005 Roland Illig <roland.illig@gmx.de>, 2004, 2005
Slava Zanko <slavazanko@google.com>, 2009 Slava Zanko <slavazanko@google.com>, 2009
Andrew Borodin <aborodin@vmail.ru>, 2009 Andrew Borodin <aborodin@vmail.ru>, 2009, 2013
Ilia Maslakov <il.smind@gmail.com>, 2009, 2010 Ilia Maslakov <il.smind@gmail.com>, 2009, 2010
This file is part of the Midnight Commander. This file is part of the Midnight Commander.
@ -52,7 +52,6 @@
#include "src/keybind-defaults.h" #include "src/keybind-defaults.h"
#include "internal.h" #include "internal.h"
#include "mcviewer.h"
/*** global variables ****************************************************************************/ /*** global variables ****************************************************************************/

View File

@ -14,6 +14,8 @@
#include "src/keybind-defaults.h" /* global_keymap_t */ #include "src/keybind-defaults.h" /* global_keymap_t */
#include "src/filemanager/dir.h" /* dir_list */ #include "src/filemanager/dir.h" /* dir_list */
#include "mcviewer.h"
/*** typedefs(not structures) and defined constants **********************************************/ /*** typedefs(not structures) and defined constants **********************************************/
typedef unsigned char byte; typedef unsigned char byte;
@ -87,7 +89,7 @@ typedef struct
struct mcview_nroff_struct; struct mcview_nroff_struct;
typedef struct mcview_struct struct mcview_struct
{ {
Widget widget; Widget widget;
@ -186,7 +188,8 @@ typedef struct mcview_struct
* Pointer is used here as reference to WPanel::count */ * Pointer is used here as reference to WPanel::count */
int *dir_idx; /* Index of current file in dir structure. int *dir_idx; /* Index of current file in dir structure.
* Pointer is used here as reference to WPanel::count */ * Pointer is used here as reference to WPanel::count */
} mcview_t; vfs_path_t *ext_script; /* Temporary script file created by regex_command_for() */
};
typedef struct mcview_nroff_struct typedef struct mcview_nroff_struct
{ {

View File

@ -3,7 +3,7 @@
Common finctions (used from some other mcviewer functions) Common finctions (used from some other mcviewer functions)
Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2009, 2011 2004, 2005, 2006, 2007, 2009, 2011, 2013
The Free Software Foundation, Inc. The Free Software Foundation, Inc.
Written by: Written by:
@ -15,7 +15,7 @@
Pavel Machek, 1998 Pavel Machek, 1998
Roland Illig <roland.illig@gmx.de>, 2004, 2005 Roland Illig <roland.illig@gmx.de>, 2004, 2005
Slava Zanko <slavazanko@google.com>, 2009 Slava Zanko <slavazanko@google.com>, 2009
Andrew Borodin <aborodin@vmail.ru>, 2009 Andrew Borodin <aborodin@vmail.ru>, 2009, 2013
Ilia Maslakov <il.smind@gmail.com>, 2009 Ilia Maslakov <il.smind@gmail.com>, 2009
This file is part of the Midnight Commander. This file is part of the Midnight Commander.
@ -53,7 +53,6 @@
#endif #endif
#include "internal.h" #include "internal.h"
#include "mcviewer.h"
/*** global variables ****************************************************************************/ /*** global variables ****************************************************************************/

View File

@ -3,7 +3,7 @@
Interface functions Interface functions
Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2009, 2011 2004, 2005, 2006, 2007, 2009, 2011, 2013
The Free Software Foundation, Inc. The Free Software Foundation, Inc.
Written by: Written by:
@ -15,7 +15,7 @@
Pavel Machek, 1998 Pavel Machek, 1998
Roland Illig <roland.illig@gmx.de>, 2004, 2005 Roland Illig <roland.illig@gmx.de>, 2004, 2005
Slava Zanko <slavazanko@google.com>, 2009 Slava Zanko <slavazanko@google.com>, 2009
Andrew Borodin <aborodin@vmail.ru>, 2009 Andrew Borodin <aborodin@vmail.ru>, 2009, 2013
Ilia Maslakov <il.smind@gmail.com>, 2009 Ilia Maslakov <il.smind@gmail.com>, 2009
This file is part of the Midnight Commander. This file is part of the Midnight Commander.
@ -50,7 +50,6 @@
#include "src/filemanager/midnight.h" /* the_menubar */ #include "src/filemanager/midnight.h" /* the_menubar */
#include "internal.h" #include "internal.h"
#include "mcviewer.h"
/*** global variables ****************************************************************************/ /*** global variables ****************************************************************************/

View File

@ -9,12 +9,13 @@
/*** typedefs(not structures) and defined constants **********************************************/ /*** typedefs(not structures) and defined constants **********************************************/
struct mcview_struct;
/*** enums ***************************************************************************************/ /*** enums ***************************************************************************************/
/*** structures declarations (and typedefs of structures)*****************************************/ /*** structures declarations (and typedefs of structures)*****************************************/
struct mcview_struct;
typedef struct mcview_struct mcview_t;
/*** global variables defined in .c file *********************************************************/ /*** global variables defined in .c file *********************************************************/
extern int mcview_default_hex_mode; extern int mcview_default_hex_mode;
@ -36,7 +37,7 @@ extern char *mcview_show_eof;
/* Creates a new mcview_t object with the given properties. Caveat: the /* Creates a new mcview_t object with the given properties. Caveat: the
* origin is in y-x order, while the extent is in x-y order. */ * origin is in y-x order, while the extent is in x-y order. */
extern struct mcview_struct *mcview_new (int y, int x, int lines, int cols, gboolean is_panel); extern mcview_t *mcview_new (int y, int x, int lines, int cols, gboolean is_panel);
/* Shows {file} or the output of {command} in the internal viewer, /* Shows {file} or the output of {command} in the internal viewer,
@ -44,7 +45,7 @@ extern struct mcview_struct *mcview_new (int y, int x, int lines, int cols, gboo
*/ */
extern gboolean mcview_viewer (const char *command, const vfs_path_t * file_vpath, int start_line); extern gboolean mcview_viewer (const char *command, const vfs_path_t * file_vpath, int start_line);
extern gboolean mcview_load (struct mcview_struct *, const char *, const char *, int); extern gboolean mcview_load (mcview_t * view, const char *command, const char *file, int start_line);
/*** inline functions ****************************************************************************/ /*** inline functions ****************************************************************************/
#endif /* MC__VIEWER_H */ #endif /* MC__VIEWER_H */

View File

@ -3,7 +3,7 @@
Function for plain view Function for plain view
Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2009, 2011 2004, 2005, 2006, 2007, 2009, 2011, 2013
The Free Software Foundation, Inc. The Free Software Foundation, Inc.
Written by: Written by:
@ -15,7 +15,7 @@
Pavel Machek, 1998 Pavel Machek, 1998
Roland Illig <roland.illig@gmx.de>, 2004, 2005 Roland Illig <roland.illig@gmx.de>, 2004, 2005
Slava Zanko <slavazanko@google.com>, 2009 Slava Zanko <slavazanko@google.com>, 2009
Andrew Borodin <aborodin@vmail.ru>, 2009, 2010 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013
Ilia Maslakov <il.smind@gmail.com>, 2009 Ilia Maslakov <il.smind@gmail.com>, 2009
This file is part of the Midnight Commander. This file is part of the Midnight Commander.
@ -47,7 +47,6 @@
#include "src/setup.h" /* option_tab_spacing */ #include "src/setup.h" /* option_tab_spacing */
#include "internal.h" #include "internal.h"
#include "mcviewer.h" /* mcview_show_eof */
/*** global variables ****************************************************************************/ /*** global variables ****************************************************************************/