wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

wmii.sh.sh (4743B)


      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 	eval "cat <<!
     89 $( (test ! -t 0 && cat; for a; do eval "$a"; done) | sed '/^[ 	]/s/\([$`\\]\)/\\\1/g')
     90 !
     91 "
     92 }
     93 
     94 _wi_events=""
     95 wi_events() {
     96 	eval=""; [ "$1" = -e ] && eval=1 && shift
     97 	_wi_events="$(_wi_text "$@")
     98 $_wi_events"
     99 	# -n "$eval" ] && printf %s "$_wi_events" | awk "$(_wi_script)" >&2
    100 	[ -n "$eval" ] && eval "$(printf %s "$_wi_events" | awk "$(_wi_script)")"
    101 }
    102 
    103 wi_events <<'!'
    104 Event Key
    105 	Key "$@"
    106 !
    107 
    108 wi_fatal() {
    109 	echo $scriptname: Fatal: $*
    110 	exit 1
    111 }
    112 
    113 wi_notice() {
    114 	xmessage $scriptname: Notice: $*
    115 }
    116 
    117 wi_readctl() {
    118 	wmiir read /ctl | sed -n 's/^'$1' //p'
    119 }
    120 
    121 wmiifont="$(wi_readctl font)"
    122 wmiinormcol="$(wi_readctl normcolors)"
    123 wmiifocuscol="$(wi_readctl focuscolors)"
    124 
    125 wi_fnmenu() {
    126 	group="$1-$2"; shift 2
    127 	_last="$(echo $group|tr - _)_last"
    128 	eval "last=\"\$$_last\""
    129 	res=$(set -- $(echo "$Menus" | awk -v "s=$group" 'BEGIN{n=length(s)}
    130 		         substr($1,1,n) == s{print substr($1,n+2)}')
    131 	      [ $# != 0 ] && wmii9menu -i "$last" "$@")
    132 	if [ -n "$res" ]; then
    133 		eval "$_last="'"$res"'
    134 		Menu $group-$res "$@"
    135 	fi
    136 }
    137 
    138 wi_proglist() {
    139         wmiir proglist -- $(echo $* | sed 'y/:/ /') | sort | uniq
    140 }
    141 
    142 wi_actions() {
    143 	{	wi_proglist $WMII_CONFPATH
    144 	 	echo -n "$Actions"
    145 	} | sort | uniq
    146 }
    147 
    148 wi_runconf() {
    149 	sflag=""; if [ "$1" = -s ]; then sflag=1; shift; fi
    150 	which="$(which which)"
    151 	prog=$(PATH="$WMII_CONFPATH" "$which" -- $1 2>/dev/null); shift
    152 	if [ -n "$prog" ]; then
    153 		if [ -z "$sflag" ]
    154 		then "$prog" "$@"
    155 		else . "$prog"
    156 		fi
    157 	else return 1
    158 	fi
    159 }
    160 
    161 wi_script() {
    162 	_noprog=true
    163 	if [ "$1" = -f ]; then
    164 		shift
    165 		_noprog=/dev/null
    166 	fi
    167 	which=$(which which)
    168 	_prog=$(PATH="$WMII_CONFPATH" $which $1 || echo $_noprog); shift
    169 	shift; echo "$_prog $*"
    170 }
    171 
    172 wi_runcmd() {
    173 	if [ "$1" = -t ]; then
    174 		shift
    175 		set -- wihack -tags $(wmiir read /tag/sel/ctl | sed 1q) "$*"
    176 	fi
    177 	eval exec "$*" &
    178 }
    179 
    180 wi_tags() {
    181 	wmiir ls /tag | sed 's,/,,; /^sel$/d'
    182 }
    183 
    184 wi_seltag() {
    185 	wmiir read /tag/sel/ctl | sed 1q | tr -d '\012'
    186 }
    187 
    188 wi_selclient() {
    189 	wmiir read /client/sel/ctl | sed 1q | tr -d '\012'
    190 }
    191 
    192 wi_nexttag() {
    193 	awk -v curtag=$(wi_seltag) '
    194 		NR==1 {first = $0}
    195 		$0==curtag { if(getline) print $0; else print first; exit }'
    196 }
    197 
    198 wi_eventloop() {
    199 	echo "$Keys" | wmiir write /keys
    200 
    201 	if [ "$1" = -i ]
    202 	then cat
    203 	else wmiir read /event
    204 	fi |
    205 	while read wi_event; do
    206 		IFS="$wi_newline"
    207 		wi_arg=$(echo "$wi_event" | sed 's/^[^ ]* //')
    208 		unset IFS
    209 		set -- $wi_event
    210 		event=$1; shift
    211 		[ "$event" = Start -a "$1" = "$wmiiscript" ] &&
    212 			exit
    213 		( Event $event "$@" )
    214 	done
    215 	true
    216 }
    217 
    218 action() {
    219 	action=$1; shift
    220 	if [ -n "$action" ]; then
    221 		set +x
    222 		Action $action "$@" \
    223 		|| wi_runconf $action "$@"
    224 	fi
    225 }
    226