Skip to content
Snippets Groups Projects
toolbar-x.el 81 KiB
Newer Older
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
		   val
		 (if (integerp val)
		     (list val)
		   (list 1)))
	     ;; then, type is radio
	     (if (integerp val)
		 val
	       (if (and val
			(listp val)
			(integerp (car val)))
		   (car val)
		 1)))))
    ;; === buiding `list-strings' and `list-buttons' ===
    ;; if only symbols, build `list-strings' and `list-buttons' from symbols
    (if (let ((only-symbols-flag t))
	  (dolist (i dropdown-list only-symbols-flag)
	    (setq only-symbols-flag (and only-symbols-flag (symbolp i)))))
	(let ((count 0))
	  (dolist (i dropdown-list)
	    ;; list-strings and list-buttons are buildt reversed
	    (setq list-strings (cons (toolbarx-make-string-from-symbol i)
				     list-strings))
	    (setq count (1+ count))
	    (setq list-buttons (cons (list i
					   :insert
					   (if (eq type 'radio)
					       (list 'eq count variable)
					     (list 'memq count variable)))
				     list-buttons))))
      ;; if not, the it must start with string
      (unless (stringp (car dropdown-list))
	(error "%s %s %s"
	       "If not all itens on dropdown are symbols, then a string"
	       "must come before each set of buttons; no string found"
	       "in first position."))
      (let ((count 0)
	    (elem)
	    (temp-list-buttons))
	(while dropdown-list
	  (setq elem (car dropdown-list))
	  (setq dropdown-list (cdr dropdown-list))
	  (if (stringp elem)
	      ;; if string, output `temp-list-buttons' and prepair it again
	      (progn
		;; list-strings and list-buttons are buildt reversed
		(setq list-strings (cons elem list-strings))
		(when temp-list-buttons
		  (setq list-buttons (cons (append (nreverse temp-list-buttons)
						   (list :insert
							 (if (eq type 'radio)
							     (list 'eq count
								   variable)
							   (list 'memq count
								 variable))))
					   list-buttons)))
		(setq temp-list-buttons nil)
		(setq count (1+ count)))
	    ;; else, if not string, just insert it to `temp-list-buttons'
	    ;; which is also buildt reversed
	    (setq temp-list-buttons (cons elem temp-list-buttons))))
	;; output last temp list, left behind
	(when temp-list-buttons
	  (setq list-buttons (cons (append (nreverse
					    temp-list-buttons)
					   (list
					    :insert (if (eq type 'radio)
							(list 'eq count
							      variable)
						      (list 'memq count
							    variable))))
				   list-buttons)))))
    ;; lists were made reversed (elements inserted at the beginning)
    (setq list-strings (nreverse list-strings))
    (setq list-buttons (nreverse list-buttons))
    ;; now, pass `list-buttons' as a group to `toolbarx-process-group'
    (let ((current-switches switches))
      (setq current-switches
	    (toolbarx-process-group list-buttons meaning-alist
				    merged-props ; pass non-processed props
				    current-switches))
      (setq current-switches
	    ;; outputing dropdown button
	    (toolbarx-process-group (append dropdown-button-without-command
					    (list :command
						  (toolbarx-mount-popup-menu
						   list-strings variable type
						   title save)))
				    meaning-alist merged-props-button-only
				    switches))
      current-switches)))



;; Still functions `toolbarx-install-toolbar' and `toolbarx-refresh'to
;; complete the parsing engine.	 Since they interface with other engines,
;; they must come in the end.

;;; How a image is made, giving a string as (part of) file name.

;; look at function `image-type-available-p' for Emacs !!!!

(defun toolbarx-find-image (image)
  "Return image descriptor or glyph for IMAGE.
In Emacs, return an image descriptor for IMAGE.  In XEmacs,
return a glyph.

IMAGE is string.  Usually IMAGE neither contains a directory nor
an extension.  If the extension is omitted, `xpm', `xbm' and
`pbm' are tried.  If the directory is omitted,
`toolbarx-image-path' is searched."
  ;; `find-image' in Emacs 21 looks in `load-path' and `data-directory'.  In
  ;; Emacs 22, we have `image-load-path' which includes `load-path' and
  ;; `data-directory'.
  ;;
  ;; If there's some API in XEmacs to find the images, we should use it
  ;; instead of locate-library.
  ;;
  ;; Emacs 22 has locate-file, but the other Emacsen don't.  The
  ;; following should hopefully get us to all images ultimately.

  (let ((file))
    (dolist (i '("" ".xpm" ".xbm" ".pbm"))
      (unless file
	(setq file (locate-library (concat image i) t toolbarx-image-path))))
    (if (featurep 'xemacs)
	(and file (make-glyph file))
      (if file
	  (create-image file)
	(find-image `((:type xpm :file ,(concat image ".xpm"))
		      (:type xbm :file ,(concat image ".xbm"))
		      (:type pbm :file ,(concat image ".pbm"))))))))

