1comptestinit () {
2  setopt extendedglob
3  [[ -d $ZTST_testdir/Modules/zsh ]] && module_path=( $ZTST_testdir/Modules )
4  fpath=( $ZTST_srcdir/../Functions/*~*/CVS(/)
5          $ZTST_srcdir/../Completion
6          $ZTST_srcdir/../Completion/*/*~*/CVS(/) )
7
8  zmodload -i zsh/zpty || return $?
9
10  comptest_zsh=${ZSH:-zsh}
11  comptest_keymap=e
12
13  while getopts vz: opt; do
14    case $opt in
15      z) comptest_zsh="$OPTARG";;
16      v) comptest_keymap="v";;
17    esac
18  done
19  (( OPTIND > 1 )) && shift $(( OPTIND - 1 ))
20
21  export PS1="<PROMPT>"
22  zpty zsh "$comptest_zsh -f +Z"
23
24  zpty -r zsh log1 "*<PROMPT>*" || { 
25    print "first prompt hasn't appeared."
26    return 1
27  }
28
29  comptesteval \
30"export LC_ALL=C" \
31"emulate -R zsh" \
32"export ZDOTDIR=$ZTST_testdir" \
33"module_path=( $module_path )" \
34"fpath=( $fpath )" \
35"bindkey -$comptest_keymap" \
36'LISTMAX=10000000
37stty 38400 columns 80 rows 24
38TERM=vt100
39setopt zle
40autoload -U compinit
41compinit -u
42zstyle ":completion:*:default" list-colors "no=<NO>" "fi=<FI>" "di=<DI>" "ln=<LN>" "pi=<PI>" "so=<SO>" "bd=<BD>" "cd=<CD>" "ex=<EX>" "mi=<MI>" "tc=<TC>" "sp=<SP>" "lc=<LC>" "ec=<EC>\n" "rc=<RC>"
43zstyle ":completion:*" group-name ""
44zstyle ":completion:*:messages" format "<MESSAGE>%d</MESSAGE>
45"
46zstyle ":completion:*:descriptions" format "<DESCRIPTION>%d</DESCRIPTION>
47"
48zstyle ":completion:*:options" verbose yes
49zstyle ":completion:*:values" verbose yes
50setopt noalwayslastprompt listrowsfirst completeinword
51zmodload zsh/complist
52expand-or-complete-with-report () {
53  print -lr "<WIDGET><expand-or-complete>"
54  zle expand-or-complete
55  print -lr - "<LBUFFER>$LBUFFER</LBUFFER>" "<RBUFFER>$RBUFFER</RBUFFER>"
56  zle clear-screen
57  zle -R
58}
59list-choices-with-report () {
60  print -lr "<WIDGET><list-choices>"
61  zle list-choices
62  zle clear-screen
63  zle -R
64}
65comp-finish () {
66  print "<WIDGET><finish>"
67  zle kill-whole-line
68  zle clear-screen
69  zle -R
70}
71zle-finish () {
72  print -lr "<WIDGET><finish>" "BUFFER: $BUFFER" "CURSOR: $CURSOR"
73  (( region_active )) && print -lr "MARK: $MARK"
74  zle -K main
75  zle kill-whole-line
76  zle clear-screen
77  zle -R
78}
79zle -N expand-or-complete-with-report
80zle -N list-choices-with-report
81zle -N comp-finish
82zle -N zle-finish
83bindkey "^I" expand-or-complete-with-report
84bindkey "^D" list-choices-with-report
85bindkey "^Z" comp-finish
86bindkey "^M" zle-finish
87bindkey -a "^M" zle-finish
88'
89}
90
91comptesteval () {
92  local tmp=/tmp/comptest.$$
93
94  print -lr - "$@" > $tmp
95  zpty -w zsh ". $tmp"
96  zpty -r -m zsh log_eval "*<PROMPT>*" || {
97    print "prompt hasn't appeared."
98    return 1
99  }
100  rm $tmp
101}
102
103comptest () {
104  input="$*"
105  zpty -n -w zsh "$input"$'\C-Z'
106  zpty -r -m zsh log "*<WIDGET><finish>*<PROMPT>*" || {
107    print "failed to invoke finish widget."
108    return 1
109  }
110
111  logs=(${(s:<WIDGET>:)log})
112  shift logs
113
114  for log in "$logs[@]"; do
115    if [[ "$log" = (#b)*$'<LBUFFER>'(*)$'</LBUFFER>\r\n<RBUFFER>'(*)$'</RBUFFER>'* ]]; then
116      print -lr "line: {$match[1]}{$match[2]}"
117    fi
118    while (( ${(N)log#*(#b)(<LC><(??)><RC>(*)<EC>|<DESCRIPTION>(*)</DESCRIPTION>|<MESSAGE>(*)</MESSAGE>|<COMPADD>(*)</COMPADD>|<INSERT_POSITIONS>(*)</INSERT_POSITIONS>)} )); do
119      log="${log[$mend[1]+1,-1]}"
120      if (( 0 <= $mbegin[2] )); then
121	if [[ $match[2] != TC && $match[3] != \ # ]]; then
122	  print -lr "$match[2]:{${match[3]%${(%):-%E}}}"
123	fi
124      elif (( 0 <= $mbegin[4] )); then
125	print -lr "DESCRIPTION:{$match[4]}"
126      elif (( 0 <= $mbegin[5] )); then
127	print -lr "MESSAGE:{$match[5]}"
128      elif (( 0 <= $mbegin[6] )); then
129        print -lr "COMPADD:{${${match[6]}//[$'\r\n']/}}"
130      elif (( 0 <= $mbegin[7] )); then
131        print -lr "INSERT_POSITIONS:{${${match[7]}//[$'\r\n']/}}"
132      fi
133    done
134  done
135}
136
137zletest () {
138  input="$*"
139  zpty -n -w zsh "$input"$'\C-M'
140  zpty -r -m zsh log "*<WIDGET><finish>*<PROMPT>*" || {
141    print "failed to invoke finish widget."
142    return 1
143  }
144  print -lr "${(@)${(ps:\r\n:)log##*<WIDGET><finish>}[1,-2]}"
145}
146