zsh-users
 help / color / mirror / code / Atom feed
* Expanding global aliases on key press
@ 2009-02-24 23:36 Thorsten Kampe
  2009-02-25  1:41 ` Bart Schaefer
  2009-02-25  1:53 ` Mikael Magnusson
  0 siblings, 2 replies; 6+ messages in thread
From: Thorsten Kampe @ 2009-02-24 23:36 UTC (permalink / raw)
  To: zsh-users

Hi,

I've read the new German Zsh book that was mentioned here some time ago 
(by the way: a huuuuge disappointment) and I've found this particular 
widget to expand global aliases on pressing blank:

#
global-alias-space()
    { local ga="$LBUFFER[(w)-1]"
      [[ -n $ga ]] && LBUFFER[(w)-1]="${${galiases[$ga]}:-$ga}"
      zle self-insert;}

zle -N global-alias-space
bindkey ' ' global-alias-space
#

Can anyone comment on the code, please?

Additionally I've adapted this widget to be bound to the Enter key:

#
accept-line()
    { local ga="$LBUFFER[(w)-1]"
      [[ -n $ga ]] && LBUFFER[(w)-1]="${${galiases[$ga]}:-$ga}"
      zle .accept-line;}

zle -N accept-line
#

I'd also like to hear your comments on that...


Thorsten


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

* Re: Expanding global aliases on key press
  2009-02-24 23:36 Expanding global aliases on key press Thorsten Kampe
@ 2009-02-25  1:41 ` Bart Schaefer
  2009-02-25 17:21   ` Thorsten Kampe
  2009-02-25  1:53 ` Mikael Magnusson
  1 sibling, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2009-02-25  1:41 UTC (permalink / raw)
  To: zsh-users

On Feb 25, 12:36am, Thorsten Kampe wrote:
} #
} global-alias-space()
}     { local ga="$LBUFFER[(w)-1]"
}       [[ -n $ga ]] && LBUFFER[(w)-1]="${${galiases[$ga]}:-$ga}"
}       zle self-insert;}
} 
} zle -N global-alias-space
} bindkey ' ' global-alias-space
} #
} 
} Can anyone comment on the code, please?

It wouldn't have occurred to me to use (w) on the left side of an
assignment like that, so that's worth some points for creativity.
The parsing rules for (w) are not the same as for global aliases,
though, so take away some for accuracy.  ${${(z)LBUFFER}[-1]} is
more correct but requires a more complicated assignment to replace
the word with its alias.

I'd probably do this:

    autoload -Uz match-words-by-style
    global-alias-space() {
      emulate -LR zsh
      match-words-by-style -w shell
      local ga=$matched_words[2]
      if [[ -n $ga ]]; then
	matched_words[2]="${${galiases[$ga]}:-$ga}"
	LBUFFER="${(j::)matched_words[1,3]}"
      fi
      zle .self-insert
    }

To observe what I suspect is a bug, try using execute-named-command
to invoke either version of global-alias-space and note that extra
whitespace ends up in the tail of LBUFFER.
 
} Additionally I've adapted this widget to be bound to the Enter key:
} 
} #
} accept-line()
}     { local ga="$LBUFFER[(w)-1]"
}       [[ -n $ga ]] && LBUFFER[(w)-1]="${${galiases[$ga]}:-$ga}"
}       zle .accept-line;}
} 
} zle -N accept-line
} #
} 
} I'd also like to hear your comments on that...

I was going to ask "why?" since you can't do anything with the expanded
alias after accepting the line, whereas with global-alias-space you
can at least edit it.  However ...

Presumably this (along with the other) is intended to store the line
into the history with global aliases expanded.  Perhaps this would be
more interesting:

    # Skip adding the raw line to the history
    zshaddhistory() { return 1 }
    # Now add the fully expanded line instead
    preexec() { print -sr -- "$3" }

Of course that expands normal aliases as well as global ones.


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

* Re: Expanding global aliases on key press
  2009-02-24 23:36 Expanding global aliases on key press Thorsten Kampe
  2009-02-25  1:41 ` Bart Schaefer
@ 2009-02-25  1:53 ` Mikael Magnusson
  2009-02-25  4:03   ` Bart Schaefer
  1 sibling, 1 reply; 6+ messages in thread
From: Mikael Magnusson @ 2009-02-25  1:53 UTC (permalink / raw)
  To: zsh-users

