zsh: Make sure xsource works in any case; add modelines for vim
[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 Dez 06 23:21:30 CET 2007 [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
17     if [[ ! -t 0 ]] ; then
18         local line
19         while read line; do
20             kept+=( $line )         # += is a zsh 4.2+ feature
21         done
22     fi
23
24     print -Rc - ${^kept%/}(T)
25 }
26 # use it via:
27 # locate -i backup | grep -i thursday | keep
28 # echo $kept
29 #
30 # or:
31 #
32 # patch < mypatch.diff
33 # keep **/*.(orig|rej)
34 # vim ${${kept:#*.orig}:r}
35 # rm $kept
36 alias keep='noglob keep'
37
38 _insert_kept() {
39     (( $#kept )) || return 1
40     local action
41     zstyle -s :completion:$curcontext insert-kept action
42
43     if [[ -n $action ]] ; then
44         compstate[insert]=$action
45     elif [[ $WIDGET == *expand* ]] ; then
46         compstate[insert]=all
47     fi
48     if [[ $WIDGET == *expand* ]] ; then
49         compadd -U ${(M)kept:#${~words[CURRENT]}}
50     else
51         compadd -a kept
52     fi
53 }
54
55 # now bind it to keys and enable completition
56 zle -C insert-kept-result complete-word _generic
57 zle -C expand-kept-result complete-word _generic
58 zstyle ':completion:*-kept-result:*' completer _insert_kept
59 zstyle ':completion:insert-kept-result:*' menu yes select
60
61 bindkey '^Xk' insert-kept-result
62 bindkey '^XK' expand-kept-result    # shift-K to get expansion
63
64 # And the "_expand_word_and_keep" replacement for _expand_word:
65 _expand_word_and_keep() {
66     function compadd() {
67         local -A args
68         zparseopts -E -A args J:
69         if [[ $args[-J] == all-expansions ]] ; then
70             builtin compadd -A kept "$@"
71             kept=( ${(Q)${(z)kept}} )
72         fi
73         builtin compadd "$@"
74     }
75     # for older versions of zsh:
76     local result
77     _main_complete _expand
78     result=$?
79     unfunction compadd
80     return result
81     # versions >=4.2.1 understand this:
82     # { _main_complete _expand } always { unfunction compadd }
83 }
84
85 # This line must come after "compinit" in startup:
86 zle -C _expand_word complete-word _expand_word_and_keep
87
88 # No bindkey needed, it's already ^Xe from _expand_word
89 zstyle ':completion:*' insert-kept menu
90 zmodload -i zsh/complist
91
92 ## END OF FILE #################################################################
93 # vim:filetype=zsh foldmethod=marker autoindent expandtab shiftwidth=4