From 7cfaf656c4035d514101bdd6aba79e2772e9d288 Mon Sep 17 00:00:00 2001 From: Bart Schaefer Date: Sat, 17 Mar 2001 22:30:11 +0000 Subject: Add zkdb function. --- ChangeLog | 7 ++ Doc/Zsh/contrib.yo | 53 +++++++++- Functions/Misc/.distfiles | 2 +- Functions/Misc/zkbd | 248 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 308 insertions(+), 2 deletions(-) create mode 100644 Functions/Misc/zkbd diff --git a/ChangeLog b/ChangeLog index efd9b640d..7c1a2211a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2001-03-17 Bart Schaefer + + * unposted (but see 13646 and users/3563): Doc/Zsh/contrib.yo, + Functions/Misc/.distfiles, Functions/Misc/zkdb: Function to read + keyboard sequences and save them in an associative array for later + reference (much improved from posted version, which was broken). + 2001-03-15 Peter Stephenson * users/3671: Completion/User/_java: complete in middle of diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo index 7025292fc..cff9f2d39 100644 --- a/Doc/Zsh/contrib.yo +++ b/Doc/Zsh/contrib.yo @@ -75,6 +75,8 @@ installation; if it is not, copy tt(Functions/Misc/run-help) to an appropriate directory. subsect(Recompiling Functions) +cindex(functions, recompiling) +cindex(zrecompile utility) If you frequently edit your zsh functions, or periodically update your zsh installation to track the latest developments, you may find that function @@ -82,7 +84,7 @@ digests compiled with the tt(zcompile) builtin are frequently out of date with respect to the function source files. This is not usually a problem, because zsh always looks for the newest file when loading a function, but it may cause slower shell startup and function loading. Also, if a digest -file is explicitly used as element of tt(fpath), zsh won't check whether +file is explicitly used as an element of tt(fpath), zsh won't check whether any of its source files has changed. The tt(zrecompile) autoloadable function, found in tt(Functions/Misc), can @@ -160,7 +162,51 @@ Once the digests have been created and your tt(fpath) modified to refer to them, you can keep them up to date by running tt(zrecompile) with no arguments. +subsect(Keyboard Definition) +cindex(keyboard definition) + +findex(zkbd) +The large number of possible combinations of keyboards, workstations, +terminals, emulators, and window systems makes it impossible for zsh to +have built-in key bindings for every situation. The tt(zkbd) utility, +found in Functions/Misc, can help you quickly create key bindings for your +configuration. + +Run tt(zkbd) either as an autoloaded function, or as a shell script: + +example(zsh -f ~/zsh-version()/Functions/Misc/zkbd) + +When you run tt(zkbd), it first asks you to enter your terminal type; if +the default it offers is correct, just press return. It then asks you to +press a number of different keys to determine characteristics of your +keyboard and terminal; tt(zkbd) warns you if it finds anything out of the +ordinary, such as a Delete key that sends neither tt(^H) nor tt(^?). + +The keystrokes read by tt(zkbd) are recorded as a definition for an +associative array named tt(key), written to a file in the subdirectory +tt(.zkbd) within either your tt(HOME) or tt(ZDOTDIR) directory. The name +of the file is composed from the tt(TERM), tt(VENDOR) and tt(OSTYPE) +parameters, joined by hyphens. + +You may read this file into your tt(.zshrc) or another startup file with +the "source" or "." commands, then reference the tt(key) parameter in +bindkey commands, like this: + +example(source ${ZDOTDIR:-$HOME}/.zkbd/$TERM-$VENDOR-$OSTYPE +[[ -n ${key[Left]} ]] && bindkey "${key[Left]}" backward-char +[[ -n ${key[Right]} ]] && bindkey "${key[Right]}" forward-char +# etc.) + +Note that in order for `tt(autoload zkbd)' to work, the tt(zkdb) file must +be in one of the directories named in your tt(fpath) array (see +ifzman(zmanref(zshparam))\ +ifnzman(noderef(Parameters Used By The Shell))\ +). This should already be the case if you have a standard zsh +installation; if it is not, copy tt(Functions/Misc/zkbd) to an +appropriate directory. + subsect(Dumping Shell State) +cindex(reporter utility) Occasionally you may encounter what appears to be a bug in the shell, particularly if you are using a beta version of zsh or a development @@ -748,6 +794,11 @@ Same as tt(zmv -C) and tt(zmv -L), respectively. These functions do not appear in the zsh distribution, but can be created by linking tt(zmv) to the names tt(zcp) and tt(zln) in some directory in your tt(fpath). ) +item(tt(zkbd))( +See `Keyboard Definition' +ifzman(above)\ +ifnzman((noderef(Utilities))). +) findex(zmv) item(tt(zmv) [ tt(-finqQsvw) ] [ -C | -L | -M | -p var(program) ] [ -o var(optstring) ] var(srcpat) var(dest) )( Move (usually, rename) files matching the pattern var(srcpat) to diff --git a/Functions/Misc/.distfiles b/Functions/Misc/.distfiles index eba35794c..5ed84ece7 100644 --- a/Functions/Misc/.distfiles +++ b/Functions/Misc/.distfiles @@ -2,5 +2,5 @@ DISTFILES_SRC=' .distfiles acx allopt cat cdmatch cdmatch2 checkmail colors cx harden is-at-least mere multicomp nslookup proto pushd randline - run-help yp yu zed zless zls zmv zrecompile zstyle+ + run-help yp yu zed zkbd zless zls zmv zrecompile zstyle+ ' diff --git a/Functions/Misc/zkbd b/Functions/Misc/zkbd new file mode 100644 index 000000000..30cb4a248 --- /dev/null +++ b/Functions/Misc/zkbd @@ -0,0 +1,248 @@ +#! /bin/zsh -f + +[[ -o interactive ]] && { + local -i ARGC + (ARGC=0) 2>/dev/null || { + print -u2 ${0}: must be run as a function or shell script, not sourced + return 1 + } +} + +emulate -RL zsh +local zkbd term key seq + +zkbd=${ZDOTDIR:-$HOME}/.zkbd +[[ -d $zkbd ]] || mkdir $zkbd || return 1 + +print 'typeset -g -A key\n' > $zkbd/$TERM.tmp || return 1 +trap "unfunction getkey getseq; command rm -f $zkbd/$TERM.tmp" 0 +trap "return 1" 1 2 15 + +getkey () { + local k='' i + for ((i=10; i>0; --i)) + do + read -t -k 1 k && break + sleep 1 + done + [[ -n $k ]] || return 1 + [[ $k = $'\012' || $k = $'\015' || $k = ' ' ]] && return 0 + print -Rn $k +} + +getseq () { + trap "stty ${$(stty -g 2>/dev/null):-echo -raw}" 0 1 2 15 + stty raw -echo + local k='' seq='' i + for ((i=10; i>0; --i)) + do + read -t -k 1 k && break + sleep 1 + done + [[ -n $k ]] || return 1 + [[ $k = $'\012' || $k = $'\015' || $k = ' ' ]] && return 0 + seq=$k + while read -t -k 1 k + do + seq=$seq$k + done + print -Rn ${(V)seq} +} + +read term"?Enter current terminal type: [$TERM] " +[[ -n $term ]] && TERM=$term + +cat <<\EOF + +We will now test some features of your keyboard and terminal. + +If you do not press the requested keys within 10 seconds, key reading will +abort. If your keyboard does not have a requested key, press Space to +skip to the next key. + +EOF + +local ctrl alt meta + +print -n "Hold down Ctrl and press X: " +ctrl=$(getkey) || return 1 +print + +if [[ $ctrl != $'\030' ]] +then + print "Your keyboard does not have a working Ctrl key?" + print "Giving up ..." + return 1 +else + print +fi + +print "Your Meta key may have a Microsoft Windows logo on the cap." +print -n "Hold down Meta and press X: " +meta=$(getkey) || return 1 +print + +if [[ $meta == x ]] +then + print "Your keyboard or terminal does not recognize the Meta key." + unset meta +elif [[ $meta > $'\177' ]] +then + print "Your keyboard uses the Meta key to send high-order characters." +else + unset meta +fi +print + +print -n "Hold down Alt and press X: " +alt=$(getkey) || return 1 +print + +if [[ $alt == x ]] +then + print "Your keyboard or terminal does not recognize the Alt key." + unset alt +elif [[ $alt == $meta ]] +then + print "Your keyboard does not distinguish Alt from Meta." +elif [[ $alt > $'\177' ]] +then + print "Your keyboard uses the Alt key to send high-order characters." +else + unset alt +fi + +(( $+alt + $+meta == 0 )) && cat </dev/tty + +for key in $pckeys # $^modifiers$^pckeys $sunkeys $^modifiers$^sunkeys +do + print -u3 -Rn "Press $key: " + seq="$(getseq)" || return 1 + print "key[$key]='${(q)seq}'" + print -u3 -R $seq +done >> $zkbd/$TERM.tmp + +source $zkbd/$TERM.tmp || return 1 +if [[ "${key[Delete]}" == "${key[Backspace]}" ]] +then + print + print Warning: Backspace and Delete key both send "${(q)key[Delete]}" +else + if [[ "${key[Delete]}" != "^?" ]] + then + print + print Warning: Delete key sends "${(q)key[Delete]}" '(not ^?)' + fi + if [[ "${key[Backspace]}" != "^H" ]] + then + print + print Warning: Backspace sends "${(q)key[Backspace]}" + fi +fi + +command mv $zkbd/$TERM.tmp $zkbd/$TERM-$VENDOR-$OSTYPE + +cat <