Skip to content

Commit

Permalink
Set OLDPWD, CLIFM_PID, and CLIFM_VERSION env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-arch committed Mar 17, 2023
1 parent e1ce21b commit 8f28aaa
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,12 @@ set_env(void)
setenv("CLIFM", config_dir ? config_dir : "1", 1);
setenv("CLIFM_PROFILE", alt_profile ? alt_profile : "default", 1);

char t[NAME_MAX];
snprintf(t, sizeof(t), "%d", (int)own_pid);
setenv("CLIFM_PID", t, 1);
snprintf(t, sizeof(t), "%s", VERSION);
setenv("CLIFM_VERSION", t, 1);

if (sel_file)
setenv("CLIFM_SELFILE", sel_file, 1);

Expand Down
9 changes: 9 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,15 @@ set_start_path(void)
workspaces[cur_ws].path = savestring(cwd, strlen(cwd));
}

/* Set OLDPWD */
char *pwd = getenv("PWD");
if (pwd && workspaces[cur_ws].path && strcmp(workspaces[cur_ws].path, pwd) != 0) {
#if _DEBUG_OLDPWD
_err(ERR_NO_LOG, PRINT_PROMPT, "OLDPWD: %s\n", pwd);
#endif
setenv("OLDPWD", pwd, 1);
}

dir_changed = 1;
return EXIT_SUCCESS;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ main(int argc, char *argv[])
init_config();
check_options();
set_sel_file();
set_env();
// set_env();
create_tmp_files();
#ifndef _NO_FZF
set_finder_paths();
Expand Down Expand Up @@ -1128,6 +1128,7 @@ main(int argc, char *argv[])

get_hostname();
init_shell();
set_env();

if (config_ok == 1)
init_history();
Expand Down
33 changes: 29 additions & 4 deletions src/navigation.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@

#define BD_CONTINUE 2

/* Set OLDPWD environment variable, provided we are not changing to the
* same directory (OLD != NEW) */
static void
set_oldpwd(const char *old, const char *new)
{
if (!old || !new || strcmp(old, new) == 0)
return;
#if _DEBUG_OLDPWD
_err(ERR_NO_LOG, PRINT_PROMPT, "OLDPWD: %s\n", old);
#endif
setenv("OLDPWD", old, 1);
}

static size_t
get_longest_workspace_name(void)
{
Expand Down Expand Up @@ -93,7 +106,7 @@ get_workspace_path_color(const uint8_t num)
return df_c;

if (!workspaces[num].path)
return DEF_DL_C; /* Unset. DL (diviling line) defaults to gray: let's use this */
return DEF_DL_C; /* Unset. DL (dividing line) defaults to gray: let's use this */

struct stat a;
if (lstat(workspaces[num].path, &a) == -1)
Expand Down Expand Up @@ -267,7 +280,8 @@ switch_workspace(int tmp_ws)
}

if (xchdir(workspaces[tmp_ws].path, SET_TITLE) == -1) {
_err(ERR_NO_STORE, NOPRINT_PROMPT, "ws: %s: %s\n", workspaces[tmp_ws].path, strerror(errno));
_err(ERR_NO_STORE, NOPRINT_PROMPT, "ws: %s: %s\n",
workspaces[tmp_ws].path, strerror(errno));
return EXIT_FAILURE;
}

Expand All @@ -284,6 +298,8 @@ switch_workspace(int tmp_ws)
if (conf.private_ws_settings == 1)
set_workspace_opts(cur_ws);

set_oldpwd(old_pwd[dirhist_cur_index], workspaces[cur_ws].path);

if (conf.autols == 1)
reload_dirlist();

Expand Down Expand Up @@ -712,11 +728,14 @@ go_home(const int cd_flag)
}

if (xchdir(user.home, SET_TITLE) != EXIT_SUCCESS) {
int err = errno;
if (cd_flag == CD_PRINT_ERROR)
_err(ERR_NO_STORE, NOPRINT_PROMPT, "cd: %s: %s\n", user.home, strerror(errno));
return errno;
_err(ERR_NO_STORE, NOPRINT_PROMPT, "cd: %s: %s\n", user.home, strerror(err));
return err;
}

set_oldpwd(old_pwd[dirhist_cur_index], user.home);

free(workspaces[cur_ws].path);
workspaces[cur_ws].path = savestring(user.home, strlen(user.home));

Expand Down Expand Up @@ -770,6 +789,8 @@ change_to_path(char *new_path, const int cd_flag)
return err;
}

set_oldpwd(workspaces[cur_ws].path, q);

free(workspaces[cur_ws].path);
workspaces[cur_ws].path = savestring(q, strlen(q));
free(q);
Expand Down Expand Up @@ -939,6 +960,8 @@ change_to_dirhist_num(int n)
return EXIT_FAILURE;
}

set_oldpwd(old_pwd[dirhist_cur_index], old_pwd[n]);

free(workspaces[cur_ws].path);
workspaces[cur_ws].path = savestring(old_pwd[n], strlen(old_pwd[n]));

Expand Down Expand Up @@ -977,6 +1000,8 @@ surf_hist(char **args)
static int
set_path(const char *new_path)
{
set_oldpwd(workspaces[cur_ws].path, new_path);

free(workspaces[cur_ws].path);
workspaces[cur_ws].path = savestring(new_path, strlen(new_path));
if (!workspaces[cur_ws].path)
Expand Down

0 comments on commit 8f28aaa

Please sign in to comment.