Applied patch by ft and updated changelog
[grml-etc-core.git] / etc / zsh / 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 ]] ; then
41       compstate[insert]=$action
42     elif [[ $WIDGET == *expand* ]] ; then
43       compstate[insert]=all
44     fi
45     if [[ $WIDGET == *expand* ]] ; then
46       compadd -U ${(M)kept:#${~words[CURRENT]}}
47     else
48       compadd -a kept
49     fi
50   }
51
52   # now bind it to keys and enable completition
53   zle -C insert-kept-result complete-word _generic
54   zle -C expand-kept-result complete-word _generic
55   zstyle ':completion:*-kept-result:*' completer _insert_kept
56   zstyle ':completion:insert-kept-result:*' menu yes select
57
58   bindkey '^Xk' insert-kept-result
59   bindkey '^XK' expand-kept-result    # shift-K to get expansion
60
61   # And the "_expand_word_and_keep" replacement for _expand_word:
62   _expand_word_and_keep() {
63     function compadd() {
64         local -A args
65         zparseopts -E -A args J:
66         if [[ $args[-J] == all-expansions ]] ; 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 #################################################################