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

Re: Expanding global aliases on key press



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.



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