comparison lisp/custom/wid-edit.el @ 134:34a5b81f86ba r20-2b1

Import from CVS: tag r20-2b1
author cvs
date Mon, 13 Aug 2007 09:30:11 +0200
parents 9b50b4588a93
children b980b6286996
comparison
equal deleted inserted replaced
133:b27e67717092 134:34a5b81f86ba
2 ;; 2 ;;
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc. 3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
4 ;; 4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk> 5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: extensions 6 ;; Keywords: extensions
7 ;; Version: 1.84 7 ;; Version: 1.89
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/ 8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
9 9
10 ;;; Commentary: 10 ;;; Commentary:
11 ;; 11 ;;
12 ;; See `widget.el'. 12 ;; See `widget.el'.
56 56
57 (unless (and (featurep 'custom) (fboundp 'custom-declare-variable)) 57 (unless (and (featurep 'custom) (fboundp 'custom-declare-variable))
58 ;; We have the old custom-library, hack around it! 58 ;; We have the old custom-library, hack around it!
59 (defmacro defgroup (&rest args) nil) 59 (defmacro defgroup (&rest args) nil)
60 (defmacro defcustom (var value doc &rest args) 60 (defmacro defcustom (var value doc &rest args)
61 `(defvar ,var ,value ,doc)) 61 (` (defvar (, var) (, value) (, doc))))
62 (defmacro defface (&rest args) nil) 62 (defmacro defface (&rest args) nil)
63 (define-widget-keywords :prefix :tag :load :link :options :type :group) 63 (define-widget-keywords :prefix :tag :load :link :options :type :group)
64 (when (fboundp 'copy-face) 64 (when (fboundp 'copy-face)
65 (copy-face 'default 'widget-documentation-face) 65 (copy-face 'default 'widget-documentation-face)
66 (copy-face 'bold 'widget-button-face) 66 (copy-face 'bold 'widget-button-face)
115 :type 'face 115 :type 'face
116 :group 'widgets) 116 :group 'widgets)
117 117
118 (defface widget-field-face '((((class grayscale color) 118 (defface widget-field-face '((((class grayscale color)
119 (background light)) 119 (background light))
120 (:background "light gray")) 120 (:background "gray85"))
121 (((class grayscale color) 121 (((class grayscale color)
122 (background dark)) 122 (background dark))
123 (:background "dark gray")) 123 (:background "dark gray"))
124 (t 124 (t
125 (:italic t))) 125 (:italic t)))
165 165
166 (defun widget-choose (title items &optional event) 166 (defun widget-choose (title items &optional event)
167 "Choose an item from a list. 167 "Choose an item from a list.
168 168
169 First argument TITLE is the name of the list. 169 First argument TITLE is the name of the list.
170 Second argument ITEMS is an alist (NAME . VALUE). 170 Second argument ITEMS is an list whose members are either
171 (NAME . VALUE), to indicate selectable items, or just strings to
172 indicate unselectable items.
171 Optional third argument EVENT is an input event. 173 Optional third argument EVENT is an input event.
172 174
173 The user is asked to choose between each NAME from the items alist, 175 The user is asked to choose between each NAME from the items alist,
174 and the VALUE of the chosen element will be returned. If EVENT is a 176 and the VALUE of the chosen element will be returned. If EVENT is a
175 mouse event, and the number of elements in items is less than 177 mouse event, and the number of elements in items is less than
186 (let ((val (get-popup-menu-response 188 (let ((val (get-popup-menu-response
187 (cons title 189 (cons title
188 (mapcar 190 (mapcar
189 (function 191 (function
190 (lambda (x) 192 (lambda (x)
191 (vector (car x) (list (car x)) t))) 193 (if (stringp x)
194 (vector x nil nil)
195 (vector (car x) (list (car x)) t))))
192 items))))) 196 items)))))
193 (setq val (and val 197 (setq val (and val
194 (listp (event-object val)) 198 (listp (event-object val))
195 (stringp (car-safe (event-object val))) 199 (stringp (car-safe (event-object val)))
196 (car (event-object val)))) 200 (car (event-object val))))
197 (cdr (assoc val items)))) 201 (cdr (assoc val items))))
198 (t 202 (t
203 (setq items (remove-if 'stringp items))
199 (let ((val (completing-read (concat title ": ") items nil t))) 204 (let ((val (completing-read (concat title ": ") items nil t)))
200 (if (stringp val) 205 (if (stringp val)
201 (let ((try (try-completion val items))) 206 (let ((try (try-completion val items)))
202 (when (stringp try) 207 (when (stringp try)
203 (setq val try)) 208 (setq val try))
369 (add-text-properties from to (list 'widget-doc widget 374 (add-text-properties from to (list 'widget-doc widget
370 'face 'widget-documentation-face))) 375 'face 'widget-documentation-face)))
371 376
372 (defmacro widget-specify-insert (&rest form) 377 (defmacro widget-specify-insert (&rest form)
373 ;; Execute FORM without inheriting any text properties. 378 ;; Execute FORM without inheriting any text properties.
374 `(save-restriction 379 (`
380 (save-restriction
375 (let ((inhibit-read-only t) 381 (let ((inhibit-read-only t)
376 result 382 result
377 after-change-functions) 383 after-change-functions)
378 (insert "<>") 384 (insert "<>")
379 (narrow-to-region (- (point) 2) (point)) 385 (narrow-to-region (- (point) 2) (point))
380 (widget-specify-none (point-min) (point-max)) 386 (widget-specify-none (point-min) (point-max))
381 (goto-char (1+ (point-min))) 387 (goto-char (1+ (point-min)))
382 (setq result (progn ,@form)) 388 (setq result (progn (,@ form)))
383 (delete-region (point-min) (1+ (point-min))) 389 (delete-region (point-min) (1+ (point-min)))
384 (delete-region (1- (point-max)) (point-max)) 390 (delete-region (1- (point-max)) (point-max))
385 (goto-char (point-max)) 391 (goto-char (point-max))
386 result))) 392 result))))
387 393
388 (defface widget-inactive-face '((((class grayscale color) 394 (defface widget-inactive-face '((((class grayscale color)
389 (background dark)) 395 (background dark))
390 (:foreground "light gray")) 396 (:foreground "light gray"))
391 (((class grayscale color) 397 (((class grayscale color)
399 (defun widget-specify-inactive (widget from to) 405 (defun widget-specify-inactive (widget from to)
400 "Make WIDGET inactive for user modifications." 406 "Make WIDGET inactive for user modifications."
401 (unless (widget-get widget :inactive) 407 (unless (widget-get widget :inactive)
402 (let ((overlay (make-overlay from to nil t nil))) 408 (let ((overlay (make-overlay from to nil t nil)))
403 (overlay-put overlay 'face 'widget-inactive-face) 409 (overlay-put overlay 'face 'widget-inactive-face)
404 (overlay-put overlay 'evaporate 't) 410 (overlay-put overlay 'evaporate t)
411 (overlay-put overlay 'priority 100)
405 (overlay-put overlay (if (string-match "XEmacs" emacs-version) 412 (overlay-put overlay (if (string-match "XEmacs" emacs-version)
406 'read-only 413 'read-only
407 'modification-hooks) '(widget-overlay-inactive)) 414 'modification-hooks) '(widget-overlay-inactive))
408 (widget-put widget :inactive overlay)))) 415 (widget-put widget :inactive overlay))))
409 416
781 (button (goto-char button)) 788 (button (goto-char button))
782 (field (goto-char field)) 789 (field (goto-char field))
783 (t 790 (t
784 (error "No buttons or fields found")))))) 791 (error "No buttons or fields found"))))))
785 (setq button (widget-at (point))) 792 (setq button (widget-at (point)))
786 (if (and button (widget-get button :tab-order) 793 (if (or (and button (widget-get button :tab-order)
787 (< (widget-get button :tab-order) 0)) 794 (< (widget-get button :tab-order) 0))
795 (and button (not (widget-apply button :active))))
788 (setq arg (1+ arg)))))) 796 (setq arg (1+ arg))))))
789 (while (< arg 0) 797 (while (< arg 0)
790 (if (= (point-min) (point)) 798 (if (= (point-min) (point))
791 (forward-char 1)) 799 (forward-char 1))
792 (setq arg (1+ arg)) 800 (setq arg (1+ arg))
819 (cond ((and button field) 827 (cond ((and button field)
820 (goto-char (max button field))) 828 (goto-char (max button field)))
821 (button (goto-char button)) 829 (button (goto-char button))
822 (field (goto-char field))) 830 (field (goto-char field)))
823 (setq button (widget-at (point))) 831 (setq button (widget-at (point)))
824 (if (and button (widget-get button :tab-order) 832 (if (or (and button (widget-get button :tab-order)
825 (< (widget-get button :tab-order) 0)) 833 (< (widget-get button :tab-order) 0))
834 (and button (not (widget-apply button :active))))
826 (setq arg (1- arg))))) 835 (setq arg (1- arg)))))
827 (widget-echo-help (point)) 836 (widget-echo-help (point))
828 (run-hooks 'widget-move-hook)) 837 (run-hooks 'widget-move-hook))
829 838
830 (defun widget-forward (arg) 839 (defun widget-forward (arg)
1068 (to (copy-marker (point-max)))) 1077 (to (copy-marker (point-max))))
1069 (widget-specify-text from to) 1078 (widget-specify-text from to)
1070 (set-marker-insertion-type from t) 1079 (set-marker-insertion-type from t)
1071 (set-marker-insertion-type to nil) 1080 (set-marker-insertion-type to nil)
1072 (widget-put widget :from from) 1081 (widget-put widget :from from)
1073 (widget-put widget :to to)))) 1082 (widget-put widget :to to)))
1083 (widget-clear-undo))
1074 1084
1075 (defun widget-default-format-handler (widget escape) 1085 (defun widget-default-format-handler (widget escape)
1076 ;; We recognize the %h escape by default. 1086 ;; We recognize the %h escape by default.
1077 (let* ((buttons (widget-get widget :buttons)) 1087 (let* ((buttons (widget-get widget :buttons))
1078 (doc-property (widget-get widget :documentation-property)) 1088 (doc-property (widget-get widget :documentation-property))
1130 (widget-apply widget :value-delete) 1140 (widget-apply widget :value-delete)
1131 (when (< from to) 1141 (when (< from to)
1132 ;; Kludge: this doesn't need to be true for empty formats. 1142 ;; Kludge: this doesn't need to be true for empty formats.
1133 (delete-region from to)) 1143 (delete-region from to))
1134 (set-marker from nil) 1144 (set-marker from nil)
1135 (set-marker to nil))) 1145 (set-marker to nil))
1146 (widget-clear-undo))
1136 1147
1137 (defun widget-default-value-set (widget value) 1148 (defun widget-default-value-set (widget value)
1138 ;; Recreate widget with new value. 1149 ;; Recreate widget with new value.
1139 (save-excursion 1150 (save-excursion
1140 (goto-char (widget-get widget :from)) 1151 (goto-char (widget-get widget :from))
1278 "A link to an info file." 1289 "A link to an info file."
1279 :action 'widget-info-link-action) 1290 :action 'widget-info-link-action)
1280 1291
1281 (defun widget-info-link-action (widget &optional event) 1292 (defun widget-info-link-action (widget &optional event)
1282 "Open the info node specified by WIDGET." 1293 "Open the info node specified by WIDGET."
1283 (Info-goto-node (widget-value widget))) 1294 (Info-goto-node (widget-value widget))
1295 ;; Steal button release event.
1296 (if (and (fboundp 'button-press-event-p)
1297 (fboundp 'next-command-event))
1298 ;; XEmacs
1299 (and event
1300 (button-press-event-p event)
1301 (next-command-event))
1302 ;; Emacs
1303 (when (memq 'down (event-modifiers event))
1304 (read-event))))
1284 1305
1285 ;;; The `url-link' Widget. 1306 ;;; The `url-link' Widget.
1286 1307
1287 (define-widget 'url-link 'link 1308 (define-widget 'url-link 'link
1288 "A link to an www page." 1309 "A link to an www page."
1488 (widget-choose tag (reverse choices) event)))) 1509 (widget-choose tag (reverse choices) event))))
1489 (when current 1510 (when current
1490 (widget-value-set widget 1511 (widget-value-set widget
1491 (widget-apply current :value-to-external 1512 (widget-apply current :value-to-external
1492 (widget-get current :value))) 1513 (widget-get current :value)))
1493 (widget-apply widget :notify widget event) 1514 (widget-apply widget :notify widget event)
1494 (widget-setup))) 1515 (widget-setup))))
1495 ;; Notify parent.
1496 (widget-apply widget :notify widget event)
1497 (widget-clear-undo))
1498 1516
1499 (defun widget-choice-validate (widget) 1517 (defun widget-choice-validate (widget)
1500 ;; Valid if we have made a valid choice. 1518 ;; Valid if we have made a valid choice.
1501 (let ((void (widget-get widget :void)) 1519 (let ((void (widget-get widget :void))
1502 (choice (widget-get widget :choice)) 1520 (choice (widget-get widget :choice))
1548 1566
1549 (defun widget-toggle-action (widget &optional event) 1567 (defun widget-toggle-action (widget &optional event)
1550 ;; Toggle value. 1568 ;; Toggle value.
1551 (widget-value-set widget (not (widget-value widget))) 1569 (widget-value-set widget (not (widget-value widget)))
1552 (widget-apply widget :notify widget event)) 1570 (widget-apply widget :notify widget event))
1553 1571
1554 ;;; The `checkbox' Widget. 1572 ;;; The `checkbox' Widget.
1555 1573
1556 (define-widget 'checkbox 'toggle 1574 (define-widget 'checkbox 'toggle
1557 "A checkbox toggle." 1575 "A checkbox toggle."
1558 :format "%[%v%]" 1576 :format "%[%v%]"