diff --git a/ChangeLog b/ChangeLog
index c80196c..3e89927 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2007-11-19  Akim Demaille  <demaille@gostai.com>
+
+	Introduce and use the %KEY?TRUE:FALSE% template token.
+	* automake.in (&transform_token): New.
+	(&transform): Use it.
+	Support %KEY?TRUE:FALSE%.
+	(&make_paragraphs): Adjust the regexp to call %transform.
+	* lib/am/data.am, lib/am/libs.am, lib/am/lisp.am, lib/am/ltlib.am,
+	* lib/am/progs.am, lib/am/python.am, lib/am/scripts.am:
+	Use %KEY?TRUE:FALSE%.
+
 2007-11-13  Bob Proulx  <bob@proulx.com>
 
 	Fix color test failure on dumb (and other) terminals.
diff --git a/automake.in b/automake.in
index 3b96f1b..8893b84 100755
--- a/automake.in
+++ b/automake.in
@@ -11,7 +11,7 @@ eval 'case $# in 0) exec @PERL@ -S "$0";; *) exec @PERL@ -S "$0" "$@";; esac'
 
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 3, or (at your option)
+# the Free Software Foundation; either version 2, or (at your option)
 # any later version.
 
 # This program is distributed in the hope that it will be useful,
@@ -6399,38 +6399,62 @@ sub flatten
   return $_;
 }
 
+
+# transform_token ($TOKEN, \%PAIRS, $KEY)
+# =======================================
+# Return the value associated to $KEY in %PAIRS, as used on $TOKEN
+# (which should be ?KEY? or any of the special %% requests)..
+sub transform_token ($$$)
+{
+  my ($token, $transform, $key) = @_;
+  my $res = $transform->{$key};
+  prog_error "Unknown key `$key' in `$token'" unless defined $res;
+  return $res;
+}
+
+
 # transform($TOKEN, \%PAIRS)
-# ==========================
+# ===========================
 # If ($TOKEN, $VAL) is in %PAIRS:
-#   - replaces %$TOKEN% with $VAL,
-#   - enables/disables ?$TOKEN? and ?!$TOKEN?,
-#   - replaces %?$TOKEN% with TRUE or FALSE.
+#   - replaces %KEY% with $VAL,
+#   - enables/disables ?KEY? and ?!KEY?,
+#   - replaces %?KEY% with TRUE or FALSE.
+#   - replaces %KEY?IFTRUE%, %KEY:IFFALSE%, and %KEY?IFTRUE:IFFALSE% with
+#     IFTRUE / IFFALSE, as appropriate.
 sub transform($$)
 {
   my ($token, $transform) = @_;
 
-  if (substr ($token, 0, 1) eq '%')
-    {
-      my $cond = (substr ($token, 1, 1) eq '?') ? 1 : 0;
-      $token = substr ($token, 1 + $cond, -1);
-      my $val = $transform->{$token};
-      prog_error "Unknown %token% `$token'" unless defined $val;
-      if ($cond)
+  # %KEY%.
+  # Must be before the following pattern to exclude the case
+  # when there is neither IFTRUE nor IFFALSE.
+  if ($token =~ /^%([\w\-]+)%$/)
 	{
-	  return $val ? 'TRUE' : 'FALSE';
+      return transform_token ($token, $transform, $1);
 	}
-      else
+  # %KEY?IFTRUE%, %KEY:IFFALSE%, and %KEY?IFTRUE:IFFALSE%.
+  elsif ($token =~ /^%([\w\-]+)(?:\?([^?:%]+))?(?::([^?:%]+))?%$/)
 	{
-	  return $val;
+      return transform_token ($token, $transform, $1) ? ($2 || '') : ($3 || '');
 	}
+  # %?KEY%.
+  elsif ($token =~ /^%\?([\w\-]+)%$/)
+    {
+      return transform_token ($token, $transform, $1) ? 'TRUE' : 'FALSE';
     }
-  # Now $token is '?xxx?' or '?!xxx?'.
-  my $neg = (substr ($token, 1, 1) eq '!') ? 1 : 0;
-  $token = substr ($token, 1 + $neg, -1);
-  my $val = $transform->{$token};
-  prog_error "Unknown ?token? `$token' (neg = $neg)" unless defined $val;
+  # ?KEY? and ?!KEY?.
+  elsif ($token =~ /^ \? (!?) ([\w\-]+) \? $/x)
+    {
+      my $neg = ($1 eq '!') ? 1 : 0;
+      my $val = transform_token ($token, $transform, $2);
   return (!!$val == $neg) ? '##%' : '';
 }
+  else
+    {
+      prog_error "Unknown request format: $token";
+    }
+}
+
 
 # @PARAGRAPHS
 # &make_paragraphs ($MAKEFILE, [%TRANSFORM])
@@ -6492,7 +6516,10 @@ sub make_paragraphs ($%)
     }
 
   # Substitute Automake template tokens.
