From 71bedb4297d1a93faf094992b17121f24bbc0ee3 Mon Sep 17 00:00:00 2001 From: rupa Date: Tue, 24 Dec 2013 23:55:19 -0500 Subject: [PATCH] Put no resolve symlinks option back --- README | 5 +++++ z.1 | 7 +++++++ z.sh | 16 ++++++++++++---- z_prompt_command.c | 14 +++++++------- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/README b/README index 2b28371..b246e23 100644 --- a/README +++ b/README @@ -61,6 +61,7 @@ NOTES Optionally: Set $_Z_CMD to change the command name (default z). Set $_Z_DATA to change the datafile (default $HOME/.z). + Set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution. Set $_Z_NO_PROMPT_COMMAND to handle PROMPT_COMMAND/precmd your- self. Set $_Z_EXCLUDE_DIRS to an array of directories to exclude. @@ -111,6 +112,10 @@ ENVIRONMENT The environment variable $_Z_DATA can be used to control the datafile location. If it is not defined, the location defaults to $HOME/.z. + The environment variable $_Z_NO_RESOLVE_SYMLINKS can be set to prevent + resolving of symlinks. If it is not set, symbolic links will be + resolved when added to the datafile. + In bash, z appends a command to the PROMPT_COMMAND environment variable to maintain its database. In zsh, z appends a function _z_precmd to the precmd_functions array. diff --git a/z.1 b/z.1 index 0489c7a..26ebeeb 100644 --- a/z.1 +++ b/z.1 @@ -83,6 +83,9 @@ Set \fB$_Z_CMD\fR to change the command name (default \fBz\fR). Set \fB$_Z_DATA\fR to change the datafile (default \fB$HOME/.z\fR). .RE .RS +Set \fB$_Z_NO_RESOLVE_SYMLINKS\fR to prevent symlink resolution. +.RE +.RS Set \fB$_Z_NO_PROMPT_COMMAND\fR to handle \fBPROMPT_COMMAND/precmd\fR yourself. .RE .RS @@ -137,6 +140,10 @@ set, \fB$_Z_CMD\fR defaults to \fBz\fR. The environment variable \fB$_Z_DATA\fR can be used to control the datafile location. If it is not defined, the location defaults to \fB$HOME/.z\fR. .P +The environment variable \fB$_Z_NO_RESOLVE_SYMLINKS\fR can be set to prevent +resolving of symlinks. If it is not set, symbolic links will be resolved when +added to the datafile. +.P In bash, \fBz\fR appends a command to the \fBPROMPT_COMMAND\fR environment variable to maintain its database. In zsh, \fBz\fR appends a function \fB_z_precmd\fR to the \fBprecmd_functions\fR array. diff --git a/z.sh b/z.sh index 2230904..c400a95 100644 --- a/z.sh +++ b/z.sh @@ -154,13 +154,21 @@ _z() { alias ${_Z_CMD:-z}='_z 2>&1' +[ "$_Z_NO_RESOLVE_SYMLINKS" ] || _Z_RESOLVE_SYMLINKS="-P" + if compctl >/dev/null 2>&1; then # zsh [ "$_Z_NO_PROMPT_COMMAND" ] || { # populate directory list, avoid clobbering any other precmds. - _z_precmd() { - /usr/local/bin/_z_prompt_command - } + if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then + _z_precmd() { + /usr/local/bin/_z_prompt_command "${PWD:a}" + } + else + _z_precmd() { + /usr/local/bin/_z_prompt_command "${PWD:A}" + } + fi [[ -n "${precmd_functions[(r)_z_precmd]}" ]] || { precmd_functions[$(($#precmd_functions+1))]=_z_precmd } @@ -179,7 +187,7 @@ elif complete >/dev/null 2>&1; then [ "$_Z_NO_PROMPT_COMMAND" ] || { # populate directory list. avoid clobbering other PROMPT_COMMANDs. grep "_z_prompt_command" <<< "$PROMPT_COMMAND" >/dev/null || { - PROMPT_COMMAND="$PROMPT_COMMAND"$'\n'"/usr/local/bin/_z_prompt_command;" + PROMPT_COMMAND="$PROMPT_COMMAND"$'\n''/usr/local/bin/_z_prompt_command "$(pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)";' } } fi diff --git a/z_prompt_command.c b/z_prompt_command.c index de0b013..d61543d 100644 --- a/z_prompt_command.c +++ b/z_prompt_command.c @@ -7,24 +7,24 @@ #include #include -int main() { +int main(int argc, char **argv) { + + if (argv[1] == NULL) return 0; + char * _Z_DATA; char datafile[PATH_MAX]; char tmpfile[PATH_MAX]; FILE *read; FILE *write; char line[LINE_MAX]; - char cwd[PATH_MAX]; char pth[PATH_MAX]; float rank; int timestamp; time_t epoch = time(NULL); int found = 0; - if (getcwd(cwd, sizeof(cwd)) == NULL) return EXIT_FAILURE; - // Don't track $HOME - if (strcmp(cwd, getenv("HOME")) == 0) return EXIT_SUCCESS; + if (strcmp(argv[1], getenv("HOME")) == 0) return EXIT_SUCCESS; _Z_DATA = getenv("_Z_DATA"); if( _Z_DATA == NULL ) { @@ -42,12 +42,12 @@ int main() { while (fgets(line, sizeof line, read) != NULL) { sscanf(line, "%[^|]|%f|%d\n", pth, &rank, ×tamp); - if (strcmp(pth, cwd) == 0) { + if (strcmp(pth, argv[1]) == 0) { fprintf(write, "%s|%f|%d\n", pth, rank + 1, (int) epoch); found = 1; } else fprintf(write, "%s", line); } - if (!found) fprintf(write, "%s|1|%d\n", cwd, (int) epoch); + if (!found) fprintf(write, "%s|1|%d\n", argv[1], (int) epoch); fclose(read); fclose(write);