2009/2/25 Thorsten Kampe <thorsten@thorstenkampe.de>:
> Hi,
>
> I've read the new German Zsh book that was mentioned here some time ago
> (by the way: a huuuuge disappointment) and I've found this particular
> widget to expand global aliases on pressing blank:
>
> #
> global-alias-space()
>    { local ga="$LBUFFER[(w)-1]"
>      [[ -n $ga ]] && LBUFFER[(w)-1]="${${galiases[$ga]}:-$ga}"
>      zle self-insert;}
>
> zle -N global-alias-space
> bindkey ' ' global-alias-space
> #
>
> Can anyone comment on the code, please?
>
> Additionally I've adapted this widget to be bound to the Enter key:
>
> #
> accept-line()
>    { local ga="$LBUFFER[(w)-1]"
>      [[ -n $ga ]] && LBUFFER[(w)-1]="${${galiases[$ga]}:-$ga}"
>      zle .accept-line;}
>
> zle -N accept-line
> #
>
> I'd also like to hear your comments on that...

This seems much simpler:
global-alias-space () { zle _expand_alias; zle .self-insert }
zle -N global-alias-space
bindkey ' ' global-alias-space
zstyle :completion:\* regular false    # I can't figure out how to
specify this more exactly

-- 
Mikael Magnusson


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

* Re: Expanding global aliases on key press
  2009-02-25  1:53 ` Mikael Magnusson
@ 2009-02-25  4:03   ` Bart Schaefer
  2009-02-25  4:29     ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2009-02-25  4:03 UTC (permalink / raw)
  To: zsh-users

On Feb 25,  2:53am, Mikael Magnusson wrote:
}
} This seems much simpler:
} global-alias-space () { zle _expand_alias; zle .self-insert }
} zle -N global-alias-space
} bindkey ' ' global-alias-space
} zstyle :completion:\* regular false    # I can't figure out how to specify this more exactly

Normally the context would be :completion:expand-alias-word:::: but
because you're invoking a completion widget from inside an ordinary
widget the usual context setup is lost and you get just :completion::
instead.

Better might be

    bindkey -s " " '^Xa^V '

to invoke _expand_alias via its intended completion widget binding.


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

* Re: Expanding global aliases on key press
  2009-02-25  4:03   ` Bart Schaefer
@ 2009-02-25  4:29     ` Bart Schaefer
  0 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2009-02-25  4:29 UTC (permalink / raw)
  To: zsh-users

On Feb 24,  8:03pm, Bart Schaefer wrote:
}
} Normally the context would be :completion:expand-alias-word:::: but
} because you're invoking a completion widget from inside an ordinary
} widget the usual context setup is lost and you get just :completion::
} instead.

Just to clarify ... this is something odd about _expand_alias, not
about completion widgets in general.  _expand_alias is for reasons
I've forgotten examining $funcstack to decide how (whether) to set
up the context, and when called in a nested fashion that way, the
$funcstack is always non-empty.  This has something to do with it
being both a completer function (as in, it can be named in your
completers zstyle) and a widget function; there might be a better way
to determine which way it's being invoked.


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

* Re: Expanding global aliases on key press
  2009-02-25  1:41 ` Bart Schaefer
@ 2009-02-25 17:21   ` Thorsten Kampe
  0 siblings, 0 replies; 6+ messages in thread
From: Thorsten Kampe @ 2009-02-25 17:21 UTC (permalink / raw)
  To: zsh-users

* Bart Schaefer (Tue, 24 Feb 2009 17:41:47 -0800)
> } Additionally I've adapted this widget to be bound to the Enter key:
> } 
> } #
> } accept-line()
> }     { local ga="$LBUFFER[(w)-1]"
> }       [[ -n $ga ]] && LBUFFER[(w)-1]="${${galiases[$ga]}:-$ga}"
> }       zle .accept-line;}
> } 
> } zle -N accept-line
> } #
> } 
> } I'd also like to hear your comments on that...
> 
> I was going to ask "why?" since you can't do anything with the expanded
> alias after accepting the line, whereas with global-alias-space you
> can at least edit it.

It just looks nicer in the terminal window - not just in history but 
also when someone looks over your shoulder or you demonstrate stuff. 
Global aliases are hugely useful and I always use them when I switch to 
bash or Take Command - and then realize that they - of course - don't 
work there.

Compare this:

    h -dE 0 G hex L

    h -dE 0 | grep hex | less

The former is easier to type but the latter is easier to read.

Thorsten


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

end of thread, other threads:[~2009-02-25 17:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-02-24 23:36 Expanding global aliases on key press Thorsten Kampe
2009-02-25  1:41 ` Bart Schaefer
2009-02-25 17:21   ` Thorsten Kampe
2009-02-25  1:53 ` Mikael Magnusson
2009-02-25  4:03   ` Bart Schaefer
2009-02-25  4:29     ` Bart Schaefer

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).