mirror of
git://git.code.sf.net/p/zsh/code
synced 2025-09-23 17:01:05 +02:00
Michael Hwang <michael.a.hwang@gmail.com>: 27428:
improved _make
This commit is contained in:
parent
b7e76da2ef
commit
b97284cde2
2 changed files with 203 additions and 149 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
2009-11-24 Peter Stephenson <pws@csr.com>
|
||||||
|
|
||||||
|
* Michael Hwang <michael.a.hwang@gmail.com>: 27428:
|
||||||
|
Completion/Unix/Command/_make: complete rewrite with
|
||||||
|
variable completion.
|
||||||
|
|
||||||
2009-11-22 Peter Stephenson <p.w.stephenson@ntlworld.com>
|
2009-11-22 Peter Stephenson <p.w.stephenson@ntlworld.com>
|
||||||
|
|
||||||
* Alexey. I Froloff: users/14588:
|
* Alexey. I Froloff: users/14588:
|
||||||
|
@ -12381,5 +12387,5 @@
|
||||||
|
|
||||||
*****************************************************
|
*****************************************************
|
||||||
* This is used by the shell to define $ZSH_PATCHLEVEL
|
* This is used by the shell to define $ZSH_PATCHLEVEL
|
||||||
* $Revision: 1.4820 $
|
* $Revision: 1.4821 $
|
||||||
*****************************************************
|
*****************************************************
|
||||||
|
|
|
@ -1,168 +1,216 @@
|
||||||
#compdef make gmake pmake dmake
|
#compdef make gmake pmake dmake
|
||||||
|
|
||||||
local prev="$words[CURRENT-1]" file expl tmp is_gnu dir incl
|
# TODO: Based on targets given on the command line, show only variables that
|
||||||
|
# are used in those targets and their dependencies.
|
||||||
|
|
||||||
|
local prev="$words[CURRENT-1]" file expl tmp is_gnu dir incl match
|
||||||
|
local -A TARGETS VARIABLES
|
||||||
|
|
||||||
expandVars() {
|
expandVars() {
|
||||||
local open close var val tmp=$2 ret=$2
|
local open close var val front ret tmp=$1
|
||||||
if (( $1 == 0 )); then
|
|
||||||
return
|
front=${tmp%%\$*}
|
||||||
fi
|
case $tmp in
|
||||||
while :; do
|
(\(*) # Variable of the form $(foobar)
|
||||||
var=${tmp#*\$}
|
open='('
|
||||||
if [[ $var != $tmp ]]; then
|
close=')'
|
||||||
tmp=$var
|
;;
|
||||||
case $var in
|
|
||||||
(\(*)
|
({*) # ${foobar}
|
||||||
open='('
|
open='{'
|
||||||
close=')'
|
close='}'
|
||||||
;;
|
;;
|
||||||
({*)
|
|
||||||
open='{'
|
([[:alpha:]]*) # $foobar. This is exactly $(f)oobar.
|
||||||
close='}'
|
open=''
|
||||||
;;
|
close=''
|
||||||
([[:alpha:]]*)
|
var=${(s::)var[1]}
|
||||||
open=''
|
;;
|
||||||
close=''
|
|
||||||
var=${(s::)var[1]}
|
(\$*) # Escaped $.
|
||||||
;;
|
print -- "${front}\$$(expandVars ${tmp#\$})"
|
||||||
(\$*)
|
return
|
||||||
# avoid parsing second $ in $$
|
;;
|
||||||
tmp=${tmp#\$}
|
|
||||||
;&
|
(*) # Nothing left to substitute.
|
||||||
(*)
|
print -- $tmp
|
||||||
continue
|
return
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
if [[ $open != '' ]]; then
|
|
||||||
var=${var#$open}
|
if [[ -n $open ]]
|
||||||
|
then
|
||||||
|
var=${tmp#$open}
|
||||||
var=${var%%$close*}
|
var=${var%%$close*}
|
||||||
fi
|
|
||||||
case $var in
|
|
||||||
([[:alnum:]_]#)
|
|
||||||
val=${(P)var}
|
|
||||||
val=$(expandVars $(($1 - 1)) $val)
|
|
||||||
ret=${ret//\$$open$var$close/$val}
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
else
|
|
||||||
print -- ${ret//\$\$/\$}
|
|
||||||
return
|
|
||||||
fi
|
fi
|
||||||
done
|
|
||||||
|
case $var in
|
||||||
|
([[:alnum:]_]#)
|
||||||
|
val=${VARIABLES[$var]}
|
||||||
|
ret=${ret//\$$open$var$close/$val}
|
||||||
|
;;
|
||||||
|
|
||||||
|
(*) # Improper variable name. No replacement. I'm not sure if this is desired behavior.
|
||||||
|
front+="\$$open$var$close"
|
||||||
|
ret=${ret/\$$open$var$close/}
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
print -- "${front}$(expandVars ${ret})"
|
||||||
}
|
}
|
||||||
|
|
||||||
# parseMakefile only runs inside $(...), so it doesn't matter that
|
parseMakefile () {
|
||||||
# it pollutes the global namespace, setting zsh variables to
|
local input var val target dep TAB=$'\t' dir=$1 tmp
|
||||||
# make variables. The difficult case is where a make variable
|
|
||||||
# is special in zsh; we use local -h to hide those. This
|
|
||||||
# isn't a complete solution since it means variables defined in
|
|
||||||
# included Makefiles are undefined before returning to the parent.
|
|
||||||
parseMakefile() {
|
|
||||||
local input var val TAB=$'\t' dir=$1
|
|
||||||
|
|
||||||
while read input; do
|
while read input
|
||||||
case "$input " in
|
do
|
||||||
([[:alnum:]][[:alnum:]_]#[ $TAB]#=*)
|
case "$input " in
|
||||||
var=${input%%[ $TAB]#=*}
|
# VARIABLE = value
|
||||||
val=${input#*=}
|
([[:alnum:]][[:alnum:]_]#[ $TAB]#=*)
|
||||||
val=${val##[ $TAB]#}
|
var=${input%%[ $TAB]#=*}
|
||||||
[[ ${(tP)var} = *special ]] && local -h $var
|
val=${input#*=}
|
||||||
eval $var=\$val
|
val=${val##[ $TAB]#}
|
||||||
;;
|
VARIABLES[$var]=$val
|
||||||
([[:alnum:]][[:alnum:]_]#[ $TAB]#:=*)
|
;;
|
||||||
var=${input%%[ $TAB]#:=*}
|
|
||||||
val=${input#*=}
|
# VARIABLE := value
|
||||||
val=${val##[ $TAB]#}
|
# Evaluated immediately
|
||||||
val=$(expandVars 10 $val)
|
([[:alnum:]][[:alnum:]_]#[ $TAB]#:=*)
|
||||||
[[ ${(tP)var} = *special ]] && local -h $var
|
var=${input%%[ $TAB]#:=*}
|
||||||
eval $var=\$val
|
val=${input#*=}
|
||||||
;;
|
val=${val##[ $TAB]#}
|
||||||
([[:alnum:]][^$TAB:=]#:[^=]*)
|
val=$(expandVars $val)
|
||||||
input=${input%%:*}
|
VARIABLES[$var]=$val
|
||||||
print $(expandVars 10 $input)
|
;;
|
||||||
;;
|
|
||||||
(${~incl} *)
|
# TARGET: dependencies
|
||||||
local f=${input##${~incl} ##}
|
# TARGET1 TARGET2 TARGET3: dependencies
|
||||||
if [[ $incl = '.include' ]]; then
|
([[:alnum:]][^$TAB:=]#:[^=]*)
|
||||||
f=${f#[\"<]}
|
input=$(expandVars $input)
|
||||||
f=${f%[\">]}
|
target=${input%%:*}
|
||||||
fi
|
dep=${input#*:}
|
||||||
f=$(expandVars 10 $f)
|
dep=${(z)dep}
|
||||||
case $f in
|
dep="$dep"
|
||||||
(/*) ;;
|
for tmp in ${(z)target}
|
||||||
(*) f=$dir/$f ;;
|
do
|
||||||
esac
|
TARGETS[$tmp]=$dep
|
||||||
if [ -r $f ]; then
|
done
|
||||||
parseMakefile ${f%%/[^/]##} < $f
|
;;
|
||||||
fi
|
|
||||||
;;
|
# Include another makefile
|
||||||
esac
|
(${~incl} *)
|
||||||
done
|
local f=${input##${~incl} ##}
|
||||||
|
if [[ $incl == '.include' ]]
|
||||||
|
then
|
||||||
|
f=${f#[\"<]}
|
||||||
|
f=${f%[\">]}
|
||||||
|
fi
|
||||||
|
f=$(expandVars $f)
|
||||||
|
case $f in
|
||||||
|
(/*) ;;
|
||||||
|
(*) f=$dir/$f ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
if [[ -r $f ]]
|
||||||
|
then
|
||||||
|
parseMakefile ${f%%/[^/]##} < $f
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
findBasedir () {
|
findBasedir () {
|
||||||
local file index basedir
|
local file index basedir
|
||||||
basedir=$PWD
|
basedir=$PWD
|
||||||
for ((index=0; index<$#@; index++)); do
|
for (( index=0; index < $#@; index++ ))
|
||||||
if [[ $@[index] = -C ]]; then
|
do
|
||||||
file=${~@[index+1]};
|
if [[ $@[index] == -C ]]
|
||||||
if [[ -z $file ]]; then
|
then
|
||||||
# make returns with an error if an empty arg is given
|
file=${~@[index+1]};
|
||||||
# even if the concatenated path is a valid directory
|
if [[ -z $file ]]
|
||||||
return
|
then
|
||||||
elif [[ $file = /* ]]; then
|
# make returns with an error if an empty arg is given
|
||||||
# Absolute path, replace base directory
|
# even if the concatenated path is a valid directory
|
||||||
basedir=$file
|
return
|
||||||
else
|
elif [[ $file == /* ]]
|
||||||
# Relative, concatenate path
|
then
|
||||||
basedir=$basedir/$file
|
# Absolute path, replace base directory
|
||||||
fi
|
basedir=$file
|
||||||
fi
|
else
|
||||||
done
|
# Relative, concatenate path
|
||||||
print -- $basedir
|
basedir=$basedir/$file
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
print -- $basedir
|
||||||
}
|
}
|
||||||
|
|
||||||
_pick_variant -r is_gnu gnu=GNU unix -v -f
|
_pick_variant -r is_gnu gnu=GNU unix -v -f
|
||||||
|
|
||||||
if [[ $is_gnu = gnu ]]; then
|
if [[ $is_gnu == gnu ]]
|
||||||
incl="(-|)include"
|
then
|
||||||
|
incl="(-|)include"
|
||||||
else
|
else
|
||||||
incl=.include
|
incl=.include
|
||||||
fi
|
fi
|
||||||
if [[ "$prev" = -[CI] ]]; then
|
|
||||||
_files -W ${(q)$(findBasedir ${words[1,CURRENT-1]})} -/
|
|
||||||
elif [[ "$prev" = -[foW] ]]; then
|
|
||||||
_files -W ${(q)$(findBasedir $words)}
|
|
||||||
else
|
|
||||||
file="$words[(I)-f]"
|
|
||||||
if (( file )); then
|
|
||||||
file=${~words[file+1]}
|
|
||||||
[[ $file = [^/]* ]] && file=${(q)$(findBasedir $words)}/$file
|
|
||||||
[[ -r $file ]] || file=
|
|
||||||
else
|
|
||||||
local basedir
|
|
||||||
basedir=${(q)$(findBasedir $words)}
|
|
||||||
if [[ $is_gnu = gnu && -r $basedir/GNUmakefile ]]; then
|
|
||||||
file=$basedir/GNUmakefile
|
|
||||||
elif [[ -r $basedir/makefile ]]; then
|
|
||||||
file=$basedir/makefile
|
|
||||||
elif [[ -r $basedir/Makefile ]]; then
|
|
||||||
file=$basedir/Makefile
|
|
||||||
else
|
|
||||||
file=''
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -n "$file" ]] && _tags targets; then
|
if [[ "$prev" == -[CI] ]]
|
||||||
if [[ $is_gnu = gnu ]] &&
|
then
|
||||||
zstyle -t ":completion:${curcontext}:targets" call-command; then
|
_files -W ${(q)$(findBasedir ${words[1,CURRENT-1]})} -/
|
||||||
tmp=( $(_call_program targets "$words[1]" -nsp --no-print-directory -f "$file" .PHONY 2> /dev/null | parseMakefile $PWD) )
|
elif [[ "$prev" == -[foW] ]]
|
||||||
else
|
then
|
||||||
tmp=( $(parseMakefile $PWD < $file) )
|
_files -W ${(q)$(findBasedir $words)}
|
||||||
fi
|
else
|
||||||
_wanted targets expl 'make target' compadd -a tmp && return 0
|
file="$words[(I)-f]"
|
||||||
fi
|
if (( file ))
|
||||||
compstate[parameter]="${PREFIX%%\=*}"
|
then
|
||||||
compset -P 1 '*='
|
file=${~words[file+1]}
|
||||||
_value "$@"
|
[[ $file == [^/]* ]] && file=${(q)$(findBasedir $words)}/$file
|
||||||
|
[[ -r $file ]] || file=
|
||||||
|
else
|
||||||
|
local basedir
|
||||||
|
basedir=${(q)$(findBasedir $words)}
|
||||||
|
if [[ $is_gnu == gnu && -r $basedir/GNUmakefile ]]
|
||||||
|
then
|
||||||
|
file=$basedir/GNUmakefile
|
||||||
|
elif [[ -r $basedir/makefile ]]
|
||||||
|
then
|
||||||
|
file=$basedir/makefile
|
||||||
|
elif [[ -r $basedir/Makefile ]]
|
||||||
|
then
|
||||||
|
file=$basedir/Makefile
|
||||||
|
else
|
||||||
|
file=''
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -n "$file" ]]
|
||||||
|
then
|
||||||
|
if [[ $is_gnu == gnu ]] && zstyle -t ":completion:${curcontext}:targets" call-command
|
||||||
|
then
|
||||||
|
parseMakefile $PWD < <(_call_program targets "$words[1]" -nsp --no-print-directory -f "$file" .PHONY 2> /dev/null)
|
||||||
|
else
|
||||||
|
parseMakefile $PWD < $file
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ $PREFIX == (#b)([^=]##)'='* ]] && [[ -n ${${(k)VARIABLES}[(r)${match[1]}]} ]]
|
||||||
|
then
|
||||||
|
_message 'override make variable'
|
||||||
|
else
|
||||||
|
_tags targets variables
|
||||||
|
while _tags
|
||||||
|
do
|
||||||
|
_requested targets expl 'make targets' \
|
||||||
|
compadd -- ${(k)TARGETS}
|
||||||
|
_requested variables expl 'make variables' \
|
||||||
|
compadd -S '=' -- ${(k)VARIABLES}
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
# These are left over from the old completion. I'm not sure what they do.
|
||||||
|
#compstate[parameter]="${PREFIX%%\=*}"
|
||||||
|
#compset -P 1 '*='
|
||||||
|
#_value "$@"
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Reference in a new issue