Mercurial > emacs
annotate lisp/wid-edit.el @ 18120:112bc3055da6
Customize.
(ada-mode): Set up support for find-file.el.
(ada-other-file-alist): New variable moved from find-file.el.
(ada-search-directories): Likewise.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Mon, 02 Jun 1997 06:06:03 +0000 |
parents | 2983683a278b |
children | fa4eb2f6b05a |
rev | line source |
---|---|
17334 | 1 ;;; wid-edit.el --- Functions for creating and using widgets. |
2 ;; | |
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc. | |
4 ;; | |
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk> | |
6 ;; Keywords: extensions | |
18090 | 7 ;; Version: 1.9905 |
17334 | 8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/ |
9 | |
17535
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
10 ;; This file is part of GNU Emacs. |
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
11 |
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
12 ;; GNU Emacs is free software; you can redistribute it and/or modify |
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
13 ;; it under the terms of the GNU General Public License as published by |
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
14 ;; the Free Software Foundation; either version 2, or (at your option) |
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
15 ;; any later version. |
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
16 |
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
17 ;; GNU Emacs is distributed in the hope that it will be useful, |
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
20 ;; GNU General Public License for more details. |
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
21 |
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
22 ;; You should have received a copy of the GNU General Public License |
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
25 ;; Boston, MA 02111-1307, USA. |
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
26 |
17334 | 27 ;;; Commentary: |
28 ;; | |
29 ;; See `widget.el'. | |
30 | |
31 ;;; Code: | |
32 | |
33 (require 'widget) | |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
34 (eval-when-compile (require 'cl)) |
17334 | 35 |
36 ;;; Compatibility. | |
37 | |
38 (eval-and-compile | |
39 (autoload 'pp-to-string "pp") | |
40 (autoload 'Info-goto-node "info") | |
41 | |
42 (when (string-match "XEmacs" emacs-version) | |
43 (condition-case nil | |
44 (require 'overlay) | |
45 (error (load-library "x-overlay")))) | |
46 | |
47 (if (string-match "XEmacs" emacs-version) | |
17799 | 48 (defun widget-event-point (event) |
49 "Character position of the end of event if that exists, or nil." | |
50 (if (mouse-event-p event) | |
51 (event-point event) | |
52 nil)) | |
53 (defun widget-event-point (event) | |
54 "Character position of the end of event if that exists, or nil." | |
55 (posn-point (event-end event)))) | |
56 | |
17334 | 57 ;; The following should go away when bundled with Emacs. |
58 (condition-case () | |
59 (require 'custom) | |
60 (error nil)) | |
61 | |
62 (unless (and (featurep 'custom) (fboundp 'custom-declare-variable)) | |
63 ;; We have the old custom-library, hack around it! | |
64 (defmacro defgroup (&rest args) nil) | |
65 (defmacro defcustom (var value doc &rest args) | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
66 (` (defvar (, var) (, value) (, doc)))) |
17334 | 67 (defmacro defface (&rest args) nil) |
68 (define-widget-keywords :prefix :tag :load :link :options :type :group) | |
69 (when (fboundp 'copy-face) | |
70 (copy-face 'default 'widget-documentation-face) | |
71 (copy-face 'bold 'widget-button-face) | |
72 (copy-face 'italic 'widget-field-face))) | |
73 | |
17799 | 74 (unless (fboundp 'button-release-event-p) |
75 ;; XEmacs function missing from Emacs. | |
76 (defun button-release-event-p (event) | |
77 "Non-nil if EVENT is a mouse-button-release event object." | |
78 (and (eventp event) | |
79 (memq (event-basic-type event) '(mouse-1 mouse-2 mouse-3)) | |
80 (or (memq 'click (event-modifiers event)) | |
81 (memq 'drag (event-modifiers event)))))) | |
17334 | 82 |
83 (unless (fboundp 'error-message-string) | |
84 ;; Emacs function missing in XEmacs. | |
85 (defun error-message-string (obj) | |
86 "Convert an error value to an error message." | |
87 (let ((buf (get-buffer-create " *error-message*"))) | |
88 (erase-buffer buf) | |
89 (display-error obj buf) | |
90 (buffer-string buf))))) | |
91 | |
18087 | 92 (when (let ((a "foo")) |
93 (put-text-property 1 2 'foo 1 a) | |
94 (put-text-property 1 2 'bar 2 a) | |
95 (set-text-properties 1 2 nil a) | |
96 (text-properties-at 1 a)) | |
97 ;; XEmacs 20.2 and earlier had a buggy set-text-properties. | |
98 (defun set-text-properties (start end props &optional buffer-or-string) | |
99 "Completely replace properties of text from START to END. | |
100 The third argument PROPS is the new property list. | |
101 The optional fourth argument, BUFFER-OR-STRING, | |
102 is the string or buffer containing the text." | |
103 (map-extents #'(lambda (extent ignored) | |
104 (remove-text-properties | |
105 start end | |
106 (list (extent-property extent 'text-prop) | |
107 nil) | |
108 buffer-or-string) | |
109 nil) | |
110 buffer-or-string start end nil nil 'text-prop) | |
111 (add-text-properties start end props buffer-or-string))) | |
112 | |
17334 | 113 ;;; Customization. |
114 | |
115 (defgroup widgets nil | |
116 "Customization support for the Widget Library." | |
117 :link '(custom-manual "(widget)Top") | |
118 :link '(url-link :tag "Development Page" | |
119 "http://www.dina.kvl.dk/~abraham/custom/") | |
120 :prefix "widget-" | |
121 :group 'extensions | |
122 :group 'faces | |
123 :group 'hypermedia) | |
124 | |
125 (defface widget-documentation-face '((((class color) | |
126 (background dark)) | |
127 (:foreground "lime green")) | |
128 (((class color) | |
129 (background light)) | |
130 (:foreground "dark green")) | |
131 (t nil)) | |
132 "Face used for documentation text." | |
133 :group 'widgets) | |
134 | |
135 (defface widget-button-face '((t (:bold t))) | |
136 "Face used for widget buttons." | |
137 :group 'widgets) | |
138 | |
139 (defcustom widget-mouse-face 'highlight | |
140 "Face used for widget buttons when the mouse is above them." | |
141 :type 'face | |
142 :group 'widgets) | |
143 | |
144 (defface widget-field-face '((((class grayscale color) | |
145 (background light)) | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
146 (:background "gray85")) |
17334 | 147 (((class grayscale color) |
148 (background dark)) | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
149 (:background "dim gray")) |
17334 | 150 (t |
151 (:italic t))) | |
152 "Face used for editable fields." | |
153 :group 'widgets) | |
154 | |
155 ;;; Utility functions. | |
156 ;; | |
157 ;; These are not really widget specific. | |
158 | |
159 (defsubst widget-plist-member (plist prop) | |
160 ;; Return non-nil if PLIST has the property PROP. | |
161 ;; PLIST is a property list, which is a list of the form | |
162 ;; (PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol. | |
163 ;; Unlike `plist-get', this allows you to distinguish between a missing | |
164 ;; property and a property with the value nil. | |
165 ;; The value is actually the tail of PLIST whose car is PROP. | |
166 (while (and plist (not (eq (car plist) prop))) | |
167 (setq plist (cdr (cdr plist)))) | |
168 plist) | |
169 | |
170 (defun widget-princ-to-string (object) | |
171 ;; Return string representation of OBJECT, any Lisp object. | |
172 ;; No quoting characters are used; no delimiters are printed around | |
173 ;; the contents of strings. | |
174 (save-excursion | |
175 (set-buffer (get-buffer-create " *widget-tmp*")) | |
176 (erase-buffer) | |
177 (let ((standard-output (current-buffer))) | |
178 (princ object)) | |
179 (buffer-string))) | |
180 | |
181 (defun widget-clear-undo () | |
182 "Clear all undo information." | |
183 (buffer-disable-undo (current-buffer)) | |
184 (buffer-enable-undo)) | |
185 | |
17799 | 186 (defcustom widget-menu-max-size 40 |
187 "Largest number of items allowed in a popup-menu. | |
188 Larger menus are read through the minibuffer." | |
189 :group 'widgets | |
190 :type 'integer) | |
191 | |
17334 | 192 (defun widget-choose (title items &optional event) |
193 "Choose an item from a list. | |
194 | |
195 First argument TITLE is the name of the list. | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
196 Second argument ITEMS is an list whose members are either |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
197 (NAME . VALUE), to indicate selectable items, or just strings to |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
198 indicate unselectable items. |
17334 | 199 Optional third argument EVENT is an input event. |
200 | |
201 The user is asked to choose between each NAME from the items alist, | |
202 and the VALUE of the chosen element will be returned. If EVENT is a | |
203 mouse event, and the number of elements in items is less than | |
204 `widget-menu-max-size', a popup menu will be used, otherwise the | |
205 minibuffer." | |
206 (cond ((and (< (length items) widget-menu-max-size) | |
207 event (fboundp 'x-popup-menu) window-system) | |
208 ;; We are in Emacs-19, pressed by the mouse | |
209 (x-popup-menu event | |
210 (list title (cons "" items)))) | |
211 ((and (< (length items) widget-menu-max-size) | |
212 event (fboundp 'popup-menu) window-system) | |
213 ;; We are in XEmacs, pressed by the mouse | |
214 (let ((val (get-popup-menu-response | |
215 (cons title | |
216 (mapcar | |
217 (function | |
218 (lambda (x) | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
219 (if (stringp x) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
220 (vector x nil nil) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
221 (vector (car x) (list (car x)) t)))) |
17334 | 222 items))))) |
223 (setq val (and val | |
224 (listp (event-object val)) | |
225 (stringp (car-safe (event-object val))) | |
226 (car (event-object val)))) | |
227 (cdr (assoc val items)))) | |
228 (t | |
18056
f8591273bf79
(widget-default-format-handler): Don't use push.
Richard M. Stallman <rms@gnu.org>
parents:
18055
diff
changeset
|
229 (setq items (widget-remove-if 'stringp items)) |
17334 | 230 (let ((val (completing-read (concat title ": ") items nil t))) |
231 (if (stringp val) | |
232 (let ((try (try-completion val items))) | |
233 (when (stringp try) | |
234 (setq val try)) | |
235 (cdr (assoc val items))) | |
236 nil))))) | |
237 | |
18056
f8591273bf79
(widget-default-format-handler): Don't use push.
Richard M. Stallman <rms@gnu.org>
parents:
18055
diff
changeset
|
238 (defun widget-remove-if (predictate list) |
f8591273bf79
(widget-default-format-handler): Don't use push.
Richard M. Stallman <rms@gnu.org>
parents:
18055
diff
changeset
|
239 (let (result (tail list)) |
f8591273bf79
(widget-default-format-handler): Don't use push.
Richard M. Stallman <rms@gnu.org>
parents:
18055
diff
changeset
|
240 (while tail |
f8591273bf79
(widget-default-format-handler): Don't use push.
Richard M. Stallman <rms@gnu.org>
parents:
18055
diff
changeset
|
241 (or (funcall predictate (car tail)) |
f8591273bf79
(widget-default-format-handler): Don't use push.
Richard M. Stallman <rms@gnu.org>
parents:
18055
diff
changeset
|
242 (setq result (cons (car tail) result))) |
f8591273bf79
(widget-default-format-handler): Don't use push.
Richard M. Stallman <rms@gnu.org>
parents:
18055
diff
changeset
|
243 (setq tail (cdr tail))) |
f8591273bf79
(widget-default-format-handler): Don't use push.
Richard M. Stallman <rms@gnu.org>
parents:
18055
diff
changeset
|
244 (nreverse result))) |
f8591273bf79
(widget-default-format-handler): Don't use push.
Richard M. Stallman <rms@gnu.org>
parents:
18055
diff
changeset
|
245 |
17334 | 246 ;;; Widget text specifications. |
247 ;; | |
248 ;; These functions are for specifying text properties. | |
249 | |
250 (defun widget-specify-none (from to) | |
251 ;; Clear all text properties between FROM and TO. | |
252 (set-text-properties from to nil)) | |
253 | |
254 (defun widget-specify-text (from to) | |
255 ;; Default properties. | |
256 (add-text-properties from to (list 'read-only t | |
257 'front-sticky t | |
18087 | 258 'rear-nonsticky nil |
18090 | 259 'start-open nil |
260 'end-open nil))) | |
17334 | 261 |
262 (defun widget-specify-field (widget from to) | |
18090 | 263 "Specify editable button for WIDGET between FROM and TO." |
264 (put-text-property from to 'read-only nil) | |
265 (add-text-properties (1- from) from | |
266 '(rear-nonsticky t end-open t read-only from)) | |
267 (add-text-properties to (1+ to) | |
268 '(front-sticky nil start-open t read-only to)) | |
17334 | 269 (let ((map (widget-get widget :keymap)) |
18090 | 270 (face (or (widget-get widget :value-face) 'widget-field-face)) |
271 (help-echo (widget-get widget :help-echo)) | |
272 (overlay (make-overlay from to nil nil t))) | |
17334 | 273 (unless (or (stringp help-echo) (null help-echo)) |
18090 | 274 (setq help-echo 'widget-mouse-help)) |
275 (widget-put widget :field-overlay overlay) | |
276 (overlay-put overlay 'detachable nil) | |
277 (overlay-put overlay 'field widget) | |
278 (overlay-put overlay 'local-map map) | |
279 (overlay-put overlay 'keymap map) | |
280 (overlay-put overlay 'face face) | |
281 (overlay-put overlay 'balloon-help help-echo) | |
282 (overlay-put overlay 'help-echo help-echo))) | |
17334 | 283 |
284 (defun widget-specify-button (widget from to) | |
18090 | 285 "Specify button for WIDGET between FROM and TO." |
17334 | 286 (let ((face (widget-apply widget :button-face-get)) |
18090 | 287 (help-echo (widget-get widget :help-echo)) |
288 (overlay (make-overlay from to nil t nil))) | |
289 (widget-put widget :button-overlay overlay) | |
17334 | 290 (unless (or (null help-echo) (stringp help-echo)) |
291 (setq help-echo 'widget-mouse-help)) | |
18090 | 292 (overlay-put overlay 'button widget) |
293 (overlay-put overlay 'mouse-face widget-mouse-face) | |
294 (overlay-put overlay 'balloon-help help-echo) | |
295 (overlay-put overlay 'help-echo help-echo) | |
296 (overlay-put overlay 'face face))) | |
17334 | 297 |
298 (defun widget-mouse-help (extent) | |
299 "Find mouse help string for button in extent." | |
300 (let* ((widget (widget-at (extent-start-position extent))) | |
301 (help-echo (and widget (widget-get widget :help-echo)))) | |
302 (cond ((stringp help-echo) | |
303 help-echo) | |
304 ((and (symbolp help-echo) (fboundp help-echo) | |
305 (stringp (setq help-echo (funcall help-echo widget)))) | |
306 help-echo) | |
307 (t | |
308 (format "(widget %S :help-echo %S)" widget help-echo))))) | |
309 | |
310 (defun widget-specify-sample (widget from to) | |
311 ;; Specify sample for WIDGET between FROM and TO. | |
312 (let ((face (widget-apply widget :sample-face-get))) | |
313 (when face | |
314 (add-text-properties from to (list 'start-open t | |
315 'end-open t | |
316 'face face))))) | |
317 | |
318 (defun widget-specify-doc (widget from to) | |
319 ;; Specify documentation for WIDGET between FROM and TO. | |
320 (add-text-properties from to (list 'widget-doc widget | |
321 'face 'widget-documentation-face))) | |
322 | |
323 (defmacro widget-specify-insert (&rest form) | |
324 ;; Execute FORM without inheriting any text properties. | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
325 (` |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
326 (save-restriction |
17334 | 327 (let ((inhibit-read-only t) |
328 result | |
329 after-change-functions) | |
330 (insert "<>") | |
331 (narrow-to-region (- (point) 2) (point)) | |
332 (widget-specify-none (point-min) (point-max)) | |
333 (goto-char (1+ (point-min))) | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
334 (setq result (progn (,@ form))) |
17334 | 335 (delete-region (point-min) (1+ (point-min))) |
336 (delete-region (1- (point-max)) (point-max)) | |
337 (goto-char (point-max)) | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
338 result)))) |
17334 | 339 |
340 (defface widget-inactive-face '((((class grayscale color) | |
341 (background dark)) | |
342 (:foreground "light gray")) | |
343 (((class grayscale color) | |
344 (background light)) | |
345 (:foreground "dark gray")) | |
346 (t | |
347 (:italic t))) | |
348 "Face used for inactive widgets." | |
349 :group 'widgets) | |
350 | |
351 (defun widget-specify-inactive (widget from to) | |
352 "Make WIDGET inactive for user modifications." | |
353 (unless (widget-get widget :inactive) | |
354 (let ((overlay (make-overlay from to nil t nil))) | |
355 (overlay-put overlay 'face 'widget-inactive-face) | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
356 (overlay-put overlay 'evaporate t) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
357 (overlay-put overlay 'priority 100) |
17334 | 358 (overlay-put overlay (if (string-match "XEmacs" emacs-version) |
359 'read-only | |
360 'modification-hooks) '(widget-overlay-inactive)) | |
361 (widget-put widget :inactive overlay)))) | |
362 | |
363 (defun widget-overlay-inactive (&rest junk) | |
364 "Ignoring the arguments, signal an error." | |
365 (unless inhibit-read-only | |
366 (error "Attempt to modify inactive widget"))) | |
367 | |
368 | |
369 (defun widget-specify-active (widget) | |
370 "Make WIDGET active for user modifications." | |
371 (let ((inactive (widget-get widget :inactive))) | |
372 (when inactive | |
373 (delete-overlay inactive) | |
374 (widget-put widget :inactive nil)))) | |
375 | |
376 ;;; Widget Properties. | |
377 | |
378 (defsubst widget-type (widget) | |
379 "Return the type of WIDGET, a symbol." | |
380 (car widget)) | |
381 | |
382 (defun widget-put (widget property value) | |
383 "In WIDGET set PROPERTY to VALUE. | |
384 The value can later be retrived with `widget-get'." | |
385 (setcdr widget (plist-put (cdr widget) property value))) | |
386 | |
387 (defun widget-get (widget property) | |
388 "In WIDGET, get the value of PROPERTY. | |
389 The value could either be specified when the widget was created, or | |
390 later with `widget-put'." | |
391 (let ((missing t) | |
392 value tmp) | |
393 (while missing | |
394 (cond ((setq tmp (widget-plist-member (cdr widget) property)) | |
395 (setq value (car (cdr tmp)) | |
396 missing nil)) | |
397 ((setq tmp (car widget)) | |
398 (setq widget (get tmp 'widget-type))) | |
399 (t | |
400 (setq missing nil)))) | |
401 value)) | |
402 | |
403 (defun widget-member (widget property) | |
404 "Non-nil iff there is a definition in WIDGET for PROPERTY." | |
405 (cond ((widget-plist-member (cdr widget) property) | |
406 t) | |
407 ((car widget) | |
408 (widget-member (get (car widget) 'widget-type) property)) | |
409 (t nil))) | |
410 | |
411 ;;;###autoload | |
412 (defun widget-apply (widget property &rest args) | |
413 "Apply the value of WIDGET's PROPERTY to the widget itself. | |
414 ARGS are passed as extra arguments to the function." | |
415 (apply (widget-get widget property) widget args)) | |
416 | |
417 (defun widget-value (widget) | |
418 "Extract the current value of WIDGET." | |
419 (widget-apply widget | |
420 :value-to-external (widget-apply widget :value-get))) | |
421 | |
422 (defun widget-value-set (widget value) | |
423 "Set the current value of WIDGET to VALUE." | |
424 (widget-apply widget | |
425 :value-set (widget-apply widget | |
426 :value-to-internal value))) | |
427 | |
428 (defun widget-match-inline (widget vals) | |
429 ;; In WIDGET, match the start of VALS. | |
430 (cond ((widget-get widget :inline) | |
431 (widget-apply widget :match-inline vals)) | |
432 ((and vals | |
433 (widget-apply widget :match (car vals))) | |
434 (cons (list (car vals)) (cdr vals))) | |
435 (t nil))) | |
436 | |
437 (defun widget-apply-action (widget &optional event) | |
438 "Apply :action in WIDGET in response to EVENT." | |
18090 | 439 (let (after-change-functions) |
440 (if (widget-apply widget :active) | |
441 (widget-apply widget :action event) | |
442 (error "Attempt to perform action on inactive widget")))) | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
443 |
17799 | 444 ;;; Helper functions. |
445 ;; | |
446 ;; These are widget specific. | |
447 | |
448 ;;;###autoload | |
449 (defun widget-prompt-value (widget prompt &optional value unbound) | |
450 "Prompt for a value matching WIDGET, using PROMPT. | |
451 The current value is assumed to be VALUE, unless UNBOUND is non-nil." | |
452 (unless (listp widget) | |
453 (setq widget (list widget))) | |
454 (setq prompt (format "[%s] %s" (widget-type widget) prompt)) | |
455 (setq widget (widget-convert widget)) | |
456 (let ((answer (widget-apply widget :prompt-value prompt value unbound))) | |
457 (unless (widget-apply widget :match answer) | |
458 (error "Value does not match %S type." (car widget))) | |
459 answer)) | |
460 | |
461 (defun widget-get-sibling (widget) | |
462 "Get the item WIDGET is assumed to toggle. | |
463 This is only meaningful for radio buttons or checkboxes in a list." | |
464 (let* ((parent (widget-get widget :parent)) | |
465 (children (widget-get parent :children)) | |
466 child) | |
467 (catch 'child | |
468 (while children | |
469 (setq child (car children) | |
470 children (cdr children)) | |
471 (when (eq (widget-get child :button) widget) | |
472 (throw 'child child))) | |
473 nil))) | |
474 | |
17334 | 475 ;;; Glyphs. |
476 | |
477 (defcustom widget-glyph-directory (concat data-directory "custom/") | |
478 "Where widget glyphs are located. | |
479 If this variable is nil, widget will try to locate the directory | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
480 automatically." |
17334 | 481 :group 'widgets |
482 :type 'directory) | |
483 | |
484 (defcustom widget-glyph-enable t | |
485 "If non nil, use glyphs in images when available." | |
486 :group 'widgets | |
487 :type 'boolean) | |
488 | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
489 (defcustom widget-image-conversion |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
490 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg") |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
491 (xbm ".xbm")) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
492 "Conversion alist from image formats to file name suffixes." |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
493 :group 'widgets |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
494 :type '(repeat (cons :format "%v" |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
495 (symbol :tag "Image Format" unknown) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
496 (repeat :tag "Suffixes" |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
497 (string :format "%v"))))) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
498 |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
499 (defun widget-glyph-find (image tag) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
500 "Create a glyph corresponding to IMAGE with string TAG as fallback. |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
501 IMAGE should either already be a glyph, or be a file name sans |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
502 extension (xpm, xbm, gif, jpg, or png) located in |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
503 `widget-glyph-directory'." |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
504 (cond ((not (and image |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
505 (string-match "XEmacs" emacs-version) |
17334 | 506 widget-glyph-enable |
507 (fboundp 'make-glyph) | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
508 (fboundp 'locate-file) |
17334 | 509 image)) |
510 ;; We don't want or can't use glyphs. | |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
511 nil) |
17334 | 512 ((and (fboundp 'glyphp) |
513 (glyphp image)) | |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
514 ;; Already a glyph. Use it. |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
515 image) |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
516 ((stringp image) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
517 ;; A string. Look it up in relevant directories. |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
518 (let* ((dirlist (list (or widget-glyph-directory |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
519 (concat data-directory |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
520 "custom/")) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
521 data-directory)) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
522 (formats widget-image-conversion) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
523 file) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
524 (while (and formats (not file)) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
525 (if (valid-image-instantiator-format-p (car (car formats))) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
526 (setq file (locate-file image dirlist |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
527 (mapconcat 'identity |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
528 (cdr (car formats)) |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
529 ":"))) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
530 (setq formats (cdr formats)))) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
531 ;; We create a glyph with the file as the default image |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
532 ;; instantiator, and the TAG fallback |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
533 (make-glyph (if file |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
534 (list (vector (car (car formats)) ':file file) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
535 (vector 'string ':data tag)) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
536 (vector 'string ':data tag))))) |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
537 ((valid-instantiator-p image 'image) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
538 ;; A valid image instantiator (e.g. [gif :file "somefile"] etc.) |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
539 (make-glyph (list image |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
540 (vector 'string ':data tag)))) |
17334 | 541 (t |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
542 ;; Oh well. |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
543 nil))) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
544 |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
545 (defun widget-glyph-insert (widget tag image &optional down inactive) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
546 "In WIDGET, insert the text TAG or, if supported, IMAGE. |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
547 IMAGE should either be a glyph, an image instantiator, or an image file |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
548 name sans extension (xpm, xbm, gif, jpg, or png) located in |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
549 `widget-glyph-directory'. |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
550 |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
551 Optional arguments DOWN and INACTIVE is used instead of IMAGE when the |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
552 glyph is pressed or inactive, respectively. |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
553 |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
554 WARNING: If you call this with a glyph, and you want the user to be |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
555 able to invoke the glyph, make sure it is unique. If you use the |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
556 same glyph for multiple widgets, invoking any of the glyphs will |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
557 cause the last created widget to be invoked." |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
558 (let ((glyph (widget-glyph-find image tag))) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
559 (if glyph |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
560 (widget-glyph-insert-glyph widget |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
561 glyph |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
562 (widget-glyph-find down tag) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
563 (widget-glyph-find inactive tag)) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
564 (insert tag)))) |
17334 | 565 |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
566 (defun widget-glyph-insert-glyph (widget glyph &optional down inactive) |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
567 "In WIDGET, insert GLYPH. |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
568 If optional arguments DOWN and INACTIVE are given, they should be |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
569 glyphs used when the widget is pushed and inactive, respectively." |
17334 | 570 (set-glyph-property glyph 'widget widget) |
17799 | 571 (when down |
572 (set-glyph-property down 'widget widget)) | |
573 (when inactive | |
574 (set-glyph-property inactive 'widget widget)) | |
17334 | 575 (insert "*") |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
576 (let ((ext (make-extent (point) (1- (point)))) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
577 (help-echo (widget-get widget :help-echo))) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
578 (set-extent-property ext 'invisible t) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
579 (set-extent-end-glyph ext glyph) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
580 (when help-echo |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
581 (set-extent-property ext 'balloon-help help-echo) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
582 (set-extent-property ext 'help-echo help-echo))) |
17799 | 583 (widget-put widget :glyph-up glyph) |
584 (when down (widget-put widget :glyph-down down)) | |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
585 (when inactive (widget-put widget :glyph-inactive inactive))) |
17334 | 586 |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
587 ;;; Buttons. |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
588 |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
589 (defgroup widget-button nil |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
590 "The look of various kinds of buttons." |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
591 :group 'widgets) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
592 |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
593 (defcustom widget-button-prefix "" |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
594 "String used as prefix for buttons." |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
595 :type 'string |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
596 :group 'widget-button) |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
597 |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
598 (defcustom widget-button-suffix "" |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
599 "String used as suffix for buttons." |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
600 :type 'string |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
601 :group 'widget-button) |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
602 |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
603 (defun widget-button-insert-indirect (widget key) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
604 "Insert value of WIDGET's KEY property." |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
605 (let ((val (widget-get widget key))) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
606 (while (and val (symbolp val)) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
607 (setq val (symbol-value val))) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
608 (when val |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
609 (insert val)))) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
610 |
17334 | 611 ;;; Creating Widgets. |
612 | |
613 ;;;###autoload | |
614 (defun widget-create (type &rest args) | |
615 "Create widget of TYPE. | |
616 The optional ARGS are additional keyword arguments." | |
617 (let ((widget (apply 'widget-convert type args))) | |
618 (widget-apply widget :create) | |
619 widget)) | |
620 | |
621 (defun widget-create-child-and-convert (parent type &rest args) | |
622 "As part of the widget PARENT, create a child widget TYPE. | |
623 The child is converted, using the keyword arguments ARGS." | |
624 (let ((widget (apply 'widget-convert type args))) | |
625 (widget-put widget :parent parent) | |
626 (unless (widget-get widget :indent) | |
627 (widget-put widget :indent (+ (or (widget-get parent :indent) 0) | |
628 (or (widget-get widget :extra-offset) 0) | |
629 (widget-get parent :offset)))) | |
630 (widget-apply widget :create) | |
631 widget)) | |
632 | |
633 (defun widget-create-child (parent type) | |
634 "Create widget of TYPE." | |
17535
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
635 (let ((widget (copy-sequence type))) |
17334 | 636 (widget-put widget :parent parent) |
637 (unless (widget-get widget :indent) | |
638 (widget-put widget :indent (+ (or (widget-get parent :indent) 0) | |
639 (or (widget-get widget :extra-offset) 0) | |
640 (widget-get parent :offset)))) | |
641 (widget-apply widget :create) | |
642 widget)) | |
643 | |
644 (defun widget-create-child-value (parent type value) | |
645 "Create widget of TYPE with value VALUE." | |
17535
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
646 (let ((widget (copy-sequence type))) |
17334 | 647 (widget-put widget :value (widget-apply widget :value-to-internal value)) |
648 (widget-put widget :parent parent) | |
649 (unless (widget-get widget :indent) | |
650 (widget-put widget :indent (+ (or (widget-get parent :indent) 0) | |
651 (or (widget-get widget :extra-offset) 0) | |
652 (widget-get parent :offset)))) | |
653 (widget-apply widget :create) | |
654 widget)) | |
655 | |
656 ;;;###autoload | |
657 (defun widget-delete (widget) | |
658 "Delete WIDGET." | |
659 (widget-apply widget :delete)) | |
660 | |
661 (defun widget-convert (type &rest args) | |
662 "Convert TYPE to a widget without inserting it in the buffer. | |
663 The optional ARGS are additional keyword arguments." | |
664 ;; Don't touch the type. | |
665 (let* ((widget (if (symbolp type) | |
666 (list type) | |
17535
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
667 (copy-sequence type))) |
17334 | 668 (current widget) |
669 (keys args)) | |
670 ;; First set the :args keyword. | |
671 (while (cdr current) ;Look in the type. | |
672 (let ((next (car (cdr current)))) | |
673 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:)) | |
674 (setq current (cdr (cdr current))) | |
675 (setcdr current (list :args (cdr current))) | |
676 (setq current nil)))) | |
677 (while args ;Look in the args. | |
678 (let ((next (nth 0 args))) | |
679 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:)) | |
680 (setq args (nthcdr 2 args)) | |
681 (widget-put widget :args args) | |
682 (setq args nil)))) | |
683 ;; Then Convert the widget. | |
684 (setq type widget) | |
685 (while type | |
686 (let ((convert-widget (plist-get (cdr type) :convert-widget))) | |
687 (if convert-widget | |
688 (setq widget (funcall convert-widget widget)))) | |
689 (setq type (get (car type) 'widget-type))) | |
690 ;; Finally set the keyword args. | |
691 (while keys | |
692 (let ((next (nth 0 keys))) | |
693 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:)) | |
694 (progn | |
695 (widget-put widget next (nth 1 keys)) | |
696 (setq keys (nthcdr 2 keys))) | |
697 (setq keys nil)))) | |
698 ;; Convert the :value to internal format. | |
699 (if (widget-member widget :value) | |
700 (let ((value (widget-get widget :value))) | |
701 (widget-put widget | |
702 :value (widget-apply widget :value-to-internal value)))) | |
703 ;; Return the newly create widget. | |
704 widget)) | |
705 | |
706 (defun widget-insert (&rest args) | |
707 "Call `insert' with ARGS and make the text read only." | |
708 (let ((inhibit-read-only t) | |
709 after-change-functions | |
710 (from (point))) | |
711 (apply 'insert args) | |
712 (widget-specify-text from (point)))) | |
713 | |
714 ;;; Keymap and Commands. | |
715 | |
716 (defvar widget-keymap nil | |
717 "Keymap containing useful binding for buffers containing widgets. | |
718 Recommended as a parent keymap for modes using widgets.") | |
719 | |
720 (unless widget-keymap | |
721 (setq widget-keymap (make-sparse-keymap)) | |
722 (define-key widget-keymap "\C-k" 'widget-kill-line) | |
723 (define-key widget-keymap "\t" 'widget-forward) | |
724 (define-key widget-keymap "\M-\t" 'widget-backward) | |
725 (define-key widget-keymap [(shift tab)] 'widget-backward) | |
726 (define-key widget-keymap [backtab] 'widget-backward) | |
17799 | 727 (if (string-match "XEmacs" emacs-version) |
17334 | 728 (progn |
17799 | 729 ;;Glyph support. |
730 (define-key widget-keymap [button1] 'widget-button1-click) | |
731 (define-key widget-keymap [button2] 'widget-button-click)) | |
17334 | 732 (define-key widget-keymap [down-mouse-2] 'widget-button-click)) |
733 (define-key widget-keymap "\C-m" 'widget-button-press)) | |
734 | |
735 (defvar widget-global-map global-map | |
736 "Keymap used for events the widget does not handle themselves.") | |
737 (make-variable-buffer-local 'widget-global-map) | |
738 | |
739 (defvar widget-field-keymap nil | |
740 "Keymap used inside an editable field.") | |
741 | |
742 (unless widget-field-keymap | |
743 (setq widget-field-keymap (copy-keymap widget-keymap)) | |
744 (unless (string-match "XEmacs" (emacs-version)) | |
745 (define-key widget-field-keymap [menu-bar] 'nil)) | |
746 (define-key widget-field-keymap "\C-m" 'widget-field-activate) | |
747 (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line) | |
748 (define-key widget-field-keymap "\C-e" 'widget-end-of-line) | |
749 (set-keymap-parent widget-field-keymap global-map)) | |
750 | |
751 (defvar widget-text-keymap nil | |
752 "Keymap used inside a text field.") | |
753 | |
754 (unless widget-text-keymap | |
755 (setq widget-text-keymap (copy-keymap widget-keymap)) | |
756 (unless (string-match "XEmacs" (emacs-version)) | |
757 (define-key widget-text-keymap [menu-bar] 'nil)) | |
758 (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line) | |
759 (define-key widget-text-keymap "\C-e" 'widget-end-of-line) | |
760 (set-keymap-parent widget-text-keymap global-map)) | |
761 | |
762 (defun widget-field-activate (pos &optional event) | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
763 "Invoke the ediable field at point." |
17334 | 764 (interactive "@d") |
18090 | 765 (let ((field (get-char-property pos 'field))) |
17334 | 766 (if field |
767 (widget-apply-action field event) | |
768 (call-interactively | |
769 (lookup-key widget-global-map (this-command-keys)))))) | |
770 | |
17799 | 771 (defface widget-button-pressed-face |
772 '((((class color)) | |
773 (:foreground "red")) | |
774 (t | |
775 (:bold t :underline t))) | |
776 "Face used for pressed buttons." | |
777 :group 'widgets) | |
778 | |
17334 | 779 (defun widget-button-click (event) |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
780 "Invoke button below mouse pointer." |
17334 | 781 (interactive "@e") |
782 (cond ((and (fboundp 'event-glyph) | |
783 (event-glyph event)) | |
17799 | 784 (widget-glyph-click event)) |
785 ((widget-event-point event) | |
786 (let* ((pos (widget-event-point event)) | |
18090 | 787 (button (get-char-property pos 'button))) |
17334 | 788 (if button |
18090 | 789 (let* ((overlay (widget-get button :button-overlay)) |
790 (face (overlay-get overlay 'face)) | |
791 (mouse-face (overlay-get overlay 'face))) | |
17799 | 792 (unwind-protect |
793 (let ((track-mouse t)) | |
18090 | 794 (overlay-put overlay |
795 'face 'widget-button-pressed-face) | |
17799 | 796 (overlay-put overlay |
797 'mouse-face 'widget-button-pressed-face) | |
798 (unless (widget-apply button :mouse-down-action event) | |
799 (while (not (button-release-event-p event)) | |
800 (setq event (if (fboundp 'read-event) | |
801 (read-event) | |
802 (next-event)) | |
803 pos (widget-event-point event)) | |
804 (if (and pos | |
18090 | 805 (eq (get-char-property pos 'button) |
17799 | 806 button)) |
807 (progn | |
808 (overlay-put overlay | |
809 'face | |
810 'widget-button-pressed-face) | |
811 (overlay-put overlay | |
812 'mouse-face | |
813 'widget-button-pressed-face)) | |
18090 | 814 (overlay-put overlay 'face face) |
815 (overlay-put overlay 'mouse-face mouse-face)))) | |
17799 | 816 (when (and pos |
18090 | 817 (eq (get-char-property pos 'button) button)) |
17799 | 818 (widget-apply-action button event))) |
18090 | 819 (overlay-put overlay 'face face) |
820 (overlay-put overlay 'mouse-face mouse-face))) | |
17334 | 821 (call-interactively |
822 (or (lookup-key widget-global-map [ button2 ]) | |
823 (lookup-key widget-global-map [ down-mouse-2 ]) | |
824 (lookup-key widget-global-map [ mouse-2])))))) | |
825 (t | |
826 (message "You clicked somewhere weird.")))) | |
827 | |
828 (defun widget-button1-click (event) | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
829 "Invoke glyph below mouse pointer." |
17334 | 830 (interactive "@e") |
831 (if (and (fboundp 'event-glyph) | |
832 (event-glyph event)) | |
17799 | 833 (widget-glyph-click event) |
834 (call-interactively (lookup-key widget-global-map (this-command-keys))))) | |
835 | |
836 (defun widget-glyph-click (event) | |
837 "Handle click on a glyph." | |
838 (let* ((glyph (event-glyph event)) | |
839 (widget (glyph-property glyph 'widget)) | |
840 (extent (event-glyph-extent event)) | |
841 (down-glyph (or (and widget (widget-get widget :glyph-down)) glyph)) | |
842 (up-glyph (or (and widget (widget-get widget :glyph-up)) glyph)) | |
843 (last event)) | |
844 ;; Wait for the release. | |
845 (while (not (button-release-event-p last)) | |
846 (if (eq extent (event-glyph-extent last)) | |
847 (set-extent-property extent 'end-glyph down-glyph) | |
848 (set-extent-property extent 'end-glyph up-glyph)) | |
849 (setq last (next-event event))) | |
850 ;; Release glyph. | |
851 (when down-glyph | |
852 (set-extent-property extent 'end-glyph up-glyph)) | |
853 ;; Apply widget action. | |
854 (when (eq extent (event-glyph-extent last)) | |
17334 | 855 (let ((widget (glyph-property (event-glyph event) 'widget))) |
17799 | 856 (cond ((null widget) |
857 (message "You clicked on a glyph.")) | |
858 ((not (widget-apply widget :active)) | |
859 (message "This glyph is inactive.")) | |
860 (t | |
861 (widget-apply-action widget event))))))) | |
17334 | 862 |
863 (defun widget-button-press (pos &optional event) | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
864 "Invoke button at POS." |
17334 | 865 (interactive "@d") |
18090 | 866 (let ((button (get-char-property pos 'button))) |
17334 | 867 (if button |
868 (widget-apply-action button event) | |
869 (let ((command (lookup-key widget-global-map (this-command-keys)))) | |
870 (when (commandp command) | |
871 (call-interactively command)))))) | |
872 | |
873 (defun widget-move (arg) | |
874 "Move point to the ARG next field or button. | |
875 ARG may be negative to move backward." | |
18090 | 876 (or (bobp) (> arg 0) (backward-char)) |
877 (let ((pos) | |
878 (number arg) | |
879 (old (or (get-char-property (point) 'button) | |
880 (get-char-property (point) 'field))) | |
881 new) | |
882 ;; Forward. | |
883 (while (> arg 0) | |
884 (if (eobp) | |
885 (goto-char (point-min)) | |
17334 | 886 (forward-char 1)) |
18090 | 887 (and (eq pos (point)) |
888 (eq arg number) | |
889 (error "No buttons or fields found")) | |
890 (let ((new (or (get-char-property (point) 'button) | |
891 (get-char-property (point) 'field)))) | |
892 (when new | |
893 (unless (eq new old) | |
894 (unless (and (widget-get new :tab-order) | |
895 (< (widget-get new :tab-order) 0)) | |
896 (setq arg (1- arg))) | |
897 (setq old new))))) | |
898 ;; Backward. | |
899 (while (< arg 0) | |
900 (if (bobp) | |
901 (goto-char (point-max)) | |
902 (backward-char 1)) | |
903 (and (eq pos (point)) | |
904 (eq arg number) | |
905 (error "No buttons or fields found")) | |
906 (let ((new (or (get-char-property (point) 'button) | |
907 (get-char-property (point) 'field)))) | |
908 (when new | |
909 (unless (eq new old) | |
910 (unless (and (widget-get new :tab-order) | |
911 (< (widget-get new :tab-order) 0)) | |
912 (setq arg (1+ arg))))))) | |
913 (while (or (get-char-property (point) 'button) | |
914 (get-char-property (point) 'field)) | |
915 (backward-char)) | |
916 (forward-char))) | |
17334 | 917 |
918 (defun widget-forward (arg) | |
919 "Move point to the next field or button. | |
920 With optional ARG, move across that many fields." | |
921 (interactive "p") | |
922 (run-hooks 'widget-forward-hook) | |
923 (widget-move arg)) | |
924 | |
925 (defun widget-backward (arg) | |
926 "Move point to the previous field or button. | |
927 With optional ARG, move across that many fields." | |
928 (interactive "p") | |
929 (run-hooks 'widget-backward-hook) | |
930 (widget-move (- arg))) | |
931 | |
932 (defun widget-beginning-of-line () | |
933 "Go to beginning of field or beginning of line, whichever is first." | |
934 (interactive) | |
935 (let ((bol (save-excursion (beginning-of-line) (point))) | |
936 (prev (previous-single-property-change (point) 'field))) | |
937 (goto-char (max bol (or prev bol))))) | |
938 | |
939 (defun widget-end-of-line () | |
940 "Go to end of field or end of line, whichever is first." | |
941 (interactive) | |
942 (let ((bol (save-excursion (end-of-line) (point))) | |
943 (prev (next-single-property-change (point) 'field))) | |
944 (goto-char (min bol (or prev bol))))) | |
945 | |
946 (defun widget-kill-line () | |
947 "Kill to end of field or end of line, whichever is first." | |
948 (interactive) | |
18090 | 949 (let ((field (get-char-property (point) 'field)) |
18087 | 950 (newline (save-excursion (forward-line 1))) |
17334 | 951 (next (next-single-property-change (point) 'field))) |
952 (if (and field (> newline next)) | |
953 (kill-region (point) next) | |
954 (call-interactively 'kill-line)))) | |
955 | |
956 ;;; Setting up the buffer. | |
957 | |
958 (defvar widget-field-new nil) | |
959 ;; List of all newly created editable fields in the buffer. | |
960 (make-variable-buffer-local 'widget-field-new) | |
961 | |
962 (defvar widget-field-list nil) | |
963 ;; List of all editable fields in the buffer. | |
964 (make-variable-buffer-local 'widget-field-list) | |
965 | |
966 (defun widget-setup () | |
967 "Setup current buffer so editing string widgets works." | |
968 (let ((inhibit-read-only t) | |
969 (after-change-functions nil) | |
970 field) | |
971 (while widget-field-new | |
972 (setq field (car widget-field-new) | |
973 widget-field-new (cdr widget-field-new) | |
974 widget-field-list (cons field widget-field-list)) | |
18090 | 975 (let ((from (car (widget-get field :field-overlay))) |
976 (to (cdr (widget-get field :field-overlay)))) | |
17334 | 977 (widget-specify-field field from to) |
18090 | 978 (set-marker from nil) |
979 (set-marker to nil)))) | |
17334 | 980 (widget-clear-undo) |
981 ;; We need to maintain text properties and size of the editing fields. | |
982 (make-local-variable 'after-change-functions) | |
18090 | 983 (if (and widget-field-list) |
17334 | 984 (setq after-change-functions '(widget-after-change)) |
985 (setq after-change-functions nil))) | |
986 | |
987 (defvar widget-field-last nil) | |
988 ;; Last field containing point. | |
989 (make-variable-buffer-local 'widget-field-last) | |
990 | |
991 (defvar widget-field-was nil) | |
992 ;; The widget data before the change. | |
993 (make-variable-buffer-local 'widget-field-was) | |
994 | |
18090 | 995 (defun widget-field-buffer (widget) |
996 "Return the start of WIDGET's editing field." | |
997 (overlay-buffer (widget-get widget :field-overlay))) | |
998 | |
999 (defun widget-field-start (widget) | |
1000 "Return the start of WIDGET's editing field." | |
1001 (overlay-start (widget-get widget :field-overlay))) | |
1002 | |
1003 (defun widget-field-end (widget) | |
1004 "Return the end of WIDGET's editing field." | |
1005 (overlay-end (widget-get widget :field-overlay))) | |
1006 | |
17334 | 1007 (defun widget-field-find (pos) |
18090 | 1008 "Return the field at POS. |
1009 Unlike (get-char-property POS 'field) this, works with empty fields too." | |
17334 | 1010 (let ((fields widget-field-list) |
1011 field found) | |
1012 (while fields | |
1013 (setq field (car fields) | |
1014 fields (cdr fields)) | |
18090 | 1015 (let ((start (widget-field-start field)) |
1016 (end (widget-field-end field))) | |
1017 (when (and (<= start pos) (<= pos end)) | |
1018 (when found | |
1019 (debug "Overlapping fields")) | |
1020 (setq found field)))) | |
17334 | 1021 found)) |
1022 | |
1023 (defun widget-after-change (from to old) | |
1024 ;; Adjust field size and text properties. | |
1025 (condition-case nil | |
1026 (let ((field (widget-field-find from)) | |
18090 | 1027 (other (widget-field-find to))) |
1028 (when field | |
1029 (unless (eq field other) | |
1030 (debug "Change in different fields")) | |
1031 (let ((size (widget-get field :size))) | |
1032 (when size | |
1033 (let ((begin (widget-field-start field)) | |
1034 (end (widget-field-end field))) | |
1035 (cond ((< (- end begin) size) | |
1036 ;; Field too small. | |
1037 (save-excursion | |
1038 (goto-char end) | |
1039 (insert-char ?\ (- (+ begin size) end)))) | |
1040 ((> (- end begin) size) | |
1041 ;; Field too large and | |
1042 (if (or (< (point) (+ begin size)) | |
1043 (> (point) end)) | |
1044 ;; Point is outside extra space. | |
1045 (setq begin (+ begin size)) | |
1046 ;; Point is within the extra space. | |
1047 (setq begin (point))) | |
1048 (save-excursion | |
1049 (goto-char end) | |
1050 (while (and (eq (preceding-char) ?\ ) | |
1051 (> (point) begin)) | |
1052 (delete-backward-char 1)))))))) | |
1053 (widget-apply field :notify field))) | |
1054 (error (debug "After Change")))) | |
17334 | 1055 |
1056 ;;; Widget Functions | |
1057 ;; | |
1058 ;; These functions are used in the definition of multiple widgets. | |
1059 | |
17799 | 1060 (defun widget-parent-action (widget &optional event) |
1061 "Tell :parent of WIDGET to handle the :action. | |
1062 Optional EVENT is the event that triggered the action." | |
1063 (widget-apply (widget-get widget :parent) :action event)) | |
1064 | |
17334 | 1065 (defun widget-children-value-delete (widget) |
1066 "Delete all :children and :buttons in WIDGET." | |
1067 (mapcar 'widget-delete (widget-get widget :children)) | |
1068 (widget-put widget :children nil) | |
1069 (mapcar 'widget-delete (widget-get widget :buttons)) | |
1070 (widget-put widget :buttons nil)) | |
1071 | |
17799 | 1072 (defun widget-children-validate (widget) |
1073 "All the :children must be valid." | |
1074 (let ((children (widget-get widget :children)) | |
1075 child found) | |
1076 (while (and children (not found)) | |
1077 (setq child (car children) | |
1078 children (cdr children) | |
1079 found (widget-apply child :validate))) | |
1080 found)) | |
1081 | |
17334 | 1082 (defun widget-types-convert-widget (widget) |
1083 "Convert :args as widget types in WIDGET." | |
1084 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args))) | |
1085 widget) | |
1086 | |
17799 | 1087 (defun widget-value-convert-widget (widget) |
1088 "Initialize :value from :args in WIDGET." | |
1089 (let ((args (widget-get widget :args))) | |
1090 (when args | |
1091 (widget-put widget :value (car args)) | |
1092 ;; Don't convert :value here, as this is done in `widget-convert'. | |
1093 ;; (widget-put widget :value (widget-apply widget | |
1094 ;; :value-to-internal (car args))) | |
1095 (widget-put widget :args nil))) | |
1096 widget) | |
1097 | |
1098 (defun widget-value-value-get (widget) | |
1099 "Return the :value property of WIDGET." | |
1100 (widget-get widget :value)) | |
1101 | |
17334 | 1102 ;;; The `default' Widget. |
1103 | |
1104 (define-widget 'default nil | |
1105 "Basic widget other widgets are derived from." | |
1106 :value-to-internal (lambda (widget value) value) | |
1107 :value-to-external (lambda (widget value) value) | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1108 :button-prefix 'widget-button-prefix |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1109 :button-suffix 'widget-button-suffix |
17334 | 1110 :create 'widget-default-create |
1111 :indent nil | |
1112 :offset 0 | |
1113 :format-handler 'widget-default-format-handler | |
1114 :button-face-get 'widget-default-button-face-get | |
1115 :sample-face-get 'widget-default-sample-face-get | |
1116 :delete 'widget-default-delete | |
1117 :value-set 'widget-default-value-set | |
1118 :value-inline 'widget-default-value-inline | |
1119 :menu-tag-get 'widget-default-menu-tag-get | |
1120 :validate (lambda (widget) nil) | |
1121 :active 'widget-default-active | |
1122 :activate 'widget-specify-active | |
1123 :deactivate 'widget-default-deactivate | |
17799 | 1124 :mouse-down-action (lambda (widget event) nil) |
17334 | 1125 :action 'widget-default-action |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
1126 :notify 'widget-default-notify |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
1127 :prompt-value 'widget-default-prompt-value) |
17334 | 1128 |
1129 (defun widget-default-create (widget) | |
1130 "Create WIDGET at point in the current buffer." | |
1131 (widget-specify-insert | |
1132 (let ((from (point)) | |
1133 button-begin button-end | |
1134 sample-begin sample-end | |
1135 doc-begin doc-end | |
1136 value-pos) | |
1137 (insert (widget-get widget :format)) | |
1138 (goto-char from) | |
1139 ;; Parse escapes in format. | |
1140 (while (re-search-forward "%\\(.\\)" nil t) | |
1141 (let ((escape (aref (match-string 1) 0))) | |
1142 (replace-match "" t t) | |
1143 (cond ((eq escape ?%) | |
1144 (insert "%")) | |
1145 ((eq escape ?\[) | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1146 (setq button-begin (point)) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1147 (widget-button-insert-indirect widget :button-prefix)) |
17334 | 1148 ((eq escape ?\]) |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1149 (widget-button-insert-indirect widget :button-suffix) |
17334 | 1150 (setq button-end (point))) |
1151 ((eq escape ?\{) | |
1152 (setq sample-begin (point))) | |
1153 ((eq escape ?\}) | |
1154 (setq sample-end (point))) | |
1155 ((eq escape ?n) | |
1156 (when (widget-get widget :indent) | |
1157 (insert "\n") | |
1158 (insert-char ? (widget-get widget :indent)))) | |
1159 ((eq escape ?t) | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1160 (let ((glyph (widget-get widget :tag-glyph)) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1161 (tag (widget-get widget :tag))) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1162 (cond (glyph |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1163 (widget-glyph-insert widget (or tag "image") glyph)) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1164 (tag |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1165 (insert tag)) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1166 (t |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1167 (let ((standard-output (current-buffer))) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1168 (princ (widget-get widget :value))))))) |
17334 | 1169 ((eq escape ?d) |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1170 (let ((doc (widget-get widget :doc))) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1171 (when doc |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1172 (setq doc-begin (point)) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1173 (insert doc) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1174 (while (eq (preceding-char) ?\n) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1175 (delete-backward-char 1)) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1176 (insert "\n") |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1177 (setq doc-end (point))))) |
17334 | 1178 ((eq escape ?v) |
1179 (if (and button-begin (not button-end)) | |
1180 (widget-apply widget :value-create) | |
1181 (setq value-pos (point)))) | |
1182 (t | |
1183 (widget-apply widget :format-handler escape))))) | |
1184 ;; Specify button, sample, and doc, and insert value. | |
1185 (and button-begin button-end | |
1186 (widget-specify-button widget button-begin button-end)) | |
1187 (and sample-begin sample-end | |
1188 (widget-specify-sample widget sample-begin sample-end)) | |
1189 (and doc-begin doc-end | |
1190 (widget-specify-doc widget doc-begin doc-end)) | |
1191 (when value-pos | |
1192 (goto-char value-pos) | |
1193 (widget-apply widget :value-create))) | |
1194 (let ((from (copy-marker (point-min))) | |
1195 (to (copy-marker (point-max)))) | |
1196 (widget-specify-text from to) | |
1197 (set-marker-insertion-type from t) | |
1198 (set-marker-insertion-type to nil) | |
1199 (widget-put widget :from from) | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
1200 (widget-put widget :to to))) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
1201 (widget-clear-undo)) |
17334 | 1202 |
1203 (defun widget-default-format-handler (widget escape) | |
1204 ;; We recognize the %h escape by default. | |
1205 (let* ((buttons (widget-get widget :buttons)) | |
1206 (doc-property (widget-get widget :documentation-property)) | |
1207 (doc-try (cond ((widget-get widget :doc)) | |
1208 ((symbolp doc-property) | |
1209 (documentation-property (widget-get widget :value) | |
1210 doc-property)) | |
1211 (t | |
1212 (funcall doc-property (widget-get widget :value))))) | |
1213 (doc-text (and (stringp doc-try) | |
1214 (> (length doc-try) 1) | |
1215 doc-try))) | |
1216 (cond ((eq escape ?h) | |
1217 (when doc-text | |
1218 (and (eq (preceding-char) ?\n) | |
1219 (widget-get widget :indent) | |
1220 (insert-char ? (widget-get widget :indent))) | |
1221 ;; The `*' in the beginning is redundant. | |
1222 (when (eq (aref doc-text 0) ?*) | |
1223 (setq doc-text (substring doc-text 1))) | |
1224 ;; Get rid of trailing newlines. | |
1225 (when (string-match "\n+\\'" doc-text) | |
1226 (setq doc-text (substring doc-text 0 (match-beginning 0)))) | |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
1227 (push (widget-create-child-and-convert |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
1228 widget 'documentation-string |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
1229 doc-text) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
1230 buttons))) |
17334 | 1231 (t |
1232 (error "Unknown escape `%c'" escape))) | |
1233 (widget-put widget :buttons buttons))) | |
1234 | |
1235 (defun widget-default-button-face-get (widget) | |
1236 ;; Use :button-face or widget-button-face | |
1237 (or (widget-get widget :button-face) 'widget-button-face)) | |
1238 | |
1239 (defun widget-default-sample-face-get (widget) | |
1240 ;; Use :sample-face. | |
1241 (widget-get widget :sample-face)) | |
1242 | |
1243 (defun widget-default-delete (widget) | |
1244 ;; Remove widget from the buffer. | |
1245 (let ((from (widget-get widget :from)) | |
1246 (to (widget-get widget :to)) | |
18089 | 1247 (inactive-overlay (widget-get widget :inactive)) |
1248 (button-overlay (widget-get widget :button-overlay)) | |
18090 | 1249 after-change-functions |
1250 (inhibit-read-only t)) | |
17334 | 1251 (widget-apply widget :value-delete) |
18089 | 1252 (when inactive-overlay |
1253 (delete-overlay inactive-overlay)) | |
1254 (when button-overlay | |
1255 (delete-overlay button-overlay)) | |
17334 | 1256 (when (< from to) |
1257 ;; Kludge: this doesn't need to be true for empty formats. | |
1258 (delete-region from to)) | |
1259 (set-marker from nil) | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
1260 (set-marker to nil)) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
1261 (widget-clear-undo)) |
17334 | 1262 |
1263 (defun widget-default-value-set (widget value) | |
1264 ;; Recreate widget with new value. | |
1265 (save-excursion | |
1266 (goto-char (widget-get widget :from)) | |
1267 (widget-apply widget :delete) | |
1268 (widget-put widget :value value) | |
1269 (widget-apply widget :create))) | |
1270 | |
1271 (defun widget-default-value-inline (widget) | |
1272 ;; Wrap value in a list unless it is inline. | |
1273 (if (widget-get widget :inline) | |
1274 (widget-value widget) | |
1275 (list (widget-value widget)))) | |
1276 | |
1277 (defun widget-default-menu-tag-get (widget) | |
1278 ;; Use tag or value for menus. | |
1279 (or (widget-get widget :menu-tag) | |
1280 (widget-get widget :tag) | |
1281 (widget-princ-to-string (widget-get widget :value)))) | |
1282 | |
1283 (defun widget-default-active (widget) | |
1284 "Return t iff this widget active (user modifiable)." | |
1285 (and (not (widget-get widget :inactive)) | |
1286 (let ((parent (widget-get widget :parent))) | |
1287 (or (null parent) | |
1288 (widget-apply parent :active))))) | |
1289 | |
1290 (defun widget-default-deactivate (widget) | |
1291 "Make WIDGET inactive for user modifications." | |
1292 (widget-specify-inactive widget | |
1293 (widget-get widget :from) | |
1294 (widget-get widget :to))) | |
1295 | |
1296 (defun widget-default-action (widget &optional event) | |
1297 ;; Notify the parent when a widget change | |
1298 (let ((parent (widget-get widget :parent))) | |
1299 (when parent | |
1300 (widget-apply parent :notify widget event)))) | |
1301 | |
1302 (defun widget-default-notify (widget child &optional event) | |
1303 ;; Pass notification to parent. | |
1304 (widget-default-action widget event)) | |
1305 | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
1306 (defun widget-default-prompt-value (widget prompt value unbound) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
1307 ;; Read an arbitrary value. Stolen from `set-variable'. |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
1308 ;; (let ((initial (if unbound |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
1309 ;; nil |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
1310 ;; ;; It would be nice if we could do a `(cons val 1)' here. |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
1311 ;; (prin1-to-string (custom-quote value)))))) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
1312 (eval-minibuffer prompt )) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
1313 |
17334 | 1314 ;;; The `item' Widget. |
1315 | |
1316 (define-widget 'item 'default | |
1317 "Constant items for inclusion in other widgets." | |
17799 | 1318 :convert-widget 'widget-value-convert-widget |
17334 | 1319 :value-create 'widget-item-value-create |
1320 :value-delete 'ignore | |
17799 | 1321 :value-get 'widget-value-value-get |
17334 | 1322 :match 'widget-item-match |
1323 :match-inline 'widget-item-match-inline | |
1324 :action 'widget-item-action | |
1325 :format "%t\n") | |
1326 | |
1327 (defun widget-item-value-create (widget) | |
1328 ;; Insert the printed representation of the value. | |
1329 (let ((standard-output (current-buffer))) | |
1330 (princ (widget-get widget :value)))) | |
1331 | |
1332 (defun widget-item-match (widget value) | |
1333 ;; Match if the value is the same. | |
1334 (equal (widget-get widget :value) value)) | |
1335 | |
1336 (defun widget-item-match-inline (widget values) | |
1337 ;; Match if the value is the same. | |
1338 (let ((value (widget-get widget :value))) | |
1339 (and (listp value) | |
1340 (<= (length value) (length values)) | |
18056
f8591273bf79
(widget-default-format-handler): Don't use push.
Richard M. Stallman <rms@gnu.org>
parents:
18055
diff
changeset
|
1341 (let ((head (widget-sublist values 0 (length value)))) |
17334 | 1342 (and (equal head value) |
18056
f8591273bf79
(widget-default-format-handler): Don't use push.
Richard M. Stallman <rms@gnu.org>
parents:
18055
diff
changeset
|
1343 (cons head (widget-sublist values (length value)))))))) |
f8591273bf79
(widget-default-format-handler): Don't use push.
Richard M. Stallman <rms@gnu.org>
parents:
18055
diff
changeset
|
1344 |
f8591273bf79
(widget-default-format-handler): Don't use push.
Richard M. Stallman <rms@gnu.org>
parents:
18055
diff
changeset
|
1345 (defun widget-sublist (list start &optional end) |
f8591273bf79
(widget-default-format-handler): Don't use push.
Richard M. Stallman <rms@gnu.org>
parents:
18055
diff
changeset
|
1346 "Return the sublist of LIST from START to END. |
f8591273bf79
(widget-default-format-handler): Don't use push.
Richard M. Stallman <rms@gnu.org>
parents:
18055
diff
changeset
|
1347 If END is omitted, it defaults to the length of LIST." |
18090 | 1348 (if (> start 0) (setq list (nthcdr start list))) |
1349 (if end | |
1350 (if (<= end start) | |
1351 nil | |
1352 (setq list (copy-sequence list)) | |
1353 (setcdr (nthcdr (- end start 1) list) nil) | |
1354 list) | |
1355 (copy-sequence list))) | |
17334 | 1356 |
1357 (defun widget-item-action (widget &optional event) | |
1358 ;; Just notify itself. | |
1359 (widget-apply widget :notify widget event)) | |
1360 | |
1361 ;;; The `push-button' Widget. | |
1362 | |
1363 (defcustom widget-push-button-gui t | |
1364 "If non nil, use GUI push buttons when available." | |
1365 :group 'widgets | |
1366 :type 'boolean) | |
1367 | |
1368 ;; Cache already created GUI objects. | |
1369 (defvar widget-push-button-cache nil) | |
1370 | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1371 (defcustom widget-push-button-prefix "[" |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1372 "String used as prefix for buttons." |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1373 :type 'string |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1374 :group 'widget-button) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1375 |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1376 (defcustom widget-push-button-suffix "]" |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1377 "String used as suffix for buttons." |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1378 :type 'string |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1379 :group 'widget-button) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1380 |
17334 | 1381 (define-widget 'push-button 'item |
1382 "A pushable button." | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1383 :button-prefix "" |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1384 :button-suffix "" |
17334 | 1385 :value-create 'widget-push-button-value-create |
1386 :format "%[%v%]") | |
1387 | |
1388 (defun widget-push-button-value-create (widget) | |
1389 ;; Insert text representing the `on' and `off' states. | |
1390 (let* ((tag (or (widget-get widget :tag) | |
1391 (widget-get widget :value))) | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1392 (text (concat widget-push-button-prefix |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1393 tag widget-push-button-suffix)) |
17334 | 1394 (gui (cdr (assoc tag widget-push-button-cache)))) |
1395 (if (and (fboundp 'make-gui-button) | |
1396 (fboundp 'make-glyph) | |
1397 widget-push-button-gui | |
1398 (fboundp 'device-on-window-system-p) | |
1399 (device-on-window-system-p) | |
1400 (string-match "XEmacs" emacs-version)) | |
1401 (progn | |
1402 (unless gui | |
1403 (setq gui (make-gui-button tag 'widget-gui-action widget)) | |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
1404 (push (cons tag gui) widget-push-button-cache)) |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1405 (widget-glyph-insert-glyph widget |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1406 (make-glyph |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1407 (list (nth 0 (aref gui 1)) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1408 (vector 'string ':data text))) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1409 (make-glyph |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1410 (list (nth 1 (aref gui 1)) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1411 (vector 'string ':data text))) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1412 (make-glyph |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1413 (list (nth 2 (aref gui 1)) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1414 (vector 'string ':data text))))) |
17334 | 1415 (insert text)))) |
1416 | |
1417 (defun widget-gui-action (widget) | |
1418 "Apply :action for WIDGET." | |
1419 (widget-apply-action widget (this-command-keys))) | |
1420 | |
1421 ;;; The `link' Widget. | |
1422 | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1423 (defcustom widget-link-prefix "[" |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1424 "String used as prefix for links." |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1425 :type 'string |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1426 :group 'widget-button) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1427 |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1428 (defcustom widget-link-suffix "]" |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1429 "String used as suffix for links." |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1430 :type 'string |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1431 :group 'widget-button) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1432 |
17334 | 1433 (define-widget 'link 'item |
1434 "An embedded link." | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1435 :button-prefix 'widget-link-prefix |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1436 :button-suffix 'widget-link-suffix |
17334 | 1437 :help-echo "Follow the link." |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1438 :format "%[%t%]") |
17334 | 1439 |
1440 ;;; The `info-link' Widget. | |
1441 | |
1442 (define-widget 'info-link 'link | |
1443 "A link to an info file." | |
1444 :action 'widget-info-link-action) | |
1445 | |
1446 (defun widget-info-link-action (widget &optional event) | |
1447 "Open the info node specified by WIDGET." | |
17799 | 1448 (Info-goto-node (widget-value widget))) |
17334 | 1449 |
1450 ;;; The `url-link' Widget. | |
1451 | |
1452 (define-widget 'url-link 'link | |
1453 "A link to an www page." | |
1454 :action 'widget-url-link-action) | |
1455 | |
1456 (defun widget-url-link-action (widget &optional event) | |
1457 "Open the url specified by WIDGET." | |
1458 (require 'browse-url) | |
1459 (funcall browse-url-browser-function (widget-value widget))) | |
1460 | |
1461 ;;; The `editable-field' Widget. | |
1462 | |
1463 (define-widget 'editable-field 'default | |
1464 "An editable text field." | |
17799 | 1465 :convert-widget 'widget-value-convert-widget |
17334 | 1466 :keymap widget-field-keymap |
1467 :format "%v" | |
1468 :value "" | |
17799 | 1469 :prompt-internal 'widget-field-prompt-internal |
1470 :prompt-history 'widget-field-history | |
1471 :prompt-value 'widget-field-prompt-value | |
17334 | 1472 :action 'widget-field-action |
1473 :validate 'widget-field-validate | |
1474 :valid-regexp "" | |
1475 :error "No match" | |
1476 :value-create 'widget-field-value-create | |
1477 :value-delete 'widget-field-value-delete | |
1478 :value-get 'widget-field-value-get | |
1479 :match 'widget-field-match) | |
1480 | |
17799 | 1481 (defvar widget-field-history nil |
1482 "History of field minibuffer edits.") | |
1483 | |
1484 (defun widget-field-prompt-internal (widget prompt initial history) | |
1485 ;; Read string for WIDGET promptinhg with PROMPT. | |
1486 ;; INITIAL is the initial input and HISTORY is a symbol containing | |
1487 ;; the earlier input. | |
1488 (read-string prompt initial history)) | |
1489 | |
1490 (defun widget-field-prompt-value (widget prompt value unbound) | |
1491 ;; Prompt for a string. | |
1492 (let ((initial (if unbound | |
1493 nil | |
1494 (cons (widget-apply widget :value-to-internal | |
1495 value) 0))) | |
1496 (history (widget-get widget :prompt-history))) | |
1497 (let ((answer (widget-apply widget | |
1498 :prompt-internal prompt initial history))) | |
1499 (widget-apply widget :value-to-external answer)))) | |
17334 | 1500 |
1501 (defun widget-field-action (widget &optional event) | |
1502 ;; Edit the value in the minibuffer. | |
17799 | 1503 (let ((invalid (widget-apply widget :validate))) |
1504 (let ((prompt (concat (widget-apply widget :menu-tag-get) ": ")) | |
1505 (value (unless invalid | |
1506 (widget-value widget)))) | |
1507 (let ((answer (widget-apply widget :prompt-value prompt value invalid) )) | |
1508 (widget-value-set widget answer))) | |
18090 | 1509 (widget-setup) |
1510 (widget-apply widget :notify widget event))) | |
17334 | 1511 |
1512 (defun widget-field-validate (widget) | |
1513 ;; Valid if the content matches `:valid-regexp'. | |
1514 (save-excursion | |
1515 (let ((value (widget-apply widget :value-get)) | |
1516 (regexp (widget-get widget :valid-regexp))) | |
1517 (if (string-match regexp value) | |
1518 nil | |
1519 widget)))) | |
1520 | |
1521 (defun widget-field-value-create (widget) | |
1522 ;; Create an editable text field. | |
1523 (let ((size (widget-get widget :size)) | |
1524 (value (widget-get widget :value)) | |
18090 | 1525 (from (point)) |
1526 (overlay (cons (make-marker) (make-marker)))) | |
1527 (widget-put widget :field-overlay overlay) | |
17334 | 1528 (insert value) |
1529 (and size | |
1530 (< (length value) size) | |
1531 (insert-char ?\ (- size (length value)))) | |
1532 (unless (memq widget widget-field-list) | |
1533 (setq widget-field-new (cons widget widget-field-new))) | |
18090 | 1534 (move-marker (cdr overlay) (point)) |
1535 (set-marker-insertion-type (cdr overlay) nil) | |
1536 (when (null size) | |
1537 (insert ?\n)) | |
1538 (move-marker (car overlay) from) | |
1539 (set-marker-insertion-type (car overlay) t))) | |
17334 | 1540 |
1541 (defun widget-field-value-delete (widget) | |
1542 ;; Remove the widget from the list of active editing fields. | |
1543 (setq widget-field-list (delq widget widget-field-list)) | |
1544 ;; These are nil if the :format string doesn't contain `%v'. | |
18090 | 1545 (let ((overlay (widget-get widget :field-overlay))) |
1546 (when overlay | |
1547 (delete-overlay overlay)))) | |
17334 | 1548 |
1549 (defun widget-field-value-get (widget) | |
1550 ;; Return current text in editing field. | |
18090 | 1551 (let ((from (widget-field-start widget)) |
1552 (to (widget-field-end widget)) | |
1553 (buffer (widget-field-buffer widget)) | |
17334 | 1554 (size (widget-get widget :size)) |
1555 (secret (widget-get widget :secret)) | |
1556 (old (current-buffer))) | |
1557 (if (and from to) | |
1558 (progn | |
18090 | 1559 (set-buffer buffer) |
17334 | 1560 (while (and size |
1561 (not (zerop size)) | |
1562 (> to from) | |
1563 (eq (char-after (1- to)) ?\ )) | |
1564 (setq to (1- to))) | |
1565 (let ((result (buffer-substring-no-properties from to))) | |
1566 (when secret | |
1567 (let ((index 0)) | |
1568 (while (< (+ from index) to) | |
1569 (aset result index | |
18090 | 1570 (get-char-property (+ from index) 'secret)) |
17334 | 1571 (setq index (1+ index))))) |
1572 (set-buffer old) | |
1573 result)) | |
1574 (widget-get widget :value)))) | |
1575 | |
1576 (defun widget-field-match (widget value) | |
1577 ;; Match any string. | |
1578 (stringp value)) | |
1579 | |
1580 ;;; The `text' Widget. | |
1581 | |
1582 (define-widget 'text 'editable-field | |
1583 :keymap widget-text-keymap | |
1584 "A multiline text area.") | |
1585 | |
1586 ;;; The `menu-choice' Widget. | |
1587 | |
1588 (define-widget 'menu-choice 'default | |
1589 "A menu of options." | |
1590 :convert-widget 'widget-types-convert-widget | |
1591 :format "%[%t%]: %v" | |
1592 :case-fold t | |
1593 :tag "choice" | |
1594 :void '(item :format "invalid (%t)\n") | |
1595 :value-create 'widget-choice-value-create | |
1596 :value-delete 'widget-children-value-delete | |
1597 :value-get 'widget-choice-value-get | |
1598 :value-inline 'widget-choice-value-inline | |
17799 | 1599 :mouse-down-action 'widget-choice-mouse-down-action |
17334 | 1600 :action 'widget-choice-action |
1601 :error "Make a choice" | |
1602 :validate 'widget-choice-validate | |
1603 :match 'widget-choice-match | |
1604 :match-inline 'widget-choice-match-inline) | |
1605 | |
1606 (defun widget-choice-value-create (widget) | |
1607 ;; Insert the first choice that matches the value. | |
1608 (let ((value (widget-get widget :value)) | |
1609 (args (widget-get widget :args)) | |
1610 current) | |
1611 (while args | |
1612 (setq current (car args) | |
1613 args (cdr args)) | |
1614 (when (widget-apply current :match value) | |
1615 (widget-put widget :children (list (widget-create-child-value | |
1616 widget current value))) | |
1617 (widget-put widget :choice current) | |
1618 (setq args nil | |
1619 current nil))) | |
1620 (when current | |
1621 (let ((void (widget-get widget :void))) | |
1622 (widget-put widget :children (list (widget-create-child-and-convert | |
1623 widget void :value value))) | |
1624 (widget-put widget :choice void))))) | |
1625 | |
1626 (defun widget-choice-value-get (widget) | |
1627 ;; Get value of the child widget. | |
1628 (widget-value (car (widget-get widget :children)))) | |
1629 | |
1630 (defun widget-choice-value-inline (widget) | |
1631 ;; Get value of the child widget. | |
1632 (widget-apply (car (widget-get widget :children)) :value-inline)) | |
1633 | |
17799 | 1634 (defcustom widget-choice-toggle nil |
1635 "If non-nil, a binary choice will just toggle between the values. | |
1636 Otherwise, the user will explicitly have to choose between the values | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1637 when he invoked the menu." |
17799 | 1638 :type 'boolean |
1639 :group 'widgets) | |
1640 | |
1641 (defun widget-choice-mouse-down-action (widget &optional event) | |
1642 ;; Return non-nil if we need a menu. | |
1643 (let ((args (widget-get widget :args)) | |
1644 (old (widget-get widget :choice))) | |
1645 (cond ((not window-system) | |
1646 ;; No place to pop up a menu. | |
1647 nil) | |
1648 ((not (or (fboundp 'x-popup-menu) (fboundp 'popup-menu))) | |
1649 ;; No way to pop up a menu. | |
1650 nil) | |
1651 ((< (length args) 2) | |
1652 ;; Empty or singleton list, just return the value. | |
1653 nil) | |
1654 ((> (length args) widget-menu-max-size) | |
1655 ;; Too long, prompt. | |
1656 nil) | |
1657 ((> (length args) 2) | |
1658 ;; Reasonable sized list, use menu. | |
1659 t) | |
1660 ((and widget-choice-toggle (memq old args)) | |
1661 ;; We toggle. | |
1662 nil) | |
1663 (t | |
1664 ;; Ask which of the two. | |
1665 t)))) | |
1666 | |
17334 | 1667 (defun widget-choice-action (widget &optional event) |
1668 ;; Make a choice. | |
1669 (let ((args (widget-get widget :args)) | |
1670 (old (widget-get widget :choice)) | |
1671 (tag (widget-apply widget :menu-tag-get)) | |
1672 (completion-ignore-case (widget-get widget :case-fold)) | |
1673 current choices) | |
1674 ;; Remember old value. | |
1675 (if (and old (not (widget-apply widget :validate))) | |
1676 (let* ((external (widget-value widget)) | |
1677 (internal (widget-apply old :value-to-internal external))) | |
1678 (widget-put old :value internal))) | |
1679 ;; Find new choice. | |
1680 (setq current | |
1681 (cond ((= (length args) 0) | |
1682 nil) | |
1683 ((= (length args) 1) | |
1684 (nth 0 args)) | |
17799 | 1685 ((and widget-choice-toggle |
1686 (= (length args) 2) | |
17334 | 1687 (memq old args)) |
1688 (if (eq old (nth 0 args)) | |
1689 (nth 1 args) | |
1690 (nth 0 args))) | |
1691 (t | |
1692 (while args | |
1693 (setq current (car args) | |
1694 args (cdr args)) | |
1695 (setq choices | |
1696 (cons (cons (widget-apply current :menu-tag-get) | |
1697 current) | |
1698 choices))) | |
1699 (widget-choose tag (reverse choices) event)))) | |
1700 (when current | |
1701 (widget-value-set widget | |
1702 (widget-apply current :value-to-external | |
1703 (widget-get current :value))) | |
18090 | 1704 (widget-setup) |
1705 (widget-apply widget :notify widget event)))) | |
17334 | 1706 |
1707 (defun widget-choice-validate (widget) | |
1708 ;; Valid if we have made a valid choice. | |
1709 (let ((void (widget-get widget :void)) | |
1710 (choice (widget-get widget :choice)) | |
1711 (child (car (widget-get widget :children)))) | |
1712 (if (eq void choice) | |
1713 widget | |
1714 (widget-apply child :validate)))) | |
1715 | |
1716 (defun widget-choice-match (widget value) | |
1717 ;; Matches if one of the choices matches. | |
1718 (let ((args (widget-get widget :args)) | |
1719 current found) | |
1720 (while (and args (not found)) | |
1721 (setq current (car args) | |
1722 args (cdr args) | |
1723 found (widget-apply current :match value))) | |
1724 found)) | |
1725 | |
1726 (defun widget-choice-match-inline (widget values) | |
1727 ;; Matches if one of the choices matches. | |
1728 (let ((args (widget-get widget :args)) | |
1729 current found) | |
1730 (while (and args (null found)) | |
1731 (setq current (car args) | |
1732 args (cdr args) | |
1733 found (widget-match-inline current values))) | |
1734 found)) | |
1735 | |
1736 ;;; The `toggle' Widget. | |
1737 | |
1738 (define-widget 'toggle 'item | |
1739 "Toggle between two states." | |
1740 :format "%[%v%]\n" | |
1741 :value-create 'widget-toggle-value-create | |
1742 :action 'widget-toggle-action | |
1743 :match (lambda (widget value) t) | |
1744 :on "on" | |
1745 :off "off") | |
1746 | |
1747 (defun widget-toggle-value-create (widget) | |
1748 ;; Insert text representing the `on' and `off' states. | |
1749 (if (widget-value widget) | |
1750 (widget-glyph-insert widget | |
1751 (widget-get widget :on) | |
1752 (widget-get widget :on-glyph)) | |
1753 (widget-glyph-insert widget | |
1754 (widget-get widget :off) | |
1755 (widget-get widget :off-glyph)))) | |
1756 | |
1757 (defun widget-toggle-action (widget &optional event) | |
1758 ;; Toggle value. | |
1759 (widget-value-set widget (not (widget-value widget))) | |
1760 (widget-apply widget :notify widget event)) | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
1761 |
17334 | 1762 ;;; The `checkbox' Widget. |
1763 | |
1764 (define-widget 'checkbox 'toggle | |
1765 "A checkbox toggle." | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1766 :button-suffix "" |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1767 :button-prefix "" |
17334 | 1768 :format "%[%v%]" |
1769 :on "[X]" | |
1770 :on-glyph "check1" | |
1771 :off "[ ]" | |
1772 :off-glyph "check0" | |
1773 :action 'widget-checkbox-action) | |
1774 | |
1775 (defun widget-checkbox-action (widget &optional event) | |
1776 "Toggle checkbox, notify parent, and set active state of sibling." | |
1777 (widget-toggle-action widget event) | |
1778 (let ((sibling (widget-get-sibling widget))) | |
1779 (when sibling | |
1780 (if (widget-value widget) | |
1781 (widget-apply sibling :activate) | |
1782 (widget-apply sibling :deactivate))))) | |
1783 | |
1784 ;;; The `checklist' Widget. | |
1785 | |
1786 (define-widget 'checklist 'default | |
1787 "A multiple choice widget." | |
1788 :convert-widget 'widget-types-convert-widget | |
1789 :format "%v" | |
1790 :offset 4 | |
1791 :entry-format "%b %v" | |
1792 :menu-tag "checklist" | |
1793 :greedy nil | |
1794 :value-create 'widget-checklist-value-create | |
1795 :value-delete 'widget-children-value-delete | |
1796 :value-get 'widget-checklist-value-get | |
1797 :validate 'widget-checklist-validate | |
1798 :match 'widget-checklist-match | |
1799 :match-inline 'widget-checklist-match-inline) | |
1800 | |
1801 (defun widget-checklist-value-create (widget) | |
1802 ;; Insert all values | |
1803 (let ((alist (widget-checklist-match-find widget (widget-get widget :value))) | |
1804 (args (widget-get widget :args))) | |
1805 (while args | |
1806 (widget-checklist-add-item widget (car args) (assq (car args) alist)) | |
1807 (setq args (cdr args))) | |
1808 (widget-put widget :children (nreverse (widget-get widget :children))))) | |
1809 | |
1810 (defun widget-checklist-add-item (widget type chosen) | |
1811 ;; Create checklist item in WIDGET of type TYPE. | |
1812 ;; If the item is checked, CHOSEN is a cons whose cdr is the value. | |
1813 (and (eq (preceding-char) ?\n) | |
1814 (widget-get widget :indent) | |
1815 (insert-char ? (widget-get widget :indent))) | |
1816 (widget-specify-insert | |
1817 (let* ((children (widget-get widget :children)) | |
1818 (buttons (widget-get widget :buttons)) | |
1819 (button-args (or (widget-get type :sibling-args) | |
1820 (widget-get widget :button-args))) | |
1821 (from (point)) | |
1822 child button) | |
1823 (insert (widget-get widget :entry-format)) | |
1824 (goto-char from) | |
1825 ;; Parse % escapes in format. | |
1826 (while (re-search-forward "%\\([bv%]\\)" nil t) | |
1827 (let ((escape (aref (match-string 1) 0))) | |
1828 (replace-match "" t t) | |
1829 (cond ((eq escape ?%) | |
1830 (insert "%")) | |
1831 ((eq escape ?b) | |
1832 (setq button (apply 'widget-create-child-and-convert | |
1833 widget 'checkbox | |
1834 :value (not (null chosen)) | |
1835 button-args))) | |
1836 ((eq escape ?v) | |
1837 (setq child | |
1838 (cond ((not chosen) | |
1839 (let ((child (widget-create-child widget type))) | |
1840 (widget-apply child :deactivate) | |
1841 child)) | |
1842 ((widget-get type :inline) | |
1843 (widget-create-child-value | |
1844 widget type (cdr chosen))) | |
1845 (t | |
1846 (widget-create-child-value | |
1847 widget type (car (cdr chosen))))))) | |
1848 (t | |
1849 (error "Unknown escape `%c'" escape))))) | |
1850 ;; Update properties. | |
1851 (and button child (widget-put child :button button)) | |
1852 (and button (widget-put widget :buttons (cons button buttons))) | |
1853 (and child (widget-put widget :children (cons child children)))))) | |
1854 | |
1855 (defun widget-checklist-match (widget values) | |
1856 ;; All values must match a type in the checklist. | |
1857 (and (listp values) | |
1858 (null (cdr (widget-checklist-match-inline widget values))))) | |
1859 | |
1860 (defun widget-checklist-match-inline (widget values) | |
1861 ;; Find the values which match a type in the checklist. | |
1862 (let ((greedy (widget-get widget :greedy)) | |
17535
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
1863 (args (copy-sequence (widget-get widget :args))) |
17334 | 1864 found rest) |
1865 (while values | |
1866 (let ((answer (widget-checklist-match-up args values))) | |
1867 (cond (answer | |
1868 (let ((vals (widget-match-inline answer values))) | |
1869 (setq found (append found (car vals)) | |
1870 values (cdr vals) | |
1871 args (delq answer args)))) | |
1872 (greedy | |
1873 (setq rest (append rest (list (car values))) | |
1874 values (cdr values))) | |
1875 (t | |
1876 (setq rest (append rest values) | |
1877 values nil))))) | |
1878 (cons found rest))) | |
1879 | |
1880 (defun widget-checklist-match-find (widget vals) | |
1881 ;; Find the vals which match a type in the checklist. | |
1882 ;; Return an alist of (TYPE MATCH). | |
1883 (let ((greedy (widget-get widget :greedy)) | |
17535
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
1884 (args (copy-sequence (widget-get widget :args))) |
17334 | 1885 found) |
1886 (while vals | |
1887 (let ((answer (widget-checklist-match-up args vals))) | |
1888 (cond (answer | |
1889 (let ((match (widget-match-inline answer vals))) | |
1890 (setq found (cons (cons answer (car match)) found) | |
1891 vals (cdr match) | |
1892 args (delq answer args)))) | |
1893 (greedy | |
1894 (setq vals (cdr vals))) | |
1895 (t | |
1896 (setq vals nil))))) | |
1897 found)) | |
1898 | |
1899 (defun widget-checklist-match-up (args vals) | |
1900 ;; Rerturn the first type from ARGS that matches VALS. | |
1901 (let (current found) | |
1902 (while (and args (null found)) | |
1903 (setq current (car args) | |
1904 args (cdr args) | |
1905 found (widget-match-inline current vals))) | |
1906 (if found | |
1907 current | |
1908 nil))) | |
1909 | |
1910 (defun widget-checklist-value-get (widget) | |
1911 ;; The values of all selected items. | |
1912 (let ((children (widget-get widget :children)) | |
1913 child result) | |
1914 (while children | |
1915 (setq child (car children) | |
1916 children (cdr children)) | |
1917 (if (widget-value (widget-get child :button)) | |
1918 (setq result (append result (widget-apply child :value-inline))))) | |
1919 result)) | |
1920 | |
1921 (defun widget-checklist-validate (widget) | |
1922 ;; Ticked chilren must be valid. | |
1923 (let ((children (widget-get widget :children)) | |
1924 child button found) | |
1925 (while (and children (not found)) | |
1926 (setq child (car children) | |
1927 children (cdr children) | |
1928 button (widget-get child :button) | |
1929 found (and (widget-value button) | |
1930 (widget-apply child :validate)))) | |
1931 found)) | |
1932 | |
1933 ;;; The `option' Widget | |
1934 | |
1935 (define-widget 'option 'checklist | |
1936 "An widget with an optional item." | |
1937 :inline t) | |
1938 | |
1939 ;;; The `choice-item' Widget. | |
1940 | |
1941 (define-widget 'choice-item 'item | |
1942 "Button items that delegate action events to their parents." | |
17799 | 1943 :action 'widget-parent-action |
17334 | 1944 :format "%[%t%] \n") |
1945 | |
1946 ;;; The `radio-button' Widget. | |
1947 | |
1948 (define-widget 'radio-button 'toggle | |
1949 "A radio button for use in the `radio' widget." | |
1950 :notify 'widget-radio-button-notify | |
1951 :format "%[%v%]" | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1952 :button-suffix "" |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
1953 :button-prefix "" |
17334 | 1954 :on "(*)" |
1955 :on-glyph "radio1" | |
1956 :off "( )" | |
1957 :off-glyph "radio0") | |
1958 | |
1959 (defun widget-radio-button-notify (widget child &optional event) | |
1960 ;; Tell daddy. | |
1961 (widget-apply (widget-get widget :parent) :action widget event)) | |
1962 | |
1963 ;;; The `radio-button-choice' Widget. | |
1964 | |
1965 (define-widget 'radio-button-choice 'default | |
1966 "Select one of multiple options." | |
1967 :convert-widget 'widget-types-convert-widget | |
1968 :offset 4 | |
1969 :format "%v" | |
1970 :entry-format "%b %v" | |
1971 :menu-tag "radio" | |
1972 :value-create 'widget-radio-value-create | |
1973 :value-delete 'widget-children-value-delete | |
1974 :value-get 'widget-radio-value-get | |
1975 :value-inline 'widget-radio-value-inline | |
1976 :value-set 'widget-radio-value-set | |
1977 :error "You must push one of the buttons" | |
1978 :validate 'widget-radio-validate | |
1979 :match 'widget-choice-match | |
1980 :match-inline 'widget-choice-match-inline | |
1981 :action 'widget-radio-action) | |
1982 | |
1983 (defun widget-radio-value-create (widget) | |
1984 ;; Insert all values | |
1985 (let ((args (widget-get widget :args)) | |
1986 arg) | |
1987 (while args | |
1988 (setq arg (car args) | |
1989 args (cdr args)) | |
1990 (widget-radio-add-item widget arg)))) | |
1991 | |
1992 (defun widget-radio-add-item (widget type) | |
1993 "Add to radio widget WIDGET a new radio button item of type TYPE." | |
1994 ;; (setq type (widget-convert type)) | |
1995 (and (eq (preceding-char) ?\n) | |
1996 (widget-get widget :indent) | |
1997 (insert-char ? (widget-get widget :indent))) | |
1998 (widget-specify-insert | |
1999 (let* ((value (widget-get widget :value)) | |
2000 (children (widget-get widget :children)) | |
2001 (buttons (widget-get widget :buttons)) | |
2002 (button-args (or (widget-get type :sibling-args) | |
2003 (widget-get widget :button-args))) | |
2004 (from (point)) | |
2005 (chosen (and (null (widget-get widget :choice)) | |
2006 (widget-apply type :match value))) | |
2007 child button) | |
2008 (insert (widget-get widget :entry-format)) | |
2009 (goto-char from) | |
2010 ;; Parse % escapes in format. | |
2011 (while (re-search-forward "%\\([bv%]\\)" nil t) | |
2012 (let ((escape (aref (match-string 1) 0))) | |
2013 (replace-match "" t t) | |
2014 (cond ((eq escape ?%) | |
2015 (insert "%")) | |
2016 ((eq escape ?b) | |
2017 (setq button (apply 'widget-create-child-and-convert | |
2018 widget 'radio-button | |
2019 :value (not (null chosen)) | |
2020 button-args))) | |
2021 ((eq escape ?v) | |
2022 (setq child (if chosen | |
2023 (widget-create-child-value | |
2024 widget type value) | |
2025 (widget-create-child widget type))) | |
2026 (unless chosen | |
2027 (widget-apply child :deactivate))) | |
2028 (t | |
2029 (error "Unknown escape `%c'" escape))))) | |
2030 ;; Update properties. | |
2031 (when chosen | |
2032 (widget-put widget :choice type)) | |
2033 (when button | |
2034 (widget-put child :button button) | |
2035 (widget-put widget :buttons (nconc buttons (list button)))) | |
2036 (when child | |
2037 (widget-put widget :children (nconc children (list child)))) | |
2038 child))) | |
2039 | |
2040 (defun widget-radio-value-get (widget) | |
2041 ;; Get value of the child widget. | |
2042 (let ((chosen (widget-radio-chosen widget))) | |
2043 (and chosen (widget-value chosen)))) | |
2044 | |
2045 (defun widget-radio-chosen (widget) | |
2046 "Return the widget representing the chosen radio button." | |
2047 (let ((children (widget-get widget :children)) | |
2048 current found) | |
2049 (while children | |
2050 (setq current (car children) | |
2051 children (cdr children)) | |
2052 (let* ((button (widget-get current :button)) | |
2053 (value (widget-apply button :value-get))) | |
2054 (when value | |
2055 (setq found current | |
2056 children nil)))) | |
2057 found)) | |
2058 | |
2059 (defun widget-radio-value-inline (widget) | |
2060 ;; Get value of the child widget. | |
2061 (let ((children (widget-get widget :children)) | |
2062 current found) | |
2063 (while children | |
2064 (setq current (car children) | |
2065 children (cdr children)) | |
2066 (let* ((button (widget-get current :button)) | |
2067 (value (widget-apply button :value-get))) | |
2068 (when value | |
2069 (setq found (widget-apply current :value-inline) | |
2070 children nil)))) | |
2071 found)) | |
2072 | |
2073 (defun widget-radio-value-set (widget value) | |
2074 ;; We can't just delete and recreate a radio widget, since children | |
2075 ;; can be added after the original creation and won't be recreated | |
2076 ;; by `:create'. | |
2077 (let ((children (widget-get widget :children)) | |
2078 current found) | |
2079 (while children | |
2080 (setq current (car children) | |
2081 children (cdr children)) | |
2082 (let* ((button (widget-get current :button)) | |
2083 (match (and (not found) | |
2084 (widget-apply current :match value)))) | |
2085 (widget-value-set button match) | |
2086 (if match | |
2087 (progn | |
2088 (widget-value-set current value) | |
2089 (widget-apply current :activate)) | |
2090 (widget-apply current :deactivate)) | |
2091 (setq found (or found match)))))) | |
2092 | |
2093 (defun widget-radio-validate (widget) | |
2094 ;; Valid if we have made a valid choice. | |
2095 (let ((children (widget-get widget :children)) | |
2096 current found button) | |
2097 (while (and children (not found)) | |
2098 (setq current (car children) | |
2099 children (cdr children) | |
2100 button (widget-get current :button) | |
2101 found (widget-apply button :value-get))) | |
2102 (if found | |
2103 (widget-apply current :validate) | |
2104 widget))) | |
2105 | |
2106 (defun widget-radio-action (widget child event) | |
2107 ;; Check if a radio button was pressed. | |
2108 (let ((children (widget-get widget :children)) | |
2109 (buttons (widget-get widget :buttons)) | |
2110 current) | |
2111 (when (memq child buttons) | |
2112 (while children | |
2113 (setq current (car children) | |
2114 children (cdr children)) | |
2115 (let* ((button (widget-get current :button))) | |
2116 (cond ((eq child button) | |
2117 (widget-value-set button t) | |
2118 (widget-apply current :activate)) | |
2119 ((widget-value button) | |
2120 (widget-value-set button nil) | |
2121 (widget-apply current :deactivate))))))) | |
2122 ;; Pass notification to parent. | |
2123 (widget-apply widget :notify child event)) | |
2124 | |
2125 ;;; The `insert-button' Widget. | |
2126 | |
2127 (define-widget 'insert-button 'push-button | |
2128 "An insert button for the `editable-list' widget." | |
2129 :tag "INS" | |
2130 :help-echo "Insert a new item into the list at this position." | |
2131 :action 'widget-insert-button-action) | |
2132 | |
2133 (defun widget-insert-button-action (widget &optional event) | |
2134 ;; Ask the parent to insert a new item. | |
2135 (widget-apply (widget-get widget :parent) | |
2136 :insert-before (widget-get widget :widget))) | |
2137 | |
2138 ;;; The `delete-button' Widget. | |
2139 | |
2140 (define-widget 'delete-button 'push-button | |
2141 "A delete button for the `editable-list' widget." | |
2142 :tag "DEL" | |
2143 :help-echo "Delete this item from the list." | |
2144 :action 'widget-delete-button-action) | |
2145 | |
2146 (defun widget-delete-button-action (widget &optional event) | |
2147 ;; Ask the parent to insert a new item. | |
2148 (widget-apply (widget-get widget :parent) | |
2149 :delete-at (widget-get widget :widget))) | |
2150 | |
2151 ;;; The `editable-list' Widget. | |
2152 | |
2153 (defcustom widget-editable-list-gui nil | |
2154 "If non nil, use GUI push-buttons in editable list when available." | |
2155 :type 'boolean | |
2156 :group 'widgets) | |
2157 | |
2158 (define-widget 'editable-list 'default | |
2159 "A variable list of widgets of the same type." | |
2160 :convert-widget 'widget-types-convert-widget | |
2161 :offset 12 | |
2162 :format "%v%i\n" | |
2163 :format-handler 'widget-editable-list-format-handler | |
2164 :entry-format "%i %d %v" | |
2165 :menu-tag "editable-list" | |
2166 :value-create 'widget-editable-list-value-create | |
2167 :value-delete 'widget-children-value-delete | |
2168 :value-get 'widget-editable-list-value-get | |
17799 | 2169 :validate 'widget-children-validate |
17334 | 2170 :match 'widget-editable-list-match |
2171 :match-inline 'widget-editable-list-match-inline | |
2172 :insert-before 'widget-editable-list-insert-before | |
2173 :delete-at 'widget-editable-list-delete-at) | |
2174 | |
2175 (defun widget-editable-list-format-handler (widget escape) | |
2176 ;; We recognize the insert button. | |
2177 (let ((widget-push-button-gui widget-editable-list-gui)) | |
2178 (cond ((eq escape ?i) | |
2179 (and (widget-get widget :indent) | |
2180 (insert-char ? (widget-get widget :indent))) | |
2181 (apply 'widget-create-child-and-convert | |
2182 widget 'insert-button | |
2183 (widget-get widget :append-button-args))) | |
2184 (t | |
2185 (widget-default-format-handler widget escape))))) | |
2186 | |
2187 (defun widget-editable-list-value-create (widget) | |
2188 ;; Insert all values | |
2189 (let* ((value (widget-get widget :value)) | |
2190 (type (nth 0 (widget-get widget :args))) | |
2191 (inlinep (widget-get type :inline)) | |
2192 children) | |
2193 (widget-put widget :value-pos (copy-marker (point))) | |
2194 (set-marker-insertion-type (widget-get widget :value-pos) t) | |
2195 (while value | |
2196 (let ((answer (widget-match-inline type value))) | |
2197 (if answer | |
2198 (setq children (cons (widget-editable-list-entry-create | |
2199 widget | |
2200 (if inlinep | |
2201 (car answer) | |
2202 (car (car answer))) | |
2203 t) | |
2204 children) | |
2205 value (cdr answer)) | |
2206 (setq value nil)))) | |
2207 (widget-put widget :children (nreverse children)))) | |
2208 | |
2209 (defun widget-editable-list-value-get (widget) | |
2210 ;; Get value of the child widget. | |
2211 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline)) | |
2212 (widget-get widget :children)))) | |
2213 | |
2214 (defun widget-editable-list-match (widget value) | |
2215 ;; Value must be a list and all the members must match the type. | |
2216 (and (listp value) | |
2217 (null (cdr (widget-editable-list-match-inline widget value))))) | |
2218 | |
2219 (defun widget-editable-list-match-inline (widget value) | |
2220 (let ((type (nth 0 (widget-get widget :args))) | |
2221 (ok t) | |
2222 found) | |
2223 (while (and value ok) | |
2224 (let ((answer (widget-match-inline type value))) | |
2225 (if answer | |
2226 (setq found (append found (car answer)) | |
2227 value (cdr answer)) | |
2228 (setq ok nil)))) | |
2229 (cons found value))) | |
2230 | |
2231 (defun widget-editable-list-insert-before (widget before) | |
2232 ;; Insert a new child in the list of children. | |
2233 (save-excursion | |
2234 (let ((children (widget-get widget :children)) | |
2235 (inhibit-read-only t) | |
2236 after-change-functions) | |
2237 (cond (before | |
2238 (goto-char (widget-get before :entry-from))) | |
2239 (t | |
2240 (goto-char (widget-get widget :value-pos)))) | |
2241 (let ((child (widget-editable-list-entry-create | |
2242 widget nil nil))) | |
2243 (when (< (widget-get child :entry-from) (widget-get widget :from)) | |
2244 (set-marker (widget-get widget :from) | |
2245 (widget-get child :entry-from))) | |
2246 (widget-specify-text (widget-get child :entry-from) | |
2247 (widget-get child :entry-to)) | |
2248 (if (eq (car children) before) | |
2249 (widget-put widget :children (cons child children)) | |
2250 (while (not (eq (car (cdr children)) before)) | |
2251 (setq children (cdr children))) | |
2252 (setcdr children (cons child (cdr children))))))) | |
2253 (widget-setup) | |
18090 | 2254 (widget-apply widget :notify widget)) |
17334 | 2255 |
2256 (defun widget-editable-list-delete-at (widget child) | |
2257 ;; Delete child from list of children. | |
2258 (save-excursion | |
17535
4d7f2035303a
Use copy-sequence, not copy-list.
Richard M. Stallman <rms@gnu.org>
parents:
17415
diff
changeset
|
2259 (let ((buttons (copy-sequence (widget-get widget :buttons))) |
17334 | 2260 button |
2261 (inhibit-read-only t) | |
2262 after-change-functions) | |
2263 (while buttons | |
2264 (setq button (car buttons) | |
2265 buttons (cdr buttons)) | |
2266 (when (eq (widget-get button :widget) child) | |
2267 (widget-put widget | |
2268 :buttons (delq button (widget-get widget :buttons))) | |
2269 (widget-delete button)))) | |
2270 (let ((entry-from (widget-get child :entry-from)) | |
2271 (entry-to (widget-get child :entry-to)) | |
2272 (inhibit-read-only t) | |
2273 after-change-functions) | |
2274 (widget-delete child) | |
2275 (delete-region entry-from entry-to) | |
2276 (set-marker entry-from nil) | |
2277 (set-marker entry-to nil)) | |
2278 (widget-put widget :children (delq child (widget-get widget :children)))) | |
2279 (widget-setup) | |
2280 (widget-apply widget :notify widget)) | |
2281 | |
2282 (defun widget-editable-list-entry-create (widget value conv) | |
2283 ;; Create a new entry to the list. | |
2284 (let ((type (nth 0 (widget-get widget :args))) | |
2285 (widget-push-button-gui widget-editable-list-gui) | |
2286 child delete insert) | |
2287 (widget-specify-insert | |
2288 (save-excursion | |
2289 (and (widget-get widget :indent) | |
2290 (insert-char ? (widget-get widget :indent))) | |
2291 (insert (widget-get widget :entry-format))) | |
2292 ;; Parse % escapes in format. | |
2293 (while (re-search-forward "%\\(.\\)" nil t) | |
2294 (let ((escape (aref (match-string 1) 0))) | |
2295 (replace-match "" t t) | |
2296 (cond ((eq escape ?%) | |
2297 (insert "%")) | |
2298 ((eq escape ?i) | |
2299 (setq insert (apply 'widget-create-child-and-convert | |
2300 widget 'insert-button | |
2301 (widget-get widget :insert-button-args)))) | |
2302 ((eq escape ?d) | |
2303 (setq delete (apply 'widget-create-child-and-convert | |
2304 widget 'delete-button | |
2305 (widget-get widget :delete-button-args)))) | |
2306 ((eq escape ?v) | |
2307 (if conv | |
2308 (setq child (widget-create-child-value | |
2309 widget type value)) | |
2310 (setq child (widget-create-child widget type)))) | |
2311 (t | |
2312 (error "Unknown escape `%c'" escape))))) | |
2313 (widget-put widget | |
2314 :buttons (cons delete | |
2315 (cons insert | |
2316 (widget-get widget :buttons)))) | |
2317 (let ((entry-from (copy-marker (point-min))) | |
2318 (entry-to (copy-marker (point-max)))) | |
2319 (widget-specify-text entry-from entry-to) | |
2320 (set-marker-insertion-type entry-from t) | |
2321 (set-marker-insertion-type entry-to nil) | |
2322 (widget-put child :entry-from entry-from) | |
2323 (widget-put child :entry-to entry-to))) | |
2324 (widget-put insert :widget child) | |
2325 (widget-put delete :widget child) | |
2326 child)) | |
2327 | |
2328 ;;; The `group' Widget. | |
2329 | |
2330 (define-widget 'group 'default | |
2331 "A widget which group other widgets inside." | |
2332 :convert-widget 'widget-types-convert-widget | |
2333 :format "%v" | |
2334 :value-create 'widget-group-value-create | |
2335 :value-delete 'widget-children-value-delete | |
2336 :value-get 'widget-editable-list-value-get | |
17799 | 2337 :validate 'widget-children-validate |
17334 | 2338 :match 'widget-group-match |
2339 :match-inline 'widget-group-match-inline) | |
2340 | |
2341 (defun widget-group-value-create (widget) | |
2342 ;; Create each component. | |
2343 (let ((args (widget-get widget :args)) | |
2344 (value (widget-get widget :value)) | |
2345 arg answer children) | |
2346 (while args | |
2347 (setq arg (car args) | |
2348 args (cdr args) | |
2349 answer (widget-match-inline arg value) | |
2350 value (cdr answer)) | |
2351 (and (eq (preceding-char) ?\n) | |
2352 (widget-get widget :indent) | |
2353 (insert-char ? (widget-get widget :indent))) | |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2354 (push (cond ((null answer) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2355 (widget-create-child widget arg)) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2356 ((widget-get arg :inline) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2357 (widget-create-child-value widget arg (car answer))) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2358 (t |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2359 (widget-create-child-value widget arg (car (car answer))))) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2360 children)) |
17334 | 2361 (widget-put widget :children (nreverse children)))) |
2362 | |
2363 (defun widget-group-match (widget values) | |
2364 ;; Match if the components match. | |
2365 (and (listp values) | |
2366 (let ((match (widget-group-match-inline widget values))) | |
2367 (and match (null (cdr match)))))) | |
2368 | |
2369 (defun widget-group-match-inline (widget vals) | |
2370 ;; Match if the components match. | |
2371 (let ((args (widget-get widget :args)) | |
2372 argument answer found) | |
2373 (while args | |
2374 (setq argument (car args) | |
2375 args (cdr args) | |
2376 answer (widget-match-inline argument vals)) | |
2377 (if answer | |
2378 (setq vals (cdr answer) | |
2379 found (append found (car answer))) | |
2380 (setq vals nil | |
2381 args nil))) | |
2382 (if answer | |
2383 (cons found vals) | |
2384 nil))) | |
2385 | |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2386 ;;; The `visibility' Widget. |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2387 |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2388 (define-widget 'visibility 'item |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2389 "An indicator and manipulator for hidden items." |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2390 :format "%[%v%]" |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2391 :button-prefix "" |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2392 :button-suffix "" |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2393 :on "hide" |
18089 | 2394 :off "show" |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2395 :value-create 'widget-visibility-value-create |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2396 :action 'widget-toggle-action |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2397 :match (lambda (widget value) t)) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2398 |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2399 (defun widget-visibility-value-create (widget) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2400 ;; Insert text representing the `on' and `off' states. |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2401 (let ((on (widget-get widget :on)) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2402 (off (widget-get widget :off))) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2403 (if on |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2404 (setq on (concat widget-push-button-prefix |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2405 on |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2406 widget-push-button-suffix)) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2407 (setq on "")) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2408 (if off |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2409 (setq off (concat widget-push-button-prefix |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2410 off |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2411 widget-push-button-suffix)) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2412 (setq off "")) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2413 (if (widget-value widget) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2414 (widget-glyph-insert widget on "down" "down-pushed") |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2415 (widget-glyph-insert widget off "right" "right-pushed") |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2416 (insert "...")))) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2417 |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2418 ;;; The `documentation-string' Widget. |
17334 | 2419 |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2420 (define-widget 'documentation-string 'item |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2421 "A documentation string." |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2422 :format "%v" |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2423 :action 'widget-documentation-string-action |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2424 :value-delete 'widget-children-value-delete |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2425 :value-create 'widget-documentation-string-value-create) |
17334 | 2426 |
18067
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2427 (defun widget-documentation-string-value-create (widget) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2428 ;; Insert documentation string. |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2429 (let ((doc (widget-value widget)) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2430 (shown (widget-get (widget-get widget :parent) :documentation-shown))) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2431 (if (string-match "\n" doc) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2432 (let ((before (substring doc 0 (match-beginning 0))) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2433 (after (substring doc (match-beginning 0))) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2434 buttons) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2435 (insert before " ") |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2436 (push (widget-create-child-and-convert |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2437 widget 'visibility |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2438 :off nil |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2439 :action 'widget-parent-action |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2440 shown) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2441 buttons) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2442 (when shown |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2443 (insert after)) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2444 (widget-put widget :buttons buttons)) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2445 (insert doc))) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2446 (insert "\n")) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2447 |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2448 (defun widget-documentation-string-action (widget &rest ignore) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2449 ;; Toggle documentation. |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2450 (let ((parent (widget-get widget :parent))) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2451 (widget-put parent :documentation-shown |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2452 (not (widget-get parent :documentation-shown)))) |
0e2aa3b58e16
Synched with version 1.9901.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
18056
diff
changeset
|
2453 ;; Redraw. |
17334 | 2454 (widget-value-set widget (widget-value widget))) |
2455 | |
2456 ;;; The Sexp Widgets. | |
2457 | |
2458 (define-widget 'const 'item | |
2459 "An immutable sexp." | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2460 :prompt-value 'widget-const-prompt-value |
17334 | 2461 :format "%t\n%d") |
2462 | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2463 (defun widget-const-prompt-value (widget prompt value unbound) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2464 ;; Return the value of the const. |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2465 (widget-value widget)) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2466 |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2467 (define-widget 'function-item 'const |
17334 | 2468 "An immutable function name." |
2469 :format "%v\n%h" | |
2470 :documentation-property (lambda (symbol) | |
2471 (condition-case nil | |
2472 (documentation symbol t) | |
2473 (error nil)))) | |
2474 | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2475 (define-widget 'variable-item 'const |
17334 | 2476 "An immutable variable name." |
2477 :format "%v\n%h" | |
2478 :documentation-property 'variable-documentation) | |
2479 | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2480 (defvar widget-string-prompt-value-history nil |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2481 "History of input to `widget-string-prompt-value'.") |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2482 |
17799 | 2483 (define-widget 'string 'editable-field |
2484 "A string" | |
2485 :tag "String" | |
2486 :format "%{%t%}: %v" | |
2487 :prompt-history 'widget-string-prompt-value-history) | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2488 |
17334 | 2489 (define-widget 'regexp 'string |
2490 "A regular expression." | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2491 :match 'widget-regexp-match |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2492 :validate 'widget-regexp-validate |
17334 | 2493 :tag "Regexp") |
2494 | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2495 (defun widget-regexp-match (widget value) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2496 ;; Match valid regexps. |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2497 (and (stringp value) |
17799 | 2498 (condition-case nil |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2499 (prog1 t |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2500 (string-match value "")) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2501 (error nil)))) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2502 |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2503 (defun widget-regexp-validate (widget) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2504 "Check that the value of WIDGET is a valid regexp." |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2505 (let ((val (widget-value widget))) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2506 (condition-case data |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2507 (prog1 nil |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2508 (string-match val "")) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2509 (error (widget-put widget :error (error-message-string data)) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2510 widget)))) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2511 |
17334 | 2512 (define-widget 'file 'string |
2513 "A file widget. | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
2514 It will read a file name from the minibuffer when invoked." |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2515 :prompt-value 'widget-file-prompt-value |
17799 | 2516 :format "%{%t%}: %v" |
17334 | 2517 :tag "File" |
2518 :action 'widget-file-action) | |
2519 | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2520 (defun widget-file-prompt-value (widget prompt value unbound) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2521 ;; Read file from minibuffer. |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2522 (abbreviate-file-name |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2523 (if unbound |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2524 (read-file-name prompt) |
17799 | 2525 (let ((prompt2 (format "%s (default %s) " prompt value)) |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2526 (dir (file-name-directory value)) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2527 (file (file-name-nondirectory value)) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2528 (must-match (widget-get widget :must-match))) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2529 (read-file-name prompt2 dir nil must-match file))))) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2530 |
17334 | 2531 (defun widget-file-action (widget &optional event) |
2532 ;; Read a file name from the minibuffer. | |
2533 (let* ((value (widget-value widget)) | |
2534 (dir (file-name-directory value)) | |
2535 (file (file-name-nondirectory value)) | |
2536 (menu-tag (widget-apply widget :menu-tag-get)) | |
2537 (must-match (widget-get widget :must-match)) | |
2538 (answer (read-file-name (concat menu-tag ": (default `" value "') ") | |
2539 dir nil must-match file))) | |
2540 (widget-value-set widget (abbreviate-file-name answer)) | |
18090 | 2541 (widget-setup) |
2542 (widget-apply widget :notify widget event))) | |
17334 | 2543 |
2544 (define-widget 'directory 'file | |
2545 "A directory widget. | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
2546 It will read a directory name from the minibuffer when invoked." |
17334 | 2547 :tag "Directory") |
2548 | |
17799 | 2549 (defvar widget-symbol-prompt-value-history nil |
2550 "History of input to `widget-symbol-prompt-value'.") | |
2551 | |
2552 (define-widget 'symbol 'editable-field | |
17334 | 2553 "A lisp symbol." |
2554 :value nil | |
2555 :tag "Symbol" | |
17799 | 2556 :format "%{%t%}: %v" |
17334 | 2557 :match (lambda (widget value) (symbolp value)) |
17799 | 2558 :prompt-internal 'widget-symbol-prompt-internal |
2559 :prompt-match 'symbolp | |
2560 :prompt-history 'widget-symbol-prompt-value-history | |
17334 | 2561 :value-to-internal (lambda (widget value) |
2562 (if (symbolp value) | |
2563 (symbol-name value) | |
2564 value)) | |
2565 :value-to-external (lambda (widget value) | |
2566 (if (stringp value) | |
2567 (intern value) | |
2568 value))) | |
2569 | |
17799 | 2570 (defun widget-symbol-prompt-internal (widget prompt initial history) |
2571 ;; Read file from minibuffer. | |
2572 (let ((answer (completing-read prompt obarray | |
2573 (widget-get widget :prompt-match) | |
2574 nil initial history))) | |
2575 (if (and (stringp answer) | |
2576 (not (zerop (length answer)))) | |
2577 answer | |
2578 (error "No value")))) | |
2579 | |
2580 (defvar widget-function-prompt-value-history nil | |
2581 "History of input to `widget-function-prompt-value'.") | |
2582 | |
17334 | 2583 (define-widget 'function 'sexp |
2584 "A lisp function." | |
17799 | 2585 :prompt-value 'widget-field-prompt-value |
2586 :prompt-internal 'widget-symbol-prompt-internal | |
2587 :prompt-match 'fboundp | |
2588 :prompt-history 'widget-function-prompt-value-history | |
2589 :action 'widget-field-action | |
17334 | 2590 :tag "Function") |
2591 | |
17799 | 2592 (defvar widget-variable-prompt-value-history nil |
2593 "History of input to `widget-variable-prompt-value'.") | |
2594 | |
17334 | 2595 (define-widget 'variable 'symbol |
2596 ;; Should complete on variables. | |
2597 "A lisp variable." | |
17799 | 2598 :prompt-match 'boundp |
2599 :prompt-history 'widget-variable-prompt-value-history | |
17334 | 2600 :tag "Variable") |
2601 | |
17799 | 2602 (define-widget 'sexp 'editable-field |
17334 | 2603 "An arbitrary lisp expression." |
2604 :tag "Lisp expression" | |
17799 | 2605 :format "%{%t%}: %v" |
17334 | 2606 :value nil |
2607 :validate 'widget-sexp-validate | |
2608 :match (lambda (widget value) t) | |
2609 :value-to-internal 'widget-sexp-value-to-internal | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2610 :value-to-external (lambda (widget value) (read value)) |
17799 | 2611 :prompt-history 'widget-sexp-prompt-value-history |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2612 :prompt-value 'widget-sexp-prompt-value) |
17334 | 2613 |
2614 (defun widget-sexp-value-to-internal (widget value) | |
2615 ;; Use pp for printer representation. | |
18055
9e0c7dffc231
(widget-sexp-value-to-internal):
Richard M. Stallman <rms@gnu.org>
parents:
18033
diff
changeset
|
2616 (let ((pp (if (symbolp value) |
9e0c7dffc231
(widget-sexp-value-to-internal):
Richard M. Stallman <rms@gnu.org>
parents:
18033
diff
changeset
|
2617 (prin1-to-string value) |
9e0c7dffc231
(widget-sexp-value-to-internal):
Richard M. Stallman <rms@gnu.org>
parents:
18033
diff
changeset
|
2618 (pp-to-string value)))) |
17334 | 2619 (while (string-match "\n\\'" pp) |
2620 (setq pp (substring pp 0 -1))) | |
2621 (if (or (string-match "\n\\'" pp) | |
2622 (> (length pp) 40)) | |
2623 (concat "\n" pp) | |
2624 pp))) | |
2625 | |
2626 (defun widget-sexp-validate (widget) | |
2627 ;; Valid if we can read the string and there is no junk left after it. | |
2628 (save-excursion | |
2629 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*")))) | |
2630 (erase-buffer) | |
2631 (insert (widget-apply widget :value-get)) | |
2632 (goto-char (point-min)) | |
2633 (condition-case data | |
2634 (let ((value (read buffer))) | |
2635 (if (eobp) | |
2636 (if (widget-apply widget :match value) | |
2637 nil | |
2638 (widget-put widget :error (widget-get widget :type-error)) | |
2639 widget) | |
2640 (widget-put widget | |
2641 :error (format "Junk at end of expression: %s" | |
2642 (buffer-substring (point) | |
2643 (point-max)))) | |
2644 widget)) | |
2645 (error (widget-put widget :error (error-message-string data)) | |
2646 widget))))) | |
2647 | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2648 (defvar widget-sexp-prompt-value-history nil |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2649 "History of input to `widget-sexp-prompt-value'.") |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2650 |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2651 (defun widget-sexp-prompt-value (widget prompt value unbound) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2652 ;; Read an arbitrary sexp. |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2653 (let ((found (read-string prompt |
17799 | 2654 (if unbound nil (cons (prin1-to-string value) 0)) |
2655 (widget-get widget :prompt-history)))) | |
2656 (save-excursion | |
2657 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*")))) | |
2658 (erase-buffer) | |
2659 (insert found) | |
2660 (goto-char (point-min)) | |
2661 (let ((answer (read buffer))) | |
2662 (unless (eobp) | |
2663 (error "Junk at end of expression: %s" | |
2664 (buffer-substring (point) (point-max)))) | |
2665 answer))))) | |
2666 | |
17334 | 2667 (define-widget 'integer 'sexp |
2668 "An integer." | |
2669 :tag "Integer" | |
2670 :value 0 | |
2671 :type-error "This field should contain an integer" | |
2672 :value-to-internal (lambda (widget value) | |
2673 (if (integerp value) | |
2674 (prin1-to-string value) | |
2675 value)) | |
2676 :match (lambda (widget value) (integerp value))) | |
2677 | |
17799 | 2678 (define-widget 'character 'editable-field |
17334 | 2679 "An character." |
2680 :tag "Character" | |
2681 :value 0 | |
2682 :size 1 | |
2683 :format "%{%t%}: %v\n" | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2684 :valid-regexp "\\`.\\'" |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2685 :error "This field should contain a single character" |
17334 | 2686 :value-to-internal (lambda (widget value) |
17799 | 2687 (if (stringp value) |
2688 value | |
2689 (char-to-string value))) | |
17334 | 2690 :value-to-external (lambda (widget value) |
2691 (if (stringp value) | |
2692 (aref value 0) | |
2693 value)) | |
17799 | 2694 :match (lambda (widget value) |
2695 (if (fboundp 'characterp) | |
2696 (characterp value) | |
2697 (integerp value)))) | |
17334 | 2698 |
2699 (define-widget 'number 'sexp | |
2700 "A floating point number." | |
2701 :tag "Number" | |
2702 :value 0.0 | |
2703 :type-error "This field should contain a number" | |
2704 :value-to-internal (lambda (widget value) | |
2705 (if (numberp value) | |
2706 (prin1-to-string value) | |
2707 value)) | |
2708 :match (lambda (widget value) (numberp value))) | |
2709 | |
2710 (define-widget 'list 'group | |
2711 "A lisp list." | |
2712 :tag "List" | |
2713 :format "%{%t%}:\n%v") | |
2714 | |
2715 (define-widget 'vector 'group | |
2716 "A lisp vector." | |
2717 :tag "Vector" | |
2718 :format "%{%t%}:\n%v" | |
2719 :match 'widget-vector-match | |
2720 :value-to-internal (lambda (widget value) (append value nil)) | |
2721 :value-to-external (lambda (widget value) (apply 'vector value))) | |
2722 | |
2723 (defun widget-vector-match (widget value) | |
2724 (and (vectorp value) | |
2725 (widget-group-match widget | |
17415 | 2726 (widget-apply widget :value-to-internal value)))) |
17334 | 2727 |
2728 (define-widget 'cons 'group | |
2729 "A cons-cell." | |
2730 :tag "Cons-cell" | |
2731 :format "%{%t%}:\n%v" | |
2732 :match 'widget-cons-match | |
2733 :value-to-internal (lambda (widget value) | |
2734 (list (car value) (cdr value))) | |
2735 :value-to-external (lambda (widget value) | |
2736 (cons (nth 0 value) (nth 1 value)))) | |
2737 | |
2738 (defun widget-cons-match (widget value) | |
2739 (and (consp value) | |
2740 (widget-group-match widget | |
2741 (widget-apply widget :value-to-internal value)))) | |
2742 | |
2743 (define-widget 'choice 'menu-choice | |
2744 "A union of several sexp types." | |
2745 :tag "Choice" | |
17799 | 2746 :format "%[%t%]: %v" |
2747 :prompt-value 'widget-choice-prompt-value) | |
2748 | |
2749 (defun widget-choice-prompt-value (widget prompt value unbound) | |
2750 "Make a choice." | |
2751 (let ((args (widget-get widget :args)) | |
2752 (completion-ignore-case (widget-get widget :case-fold)) | |
2753 current choices old) | |
2754 ;; Find the first arg that match VALUE. | |
2755 (let ((look args)) | |
2756 (while look | |
2757 (if (widget-apply (car look) :match value) | |
2758 (setq old (car look) | |
2759 look nil) | |
2760 (setq look (cdr look))))) | |
2761 ;; Find new choice. | |
2762 (setq current | |
2763 (cond ((= (length args) 0) | |
2764 nil) | |
2765 ((= (length args) 1) | |
2766 (nth 0 args)) | |
2767 ((and (= (length args) 2) | |
2768 (memq old args)) | |
2769 (if (eq old (nth 0 args)) | |
2770 (nth 1 args) | |
2771 (nth 0 args))) | |
2772 (t | |
2773 (while args | |
2774 (setq current (car args) | |
2775 args (cdr args)) | |
2776 (setq choices | |
2777 (cons (cons (widget-apply current :menu-tag-get) | |
2778 current) | |
2779 choices))) | |
2780 (let ((val (completing-read prompt choices nil t))) | |
2781 (if (stringp val) | |
2782 (let ((try (try-completion val choices))) | |
2783 (when (stringp try) | |
2784 (setq val try)) | |
2785 (cdr (assoc val choices))) | |
2786 nil))))) | |
2787 (if current | |
2788 (widget-prompt-value current prompt nil t) | |
2789 value))) | |
17334 | 2790 |
2791 (define-widget 'radio 'radio-button-choice | |
2792 "A union of several sexp types." | |
2793 :tag "Choice" | |
17799 | 2794 :format "%{%t%}:\n%v" |
2795 :prompt-value 'widget-choice-prompt-value) | |
17334 | 2796 |
2797 (define-widget 'repeat 'editable-list | |
2798 "A variable length homogeneous list." | |
2799 :tag "Repeat" | |
2800 :format "%{%t%}:\n%v%i\n") | |
2801 | |
2802 (define-widget 'set 'checklist | |
2803 "A list of members from a fixed set." | |
2804 :tag "Set" | |
2805 :format "%{%t%}:\n%v") | |
2806 | |
2807 (define-widget 'boolean 'toggle | |
2808 "To be nil or non-nil, that is the question." | |
2809 :tag "Boolean" | |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2810 :prompt-value 'widget-boolean-prompt-value |
17799 | 2811 :format "%[%t%]: %v\n") |
17334 | 2812 |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2813 (defun widget-boolean-prompt-value (widget prompt value unbound) |
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2814 ;; Toggle a boolean. |
17799 | 2815 (y-or-n-p prompt)) |
17550
d6545cfb6c5a
Synched with custom 1.90.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17536
diff
changeset
|
2816 |
17334 | 2817 ;;; The `color' Widget. |
2818 | |
2819 (define-widget 'color-item 'choice-item | |
2820 "A color name (with sample)." | |
2821 :format "%v (%{sample%})\n" | |
2822 :sample-face-get 'widget-color-item-button-face-get) | |
2823 | |
2824 (defun widget-color-item-button-face-get (widget) | |
18033
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
2825 (let ((symbol (intern (concat "fg:" (widget-value widget))))) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
2826 (if (string-match "XEmacs" emacs-version) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
2827 (prog1 symbol |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
2828 (or (find-face symbol) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
2829 (set-face-foreground (make-face symbol) (widget-value widget)))) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
2830 (condition-case nil |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
2831 (facemenu-get-face symbol) |
bccd356a3b7c
Synched with version 1.9900.
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
17799
diff
changeset
|
2832 (error 'default))))) |
17334 | 2833 |
2834 (define-widget 'color 'push-button | |
2835 "Choose a color name (with sample)." | |
2836 :format "%[%t%]: %v" | |
2837 :tag "Color" | |
2838 :value "black" | |
2839 :value-create 'widget-color-value-create | |
2840 :value-delete 'widget-children-value-delete | |
2841 :value-get 'widget-color-value-get | |
2842 :value-set 'widget-color-value-set | |
2843 :action 'widget-color-action | |
2844 :match 'widget-field-match | |
2845 :tag "Color") | |
2846 | |
2847 (defvar widget-color-choice-list nil) | |
2848 ;; Variable holding the possible colors. | |
2849 | |
2850 (defun widget-color-choice-list () | |
2851 (unless widget-color-choice-list | |
2852 (setq widget-color-choice-list | |
2853 (mapcar '(lambda (color) (list color)) | |
2854 (x-defined-colors)))) | |
2855 widget-color-choice-list) | |
2856 | |
2857 (defun widget-color-value-create (widget) | |
2858 (let ((child (widget-create-child-and-convert | |
2859 widget 'color-item (widget-get widget :value)))) | |
2860 (widget-put widget :children (list child)))) | |
2861 | |
2862 (defun widget-color-value-get (widget) | |
2863 ;; Pass command to first child. | |
2864 (widget-apply (car (widget-get widget :children)) :value-get)) | |
2865 | |
2866 (defun widget-color-value-set (widget value) | |
2867 ;; Pass command to first child. | |
2868 (widget-apply (car (widget-get widget :children)) :value-set value)) | |
2869 | |
2870 (defvar widget-color-history nil | |
2871 "History of entered colors") | |
2872 | |
2873 (defun widget-color-action (widget &optional event) | |
2874 ;; Prompt for a color. | |
2875 (let* ((tag (widget-apply widget :menu-tag-get)) | |
2876 (prompt (concat tag ": ")) | |
2877 (answer (cond ((string-match "XEmacs" emacs-version) | |
2878 (read-color prompt)) | |
2879 ((fboundp 'x-defined-colors) | |
2880 (completing-read (concat tag ": ") | |
2881 (widget-color-choice-list) | |
2882 nil nil nil 'widget-color-history)) | |
2883 (t | |
2884 (read-string prompt (widget-value widget)))))) | |
2885 (unless (zerop (length answer)) | |
2886 (widget-value-set widget answer) | |
18090 | 2887 (widget-setup) |
2888 (widget-apply widget :notify widget event)))) | |
17334 | 2889 |
2890 ;;; The Help Echo | |
2891 | |
2892 (defun widget-echo-help-mouse () | |
2893 "Display the help message for the widget under the mouse. | |
2894 Enable with (run-with-idle-timer 1 t 'widget-echo-help-mouse)" | |
2895 (let* ((pos (mouse-position)) | |
2896 (frame (car pos)) | |
2897 (x (car (cdr pos))) | |
2898 (y (cdr (cdr pos))) | |
2899 (win (window-at x y frame)) | |
2900 (where (coordinates-in-window-p (cons x y) win))) | |
2901 (when (consp where) | |
2902 (save-window-excursion | |
2903 (progn ; save-excursion | |
2904 (select-window win) | |
2905 (let* ((result (compute-motion (window-start win) | |
2906 '(0 . 0) | |
2907 (window-end win) | |
2908 where | |
2909 (window-width win) | |
2910 (cons (window-hscroll) 0) | |
2911 win))) | |
2912 (when (and (eq (nth 1 result) x) | |
2913 (eq (nth 2 result) y)) | |
2914 (widget-echo-help (nth 0 result)))))))) | |
2915 (unless track-mouse | |
2916 (setq track-mouse t) | |
2917 (add-hook 'post-command-hook 'widget-stop-mouse-tracking))) | |
2918 | |
2919 (defun widget-stop-mouse-tracking (&rest args) | |
2920 "Stop the mouse tracking done while idle." | |
2921 (remove-hook 'post-command-hook 'widget-stop-mouse-tracking) | |
2922 (setq track-mouse nil)) | |
2923 | |
2924 (defun widget-at (pos) | |
2925 "The button or field at POS." | |
18090 | 2926 (or (get-char-property pos 'button) |
2927 (get-char-property pos 'field))) | |
17334 | 2928 |
2929 (defun widget-echo-help (pos) | |
2930 "Display the help echo for widget at POS." | |
2931 (let* ((widget (widget-at pos)) | |
2932 (help-echo (and widget (widget-get widget :help-echo)))) | |
2933 (cond ((stringp help-echo) | |
2934 (message "%s" help-echo)) | |
2935 ((and (symbolp help-echo) (fboundp help-echo) | |
2936 (stringp (setq help-echo (funcall help-echo widget)))) | |
2937 (message "%s" help-echo))))) | |
2938 | |
2939 ;;; The End: | |
2940 | |
2941 (provide 'wid-edit) | |
2942 | |
2943 ;; wid-edit.el ends here |