Refactoring: move some functions from src/main.c into more meaningful places.

Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
Slava Zanko 2009-09-29 02:02:44 +03:00
parent 54b0349fcd
commit 8d4518eacb
12 changed files with 220 additions and 226 deletions

115
src/cmd.c
View File

@ -111,6 +111,89 @@ typedef enum {
static select_flags_t select_flags = SELECT_MATCH_CASE | SELECT_SHELL_PATTERNS;
/*
* If we moved to the parent directory move the selection pointer to
* the old directory name; If we leave VFS dir, remove FS specificator.
*
* You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
*/
static const char *
get_parent_dir_name (const char *cwd, const char *lwd)
{
const char *p;
if (strlen (lwd) > strlen (cwd))
if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
((gsize)strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
cwd[1] == '\0'))) {
return (p + 1);
}
return NULL;
}
/*
* Changes the current directory of the panel.
* Don't record change in the directory history.
*/
int
cmd_do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
{
const char *directory;
char *olddir;
char temp[MC_MAXPATHLEN];
char *translated_url;
if (cd_type == cd_parse_command) {
while (*new_dir == ' ')
new_dir++;
}
olddir = g_strdup (panel->cwd);
new_dir = translated_url = vfs_translate_url (new_dir);
/* Convert *new_path to a suitable pathname, handle ~user */
if (cd_type == cd_parse_command) {
if (!strcmp (new_dir, "-")) {
strcpy (temp, panel->lwd);
new_dir = temp;
}
}
directory = *new_dir ? new_dir : home_dir;
if (mc_chdir (directory) == -1) {
strcpy (panel->cwd, olddir);
g_free (olddir);
g_free (translated_url);
return 0;
}
g_free (translated_url);
/* Success: save previous directory, shutdown status of previous dir */
strcpy (panel->lwd, olddir);
free_completions (cmdline);
mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
vfs_release_path (olddir);
subshell_chdir (panel->cwd);
/* Reload current panel */
panel_clean_dir (panel);
panel->count =
do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
panel->reverse, panel->case_sensitive,
panel->exec_first, panel->filter);
try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
load_hint (0);
panel->dirty = 1;
update_xterm_title_path ();
g_free (olddir);
return 1;
}
int
view_file_at_line (const char *filename, int plain_view, int internal,
int start_line)
@ -1445,3 +1528,35 @@ encoding_cmd (void)
panel = MENU_PANEL;
set_panel_encoding (panel);
}
void
directory_history_add (struct WPanel *panel, const char *dir)
{
char *tmp;
tmp = g_strdup (dir);
strip_password (tmp, 1);
panel->dir_history = list_append_unique (panel->dir_history, tmp);
}
/*
* Changes the current directory of the panel.
* Record change in the directory history.
*/
int
do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
{
int r;
r = cmd_do_panel_cd (panel, new_dir, cd_type);
if (r)
directory_history_add (panel, panel->cwd);
return r;
}
int
do_cd (const char *new_dir, enum cd_enum exact)
{
return (do_panel_cd (current_panel, new_dir, exact));
}

View File

@ -66,4 +66,17 @@ void quick_view_cmd (void);
void toggle_listing_cmd (void);
void encoding_cmd (void);
struct WPanel;
enum cd_enum {
cd_parse_command,
cd_exact
};
int do_cd (const char *, enum cd_enum); /* For find.c */
void directory_history_add (struct WPanel *, const char *);
int do_panel_cd (struct WPanel *, const char *, enum cd_enum);
int cmd_do_panel_cd (struct WPanel *, const char *, enum cd_enum);
#endif

View File

@ -34,6 +34,7 @@
#include "global.h" /* home_dir */
#include "args.h" /* mc_args__use_subshell */
#include "cmd.h"
#include "../src/tty/tty.h"
#include "widget.h" /* WInput */
#include "command.h"

View File

@ -31,6 +31,7 @@
#include "../src/tty/key.h"
#include "../src/tty/win.h"
#include "cmd.h"
#include "main.h"
#include "cons.saver.h"
#include "subshell.h"
@ -397,3 +398,15 @@ execute_with_vfs_arg (const char *command, const char *filename)
g_free (localcopy);
g_free (fn);
}
/* Save current stat of directories to avoid reloading the panels */
/* when no modifications have taken place */
void
save_cwds_stat (void)
{
if (fast_reload) {
mc_stat (current_panel->cwd, &(current_panel->dir_stat));
if (get_other_type () == view_listing)
mc_stat (other_panel->cwd, &(other_panel->dir_stat));
}
}

