wmii.sh (4514B)
1 2 [ -z "$scriptname" ] && scriptname="$wmiiscript" 3 echo Start $wmiiscript | wmiir write /event 2>/dev/null || 4 exit 1 5 6 wi_newline=' 7 ' 8 9 _wi_script() { 10 # Awk script to mangle key/event/action definition spec 11 # into switch-case functions and lists of the cases. Also 12 # generates a simple key binding help text based on KeyGroup 13 # clauses and comments that appear on key lines. 14 # 15 # Each clause (Key, Event, Action) generates a function of the 16 # same name which executes the indented text after the matching 17 # clause. Clauses are selected based on the first argument passed 18 # to the mangled function. Additionally, a variable is created named 19 # for the plouralized version of the clause name (Keys, Events, 20 # Actions) which lists each case value. These are used for actions 21 # menus and to write wmii's /keys file. 22 cat <<'!' 23 BEGIN { 24 arg[1] = "Nop" 25 narg = 1; 26 body = ""; 27 keyhelp = "" 28 } 29 function quote(s) { 30 gsub(/'/, "'\\''", s) 31 return "'" s "'" 32 } 33 function addevent() { 34 var = arg[1] "s" 35 for(i=2; i <= narg; i++) { 36 if(body == "") 37 delete a[arg[1],arg[i]] 38 else 39 a[arg[1],arg[i]] = body 40 if(i == 2) { 41 # There's a bug here. Can you spot it? 42 gsub("[^a-zA-Z_0-9]", "_", arg[2]); 43 body = sprintf("%s %s \"$@\"", arg[1], arg[2]) 44 } 45 } 46 } 47 /^(Key)Group[ \t]/ { 48 sub(/^[^ \t]+[ \t]+/, "") 49 keyhelp = keyhelp "\n " $0 "\n" 50 } 51 /^(Event|Key|Action|Menu)[ \t]/ { 52 addevent() 53 split($0, tmp, /[ \t]+#[ \t]*/) 54 narg = split(tmp[1], arg) 55 if(arg[1] == "Key" && tmp[2]) 56 for (i=2; i <= narg; i++) 57 keyhelp = keyhelp sprintf(" %-20s %s\n", 58 arg[i], tmp[2]) 59 body = "" 60 } 61 /^[ \t]/ { 62 sub(/^( |\t)/, "") 63 body = body"\n"$0 64 } 65 66 END { 67 addevent() 68 for(k in a) { 69 split(k, b, SUBSEP) 70 c[b[1]] = c[b[1]] b[2] "\n" 71 if(body != "") 72 d[b[1]] = d[b[1]] quote(b[2]) ")" a[k] "\n;;\n" 73 } 74 for(k in c) 75 printf "%ss=%s\n", k, quote(c[k]) 76 for(k in d) { 77 printf "%s() {\n", k 78 printf " %s=$1; shift\n", tolower(k) 79 printf "case $%s in\n%s\n*) return 1\nesac\n", tolower(k), d[k] 80 printf "}\n" 81 } 82 print "KeysHelp=" quote(keyhelp) 83 } 84 ! 85 } 86 87 _wi_text() { 88 cat <<'!' 89 Event Start 90 if [ "$1" = "$wmiiscript" ]; then 91 exit 92 fi 93 Event Key 94 Key "$@" 95 ! 96 eval "cat <<! 97 $( (test ! -t 0 && cat; for a; do eval "$a"; done) | sed '/^[ ]/s/\([$`\\]\)/\\\1/g') 98 ! 99 " 100 } 101 102 wi_events() { 103 #cho "$(_wi_text "$@" | awk "$(_wi_script)")" | cat -n 104 eval "$(_wi_text "$@" | awk "$(_wi_script)")" 105 } 106 107 wi_fatal() { 108 echo $scriptname: Fatal: $* 109 exit 1 110 } 111 112 wi_notice() { 113 xmessage $scriptname: Notice: $* 114 } 115 116 wi_readctl() { 117 wmiir read /ctl | sed -n 's/^'$1' //p' 118 } 119 120 wmiifont="$(wi_readctl font)" 121 wmiinormcol="$(wi_readctl normcolors)" 122 wmiifocuscol="$(wi_readctl focuscolors)" 123 124 wi_fnmenu() { 125 group="$1-$2"; shift 2 126 _last="$(echo $group|tr - _)_last" 127 eval "last=\"\$$_last\"" 128 res=$(set -- $(echo "$Menus" | awk -v "s=$group" 'BEGIN{n=length(s)} 129 substr($1,1,n) == s{print substr($1,n+2)}') 130 [ $# != 0 ] && wmii9menu -i "$last" "$@") 131 if [ -n "$res" ]; then 132 eval "$_last="'"$res"' 133 Menu $group-$res "$@" 134 fi 135 } 136 137 wi_proglist() { 138 ls -lL $(echo $* | sed 'y/:/ /') 2>/dev/null \ 139 | awk '$1 ~ /^[^d].*x/ { print $NF }' \ 140 | sort | uniq 141 } 142 143 wi_actions() { 144 { wi_proglist $WMII_CONFPATH 145 echo -n "$Actions" 146 } | sort | uniq 147 } 148 149 wi_runconf() { 150 sflag=""; if [ "$1" = -s ]; then sflag=1; shift; fi 151 which="$(which which)" 152 prog=$(PATH="$WMII_CONFPATH" "$which" -- $1 2>/dev/null); shift 153 if [ -n "$prog" ]; then 154 if [ -z "$sflag" ] 155 then "$prog" "$@" 156 else . "$prog" 157 fi 158 else return 1 159 fi 160 } 161 162 wi_script() { 163 _noprog=true 164 if [ "$1" = -f ]; then 165 shift 166 _noprog=/dev/null 167 fi 168 which=$(which which) 169 _prog=$(PATH="$WMII_CONFPATH" $which $1 || echo $_noprog); shift 170 shift; echo "$_prog $*" 171 } 172 173 wi_runcmd() { 174 if [ "$1" = -t ]; then 175 shift 176 set -- wihack -tags $(wmiir read /tag/sel/ctl | sed 1q) "$*" 177 fi 178 eval exec "$*" & 179 } 180 181 wi_tags() { 182 wmiir ls /tag | sed 's,/,,; /^sel$/d' 183 } 184 185 wi_seltag() { 186 wmiir read /tag/sel/ctl | sed 1q | tr -d '\012' 187 } 188 189 wi_selclient() { 190 wmiir read /client/sel/ctl | sed 1q | tr -d '\012' 191 } 192 193 wi_eventloop() { 194 echo "$Keys" | wmiir write /keys 195 196 if [ "$1" = -i ] 197 then cat 198 else wmiir read /event 199 fi | 200 while read wi_event; do 201 IFS="$wi_newline" 202 wi_arg=$(echo "$wi_event" | sed 's/^[^ ]* //') 203 unset IFS 204 set -- $wi_event 205 event=$1; shift 206 ( Event $event "$@" ) 207 done 208 true 209 } 210 211 action() { 212 action=$1; shift 213 if [ -n "$action" ]; then 214 set +x 215 Action $action "$@" \ 216 || wi_runconf $action "$@" 217 fi 218 } 219