-  s/(?:%\??[\w\-]+%|\?!?[\w\-]+\?)/transform($&, \%transform)/ge;
+  s/(?: % \?? [\w\-]+ %
+      | % [\w\-]+ (?:\?[^?:%]+)? (?::[^?:%]+)? %
+      | \? !? [\w\-]+ \?
+    )/transform($&, \%transform)/gex;
   # transform() may have added some ##%-comments to strip.
   # (we use `##%' instead of `##' so we can distinguish ##%##%##% from
   # ####### and do not remove the latter.)
diff --git a/lib/am/data.am b/lib/am/data.am
index eea2ef2..2b762f0 100644
--- a/lib/am/data.am
+++ b/lib/am/data.am
@@ -1,5 +1,5 @@
 ## automake - create Makefile.in from Makefile.am
-## Copyright (C) 1994, 1995, 1996, 1998, 1999, 2001, 2003, 2004, 2006
+## Copyright (C) 1994, 1995, 1996, 1998, 1999, 2001, 2003, 2004, 2006, 2007
 ## Free Software Foundation, Inc.
 
 ## This program is free software; you can redistribute it and/or modify
@@ -37,15 +37,13 @@ install-%DIR%%PRIMARY%: $(%DIR%_%PRIMARY%)
 ?!BASE?	@$(am__vpath_adj_setup) \
 ## Funny invocation because Makefile variable can be empty, leading to
 ## a syntax error in sh.
-?!BASE?	list='$(%DIR%_%PRIMARY%)'; for p in $$list; do \
-?BASE?	@list='$(%DIR%_%PRIMARY%)'; for p in $$list; do \
+	%BASE?@%list='$(%DIR%_%PRIMARY%)'; for p in $$list; do \
 ## A file can be in the source directory or the build directory.
 	  if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \
 ## If the _%PRIMARY% variable has an entry like foo/bar, install it as
 ## $(destdir)/bar, not $(destdir)/foo/bar.  The user can make a
 ## new dir variable or use a nobase_ target for the latter case.
-?BASE?	  f=$(am__strip_dir) \
-?!BASE?	  $(am__vpath_adj) \
+	  %BASE?f=$(am__strip_dir):$(am__vpath_adj)% \
 	  echo " $(%DIR%%PRIMARY%_INSTALL) '$$d$$p' '$(DESTDIR)$(%NDIR%dir)/$$f'"; \
 	  $(%DIR%%PRIMARY%_INSTALL) "$$d$$p" "$(DESTDIR)$(%NDIR%dir)/$$f"; \
 	done
@@ -61,10 +59,8 @@ if %?INSTALL%
 uninstall-%DIR%%PRIMARY%:
 	@$(NORMAL_UNINSTALL)
 ?!BASE?	@$(am__vpath_adj_setup) \
-?!BASE?	list='$(%DIR%_%PRIMARY%)'; for p in $$list; do \
-?BASE?	@list='$(%DIR%_%PRIMARY%)'; for p in $$list; do \
-?BASE?	  f=$(am__strip_dir) \
-?!BASE?	  $(am__vpath_adj) \
+	%BASE?@%list='$(%DIR%_%PRIMARY%)'; for p in $$list; do \
+	  %BASE?f=$(am__strip_dir):$(am__vpath_adj)% \
 	  echo " rm -f '$(DESTDIR)$(%NDIR%dir)/$$f'"; \
 	  rm -f "$(DESTDIR)$(%NDIR%dir)/$$f"; \
 	done
diff --git a/lib/am/libs.am b/lib/am/libs.am
index 505f7f8..18d203d 100644
--- a/lib/am/libs.am
+++ b/lib/am/libs.am
@@ -1,5 +1,5 @@
 ## automake - create Makefile.in from Makefile.am
-## Copyright (C) 1994, 1995, 1996, 1998, 1999, 2001, 2002, 2003, 2004, 2006
+## Copyright (C) 1994, 1995, 1996, 1998, 1999, 2001, 2002, 2003, 2004, 2006, 2007
 ## Free Software Foundation, Inc.
 
 ## This program is free software; you can redistribute it and/or modify
