wmii.rc.rc (3157B)
1 2 # For the time being, this file follows the lisp bracing 3 # convention. i.e.: 4 # if(frob this) { 5 # frob that 6 # if(frob theother) { 7 # unfrob this 8 # unfrob that}} 9 10 wmiiscript=$1 11 wmiikeys=() 12 wmiikeyhelp='' 13 14 wi_newline=' 15 ' 16 17 echo Start $wmiiscript | wmiir write /event >[2]/dev/null \ 18 || exit write 19 20 if(~ $scriptname '') 21 scriptname=$wmiiscript 22 23 # Blech. 24 if(! test -x $PLAN9/bin/read) 25 fn read { sh -c 'read -r x || exit 1; echo $x' } 26 27 fn wi_atexit {} 28 fn sigexit { 29 wi_atexit 30 } 31 32 fn wi_fatal { 33 echo $scriptname: Fatal: $* 34 exit fatal 35 } 36 37 fn wi_notice { 38 xmessage $scriptname: Notice: $* 39 } 40 41 fn wi_readctl { wmiir read /ctl | sed -n 's/^'$1' (.*)/\1/p' } 42 43 wmiifont=`{wi_readctl font} 44 wmiinormcol=`{wi_readctl normcolors} 45 wmiifocuscol=`{wi_readctl focuscolors} 46 47 fn wi_fnmenu { 48 group=$1^Menu-$2 last=$group^_last fns=`{wi_getfuns $group} { 49 shift 2 50 if(! ~ $#fns 0) { 51 res = `{wmii9menu -i $"($last) $fns} \ 52 if(! ~ $res '') { 53 ($last) = $res 54 $group-$res $*}}} 55 } 56 57 fn wi_fn-p { 58 rc -c 'whatis '$1 >[2]/dev/null | grep -s '^fn ' 59 } 60 61 fn wi_proglist { 62 ifs=: { wmiir proglist -- `{echo -n $*} | sort | uniq } 63 } 64 65 fn wi_actions { 66 { wi_proglist $WMII_CONFPATH 67 wi_getfuns Action 68 } | sort | uniq 69 } 70 71 fn wi_script { 72 noprog=true prog=() { 73 if(~ $1 -f) { 74 shift 75 noprog=/dev/null 76 } 77 prog = `{rc -c 'path=$confpath whatis '$1 >[2]/dev/null \ 78 | grep -v '^fn |=' || echo $noprog} 79 shift; echo $prog $*} 80 } 81 82 83 fn wi_initkeys { 84 ifs=() { 85 wmiikeys = `{wmiir read /keys} { 86 mykeys = `{comm -23 \ 87 <{wi_getfuns Key | sort | uniq} \ 88 <{echo $wmiikeys | sort | uniq}} 89 {echo $wmiikeys; wi_getfuns Key} \ 90 | sort | uniq \ 91 | wmiir write /keys }} 92 fn wi_atexit { 93 wi_cleankeys 94 } 95 } 96 97 fn wi_cleankeys { 98 ifs=() { 99 wmiikeys = `{wmiir read /keys} { 100 comm -23 <{echo $wmiikeys | sort | uniq} \ 101 <{echo $mykeys} \ 102 | wmiir write /keys }} 103 } 104 105 fn wi_runcmd { @{ 106 rfork ns 107 path=$oldpath 108 if(~ $1 -t) { 109 shift 110 * = (wihack -tags `{wmiir read /tag/sel/ctl | sed 1q} $*) } 111 fn `{env | 9 sed -n 's/^fn#([^=]+).*/\1/p'} 112 mykeys=() 113 if(! ~ $* '') 114 eval exec $* & } 115 } 116 117 fn wi_getfuns { 118 env | sed -n 's/^fn#'^$1^'-([^=]+).*/\1/p' | sort | uniq 119 } 120 121 for(i in Key Event Action '*Menu') 122 fns=`{wi_getfuns $i} { 123 if(! ~ $fns '') 124 fn $i-^$fns} 125 126 fn wi_tags { 127 wmiir ls /tag | sed 's,/,,; /^sel$/d' 128 } 129 130 fn wi_seltag { 131 wmiir read /tag/sel/ctl | sed 1q 132 } 133 134 fn wi_selclient { 135 wmiir read /client/sel/ctl | sed 1q 136 } 137 138 fn wi_nexttag { 139 awk -v 'curtag='^`{wi_seltag} ' 140 NR==1 {first = $0} 141 $0==curtag { if(getline) print $0; else print first; exit }' 142 } 143 144 fn wi_eventloop { 145 wi_initkeys 146 147 { 148 if(~ $1 -i) 149 cat 150 if not 151 wmiir read /event 152 } | while(ifs=$wi_newline{wi_event=`{read}}) { 153 ifs=$wi_newline{ 154 wi_arg=`{echo $wi_event | sed 's/^[^ ]+ //'}} 155 * = `{echo $wi_event} 156 event = $1; shift 157 Event-$"event $* 158 } >[2]/dev/null </dev/null 159 true 160 } 161 162 fn Event-Key { 163 Key-$1 $1 164 } 165 166 fn Event-Quit { 167 exit 168 } 169 170 fn Event-Start { 171 if(~ $1 $wmiiscript) 172 exit 173 } 174 175 fn Action { 176 cmd=$1 action=Action-$"cmd { shift 177 if(! ~ $cmd '') { 178 if(wi_fn-p $action) 179 $action $* 180 if not 181 wi_runcmd `{wi_script $cmd} $* 182 } 183 } 184 } 185