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