@@ -27,10 +27,8 @@ endif %?INSTALL%
 
 if %?INSTALL%
 am__installdirs += "$(DESTDIR)$(%NDIR%dir)"
-?BASE?%DIR%LIBRARIES_INSTALL = $(INSTALL_DATA)
-?!BASE?%DIR%LIBRARIES_INSTALL = $(install_sh_DATA)
-?EXEC?.PHONY install-exec-am: install-%DIR%LIBRARIES
-?!EXEC?.PHONY install-data-am: install-%DIR%LIBRARIES
+%DIR%LIBRARIES_INSTALL = %BASE?$(INSTALL_DATA):$(install_sh_DATA)%
+.PHONY install-%EXEC?exec:data%-am: install-%DIR%LIBRARIES
 install-%DIR%LIBRARIES: $(%DIR%_LIBRARIES)
 	@$(NORMAL_INSTALL)
 	test -z "$(%NDIR%dir)" || $(MKDIR_P) "$(DESTDIR)$(%NDIR%dir)"
diff --git a/lib/am/lisp.am b/lib/am/lisp.am
index 715412e..d699da4 100644
--- a/lib/am/lisp.am
+++ b/lib/am/lisp.am
@@ -1,5 +1,5 @@
 ## automake - create Makefile.in from Makefile.am
-## Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
+## Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
 ## Free Software Foundation, Inc.
 
 ## This program is free software; you can redistribute it and/or modify
@@ -82,10 +82,8 @@ $(am__ELCFILES): elc-stamp
 
 if %?INSTALL%
 am__installdirs += "$(DESTDIR)$(%NDIR%dir)"
-?BASE?%DIR%LISP_INSTALL = $(INSTALL_DATA)
-?!BASE?%DIR%LISP_INSTALL = $(install_sh_DATA)
-?EXEC?.PHONY install-exec-am: install-%DIR%LISP
-?!EXEC?.PHONY install-data-am: install-%DIR%LISP
+%DIR%LISP_INSTALL = %BASE?$(INSTALL_DATA):$(install_sh_DATA)%
+.PHONY install-%EXEC?exec:data%-am: install-%DIR%LISP
 install-%DIR%LISP: $(%DIR%_LISP) $(ELCFILES)
 	@$(NORMAL_INSTALL)
 ## Do not install anything if EMACS was not found.
@@ -97,8 +95,7 @@ install-%DIR%LISP: $(%DIR%_LISP) $(ELCFILES)
 	  list='$(%DIR%_LISP)'; for p in $$list; do \
 ## A lisp file can be in the source directory or the build directory.
 	    if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \
-?BASE?	    f=$(am__strip_dir) \
-?!BASE?	    $(am__vpath_adj) \
+	    %BASE?f=$(am__strip_dir):$(am__vpath_adj)% \
 	    echo " $(%DIR%LISP_INSTALL) '$$d$$p' '$(DESTDIR)$(%NDIR%dir)/$$f'"; \
 	    $(%DIR%LISP_INSTALL) "$$d$$p" "$(DESTDIR)$(%NDIR%dir)/$$f"; \
 ## Only install .elc file if it exists.
@@ -123,8 +120,7 @@ uninstall-%DIR%LISP:
 	@if test "$(EMACS)" != no; then \
 ?!BASE?	  $(am__vpath_adj_setup) \
 	  list='$(%DIR%_LISP)'; for p in $$list; do \
-?BASE?	    f=$(am__strip_dir) \
-?!BASE?	    $(am__vpath_adj) \
+	    %BASE?f=$(am__strip_dir):$(am__vpath_adj)% \
 	    echo " rm -f '$(DESTDIR)$(%NDIR%dir)/$$f' '$(DESTDIR)$(%NDIR%dir)/$${f}c'"; \
 	    rm -f "$(DESTDIR)$(%NDIR%dir)/$$f" "$(DESTDIR)$(%NDIR%dir)/$${f}c"; \
 	  done; \
diff --git a/lib/am/ltlib.am b/lib/am/ltlib.am
index e440772..a16f71b 100644
--- a/lib/am/ltlib.am
+++ b/lib/am/ltlib.am
@@ -27,10 +27,8 @@ endif %?INSTALL%
 
 if %?INSTALL%
 am__installdirs += "$(DESTDIR)$(%NDIR%dir)"