View File

@ -26,4 +26,6 @@ void suspend_cmd (void);
/* Execute command on a filename that can be on VFS */
void execute_with_vfs_arg (const char *command, const char *filename);
void save_cwds_stat (void);
#endif /* !MC_EXECUTE_H */

View File

@ -36,6 +36,7 @@
#include "../src/tty/tty.h"
#include "user.h"
#include "main.h"
#include "cmd.h"
#include "wtools.h"
#include "ext.h"
#include "execute.h"

View File

@ -386,37 +386,6 @@ update_one_panel_widget (WPanel *panel, int force_update,
g_free (my_current_file);
}
static void
update_one_panel (int which, int force_update, const char *current_file)
{
WPanel *panel;
if (get_display_type (which) != view_listing)
return;
panel = (WPanel *) get_panel_widget (which);
update_one_panel_widget (panel, force_update, current_file);
}
/* Save current stat of directories to avoid reloading the panels */
/* when no modifications have taken place */
void
save_cwds_stat (void)
{
if (fast_reload) {
mc_stat (current_panel->cwd, &(current_panel->dir_stat));
if (get_other_type () == view_listing)
mc_stat (other_panel->cwd, &(other_panel->dir_stat));
}
}
void
change_panel (void)
{
free_completions (cmdline);
dlg_one_down (midnight_dlg);
}
/* Stop MC main dialog and the current dialog if it exists.
* Needed to provide fast exit from MC viewer or editor on shell exit */
static void
@ -468,181 +437,6 @@ quiet_quit_cmd (void)
quit_cmd_internal (1);
}
/* Wrapper for do_subshell_chdir, check for availability of subshell */
void
subshell_chdir (const char *directory)
{
#ifdef HAVE_SUBSHELL_SUPPORT
if (mc_args__use_subshell) {
if (vfs_current_is_local ())
do_subshell_chdir (directory, 0, 1);
}
#endif /* HAVE_SUBSHELL_SUPPORT */
}
void
directory_history_add (struct WPanel *panel, const char *dir)
{
char *tmp;
tmp = g_strdup (dir);
strip_password (tmp, 1);
panel->dir_history = list_append_unique (panel->dir_history, tmp);
}
/*
* If we moved to the parent directory move the selection pointer to
* the old directory name; If we leave VFS dir, remove FS specificator.
*
* You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
*/
static const char *
get_parent_dir_name (const char *cwd, const char *lwd)
{
const char *p;
if (strlen (lwd) > strlen (cwd))
if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
((gsize)strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
cwd[1] == '\0'))) {
return (p + 1);
}
return NULL;
}
/*
* Changes the current directory of the panel.
* Don't record change in the directory history.
*/
static int
_do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
{
const char *directory;
char *olddir;
char temp[MC_MAXPATHLEN];
char *translated_url;
if (cd_type == cd_parse_command) {
while (*new_dir == ' ')
new_dir++;
}
olddir = g_strdup (panel->cwd);
new_dir = translated_url = vfs_translate_url (new_dir);
/* Convert *new_path to a suitable pathname, handle ~user */
if (cd_type == cd_parse_command) {
if (!strcmp (new_dir, "-")) {
strcpy (temp, panel->lwd);
new_dir = temp;
}
}
directory = *new_dir ? new_dir : home_dir;
if (mc_chdir (directory) == -1) {
strcpy (panel->cwd, olddir);
g_free (olddir);
g_free (translated_url);
return 0;
}
g_free (translated_url);
/* Success: save previous directory, shutdown status of previous dir */
strcpy (panel->lwd, olddir);
free_completions (cmdline);
mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
vfs_release_path (olddir);
subshell_chdir (panel->cwd);
/* Reload current panel */
panel_clean_dir (panel);
panel->count =
do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
panel->reverse, panel->case_sensitive,
panel->exec_first, panel->filter);
try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
load_hint (0);
panel->dirty = 1;
update_xterm_title_path ();
g_free (olddir);
return 1;
}
/*
* Changes the current directory of the panel.
* Record change in the directory history.
*/
int
do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
{
int r;
r = _do_panel_cd (panel, new_dir, cd_type);
if (r)
directory_history_add (panel, panel->cwd);
return r;
}
int
do_cd (const char *new_dir, enum cd_enum exact)
{
return (do_panel_cd (current_panel, new_dir, exact));
}
void
directory_history_next (WPanel *panel)
{
GList *nextdir;
nextdir = g_list_next (panel->dir_history);
if (!nextdir)
return;
if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
panel->dir_history = nextdir;
}
void
directory_history_prev (WPanel *panel)
{
GList *prevdir;
prevdir = g_list_previous (panel->dir_history);
if (!prevdir)
return;
if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
panel->dir_history = prevdir;
}
void
directory_history_list (WPanel *panel)
{
char *s;
if (!panel->dir_history)
return;
s = show_hist (panel->dir_history, &panel->widget);
if (!s)
return;
if (_do_panel_cd (panel, s, cd_exact))
directory_history_add (panel, panel->cwd);
else
message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
g_free (s);
}
#ifdef HAVE_SUBSHELL_SUPPORT
int
load_prompt (int fd, void *unused)
@ -1185,11 +979,6 @@ ctl_x_cmd (void)
ctl_x_map_enabled = 1;
}
static void
nothing (void)
{
}
static cb_ret_t
midnight_execute_cmd(int command)
{

View File

@ -21,8 +21,6 @@ extern volatile int quit;
/* If true, after executing a command, wait for a keystroke */
enum { pause_never, pause_on_dumb_terminals, pause_always };
void subshell_chdir (const char *command);
/* See main.c for details on these variables */
extern int mark_moves_down;
extern int auto_menu;
@ -85,15 +83,7 @@ extern GArray *input_keymap;
extern const global_key_map_t *panel_map;
extern const global_key_map_t *input_map;
enum cd_enum {
cd_parse_command,
cd_exact
};
int do_cd (const char *new_dir, enum cd_enum cd_type); /* For find.c */
void change_panel (void);
int load_prompt (int, void *);
void save_cwds_stat (void);
void quiet_quit_cmd (void); /* For cmd.c and command.c */
void touch_bar (void);
@ -120,9 +110,4 @@ char *remove_encoding_from_path (const char *);
#define MC_BASE "/.mc/"
struct WPanel;
void directory_history_add (struct WPanel *panel, const char *dir);
int do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type);
#endif

