]> gitweb.pimeys.fr Git - config-20-100.git/blob - .zsh/rc/base/30_completion
Ajout du zshrc, même s'il manque encore de commentaires etc.
[config-20-100.git] / .zsh / rc / base / 30_completion
1
2 # a generic accept-line wrapper {{{
3
4 # This widget can prevent unwanted autocorrections from command-name
5 # to _command-name, rehash automatically on enter and call any number
6 # of builtin and user-defined widgets in different contexts.
7 #
8 # For a broader description, see:
9 # <http://bewatermyfriend.org/posts/2007/12-26.11-50-38-tooltime.html>
10 #
11 # The code is imported from the file 'zsh/functions/accept-line' from
12 # <http://ft.bewatermyfriend.org/comp/zsh/zsh-dotfiles.tar.bz2>, which
13 # distributed under the same terms as zsh itself.
14
15 # A newly added command will may not be found or will cause false
16 # correction attempts, if you got auto-correction set. By setting the
17 # following style, we force accept-line() to rehash, if it cannot
18 # find the first word on the command line in the $command[] hash.
19 zstyle ':acceptline:*' rehash true
20
21 function Accept-Line() {
22 emulate -L zsh
23 local -a subs
24 local -xi aldone
25 local sub
26
27 zstyle -a ":acceptline:${alcontext}" actions subs
28
29 (( ${#subs} < 1 )) && return 0
30
31 (( aldone = 0 ))
32 for sub in ${subs} ; do
33 [[ ${sub} == 'accept-line' ]] && sub='.accept-line'
34 zle ${sub}
35
36 (( aldone > 0 )) && break
37 done
38 }
39
40 function Accept-Line-getdefault() {
41 emulate -L zsh
42 local default_action
43
44 zstyle -s ":acceptline:${alcontext}" default_action default_action
45 case ${default_action} in
46 ((accept-line|))
47 printf ".accept-line"
48 ;;
49 (*)
50 printf ${default_action}
51 ;;
52 esac
53 }
54
55 function accept-line() {
56 emulate -L zsh
57 local -a cmdline
58 local -x alcontext
59 local buf com fname format msg default_action
60
61 alcontext='default'
62 buf="${BUFFER}"
63 cmdline=(${(z)BUFFER})
64 com="${cmdline[1]}"
65 fname="_${com}"
66
67 zstyle -t ":acceptline:${alcontext}" rehash \
68 && [[ -z ${commands[$com]} ]] \
69 && rehash
70
71 if [[ -n ${reswords[(r)$com]} ]] \
72 || [[ -n ${aliases[$com]} ]] \
73 || [[ -n ${functions[$com]} ]] \
74 || [[ -n ${builtins[$com]} ]] \
75 || [[ -n ${commands[$com]} ]] ; then
76
77 # there is something sensible to execute, just do it.
78 alcontext='normal'
79 zle Accept-Line
80
81 default_action=$(Accept-Line-getdefault)
82 zstyle -T ":acceptline:${alcontext}" call_default \
83 && zle ${default_action}
84 return
85 fi
86
87 if [[ -o correct ]] \
88 || [[ -o correctall ]] \
89 && [[ -n ${functions[$fname]} ]] ; then
90
91 # nothing there to execute but there is a function called
92 # _command_name; a completion widget. Makes no sense to
93 # call it on the commandline, but the correct{,all} options
94 # will ask for it nevertheless, so warn the user.
95 if [[ ${LASTWIDGET} == 'accept-line' ]] ; then
96 # Okay, we warned the user before, he called us again,
97 # so have it his way.
98 alcontext='force'
99 zle Accept-Line
100
101 default_action=$(Accept-Line-getdefault)
102 zstyle -T ":acceptline:${alcontext}" call_default \
103 && zle ${default_action}
104 return
105 fi
106
107 # prepare warning message for the user, configurable via zstyle.
108 zstyle -s ":acceptline:${alcontext}" compwarnfmt msg
109
110 if [[ -z ${msg} ]] ; then
111 msg="%c will not execute and completion %f exists."
112 fi
113
114 zformat -f msg "${msg}" "c:${com}" "f:${fname}"
115
116 zle -M -- "${msg}"
117 return
118 elif [[ -n ${buf//[$' \t\n']##/} ]] ; then
119 # If we are here, the commandline contains something that is not
120 # executable, which is neither subject to _command_name correction
121 # and is not empty. might be a variable assignment
122 alcontext='misc'
123 zle Accept-Line
124
125 default_action=$(Accept-Line-getdefault)
126 zstyle -T ":acceptline:${alcontext}" call_default \
127 && zle ${default_action}
128 return
129 fi
130
131 # If we got this far, the commandline only contains whitespace, or is empty.
132 alcontext='empty'
133 zle Accept-Line
134
135 default_action=$(Accept-Line-getdefault)
136 zstyle -T ":acceptline:${alcontext}" call_default \
137 && zle ${default_action}
138 }
139
140 zle -N accept-line
141 zle -N Accept-Line
142
143 # }}}
144
145 # power completion - abbreviation expansion {{{
146 # power completion / abbreviation expansion / buffer expansion
147 # see http://zshwiki.org/home/examples/zleiab for details
148 # less risky than the global aliases but powerful as well
149 # just type the abbreviation key and afterwards ',.' to expand it
150 declare -A abk
151 setopt extendedglob
152 setopt interactivecomments
153 abk=(
154 # key # value (#d additional doc string)
155 #A# start
156 '...' '../..'
157 '....' '../../..'
158 'BG' '& exit'
159 'C' '| wc -l'
160 'G' '|& grep --color=auto '
161 'H' '| head'
162 'Hl' ' --help |& less -r' #d (Display help in pager)
163 'L' '| less'
164 'LL' '|& less -r'
165 'M' '| most'
166 'N' '&>/dev/null' #d (No Output)
167 'R' '| tr A-z N-za-m' #d (ROT13)
168 'SL' '| sort | less'
169 'S' '| sort -u'
170 'T' '| tail'
171 'V' '|& vim -'
172 #A# end
173 'co' './configure && make && sudo make install'
174 )
175
176 globalias() {
177 emulate -L zsh
178 setopt extendedglob
179 local MATCH
180
181 if (( NOABBREVIATION > 0 )) ; then
182 LBUFFER="${LBUFFER},."
183 return 0
184 fi
185
186 matched_chars='[.-|_a-zA-Z0-9]#'
187 LBUFFER=${LBUFFER%%(#m)[.-|_a-zA-Z0-9]#}
188 LBUFFER+=${abk[$MATCH]:-$MATCH}
189 }
190
191 zle -N globalias
192 bindkey ",." globalias
193 # }}}