The Great Whitespace Cleanup
[sxemacs] / m4 / sxe-option-groups.m4
1 dnl sxe-option-groups.m4 --- Option group miracle
2 dnl
3 dnl Copyright (C) 2005, 2006, 2007, 2008 Sebastian Freundt
4 dnl
5 dnl Author: Sebastian Freundt <hroptatyr@sxemacs.org>
6 dnl
7 dnl This file is part of SXEmacs.
8
9
10 AC_DEFUN([OG_ARG_POSSVAL], [dnl
11 [Possible arguments:]ifelse($2,,,[ for $2])[]dnl
12 ifelse(dnl
13 $1,[og_any], [dnl
14 [ anything]],
15 $1,[og_file], [dnl
16 [ ]any file names],
17 $1,,[dnl
18 [ ]either `yes' or `no'],[dnl
19 [ ]one of [`]patsubst([$1],[ ],['[,] `])[']])])
20
21 AC_DEFUN([OG_OPTION_GROUP], [dnl
22 AC_ARG_WITH(xyzoptiongroup,[
23 [$1]
24 patsubst([$1],[.],[=])])])
25
26 pushdef([OG_ERROR_ADDITIONAL_VERBOSITY], [dnl
27 - If you do not see or know what the option --with-$1 is all about
28   consider to consult $sxe_srcdir/configure --help.
29 - Also SXEmacs comes with a sophisticated set of default values which
30   run out of the box on most machines so retry your configure line
31   WITHOUT this option at all.
32 - If you used to pass the option `--with-$1=$withval' and it formerly
33   has been accepted please consult $sxe_srcdir/configure --help and perhaps
34   update your scripts.
35 - If you are absolutely sure that `$withval' _should_ work[,] try again!
36   .oO(and see me barf again, too)
37 ])
38
39 dnl borrowed from the openldap crew, slightly modified
40 dnl --------------------------------------------------------------------
41 dnl Restricted form of AC_ARG_WITH that limits user options
42 dnl
43 dnl $1 = option name
44 dnl $2 = help-string
45 dnl $3 = default value, can be auto or none
46 dnl $4 = default value documentation
47 dnl $5 = allowed values [yes no] if empty, can be [og_any]
48 dnl $6 = actions in case option was set (old AC_ARG_WITH arg #3)
49 dnl $7 = actions in case option was not set (old AC_ARG_WITH arg #4)
50 AC_DEFUN([OG_ARG_WITH], [# Option Group --with-$1
51         ## announce this option as valid option
52         pushdef([og_group], translit([$1], [-], [_]))
53
54         sxe_with_options="$sxe_with_options $1"
55         og_[]og_group[]_opts="$5"
56
57         AC_ARG_WITH($1,[
58 [$2]ifelse($5,[og_any],,[
59 AS_HELP_STRING([], (OG_ARG_POSSVAL($5)))])[]ifelse($4,,,[
60 AS_HELP_STRING([], [@<:@Default: $4@:>@])])],[
61         ifelse($5,og_any,,[
62                 og_arg=invalid
63                 for og_val in ifelse($5,,[yes no],[$5]) ; do
64                         if test "$withval" = "$og_val" ; then
65                                 og_arg="$og_val"
66                         fi
67                 done
68                 if test "$og_arg" = "invalid" ; then
69                         AS_MESSAGE(Wait a moment ... Are you nuts?)
70                         if test "`echo $withval | grep ','`"; then
71                                 AC_MSG_ERROR([dnl
72 Bad multiple value `$withval' for --with-$1.
73 ifelse($2,[og_any],,[
74 AS_ESCAPE(OG_ARG_POSSVAL($5,[--with-$1]), [`])
75 (and remember do NOT specify 2 or 3 or 4 or more values, ONE is enough! Believe me!)
76
77 AS_ESCAPE(OG_ERROR_ADDITIONAL_VERBOSITY($1), [`])])])
78
79                         elif test "$withval" = "foo"; then
80                                 AC_MSG_ERROR([dnl
81 --with-$1=foo?! In your dreams, man!
82                         ])
83                         else
84                                 AC_MSG_ERROR([dnl
85 Bad value `$withval' for --with-$1.
86 ifelse($5,[og_any],,[
87 AS_ESCAPE(OG_ARG_POSSVAL($5,[--with-$1]), [`])
88 (and nothing else, and especially not `$withval'!)
89
90 AS_ESCAPE(OG_ERROR_ADDITIONAL_VERBOSITY($1), [`])])])
91                         fi
92                 fi])
93         with_[]og_group[]="$withval"
94         $6
95 ],[dnl
96         with_[]og_group[]=ifelse($3,,"",$3,none,"",$3,auto,"",["$3"])
97         $7])dnl
98 dnl AC_MSG_RESULT([Option Group --with-$1 $og_with_$1])
99 # end --with-$1
100         popdef([og_group])
101 ])dnl
102
103 dnl helper funs for multi arg case
104 dnl ==============================
105 dnl $1 - possible option values
106 dnl $2 - option name
107 AC_DEFUN([OG_MULTIARG_POSSVAL], [dnl
108 [Possible arguments:]ifelse($2,,,[ for $2])[]dnl
109 ifelse(dnl
110 $1,[og_any], [dnl
111 [ anything]],
112 $1,[og_file], [dnl
113 [ ]any file names],
114 $1,,[dnl
115 [ ]either `all' or `none'],[dnl
116 [ ]`all' or `none' as the first argument[,] followed by dnl
117 any (comma-separated) combination out of [`(no)]patsubst([$1],[ ],['[,] `(no)])['].])
118 ])dnl OG_MULTIARG_POSSVAL
119
120 AC_DEFUN([OG_MULTIARG_MORE_ON_POSSVALS], [dnl
121 The `no' prefix hereby means not to include the item in question. dnl
122 E.g. combinations like `all[,]nofoo[,]nobar' are allowed to select dnl
123 everything but foo and bar. dnl
124 Later options in the chain override earlier ones. dnl
125 If `all' or `none' (as first argument) in the chain are omitted dnl
126 all items are supposed to be specified relative to the default value.])
127
128 dnl
129 dnl $1 = option name
130 dnl $2 = default value, can be auto, all, none, or any combination of $3
131 dnl $3 = allowed values, can be [og_any]
132 AC_DEFUN([OG_MULTIARG_PROCESS], [dnl
133         pushdef([og_group], [translit([$1],[-],[_])])
134         new_default=
135         for og_val in $2 `echo "$withval" | sed -e 's/,/ /g'`; do
136                 og_arg=invalid
137                 case "$og_val" in
138                 dnl all and none are only permitted as the first in the list.
139                 no | none )             new_default=no ;;
140                 yes | all | auto )      new_default=yes ;;
141                 esac
142                 ifelse($3,,,[
143                         case "$og_val" in
144                         translit([$3],[ ],[|]) )
145                                 og_arg=valid
146                                 eval with_[]og_group[]_${og_val}=yes;;
147                         [no]patsubst([$3],[ ],[|no]) )
148                                 og_arg=valid
149                                 eval `echo $og_val | sed -e 's/^no/with_[]og_group[]_/'`=no
150                         esac
151                 ])
152                 if test -n "$new_default"; then
153 dnl                     for og_item in "[$3]"; do
154 dnl                             with_[]og_group[]_$og_item="$new_default"
155 dnl                     done
156                         with_[]og_group[]_[]patsubst([$3],[ ],[=$new_default; with_[]og_group[]_])[]=$new_default
157                                 new_default=
158                                 og_arg=valid
159                         fi
160                 if test "$og_arg" = "invalid" ; then
161                         AS_MESSAGE(Wait a moment ... dnl
162 Find a coffee factory! Drink it! And now listen:)
163                         AC_MSG_ERROR(dnl
164 Bad value `$og_val' for --with-$1.
165
166 m4_text_wrap(OG_MULTIARG_POSSVAL($3, [--with-$1])
167 OG_MULTIARG_MORE_ON_POSSVALS)
168
169 OG_ERROR_ADDITIONAL_VERBOSITY($1)
170 )
171                 fi
172         done
173         popdef([og_group])
174 ])
175
176 dnl helper for OG_MULTIARG_HELP_STINGS
177 dnl mimicking AS_HELP_STRING here
178 dnl we can't use AS_HELP_STRING (or m4_text_wrap) directly because
179 dnl we need $1 and $2 expanded and indirected, a bit like a lisp `
180 AC_DEFUN([_OG_MULTIARG_ITEM_EXPL], [dnl
181 [                          ][- $1 for $2]
182 ])dnl _OG_MULTIARG_ITEM_EXPL
183
184 dnl automatically generated help string
185 AC_DEFUN([OG_MULTIARG_HELP_STRINGS], [dnl
186         pushdef([OG_MULTIARG_ITEM],
187                 _OG_MULTIARG_ITEM_EXPL($[1], $[3]))
188         pushdef([OG_MULTIARG_MUTEX], [])
189 AS_HELP_STRING([], [Explanation of the items:])
190 $1
191         popdef([OG_MULTIARG_MUTEX])
192         popdef([OG_MULTIARG_ITEM])
193 ])dnl OG_MULTIARG_HELP_STRINGS
194
195 dnl multiple arg form of OG_ARG_WITH, that is it accepts any (comma separated)
196 dnl combination of args
197 dnl
198 dnl $1 = option name
199 dnl $2 = help-string
200 dnl $3 = default value, can be auto or none
201 dnl $3 = default value documentation
202 dnl $5 = allowed values [yes no] if empty, can be [og_any]
203 dnl $6 = actions in case option was set (old AC_ARG_WITH arg #3)
204 dnl $7 = actions in case option was not set (old AC_ARG_WITH arg #4)
205 dnl improved version
206 AC_DEFUN([OG_MULTIARG_WITH], [# Option Group --with-$1 (multiarg)
207         ## announce this option as valid option
208         sxe_with_options="$sxe_with_options $1"
209
210         pushdef([og_group], [translit([$1],[-],[_])])
211         pushdef([og_DefVal],ifelse($3,,auto,$3))
212         pushdef([OG_MULTIARG_ITEM], dnl
213                 _OG_MULTIARG_ITEM($1, $[1], $[2], $[3]))
214         pushdef([OG_MULTIARG_MUTEX], dnl
215                 _OG_MULTIARG_MUTEX($1, $[1], $[2], $[3], $[4]))
216         $5
217         popdef([OG_MULTIARG_ITEM])
218         popdef([OG_MULTIARG_MUTEX])
219         pushdef([OG_MULTIARG_ITEM], $[1])
220         pushdef([OG_MULTIARG_MUTEX], [])
221         pushdef([og_items],
222                 patsubst(
223                         patsubst(
224                                 patsubst(
225                                         patsubst([$5], [[        ]+], []),
226                         [
227 ], [ ]), [^ +], []), [ +$], [])) dnl autoconf is soooo brilliant, is it not? :)
228         popdef([OG_MULTIARG_MUTEX])
229         popdef([OG_MULTIARG_ITEM])
230
231         pushdef([OG_HELP_STRING], [AS_HELP_STRING($[1], $[2])])
232         pushdef([og_desc], [dnl
233 patsubst([$2], [^[      ]AS_HELP_STRING], [AS_HELP_STRING])])
234         pushdef([og_helps], [dnl
235 patsubst(patsubst(patsubst([OG_MULTIARG_HELP_STRINGS($5)], [^[  ]+], []), [[    ]+$], []), [^[  ]*
236 ], [])])
237
238         dnl I personally shoot everybody who fiddles with the whitespace here!!!!
239         AC_ARG_WITH($1, [
240 og_desc[]ifelse($5,[og_any],,[
241 AS_HELP_STRING([], OG_MULTIARG_POSSVAL(og_items))
242 AS_HELP_STRING([], OG_MULTIARG_MORE_ON_POSSVALS)
243 og_helps])[]ifelse($4,,,[dnl
244 AS_HELP_STRING([], [@<:@Default: $4@:>@])])],[
245
246         ## now process all supplied options (prepend the default opts)
247         OG_MULTIARG_PROCESS($1,og_DefVal,og_items)
248         $6
249 ],[
250         withval=
251         with_[]og_group[]="og_DefVal"
252         OG_MULTIARG_PROCESS($1,og_DefVal,og_items)
253         $7
254         # end --with-$1
255 ])
256
257         dnl AC_MSG_RESULT([Option Group --with-$1 $og_with_$1])
258         popdef([og_desc])
259         popdef([og_helps])
260         popdef([og_items])
261         popdef([og_DefVal])
262         popdef([og_group])
263 ])dnl
264
265 dnl Print a summary for OG_ARG_WITH options
266 dnl
267 dnl $1 = option name
268 dnl $2 = headline for option
269 dnl $3 = per-line indentation
270 AC_DEFUN([OG_ARG_SUMMARY], [# Option Group --with-$1 (arg)
271         result="uncertain"
272         pushdef([og_group], [translit([$1],[-],[_])])
273         pushdef([indent], [ifelse([$3],[],[],[$3])])
274
275         if test -z "$og_[]og_group[]_opts"; then
276                 if eval "test \"\$have_[]og_group\" = \"yes\" -a \
277                         \"\$with_[]og_group\" = \"yes\""; then
278                         result="yes"
279                 elif eval "test \"\$have_[]og_group\" = \"yes\" -a \
280                         \"\$with_[]og_group\" != \"yes\""; then
281                         result="yes+"
282                 elif eval "test \"\$have_[]og_group\" != \"yes\" -a \
283                         \"\$with_[]og_group\" = \"yes\""; then
284                         result="no*"
285                 else
286                         result="no"
287                 fi
288                 echo "indent[$2]: $result"
289         else
290                 echo "indent[$2]:"
291                 for og_val in `expr "$og_[]og_group[]_opts"`; do
292                         if test "$og_val" = "auto"; then
293                                 ## do not list auto as option
294                                 :
295                         elif test "$have_[]og_group" = "$og_val" -a \
296                                 "$with_[]og_group" = "auto"; then
297                                 echo "indent[  ](X) $og_val (auto)"
298                         elif test "$have_[]og_group" = "$og_val"; then
299                                 echo "indent[  ](X) $og_val"
300                         else
301                                 echo "indent[  ]( ) $og_val"
302                         fi
303                 done
304         fi
305
306         popdef([indent])
307         popdef([og_group])
308 ])
309
310 dnl Print a summary for OG_MULTIARG_WITH options
311 dnl
312 dnl $1 = option name
313 dnl $2 = headline for option
314 AC_DEFUN([OG_MULTIARG_SUMMARY], [# Option Group --with-$1 (multiarg)
315         tmp_enabled=
316         tmp_enabled_verb=
317         tmp_disabled=
318         tmp_omitted=
319         tmp_omitted_verb=
320         pushdef([og_group], [translit([$1],[-],[_])])
321         pushdef([og_items], [ifelse([$4],[],[$sxe_og_[]og_group[]_items],[$4])])
322         pushdef([og_mutices], [$sxe_og_[]og_group[]_mutices])
323         pushdef([indent], [ifelse([$3],[],[],[$3])])
324
325         for og_val in og_items; do
326                 if eval "test \"\$have_[]og_group[]_${og_val}\" = \"yes\""; then
327                         if eval "test \"\$with_[]og_group[]_${og_val}\" \
328                            != \"yes\""; then
329                                 tmp_enabled="$tmp_enabled $og_val+"
330                         else
331                                 tmp_enabled="$tmp_enabled $og_val"
332                         fi
333
334                         if eval "test -n \
335                            \"\$sxe_og_[]og_group[]_${og_val}_mutexgroup\""; then
336                                 tmp_mtx_grp=$(eval "echo \$sxe_og_[]og_group[]_${og_val}_mutexgroup")
337                                 tmp_mtx_grp_desc=$(eval "echo \$sxe_og_[]og_group[]_${tmp_mtx_grp}_mutexdesc")
338                                 tmp_enabled_verb="$tmp_enabled_verb
339 echo \"[]indent[  ]  + ${tmp_mtx_grp} (${tmp_mtx_grp_desc})\"
340 echo \"[]indent[  ]    (X) \$sxe_og_[]og_group[]_${og_val}_short \
341 (\$sxe_og_[]og_group[]_${og_val}_desc)\""
342                                 tmp_enabled_verb="$tmp_enabled_verb
343 eval \"\${sum_[]og_group[]_omitted_${tmp_mtx_grp}_verb}\""
344                                 tmp_mtx_grp=
345                                 tmp_mtx_grp_desc=
346
347                         else
348                                 tmp_enabled_verb="$tmp_enabled_verb
349 echo \"[]indent[  ]  - \$sxe_og_[]og_group[]_${og_val}_short \
350 (\$sxe_og_[]og_group[]_${og_val}_desc)\""
351                         fi
352                 elif eval "test \"\$with_[]og_group[]_${og_val}\" = \"yes\" \
353                         -a -n \"\$omit_[]og_group[]_${og_val}_in_favour_of\" \
354                         -a -n \"\$sxe_og_[]og_group[]_${og_val}_mutexgroup\""; then
355                         tmp_omitted="$tmp_omitted $og_val"
356                         tmp_mtx_grp=$(eval "echo \$sxe_og_[]og_group[]_${og_val}_mutexgroup")
357
358                         eval sum_[]og_group[]_omitted_${tmp_mtx_grp}_verb="\"\$sum_[]og_group[]_omitted_${tmp_mtx_grp}_verb
359 echo \\\"[]indent[  ]    ( ) \$sxe_og_[]og_group[]_${og_val}_short \
360 (\$sxe_og_[]og_group[]_${og_val}_desc) available but omitted\\\"\""
361                         tmp_mtx_grp=
362                 elif eval "test \"\$with_[]og_group[]_${og_val}\" = \"yes\""; then
363                         tmp_disabled="$tmp_disabled $og_val*"
364                 else
365                         tmp_disabled="$tmp_disabled $og_val"
366                 fi
367         done
368
369         if test -z "$tmp_enabled"; then
370                 tmp_enabled="None."
371         fi
372         if test -z "$tmp_disabled"; then
373                 tmp_disabled="None."
374         fi
375         if test -z "$tmp_omitted"; then
376                 tmp_omitted="None."
377         fi
378
379         echo "indent[$2]:"
380         echo "indent  Enabled [$2]: $tmp_enabled"
381         eval "$tmp_enabled_verb"
382         if test -n "$sxe_og_[]og_group[]_mutices"; then
383                 echo "indent  Omitted [$2]: $tmp_omitted"
384                 dnl eval "$tmp_omitted_verb"
385         fi
386         echo "indent  Disabled [$2]: $tmp_disabled"
387
388         popdef([indent])
389         popdef([og_mutices])
390         popdef([og_items])
391         popdef([og_group])
392 ])
393
394 AC_DEFUN([_OG_MULTIARG_ITEM], [dnl
395         # $1 option group name,
396         # $2 item name
397         # $3 short description
398         # $4 long description
399         pushdef([og_group], [translit([$1],[-],[_])])
400         sxe_og_[]og_group[]_items="$sxe_og_[]og_group[]_items $2"
401         sxe_og_[]og_group[]_[]$2[]_short="[$3]"
402         sxe_og_[]og_group[]_[]$2[]_desc="[$4]"
403         popdef([og_group])
404 ])
405
406 AC_DEFUN([_OG_MULTIARG_MUTEX], [dnl
407         # $1 option group name,
408         # $2 mutex name
409         # $3 mutex description
410         # $4 item names in descending order of favouriteness
411         pushdef([og_group], [translit([$1],[-],[_])])
412         sxe_og_[]og_group[]_mutices="$sxe_og_[]og_group[]_mutices $2"
413         sxe_og_[]og_group[]_[]$2[]_mutexdesc="[$3]"
414         sxe_og_[]og_group[]_[]$2[]_mutexitems="[$4]"
415         for og_item in [$4]; do
416                 ## bind each mutex item to name the mutex group it belongs to
417                 eval "sxe_og_[]og_group[]_${og_item}_mutexgroup=\"[$2]\""
418         done
419         popdef([og_group])
420 ])
421
422 AC_DEFUN([OG_CHECK_OPTION], [## arg #1 is with or enable, arg #2 the option
423         pushdef([og_group], [$1])
424         __foundp="nil"
425         for __opt in $sxe_[]og_group[]_options; do
426                 if test "[$2]" = "$__opt"; then
427                         __foundp="t"
428                         break
429                 fi
430         done
431         if test "$__foundp" != "nil"; then
432                 [$3]
433                 :
434         else
435                 [$4]
436                 :
437         fi
438         popdef([og_group])
439 ])
440
441
442 AC_DEFUN([OG_WARN_OBSOLETE_OPTION], [dnl
443         ## arg1 is the --with option
444         ## arg2 is the instead string
445         ## arg3 is the penalty time, 10 seconds by default
446         pushdef([OG_OPT], [$1])
447         pushdef([OG_INSTEAD], [$2])
448         pushdef([OG_PENALTY_TIME], ifelse($3,,10,$3))
449         pushdef([OG_PENALTY_TIME_UNIT], ifelse(OG_PENALTY_TIME,1,[second],[seconds]))
450
451         AC_MSG_WARN([You've just been caught using an obsolete option.])
452         AC_MSG_WARN([Listen, mate, thou shalt not use ]OG_OPT[,])
453         AC_MSG_WARN([as it will disappear soon and thence cause more trouble.])
454         AC_MSG_WARN([To migrate cleanly you should use instead:]
455         OG_INSTEAD[])
456         echo ""
457         AC_MSG_WARN([To substantiate my seriousness I will go on strike for ]
458 OG_PENALTY_TIME[ ]OG_PENALTY_TIME_UNIT[ now!])
459         sleep OG_PENALTY_TIME
460         echo ""
461
462         popdef([OG_OPT])
463         popdef([OG_INSTEAD])
464         popdef([OG_PENALTY_TIME])
465         popdef([OG_PENALTY_TIME_UNIT])
466 ])dnl OG_WARN_OBSOLETE_OPTION