Skip to content

Commit

Permalink
ZSH USERS NOTE BACKWARD COMPATIBILITY WARNING!
Browse files Browse the repository at this point in the history
Better documentation of zsh changes in README
  • Loading branch information
rupa committed Aug 30, 2012
1 parent 138cf7a commit 3d896ea
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README
Expand Up @@ -3,7 +3,16 @@
* ZSH USERS BACKWARD COMPATIBILITY WARNING * * ZSH USERS BACKWARD COMPATIBILITY WARNING *
* * * *
* z now handles 'precmd' set up for zsh. Current users using zsh should * * z now handles 'precmd' set up for zsh. Current users using zsh should *
* remove the precmd function described in the Installation instructions. * * remove the precmd function that was described in the installation *
* instructions for previous versions. *
* *
* In short, this: *
* . /path/to/z.sh *
* function precmd () { *
* _z --add "$(pwd -P)" *
* } *
* should now just be: *
* . /path/to/z.sh *
* * * *
* ZSH USERS BACKWARD COMPATIBILITY WARNING * * ZSH USERS BACKWARD COMPATIBILITY WARNING *
* * * *
Expand Down

20 comments on commit 3d896ea

@sorin-ionescu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how I load z in Prezto.

Is it no longer necessary?

function _z-precmd {
  _z --add "${PWD:A}"
}

autoload -Uz add-zsh-hook
add-zsh-hook precmd _z-precmd

Also, you should use "${PWD:A}" instead of a sub shell.

@rupa
Copy link
Owner Author

@rupa rupa commented on 3d896ea Aug 30, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

z now appends a function _z_precmd to the precmd_functions array (as documented http://zsh.sourceforge.net/Doc/Release/Functions.html), so it'll be run as a precmd without clobbering existing stuff (the reason I felt I couldn't automate it before was worrying about clobbering existing precmds).

I would prefer to use the $PWD variable, but pwd allows whether symlinks are resolved to be configurable, and $PWD doesn't resolve symlinks.

So yeah you should either remove that stuff, or you can also set $_Z_NO_PROMPT_COMMAND if you want to continue handling precmd yourself.

@rupa
Copy link
Owner Author

@rupa rupa commented on 3d896ea Aug 30, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the :A do to $PWD?

@sorin-ionescu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, add-zsh-hook is a convenience function to add a bunch of hooks. In this case, it actually does precmd_functions+=(foo).

${PWD:A} resolves the full path, including symbolic links. Read the Modifiers section in man zshexpn, more specifically, a and A.

@rupa
Copy link
Owner Author

@rupa rupa commented on 3d896ea Aug 30, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awww, I wish bash had that. I'm going to leave things the way they are in z, but if you want that bit of extra oomph you can keep handling things manually as described above.

Thanks :)

@sorin-ionescu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BASH lacks many useful things. Zsh is bloated with quite a few useless things.

If I source the latest z, it's going to use the sub shell no matter what. I can't add my own hook since there will be two hooks.

@rupa
Copy link
Owner Author

@rupa rupa commented on 3d896ea Aug 30, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you export _Z_NO_PROMPT_COMMAND=1 before sourcing z, it won't do any of the precmd management, so you can manage the hook yourself.

@sorin-ionescu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, since I'm using the same function name as yours, it would just redefine the function.

Why does the alias z generates have 2>&1 in it?

@sorin-ionescu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After taking a second look, that precmd function seems to only affect Zsh, not both Zsh and BASH; so, you could change it to my own.

@rupa
Copy link
Owner Author

@rupa rupa commented on 3d896ea Aug 30, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yours has a dash (_z-precmd) where mine has an underscore (_z_precmd), but that seems plausible, as long as you redefine it after z is sourced. And you'd have to make sure that it doesn't get appended to precmd_functions twice.

If i remember correctly, the stderr redirect is because, when doing z -l, I did a 'clever hack' and print to stderr, so that stdout remains empty, and z doesn't attempt to cd. Someone complained that it made it hard for them to pipe the output of z -l, so I redirect stderr back to stdout after the fact and everyone was happy.