View File

@ -130,4 +130,6 @@ void directory_history_next (WPanel *panel);
void directory_history_prev (WPanel *panel);
void directory_history_list (WPanel *panel);
void change_panel (void);
#endif

View File

@ -55,6 +55,7 @@
#include "menu.h" /* menubar_visible */
#include "main-widgets.h"
#include "main.h"
#include "subshell.h"
#include "unixcompat.h"
#include "mountlist.h" /* my_statfs */
#include "selcodepage.h" /* select_charset () */
@ -2871,3 +2872,58 @@ update_panels (int force_update, const char *current_file)
mc_chdir (panel->cwd);
}
void
change_panel (void)
{
free_completions (cmdline);
dlg_one_down (midnight_dlg);
}
void
directory_history_next (WPanel *panel)
{
GList *nextdir;
nextdir = g_list_next (panel->dir_history);
if (!nextdir)
return;
if (cmd_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
panel->dir_history = nextdir;
}
void
directory_history_prev (WPanel *panel)
{
GList *prevdir;
prevdir = g_list_previous (panel->dir_history);
if (!prevdir)
return;
if (cmd_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
panel->dir_history = prevdir;
}
void
directory_history_list (WPanel *panel)
{
char *s;
if (!panel->dir_history)
return;
s = show_hist (panel->dir_history, &panel->widget);
if (!s)
return;
if (cmd_do_panel_cd (panel, s, cd_exact))
directory_history_add (panel, panel->cwd);
else
message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
g_free (s);
}

View File

@ -1233,5 +1233,21 @@ do_update_prompt (void)
update_prompt = 0;
}
}
/* Wrapper for do_subshell_chdir, check for availability of subshell */
void
subshell_chdir (const char *directory)
{
if (mc_args__use_subshell) {
if (vfs_current_is_local ())
do_subshell_chdir (directory, 0, 1);
}
}
#else
void
subshell_chdir (const char *directory)
{
(void) directory
}
#endif /* HAVE_SUBSHELL_SUPPORT */

View File

@ -31,6 +31,7 @@ int invoke_subshell (const char *command, int how, char **new_dir);
int read_subshell_prompt (void);
void resize_subshell (void);
int exit_subshell (void);
void subshell_chdir (const char *);
void do_subshell_chdir (const char *directory, int update_prompt, int reset_prompt);
void subshell_get_console_attributes (void);
void sigchld_handler (int sig);