-?BASE?%DIR%LTLIBRARIES_INSTALL = $(INSTALL)
-?!BASE?%DIR%LTLIBRARIES_INSTALL = $(install_sh) -c
-?EXEC?.PHONY install-exec-am: install-%DIR%LTLIBRARIES
-?!EXEC?.PHONY install-data-am: install-%DIR%LTLIBRARIES
+%DIR%LTLIBRARIES_INSTALL = %BASE?$(INSTALL):$(install_sh) -c%
+.PHONY install-%EXEC?exec:data%-am: install-%DIR%LTLIBRARIES
 install-%DIR%LTLIBRARIES: $(%DIR%_LTLIBRARIES)
 	@$(NORMAL_INSTALL)
 	test -z "$(%NDIR%dir)" || $(MKDIR_P) "$(DESTDIR)$(%NDIR%dir)"
diff --git a/lib/am/progs.am b/lib/am/progs.am
index 98662fe..c721b42 100644
--- a/lib/am/progs.am
+++ b/lib/am/progs.am
@@ -24,10 +24,8 @@
 
 if %?INSTALL%
 am__installdirs += "$(DESTDIR)$(%NDIR%dir)"
-?BASE?%DIR%PROGRAMS_INSTALL = $(INSTALL_PROGRAM)
-?!BASE?%DIR%PROGRAMS_INSTALL = $(install_sh_PROGRAM)
-?EXEC?.PHONY install-exec-am: install-%DIR%PROGRAMS
-?!EXEC?.PHONY install-data-am: install-%DIR%PROGRAMS
+%DIR%PROGRAMS_INSTALL = %BASE?$(INSTALL_PROGRAM):$(install_sh_PROGRAM)%
+.PHONY install-%EXEC?exec:data%-am: install-%DIR%PROGRAMS
 install-%DIR%PROGRAMS: $(%DIR%_PROGRAMS)
 	@$(NORMAL_INSTALL)
 	test -z "$(%NDIR%dir)" || $(MKDIR_P) "$(DESTDIR)$(%NDIR%dir)"
diff --git a/lib/am/python.am b/lib/am/python.am
index a55f319..fae49aa 100644
--- a/lib/am/python.am
+++ b/lib/am/python.am
@@ -1,5 +1,5 @@
 ## automake - create Makefile.in from Makefile.am
-## Copyright (C) 1999, 2001, 2003, 2004, 2006 Free Software Foundation, Inc.
+## Copyright (C) 1999, 2001, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
 
 ## This program is free software; you can redistribute it and/or modify
 ## it under the terms of the GNU General Public License as published by
@@ -26,24 +26,20 @@ endif %?INSTALL%
 
 if %?INSTALL%
 am__installdirs += "$(DESTDIR)$(%NDIR%dir)"
-?BASE?%DIR%PYTHON_INSTALL = $(INSTALL_DATA)
-?!BASE?%DIR%PYTHON_INSTALL = $(install_sh_DATA)
-?EXEC?.PHONY install-exec-am: install-%DIR%PYTHON
-?!EXEC?.PHONY install-data-am: install-%DIR%PYTHON
+%DIR%PYTHON_INSTALL = %BASE?$(INSTALL_DATA):$(install_sh_DATA)%
+.PHONY install-%EXEC?exec:data%-am: install-%DIR%PYTHON
 install-%DIR%PYTHON: $(%DIR%_PYTHON)
 	@$(NORMAL_INSTALL)
 	test -z "$(%NDIR%dir)" || $(MKDIR_P) "$(DESTDIR)$(%NDIR%dir)"
 ?!BASE?	@$(am__vpath_adj_setup) \
-?!BASE?	list='$(%DIR%_PYTHON)'; dlist=''; for p in $$list; do\
-?BASE?	@list='$(%DIR%_PYTHON)'; dlist=''; for p in $$list; do\
+	%BASE?@%list='$(%DIR%_PYTHON)'; dlist=''; for p in $$list; do\
 ## A file can be in the source directory or the build directory.
 	  if test -f "$$p"; then b=; else b="$(srcdir)/"; fi; \
 	  if test -f $$b$$p; then \
 ## Compute basename of source file.  Unless this is a nobase_ target, we
 ## want to install 'python/foo.py' as '$(DESTDIR)$(%NDIR%dir)/foo.py',
 ## not '$(DESTDIR)$(%NDIR%dir)/python/foo.py'.
