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