@rupa
Copy link
Owner Author

@rupa rupa commented on 3d896ea Aug 30, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's definitely zsh only and I'm tempted to change it, but I want it to respect $_Z_NO_RESOLVE_SYMLINKS, and I cant think of anything more elegant than:


if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then
  _z_precmd() {
    _z --add "${PWD}"
  }
else
  _z_precmd() {
    _z --add "${PWD:A}"
  }
fi

@sorin-ionescu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is $PWD ever relative? You may want lowercase a, as in "${PWD:a}" to still record full paths, just not resolve symbolic links.

It may not be elegant, but there is no reason to complicate it by inserting the if inside of the function.

@rupa
Copy link
Owner Author

@rupa rupa commented on 3d896ea Aug 30, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right about 'a', I'm going to go ahead and commit the thing, despite it being kind of clunky.

@sorin-ionescu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zsh has a mathematical ternary operator.

$ print $((1?1:0))          
1
$ print $((0?1:0))
0

You could have something like bellow, which is uglier than the if statement.

_z_precmd() {
  _z --add "${PWD:$((_Z_NO_RESOLVE_SYMLINKS?a:A}"
}

@rupa
Copy link
Owner Author

@rupa rupa commented on 3d896ea Aug 31, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! In this codebase, but not in general, I prefer ugly to verbose :), so I'll probably use that in preference to the if statement. I was messing around with variable/parameter substitutions to no avail, but that's gonna work! I'll probably commit that (with the typo fixed, after some testing) sometime later, but in any case, it won't effect any changes you need to make in your project.

Thanks!

@rupa
Copy link
Owner Author

@rupa rupa commented on 3d896ea Aug 31, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On review it looks like this doesn't work with strings:


~$ print $((0?a:A))
0
~$ print $((1?a:A)) 
0

@sorin-ionescu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it only works for math. Now, I remember why I don't use it.

There is even an uglier way.

_z_precmd() {
  setopt LOCAL_OPTIONS PROMPT_PERCENT
  _z --add "${PWD:${(%):-%($_Z_NO_RESOLVE_SYMLINKS:a:A)}}"
}

@sorin-ionescu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably should not use that even if it works. It's ugly beyond readability. Things can break. Using prompt expansion outside of prompts is complicated and finicky.

@rupa
Copy link
Owner Author

@rupa rupa commented on 3d896ea Aug 31, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't quite work but I'm impressed at something that arguably surpasses perl at being unreadable. Super fun!:


rupa@lucy/etc% zsh
rupa@lucy/etc% echo ${PWD:${(%):-%($_Z_NO_RESOLVE_SYMLINKS:a:A)}}
/etc
rupa@lucy/etc% echo ${PWD:${(%):-%($SHELL:a:A)}}                 
zsh: bad math expression: operand expected at `'
rupa@lucy/etc% _Z_NO_RESOLVE_SYMLINKS=1 echo ${PWD:${(%):-%($_Z_NO_RESOLVE_SYMLINKS:a:A)}}
/etc
rupa@lucy/etc% export _Z_NO_RESOLVE_SYMLINKS=1
rupa@lucy/etc% echo ${PWD:${(%):-%($_Z_NO_RESOLVE_SYMLINKS:a:A)}}
zsh: division by zero
rupa@lucy/etc% echo {PWD:${(%):-%($_Z_NO_RESOLVE_SYMLINKS:a:A)}} 
{PWD:rivate/etc/1)}
rupa@lucy/etc% export _Z_NO_RESOLVE_SYMLINKS=
rupa@lucy/etc% echo {PWD:${(%):-%($_Z_NO_RESOLVE_SYMLINKS:a:A)}}
{PWD:}
rupa@lucy/etc% 

I'm not sure what's going on, but I'm enjoying it! And it's close ...

I have a feeling if I read the man stuff you linked I could get it, and despite that I'm a bash person, I just might.

(I'm in /etc cause it's actually a symlink to /private/etc, but I'm sure you knew that :)

@sorin-ionescu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably should employ quotations around some stuff.

Please sign in to comment.