Introduce panelize_absolutize_if_needed(), plus documentation.

Instead of imperfectly duplicating code we create a function.

Signed-off-by: Mooffie <mooffie@gmail.com>
This commit is contained in:
Mooffie 2017-03-03 12:53:38 +02:00
parent bb45f84071
commit 92eea68251
3 changed files with 41 additions and 23 deletions

View File

@ -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);
}

View File

@ -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)
{

View File

@ -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 */