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