initial checkin
[zsh-lovers.git] / zsh_people / grml / keephack
1 # Filename:      /etc/zsh/keephack
2 # Purpose:       this file belongs to the zsh setup (see /etc/zsh/zshrc)
3 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
4 # Bug-Reports:   see http://grml.org/bugs/
5 # License:       This file is licensed under the GPL v2.
6 # Latest change: Don Jän 27 23:38:57 CET 2005 [mika]
7 ################################################################################
8
9 # save output in a variable for later use
10 # Written by Bart Schaefer, for more details see:
11 # http://www.zsh.org/cgi-bin/mla/wilma_hiliter/users/2004/msg00894.html ff.
12   function keep {
13     setopt localoptions nomarkdirs nonomatch nocshnullglob nullglob
14     kept=()         # Erase old value in case of error on next line
15     kept=($~*)
16     if [[ ! -t 0 ]]; then
17         local line
18         while read line; do
19             kept+=( $line )         # += is a zsh 4.2+ feature
20         done
21     fi
22     print -Rc - ${^kept%/}(T)
23   }
24   # use it via:
25   # locate -i backup | grep -i thursday | keep
26   # echo $kept
27   #
28   # or:
29   #
30   # patch < mypatch.diff
31   # keep **/*.(orig|rej)
32   # vim ${${kept:#*.orig}:r}
33   # rm $kept
34   alias keep='noglob keep'
35
36   _insert_kept() {
37     (( $#kept )) || return 1
38     local action
39     zstyle -s :completion:$curcontext insert-kept action
40     if [[ -n $action ]]
41     then compstate[insert]=$action
42     elif [[ $WIDGET = *expand* ]]
43     then compstate[insert]=all
44     fi
45     if [[ $WIDGET = *expand* ]]
46     then compadd -U ${(M)kept:#${~words[CURRENT]}}
47     else compadd -a kept
48     fi
49   }
50
51   # now bind it to keys and enable completition
52   zle -C insert-kept-result complete-word _generic
53   zle -C expand-kept-result complete-word _generic
54   zstyle ':completion:*-kept-result:*' completer _insert_kept
55   zstyle ':completion:insert-kept-result:*' menu yes select
56
57   bindkey '^Xk' insert-kept-result
58   bindkey '^XK' expand-kept-result    # shift-K to get expansion
59
60   # And the "_expand_word_and_keep" replacement for _expand_word:
61   _expand_word_and_keep() {
62     function compadd() {
63         local -A args
64         zparseopts -E -A args J:
65         if [[ $args[-J] == all-expansions ]]
66         then
67             builtin compadd -A kept "$@"
68             kept=( ${(Q)${(z)kept}} )
69         fi
70         builtin compadd "$@"
71     }
72     # for older versions of zsh:
73      local result
74      _main_complete _expand
75      result=$?
76      unfunction compadd
77      return result
78     # versions >=4.2.1 understand this:
79     # { _main_complete _expand } always { unfunction compadd }
80   }
81
82   # This line must come after "compinit" in startup:
83   zle -C _expand_word complete-word _expand_word_and_keep
84   # No bindkey needed, it's already ^Xe from _expand_word
85   zstyle ':completion:*' insert-kept menu
86   zmodload -i zsh/complist
87
88 ## END OF FILE #################################################################