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

Re: Tip of the day: previous command output



On Fri, 20 Aug 2004, Andy Spiegl wrote:

> What would be the difference if I dropped alias with the noglob and used 
> $* instead of $~*?  I guess zsh would expand the globs before calling 
> the function, right?

Right.

>  But the result should be the same or not?

Should be the same.  Waiting to expand uses a little less memory.


On Fri, 20 Aug 2004, Vincent Lefevre wrote:

> On 2004-08-20 11:30:44 +0200, Andy Spiegl wrote:
> > Hm, wouldn't it be a nice feature in general to have zsh always remember
> > the output of the last command, i.e. an automagic "keep"?
> 
> This would be nice, but I assume that there would be similar problems
> to stderr coloring (if you remember the thread...).

Exactly right, Vincent.


On Fri, 20 Aug 2004, Andy Spiegl wrote:

> Now that I've got the list of lines (typically filenames) in $kept
> how do I get them into the command line _quoted_.

The best way is probably to use a completion widget.

  insert-kept-result () {
    compstate[insert]=all
    compadd -a kept
  }
  zle -C insert-kept-result complete-word insert-kept-result

> Actually, ideally I'd like only filenames with spaces and special chars 
> to be quoted to avoid cluttering up the command line, but I assume 
> that's even tougher to do. :-(

Not with completion, it isn't.

As a bonus, if you type part of a word and then invoke insert-kept-result
it'll only insert the subset of $kept that matches the partial word.


On Fri, 20 Aug 2004, Vincent Lefevre wrote:

> On 2004-08-20 14:12:02 +0200, Andy Spiegl wrote:
> > Now that I've got the list of lines (typically filenames) in $kept
> > how do I get them into the command line _quoted_.
> 
> And would it be possible to get a menu, in order to select one or
> several filenames manually?

For that we get a little more intense:

  _insert_kept() {
    (( $#kept )) || return 1
    local action
    zstyle -s :completion:$curcontext insert-kept action
    if [[ -n $action ]]
    then compstate[insert]=$action
    fi
    compadd -a kept
  }
  zle -C insert-kept-result complete-word _generic
  zstyle ':completion:insert-kept-result:*' completer _insert_kept

Now you get Andy's thing with

  zstyle ':completion:*' insert-kept all

Or you can get menu completion with

  zstyle ':completion:*' insert-kept menu

and it does menu completion according to whatever your other usual styles
are (or you can add other specific styles for this context).

Just remember that TAB is still going to execute _main_complete directly,
so if you start whacking TAB to cycle through the menu you won't get very
far -- the normal completion context will take over and discard the list
taken from $kept.  You have to use the same keybinding to cycle as you did
to get into the menu to begin with.

Note also that if you simply stick _insert_kept into your normal completer
style, the TAB key will insert values from $kept when it has a value and
go on to use other completions when $kept is empty or unset.

> And how about typing a pattern somewhere in the command line, then
> applying a function that replaces this pattern by the matching lines
> (quoted) of $kept?

You'll have to be more specific.  You mean something like expand-word,
except that the expansion gets stashed in $kept first?

I won't try to test this, but something like this should do it:

  _expand_word_and_keep() {
    function compadd() {
      local -a args
      zparseopts -E -a args A: O:
      if (( ! $#args ))
      then builtin compadd -A kept "$@"
      fi
      builtin compadd "$@"
    }
    local result
    kept=()
    _main_complete _expand
    result=$?
    unfunction compadd
    return $result
  }

See the #compdef line at the top of the _expand_word function file in the 
distribution for how you'd plug that onto a key.

Hmm, it occurs to me that I'm not sure what happens when you combine the
-A and -C options of compadd ...



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