Fix a bunch of typos
[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 ################################################################################
7
8 # save output in a variable for later use
9 # Written by Bart Schaefer, for more details see:
10 # http://www.zsh.org/cgi-bin/mla/wilma_hiliter/users/2004/msg00894.html ff.
11 function keep {
12     setopt localoptions nomarkdirs nonomatch nocshnullglob nullglob
13     kept=()         # Erase old value in case of error on next line
14     kept=($~*)
15
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
23     print -Rc - ${^kept%/}(T)
24 }
25 # use it via:
26 # locate -i backup | grep -i thursday | keep
27 # echo $kept
28 #
29 # or:
30 #
31 # patch < mypatch.diff
32 # keep **/*.(orig|rej)
33 # vim ${${kept:#*.orig}:r}
34 # rm $kept
35 alias keep='noglob keep'
36
37 _insert_kept() {
38     (( $#kept )) || return 1
39     local action
40     zstyle -s :completion:$curcontext insert-kept action
41
42     if [[ -n $action ]] ; then
43         compstate[insert]=$action
44     elif [[ $WIDGET == *expand* ]] ; then
45         compstate[insert]=all
46     fi
47     if [[ $WIDGET == *expand* ]] ; then
48         compadd -U ${(M)kept:#${~words[CURRENT]}}
49     else
50         compadd -a kept
51     fi
52 }
53
54 # now bind it to keys and enable completion
55 zle -C insert-kept-result complete-word _generic
56 zle -C expand-kept-result complete-word _generic
57 zstyle ':completion:*-kept-result:*' completer _insert_kept
58 zstyle ':completion:insert-kept-result:*' menu yes select
59
60 bindkey '^Xk' insert-kept-result
61 bindkey '^XK' expand-kept-result    # shift-K to get expansion
62
63 # And the "_expand_word_and_keep" replacement for _expand_word:
64 _expand_word_and_keep() {
65     function compadd() {
66         local -A args
67         zparseopts -E -A args J:
68         if [[ $args[-J] == all-expansions ]] ; then
69             builtin compadd -A kept "$@"
70             kept=( ${(Q)${(z)kept}} )
71         fi
72         builtin compadd "$@"
73     }
74     # for older versions of zsh:
75     local result
76     _main_complete _expand
77     result=$?
78     unfunction compadd
79     return result
80     # versions >=4.2.1 understand this:
81     # { _main_complete _expand } always { unfunction compadd }
82 }
83
84 # This line must come after "compinit" in startup:
85 zle -C _expand_word complete-word _expand_word_and_keep
86
87 # No bindkey needed, it's already ^Xe from _expand_word
88 zstyle ':completion:*' insert-kept menu
89 zmodload -i zsh/complist
90
91 ## END OF FILE #################################################################
92 # vim:filetype=zsh foldmethod=marker autoindent expandtab shiftwidth=4