dot/xinitrc: Search for Emacs 23, and prefer it over other Emacsen.
[profile] / dot / xinitrc
CommitLineData
f617db13 1#! /bin/bash
8e08f814
MW
2###
3### X startup script
f617db13 4
8e08f814
MW
5###--------------------------------------------------------------------------
6### Utility functions.
f617db13 7
8e08f814
MW
8## Progress indicators.
9info=yes
10info () {
11 case $info in yes) echo "- $*" >&2 ;; esac
12}
f617db13 13
8e08f814
MW
14run=yes
15run () {
16 local what=$1; shift
17 local bg=no
f617db13 18
8e08f814
MW
19 case $what in bg*) bg=yes what=${what#bg} ;; esac
20 info "run $what: $*"
f617db13 21
8e08f814
MW
22 case "$run,$bg" in
23 yes,no) "$@" ;;
24 yes,yes) "$@" & ;;
25 esac
26}
eebca092 27
8e08f814 28## Program choice
eebca092 29pick_program () {
8e08f814
MW
30 local what=$1; shift
31 local choice=false
eebca092 32 for i in "$@"; do
8e08f814 33 if type -t >/dev/null "$i"; then choice=$i; break; fi
eebca092 34 done
8e08f814
MW
35 info "pick $what = $choice"
36 echo "$choice"
eebca092
MW
37}
38
8e08f814
MW
39###--------------------------------------------------------------------------
40### Parse arguments.
f617db13 41
8e08f814
MW
42vnc=no
43atomtag=
44start=yes
45wait=yes
f617db13 46
8e08f814
MW
47for opt; do
48 case "$opt" in
49 help)
50 cat <<EOF
51Options:
52 tag=TAG
53 [no]trace
54 [no]info
55 [no]run
56 [no]start
57 [no]wait
58 [no]vnc
59EOF
60 exit
61 ;;
f617db13 62
8e08f814
MW
63 tag=*) atomtag=/${opt#tag=} ;;
64 trace) set -x ;;
65 notrace) set +x ;;
66 info | run | start | wait | vnc) eval "$opt=yes" ;;
67 noinfo | norun | nostart | nowait | novnc) eval "${opt#no}=no" ;;
68
69 *) echo "unknown option $opt" >&2; exit 1 ;;
70 esac
71done
72
73###--------------------------------------------------------------------------
74### Iniitial settings.
75
76## Assume X sessions are secure.
77export __mdw_sechost="`hostname`"
78
79## Obtain the screen dimensions.
80case ",$XWIDTH,$XHEIGHT," in
81 *,,*) eval $(xscsize -bx) ;;
82esac
83info "screen size = $XWIDTH x $XHEIGHT"
84
85initialize () {
86 ## Load the X resource database.
87 run init xrdb -override $HOME/.Xdefaults
88
89 ## Random xsettery.
90 run init xset b 10 2000 50
91 run init xset r rate 500 50
92 run init xset m 2 1
93
f4450ac9
MW
94 ## Key mappings.
95 xmodmap -e 'keysym BackSpace = BackSpace BackSpace'
96
8e08f814
MW
97 ## Gnome settings.
98 case $vnc in no) run bginit gnome-settings-daemon ;; esac
99}
100
101###--------------------------------------------------------------------------
102### Start a window manager.
103
104wm=$(pick_program window-manager enlightenment e16 twm)
105wmopts=""
106case "$wm,$vnc" in
107 enlightenment,yes | e16,yes)
108 wmopts="$eopts -econfdir $HOME/.enlightenment-vnc"
109 ;;
110esac
111
112start-window-manager () {
113 run bginit $wm $wmopts
114}
115
116###--------------------------------------------------------------------------
117### Random useful clients.
118
54951353
MW
119start-clients-local () { :; }
120
8e08f814
MW
121start-clients () {
122 ## Mail notification.
123 run bginit mail-notification
124
125 ## System monitor.
126 case $vnc in no) run bginit gkrellm ;; esac
127
c02999e8
MW
128 ## Screensaver.
129 case $vnc in
130 no)
131 run init xscreensaver-command -exit
132 run bginit xscreensaver -no-splash
133 ;;
134 esac
135
8e08f814
MW
136 ## Panel.
137 case $vnc in no) run bginit gnome-panel ;; esac
54951353 138
e517857c
MW
139 ## System tray.
140 run bginit stalonetray
141
54951353
MW
142 ## Local clients.
143 start-clients-local
8e08f814
MW
144}
145
146###--------------------------------------------------------------------------
147### Main screen layout.
f617db13 148
8e08f814 149## Choose appropriate clients.
0c47c911 150emacs=$(pick_program emacs emacs23 emacs22 emacs21 emacs)
8e08f814 151term=$(pick_program terminal pterm Eterm xterm)
f617db13 152
8e08f814 153## Emacs window measurements.
85bbf8d3 154case "$emacs" in
8e08f814 155 emacs21 | emacs)
85bbf8d3
MW
156 e_colwd=492 e_hextra=34
157 e_colchars=82 e_cextra=-2
158 e_lineht=13 e_vextra=52
159 ;;
0c47c911 160 emacs22 | emacs23)
85bbf8d3
MW
161 e_colwd=492 e_hextra=8
162 e_colchars=82 e_cextra=-6
163 e_lineht=13 e_vextra=46
164 ;;
165esac
f617db13 166
8e08f814 167## Terminal window measurements.
85bbf8d3 168case "$term" in
e2b44bd9 169 pterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-geometry;;
85bbf8d3
MW
170 Eterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-g;;
171 xterm) t_wd=507 t_lineht=13 t_vextra=27 geom=-geometry;;
172esac
f617db13 173
8e08f814 174## GNOME stuff measurements.
f617db13
MW
175declare -i xbound="XWIDTH - 113"
176
8e08f814
MW
177## Choose a width for Emacs.
178##
179## We'd like it to be as wide as possible, allowing for a column of xterms
180## down the right hand side. However, I'd prefer a double-width Emacs to a
181## single-width Emacs and xterms. If it's not going to work at all, a single
182## Emacs column will have to do. Also, there's a strange thing with Emacs21
183## and the toolbar, so we add on some rows which are later mysteriously
184## subtracted.
f617db13 185
85bbf8d3
MW
186declare -i ecols="(xbound - t_wd - e_hextra)/e_colwd"
187if (( ecols < 2 && xbound > e_colwd * 2 + e_hextra )); then
f617db13
MW
188 ecols=2
189elif (( ecols < 1 )); then
190 ecols=1
191fi
192
8e08f814
MW
193declare -i \
194 emacsx="ecols * e_colchars + e_cextra" \
195 emacsy="(XHEIGHT - e_vextra)/e_lineht"
196
197start-emacs () {
198 run bgclients noip $emacs -geometry ${emacsx}x${emacsy}+0+0
199}
200
201## Now place some xterms.
202##
203## A few smaller xterms are in general better than one great big one. 35
204## lines is a good height for most terminals. 25 lines is a minimum. The
205## strategy for doling out xterms into a column is to make as many 35-liners
206## as we can, until the remaining space would be too small for a 25-liner.
207## If we can get two 25s out of that then we do (largest first); otherwise
208## just make one big one. We stop at the end of a page, once we've made
209## three xterms.
210
211start-xterms () {
212
213 ## Initialize some parameters.
6b4faba6 214 declare -i x="ecols * e_colwd + e_hextra" xb=xbound
8e08f814
MW
215 declare -i n=0 pgx=0 l h y ht
216 declare -i hstd="35 * t_lineht + t_vextra" hmin="25 * t_lineht + t_vextra"
217
218 ## Do the placement.
219 while :; do
220
221 ## Start a new iteration.
6b4faba6 222 if ((x + t_wd > xb)); then
8e08f814 223 if ((n >= 3)); then break; fi
6b4faba6 224 x="pgx + XWIDTH" pgx="pgx + XWIDTH" xb="xb + XWIDTH"
8e08f814
MW
225 fi
226
227 ## Make large xterms.
228 y=0 ht=XHEIGHT
229 while ((ht - hstd >= hmin)); do
230 run bgclients $term $geom 80x35+$x+$y
231 y="y + hstd" ht="ht - hstd" n="n + 1"
232 done
233
234 ## Fill the remaining space.
235 if ((ht >= 2 * hmin)); then h="ht - hmin"; else h=ht; fi
236 l="(h - t_vextra)/t_lineht" h="l * t_lineht + t_vextra"
237 run bgclients $term $geom 80x$l+$x+$y
238 y="y + h" ht="ht - h" n="n + 1"
239 if ((ht >= hmin)); then
240 run bgclients $term $geom 80x25+$x+$y
241 n="n + 1"
242 fi
243 x="x + t_wd"
f617db13 244 done
8e08f814 245}
f617db13 246
8e08f814 247###--------------------------------------------------------------------------
3bdada49
MW
248### Requesters.
249
250req () {
251 declare title=$1 hist=$2; shift 2
252 cmd=$(xgetline -t "$title" -p "Command:" -Hl "$HOME/$hist") &&
253 exec "$@" "$cmd"
254}
255
256###--------------------------------------------------------------------------
8e08f814
MW
257### Final waiting.
258
259atom=XINIT_COMMAND$atomtag
260
261xwait () {
262 while :; do
263 xatom delete $atom
264 info "waiting on $atom"
265 line=$(xatom wait $atom)
266 info "xatom: $line"
267
268 case "$line" in
269 :help)
270 xmsg -I -t "xinitrc help" -d "xinitrc commands" - <<EOF &
271:help
272:emacs :xterms :window-manager :clients
3bdada49 273:ask-run :ask-command
8e08f814
MW
274:init
275:terminal
276! SHELL-COMMAND
277CLIENT
278EOF
279 ;;
280 :emacs | :xterms | :window-manager | :clients)
281 start-${line#:}
282 ;;
283 :terminal)
284 run bgclients $term
285 ;;
286 :init)
287 initialize
288 ;;
289 :exec)
290 info "restarting xinitrc"
291 exec "$0" wait nostart
292 ;;
3bdada49
MW
293 :ask-run)
294 req "Shell command" .cmd.hist xcatch -FMiscFixed6x13 -- sh -c&
295 ;;
296 :ask-command)
297 req "xinit command" .xinit.hist xatom set XINIT_COMMAND$atomtag&
298 ;;
8e08f814
MW
299 :*)
300 xmsg -E -t "xinitrc error" "Unknown command \`$line'" &
301 ;;
302 !*)
303 eval "${line#!}"
304 ;;
305 *)
306 set -- $line
307 run bgclients "$@"
308 ;;
309 esac
310 done
311}
312
313###--------------------------------------------------------------------------
314### Actually start things up.
315
54951353
MW
316if [ -f $HOME/.xinitrc-local ]; then
317 . $HOME/.xinitrc-local
318fi
319
8e08f814
MW
320case "$start" in
321 yes)
322 info "starting standard clients"
323 initialize
324 start-window-manager
325 start-clients
326 start-emacs
327 start-xterms
328 ;;
329 no)
330 info "not starting standard clients"
331 ;;
332esac
333
334case "$wait" in
335 yes)
336 xwait
337 ;;
338 no)
339 info "not waiting before exit"
340 ;;
341esac
f617db13 342
8e08f814 343###----- That's all, folks --------------------------------------------------