zsh-users
 help / color / mirror / code / Atom feed
* Widget of the day: expand absolute path
@ 2014-02-28 12:56 Peter Stephenson
  2014-02-28 15:14 ` Ray Andrews
  2014-02-28 18:19 ` m0viefreak
  0 siblings, 2 replies; 5+ messages in thread
From: Peter Stephenson @ 2014-02-28 12:56 UTC (permalink / raw)
  To: Zsh Users' List

Sometimes I want to expand the name of a file on the command line to its
absolute path with all symbolic links removed, e.g. because I know the
symbolic link is volatile.  I can do that with glob qualifiers, but if
I'm likely to want to refer to the file in the ZLE history, or I want to
edit the path to refer to a related file in the resulting target path,
it's quite convenient to expand it immediately. This widget does that.

You can get some of this effect by adding glob qualifiers and using
expansion, but there's no glob qualifier to insert directory names,
which I wanted for neatness.  It doesn't really make sense to have
one, in fact, because the result of a glob is usually passed directly
to a programme which wouldn't be able to re-expand the name.

It also serves as a model for how to write a front-end to
modify-current-argument to do similar file-related tricks.  I should
probably try to enhance modify-current-argument in some way to do this
without needing an auxiliary function.


#START
# expand-absolute-path
# This is a ZLE widget to expand the absolute path to a file,
# using directory naming to shorten the path where possible.

emulate -L zsh
setopt extendedglob cbases

autoload -Uz modify-current-argument

if (( ! ${+functions[glob-expand-absolute-path]} )); then
  glob-expand-absolute-path() {
    local -a files
    files=(${~1}(:A))
    REPLY=${(D)files[1]}
  }
fi

modify-current-argument glob-expand-absolute-path
#END


pws


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Widget of the day: expand absolute path
  2014-02-28 12:56 Widget of the day: expand absolute path Peter Stephenson
@ 2014-02-28 15:14 ` Ray Andrews
  2014-02-28 15:25   ` Peter Stephenson
  2014-02-28 18:19 ` m0viefreak
  1 sibling, 1 reply; 5+ messages in thread
From: Ray Andrews @ 2014-02-28 15:14 UTC (permalink / raw)
  To: zsh-users

On 02/28/2014 04:56 AM, Peter Stephenson wrote:


Peter,

It's probably obvious to most, but not at my level.  How does one 'use' 
this?  Where does the code go? What activates it?
> Sometimes I want to expand the name of a file on the command line to its
> absolute path with all symbolic links removed, e.g. because I know the
> symbolic link is volatile.  I can do that with glob qualifiers, but if
> I'm likely to want to refer to the file in the ZLE history, or I want to
> edit the path to refer to a related file in the resulting target path,
> it's quite convenient to expand it immediately. This widget does that.
>
> You can get some of this effect by adding glob qualifiers and using
> expansion, but there's no glob qualifier to insert directory names,
> which I wanted for neatness.  It doesn't really make sense to have
> one, in fact, because the result of a glob is usually passed directly
> to a programme which wouldn't be able to re-expand the name.
>
> It also serves as a model for how to write a front-end to
> modify-current-argument to do similar file-related tricks.  I should
> probably try to enhance modify-current-argument in some way to do this
> without needing an auxiliary function.
>
>
> #START
> # expand-absolute-path
> # This is a ZLE widget to expand the absolute path to a file,
> # using directory naming to shorten the path where possible.
>
> emulate -L zsh
> setopt extendedglob cbases
>
> autoload -Uz modify-current-argument
>
> if (( ! ${+functions[glob-expand-absolute-path]} )); then
>    glob-expand-absolute-path() {
>      local -a files
>      files=(${~1}(:A))
>      REPLY=${(D)files[1]}
>    }
> fi
>
> modify-current-argument glob-expand-absolute-path
> #END
>
>
> pws
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Widget of the day: expand absolute path
  2014-02-28 15:14 ` Ray Andrews
@ 2014-02-28 15:25   ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2014-02-28 15:25 UTC (permalink / raw)
  To: zsh-users

On Fri, 28 Feb 2014 07:14:04 -0800
Ray Andrews <rayandrews@eastlink.ca> wrote:
> It's probably obvious to most, but not at my level.  How does one 'use' 
> this?  Where does the code go? What activates it?

The zle manual page fully documents this, but we're between a rock and a
hard place there: either it's heavy going or it's not fully documented.

For a quick summary, see the section ZLE FUNCTIONS in the zshcontrib
manual page, which is where contributed widgets are described (as this
would be if I added it to the distribution).

Chapter 14 of From Bash to Zsh is recommended (certainly by Oliver and
me!)  See www.bash2zsh.com.

Here's a useful function I use for doing all this in one go that assumes
expand-absolute-path is autoloaded from your $fpath.


zlewidget() {
  # bindkey KEY to new WIDGET, possibly implemented by optional FUNCTION.
  # FUNCTION defaults to WIDGET and will be marked for autoload -Uz.

  local key=$1
  local widget=$2
  local function=${3:-$2}

  autoload -Uz $function
  zle -N $widget $function
  bindkey $key $widget
}
zlewidget '^xx' expand-absolute-path


pws


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Widget of the day: expand absolute path
  2014-02-28 12:56 Widget of the day: expand absolute path Peter Stephenson
  2014-02-28 15:14 ` Ray Andrews
@ 2014-02-28 18:19 ` m0viefreak
  2014-02-28 18:27   ` Peter Stephenson
  1 sibling, 1 reply; 5+ messages in thread
From: m0viefreak @ 2014-02-28 18:19 UTC (permalink / raw)
  To: zsh-users


> Sometimes I want to expand the name of a file on the command line to its
> absolute path with all symbolic links removed, e.g. because I know the
> symbolic link is volatile.  I can do that with glob qualifiers, but if
> I'm likely to want to refer to the file in the ZLE history, or I want to
> edit the path to refer to a related file in the resulting target path,
> it's quite convenient to expand it immediately. This widget does that.

Thank you! This is very useful.

But there is a small problem when executing it on a word that does not
actually match any file. It will print an error message and clear out
the word completely.

I suggest the following change:

@@ -10,7 +10,8 @@ autoload -Uz modify-current-argument
 if (( ! ${+functions[glob-expand-absolute-path]} )); then
   glob-expand-absolute-path() {
     local -a files
-    files=(${~1}(:A))
+    files=(${~1}(N:A))
+    (( $#files )) || return
     REPLY=${(D)files[1]}
   }
 fi


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Widget of the day: expand absolute path
  2014-02-28 18:19 ` m0viefreak
@ 2014-02-28 18:27   ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2014-02-28 18:27 UTC (permalink / raw)
  To: zsh-users

On Fri, 28 Feb 2014 19:19:06 +0100
m0viefreak <m0viefreak.cm@googlemail.com> wrote:
> But there is a small problem when executing it on a word that does not
> actually match any file. It will print an error message and clear out
> the word completely.
> 
> I suggest the following change:

Excellent point.  I've updated my copy.  I should really make it
possible for modify-current-argument to pass up a failure.

pws


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-02-28 18:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-28 12:56 Widget of the day: expand absolute path Peter Stephenson
2014-02-28 15:14 ` Ray Andrews
2014-02-28 15:25   ` Peter Stephenson
2014-02-28 18:19 ` m0viefreak
2014-02-28 18:27   ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).