Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Resolving absolute path of named directory

On Tue, 22 Jan 2013 11:58:44 +0100
Jesper Nygårds <jesper.nygards@xxxxxxxxx> wrote:
> Thank you for your help, Thomas, but I believe my example obscured what I
> am trying to do, so I will try to clarify with a better example. Consider
> this zle widget that I believe I have picked up from this list:
> current-argument-absolute-path() {
>     modify-current-argument '$ARG:a'
> }
> zle -N current-argument-absolute-path
> bindkey '\e+' current-argument-absolute-path
> As its name implies, it takes the current word on the command line, and
> replaces it with its corresponding absolute path. However, it does not work
> for named directories. If I write ls ~<\e+> when standing in, say,
> /usr/lib, it expands the "~" to "/usr/lib/~", not "/home/jesper". And the
> same for named directories that I have defined myself. So my question is,
> can I rewrite this function so that if what's on the command line is a
> named directory, it is expanded into its correct absolute path? And I still
> want it to work for arguments that are not named directories, of course.

Well, you could do something like

current-argument-absolute-path() {
    modify-current-argument '${$(print -r -- ${~ARG}):a}'

but it seems cheating, and if ARG contains special characters for
globbing you're in trouble.  I couldn't offhand think of a direct way of
getting ~ expansion within a double-quoted expression, which is what's
happening internally.

Actually, the key chunk of modify-current-argument that does this,

local ARG="${reply[REPLY]}" repl
eval repl=\"$1\"

is a real hack that's crying out for something a bit more sophisticated.

Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/

Messages sorted by: Reverse Date, Date, Thread, Author