From 92eea682511e01c73d7befa012b2965635d5c882 Mon Sep 17 00:00:00 2001 From: Mooffie Date: Fri, 3 Mar 2017 12:53:38 +0200 Subject: [PATCH] Introduce panelize_absolutize_if_needed(), plus documentation. Instead of imperfectly duplicating code we create a function. Signed-off-by: Mooffie --- src/filemanager/find.c | 11 +------- src/filemanager/panelize.c | 52 ++++++++++++++++++++++++++++---------- src/filemanager/panelize.h | 1 + 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/filemanager/find.c b/src/filemanager/find.c index f7f33ce6b..01b0a4bf7 100644 --- a/src/filemanager/find.c +++ b/src/filemanager/find.c @@ -1820,16 +1820,7 @@ do_find (const char *start_dir, ssize_t start_dir_len, const char *ignore_dirs, } current_panel->is_panelized = TRUE; - - /* absolute path */ - if (start_dir_len < 0) - { - int ret; - vfs_path_free (current_panel->cwd_vpath); - current_panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR); - ret = chdir (PATH_SEP_STR); - (void) ret; - } + panelize_absolutize_if_needed (current_panel); panelize_save_panel (current_panel); } diff --git a/src/filemanager/panelize.c b/src/filemanager/panelize.c index 336825665..6d9fe3ecb 100644 --- a/src/filemanager/panelize.c +++ b/src/filemanager/panelize.c @@ -355,19 +355,7 @@ do_external_panelize (char *command) } current_panel->is_panelized = TRUE; - - if (list->len > 1 && IS_PATH_SEP (list->list[1].fname[0])) - { - vfs_path_t *vpath_root; - int ret; - - vpath_root = vfs_path_from_str (PATH_SEP_STR); - panel_set_cwd (current_panel, vpath_root); - ret = mc_chdir (vpath_root); - vfs_path_free (vpath_root); - - (void) ret; - } + panelize_absolutize_if_needed (current_panel); if (pclose (external) < 0) message (D_NORMAL, _("External panelize"), _("Pipe close failed")); @@ -484,6 +472,44 @@ panelize_save_panel (WPanel * panel) /* --------------------------------------------------------------------------------------------- */ +/** + * Conditionally switches a panel's directory to "/" (root). + * + * If a panelized panel's listing contain absolute paths, this function + * sets the panel's directory to "/". Otherwise it does nothing. + * + * Rationale: + * + * This makes tokenized strings like "%d/%p" work. This also makes other + * places work where such naive concatenation is done in code (e.g., when + * pressing ctrl+shift+enter, for CK_PutCurrentFullSelected). + * + * When to call: + * + * You should always call this function after you populate the listing + * of a panelized panel. + */ +void +panelize_absolutize_if_needed (WPanel * panel) +{ + const dir_list *const list = &panel->dir; + + /* Note: We don't support mixing of absolute and relative paths, which is + * why it's ok for us to check only the 1st entry. */ + if (list->len > 1 && g_path_is_absolute (list->list[1].fname)) + { + vfs_path_t *root; + + root = vfs_path_from_str (PATH_SEP_STR); + panel_set_cwd (panel, root); + if (panel == current_panel) + mc_chdir (root); + vfs_path_free (root); + } +} + +/* --------------------------------------------------------------------------------------------- */ + void cd_panelize_cmd (void) { diff --git a/src/filemanager/panelize.h b/src/filemanager/panelize.h index 4bb844058..f230b2956 100644 --- a/src/filemanager/panelize.h +++ b/src/filemanager/panelize.h @@ -22,6 +22,7 @@ void done_panelize (void); void cd_panelize_cmd (void); void panelize_save_panel (WPanel * panel); void panelize_change_root (const vfs_path_t * new_root); +void panelize_absolutize_if_needed (WPanel * panel); /*** inline functions ****************************************************************************/ #endif /* MC__PANELIZE_H */