;; next variable interfaces between parsing and display engines
(defvar toolbarx-internal-button-switches nil
  "Store the list of processed buttons, used by `toolbarx-refresh'.
This variable can store different values for the different buffers.")


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Second engine: display parsed buttons in Emacs

(defun toolbarx-emacs-add-button (button used-keys keymap)
  "Insert a button where BUTTON is its description.
USED-KEYS should be a list of symbols, where the first element is
`:used-symbols'.  This list should store the symbols of the
buttons already inserted.  This list is changed by side effect.
KEYMAP is the keymap where the menu-item corresponding to the
tool-bal button is going to be inserted.  Insertion is made in
the end of KEYMAP.

BUTTON should be a list of form (SYMBOL . PROP-LIST).  SYMBOL is
a symbol that \"names\" this button.  PROP-LIST is a list in the
format (PROP VAL ... PROP VAL).	 The supported properties are
`:image', `:command', `:append-command', `:prepend-command',
`:help', `:enable', `:visible', `:button', `:insert' and
`:toolbar'. For a description of properties, see documentation of
function `toolbar-install-toolbar'."
  (let* ((symbol (nth 0 button))
	 (used-keys-list (when used-keys
			   (cdr used-keys)))
	 (filtered-props
	  (let* ((filtered-props-temp)
		 (prop-good-val)
		 (prop))
	    (dolist (p (nth 0 toolbarx-button-props) filtered-props-temp)
	      ;;    property	       -> (car p)
	      ;;    test type function -> (cadr p)
	      ;;    add-function       -> (cddr p)
	      (setq prop (memq (car p) button))
	      ;; if so, check if value is of correct type
	      (when prop
		;; if property is of add-type, them the value is a list
		;; (:add-value-list VAL VAL). Each VAL should be checked.
		(if (and (cddr p) (eq :add-value-list (car (cadr prop))))
		    (let* ((add-list (list (cddr p))))
		      (dolist (val (cdr (cadr prop)))
			(setq prop-good-val (funcall (cadr p) val))
			(when (car prop-good-val)
			  (setq add-list (cons (cdr prop-good-val) add-list))))
		      (setq add-list (nreverse add-list))
		      (when (eq 2 (length add-list)) ; just 1 value, no
						     ; add-function
			(setq add-list (cadr add-list)))
		      (setq filtered-props-temp (append
						 (list (car p) add-list)
						 filtered-props-temp)))
		  ;; if override-property
		  (setq prop-good-val (funcall (cadr p) (cadr prop)))
		  (when (car prop-good-val)
		    (setq filtered-props-temp (append
					       (list (car p)
						     (cdr prop-good-val))
					       filtered-props-temp))))))))
	 (insert (or (not (memq :insert filtered-props))
		     ;; (memq :insert filtered-props)
		     (eval (nth 1 (memq :insert filtered-props))))))
    (when insert
      (cond
       (t
	;; symbol is not :new-line, therefore a normal button
	(let* ((image (cadr (memq :image filtered-props)))
	       (image-descriptor
		(when (memq :image filtered-props)
		  (cond
		   ((stringp image) 	; string
		    (toolbarx-find-image image))
		   ((and (consp image)	; or image descriptor
			 (eq (car image) 'image))
		    image)
		   ((and (symbolp image) ; or a symbol bound to a
			 (boundp image)  ; image descriptor (defined
				       ; with `defimage')g
			 (consp (eval image))
			 (eq (car (eval image)) 'image))
		    (eval image))
		   (t			; otherwise, must be a list
					; with 4 strings or image
					; descriptors
		    (apply 'vector (mapcar (lambda (img)
					      (if (stringp img)
						  (toolbarx-find-image img)
						img))
					   image))))))
	       (command
		(let* ((com (nth 1 (memq :command filtered-props)))
		       (app (nth 1 (memq :append-command filtered-props)))
		       (prep (nth 1 (memq :prepend-command filtered-props))))
		  (when (or com app prep)
		    (toolbarx-make-command com prep app))))
	       (help (cons (memq :help filtered-props)
			   (cadr (memq :help filtered-props))))
	       (enable (cons (memq :enable filtered-props)
			     (cadr (memq :enable filtered-props))))
	       (visible (cons (memq :visible filtered-props)
			      (cadr (memq :visible filtered-props))))
	       (button (cons (memq :button filtered-props)
			     (cadr (memq :button filtered-props))))
	       (menuitem (append
			  (list 'menu-item
				(toolbarx-make-string-from-symbol symbol)
				command
				:image image-descriptor)
			  (when (car help)
			    (list :help (cdr help)))
			  (when (car enable)
			    (list :enable (cdr enable)))
			  (when (car visible)
			    (list :visible (cdr visible)))
			  (when (car button)
			    (list :button (cdr button)))))
	       (key-not-used
		(let* ((count 0)
		       (symb symbol))
		  (while (memq symb used-keys-list)
		    (setq count (1+ count))
		    (setq symb (intern (format "%s-%d" symbol count))))
		  symb)))
	  (when (and image-descriptor command)
	    (setq used-keys-list (cons key-not-used used-keys-list))
	    (define-key-after keymap
	      (vector key-not-used) menuitem))))))
    (when used-keys (setcdr used-keys used-keys-list))))


(defun toolbarx-emacs-refresh-process-button-or-insert-list (switches
							     used-keys
							     keymap)
  "Process SWITCHES, inserting buttons in `tool-bar-map'.
If a button is actually a `:insert' clause group (if `car' is
`:insert') and evaluation of `cdr' yields non-nil, process `cddr'
recursively as SWITCHES.  USED-KEYS is a list which `car' is
`:used-symbols' and which `cdr' is a list of symbols that have already
been used as keys in the keymap `tool-bar-map'."
  (dolist (button switches)
    (if (eq (car button) :insert)
	(when (eval (cadr button))
	  (toolbarx-emacs-refresh-process-button-or-insert-list (cddr button)
								used-keys
								keymap))
      (toolbarx-emacs-add-button button used-keys keymap))))



(defun toolbarx-emacs-refresh (&optional global-flag)
  "Refresh and redraw the toolbar in Emacs.
If GLOBAL-FLAG is non-nil, the default value of toolbar switches
is used and the default value of `toolbarx-map' is changed."
  (let* ((switches (if global-flag
		       (if (default-boundp 'toolbarx-internal-button-switches)
			   (default-value 'toolbarx-internal-button-switches)
			 toolbarx-internal-button-switches)
		     toolbarx-internal-button-switches))
	 (used-keys (list :used-symbols nil))
	 (tool-bar-map-temp (make-sparse-keymap)))
    (toolbarx-emacs-refresh-process-button-or-insert-list switches used-keys
							  tool-bar-map-temp)
    (if global-flag
	(setq-default tool-bar-map tool-bar-map-temp)
      (setq tool-bar-map tool-bar-map-temp))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Third engine: display parsed buttons in XEmacs

(defun toolbarx-xemacs-image-properties (image)
  "Return a list of properties of IMAGE.
IMAGE should be a string or a list of one to six strings or
glyphs or nil, or a symbol bound to a list of one to six
glyphs (them must be a valid image list, like one created with
the function `toolbar-make-button-list').  Return a
list (GLYPH-LIST HEIGHT WIDTH) where HEIGHT (resp. WIDTH) is the
maximum of the heights (resp. widths) of all glyphs (or strings
converted to glyphs) in GLYPH-LIST.  If IMAGE is not a list, it
is treated as a list with IMAGE as only element.  Strings are
converted to glyphs with the function `toolbarx-find-image'.  If,
after possible string-to-glyph convertions, the list of glyphs
has nil as first element, GLYPH-LIST becomes nil."
  (let* ((glyph-list
	  (if (symbolp image)		; if symbol, them must be a
					; valid image list, like
					; created by function
					; `toolbar-make-button-list'
	      (eval image)
	    (let ((img-list (if (listp image)
				image
			      (list image)))
		  (glyph-list-temp))
	      ;; glyph-list-temp
	      (setq glyph-list-temp
		    (dolist (glyph img-list (nreverse glyph-list-temp))
		      (if (stringp glyph)
			  (setq glyph-list-temp
				(cons (toolbarx-find-image glyph)
				      glyph-list-temp))
			(setq glyph-list-temp (cons glyph glyph-list-temp)))))
	      (unless (car glyph-list-temp)
		(setq glyph-list-temp nil))
	      glyph-list-temp)))
	 (usable-buttons
	  ;; computing inheritage
	  (let* ((usable-temp))
	    (if toolbar-captioned-p	; problematic point :-(
		(progn
		  ;; CAP-UP:  cap-up -> up
		  (setq usable-temp (cons (cond
					   ((nth 3 glyph-list))
					   ((nth 0 glyph-list)))
					  usable-temp))
		  ;; CAP-DOWN:	cap-down -> cap-up -> down -> up
		  (setq usable-temp (cons (cond
					   ((nth 4 glyph-list))
					   ((nth 3 glyph-list))
					   ((nth 1 glyph-list))
					   ((nth 0 glyph-list)))
					  usable-temp))
		  ;; CAP-DISABLED:  cap-disabled -> cap-up -> disabled -> up
		  (setq usable-temp (cons (cond
					   ((nth 5 glyph-list))
					   ((nth 3 glyph-list))
					   ((nth 2 glyph-list))
					   ((nth 0 glyph-list)))
					  usable-temp)))
	      ;; UP:  up
	      (setq usable-temp (cons (nth 0 glyph-list) usable-temp))
	      ;; DOWN:	down -> up
	      (setq usable-temp (cons (cond
				       ((nth 1 glyph-list))
				       ((nth 0 glyph-list)))
				      usable-temp))
	      ;; DISABLED:  disabled -> up
	      (setq usable-temp (cons (cond
				       ((nth 2 glyph-list))
				       ((nth 0 glyph-list)))
				      usable-temp)))
	    usable-temp))
	 (height (apply 'max 0 (mapcar (lambda (glyph)
					 (if glyph
					     (glyph-height glyph)
					   0))
				       usable-buttons)))
	 (width (apply 'max 0 (mapcar (lambda (glyph)
					(if glyph
					    (glyph-width glyph)
					  0))
				      usable-buttons))))
    (list (if (symbolp image) image glyph-list) height width)))



(defun toolbarx-xemacs-button-properties (button)
  "Return a list of properties of BUTTON.
The result is either nil (if not to be inserted) or a list in the format
 (TOOLBAR HEIGHT WIDTH BUTTON-DESCRIPTION)
where

TOOLBAR is one of the symbols `default', `top', `right', `bottom'
  or `left'.

HEIGHT and WIDTH are the maximal dimentions of all the glyphs
  involved.

BUTTON-DESCRIPTION is button definition in XEmacs; see the
  documentation of variable `default-toolbar'."
  (let* ((filtered-props
	  (let* ((filtered-props-temp)
		 (prop-good-val)
		 (prop))
	    (dolist (p (nth 0 toolbarx-button-props) filtered-props-temp)
	      ;;    property	       -> (car p)
	      ;;    test type function -> (cadr p)
	      ;;    add-function       -> (cddr p)
	      (setq prop (memq (car p) button))
	      ;; if so, check if value is of correct type
	      (when prop
		;; if property is of add-type, them the value is a list
		;; (:add-value-list VAL VAL). Each VAL should be checked.
		(if (and (cddr p) (eq :add-value-list (car (cadr prop))))
		    (let* ((add-list (list (cddr p))))
		      (dolist (val (cdr (cadr prop)))
			(setq prop-good-val (funcall (cadr p) val))
			(when (car prop-good-val)
			  (setq add-list (cons (cdr prop-good-val) add-list))))
		      (setq add-list (nreverse add-list))
		      (when (eq 2 (length add-list)) ; just 1 value, no
						     ; add-function
			(setq add-list (cadr add-list)))
		      (setq filtered-props-temp (append
						 (list (car p) add-list)
						 filtered-props-temp)))
		  ;; if override-property
		  (setq prop-good-val (funcall (cadr p) (cadr prop)))
		  (when (car prop-good-val)
		    (setq filtered-props-temp (append
					       (list (car p)
						     (cdr prop-good-val))
					       filtered-props-temp))))))))
	 (insert (or (not (memq :insert filtered-props))
		     ;; (memq :insert filtered-props) holds
		     (eval (nth 1 (memq :insert filtered-props))))))
    (when insert
      (let* ((image-props (toolbarx-xemacs-image-properties
			   (cadr (memq :image filtered-props))))
	     (glyph-list (car image-props))
	     (image-height (nth 1 image-props))
	     (image-width (nth 2 image-props))
	     (command
	      (let* ((com (nth 1 (memq :command filtered-props)))
		     (app (nth 1 (memq :append-command filtered-props)))
		     (prep (nth 1 (memq :prepend-command filtered-props))))
		(when (or com app prep)
		  (toolbarx-make-command com prep app))))
	     ;; enable defaults to `t'
	     (enable (if (memq :enable filtered-props)
			 (cadr (memq :enable filtered-props))
		       t))
	    ;; help defaults to nil
	     (help (when (memq :help filtered-props)
		     (cadr (memq :help filtered-props))))
	     ;; toolbar defaults to `default'
	     (toolbar-prop (cons (memq :toolbar filtered-props)
				 (cadr (memq :toolbar filtered-props))))
	     (toolbar (if (car toolbar-prop)
			  (if (symbolp (cdr toolbar-prop))
			      (cdr toolbar-prop)
			    ;; (cdr toolbar-prop) is cons cell
			    (if (eq (cadr toolbar-prop)
					  (default-toolbar-position))
				      (cddr toolbar-prop)
				   (cadr toolbar-prop)))
			'default)))
	(when glyph-list
	  (list toolbar image-height image-width
		(vector glyph-list command enable help)))))))

(defun toolbarx-xemacs-refresh-process-button-or-insert-list (switches
							      toolbar-props)
  "Process SWITCHES, returning an updated version of TOOLBAR-PROPS.
TOOLBAR-PROPS should be a list with 12 elements, each one representing
properties (in this order) `locale', `default', `top', `right',
`bottom', `left', `default-height', `default-width', `top-height',
`right-width', `bottom-height' and `left-width'.  The return is a list
with the same properties updated.

NB: Buttons (vectors) are inserted in front of the lists
represented by `default', `top', `right', `bottom' and `left', so
the lists are built reversed."
  (let ((locale		 (nth 0	 toolbar-props))
	(default	 (nth 1	 toolbar-props))
	(top		 (nth 2	 toolbar-props))
	(right		 (nth 3	 toolbar-props))
	(bottom		 (nth 4	 toolbar-props))
	(left		 (nth 5	 toolbar-props))
	(default-height	 (nth 6	 toolbar-props))
	(default-width	 (nth 7	 toolbar-props))
	(top-height	 (nth 8	 toolbar-props))
	(right-width	 (nth 9	 toolbar-props))
	(bottom-height	 (nth 10 toolbar-props))
	(left-width	 (nth 11 toolbar-props))
	(toolbar-props-temp))
    (dolist (button switches)
      (if (eq (car button) :insert)
	  (when (eval (cadr button))
	    ;; if insert group, process `cddr'
	    (progn
	      (setq toolbar-props-temp
		    (toolbarx-xemacs-refresh-process-button-or-insert-list
		     (cddr button)
		     (list locale default top right bottom left
			   default-height default-width top-height
			   right-width bottom-height left-width)))
	      (setq default	   (nth 1  toolbar-props-temp))
	      (setq top		   (nth 2  toolbar-props-temp))
	      (setq right	   (nth 3  toolbar-props-temp))
	      (setq bottom	   (nth 4  toolbar-props-temp))
	      (setq left	   (nth 5  toolbar-props-temp))
	      (setq default-height (nth 6  toolbar-props-temp))
	      (setq default-width  (nth 7  toolbar-props-temp))
	      (setq top-height	   (nth 8  toolbar-props-temp))
	      (setq right-width	   (nth 9  toolbar-props-temp))
	      (setq bottom-height  (nth 10 toolbar-props-temp))
	      (setq left-width	   (nth 11 toolbar-props-temp))))
	;; else, if normal button
	(let* ((button-props (toolbarx-xemacs-button-properties button))
	       (toolbar (nth 0 button-props))
	       (height (nth 1 button-props))
	       (width (nth 2 button-props))
	       (button-description (nth 3 button-props)))
	  (when button-props
	    (cond
	     ;; default
	     ((eq toolbar 'default)
	      (setq default (cons button-description default))
	      (setq default-height (max default-height height))
	      (setq default-width (max default-width width)))
	     ;; top
	     ((eq toolbar 'top)
	      (setq top (cons button-description top))
	      (setq top-height (max top-height height)))
	     ;; right
	     ((eq toolbar 'right)
	      (setq right (cons button-description right))
	      (setq right-width (max right-width width)))
	     ;; bottom
	     ((eq toolbar 'bottom)
	      (setq bottom (cons button-description bottom))
	      (setq bottom-height (max bottom-height height)))
	     ;; left
	     ((eq toolbar 'left)
	      (setq left (cons button-description left))
	      (setq left-width (max left-width width))))))))
    ;; return a list similar to toolbar-props
    (list locale default top right bottom left default-height
	  default-width top-height right-width bottom-height left-width)))


(defun toolbarx-xemacs-refresh (&optional global-flag)
  "Refresh the toolbar in XEmacs."
  (let* ((switches (if global-flag
		       (if (default-boundp 'toolbarx-internal-button-switches)
			   (default-value 'toolbarx-internal-button-switches)
			 toolbarx-internal-button-switches)
		     toolbarx-internal-button-switches))
	 (locale  (if global-flag 'global (current-buffer)))
	 (toolbar-init (list locale	; locale
			     nil	; default
			     nil	; top
			     nil	; right
			     nil	; bottom
			     nil	; left
			     0		; default-height
			     0		; default-width
			     0		; top-height
			     0		; right-width
			     0		; bottom-height
			     0))	; left-width
	 (toolbar-props
	  (toolbarx-xemacs-refresh-process-button-or-insert-list switches
								 toolbar-init))
	 ;; NB: Buttons (vectors) are inserted in front of the lists
	 ;; represented by `default', `top', `right', `bottom' and
	 ;; `left', so the lists are built reversed.
	 (default	  (nreverse (nth 1  toolbar-props)))
	 (top		  (nreverse (nth 2  toolbar-props)))
	 (right		  (nreverse (nth 3  toolbar-props)))
	 (bottom	  (nreverse (nth 4  toolbar-props)))
	 (left		  (nreverse (nth 5  toolbar-props)))
	 (default-height  (nth 6  toolbar-props))
	 (default-width	  (nth 7  toolbar-props))
	 (top-height	  (nth 8  toolbar-props))
	 (right-width	  (nth 9  toolbar-props))
	 (bottom-height	  (nth 10 toolbar-props))
	 (left-width	  (nth 11 toolbar-props))
	 (button-raised-border 2)
	 (default-border (specifier-instance default-toolbar-border-width))
	 (top-border (specifier-instance top-toolbar-border-width))
	 (right-border (specifier-instance right-toolbar-border-width))
	 (bottom-border (specifier-instance bottom-toolbar-border-width))
	 (left-border (specifier-instance left-toolbar-border-width)))
    ;; adding borders
    (when default
      (setq default-height (+ (* 2 button-raised-border)
			      (* 2 default-border)
			      default-height))
      (setq default-width (+ (* 2 button-raised-border)
			     (* 2 default-border)
			     default-width)))
    (when top
      (setq top-height (+ (* 2 button-raised-border)
			  (* 2 top-border)
			  top-height)))
    (when right
      (setq right-width (+ (* 2 button-raised-border)
			   (* 2 right-border)
			   right-width)))
    (when bottom
      (setq bottom-height (+ (* 2 button-raised-border)
			     (* 2 bottom-border)
			     bottom-height)))
    (when left
      (setq left-width (+ (* 2 button-raised-border)
			  (* 2 left-border)
			  left-width)))
    ;; deal with specifiers
    ;; - remove all specifiers for toolbars witout buttons
    (if default
	(progn
	  (if (memq (default-toolbar-position) '(top bottom))
	      (set-specifier default-toolbar-visible-p
			     (not (not default)) locale)
	    (set-specifier default-toolbar-visible-p
			   (not (not default)) locale))
	  (if (memq (default-toolbar-position) '(top bottom))
	      (set-specifier default-toolbar-height default-height locale)
	    (set-specifier default-toolbar-width default-width locale))
	  (set-specifier default-toolbar default locale))
      (remove-specifier default-toolbar locale)
      (remove-specifier default-toolbar-visible-p locale)
      (remove-specifier default-toolbar-height locale)
      (remove-specifier default-toolbar-width locale))
    (if top
	(progn
	  (set-specifier top-toolbar-visible-p (not (not top)) locale)
	  (set-specifier top-toolbar-height top-height locale)
	  (set-specifier top-toolbar top locale))
      (remove-specifier top-toolbar locale)
      (remove-specifier top-toolbar-visible-p locale)
      (remove-specifier top-toolbar-height locale))
    (if right
	(progn
	  (set-specifier right-toolbar-visible-p (not (not right))
			 locale)
	  (set-specifier right-toolbar-width right-width locale)
	  (set-specifier right-toolbar right locale))
      (remove-specifier right-toolbar locale)
      (remove-specifier right-toolbar-visible-p locale)
      (remove-specifier right-toolbar-width locale))
    (if bottom
	(progn
	  (set-specifier bottom-toolbar-visible-p (not (not bottom)) locale)
	  (set-specifier bottom-toolbar-height bottom-height locale)
	  (set-specifier bottom-toolbar bottom locale))
      (remove-specifier bottom-toolbar locale)
      (remove-specifier bottom-toolbar-visible-p locale)
      (remove-specifier bottom-toolbar-height locale))
    (if left
	(progn
	  (set-specifier left-toolbar-visible-p (not (not left)) locale)
	  (set-specifier left-toolbar-width left-width locale)
	  (set-specifier left-toolbar left locale))
      (remove-specifier left-toolbar locale)
      (remove-specifier left-toolbar-visible-p locale)
      (remove-specifier left-toolbar-width locale))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; finishing parsing engine

(defun toolbarx-refresh (&optional global-flag)
  "Redraw the toolbar, peviously installed with `toolbarx'.
Force global refresh if GLOBAL-FLAG is non-nil."
  (interactive "P")
  (if (featurep 'xemacs)
      (toolbarx-xemacs-refresh global-flag)
    (toolbarx-emacs-refresh global-flag)))

;;;###autoload (autoload 'toolbarx-install-toolbar "toolbar-x")

(defun toolbarx-install-toolbar (buttons &optional meaning-alist global-flag)
  "Install toolbar buttons given in BUTTONS.
Button properties are optionally given in MEANING-ALIST.  If
GLOBAL-FLAG is non-nil, toolbar is installed globally (on every
buffer that does not have a toolbar set locally).  BUTTONS is a
list of format
  (ELEM ... ELEM . PROPS),
where each ELEM is either

 - a list in the same format od BUTTONS, which is going to be
   refered as a *group*; groups are used to distribute properties
   recursively to its elements; there are groups with special
   format for special purpose: *dropdown groups* and also *eval
   groups*.

 - a symbol, which could be associated in MEANING-ALIST with a
   list of button properties (symbol + properties = a *button*)
   or associated to a special kind of group (an *alias group*).

 - a vector, which elements are on the previous formats (but not
   another vector); this is useful to specify different
   ingredients to the toolbar depending if editor is Emacs or
   XEmacs; the first element will be used in Emacs; the second
   element is going to be used in XEmacs.

Meaning alist
=============

MEANING-ALIST is a list where each element is in one of the
formats (SYMB . BUTTON-PROPS-LIST) or (SYMB .  ALIAS-GROUP).
BUTTON-PROPS-LIST is a list in one of the formats
  (IMAGE COMMAND PROP VAL PROP VAL ... PROP VAL)  or
  (PROP VAL PROP VAL ... PROP VAL).
The IMAGE is going to be used as the `:image' property of the
button (see button properties bellow), and COMMAND shall be used
as the `:command' property of the button.  Each PROP is one of
the button properties, and VAL is its respective value.
ALIAS-GROUP is a list which first element is the symbol `:alias'
and the cdr shall be processed as a group.

However, a symbol is not required to have an association in
MEANING-ALIST, which is only a way to specify properties to a
button.	 One can use groups to specify properties.  Nil is a good
MEANING-ALIST.

Buttons
=======

A toolbar button in `toolbarx' is the set with a symbol and
properties used to display the button, like a image and a command
to call when the button is pressed (which are the minimal
elements that a button should have.)  The supported properties
for buttons and their `basic types' (see note on how values of
properties are obtained!) are:

 :image -- in Emacs, either a string or image descriptor (see
   info for a definition), or a variable bound to a image
   descriptor (like those defined with `defimage') or a list of 4
   strings or image descriptors; in XEmacs, either a string or a
   glyph, or a symbol bount to a glyph, or a list of at least 1
   and at most 6 strings or glyphs or nil (not the first element
   though); defines the image file displayed by the button.  If
   it is a string, the image file found with that name (always
   using the function `toolbarx-find-image' to make the
   \`internal\' image descriptor) is used as button image.  For
   the other formats, the button image is handled in the same way
   as it is treated by the editors; see info nodes bellow for a
   description of the capabilities of each editor
      Emacs: info file \"elisp\", node \"Tool Bar\" (see `:image'
             property);
             PS: a *vector* of four strings is used in the Emacs
             Lisp documentation as the `more ellaborated' image
             property format, but here we reserve vectors to
             provide editor-dependent values; this motivates our
             choice for a list instead of vector (however,
             internally the list becomes a vector when displaying
             the button).
     XEmacs: info file \"lispref\", node \"Toolbar Descriptor
             Format\" (see GLYPH-LIST) or the documentation of
             the variable `default-toolbar'; check the inheritage
             in case of a ommited glyph or nil instead of glyph.

 :command -- a form; if the form happens to be a command, it will
   be called with `call-interactively'.

 :append-command -- a form added to the end of the value of
   `:command'.

 :prepend-command -- a form added at the beginning of the value
   of `:command'.

 :help -- either a string or nil; defined the help string of the
   button;

 :enable -- a form, evaluated constantly by both editors to
   determine if a button is active (enabled) or not.

 :visible -- in Emacs, a form that is evaluated constantly to
   determine if a button is visible; in XEmacs, this property is
   ignored.

 :button -- in Emacs, a cons cell (TYPE .  SELECTED) where the
   TYPE should be `:toggle' or `:radio' and the cdr should be a
   form.  SELECTED is evaluated to determine when the button is
   selected.  This property is ignored in XEmacs.

 :insert -- a form that is evaluated every time that the toolbar
   is refresh (a call of `toolbarx-refresh') to determine if the
   button is inserted or just ignored (until next refresh).

 :toolbar -- in XEmacs, either one of the symbols `default',
   `top', `bottom', `left', `right', or a cons cell
   (POS . POS-AVOID-DEFAULT) where POS and POS-AVOID-DEFAULT
   should be one of the symbols `top', `bottom', `left', `right';
   if a symbol, the button will be inserted in one of these
   toolbars; if a cons cell, button will be inserted in toolbar
   POS unless the position of the default toolbar is POS (then,
   the default toolbar would override the position-specific
   toolbar), and in this case, button will be inserted in toolbar
   POS-AVOID-DEFAULT; in Emacs, this property is meaningless, and
   therefore ignored.  Hint of use of this property: in a
   program, use or everything with `default' and the cons format
   to avoid the default toolbar, or use only the position
   specific buttons (symbols that are not `default'), because of
   the `overriding' system in XEmacs, when a position-specific
   toolbar overrides the default toolbar; for instance, if you
   put a button in the default toolbar and another in the top
   toolbar (and the default toolbar is in the top), then *only*
   the ones in the top toolbar will be visible!

How to specify a button
=======================

One can specify a button by its symbol or by a group to specify
properties.  For example,
  BUTTON =
    ( foo
      (bar :image [\"bar-Emacs\" \"bar-XEmacs\"]
           :command bar-function :help \"Bar help string\")
      :insert foo-bar )
  MEANING-ALIST = ( (foo :image \"foo\" :command foo-function) )
specifiy two buttons `foo' and `bar', each one with its necessary
:image and :command properties, and both use the :insert property
specified ate the end of BUTTONS (because groups distribute
properties to all its elements).  `foo' and `bar' will be
inserted only if `foo-bar' evaluation yields non-nil.  `bar' used
a different :image property depending if editor is Emacs or
XEmacs.

Note on how values of properties are obtained
=============================================

For each property PROP, its value should be either:
   i) a vector of 2 elements; then each element should be of the
      basic type of PROP.
  ii) an element on the basic type of PROP.
 iii) a function (that does not need arguments); it is evaluated
      and the return should be ot type i) or ii) above
  iv) a symbol bound to a element of type i) or ii).

The type is cheched in the order i), ii) iii) and iv).	This
evaluations are done every time that the oolbar is refresh.

Ps.: in order to specify a vector as value of a property (like
the :image in Emacs), it is necessary to provide the vector as
element of another vector.

Special groups
==============

Eval groups
-----------

If the first element of a group is the symbol `:eval-group', each
element is evaluated (with `eval'), put inside a list and
processed like a group.	 Eval groups are useful to store
definition of buttons in a variable.

Dropdown groups
---------------

The idea is to specify a set of buttons that appear when a
determined menu item of a dropdown menu is active.  The dropdown
menu appears when a button (by default with a triangle pointing
down) is clicked.  This button is called `dropdown button'.  The
dropdown button appears on the left of the currently visible
buttons of the dropdown group.

A dropdown group is a list which first element is the symbol
`:dropdown-group' and in one of the following formats
  (:dropdown-group SYMBOL-1 ... SYMBOL-n  PROP-1 VAL-1 ... PROP-k VAL-k)
or
  (:dropdown-group
     STRING-1 ITEM-11 ... ITEM-1n
     STRING-2 ITEM-21 ... ITEM-2m
	   . . .
     STRING-n ITEM-n1 ... ITEM-np
       PROP-1 VAL-1 ... PROP-j VAL-j)
where
 SYMBOL-* is a symbol that defines a button in MEANING-ALIST;
 STRING-* is a string that will appear in the dropdown menu;
 ITEM-* is any format that define buttons or groups.

\(a dropdown group of first format is internally converted to the
second by making strings from the symbols and each symbol is the
item)

The same rules for obtaining property values, described above,
apply here.  Properties are also distributed by groups.	 The
supported properties and their basic type are:

 :type -- one of the symbols `radio' (default) or `toggle'; if
   type is radio, only one of the itens may be active, and if
   type is toggle, any item number of itens can be active.

 :variable -- a symbol; it is the variable that govern the
   dropdown button; every time the value should be an integer
   starting from 1 (if type is radio) or a list of integers (if
   type is toggle).  The Nth set of buttons is :insert'ed.

 :default -- determines the default value when the menu is
   installed; it is ignored if a value was saved with custom; it
   defaults to 1 if type is radio or nil if type is toggle.  If
   value is a integer and type is `toggle', value used is a list
   with that integer.

 :save -- one of the symbols nil (default), `offer' or
   `always'; determined if it is possible for the user to save
   the which menu itens are active, for a next session.	 If value
   is `offer', a item (offering to save) is added to the
   popup menu.	If the value is `always', every time that a item
   is selected, the variable is saved.	If value is nil, variable
   shall not be saved.	If value is non-nil then `:variable' is
   mandatory.

 :title -- a string or nil; if a string, the popup menu will show
   is as menu title; if nil, no title is shown.

 :dropdown-help -- a string or nil; the help string of the
   dropdown button.

 :dropdown-image -- in Emacs, either a string or a vector of 4
   strings; in XEmacs, either a string or a glyph or a list of at
   least 1 and at most 6 strings or glyphs; defines the image
   file displayed by the dropdown button; by default, it is the
   string \"dropdown\".

 :dropdown-append-command,
 :dropdownprepend-command -- a form; append or prepend forms to
   the command that shows the dropdown menu, allowing extra code
   to run before or after the menu appears (remember that every
   menu item clicked refresh the toolbar.)

 :dropdown-enable -- a form; evaluated constantly by both editors
   to determine if the dropdown button is active (enabled) or
   not.

 :dropdown-visible -- a form; in Emacs, it is evaluated
   constantly to determine if the dropdown button is visible; in
   XEmacs, this property is ignored.

 :dropdown-toolbar -- in XEmacs, one of the symbols `default',
   `opposite', `top', `bottom', `left' or `right'; ignored in
   Emacs; in XEmacs, the toolbar where the dropdown button will
   appear.

Also, if the symbol `dropdown' is associted in MEANING-ALIST
with some properties, these properties override (or add) with
higher precedence.

Special buttons
===============

If the symbol of a button is `:new-line', it is inserted
a (faked) return, and the next button will be displayed a next
line of buttons.  The only property supported for this button is
`:insert'.  This feature is available only in Emacs.  In XEmacs,
this button is ignored."
  (let ((switches (toolbarx-process-group buttons meaning-alist nil nil)))
    (if global-flag
	(setq-default toolbarx-internal-button-switches
		      switches)
      (set (make-local-variable 'toolbarx-internal-button-switches)
	   switches)
      (unless (featurep 'xemacs)
	(make-local-variable 'tool-bar-map))))
  (toolbarx-refresh global-flag))


(defconst toolbarx-default-toolbar-meaning-alist
  '((separator :image "sep" :command t :enable nil :help "")
    (open-file :image ["new" toolbar-file-icon]
	       :command [find-file toolbar-open]
	       :enable [(not (window-minibuffer-p
			      (frame-selected-window menu-updating-frame)))
			t]
	       :help ["Read a file into an Emacs buffer" "Open a file"])

    (dired :image ["open" toolbar-folder-icon]
	   :command [dired toolbar-dired]
	   :help ["Read a directory, operate on its files" "Edit a directory"])

    (save-buffer :image ["save" toolbar-disk-icon]
		 :command [save-buffer toolbar-save]
		 :enable [(and
			   (buffer-modified-p)
			   (buffer-file-name)
			   (not (window-minibuffer-p
				 (frame-selected-window menu-updating-frame))))
			  t]
		 :help ["Save current buffer to its file"  "Save buffer"]
		 :visible (or buffer-file-name
			      (not (eq 'special