-?BASE?	    f=$(am__strip_dir) \
-?!BASE?	    $(am__vpath_adj) \
+	    %BASE?f=$(am__strip_dir):$(am__vpath_adj)% \
 	    dlist="$$dlist $$f"; \
 ## Don't perform translation, since script name is important.
 	    echo " $(%DIR%PYTHON_INSTALL) '$$b$$p' '$(DESTDIR)$(%NDIR%dir)/$$f'"; \
@@ -71,10 +67,8 @@ if %?INSTALL%
 uninstall-%DIR%PYTHON:
 	@$(NORMAL_UNINSTALL)
 ?!BASE?	@$(am__vpath_adj_setup) \
-?!BASE?	list='$(%DIR%_PYTHON)'; dlist=''; for p in $$list; do\
-?BASE?	@list='$(%DIR%_PYTHON)'; dlist=''; for p in $$list; do\
-?BASE?	  f=$(am__strip_dir) \
-?!BASE?	  $(am__vpath_adj) \
+	%BASE?@%list='$(%DIR%_PYTHON)'; dlist=''; for p in $$list; do\
+	  %BASE?f=$(am__strip_dir):$(am__vpath_adj)% \
 	  rm -f "$(DESTDIR)$(%NDIR%dir)/$$f"; \
 ## This is to remove the .pyc and .pyo byte compiled versions (a bit
 ## of a hack).
diff --git a/lib/am/scripts.am b/lib/am/scripts.am
index 0e11c80..a2ab18f 100644
--- a/lib/am/scripts.am
+++ b/lib/am/scripts.am
@@ -1,5 +1,5 @@
 ## automake - create Makefile.in from Makefile.am
-## Copyright (C) 1994, 1995, 1996, 1998, 1999, 2001, 2003, 2004, 2006
+## Copyright (C) 1994, 1995, 1996, 1998, 1999, 2001, 2003, 2004, 2006, 2007
 ## Free Software Foundation, Inc.
 
 ## This program is free software; you can redistribute it and/or modify
@@ -30,18 +30,15 @@ endif %?INSTALL%
 if %?INSTALL%
 ## if doesn't work properly for Automake variables yet.
 am__installdirs += "$(DESTDIR)$(%NDIR%dir)"
-?BASE?%DIR%SCRIPT_INSTALL = $(INSTALL_SCRIPT)
-?!BASE?%DIR%SCRIPT_INSTALL = $(install_sh_SCRIPT)
-?EXEC?.PHONY install-exec-am: install-%DIR%SCRIPTS
-?!EXEC?.PHONY install-data-am: install-%DIR%SCRIPTS
+%DIR%SCRIPT_INSTALL = %BASE?$(INSTALL_SCRIPT):$(install_sh_SCRIPT)%
+.PHONY install-%EXEC?exec:data%-am: install-%DIR%SCRIPTS
 install-%DIR%SCRIPTS: $(%DIR%_SCRIPTS)
 	@$(NORMAL_INSTALL)
 	test -z "$(%NDIR%dir)" || $(MKDIR_P) "$(DESTDIR)$(%NDIR%dir)"
 ?!BASE?	@$(am__vpath_adj_setup) \
 ## Funny invocation because Makefile variable can be empty, leading to
 ## a syntax error in sh.
-?!BASE?	list='$(%DIR%_SCRIPTS)'; for p in $$list; do \
-?BASE?	@list='$(%DIR%_SCRIPTS)'; for p in $$list; do \
+	%BASE?@%list='$(%DIR%_SCRIPTS)'; for p in $$list; do \
 ?!BASE?	  $(am__vpath_adj) p=$$f; \
 ## A file can be in the source directory or the build directory.
 	  if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \
@@ -70,8 +67,7 @@ if %?INSTALL%
 uninstall-%DIR%SCRIPTS:
 	@$(NORMAL_UNINSTALL)
 ?!BASE?	@$(am__vpath_adj_setup) \
-?!BASE?	list='$(%DIR%_SCRIPTS)'; for p in $$list; do \
-?BASE?	@list='$(%DIR%_SCRIPTS)'; for p in $$list; do \
+	%BASE?@%list='$(%DIR%_SCRIPTS)'; for p in $$list; do \
 ?!BASE?	  $(am__vpath_adj) p=$$f; \
 ## Remove any leading directory before applying $(transform).
 	  f=`echo "$$p" | sed 's|^.*/||;$(transform)'`; \
