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