209
|
1 ;;; wid-edit.el --- Functions for creating and using widgets.
|
|
2 ;;
|
398
|
3 ;; Copyright (C) 1996, 1997, 2000 Free Software Foundation, Inc.
|
209
|
4 ;;
|
|
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
|
398
|
6 ;; Maintainer: Hrvoje Niksic <hniksic@xemacs.org>
|
209
|
7 ;; Keywords: extensions
|
|
8 ;; Version: 1.9960-x
|
|
9 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
|
|
10
|
|
11 ;; This file is part of XEmacs.
|
|
12
|
|
13 ;; XEmacs is free software; you can redistribute it and/or modify
|
|
14 ;; it under the terms of the GNU General Public License as published by
|
|
15 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;; any later version.
|
|
17
|
|
18 ;; XEmacs is distributed in the hope that it will be useful,
|
|
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 ;; GNU General Public License for more details.
|
|
22
|
|
23 ;; You should have received a copy of the GNU General Public License
|
|
24 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
26 ;; Boston, MA 02111-1307, USA.
|
|
27
|
|
28 ;;; Commentary:
|
|
29 ;;
|
|
30 ;; See `widget.el'.
|
|
31
|
|
32
|
|
33 ;;; Code:
|
|
34
|
|
35 (require 'widget)
|
|
36
|
|
37 (autoload 'finder-commentary "finder" nil t)
|
|
38
|
|
39 ;;; Customization.
|
|
40
|
|
41 (defgroup widgets nil
|
|
42 "Customization support for the Widget Library."
|
|
43 :link '(custom-manual "(widget)Top")
|
|
44 :link '(url-link :tag "Development Page"
|
|
45 "http://www.dina.kvl.dk/~abraham/custom/")
|
|
46 :link '(emacs-library-link :tag "Lisp File" "widget.el")
|
|
47 :prefix "widget-"
|
|
48 :group 'extensions
|
|
49 :group 'hypermedia)
|
|
50
|
|
51 (defgroup widget-documentation nil
|
398
|
52 "Options controlling the display of documentation strings."
|
209
|
53 :group 'widgets)
|
|
54
|
|
55 (defgroup widget-faces nil
|
|
56 "Faces used by the widget library."
|
|
57 :group 'widgets
|
|
58 :group 'faces)
|
|
59
|
|
60 (defvar widget-documentation-face 'widget-documentation-face
|
|
61 "Face used for documentation strings in widges.
|
|
62 This exists as a variable so it can be set locally in certain buffers.")
|
|
63
|
|
64 (defface widget-documentation-face '((((class color)
|
|
65 (background dark))
|
|
66 (:foreground "lime green"))
|
|
67 (((class color)
|
|
68 (background light))
|
|
69 (:foreground "dark green"))
|
|
70 (t nil))
|
|
71 "Face used for documentation text."
|
|
72 :group 'widget-documentation
|
|
73 :group 'widget-faces)
|
|
74
|
|
75 (defvar widget-button-face 'widget-button-face
|
|
76 "Face used for buttons in widges.
|
|
77 This exists as a variable so it can be set locally in certain buffers.")
|
|
78
|
|
79 (defface widget-button-face '((t (:bold t)))
|
|
80 "Face used for widget buttons."
|
|
81 :group 'widget-faces)
|
|
82
|
|
83 (defcustom widget-mouse-face 'highlight
|
|
84 "Face used for widget buttons when the mouse is above them."
|
|
85 :type 'face
|
|
86 :group 'widget-faces)
|
|
87
|
|
88 (defface widget-field-face '((((class grayscale color)
|
|
89 (background light))
|
|
90 (:background "gray85"))
|
|
91 (((class grayscale color)
|
|
92 (background dark))
|
|
93 (:background "dim gray"))
|
|
94 (t
|
|
95 (:italic t)))
|
|
96 "Face used for editable fields."
|
|
97 :group 'widget-faces)
|
|
98
|
|
99 ;; Currently unused
|
|
100 ;(defface widget-single-line-field-face '((((class grayscale color)
|
|
101 ; (background light))
|
|
102 ; (:background "gray85"))
|
|
103 ; (((class grayscale color)
|
|
104 ; (background dark))
|
|
105 ; (:background "dim gray"))
|
|
106 ; (t
|
|
107 ; (:italic t)))
|
|
108 ; "Face used for editable fields spanning only a single line."
|
|
109 ; :group 'widget-faces)
|
|
110 ;
|
|
111 ;(defvar widget-single-line-display-table
|
|
112 ; (let ((table (make-display-table)))
|
|
113 ; (aset table 9 "^I")
|
|
114 ; (aset table 10 "^J")
|
|
115 ; table)
|
|
116 ; "Display table used for single-line editable fields.")
|
|
117 ;
|
|
118 ;(set-face-display-table 'widget-single-line-field-face
|
|
119 ; widget-single-line-display-table)
|
|
120
|
|
121
|
|
122 ;; Some functions from this file have been ported to C for speed.
|
|
123 ;; Setting this to t (*before* loading wid-edit.el) will make them
|
|
124 ;; shadow the subrs. It should be used only for debugging purposes.
|
|
125 (defvar widget-shadow-subrs nil)
|
|
126
|
|
127
|
|
128 ;;; Utility functions.
|
|
129 ;;
|
|
130 ;; These are not really widget specific.
|
|
131
|
|
132 (when (or (not (fboundp 'widget-plist-member))
|
|
133 widget-shadow-subrs)
|
|
134 ;; Recoded in C, for efficiency. It used to be a defsubst, but old
|
|
135 ;; compiled code won't fail -- it will just be slower.
|
|
136 (defun widget-plist-member (plist prop)
|
|
137 ;; Return non-nil if PLIST has the property PROP.
|
|
138 ;; PLIST is a property list, which is a list of the form
|
|
139 ;; (PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol.
|
|
140 ;; Unlike `plist-get', this allows you to distinguish between a missing
|
|
141 ;; property and a property with the value nil.
|
|
142 ;; The value is actually the tail of PLIST whose car is PROP.
|
|
143 (while (and plist (not (eq (car plist) prop)))
|
|
144 (setq plist (cddr plist)))
|
|
145 plist))
|
|
146
|
|
147 (defun widget-princ-to-string (object)
|
|
148 ;; Return string representation of OBJECT, any Lisp object.
|
|
149 ;; No quoting characters are used; no delimiters are printed around
|
|
150 ;; the contents of strings.
|
|
151 (with-current-buffer (get-buffer-create " *widget-tmp*")
|
|
152 (erase-buffer)
|
|
153 (princ object (current-buffer))
|
|
154 (buffer-string)))
|
|
155
|
219
|
156 (defun widget-prettyprint-to-string (object)
|
|
157 ;; Like pp-to-string, but uses `cl-prettyprint'
|
|
158 (with-current-buffer (get-buffer-create " *widget-tmp*")
|
|
159 (erase-buffer)
|
|
160 (cl-prettyprint object)
|
221
|
161 ;; `cl-prettyprint' always surrounds the text with newlines.
|
|
162 (when (eq (char-after (point-min)) ?\n)
|
|
163 (delete-region (point-min) (1+ (point-min))))
|
|
164 (when (eq (char-before (point-max)) ?\n)
|
|
165 (delete-region (1- (point-max)) (point-max)))
|
219
|
166 (buffer-string)))
|
|
167
|
209
|
168 (defun widget-clear-undo ()
|
|
169 "Clear all undo information."
|
|
170 (buffer-disable-undo)
|
|
171 (buffer-enable-undo))
|
|
172
|
|
173 (defcustom widget-menu-max-size 40
|
|
174 "Largest number of items allowed in a popup-menu.
|
|
175 Larger menus are read through the minibuffer."
|
|
176 :group 'widgets
|
|
177 :type 'integer)
|
|
178
|
|
179 (defcustom widget-menu-minibuffer-flag nil
|
|
180 "*Control how to ask for a choice from the keyboard.
|
|
181 Non-nil means use the minibuffer;
|
|
182 nil means read a single character."
|
|
183 :group 'widgets
|
|
184 :type 'boolean)
|
|
185
|
|
186 (defun widget-choose (title items &optional event)
|
|
187 "Choose an item from a list.
|
|
188
|
|
189 First argument TITLE is the name of the list.
|
|
190 Second argument ITEMS is an list whose members are either
|
|
191 (NAME . VALUE), to indicate selectable items, or just strings to
|
|
192 indicate unselectable items.
|
|
193 Optional third argument EVENT is an input event.
|
|
194
|
|
195 The user is asked to choose between each NAME from the items alist,
|
|
196 and the VALUE of the chosen element will be returned. If EVENT is a
|
|
197 mouse event, and the number of elements in items is less than
|
|
198 `widget-menu-max-size', a popup menu will be used, otherwise the
|
|
199 minibuffer."
|
|
200 (cond ((and (< (length items) widget-menu-max-size)
|
|
201 event
|
|
202 (console-on-window-system-p))
|
|
203 ;; Pressed by the mouse.
|
|
204 (let ((val (get-popup-menu-response
|
|
205 (cons title
|
|
206 (mapcar (lambda (x)
|
|
207 (if (stringp x)
|
|
208 (vector x nil nil)
|
|
209 (vector (car x) (list (car x)) t)))
|
|
210 items)))))
|
|
211 (setq val (and val
|
|
212 (listp (event-object val))
|
|
213 (stringp (car-safe (event-object val)))
|
|
214 (car (event-object val))))
|
|
215 (cdr (assoc val items))))
|
|
216 ((and (not widget-menu-minibuffer-flag)
|
|
217 ;; Can't handle more than 10 items (as many digits)
|
|
218 (<= (length items) 10))
|
|
219 ;; Construct a menu of the choices
|
|
220 ;; and then use it for prompting for a single character.
|
|
221 (let* ((overriding-terminal-local-map (make-sparse-keymap))
|
|
222 (map (make-sparse-keymap title))
|
|
223 (next-digit ?0)
|
|
224 some-choice-enabled value)
|
|
225 ;; Define SPC as a prefix char to get to this menu.
|
|
226 (define-key overriding-terminal-local-map " " map)
|
|
227 (with-current-buffer (get-buffer-create " widget-choose")
|
|
228 (erase-buffer)
|
|
229 (insert "Available choices:\n\n")
|
|
230 (dolist (choice items)
|
|
231 (when (consp choice)
|
|
232 (let* ((name (car choice))
|
|
233 (function (cdr choice)))
|
|
234 (insert (format "%c = %s\n" next-digit name))
|
|
235 (define-key map (vector next-digit) function)
|
|
236 (setq some-choice-enabled t)))
|
|
237 ;; Allocate digits to disabled alternatives
|
|
238 ;; so that the digit of a given alternative never varies.
|
|
239 (incf next-digit))
|
|
240 (insert "\nC-g = Quit"))
|
|
241 (or some-choice-enabled
|
|
242 (error "None of the choices is currently meaningful"))
|
|
243 (define-key map [?\C-g] 'keyboard-quit)
|
|
244 (define-key map [t] 'keyboard-quit)
|
|
245 ;(setcdr map (nreverse (cdr map)))
|
|
246 ;; Unread a SPC to lead to our new menu.
|
|
247 (push (character-to-event ?\ ) unread-command-events)
|
|
248 ;; Read a char with the menu, and return the result
|
|
249 ;; that corresponds to it.
|
|
250 (save-window-excursion
|
|
251 (display-buffer (get-buffer " widget-choose"))
|
|
252 (let ((cursor-in-echo-area t))
|
|
253 (setq value
|
|
254 (lookup-key overriding-terminal-local-map
|
|
255 (read-key-sequence (concat title ": ") t)))))
|
|
256 (message "")
|
|
257 (when (or (eq value 'keyboard-quit)
|
|
258 (null value))
|
|
259 (error "Canceled"))
|
|
260 value))
|
|
261 (t
|
|
262 ;; Read the choice of name from the minibuffer.
|
|
263 (setq items (remove-if 'stringp items))
|
|
264 (let ((val (completing-read (concat title ": ") items nil t)))
|
|
265 (if (stringp val)
|
|
266 (let ((try (try-completion val items)))
|
|
267 (when (stringp try)
|
|
268 (setq val try))
|
|
269 (cdr (assoc val items)))
|
|
270 nil)))))
|
|
271
|
|
272
|
|
273 ;;; Widget text specifications.
|
|
274 ;;
|
|
275 ;; These functions are for specifying text properties.
|
|
276
|
|
277 (defcustom widget-field-add-space t
|
|
278 ;; Setting this to nil might be available, once some problems are resolved.
|
|
279 "Non-nil means add extra space at the end of editable text fields.
|
|
280
|
|
281 This is needed on all versions of Emacs. If you don't add the space,
|
|
282 it will become impossible to edit a zero size field."
|
|
283 :type 'boolean
|
|
284 :group 'widgets)
|
|
285
|
|
286 (defcustom widget-field-use-before-change
|
|
287 (and (or (> emacs-minor-version 34)
|
|
288 (> emacs-major-version 19))
|
|
289 (not (string-match "XEmacs" emacs-version)))
|
|
290 "Non-nil means use `before-change-functions' to track editable fields.
|
|
291 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
|
|
292 Using before hooks also means that the :notify function can't know the
|
|
293 new value."
|
|
294 :type 'boolean
|
|
295 :group 'widgets)
|
|
296
|
276
|
297 (defun widget-echo-this-extent (extent)
|
|
298 (let* ((widget (or (extent-property extent 'button)
|
|
299 (extent-property extent 'field)
|
|
300 (extent-property extent 'glyph-widget)))
|
|
301 (help-echo (and widget (widget-get widget :help-echo))))
|
|
302 (and (functionp help-echo)
|
|
303 (setq help-echo (funcall help-echo widget)))
|
|
304 (when (stringp help-echo)
|
382
|
305 (setq help-echo-owns-message t)
|
276
|
306 (display-message 'help-echo help-echo))))
|
|
307
|
|
308 (defsubst widget-handle-help-echo (extent help-echo)
|
|
309 (set-extent-property extent 'balloon-help help-echo)
|
|
310 (set-extent-property extent 'help-echo help-echo)
|
|
311 (when (functionp help-echo)
|
|
312 (set-extent-property extent 'balloon-help 'widget-echo-this-extent)
|
|
313 (set-extent-property extent 'help-echo 'widget-echo-this-extent)))
|
|
314
|
209
|
315 (defun widget-specify-field (widget from to)
|
|
316 "Specify editable button for WIDGET between FROM and TO."
|
|
317 (save-excursion
|
|
318 (goto-char to)
|
|
319 (cond ((null (widget-get widget :size))
|
|
320 (forward-char 1))
|
|
321 ;; Terminating space is not part of the field, but necessary in
|
|
322 ;; order for local-map to work. Remove next sexp if local-map works
|
|
323 ;; at the end of the extent.
|
|
324 (widget-field-add-space
|
|
325 (insert-and-inherit " ")))
|
|
326 (setq to (point)))
|
|
327 (let ((map (widget-get widget :keymap))
|
|
328 (face (or (widget-get widget :value-face) 'widget-field-face))
|
|
329 (help-echo (widget-get widget :help-echo))
|
|
330 (extent (make-extent from to)))
|
|
331 (unless (or (stringp help-echo) (null help-echo))
|
|
332 (setq help-echo 'widget-mouse-help))
|
|
333 (widget-put widget :field-extent extent)
|
|
334 (and (or (not widget-field-add-space)
|
|
335 (widget-get widget :size))
|
|
336 (set-extent-property extent 'end-closed nil))
|
|
337 (set-extent-property extent 'detachable nil)
|
|
338 (set-extent-property extent 'field widget)
|
|
339 (set-extent-property extent 'button-or-field t)
|
|
340 (set-extent-property extent 'keymap map)
|
|
341 (set-extent-property extent 'face face)
|
290
|
342 (widget-handle-help-echo extent help-echo))
|
|
343 (widget-specify-secret widget))
|
|
344
|
|
345 (defun widget-specify-secret (field)
|
|
346 "Replace text in FIELD with value of `:secret', if non-nil."
|
|
347 (let ((secret (widget-get field :secret))
|
|
348 (size (widget-get field :size)))
|
|
349 (when secret
|
|
350 (let ((begin (widget-field-start field))
|
|
351 (end (widget-field-end field)))
|
|
352 (when size
|
|
353 (while (and (> end begin)
|
|
354 (eq (char-after (1- end)) ?\ ))
|
|
355 (setq end (1- end))))
|
|
356 (while (< begin end)
|
|
357 (let ((old (char-after begin)))
|
|
358 (unless (eq old secret)
|
|
359 (subst-char-in-region begin (1+ begin) old secret)
|
|
360 (put-text-property begin (1+ begin) 'secret old))
|
|
361 (setq begin (1+ begin))))))))
|
209
|
362
|
|
363 (defun widget-specify-button (widget from to)
|
|
364 "Specify button for WIDGET between FROM and TO."
|
|
365 (let ((face (widget-apply widget :button-face-get))
|
|
366 (help-echo (widget-get widget :help-echo))
|
|
367 (extent (make-extent from to))
|
|
368 (map (widget-get widget :button-keymap)))
|
|
369 (widget-put widget :button-extent extent)
|
|
370 (unless (or (null help-echo) (stringp help-echo))
|
|
371 (setq help-echo 'widget-mouse-help))
|
|
372 (set-extent-property extent 'start-open t)
|
|
373 (set-extent-property extent 'button widget)
|
|
374 (set-extent-property extent 'button-or-field t)
|
|
375 (set-extent-property extent 'mouse-face widget-mouse-face)
|
276
|
376 (widget-handle-help-echo extent help-echo)
|
209
|
377 (set-extent-property extent 'face face)
|
|
378 (set-extent-property extent 'keymap map)))
|
|
379
|
|
380 (defun widget-mouse-help (extent)
|
|
381 "Find mouse help string for button in extent."
|
|
382 (let* ((widget (widget-at (extent-start-position extent)))
|
|
383 (help-echo (and widget (widget-get widget :help-echo))))
|
|
384 (cond ((stringp help-echo)
|
|
385 help-echo)
|
|
386 ((and (functionp help-echo)
|
|
387 (stringp (setq help-echo (funcall help-echo widget))))
|
|
388 help-echo)
|
|
389 (t
|
|
390 (format "(widget %S :help-echo %S)" widget help-echo)))))
|
|
391
|
|
392 (defun widget-specify-sample (widget from to)
|
|
393 ;; Specify sample for WIDGET between FROM and TO.
|
|
394 (let ((face (widget-apply widget :sample-face-get))
|
|
395 (extent (make-extent from to nil)))
|
|
396 (set-extent-property extent 'start-open t)
|
|
397 (set-extent-property extent 'face face)
|
|
398 (widget-put widget :sample-extent extent)))
|
|
399
|
|
400 (defun widget-specify-doc (widget from to)
|
|
401 ;; Specify documentation for WIDGET between FROM and TO.
|
|
402 (let ((extent (make-extent from to)))
|
|
403 (set-extent-property extent 'start-open t)
|
|
404 (set-extent-property extent 'widget-doc widget)
|
|
405 (set-extent-property extent 'face widget-documentation-face)
|
|
406 (widget-put widget :doc-extent extent)))
|
|
407
|
|
408 (defmacro widget-specify-insert (&rest form)
|
|
409 ;; Execute FORM without inheriting any text properties.
|
|
410 `(save-restriction
|
|
411 (let ((inhibit-read-only t)
|
|
412 before-change-functions
|
|
413 after-change-functions)
|
|
414 (insert "<>")
|
|
415 (narrow-to-region (- (point) 2) (point))
|
|
416 (goto-char (1+ (point-min)))
|
|
417 ;; We use `prog1' instead of a `result' variable, as the latter
|
|
418 ;; confuses the byte-compiler in some cases (a warning).
|
|
419 (prog1 (progn ,@form)
|
|
420 (delete-region (point-min) (1+ (point-min)))
|
|
421 (delete-region (1- (point-max)) (point-max))
|
|
422 (goto-char (point-max))))))
|
|
423
|
|
424 (put 'widget-specify-insert 'edebug-form-spec '(&rest form))
|
|
425
|
|
426
|
|
427 ;;; Inactive Widgets.
|
|
428
|
|
429 (defface widget-inactive-face '((((class grayscale color)
|
|
430 (background dark))
|
|
431 (:foreground "light gray"))
|
|
432 (((class grayscale color)
|
|
433 (background light))
|
|
434 (:foreground "dim gray"))
|
|
435 (t
|
|
436 (:italic t)))
|
|
437 "Face used for inactive widgets."
|
|
438 :group 'widget-faces)
|
|
439
|
|
440 ;; For inactiveness to work on complex structures, it is not
|
|
441 ;; sufficient to keep track of whether a button/field/glyph is
|
371
|
442 ;; inactive or not -- we must know how many time it was deactivated
|
209
|
443 ;; (inactiveness level). Successive deactivations of the same button
|
|
444 ;; increment its inactive-count, and activations decrement it. When
|
|
445 ;; inactive-count reaches 0, the button/field/glyph is reactivated.
|
|
446
|
|
447 (defun widget-activation-widget-mapper (extent action)
|
|
448 "Activate or deactivate EXTENT's widget (button or field).
|
|
449 Suitable for use with `map-extents'."
|
|
450 (ecase action
|
|
451 (:activate
|
|
452 (decf (extent-property extent :inactive-count))
|
|
453 (when (zerop (extent-property extent :inactive-count))
|
|
454 (set-extent-properties
|
|
455 extent (extent-property extent :inactive-plist))
|
|
456 (set-extent-property extent :inactive-plist nil)))
|
|
457 (:deactivate
|
|
458 (incf (extent-property extent :inactive-count 0))
|
|
459 ;; Store a plist of old properties, which will be fed to
|
|
460 ;; `set-extent-properties'.
|
|
461 (unless (extent-property extent :inactive-plist)
|
|
462 (set-extent-property
|
|
463 extent :inactive-plist
|
|
464 (list 'mouse-face (extent-property extent 'mouse-face)
|
|
465 'help-echo (extent-property extent 'help-echo)
|
|
466 'keymap (extent-property extent 'keymap)))
|
|
467 (set-extent-properties
|
|
468 extent '(mouse-face nil help-echo nil keymap nil)))))
|
|
469 nil)
|
|
470
|
|
471 (defun widget-activation-glyph-mapper (extent action)
|
|
472 (let ((activate-p (if (eq action :activate) t nil)))
|
|
473 (if activate-p
|
|
474 (decf (extent-property extent :inactive-count))
|
|
475 (incf (extent-property extent :inactive-count 0)))
|
|
476 (when (or (and activate-p
|
|
477 (zerop (extent-property extent :inactive-count)))
|
|
478 (and (not activate-p)
|
|
479 (not (zerop (extent-property extent :inactive-count)))))
|
|
480 (let* ((glyph-widget (extent-property extent 'glyph-widget))
|
|
481 (up-glyph (widget-get glyph-widget :glyph-up))
|
|
482 (inactive-glyph (widget-get glyph-widget :glyph-inactive))
|
|
483 (new-glyph (if activate-p up-glyph inactive-glyph)))
|
|
484 ;; Check that the new glyph exists, and differs from the
|
|
485 ;; default one.
|
|
486 (and up-glyph inactive-glyph (not (eq up-glyph inactive-glyph))
|
|
487 ;; Check if the glyph is already installed.
|
|
488 (not (eq (extent-end-glyph extent) new-glyph))
|
|
489 ;; Change it.
|
|
490 (set-extent-end-glyph extent new-glyph)))))
|
|
491 nil)
|
|
492
|
|
493 (defun widget-specify-inactive (widget from to)
|
|
494 "Make WIDGET inactive for user modifications."
|
|
495 (unless (widget-get widget :inactive)
|
|
496 (let ((extent (make-extent from to)))
|
|
497 ;; It is no longer necessary for the extent to be read-only, as
|
|
498 ;; the inactive editable fields now lose their keymaps.
|
|
499 (set-extent-properties
|
|
500 extent '(start-open t face widget-inactive-face
|
|
501 detachable t priority 2001 widget-inactive t))
|
|
502 (widget-put widget :inactive extent))
|
|
503 ;; Deactivate the buttons and fields within the range. In some
|
|
504 ;; cases, the fields are not yet setup at the time this function
|
|
505 ;; is called. Those fields are deactivated explicitly by
|
|
506 ;; `widget-setup'.
|
|
507 (map-extents 'widget-activation-widget-mapper
|
|
508 nil from to :deactivate nil 'button-or-field)
|
|
509 ;; Deactivate glyphs.
|
|
510 (map-extents 'widget-activation-glyph-mapper
|
|
511 nil from to :deactivate nil 'glyph-widget)))
|
|
512
|
|
513 (defun widget-specify-active (widget)
|
|
514 "Make WIDGET active for user modifications."
|
371
|
515 (let ((inactive (widget-get widget :inactive)))
|
276
|
516 (when (and inactive (not (extent-detached-p inactive)))
|
209
|
517 ;; Reactivate the buttons and fields covered by the extent.
|
|
518 (map-extents 'widget-activation-widget-mapper
|
371
|
519 inactive nil nil :activate nil 'button-or-field)
|
209
|
520 ;; Reactivate the glyphs.
|
|
521 (map-extents 'widget-activation-glyph-mapper
|
371
|
522 inactive nil nil :activate nil 'end-glyph)
|
209
|
523 (delete-extent inactive)
|
|
524 (widget-put widget :inactive nil))))
|
|
525
|
|
526
|
|
527 ;;; Widget Properties.
|
|
528
|
|
529 (defsubst widget-type (widget)
|
|
530 "Return the type of WIDGET, a symbol."
|
|
531 (car widget))
|
|
532
|
|
533 (when (or (not (fboundp 'widget-put))
|
|
534 widget-shadow-subrs)
|
|
535 (defun widget-put (widget property value)
|
|
536 "In WIDGET set PROPERTY to VALUE.
|
380
|
537 The value can later be retrieved with `widget-get'."
|
209
|
538 (setcdr widget (plist-put (cdr widget) property value))))
|
|
539
|
|
540 ;; Recoded in C, for efficiency:
|
|
541 (when (or (not (fboundp 'widget-get))
|
|
542 widget-shadow-subrs)
|
|
543 (defun widget-get (widget property)
|
|
544 "In WIDGET, get the value of PROPERTY.
|
|
545 The value could either be specified when the widget was created, or
|
|
546 later with `widget-put'."
|
|
547 (let ((missing t)
|
|
548 value tmp)
|
|
549 (while missing
|
|
550 (cond ((setq tmp (widget-plist-member (cdr widget) property))
|
|
551 (setq value (car (cdr tmp))
|
|
552 missing nil))
|
|
553 ((setq tmp (car widget))
|
|
554 (setq widget (get tmp 'widget-type)))
|
|
555 (t
|
|
556 (setq missing nil))))
|
|
557 value)))
|
|
558
|
|
559 (defun widget-get-indirect (widget property)
|
|
560 "In WIDGET, get the value of PROPERTY.
|
|
561 If the value is a symbol, return its binding.
|
|
562 Otherwise, just return the value."
|
|
563 (let ((value (widget-get widget property)))
|
|
564 (if (symbolp value)
|
|
565 (symbol-value value)
|
|
566 value)))
|
|
567
|
|
568 (defun widget-member (widget property)
|
|
569 "Non-nil iff there is a definition in WIDGET for PROPERTY."
|
|
570 (cond ((widget-plist-member (cdr widget) property)
|
|
571 t)
|
|
572 ((car widget)
|
|
573 (widget-member (get (car widget) 'widget-type) property))
|
|
574 (t nil)))
|
|
575
|
|
576 (when (or (not (fboundp 'widget-apply))
|
|
577 widget-shadow-subrs)
|
|
578 ;;This is in C, so don't ###utoload
|
|
579 (defun widget-apply (widget property &rest args)
|
|
580 "Apply the value of WIDGET's PROPERTY to the widget itself.
|
|
581 ARGS are passed as extra arguments to the function."
|
|
582 (apply (widget-get widget property) widget args)))
|
|
583
|
|
584 (defun widget-value (widget)
|
|
585 "Extract the current value of WIDGET."
|
|
586 (widget-apply widget
|
|
587 :value-to-external (widget-apply widget :value-get)))
|
|
588
|
|
589 (defun widget-value-set (widget value)
|
|
590 "Set the current value of WIDGET to VALUE."
|
|
591 (widget-apply widget
|
|
592 :value-set (widget-apply widget
|
|
593 :value-to-internal value)))
|
|
594
|
280
|
595 (defun widget-default-get (widget)
|
|
596 "Extract the defaylt value of WIDGET."
|
|
597 (or (widget-get widget :value)
|
|
598 (widget-apply widget :default-get)))
|
|
599
|
209
|
600 (defun widget-match-inline (widget vals)
|
|
601 ;; In WIDGET, match the start of VALS.
|
|
602 (cond ((widget-get widget :inline)
|
|
603 (widget-apply widget :match-inline vals))
|
398
|
604 ((and (listp vals)
|
209
|
605 (widget-apply widget :match (car vals)))
|
|
606 (cons (list (car vals)) (cdr vals)))
|
|
607 (t nil)))
|
|
608
|
|
609 (defun widget-apply-action (widget &optional event)
|
|
610 "Apply :action in WIDGET in response to EVENT."
|
|
611 (if (widget-apply widget :active)
|
|
612 (widget-apply widget :action event)
|
|
613 (error "Attempt to perform action on inactive widget")))
|
|
614
|
|
615
|
|
616 ;;; Helper functions.
|
|
617 ;;
|
|
618 ;; These are widget specific.
|
|
619
|
|
620 ;;;###autoload
|
|
621 (defun widget-prompt-value (widget prompt &optional value unbound)
|
|
622 "Prompt for a value matching WIDGET, using PROMPT.
|
|
623 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
|
|
624 (unless (listp widget)
|
|
625 (setq widget (list widget)))
|
|
626 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
|
|
627 (setq widget (widget-convert widget))
|
|
628 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
|
227
|
629 (while (not (widget-apply widget :match answer))
|
|
630 (setq answer (signal 'error (list "Answer does not match type"
|
|
631 answer (widget-type widget)))))
|
209
|
632 answer))
|
|
633
|
|
634 (defun widget-get-sibling (widget)
|
|
635 "Get the item WIDGET is assumed to toggle.
|
|
636 This is only meaningful for radio buttons or checkboxes in a list."
|
|
637 (let* ((parent (widget-get widget :parent))
|
|
638 (children (widget-get parent :children))
|
|
639 child)
|
|
640 (catch 'child
|
|
641 (while children
|
|
642 (setq child (car children)
|
|
643 children (cdr children))
|
|
644 (when (eq (widget-get child :button) widget)
|
|
645 (throw 'child child)))
|
|
646 nil)))
|
|
647
|
|
648 (defun widget-map-buttons (function &optional buffer maparg)
|
|
649 "Map FUNCTION over the buttons in BUFFER.
|
|
650 FUNCTION is called with the arguments WIDGET and MAPARG.
|
|
651
|
|
652 If FUNCTION returns non-nil, the walk is cancelled.
|
|
653
|
|
654 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
|
|
655 respectively."
|
|
656 (map-extents (lambda (extent ignore)
|
|
657 ;; If FUNCTION returns non-nil, we bail out
|
|
658 (funcall function (extent-property extent 'button) maparg))
|
|
659 nil nil nil nil nil
|
|
660 'button))
|
|
661
|
|
662
|
|
663 ;;; Glyphs.
|
|
664
|
|
665 (defcustom widget-glyph-directory (locate-data-directory "custom")
|
|
666 "Where widget glyphs are located.
|
|
667 If this variable is nil, widget will try to locate the directory
|
|
668 automatically."
|
|
669 :group 'widgets
|
|
670 :type 'directory)
|
|
671
|
|
672 (defcustom widget-glyph-enable t
|
|
673 "If non nil, use glyphs in images when available."
|
|
674 :group 'widgets
|
|
675 :type 'boolean)
|
|
676
|
398
|
677 (defcustom widget-image-file-name-suffixes
|
371
|
678 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
|
209
|
679 (xbm ".xbm"))
|
|
680 "Conversion alist from image formats to file name suffixes."
|
|
681 :group 'widgets
|
|
682 :type '(repeat (cons :format "%v"
|
|
683 (symbol :tag "Image Format" unknown)
|
|
684 (repeat :tag "Suffixes"
|
|
685 (string :format "%v")))))
|
|
686
|
219
|
687 ;; Don't use this, because we cannot yet distinguish between widget
|
|
688 ;; glyphs associated with user action, and actionless ones.
|
|
689 ;(defvar widget-glyph-pointer-glyph
|
|
690 ; (make-pointer-glyph [cursor-font :data "hand2"])
|
|
691 ; "Glyph to be used as the mouse pointer shape over glyphs.
|
|
692 ;Use `set-glyph-image' to change this.")
|
211
|
693
|
209
|
694 (defvar widget-glyph-cache nil
|
|
695 "Cache of glyphs associated with strings (files).")
|
|
696
|
|
697 (defun widget-glyph-find (image tag)
|
|
698 "Create a glyph corresponding to IMAGE with string TAG as fallback.
|
|
699 IMAGE can already be a glyph, or a file name sans extension (xpm,
|
|
700 xbm, gif, jpg, or png) located in `widget-glyph-directory', or
|
|
701 in one of the data directories.
|
|
702 It can also be a valid image instantiator, in which case it will be
|
|
703 used to make the glyph, with an additional TAG string fallback."
|
|
704 (cond ((not (and image widget-glyph-enable))
|
|
705 ;; We don't want to use glyphs.
|
|
706 nil)
|
|
707 ((and (not (console-on-window-system-p))
|
|
708 ;; We don't use glyphs on TTY consoles, although we
|
|
709 ;; could. However, glyph faces aren't yet working
|
|
710 ;; properly, and movement through glyphs is unintuitive.
|
|
711 ;; As an exception, when TAG is nil, we assume that the
|
|
712 ;; caller knows what he is doing, and that the tag is
|
|
713 ;; encoded within the glyph.
|
|
714 (not (glyphp image)))
|
|
715 nil)
|
|
716 ((glyphp image)
|
|
717 ;; Already a glyph. Use it.
|
|
718 image)
|
|
719 ((stringp image)
|
|
720 ;; A string. Look it up in the cache first...
|
|
721 (or (lax-plist-get widget-glyph-cache image)
|
|
722 ;; ...and then in the relevant directories
|
|
723 (let* ((dirlist (cons (or widget-glyph-directory
|
|
724 (locate-data-directory "custom"))
|
|
725 data-directory-list))
|
398
|
726 (all-suffixes
|
|
727 (apply #'append
|
|
728 (mapcar
|
|
729 (lambda (el)
|
|
730 (and (valid-image-instantiator-format-p (car el))
|
|
731 (cdr el)))
|
|
732 widget-image-file-name-suffixes)))
|
|
733 (file (locate-file image dirlist all-suffixes)))
|
209
|
734 (when file
|
398
|
735 (let* ((extension (concat "." (file-name-extension file)))
|
|
736 (format (car (rassoc* extension
|
|
737 widget-image-file-name-suffixes
|
|
738 :test #'member))))
|
|
739 ;; We create a glyph with the file as the default image
|
|
740 ;; instantiator, and the TAG fallback
|
|
741 (let ((glyph (make-glyph `([,format :file ,file]
|
|
742 [string :data ,tag]))))
|
|
743 ;; Cache the glyph
|
|
744 (laxputf widget-glyph-cache image glyph)
|
|
745 ;; ...and return it
|
|
746 glyph))))))
|
209
|
747 ((valid-instantiator-p image 'image)
|
|
748 ;; A valid image instantiator (e.g. [gif :file "somefile"] etc.)
|
|
749 (make-glyph `(,image [string :data ,tag])))
|
|
750 (t
|
|
751 ;; Oh well.
|
|
752 nil)))
|
|
753
|
|
754 (defun widget-glyph-insert (widget tag image &optional down inactive)
|
|
755 "In WIDGET, insert the text TAG or, if supported, IMAGE.
|
|
756 IMAGE should either be a glyph, an image instantiator, an image file
|
|
757 name sans extension (xpm, xbm, gif, jpg, or png) located in
|
|
758 `widget-glyph-directory', or anything else allowed by
|
|
759 `widget-glyph-find'.
|
|
760
|
|
761 If IMAGE is a list, it will be taken as a list of (UP DOWN INACTIVE)
|
|
762 glyphs. The down and inactive glyphs are shown when glyph is pressed
|
|
763 or inactive, respectively.
|
|
764
|
|
765 The optional DOWN and INACTIVE arguments are deprecated, and exist
|
|
766 only because of compatibility."
|
|
767 ;; Convert between IMAGE being a list, etc. Must use `psetq',
|
|
768 ;; because otherwise change to `image' screws up the rest.
|
|
769 (psetq image (or (and (consp image)
|
|
770 (car image))
|
|
771 image)
|
|
772 down (or (and (consp image)
|
|
773 (nth 1 image))
|
|
774 down)
|
|
775 inactive (or (and (consp image)
|
|
776 (nth 2 image))
|
|
777 inactive))
|
|
778 (let ((glyph (widget-glyph-find image tag)))
|
|
779 (if glyph
|
|
780 (widget-glyph-insert-glyph widget glyph
|
|
781 (widget-glyph-find down tag)
|
|
782 (widget-glyph-find inactive tag))
|
|
783 (insert tag))
|
|
784 glyph))
|
|
785
|
|
786 (defun widget-glyph-insert-glyph (widget glyph &optional down inactive)
|
|
787 "In WIDGET, insert GLYPH.
|
|
788 If optional arguments DOWN and INACTIVE are given, they should be
|
|
789 glyphs used when the widget is pushed and inactive, respectively."
|
|
790 (insert "*")
|
|
791 (let ((extent (make-extent (point) (1- (point))))
|
|
792 (help-echo (and widget (widget-get widget :help-echo)))
|
|
793 (map (and widget (widget-get widget :button-keymap))))
|
|
794 (set-extent-property extent 'glyph-widget widget)
|
|
795 ;; It would be fun if we could make this extent atomic, so it
|
|
796 ;; doesn't mess with cursor motion. But atomic-extents library is
|
|
797 ;; currently a mess, so I'd rather not use it.
|
|
798 (set-extent-property extent 'invisible t)
|
|
799 (set-extent-property extent 'start-open t)
|
|
800 (set-extent-property extent 'end-open t)
|
|
801 (set-extent-property extent 'keymap map)
|
219
|
802 ;;(set-extent-property extent 'pointer widget-glyph-pointer-glyph)
|
209
|
803 (set-extent-end-glyph extent glyph)
|
|
804 (unless (or (stringp help-echo) (null help-echo))
|
|
805 (setq help-echo 'widget-mouse-help))
|
|
806 (when help-echo
|
276
|
807 (widget-handle-help-echo extent help-echo)))
|
209
|
808 (when widget
|
|
809 (widget-put widget :glyph-up glyph)
|
|
810 (when down (widget-put widget :glyph-down down))
|
|
811 (when inactive (widget-put widget :glyph-inactive inactive))))
|
|
812
|
|
813
|
|
814 ;;; Buttons.
|
|
815
|
|
816 (defgroup widget-button nil
|
|
817 "The look of various kinds of buttons."
|
|
818 :group 'widgets)
|
|
819
|
|
820 (defcustom widget-button-prefix ""
|
|
821 "String used as prefix for buttons."
|
|
822 :type 'string
|
|
823 :group 'widget-button)
|
|
824
|
|
825 (defcustom widget-button-suffix ""
|
|
826 "String used as suffix for buttons."
|
|
827 :type 'string
|
|
828 :group 'widget-button)
|
|
829
|
|
830
|
|
831 ;;; Creating Widgets.
|
|
832
|
|
833 ;;;###autoload
|
|
834 (defun widget-create (type &rest args)
|
|
835 "Create widget of TYPE.
|
|
836 The optional ARGS are additional keyword arguments."
|
|
837 (let ((widget (apply 'widget-convert type args)))
|
|
838 (widget-apply widget :create)
|
|
839 widget))
|
|
840
|
|
841 (defun widget-create-child-and-convert (parent type &rest args)
|
|
842 "As part of the widget PARENT, create a child widget TYPE.
|
|
843 The child is converted, using the keyword arguments ARGS."
|
|
844 (let ((widget (apply 'widget-convert type args)))
|
|
845 (widget-put widget :parent parent)
|
|
846 (unless (widget-get widget :indent)
|
|
847 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
|
|
848 (or (widget-get widget :extra-offset) 0)
|
|
849 (widget-get parent :offset))))
|
|
850 (widget-apply widget :create)
|
|
851 widget))
|
|
852
|
|
853 (defun widget-create-child (parent type)
|
|
854 "Create widget of TYPE."
|
|
855 (let ((widget (copy-sequence type)))
|
|
856 (widget-put widget :parent parent)
|
|
857 (unless (widget-get widget :indent)
|
|
858 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
|
|
859 (or (widget-get widget :extra-offset) 0)
|
|
860 (widget-get parent :offset))))
|
|
861 (widget-apply widget :create)
|
|
862 widget))
|
|
863
|
|
864 (defun widget-create-child-value (parent type value)
|
|
865 "Create widget of TYPE with value VALUE."
|
|
866 (let ((widget (copy-sequence type)))
|
|
867 (widget-put widget :value (widget-apply widget :value-to-internal value))
|
|
868 (widget-put widget :parent parent)
|
|
869 (unless (widget-get widget :indent)
|
|
870 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
|
|
871 (or (widget-get widget :extra-offset) 0)
|
|
872 (widget-get parent :offset))))
|
|
873 (widget-apply widget :create)
|
|
874 widget))
|
|
875
|
|
876 ;;;###autoload
|
|
877 (defun widget-delete (widget)
|
|
878 "Delete WIDGET."
|
|
879 (widget-apply widget :delete))
|
|
880
|
|
881 (defun widget-convert (type &rest args)
|
|
882 "Convert TYPE to a widget without inserting it in the buffer.
|
|
883 The optional ARGS are additional keyword arguments."
|
|
884 ;; Don't touch the type.
|
|
885 (let* ((widget (if (symbolp type)
|
|
886 (list type)
|
|
887 (copy-sequence type)))
|
|
888 (current widget)
|
|
889 (keys args))
|
|
890 ;; First set the :args keyword.
|
|
891 (while (cdr current) ;Look in the type.
|
|
892 (let ((next (car (cdr current))))
|
|
893 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
|
|
894 (setq current (cdr (cdr current)))
|
|
895 (setcdr current (list :args (cdr current)))
|
|
896 (setq current nil))))
|
|
897 (while args ;Look in the args.
|
|
898 (let ((next (nth 0 args)))
|
|
899 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
|
|
900 (setq args (nthcdr 2 args))
|
|
901 (widget-put widget :args args)
|
|
902 (setq args nil))))
|
|
903 ;; Then Convert the widget.
|
|
904 (setq type widget)
|
|
905 (while type
|
|
906 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
|
|
907 (if convert-widget
|
|
908 (setq widget (funcall convert-widget widget))))
|
|
909 (setq type (get (car type) 'widget-type)))
|
|
910 ;; Finally set the keyword args.
|
|
911 (while keys
|
|
912 (let ((next (nth 0 keys)))
|
|
913 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
|
|
914 (progn
|
|
915 (widget-put widget next (nth 1 keys))
|
|
916 (setq keys (nthcdr 2 keys)))
|
|
917 (setq keys nil))))
|
|
918 ;; Convert the :value to internal format.
|
|
919 (if (widget-member widget :value)
|
|
920 (let ((value (widget-get widget :value)))
|
|
921 (widget-put widget
|
|
922 :value (widget-apply widget :value-to-internal value))))
|
|
923 ;; Return the newly created widget.
|
|
924 widget))
|
|
925
|
|
926 (defun widget-insert (&rest args)
|
|
927 "Call `insert' with ARGS and make the text read only."
|
|
928 (let ((inhibit-read-only t)
|
|
929 before-change-functions
|
|
930 after-change-functions)
|
|
931 (apply 'insert args)))
|
|
932
|
|
933 (defun widget-convert-text (type from to
|
|
934 &optional button-from button-to
|
|
935 &rest args)
|
|
936 "Return a widget of type TYPE with endpoint FROM TO.
|
|
937 Optional ARGS are extra keyword arguments for TYPE.
|
|
938 and TO will be used as the widgets end points. If optional arguments
|
|
939 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
|
|
940 button end points.
|
|
941 Optional ARGS are extra keyword arguments for TYPE."
|
|
942 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
|
|
943 (from (copy-marker from))
|
|
944 (to (copy-marker to)))
|
|
945 (set-marker-insertion-type from t)
|
|
946 (set-marker-insertion-type to nil)
|
|
947 (widget-put widget :from from)
|
|
948 (widget-put widget :to to)
|
|
949 (when button-from
|
|
950 (widget-specify-button widget button-from button-to))
|
|
951 widget))
|
|
952
|
|
953 (defun widget-convert-button (type from to &rest args)
|
|
954 "Return a widget of type TYPE with endpoint FROM TO.
|
|
955 Optional ARGS are extra keyword arguments for TYPE.
|
|
956 No text will be inserted to the buffer, instead the text between FROM
|
|
957 and TO will be used as the widgets end points, as well as the widgets
|
|
958 button end points."
|
|
959 (apply 'widget-convert-text type from to from to args))
|
|
960
|
|
961 (defun widget-leave-text (widget)
|
|
962 "Remove markers and extents from WIDGET and its children."
|
|
963 (let ((from (widget-get widget :from))
|
|
964 (to (widget-get widget :to))
|
|
965 (button (widget-get widget :button-extent))
|
|
966 (sample (widget-get widget :sample-extent))
|
|
967 (doc (widget-get widget :doc-extent))
|
|
968 (field (widget-get widget :field-extent))
|
|
969 (children (widget-get widget :children)))
|
|
970 (set-marker from nil)
|
|
971 (set-marker to nil)
|
|
972 ;; Maybe we should delete the extents here? As this code doesn't
|
|
973 ;; remove them from widget structures, maybe it's safer to just
|
|
974 ;; detach them. That's what `delete-overlay' did.
|
|
975 (when button
|
|
976 (detach-extent button))
|
|
977 (when sample
|
|
978 (detach-extent sample))
|
|
979 (when doc
|
|
980 (detach-extent doc))
|
|
981 (when field
|
|
982 (detach-extent field))
|
|
983 (mapc 'widget-leave-text children)))
|
|
984
|
|
985
|
|
986 ;;; Keymap and Commands.
|
|
987
|
|
988 (defvar widget-keymap nil
|
|
989 "Keymap containing useful binding for buffers containing widgets.
|
|
990 Recommended as a parent keymap for modes using widgets.")
|
|
991
|
|
992 (unless widget-keymap
|
|
993 (setq widget-keymap (make-sparse-keymap))
|
|
994 (define-key widget-keymap [tab] 'widget-forward)
|
|
995 (define-key widget-keymap [(shift tab)] 'widget-backward)
|
|
996 (define-key widget-keymap [(meta tab)] 'widget-backward)
|
|
997 (define-key widget-keymap [backtab] 'widget-backward))
|
|
998
|
|
999 (defvar widget-global-map global-map
|
|
1000 "Keymap used for events the widget does not handle themselves.")
|
|
1001 (make-variable-buffer-local 'widget-global-map)
|
|
1002
|
|
1003 (defvar widget-field-keymap nil
|
|
1004 "Keymap used inside an editable field.")
|
|
1005
|
|
1006 (unless widget-field-keymap
|
|
1007 (setq widget-field-keymap (make-sparse-keymap))
|
|
1008 (set-keymap-parents widget-field-keymap global-map)
|
|
1009 (define-key widget-field-keymap "\C-k" 'widget-kill-line)
|
|
1010 (define-key widget-field-keymap [(meta tab)] 'widget-complete)
|
|
1011 (define-key widget-field-keymap [tab] 'widget-forward)
|
|
1012 (define-key widget-field-keymap [(shift tab)] 'widget-backward)
|
|
1013 (define-key widget-field-keymap "\C-m" 'widget-field-activate)
|
|
1014 (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line)
|
|
1015 (define-key widget-field-keymap "\C-e" 'widget-end-of-line)
|
|
1016 (define-key widget-field-keymap "\C-t" 'widget-transpose-chars))
|
|
1017
|
|
1018 (defvar widget-text-keymap nil
|
|
1019 "Keymap used inside a text field.")
|
|
1020
|
|
1021 (unless widget-text-keymap
|
|
1022 (setq widget-text-keymap (make-sparse-keymap))
|
|
1023 (set-keymap-parents widget-field-keymap global-map)
|
|
1024 (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line)
|
|
1025 (define-key widget-text-keymap "\C-e" 'widget-end-of-line)
|
|
1026 (define-key widget-text-keymap "\C-t" 'widget-transpose-chars))
|
|
1027
|
|
1028 (defvar widget-button-keymap nil
|
|
1029 "Keymap used inside a button.")
|
|
1030
|
|
1031 (unless widget-button-keymap
|
|
1032 (setq widget-button-keymap (make-sparse-keymap))
|
|
1033 (set-keymap-parents widget-button-keymap widget-keymap)
|
|
1034 (define-key widget-button-keymap "\C-m" 'widget-button-press)
|
|
1035 (define-key widget-button-keymap [button2] 'widget-button-click)
|
|
1036 ;; Ideally, button3 within a button should invoke a button-specific
|
|
1037 ;; menu.
|
|
1038 (define-key widget-button-keymap [button3] 'widget-button-click)
|
|
1039 ;;Glyph support.
|
|
1040 (define-key widget-button-keymap [button1] 'widget-button1-click))
|
|
1041
|
|
1042
|
|
1043 (defun widget-field-activate (pos &optional event)
|
|
1044 "Invoke the ediable field at point."
|
|
1045 (interactive "@d")
|
|
1046 (let ((field (widget-field-find pos)))
|
|
1047 (if field
|
|
1048 (widget-apply-action field event)
|
|
1049 (call-interactively
|
|
1050 (lookup-key widget-global-map (this-command-keys))))))
|
|
1051
|
|
1052 (defface widget-button-pressed-face
|
|
1053 '((((class color))
|
|
1054 (:foreground "red"))
|
|
1055 (t
|
|
1056 (:bold t :underline t)))
|
|
1057 "Face used for pressed buttons."
|
|
1058 :group 'widget-faces)
|
|
1059
|
|
1060 (defun widget-event-point (event)
|
|
1061 "Character position of the mouse event, or nil."
|
|
1062 (and (mouse-event-p event)
|
|
1063 (event-point event)))
|
|
1064
|
|
1065 (defun widget-button-click (event)
|
|
1066 "Invoke button below mouse pointer."
|
377
|
1067 (interactive "e")
|
|
1068 (with-current-buffer (event-buffer event)
|
|
1069 (cond ((event-glyph event)
|
|
1070 (widget-glyph-click event))
|
|
1071 ((widget-event-point event)
|
|
1072 (let* ((pos (widget-event-point event))
|
|
1073 (button (get-char-property pos 'button)))
|
|
1074 (if button
|
|
1075 (let* ((extent (widget-get button :button-extent))
|
|
1076 (face (extent-property extent 'face))
|
|
1077 (mouse-face (extent-property extent 'mouse-face))
|
|
1078 (help-echo (extent-property extent 'help-echo)))
|
|
1079 (unwind-protect
|
|
1080 (progn
|
|
1081 ;; Merge relevant faces, and make the result mouse-face.
|
|
1082 (let ((merge `(widget-button-pressed-face ,mouse-face)))
|
|
1083 (nconc merge (if (listp face)
|
|
1084 face (list face)))
|
|
1085 (setq merge (delete-if-not 'find-face merge))
|
|
1086 (set-extent-property extent 'mouse-face merge))
|
|
1087 (unless (widget-apply button :mouse-down-action event)
|
|
1088 ;; Wait for button release.
|
|
1089 (while (not (button-release-event-p
|
|
1090 (setq event (next-event))))
|
|
1091 (dispatch-event event)))
|
|
1092 ;; Disallow mouse-face and help-echo.
|
|
1093 (set-extent-property extent 'mouse-face nil)
|
|
1094 (set-extent-property extent 'help-echo nil)
|
|
1095 (setq pos (widget-event-point event))
|
|
1096 (unless (eq (current-buffer) (extent-object extent))
|
|
1097 ;; Barf if dispatch-event tripped us by
|
|
1098 ;; changing buffer.
|
|
1099 (error "Buffer changed during mouse motion"))
|
|
1100 ;; Do the associated action.
|
|
1101 (when (and pos (extent-in-region-p extent pos pos))
|
|
1102 (widget-apply-action button event)))
|
|
1103 ;; Unwinding: fully release the button.
|
|
1104 (set-extent-property extent 'mouse-face mouse-face)
|
|
1105 (set-extent-property extent 'help-echo help-echo)))
|
|
1106 ;; This should not happen!
|
|
1107 (error "`widget-button-click' called outside button"))))
|
|
1108 (t
|
|
1109 (message "You clicked somewhere weird")))))
|
209
|
1110
|
|
1111 (defun widget-button1-click (event)
|
|
1112 "Invoke glyph below mouse pointer."
|
|
1113 (interactive "@e")
|
|
1114 (if (event-glyph event)
|
|
1115 (widget-glyph-click event)
|
|
1116 ;; Should somehow avoid this.
|
|
1117 (let ((command (lookup-key widget-global-map (this-command-keys))))
|
|
1118 (and (commandp command)
|
|
1119 (call-interactively command)))))
|
|
1120
|
|
1121 (defun widget-glyph-click (event)
|
|
1122 "Handle click on a glyph."
|
|
1123 (let* ((glyph (event-glyph event))
|
|
1124 (extent (event-glyph-extent event))
|
|
1125 (widget (extent-property extent 'glyph-widget))
|
|
1126 (down-glyph (or (and widget (widget-get widget :glyph-down)) glyph))
|
|
1127 (up-glyph (or (and widget (widget-get widget :glyph-up)) glyph))
|
|
1128 (last event))
|
|
1129 (unless (widget-apply widget :active)
|
|
1130 (error "This widget is inactive"))
|
|
1131 (let ((current-glyph 'down))
|
|
1132 ;; We always know what glyph is drawn currently, to avoid
|
380
|
1133 ;; unnecessary extent changes. Is this any noticeable gain?
|
209
|
1134 (unwind-protect
|
|
1135 (progn
|
|
1136 ;; Press the glyph.
|
|
1137 (set-extent-end-glyph extent down-glyph)
|
|
1138 ;; Redisplay (shouldn't be needed, but...)
|
|
1139 (sit-for 0)
|
|
1140 (unless (widget-apply widget :mouse-down-action event)
|
|
1141 ;; Wait for the release.
|
|
1142 (while (not (button-release-event-p last))
|
|
1143 (unless (button-press-event-p last)
|
|
1144 (dispatch-event last))
|
|
1145 (when (motion-event-p last)
|
|
1146 ;; Update glyphs on mouse motion.
|
|
1147 (if (eq extent (event-glyph-extent last))
|
|
1148 (unless (eq current-glyph 'down)
|
|
1149 (set-extent-end-glyph extent down-glyph)
|
|
1150 (setq current-glyph 'down))
|
|
1151 (unless (eq current-glyph 'up)
|
|
1152 (set-extent-end-glyph extent up-glyph)
|
|
1153 (setq current-glyph 'up))))
|
|
1154 (setq last (next-event event))))
|
|
1155 (unless (eq (current-buffer) (extent-object extent))
|
|
1156 ;; Barf if dispatch-event tripped us by changing buffer.
|
|
1157 (error "Buffer changed during mouse motion"))
|
|
1158 ;; Apply widget action.
|
|
1159 (when (eq extent (event-glyph-extent last))
|
|
1160 (let ((widget (extent-property (event-glyph-extent event)
|
|
1161 'glyph-widget)))
|
|
1162 (cond ((null widget)
|
|
1163 (message "You clicked on a glyph"))
|
|
1164 ((not (widget-apply widget :active))
|
|
1165 (error "This glyph is inactive"))
|
|
1166 (t
|
|
1167 (widget-apply-action widget event))))))
|
|
1168 ;; Release the glyph.
|
|
1169 (and (eq current-glyph 'down)
|
|
1170 ;; The extent might have been detached or deleted
|
|
1171 (extent-live-p extent)
|
|
1172 (not (extent-detached-p extent))
|
|
1173 (set-extent-end-glyph extent up-glyph))))))
|
|
1174
|
|
1175 (defun widget-button-press (pos &optional event)
|
|
1176 "Invoke button at POS."
|
|
1177 (interactive "@d")
|
|
1178 (let ((button (get-char-property pos 'button)))
|
|
1179 (if button
|
|
1180 (widget-apply-action button event)
|
|
1181 (let ((command (lookup-key widget-global-map (this-command-keys))))
|
|
1182 (when (commandp command)
|
|
1183 (call-interactively command))))))
|
|
1184
|
|
1185 (defun widget-tabable-at (&optional pos last-tab backwardp)
|
|
1186 "Return the tabable widget at POS, or nil.
|
|
1187 POS defaults to the value of (point)."
|
|
1188 (unless pos
|
|
1189 (setq pos (point)))
|
|
1190 (let ((widget (widget-at pos)))
|
|
1191 (if widget
|
|
1192 (let ((order (widget-get widget :tab-order)))
|
|
1193 (if order
|
|
1194 (if last-tab (and (= order (if backwardp
|
|
1195 (1- last-tab)
|
|
1196 (1+ last-tab)))
|
|
1197 widget)
|
|
1198 (and (> order 0) widget))
|
|
1199 widget))
|
|
1200 nil)))
|
|
1201
|
|
1202 ;; Return the button or field extent at point.
|
|
1203 (defun widget-button-or-field-extent (pos)
|
|
1204 (or (and (get-char-property pos 'button)
|
|
1205 (widget-get (get-char-property pos 'button)
|
|
1206 :button-extent))
|
|
1207 (and (get-char-property pos 'field)
|
|
1208 (widget-get (get-char-property pos 'field)
|
|
1209 :field-extent))))
|
|
1210
|
|
1211 (defun widget-next-button-or-field (pos)
|
|
1212 "Find the next button, or field, and return its start position, or nil.
|
|
1213 Internal function, don't use it outside `wid-edit'."
|
|
1214 (let* ((at-point (widget-button-or-field-extent pos))
|
|
1215 (extent (map-extents
|
|
1216 (lambda (ext ignore)
|
|
1217 ext)
|
|
1218 nil (if at-point (extent-end-position at-point) pos)
|
|
1219 nil nil 'start-open 'button-or-field)))
|
|
1220 (and extent
|
|
1221 (extent-start-position extent))))
|
|
1222
|
|
1223 ;; This is too slow in buffers with many buttons (W3).
|
|
1224 (defun widget-previous-button-or-field (pos)
|
|
1225 "Find the previous button, or field, and return its start position, or nil.
|
|
1226 Internal function, don't use it outside `wid-edit'."
|
|
1227 (let* ((at-point (widget-button-or-field-extent pos))
|
|
1228 previous-extent)
|
|
1229 (map-extents
|
|
1230 (lambda (ext ignore)
|
|
1231 (if (eq ext at-point)
|
|
1232 ;; We reached the extent we were on originally
|
|
1233 (if (= pos (extent-start-position at-point))
|
|
1234 previous-extent
|
|
1235 (setq previous-extent at-point))
|
|
1236 (setq previous-extent ext)
|
|
1237 nil))
|
|
1238 nil nil pos nil 'start-open 'button-or-field)
|
|
1239 (and previous-extent
|
|
1240 (extent-start-position previous-extent))))
|
|
1241
|
|
1242 (defun widget-move (arg)
|
|
1243 "Move point to the ARG next field or button.
|
|
1244 ARG may be negative to move backward."
|
|
1245 (let ((opoint (point)) (wrapped 0)
|
|
1246 (last-tab (widget-get (widget-at (point)) :tab-order))
|
|
1247 nextpos found)
|
|
1248 ;; Movement backward
|
|
1249 (while (< arg 0)
|
|
1250 (setq nextpos (widget-previous-button-or-field (point)))
|
|
1251 (if nextpos
|
|
1252 (progn
|
|
1253 (goto-char nextpos)
|
|
1254 (when (and (not (get-char-property nextpos 'widget-inactive))
|
|
1255 (widget-tabable-at nil last-tab t))
|
|
1256 (incf arg)
|
|
1257 (setq found t
|
|
1258 last-tab (widget-get (widget-at (point))
|
|
1259 :tab-order))))
|
|
1260 (if (and (not found) (> wrapped 1))
|
|
1261 (setq arg 0
|
|
1262 found nil)
|
|
1263 (goto-char (point-max))
|
|
1264 (incf wrapped))))
|
|
1265 ;; Movement forward
|
|
1266 (while (> arg 0)
|
|
1267 (setq nextpos (widget-next-button-or-field (point)))
|
|
1268 (if nextpos
|
|
1269 (progn
|
|
1270 (goto-char nextpos)
|
|
1271 (when (and (not (get-char-property nextpos 'widget-inactive))
|
|
1272 (widget-tabable-at nil last-tab))
|
|
1273 (decf arg)
|
|
1274 (setq found t
|
|
1275 last-tab (widget-get (widget-at (point))
|
|
1276 :tab-order))))
|
|
1277 (if (and (not found) (> wrapped 1))
|
|
1278 (setq arg 0
|
|
1279 found nil)
|
|
1280 (goto-char (point-min))
|
|
1281 (incf wrapped))))
|
|
1282 (if (not found)
|
|
1283 (goto-char opoint)
|
|
1284 (widget-echo-help (point))
|
|
1285 (run-hooks 'widget-move-hook))))
|
|
1286
|
|
1287 (defun widget-forward (arg)
|
|
1288 "Move point to the next field or button.
|
|
1289 With optional ARG, move across that many fields."
|
|
1290 (interactive "p")
|
|
1291 (run-hooks 'widget-forward-hook)
|
|
1292 (widget-move arg))
|
|
1293
|
|
1294 (defun widget-backward (arg)
|
|
1295 "Move point to the previous field or button.
|
|
1296 With optional ARG, move across that many fields."
|
|
1297 (interactive "p")
|
|
1298 (run-hooks 'widget-backward-hook)
|
|
1299 (widget-move (- arg)))
|
|
1300
|
|
1301 (defun widget-beginning-of-line ()
|
|
1302 "Go to beginning of field or beginning of line, whichever is first."
|
|
1303 (interactive "_")
|
|
1304 (let* ((field (widget-field-find (point)))
|
|
1305 (start (and field (widget-field-start field))))
|
|
1306 (if (and start (not (eq start (point))))
|
|
1307 (goto-char start)
|
|
1308 (call-interactively 'beginning-of-line))))
|
|
1309
|
|
1310 (defun widget-end-of-line ()
|
|
1311 "Go to end of field or end of line, whichever is first."
|
|
1312 (interactive "_")
|
|
1313 (let* ((field (widget-field-find (point)))
|
|
1314 (end (and field (widget-field-end field))))
|
|
1315 (if (and end (not (eq end (point))))
|
|
1316 (goto-char end)
|
|
1317 (call-interactively 'end-of-line))))
|
|
1318
|
|
1319 (defun widget-kill-line ()
|
|
1320 "Kill to end of field or end of line, whichever is first."
|
|
1321 (interactive)
|
|
1322 (let* ((field (widget-field-find (point)))
|
|
1323 (newline (save-excursion (forward-line 1) (point)))
|
|
1324 (end (and field (widget-field-end field))))
|
|
1325 (if (and field (> newline end))
|
|
1326 (kill-region (point) end)
|
|
1327 (call-interactively 'kill-line))))
|
|
1328
|
|
1329 (defun widget-transpose-chars (arg)
|
|
1330 "Like `transpose-chars', but works correctly at end of widget."
|
|
1331 (interactive "*P")
|
|
1332 (let* ((field (widget-field-find (point)))
|
|
1333 (start (and field (widget-field-start field)))
|
|
1334 (end (and field (widget-field-end field)))
|
|
1335 (last-non-space (and start end
|
|
1336 (save-excursion
|
|
1337 (goto-char end)
|
|
1338 (skip-chars-backward " \t\n" start)
|
|
1339 (point)))))
|
|
1340 (cond ((and last-non-space
|
|
1341 (or (= last-non-space start)
|
|
1342 (= last-non-space (1+ start))))
|
|
1343 ;; empty or one-character field
|
|
1344 nil)
|
|
1345 ((= (point) start)
|
|
1346 ;; at the beginning of the field -- we would get an error here.
|
|
1347 (error "Cannot transpose at beginning of field"))
|
|
1348 (t
|
|
1349 (when (and (null arg)
|
|
1350 (= last-non-space (point)))
|
|
1351 (forward-char -1))
|
|
1352 (transpose-chars arg)))))
|
|
1353
|
|
1354 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
|
|
1355 "Default function to call for completion inside fields."
|
|
1356 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
|
|
1357 :type 'function
|
|
1358 :group 'widgets)
|
|
1359
|
|
1360 (defun widget-complete ()
|
|
1361 "Complete content of editable field from point.
|
|
1362 When not inside a field, move to the previous button or field."
|
|
1363 (interactive)
|
|
1364 ;; Somehow, this should make pressing M-TAB twice scroll the
|
|
1365 ;; completions window.
|
|
1366 (let ((field (widget-field-find (point))))
|
|
1367 (if field
|
|
1368 (widget-apply field :complete)
|
|
1369 (error "Not in an editable field"))))
|
|
1370
|
|
1371
|
|
1372 ;;; Setting up the buffer.
|
|
1373
|
|
1374 (defvar widget-field-new nil)
|
|
1375 ;; List of all newly created editable fields in the buffer.
|
|
1376 (make-variable-buffer-local 'widget-field-new)
|
|
1377
|
|
1378 (defvar widget-field-list nil)
|
|
1379 ;; List of all editable fields in the buffer.
|
|
1380 (make-variable-buffer-local 'widget-field-list)
|
|
1381
|
|
1382 (defun widget-setup ()
|
|
1383 "Setup current buffer so editing string widgets works."
|
|
1384 (let ((inhibit-read-only t)
|
|
1385 (after-change-functions nil)
|
|
1386 before-change-functions
|
|
1387 field)
|
|
1388 (while widget-field-new
|
|
1389 (setq field (car widget-field-new)
|
|
1390 widget-field-new (cdr widget-field-new)
|
|
1391 widget-field-list (cons field widget-field-list))
|
|
1392 (let ((from (car (widget-get field :field-extent)))
|
|
1393 (to (cdr (widget-get field :field-extent))))
|
|
1394 (widget-specify-field field
|
|
1395 (marker-position from) (marker-position to))
|
|
1396 (set-marker from nil)
|
|
1397 (set-marker to nil))
|
|
1398 ;; If the field is placed within the inactive zone, deactivate it.
|
|
1399 (let ((extent (widget-get field :field-extent)))
|
|
1400 (when (get-char-property (extent-start-position extent)
|
|
1401 'widget-inactive)
|
|
1402 (widget-activation-widget-mapper extent :deactivate)))))
|
|
1403 (widget-clear-undo)
|
|
1404 (widget-add-change))
|
|
1405
|
|
1406 (defvar widget-field-last nil)
|
|
1407 ;; Last field containing point.
|
|
1408 (make-variable-buffer-local 'widget-field-last)
|
|
1409
|
|
1410 (defvar widget-field-was nil)
|
|
1411 ;; The widget data before the change.
|
|
1412 (make-variable-buffer-local 'widget-field-was)
|
|
1413
|
|
1414 (defun widget-field-buffer (widget)
|
|
1415 "Return the start of WIDGET's editing field."
|
|
1416 (let ((extent (widget-get widget :field-extent)))
|
|
1417 (and extent (extent-object extent))))
|
|
1418
|
|
1419 (defun widget-field-start (widget)
|
|
1420 "Return the start of WIDGET's editing field."
|
|
1421 (let ((extent (widget-get widget :field-extent)))
|
|
1422 (and extent (extent-start-position extent))))
|
|
1423
|
|
1424 (defun widget-field-end (widget)
|
|
1425 "Return the end of WIDGET's editing field."
|
|
1426 (let ((extent (widget-get widget :field-extent)))
|
|
1427 ;; Don't subtract one if local-map works at the end of the extent.
|
|
1428 (and extent (if (or widget-field-add-space
|
|
1429 (null (widget-get widget :size)))
|
|
1430 (1- (extent-end-position extent))
|
|
1431 (extent-end-position extent)))))
|
|
1432
|
|
1433 (defun widget-field-find (pos)
|
|
1434 "Return the field at POS.
|
|
1435 Unlike (get-char-property POS 'field) this, works with empty fields too."
|
|
1436 (let ((field-extent (map-extents (lambda (extent ignore)
|
|
1437 extent)
|
|
1438 nil pos pos nil nil 'field)))
|
|
1439 (and field-extent
|
|
1440 (extent-property field-extent 'field))))
|
|
1441
|
|
1442 ;; Old version, without `map-extents'.
|
|
1443 ;(defun widget-field-find (pos)
|
|
1444 ; (let ((fields widget-field-list)
|
|
1445 ; field found)
|
|
1446 ; (while fields
|
|
1447 ; (setq field (car fields)
|
|
1448 ; fields (cdr fields))
|
|
1449 ; (let ((start (widget-field-start field))
|
|
1450 ; (end (widget-field-end field)))
|
|
1451 ; (when (and (<= start pos) (<= pos end))
|
|
1452 ; (when found
|
|
1453 ; (debug "Overlapping fields"))
|
|
1454 ; (setq found field))))
|
|
1455 ; found))
|
|
1456
|
|
1457 (defun widget-before-change (from to)
|
|
1458 ;; Barf if the text changed is outside the editable fields.
|
|
1459 (unless inhibit-read-only
|
|
1460 (let ((from-field (widget-field-find from))
|
|
1461 (to-field (widget-field-find to)))
|
|
1462 (cond ((or (null from-field)
|
|
1463 (null to-field))
|
|
1464 ;; Either end of change is not within a field.
|
|
1465 (add-hook 'post-command-hook 'widget-add-change nil t)
|
|
1466 (error "Attempt to change text outside editable field"))
|
|
1467 ((not (eq from-field to-field))
|
|
1468 ;; The change begins in one fields, and ends in another one.
|
|
1469 (add-hook 'post-command-hook 'widget-add-change nil t)
|
|
1470 (error "Change should be restricted to a single field"))
|
213
|
1471 ((or (and from-field
|
|
1472 (get-char-property from 'widget-inactive))
|
|
1473 (and to-field
|
|
1474 (get-char-property to 'widget-inactive)))
|
|
1475 ;; Trying to change an inactive editable field.
|
|
1476 (add-hook 'post-command-hook 'widget-add-change nil t)
|
|
1477 (error "Attempt to change an inactive field"))
|
209
|
1478 (widget-field-use-before-change
|
|
1479 ;; #### Bletch! This loses because XEmacs get confused
|
|
1480 ;; if before-change-functions change the contents of
|
|
1481 ;; buffer before from/to.
|
|
1482 (condition-case nil
|
|
1483 (widget-apply from-field :notify from-field)
|
|
1484 (error (debug "Before Change"))))))))
|
|
1485
|
|
1486 (defun widget-add-change ()
|
|
1487 (make-local-hook 'post-command-hook)
|
|
1488 (remove-hook 'post-command-hook 'widget-add-change t)
|
|
1489 (make-local-hook 'before-change-functions)
|
|
1490 (add-hook 'before-change-functions 'widget-before-change nil t)
|
|
1491 (make-local-hook 'after-change-functions)
|
|
1492 (add-hook 'after-change-functions 'widget-after-change nil t))
|
|
1493
|
|
1494 (defun widget-after-change (from to old)
|
|
1495 ;; Adjust field size and text properties.
|
|
1496
|
|
1497 ;; Also, notify the widgets (so, for example, a variable changes its
|
|
1498 ;; state to `modified'. when it is being edited.)
|
|
1499 (condition-case nil
|
|
1500 (let ((field (widget-field-find from))
|
|
1501 (other (widget-field-find to)))
|
|
1502 (when field
|
|
1503 (unless (eq field other)
|
|
1504 (debug "Change in different fields"))
|
290
|
1505 (let ((size (widget-get field :size)))
|
209
|
1506 (when size
|
|
1507 (let ((begin (widget-field-start field))
|
|
1508 (end (widget-field-end field)))
|
|
1509 (cond ((< (- end begin) size)
|
|
1510 ;; Field too small.
|
|
1511 (save-excursion
|
|
1512 (goto-char end)
|
|
1513 (insert-char ?\ (- (+ begin size) end))))
|
|
1514 ((> (- end begin) size)
|
|
1515 ;; Field too large and
|
|
1516 (if (or (< (point) (+ begin size))
|
|
1517 (> (point) end))
|
|
1518 ;; Point is outside extra space.
|
|
1519 (setq begin (+ begin size))
|
|
1520 ;; Point is within the extra space.
|
|
1521 (setq begin (point)))
|
|
1522 (save-excursion
|
|
1523 (goto-char end)
|
|
1524 (while (and (eq (preceding-char) ?\ )
|
|
1525 (> (point) begin))
|
|
1526 (delete-backward-char 1)))))))
|
290
|
1527 (widget-specify-secret field))
|
209
|
1528 (widget-apply field :notify field)))
|
|
1529 (error (debug "After Change"))))
|
|
1530
|
|
1531
|
|
1532 ;;; Widget Functions
|
|
1533 ;;
|
|
1534 ;; These functions are used in the definition of multiple widgets.
|
|
1535
|
|
1536 (defun widget-parent-action (widget &optional event)
|
|
1537 "Tell :parent of WIDGET to handle the :action.
|
|
1538 Optional EVENT is the event that triggered the action."
|
|
1539 (widget-apply (widget-get widget :parent) :action event))
|
|
1540
|
|
1541 (defun widget-children-value-delete (widget)
|
|
1542 "Delete all :children and :buttons in WIDGET."
|
|
1543 (mapc 'widget-delete (widget-get widget :children))
|
|
1544 (widget-put widget :children nil)
|
|
1545 (mapc 'widget-delete (widget-get widget :buttons))
|
|
1546 (widget-put widget :buttons nil))
|
|
1547
|
|
1548 (defun widget-children-validate (widget)
|
|
1549 "All the :children must be valid."
|
|
1550 (let ((children (widget-get widget :children))
|
|
1551 child found)
|
|
1552 (while (and children (not found))
|
|
1553 (setq child (car children)
|
|
1554 children (cdr children)
|
|
1555 found (widget-apply child :validate)))
|
|
1556 found))
|
|
1557
|
|
1558 (defun widget-types-convert-widget (widget)
|
|
1559 "Convert :args as widget types in WIDGET."
|
|
1560 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
|
|
1561 widget)
|
|
1562
|
|
1563 (defun widget-value-convert-widget (widget)
|
|
1564 "Initialize :value from :args in WIDGET."
|
|
1565 (let ((args (widget-get widget :args)))
|
|
1566 (when args
|
|
1567 (widget-put widget :value (car args))
|
|
1568 ;; Don't convert :value here, as this is done in `widget-convert'.
|
|
1569 ;; (widget-put widget :value (widget-apply widget
|
|
1570 ;; :value-to-internal (car args)))
|
|
1571 (widget-put widget :args nil)))
|
|
1572 widget)
|
|
1573
|
|
1574 (defun widget-value-value-get (widget)
|
|
1575 "Return the :value property of WIDGET."
|
|
1576 (widget-get widget :value))
|
|
1577
|
|
1578 ;;; The `default' Widget.
|
|
1579
|
|
1580 (define-widget 'default nil
|
|
1581 "Basic widget other widgets are derived from."
|
|
1582 :value-to-internal (lambda (widget value) value)
|
|
1583 :value-to-external (lambda (widget value) value)
|
|
1584 :button-prefix 'widget-button-prefix
|
|
1585 :button-suffix 'widget-button-suffix
|
|
1586 :complete 'widget-default-complete
|
|
1587 :create 'widget-default-create
|
|
1588 :indent nil
|
|
1589 :offset 0
|
|
1590 :format-handler 'widget-default-format-handler
|
|
1591 :button-face-get 'widget-default-button-face-get
|
|
1592 :sample-face-get 'widget-default-sample-face-get
|
|
1593 :button-keymap widget-button-keymap
|
|
1594 :delete 'widget-default-delete
|
|
1595 :value-set 'widget-default-value-set
|
|
1596 :value-inline 'widget-default-value-inline
|
280
|
1597 :default-get 'widget-default-default-get
|
209
|
1598 :menu-tag-get 'widget-default-menu-tag-get
|
|
1599 :validate (lambda (widget) nil)
|
|
1600 :active 'widget-default-active
|
|
1601 :activate 'widget-specify-active
|
|
1602 :deactivate 'widget-default-deactivate
|
|
1603 :mouse-down-action (lambda (widget event) nil)
|
|
1604 :action 'widget-default-action
|
|
1605 :notify 'widget-default-notify
|
|
1606 :prompt-value 'widget-default-prompt-value)
|
|
1607
|
|
1608 (defun widget-default-complete (widget)
|
|
1609 "Call the value of the :complete-function property of WIDGET.
|
|
1610 If that does not exists, call the value of `widget-complete-field'."
|
|
1611 (let ((fun (widget-get widget :complete-function)))
|
|
1612 (call-interactively (or fun widget-complete-field))))
|
|
1613
|
|
1614 (defun widget-default-create (widget)
|
|
1615 "Create WIDGET at point in the current buffer."
|
|
1616 (widget-specify-insert
|
|
1617 (let ((from (point))
|
|
1618 button-begin button-end button-glyph
|
|
1619 sample-begin sample-end
|
|
1620 doc-begin doc-end
|
|
1621 value-pos)
|
|
1622 (insert (widget-get widget :format))
|
|
1623 (goto-char from)
|
|
1624 ;; Parse escapes in format. Coding this in C would speed up
|
|
1625 ;; things *a lot*.
|
|
1626 (while (re-search-forward "%\\(.\\)" nil t)
|
|
1627 (let ((escape (aref (match-string 1) 0)))
|
|
1628 (replace-match "" t t)
|
|
1629 (cond ((eq escape ?%)
|
|
1630 (insert "%"))
|
|
1631 ((eq escape ?\[)
|
|
1632 (setq button-begin (point-marker))
|
|
1633 (set-marker-insertion-type button-begin nil))
|
|
1634 ((eq escape ?\])
|
|
1635 (setq button-end (point-marker))
|
|
1636 (set-marker-insertion-type button-end nil))
|
|
1637 ((eq escape ?\{)
|
|
1638 (setq sample-begin (point)))
|
|
1639 ((eq escape ?\})
|
|
1640 (setq sample-end (point)))
|
|
1641 ((eq escape ?n)
|
|
1642 (when (widget-get widget :indent)
|
|
1643 (insert "\n")
|
|
1644 (insert-char ?\ (widget-get widget :indent))))
|
|
1645 ((eq escape ?t)
|
|
1646 (let* ((tag (widget-get widget :tag))
|
|
1647 (glyph (widget-get widget :tag-glyph)))
|
|
1648 (cond (glyph
|
|
1649 (setq button-glyph
|
|
1650 (widget-glyph-insert
|
|
1651 widget (or tag "Image") glyph)))
|
|
1652 (tag
|
|
1653 (insert tag))
|
|
1654 (t
|
|
1655 (let ((standard-output (current-buffer)))
|
|
1656 (princ (widget-get widget :value)))))))
|
|
1657 ((eq escape ?d)
|
|
1658 (let ((doc (widget-get widget :doc)))
|
|
1659 (when doc
|
|
1660 (setq doc-begin (point))
|
|
1661 (insert doc)
|
|
1662 (while (eq (preceding-char) ?\n)
|
|
1663 (delete-backward-char 1))
|
|
1664 (insert "\n")
|
|
1665 (setq doc-end (point)))))
|
|
1666 ((eq escape ?v)
|
|
1667 (if (and button-begin (not button-end))
|
|
1668 (widget-apply widget :value-create)
|
|
1669 (setq value-pos (point-marker))))
|
|
1670 (t
|
|
1671 (widget-apply widget :format-handler escape)))))
|
|
1672 ;; Specify button, sample, and doc, and insert value.
|
|
1673 (when (and button-begin button-end)
|
|
1674 (unless button-glyph
|
|
1675 (goto-char button-begin)
|
|
1676 (insert (widget-get-indirect widget :button-prefix))
|
|
1677 (goto-char button-end)
|
|
1678 (set-marker-insertion-type button-end t)
|
|
1679 (insert (widget-get-indirect widget :button-suffix)))
|
|
1680 (widget-specify-button widget button-begin button-end)
|
|
1681 ;; Is this necessary?
|
|
1682 (set-marker button-begin nil)
|
|
1683 (set-marker button-end nil))
|
|
1684 (and sample-begin sample-end
|
|
1685 (widget-specify-sample widget sample-begin sample-end))
|
|
1686 (and doc-begin doc-end
|
|
1687 (widget-specify-doc widget doc-begin doc-end))
|
|
1688 (when value-pos
|
|
1689 (goto-char value-pos)
|
|
1690 (widget-apply widget :value-create)))
|
|
1691 (let ((from (point-min-marker))
|
|
1692 (to (point-max-marker)))
|
|
1693 (set-marker-insertion-type from t)
|
|
1694 (set-marker-insertion-type to nil)
|
|
1695 (widget-put widget :from from)
|
|
1696 (widget-put widget :to to)))
|
|
1697 (widget-clear-undo))
|
|
1698
|
|
1699 (defun widget-default-format-handler (widget escape)
|
|
1700 ;; We recognize the %h escape by default.
|
|
1701 (let* ((buttons (widget-get widget :buttons)))
|
|
1702 (cond ((eq escape ?h)
|
|
1703 (let* ((doc-property (widget-get widget :documentation-property))
|
|
1704 (doc-try (cond ((widget-get widget :doc))
|
|
1705 ((symbolp doc-property)
|
|
1706 (documentation-property
|
|
1707 (widget-get widget :value)
|
|
1708 doc-property))
|
|
1709 (t
|
|
1710 (funcall doc-property
|
|
1711 (widget-get widget :value)))))
|
|
1712 (doc-text (and (stringp doc-try)
|
|
1713 (> (length doc-try) 1)
|
|
1714 doc-try))
|
|
1715 (doc-indent (widget-get widget :documentation-indent)))
|
|
1716 (when doc-text
|
|
1717 (and (eq (preceding-char) ?\n)
|
|
1718 (widget-get widget :indent)
|
|
1719 (insert-char ?\ (widget-get widget :indent)))
|
|
1720 ;; The `*' in the beginning is redundant.
|
|
1721 (when (eq (aref doc-text 0) ?*)
|
|
1722 (setq doc-text (substring doc-text 1)))
|
|
1723 ;; Get rid of trailing newlines.
|
|
1724 (when (string-match "\n+\\'" doc-text)
|
|
1725 (setq doc-text (substring doc-text 0 (match-beginning 0))))
|
|
1726 (push (widget-create-child-and-convert
|
|
1727 widget 'documentation-string
|
|
1728 :indent (cond ((numberp doc-indent)
|
|
1729 doc-indent)
|
|
1730 ((null doc-indent)
|
|
1731 nil)
|
|
1732 (t 0))
|
|
1733 doc-text)
|
|
1734 buttons))))
|
|
1735 (t
|
227
|
1736 (signal 'error (list "Unknown escape" escape))))
|
209
|
1737 (widget-put widget :buttons buttons)))
|
|
1738
|
|
1739 (defun widget-default-button-face-get (widget)
|
|
1740 ;; Use :button-face or widget-button-face
|
|
1741 (or (widget-get widget :button-face)
|
|
1742 (let ((parent (widget-get widget :parent)))
|
|
1743 (if parent
|
|
1744 (widget-apply parent :button-face-get)
|
|
1745 widget-button-face))))
|
|
1746
|
|
1747 (defun widget-default-sample-face-get (widget)
|
|
1748 ;; Use :sample-face.
|
|
1749 (widget-get widget :sample-face))
|
|
1750
|
|
1751 (defun widget-default-delete (widget)
|
|
1752 ;; Remove widget from the buffer.
|
|
1753 (let ((from (widget-get widget :from))
|
|
1754 (to (widget-get widget :to))
|
|
1755 (inactive-extent (widget-get widget :inactive))
|
|
1756 (button-extent (widget-get widget :button-extent))
|
|
1757 (sample-extent (widget-get widget :sample-extent))
|
|
1758 (doc-extent (widget-get widget :doc-extent))
|
|
1759 before-change-functions
|
|
1760 after-change-functions
|
|
1761 (inhibit-read-only t))
|
|
1762 (widget-apply widget :value-delete)
|
|
1763 (when inactive-extent
|
|
1764 (detach-extent inactive-extent))
|
|
1765 (when button-extent
|
|
1766 (detach-extent button-extent))
|
|
1767 (when sample-extent
|
|
1768 (detach-extent sample-extent))
|
|
1769 (when doc-extent
|
|
1770 (detach-extent doc-extent))
|
|
1771 (when (< from to)
|
|
1772 ;; Kludge: this doesn't need to be true for empty formats.
|
|
1773 (delete-region from to))
|
|
1774 (set-marker from nil)
|
|
1775 (set-marker to nil))
|
|
1776 (widget-clear-undo))
|
|
1777
|
|
1778 (defun widget-default-value-set (widget value)
|
|
1779 ;; Recreate widget with new value.
|
|
1780 (let* ((old-pos (point))
|
|
1781 (from (copy-marker (widget-get widget :from)))
|
|
1782 (to (copy-marker (widget-get widget :to)))
|
|
1783 (offset (if (and (<= from old-pos) (<= old-pos to))
|
|
1784 (if (>= old-pos (1- to))
|
|
1785 (- old-pos to 1)
|
|
1786 (- old-pos from)))))
|
|
1787 ;;??? Bug: this ought to insert the new value before deleting the old one,
|
|
1788 ;; so that markers on either side of the value automatically
|
|
1789 ;; stay on the same side. -- rms.
|
|
1790 (save-excursion
|
|
1791 (goto-char (widget-get widget :from))
|
|
1792 (widget-apply widget :delete)
|
|
1793 (widget-put widget :value value)
|
|
1794 (widget-apply widget :create))
|
|
1795 (when offset
|
|
1796 (if (< offset 0)
|
|
1797 (goto-char (+ (widget-get widget :to) offset 1))
|
|
1798 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
|
|
1799
|
|
1800 (defun widget-default-value-inline (widget)
|
|
1801 ;; Wrap value in a list unless it is inline.
|
|
1802 (if (widget-get widget :inline)
|
|
1803 (widget-value widget)
|
|
1804 (list (widget-value widget))))
|
|
1805
|
280
|
1806 (defun widget-default-default-get (widget)
|
|
1807 ;; Get `:value'.
|
|
1808 (widget-get widget :value))
|
|
1809
|
209
|
1810 (defun widget-default-menu-tag-get (widget)
|
|
1811 ;; Use tag or value for menus.
|
|
1812 (or (widget-get widget :menu-tag)
|
|
1813 (widget-get widget :tag)
|
|
1814 (widget-princ-to-string (widget-get widget :value))))
|
|
1815
|
|
1816 (defun widget-default-active (widget)
|
|
1817 "Return t iff this widget active (user modifiable)."
|
|
1818 (and (not (widget-get widget :inactive))
|
|
1819 (let ((parent (widget-get widget :parent)))
|
|
1820 (or (null parent)
|
|
1821 (widget-apply parent :active)))))
|
|
1822
|
|
1823 (defun widget-default-deactivate (widget)
|
|
1824 "Make WIDGET inactive for user modifications."
|
|
1825 (widget-specify-inactive widget
|
|
1826 (widget-get widget :from)
|
|
1827 (widget-get widget :to)))
|
|
1828
|
|
1829 (defun widget-default-action (widget &optional event)
|
|
1830 ;; Notify the parent when a widget change
|
|
1831 (let ((parent (widget-get widget :parent)))
|
|
1832 (when parent
|
|
1833 (widget-apply parent :notify widget event))))
|
|
1834
|
|
1835 (defun widget-default-notify (widget child &optional event)
|
|
1836 ;; Pass notification to parent.
|
|
1837 (widget-default-action widget event))
|
|
1838
|
|
1839 (defun widget-default-prompt-value (widget prompt value unbound)
|
|
1840 ;; Read an arbitrary value. Stolen from `set-variable'.
|
|
1841 ;; (let ((initial (if unbound
|
|
1842 ;; nil
|
|
1843 ;; ;; It would be nice if we could do a `(cons val 1)' here.
|
|
1844 ;; (prin1-to-string (custom-quote value))))))
|
|
1845 (eval-minibuffer prompt ))
|
|
1846
|
|
1847 ;;; The `item' Widget.
|
|
1848
|
|
1849 (define-widget 'item 'default
|
|
1850 "Constant items for inclusion in other widgets."
|
|
1851 :convert-widget 'widget-value-convert-widget
|
|
1852 :value-create 'widget-item-value-create
|
|
1853 :value-delete 'ignore
|
|
1854 :value-get 'widget-value-value-get
|
|
1855 :match 'widget-item-match
|
|
1856 :match-inline 'widget-item-match-inline
|
|
1857 :action 'widget-item-action
|
|
1858 :format "%t\n")
|
|
1859
|
|
1860 (defun widget-item-value-create (widget)
|
|
1861 ;; Insert the printed representation of the value.
|
|
1862 (let ((standard-output (current-buffer)))
|
|
1863 (princ (widget-get widget :value))))
|
|
1864
|
|
1865 (defun widget-item-match (widget value)
|
|
1866 ;; Match if the value is the same.
|
|
1867 (equal (widget-get widget :value) value))
|
|
1868
|
|
1869 (defun widget-item-match-inline (widget values)
|
|
1870 ;; Match if the value is the same.
|
|
1871 (let ((value (widget-get widget :value)))
|
|
1872 (and (listp value)
|
|
1873 (<= (length value) (length values))
|
|
1874 (let ((head (widget-sublist values 0 (length value))))
|
|
1875 (and (equal head value)
|
|
1876 (cons head (widget-sublist values (length value))))))))
|
|
1877
|
|
1878 (defun widget-sublist (list start &optional end)
|
|
1879 "Return the sublist of LIST from START to END.
|
|
1880 If END is omitted, it defaults to the length of LIST."
|
|
1881 (if (> start 0) (setq list (nthcdr start list)))
|
|
1882 (if end
|
|
1883 (if (<= end start)
|
|
1884 nil
|
|
1885 (setq list (copy-sequence list))
|
|
1886 (setcdr (nthcdr (- end start 1) list) nil)
|
|
1887 list)
|
|
1888 (copy-sequence list)))
|
|
1889
|
|
1890 (defun widget-item-action (widget &optional event)
|
|
1891 ;; Just notify itself.
|
|
1892 (widget-apply widget :notify widget event))
|
|
1893
|
|
1894 ;;; The `push-button' Widget.
|
|
1895
|
|
1896 (defcustom widget-push-button-gui widget-glyph-enable
|
|
1897 "If non nil, use GUI push buttons when available."
|
|
1898 :group 'widgets
|
|
1899 :type 'boolean)
|
|
1900
|
|
1901 (defcustom widget-push-button-prefix "["
|
|
1902 "String used as prefix for buttons."
|
|
1903 :type 'string
|
|
1904 :group 'widget-button)
|
|
1905
|
|
1906 (defcustom widget-push-button-suffix "]"
|
|
1907 "String used as suffix for buttons."
|
|
1908 :type 'string
|
|
1909 :group 'widget-button)
|
|
1910
|
|
1911 (define-widget 'push-button 'item
|
|
1912 "A pushable button."
|
|
1913 :button-prefix ""
|
|
1914 :button-suffix ""
|
|
1915 :value-create 'widget-push-button-value-create
|
|
1916 :format "%[%v%]")
|
|
1917
|
|
1918 (defun widget-push-button-value-create (widget)
|
|
1919 ;; Insert text representing the `on' and `off' states.
|
|
1920 (let* ((tag (or (widget-get widget :tag)
|
|
1921 (widget-get widget :value)))
|
|
1922 (tag-glyph (widget-get widget :tag-glyph))
|
|
1923 (text (concat widget-push-button-prefix
|
|
1924 tag widget-push-button-suffix))
|
398
|
1925 gui)
|
209
|
1926 (cond (tag-glyph
|
|
1927 (widget-glyph-insert widget text tag-glyph))
|
|
1928 ;; We must check for console-on-window-system-p here,
|
|
1929 ;; because GUI will not work otherwise (it needs RGB
|
|
1930 ;; components for colors, and they are not known on TTYs).
|
|
1931 ((and widget-push-button-gui
|
|
1932 (console-on-window-system-p))
|
398
|
1933 (let* ((gui-button-shadow-thickness 1))
|
|
1934 (setq gui (make-glyph
|
|
1935 (make-gui-button tag 'widget-gui-action widget))))
|
|
1936 (widget-glyph-insert-glyph widget gui))
|
209
|
1937 (t
|
|
1938 (insert text)))))
|
|
1939
|
|
1940 (defun widget-gui-action (widget)
|
|
1941 "Apply :action for WIDGET."
|
|
1942 (widget-apply-action widget (this-command-keys)))
|
|
1943
|
|
1944 ;;; The `link' Widget.
|
|
1945
|
|
1946 (defcustom widget-link-prefix "["
|
|
1947 "String used as prefix for links."
|
|
1948 :type 'string
|
|
1949 :group 'widget-button)
|
|
1950
|
|
1951 (defcustom widget-link-suffix "]"
|
|
1952 "String used as suffix for links."
|
|
1953 :type 'string
|
|
1954 :group 'widget-button)
|
|
1955
|
|
1956 (define-widget 'link 'item
|
|
1957 "An embedded link."
|
|
1958 :button-prefix 'widget-link-prefix
|
|
1959 :button-suffix 'widget-link-suffix
|
|
1960 :help-echo "Follow the link"
|
|
1961 :format "%[%t%]")
|
|
1962
|
|
1963 ;;; The `info-link' Widget.
|
|
1964
|
|
1965 (define-widget 'info-link 'link
|
|
1966 "A link to an info file."
|
|
1967 :help-echo 'widget-info-link-help-echo
|
|
1968 :action 'widget-info-link-action)
|
|
1969
|
|
1970 (defun widget-info-link-help-echo (widget)
|
|
1971 (concat "Read the manual entry `" (widget-value widget) "'"))
|
|
1972
|
|
1973 (defun widget-info-link-action (widget &optional event)
|
|
1974 "Open the info node specified by WIDGET."
|
|
1975 (Info-goto-node (widget-value widget)))
|
|
1976
|
|
1977 ;;; The `url-link' Widget.
|
|
1978
|
|
1979 (define-widget 'url-link 'link
|
|
1980 "A link to an www page."
|
|
1981 :help-echo 'widget-url-link-help-echo
|
|
1982 :action 'widget-url-link-action)
|
|
1983
|
|
1984 (defun widget-url-link-help-echo (widget)
|
|
1985 (concat "Visit <URL:" (widget-value widget) ">"))
|
|
1986
|
|
1987 (defun widget-url-link-action (widget &optional event)
|
|
1988 "Open the url specified by WIDGET."
|
219
|
1989 (if (boundp 'browse-url-browser-function)
|
|
1990 (funcall browse-url-browser-function (widget-value widget))
|
|
1991 (error "Cannot follow URLs in this XEmacs")))
|
209
|
1992
|
|
1993 ;;; The `function-link' Widget.
|
|
1994
|
|
1995 (define-widget 'function-link 'link
|
|
1996 "A link to an Emacs function."
|
|
1997 :action 'widget-function-link-action)
|
|
1998
|
|
1999 (defun widget-function-link-action (widget &optional event)
|
|
2000 "Show the function specified by WIDGET."
|
|
2001 (describe-function (widget-value widget)))
|
|
2002
|
|
2003 ;;; The `variable-link' Widget.
|
|
2004
|
|
2005 (define-widget 'variable-link 'link
|
|
2006 "A link to an Emacs variable."
|
|
2007 :action 'widget-variable-link-action)
|
|
2008
|
|
2009 (defun widget-variable-link-action (widget &optional event)
|
|
2010 "Show the variable specified by WIDGET."
|
|
2011 (describe-variable (widget-value widget)))
|
|
2012
|
|
2013 ;;; The `file-link' Widget.
|
|
2014
|
|
2015 (define-widget 'file-link 'link
|
|
2016 "A link to a file."
|
|
2017 :action 'widget-file-link-action)
|
|
2018
|
|
2019 (defun widget-file-link-action (widget &optional event)
|
|
2020 "Find the file specified by WIDGET."
|
|
2021 (find-file (widget-value widget)))
|
|
2022
|
|
2023 ;;; The `emacs-library-link' Widget.
|
|
2024
|
|
2025 (define-widget 'emacs-library-link 'link
|
|
2026 "A link to an Emacs Lisp library file."
|
|
2027 :help-echo 'widget-emacs-library-link-help-echo
|
|
2028 :action 'widget-emacs-library-link-action)
|
|
2029
|
|
2030 (defun widget-emacs-library-link-help-echo (widget)
|
|
2031 (concat "Visit " (widget-value widget)))
|
|
2032
|
|
2033 (defun widget-emacs-library-link-action (widget &optional event)
|
|
2034 "Find the Emacs Library file specified by WIDGET."
|
|
2035 (find-file (locate-library (widget-value widget))))
|
|
2036
|
|
2037 ;;; The `emacs-commentary-link' Widget.
|
|
2038
|
|
2039 (define-widget 'emacs-commentary-link 'link
|
|
2040 "A link to Commentary in an Emacs Lisp library file."
|
|
2041 :action 'widget-emacs-commentary-link-action)
|
|
2042
|
|
2043 (defun widget-emacs-commentary-link-action (widget &optional event)
|
|
2044 "Find the Commentary section of the Emacs file specified by WIDGET."
|
|
2045 (finder-commentary (widget-value widget)))
|
|
2046
|
|
2047 ;;; The `editable-field' Widget.
|
|
2048
|
|
2049 (define-widget 'editable-field 'default
|
|
2050 "An editable text field."
|
|
2051 :convert-widget 'widget-value-convert-widget
|
|
2052 :keymap widget-field-keymap
|
|
2053 :format "%v"
|
|
2054 :value ""
|
|
2055 :prompt-internal 'widget-field-prompt-internal
|
|
2056 :prompt-history 'widget-field-history
|
|
2057 :prompt-value 'widget-field-prompt-value
|
|
2058 :action 'widget-field-action
|
|
2059 :validate 'widget-field-validate
|
|
2060 :valid-regexp ""
|
|
2061 :error "No match"
|
|
2062 :value-create 'widget-field-value-create
|
|
2063 :value-delete 'widget-field-value-delete
|
|
2064 :value-get 'widget-field-value-get
|
|
2065 :match 'widget-field-match)
|
|
2066
|
|
2067 (defvar widget-field-history nil
|
|
2068 "History of field minibuffer edits.")
|
|
2069
|
|
2070 (defun widget-field-prompt-internal (widget prompt initial history)
|
|
2071 ;; Read string for WIDGET prompting with PROMPT.
|
|
2072 ;; INITIAL is the initial input and HISTORY is a symbol containing
|
|
2073 ;; the earlier input.
|
|
2074 (read-string prompt initial history))
|
|
2075
|
|
2076 (defun widget-field-prompt-value (widget prompt value unbound)
|
|
2077 ;; Prompt for a string.
|
|
2078 (let ((initial (if unbound
|
|
2079 nil
|
|
2080 (cons (widget-apply widget :value-to-internal
|
|
2081 value) 0)))
|
|
2082 (history (widget-get widget :prompt-history)))
|
|
2083 (let ((answer (widget-apply widget
|
|
2084 :prompt-internal prompt initial history)))
|
|
2085 (widget-apply widget :value-to-external answer))))
|
|
2086
|
|
2087 (defvar widget-edit-functions nil)
|
|
2088
|
|
2089 (defun widget-field-action (widget &optional event)
|
|
2090 ;; Edit the value in the minibuffer.
|
|
2091 (let* ((invalid (widget-apply widget :validate))
|
|
2092 (prompt (concat (widget-apply widget :menu-tag-get) ": "))
|
|
2093 (value (unless invalid
|
|
2094 (widget-value widget)))
|
|
2095 (answer (widget-apply widget :prompt-value prompt value invalid)))
|
|
2096 (unless (equal value answer)
|
|
2097 ;; This is a hack. We can't properly validate the widget
|
|
2098 ;; because validation requires the new value to be in the field.
|
|
2099 ;; However, widget-field-value-create will not function unless
|
|
2100 ;; the new value matches. So, we check whether the thing
|
|
2101 ;; matches, and if it does, use either the real or a dummy error
|
|
2102 ;; message.
|
|
2103 (unless (widget-apply widget :match answer)
|
|
2104 (let ((error-message (or (widget-get widget :type-error)
|
|
2105 "Invalid field contents")))
|
|
2106 (widget-put widget :error error-message)
|
|
2107 (error error-message)))
|
|
2108 (widget-value-set widget answer)
|
|
2109 (widget-apply widget :notify widget event)
|
|
2110 (widget-setup))
|
|
2111 (run-hook-with-args 'widget-edit-functions widget)))
|
|
2112
|
|
2113 ;(defun widget-field-action (widget &optional event)
|
|
2114 ; ;; Move to next field.
|
|
2115 ; (widget-forward 1)
|
|
2116 ; (run-hook-with-args 'widget-edit-functions widget))
|
|
2117
|
|
2118 (defun widget-field-validate (widget)
|
|
2119 ;; Valid if the content matches `:valid-regexp'.
|
|
2120 (save-excursion
|
|
2121 (let ((value (widget-apply widget :value-get))
|
|
2122 (regexp (widget-get widget :valid-regexp)))
|
|
2123 (if (string-match regexp value)
|
|
2124 nil
|
|
2125 widget))))
|
|
2126
|
|
2127 (defun widget-field-value-create (widget)
|
|
2128 ;; Create an editable text field.
|
|
2129 (let ((size (widget-get widget :size))
|
|
2130 (value (widget-get widget :value))
|
|
2131 (from (point))
|
|
2132 ;; This is changed to a real extent in `widget-setup'. We
|
|
2133 ;; need the end points to behave differently until
|
|
2134 ;; `widget-setup' is called. Should probably be replaced with
|
|
2135 ;; a genuine extent, but some things break, then.
|
|
2136 (extent (cons (make-marker) (make-marker))))
|
|
2137 (widget-put widget :field-extent extent)
|
|
2138 (insert value)
|
|
2139 (and size
|
|
2140 (< (length value) size)
|
|
2141 (insert-char ?\ (- size (length value))))
|
|
2142 (unless (memq widget widget-field-list)
|
|
2143 (push widget widget-field-new))
|
|
2144 (move-marker (cdr extent) (point))
|
|
2145 (set-marker-insertion-type (cdr extent) nil)
|
|
2146 (when (null size)
|
|
2147 (insert ?\n))
|
|
2148 (move-marker (car extent) from)
|
|
2149 (set-marker-insertion-type (car extent) t)))
|
|
2150
|
|
2151 (defun widget-field-value-delete (widget)
|
|
2152 ;; Remove the widget from the list of active editing fields.
|
|
2153 (setq widget-field-list (delq widget widget-field-list))
|
|
2154 ;; These are nil if the :format string doesn't contain `%v'.
|
|
2155 (let ((extent (widget-get widget :field-extent)))
|
|
2156 (when extent
|
|
2157 (detach-extent extent))))
|
|
2158
|
|
2159 (defun widget-field-value-get (widget)
|
|
2160 ;; Return current text in editing field.
|
|
2161 (let ((from (widget-field-start widget))
|
|
2162 (to (widget-field-end widget))
|
|
2163 (buffer (widget-field-buffer widget))
|
|
2164 (size (widget-get widget :size))
|
|
2165 (secret (widget-get widget :secret))
|
|
2166 (old (current-buffer)))
|
|
2167 (cond
|
|
2168 ((and from to)
|
|
2169 (set-buffer buffer)
|
|
2170 (while (and size
|
|
2171 (not (zerop size))
|
|
2172 (> to from)
|
|
2173 (eq (char-after (1- to)) ?\ ))
|
|
2174 (setq to (1- to)))
|
|
2175 (let ((result (buffer-substring-no-properties from to)))
|
|
2176 (when secret
|
|
2177 (let ((index 0))
|
|
2178 (while (< (+ from index) to)
|
|
2179 (aset result index
|
|
2180 (get-char-property (+ from index) 'secret))
|
|
2181 (incf index))))
|
|
2182 (set-buffer old)
|
|
2183 result))
|
|
2184 (t
|
|
2185 (widget-get widget :value)))))
|
|
2186
|
|
2187 (defun widget-field-match (widget value)
|
|
2188 ;; Match any string.
|
|
2189 (stringp value))
|
|
2190
|
|
2191 ;;; The `text' Widget.
|
|
2192
|
|
2193 (define-widget 'text 'editable-field
|
|
2194 :keymap widget-text-keymap
|
|
2195 "A multiline text area.")
|
|
2196
|
|
2197 ;;; The `menu-choice' Widget.
|
|
2198
|
|
2199 (define-widget 'menu-choice 'default
|
|
2200 "A menu of options."
|
|
2201 :convert-widget 'widget-types-convert-widget
|
|
2202 :format "%[%t%]: %v"
|
|
2203 :case-fold t
|
|
2204 :tag "choice"
|
|
2205 :void '(item :format "invalid (%t)\n")
|
|
2206 :value-create 'widget-choice-value-create
|
|
2207 :value-delete 'widget-children-value-delete
|
|
2208 :value-get 'widget-choice-value-get
|
|
2209 :value-inline 'widget-choice-value-inline
|
280
|
2210 :default-get 'widget-choice-default-get
|
209
|
2211 :mouse-down-action 'widget-choice-mouse-down-action
|
|
2212 :action 'widget-choice-action
|
|
2213 :error "Make a choice"
|
|
2214 :validate 'widget-choice-validate
|
|
2215 :match 'widget-choice-match
|
|
2216 :match-inline 'widget-choice-match-inline)
|
|
2217
|
|
2218 (defun widget-choice-value-create (widget)
|
|
2219 ;; Insert the first choice that matches the value.
|
|
2220 (let ((value (widget-get widget :value))
|
|
2221 (args (widget-get widget :args))
|
267
|
2222 (explicit (widget-get widget :explicit-choice))
|
209
|
2223 current)
|
267
|
2224 (if explicit
|
|
2225 (progn
|
|
2226 (widget-put widget :children (list (widget-create-child-value
|
|
2227 widget explicit value)))
|
|
2228 (widget-put widget :choice explicit))
|
|
2229 (while args
|
|
2230 (setq current (car args)
|
|
2231 args (cdr args))
|
|
2232 (when (widget-apply current :match value)
|
|
2233 (widget-put widget :children (list (widget-create-child-value
|
|
2234 widget current value)))
|
|
2235 (widget-put widget :choice current)
|
|
2236 (setq args nil
|
|
2237 current nil)))
|
|
2238 (when current
|
|
2239 (let ((void (widget-get widget :void)))
|
|
2240 (widget-put widget :children (list (widget-create-child-and-convert
|
|
2241 widget void :value value)))
|
|
2242 (widget-put widget :choice void))))))
|
209
|
2243
|
|
2244 (defun widget-choice-value-get (widget)
|
|
2245 ;; Get value of the child widget.
|
|
2246 (widget-value (car (widget-get widget :children))))
|
|
2247
|
|
2248 (defun widget-choice-value-inline (widget)
|
|
2249 ;; Get value of the child widget.
|
|
2250 (widget-apply (car (widget-get widget :children)) :value-inline))
|
|
2251
|
280
|
2252 (defun widget-choice-default-get (widget)
|
|
2253 ;; Get default for the first choice.
|
|
2254 (widget-default-get (car (widget-get widget :args))))
|
|
2255
|
209
|
2256 (defcustom widget-choice-toggle nil
|
|
2257 "If non-nil, a binary choice will just toggle between the values.
|
|
2258 Otherwise, the user will explicitly have to choose between the values
|
|
2259 when he invoked the menu."
|
|
2260 :type 'boolean
|
|
2261 :group 'widgets)
|
|
2262
|
|
2263 (defun widget-choice-mouse-down-action (widget &optional event)
|
|
2264 ;; Return non-nil if we need a menu.
|
|
2265 (let ((args (widget-get widget :args))
|
|
2266 (old (widget-get widget :choice)))
|
|
2267 (cond ((not (console-on-window-system-p))
|
|
2268 ;; No place to pop up a menu.
|
|
2269 nil)
|
|
2270 ((< (length args) 2)
|
|
2271 ;; Empty or singleton list, just return the value.
|
|
2272 nil)
|
|
2273 ((> (length args) widget-menu-max-size)
|
|
2274 ;; Too long, prompt.
|
|
2275 nil)
|
|
2276 ((> (length args) 2)
|
|
2277 ;; Reasonable sized list, use menu.
|
|
2278 t)
|
|
2279 ((and widget-choice-toggle (memq old args))
|
|
2280 ;; We toggle.
|
|
2281 nil)
|
|
2282 (t
|
|
2283 ;; Ask which of the two.
|
|
2284 t))))
|
|
2285
|
|
2286 (defun widget-choice-action (widget &optional event)
|
|
2287 ;; Make a choice.
|
|
2288 (let ((args (widget-get widget :args))
|
|
2289 (old (widget-get widget :choice))
|
|
2290 (tag (widget-apply widget :menu-tag-get))
|
|
2291 (completion-ignore-case (widget-get widget :case-fold))
|
|
2292 current choices)
|
|
2293 ;; Remember old value.
|
|
2294 (if (and old (not (widget-apply widget :validate)))
|
|
2295 (let* ((external (widget-value widget))
|
|
2296 (internal (widget-apply old :value-to-internal external)))
|
|
2297 (widget-put old :value internal)))
|
|
2298 ;; Find new choice.
|
|
2299 (setq current
|
|
2300 (cond ((= (length args) 0)
|
|
2301 nil)
|
|
2302 ((= (length args) 1)
|
|
2303 (nth 0 args))
|
|
2304 ((and widget-choice-toggle
|
|
2305 (= (length args) 2)
|
|
2306 (memq old args))
|
|
2307 (if (eq old (nth 0 args))
|
|
2308 (nth 1 args)
|
|
2309 (nth 0 args)))
|
|
2310 (t
|
|
2311 (while args
|
|
2312 (setq current (car args)
|
|
2313 args (cdr args))
|
|
2314 (setq choices
|
|
2315 (cons (cons (widget-apply current :menu-tag-get)
|
|
2316 current)
|
|
2317 choices)))
|
267
|
2318 (let ((choice
|
|
2319 (widget-choose tag (reverse choices) event)))
|
|
2320 (widget-put widget :explicit-choice choice)
|
|
2321 choice))))
|
209
|
2322 (when current
|
280
|
2323 (let ((value (widget-default-get current)))
|
|
2324 (widget-value-set widget
|
|
2325 (widget-apply current :value-to-external value)))
|
209
|
2326 (widget-setup)
|
|
2327 (widget-apply widget :notify widget event)))
|
|
2328 (run-hook-with-args 'widget-edit-functions widget))
|
|
2329
|
|
2330 (defun widget-choice-validate (widget)
|
|
2331 ;; Valid if we have made a valid choice.
|
|
2332 (let ((void (widget-get widget :void))
|
|
2333 (choice (widget-get widget :choice))
|
|
2334 (child (car (widget-get widget :children))))
|
|
2335 (if (eq void choice)
|
|
2336 widget
|
|
2337 (widget-apply child :validate))))
|
|
2338
|
|
2339 (defun widget-choice-match (widget value)
|
|
2340 ;; Matches if one of the choices matches.
|
|
2341 (let ((args (widget-get widget :args))
|
|
2342 current found)
|
|
2343 (while (and args (not found))
|
|
2344 (setq current (car args)
|
|
2345 args (cdr args)
|
|
2346 found (widget-apply current :match value)))
|
|
2347 found))
|
|
2348
|
|
2349 (defun widget-choice-match-inline (widget values)
|
|
2350 ;; Matches if one of the choices matches.
|
|
2351 (let ((args (widget-get widget :args))
|
|
2352 current found)
|
|
2353 (while (and args (null found))
|
|
2354 (setq current (car args)
|
|
2355 args (cdr args)
|
|
2356 found (widget-match-inline current values)))
|
|
2357 found))
|
|
2358
|
|
2359 ;;; The `toggle' Widget.
|
|
2360
|
|
2361 (define-widget 'toggle 'item
|
|
2362 "Toggle between two states."
|
|
2363 :format "%[%v%]\n"
|
|
2364 :value-create 'widget-toggle-value-create
|
|
2365 :action 'widget-toggle-action
|
|
2366 :match (lambda (widget value) t)
|
|
2367 :on "on"
|
|
2368 :off "off")
|
|
2369
|
|
2370 (defun widget-toggle-value-create (widget)
|
|
2371 ;; Insert text representing the `on' and `off' states.
|
|
2372 (if (widget-value widget)
|
|
2373 (widget-glyph-insert widget
|
|
2374 (widget-get widget :on)
|
|
2375 (widget-get widget :on-glyph))
|
|
2376 (widget-glyph-insert widget
|
|
2377 (widget-get widget :off)
|
|
2378 (widget-get widget :off-glyph))))
|
|
2379
|
|
2380 (defun widget-toggle-action (widget &optional event)
|
|
2381 ;; Toggle value.
|
|
2382 (widget-value-set widget (not (widget-value widget)))
|
|
2383 (widget-apply widget :notify widget event)
|
|
2384 (run-hook-with-args 'widget-edit-functions widget))
|
|
2385
|
|
2386 ;;; The `checkbox' Widget.
|
|
2387
|
|
2388 (define-widget 'checkbox 'toggle
|
|
2389 "A checkbox toggle."
|
|
2390 :button-suffix ""
|
|
2391 :button-prefix ""
|
|
2392 :format "%[%v%]"
|
|
2393 :on "[X]"
|
|
2394 :on-glyph "check1"
|
|
2395 :off "[ ]"
|
|
2396 :off-glyph "check0"
|
|
2397 :action 'widget-checkbox-action)
|
|
2398
|
|
2399 (defun widget-checkbox-action (widget &optional event)
|
|
2400 "Toggle checkbox, notify parent, and set active state of sibling."
|
|
2401 (widget-toggle-action widget event)
|
|
2402 (let ((sibling (widget-get-sibling widget)))
|
|
2403 (when sibling
|
|
2404 (if (widget-value widget)
|
|
2405 (widget-apply sibling :activate)
|
|
2406 (widget-apply sibling :deactivate)))))
|
|
2407
|
|
2408 ;;; The `checklist' Widget.
|
|
2409
|
|
2410 (define-widget 'checklist 'default
|
|
2411 "A multiple choice widget."
|
|
2412 :convert-widget 'widget-types-convert-widget
|
|
2413 :format "%v"
|
|
2414 :offset 4
|
|
2415 :entry-format "%b %v"
|
|
2416 :menu-tag "checklist"
|
|
2417 :greedy nil
|
|
2418 :value-create 'widget-checklist-value-create
|
|
2419 :value-delete 'widget-children-value-delete
|
|
2420 :value-get 'widget-checklist-value-get
|
|
2421 :validate 'widget-checklist-validate
|
|
2422 :match 'widget-checklist-match
|
|
2423 :match-inline 'widget-checklist-match-inline)
|
|
2424
|
|
2425 (defun widget-checklist-value-create (widget)
|
|
2426 ;; Insert all values
|
|
2427 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
|
|
2428 (args (widget-get widget :args)))
|
|
2429 (while args
|
|
2430 (widget-checklist-add-item widget (car args) (assq (car args) alist))
|
|
2431 (setq args (cdr args)))
|
|
2432 (widget-put widget :children (nreverse (widget-get widget :children)))))
|
|
2433
|
|
2434 (defun widget-checklist-add-item (widget type chosen)
|
|
2435 ;; Create checklist item in WIDGET of type TYPE.
|
|
2436 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
|
|
2437 (and (eq (preceding-char) ?\n)
|
|
2438 (widget-get widget :indent)
|
|
2439 (insert-char ?\ (widget-get widget :indent)))
|
|
2440 (widget-specify-insert
|
|
2441 (let* ((children (widget-get widget :children))
|
|
2442 (buttons (widget-get widget :buttons))
|
|
2443 (button-args (or (widget-get type :sibling-args)
|
|
2444 (widget-get widget :button-args)))
|
|
2445 (from (point))
|
|
2446 child button)
|
|
2447 (insert (widget-get widget :entry-format))
|
|
2448 (goto-char from)
|
|
2449 ;; Parse % escapes in format.
|
|
2450 (while (re-search-forward "%\\([bv%]\\)" nil t)
|
|
2451 (let ((escape (aref (match-string 1) 0)))
|
|
2452 (replace-match "" t t)
|
|
2453 (cond ((eq escape ?%)
|
|
2454 (insert "%"))
|
|
2455 ((eq escape ?b)
|
|
2456 (setq button (apply 'widget-create-child-and-convert
|
|
2457 widget 'checkbox
|
|
2458 :value (not (null chosen))
|
|
2459 button-args)))
|
|
2460 ((eq escape ?v)
|
|
2461 (setq child
|
|
2462 (cond ((not chosen)
|
|
2463 (let ((child (widget-create-child widget type)))
|
|
2464 (widget-apply child :deactivate)
|
|
2465 child))
|
|
2466 ((widget-get type :inline)
|
|
2467 (widget-create-child-value
|
|
2468 widget type (cdr chosen)))
|
|
2469 (t
|
|
2470 (widget-create-child-value
|
|
2471 widget type (car (cdr chosen)))))))
|
|
2472 (t
|
227
|
2473 (signal 'error (list "Unknown escape" escape))))))
|
209
|
2474 ;; Update properties.
|
|
2475 (and button child (widget-put child :button button))
|
|
2476 (and button (widget-put widget :buttons (cons button buttons)))
|
|
2477 (and child (widget-put widget :children (cons child children))))))
|
|
2478
|
|
2479 (defun widget-checklist-match (widget values)
|
|
2480 ;; All values must match a type in the checklist.
|
|
2481 (and (listp values)
|
|
2482 (null (cdr (widget-checklist-match-inline widget values)))))
|
|
2483
|
|
2484 (defun widget-checklist-match-inline (widget values)
|
|
2485 ;; Find the values which match a type in the checklist.
|
|
2486 (let ((greedy (widget-get widget :greedy))
|
|
2487 (args (copy-sequence (widget-get widget :args)))
|
|
2488 found rest)
|
|
2489 (while values
|
|
2490 (let ((answer (widget-checklist-match-up args values)))
|
|
2491 (cond (answer
|
|
2492 (let ((vals (widget-match-inline answer values)))
|
|
2493 (setq found (append found (car vals))
|
|
2494 values (cdr vals)
|
|
2495 args (delq answer args))))
|
|
2496 (greedy
|
|
2497 (setq rest (append rest (list (car values)))
|
|
2498 values (cdr values)))
|
|
2499 (t
|
|
2500 (setq rest (append rest values)
|
|
2501 values nil)))))
|
|
2502 (cons found rest)))
|
|
2503
|
|
2504 (defun widget-checklist-match-find (widget vals)
|
|
2505 ;; Find the vals which match a type in the checklist.
|
|
2506 ;; Return an alist of (TYPE MATCH).
|
|
2507 (let ((greedy (widget-get widget :greedy))
|
|
2508 (args (copy-sequence (widget-get widget :args)))
|
|
2509 found)
|
|
2510 (while vals
|
|
2511 (let ((answer (widget-checklist-match-up args vals)))
|
|
2512 (cond (answer
|
|
2513 (let ((match (widget-match-inline answer vals)))
|
|
2514 (setq found (cons (cons answer (car match)) found)
|
|
2515 vals (cdr match)
|
|
2516 args (delq answer args))))
|
|
2517 (greedy
|
|
2518 (setq vals (cdr vals)))
|
|
2519 (t
|
|
2520 (setq vals nil)))))
|
|
2521 found))
|
|
2522
|
|
2523 (defun widget-checklist-match-up (args vals)
|
398
|
2524 ;; Return the first type from ARGS that matches VALS.
|
209
|
2525 (let (current found)
|
|
2526 (while (and args (null found))
|
|
2527 (setq current (car args)
|
|
2528 args (cdr args)
|
|
2529 found (widget-match-inline current vals)))
|
|
2530 (if found
|
|
2531 current
|
|
2532 nil)))
|
|
2533
|
|
2534 (defun widget-checklist-value-get (widget)
|
|
2535 ;; The values of all selected items.
|
|
2536 (let ((children (widget-get widget :children))
|
|
2537 child result)
|
|
2538 (while children
|
|
2539 (setq child (car children)
|
|
2540 children (cdr children))
|
|
2541 (if (widget-value (widget-get child :button))
|
|
2542 (setq result (append result (widget-apply child :value-inline)))))
|
|
2543 result))
|
|
2544
|
|
2545 (defun widget-checklist-validate (widget)
|
398
|
2546 ;; Ticked children must be valid.
|
209
|
2547 (let ((children (widget-get widget :children))
|
|
2548 child button found)
|
|
2549 (while (and children (not found))
|
|
2550 (setq child (car children)
|
|
2551 children (cdr children)
|
|
2552 button (widget-get child :button)
|
|
2553 found (and (widget-value button)
|
|
2554 (widget-apply child :validate))))
|
|
2555 found))
|
|
2556
|
|
2557 ;;; The `option' Widget
|
|
2558
|
|
2559 (define-widget 'option 'checklist
|
|
2560 "An widget with an optional item."
|
|
2561 :inline t)
|
|
2562
|
|
2563 ;;; The `choice-item' Widget.
|
|
2564
|
|
2565 (define-widget 'choice-item 'item
|
|
2566 "Button items that delegate action events to their parents."
|
|
2567 :action 'widget-parent-action
|
|
2568 :format "%[%t%] \n")
|
|
2569
|
|
2570 ;;; The `radio-button' Widget.
|
|
2571
|
|
2572 (define-widget 'radio-button 'toggle
|
|
2573 "A radio button for use in the `radio' widget."
|
|
2574 :notify 'widget-radio-button-notify
|
|
2575 :format "%[%v%]"
|
|
2576 :button-suffix ""
|
|
2577 :button-prefix ""
|
|
2578 :on "(*)"
|
|
2579 :on-glyph '("radio1" nil "radio0")
|
|
2580 :off "( )"
|
|
2581 :off-glyph "radio0")
|
|
2582
|
|
2583 (defun widget-radio-button-notify (widget child &optional event)
|
|
2584 ;; Tell daddy.
|
|
2585 (widget-apply (widget-get widget :parent) :action widget event))
|
|
2586
|
|
2587 ;;; The `radio-button-choice' Widget.
|
|
2588
|
|
2589 (define-widget 'radio-button-choice 'default
|
|
2590 "Select one of multiple options."
|
|
2591 :convert-widget 'widget-types-convert-widget
|
|
2592 :offset 4
|
|
2593 :format "%v"
|
|
2594 :entry-format "%b %v"
|
|
2595 :menu-tag "radio"
|
|
2596 :value-create 'widget-radio-value-create
|
|
2597 :value-delete 'widget-children-value-delete
|
|
2598 :value-get 'widget-radio-value-get
|
|
2599 :value-inline 'widget-radio-value-inline
|
|
2600 :value-set 'widget-radio-value-set
|
|
2601 :error "You must push one of the buttons"
|
|
2602 :validate 'widget-radio-validate
|
|
2603 :match 'widget-choice-match
|
|
2604 :match-inline 'widget-choice-match-inline
|
|
2605 :action 'widget-radio-action)
|
|
2606
|
|
2607 (defun widget-radio-value-create (widget)
|
|
2608 ;; Insert all values
|
|
2609 (let ((args (widget-get widget :args))
|
|
2610 arg)
|
|
2611 (while args
|
|
2612 (setq arg (car args)
|
|
2613 args (cdr args))
|
|
2614 (widget-radio-add-item widget arg))))
|
|
2615
|
|
2616 (defun widget-radio-add-item (widget type)
|
|
2617 "Add to radio widget WIDGET a new radio button item of type TYPE."
|
|
2618 ;; (setq type (widget-convert type))
|
|
2619 (and (eq (preceding-char) ?\n)
|
|
2620 (widget-get widget :indent)
|
|
2621 (insert-char ?\ (widget-get widget :indent)))
|
|
2622 (widget-specify-insert
|
|
2623 (let* ((value (widget-get widget :value))
|
|
2624 (children (widget-get widget :children))
|
|
2625 (buttons (widget-get widget :buttons))
|
|
2626 (button-args (or (widget-get type :sibling-args)
|
|
2627 (widget-get widget :button-args)))
|
|
2628 (from (point))
|
|
2629 (chosen (and (null (widget-get widget :choice))
|
|
2630 (widget-apply type :match value)))
|
|
2631 child button)
|
|
2632 (insert (widget-get widget :entry-format))
|
|
2633 (goto-char from)
|
|
2634 ;; Parse % escapes in format.
|
|
2635 (while (re-search-forward "%\\([bv%]\\)" nil t)
|
|
2636 (let ((escape (aref (match-string 1) 0)))
|
|
2637 (replace-match "" t t)
|
|
2638 (cond ((eq escape ?%)
|
|
2639 (insert "%"))
|
|
2640 ((eq escape ?b)
|
|
2641 (setq button (apply 'widget-create-child-and-convert
|
|
2642 widget 'radio-button
|
|
2643 :value (not (null chosen))
|
|
2644 button-args)))
|
|
2645 ((eq escape ?v)
|
|
2646 (setq child (if chosen
|
|
2647 (widget-create-child-value
|
|
2648 widget type value)
|
|
2649 (widget-create-child widget type)))
|
|
2650 (unless chosen
|
|
2651 (widget-apply child :deactivate)))
|
|
2652 (t
|
227
|
2653 (signal 'error (list "Unknown escape" escape))))))
|
209
|
2654 ;; Update properties.
|
|
2655 (when chosen
|
|
2656 (widget-put widget :choice type))
|
|
2657 (when button
|
|
2658 (widget-put child :button button)
|
|
2659 (widget-put widget :buttons (nconc buttons (list button))))
|
|
2660 (when child
|
|
2661 (widget-put widget :children (nconc children (list child))))
|
|
2662 child)))
|
|
2663
|
|
2664 (defun widget-radio-value-get (widget)
|
|
2665 ;; Get value of the child widget.
|
|
2666 (let ((chosen (widget-radio-chosen widget)))
|
|
2667 (and chosen (widget-value chosen))))
|
|
2668
|
|
2669 (defun widget-radio-chosen (widget)
|
|
2670 "Return the widget representing the chosen radio button."
|
|
2671 (let ((children (widget-get widget :children))
|
|
2672 current found)
|
|
2673 (while children
|
|
2674 (setq current (car children)
|
|
2675 children (cdr children))
|
|
2676 (let* ((button (widget-get current :button))
|
|
2677 (value (widget-apply button :value-get)))
|
|
2678 (when value
|
|
2679 (setq found current
|
|
2680 children nil))))
|
|
2681 found))
|
|
2682
|
|
2683 (defun widget-radio-value-inline (widget)
|
|
2684 ;; Get value of the child widget.
|
|
2685 (let ((children (widget-get widget :children))
|
|
2686 current found)
|
|
2687 (while children
|
|
2688 (setq current (car children)
|
|
2689 children (cdr children))
|
|
2690 (let* ((button (widget-get current :button))
|
|
2691 (value (widget-apply button :value-get)))
|
|
2692 (when value
|
|
2693 (setq found (widget-apply current :value-inline)
|
|
2694 children nil))))
|
|
2695 found))
|
|
2696
|
|
2697 (defun widget-radio-value-set (widget value)
|
|
2698 ;; We can't just delete and recreate a radio widget, since children
|
|
2699 ;; can be added after the original creation and won't be recreated
|
|
2700 ;; by `:create'.
|
|
2701 (let ((children (widget-get widget :children))
|
|
2702 current found)
|
|
2703 (while children
|
|
2704 (setq current (car children)
|
|
2705 children (cdr children))
|
|
2706 (let* ((button (widget-get current :button))
|
|
2707 (match (and (not found)
|
|
2708 (widget-apply current :match value))))
|
|
2709 (widget-value-set button match)
|
|
2710 (if match
|
|
2711 (progn
|
|
2712 (widget-value-set current value)
|
|
2713 (widget-apply current :activate))
|
|
2714 (widget-apply current :deactivate))
|
|
2715 (setq found (or found match))))))
|
|
2716
|
|
2717 (defun widget-radio-validate (widget)
|
|
2718 ;; Valid if we have made a valid choice.
|
|
2719 (let ((children (widget-get widget :children))
|
|
2720 current found button)
|
|
2721 (while (and children (not found))
|
|
2722 (setq current (car children)
|
|
2723 children (cdr children)
|
|
2724 button (widget-get current :button)
|
|
2725 found (widget-apply button :value-get)))
|
|
2726 (if found
|
|
2727 (widget-apply current :validate)
|
|
2728 widget)))
|
|
2729
|
|
2730 (defun widget-radio-action (widget child event)
|
|
2731 ;; Check if a radio button was pressed.
|
|
2732 (let ((children (widget-get widget :children))
|
|
2733 (buttons (widget-get widget :buttons))
|
|
2734 current)
|
|
2735 (when (memq child buttons)
|
|
2736 (while children
|
|
2737 (setq current (car children)
|
|
2738 children (cdr children))
|
|
2739 (let* ((button (widget-get current :button)))
|
|
2740 (cond ((eq child button)
|
|
2741 (widget-value-set button t)
|
|
2742 (widget-apply current :activate))
|
|
2743 ((widget-value button)
|
|
2744 (widget-value-set button nil)
|
|
2745 (widget-apply current :deactivate)))))))
|
|
2746 ;; Pass notification to parent.
|
|
2747 (widget-apply widget :notify child event))
|
|
2748
|
|
2749 ;;; The `insert-button' Widget.
|
|
2750
|
|
2751 (define-widget 'insert-button 'push-button
|
|
2752 "An insert button for the `editable-list' widget."
|
|
2753 :tag "INS"
|
|
2754 :help-echo "Insert a new item into the list at this position"
|
|
2755 :action 'widget-insert-button-action)
|
|
2756
|
|
2757 (defun widget-insert-button-action (widget &optional event)
|
|
2758 ;; Ask the parent to insert a new item.
|
|
2759 (widget-apply (widget-get widget :parent)
|
|
2760 :insert-before (widget-get widget :widget)))
|
|
2761
|
|
2762 ;;; The `delete-button' Widget.
|
|
2763
|
|
2764 (define-widget 'delete-button 'push-button
|
|
2765 "A delete button for the `editable-list' widget."
|
|
2766 :tag "DEL"
|
|
2767 :help-echo "Delete this item from the list"
|
|
2768 :action 'widget-delete-button-action)
|
|
2769
|
|
2770 (defun widget-delete-button-action (widget &optional event)
|
|
2771 ;; Ask the parent to insert a new item.
|
|
2772 (widget-apply (widget-get widget :parent)
|
|
2773 :delete-at (widget-get widget :widget)))
|
|
2774
|
|
2775 ;;; The `editable-list' Widget.
|
|
2776
|
|
2777 (defcustom widget-editable-list-gui nil
|
|
2778 "If non nil, use GUI push-buttons in editable list when available."
|
|
2779 :type 'boolean
|
|
2780 :group 'widgets)
|
|
2781
|
|
2782 (define-widget 'editable-list 'default
|
|
2783 "A variable list of widgets of the same type."
|
|
2784 :convert-widget 'widget-types-convert-widget
|
|
2785 :offset 12
|
|
2786 :format "%v%i\n"
|
|
2787 :format-handler 'widget-editable-list-format-handler
|
|
2788 :entry-format "%i %d %v"
|
|
2789 :menu-tag "editable-list"
|
|
2790 :value-create 'widget-editable-list-value-create
|
|
2791 :value-delete 'widget-children-value-delete
|
|
2792 :value-get 'widget-editable-list-value-get
|
|
2793 :validate 'widget-children-validate
|
|
2794 :match 'widget-editable-list-match
|
|
2795 :match-inline 'widget-editable-list-match-inline
|
|
2796 :insert-before 'widget-editable-list-insert-before
|
|
2797 :delete-at 'widget-editable-list-delete-at)
|
|
2798
|
|
2799 (defun widget-editable-list-format-handler (widget escape)
|
|
2800 ;; We recognize the insert button.
|
|
2801 (let ((widget-push-button-gui widget-editable-list-gui))
|
|
2802 (cond ((eq escape ?i)
|
|
2803 (and (widget-get widget :indent)
|
|
2804 (insert-char ?\ (widget-get widget :indent)))
|
|
2805 (apply 'widget-create-child-and-convert
|
|
2806 widget 'insert-button
|
|
2807 (widget-get widget :append-button-args)))
|
|
2808 (t
|
|
2809 (widget-default-format-handler widget escape)))))
|
|
2810
|
|
2811 (defun widget-editable-list-value-create (widget)
|
|
2812 ;; Insert all values
|
|
2813 (let* ((value (widget-get widget :value))
|
|
2814 (type (nth 0 (widget-get widget :args)))
|
|
2815 (inlinep (widget-get type :inline))
|
|
2816 children)
|
|
2817 (widget-put widget :value-pos (copy-marker (point)))
|
|
2818 (set-marker-insertion-type (widget-get widget :value-pos) t)
|
|
2819 (while value
|
|
2820 (let ((answer (widget-match-inline type value)))
|
|
2821 (if answer
|
|
2822 (setq children (cons (widget-editable-list-entry-create
|
|
2823 widget
|
|
2824 (if inlinep
|
|
2825 (car answer)
|
|
2826 (car (car answer)))
|
|
2827 t)
|
|
2828 children)
|
|
2829 value (cdr answer))
|
|
2830 (setq value nil))))
|
|
2831 (widget-put widget :children (nreverse children))))
|
|
2832
|
|
2833 (defun widget-editable-list-value-get (widget)
|
|
2834 ;; Get value of the child widget.
|
|
2835 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
|
|
2836 (widget-get widget :children))))
|
|
2837
|
|
2838 (defun widget-editable-list-match (widget value)
|
|
2839 ;; Value must be a list and all the members must match the type.
|
|
2840 (and (listp value)
|
|
2841 (null (cdr (widget-editable-list-match-inline widget value)))))
|
|
2842
|
|
2843 (defun widget-editable-list-match-inline (widget value)
|
|
2844 (let ((type (nth 0 (widget-get widget :args)))
|
|
2845 (ok t)
|
|
2846 found)
|
|
2847 (while (and value ok)
|
|
2848 (let ((answer (widget-match-inline type value)))
|
|
2849 (if answer
|
|
2850 (setq found (append found (car answer))
|
|
2851 value (cdr answer))
|
|
2852 (setq ok nil))))
|
|
2853 (cons found value)))
|
|
2854
|
|
2855 (defun widget-editable-list-insert-before (widget before)
|
|
2856 ;; Insert a new child in the list of children.
|
|
2857 (save-excursion
|
|
2858 (let ((children (widget-get widget :children))
|
|
2859 (inhibit-read-only t)
|
|
2860 before-change-functions
|
|
2861 after-change-functions)
|
|
2862 (cond (before
|
|
2863 (goto-char (widget-get before :entry-from)))
|
|
2864 (t
|
|
2865 (goto-char (widget-get widget :value-pos))))
|
|
2866 (let ((child (widget-editable-list-entry-create
|
|
2867 widget nil nil)))
|
|
2868 (when (< (widget-get child :entry-from) (widget-get widget :from))
|
|
2869 (set-marker (widget-get widget :from)
|
|
2870 (widget-get child :entry-from)))
|
|
2871 (if (eq (car children) before)
|
|
2872 (widget-put widget :children (cons child children))
|
|
2873 (while (not (eq (car (cdr children)) before))
|
|
2874 (setq children (cdr children)))
|
|
2875 (setcdr children (cons child (cdr children)))))))
|
|
2876 (widget-setup)
|
|
2877 (widget-apply widget :notify widget))
|
|
2878
|
|
2879 (defun widget-editable-list-delete-at (widget child)
|
|
2880 ;; Delete child from list of children.
|
|
2881 (save-excursion
|
|
2882 (let ((buttons (copy-sequence (widget-get widget :buttons)))
|
|
2883 button
|
|
2884 (inhibit-read-only t)
|
|
2885 before-change-functions
|
|
2886 after-change-functions)
|
|
2887 (while buttons
|
|
2888 (setq button (car buttons)
|
|
2889 buttons (cdr buttons))
|
|
2890 (when (eq (widget-get button :widget) child)
|
|
2891 (widget-put widget
|
|
2892 :buttons (delq button (widget-get widget :buttons)))
|
|
2893 (widget-delete button))))
|
|
2894 (let ((entry-from (widget-get child :entry-from))
|
|
2895 (entry-to (widget-get child :entry-to))
|
|
2896 (inhibit-read-only t)
|
|
2897 before-change-functions
|
|
2898 after-change-functions)
|
|
2899 (widget-delete child)
|
|
2900 (delete-region entry-from entry-to)
|
|
2901 (set-marker entry-from nil)
|
|
2902 (set-marker entry-to nil))
|
|
2903 (widget-put widget :children (delq child (widget-get widget :children))))
|
|
2904 (widget-setup)
|
|
2905 (widget-apply widget :notify widget))
|
|
2906
|
|
2907 (defun widget-editable-list-entry-create (widget value conv)
|
|
2908 ;; Create a new entry to the list.
|
|
2909 (let ((type (nth 0 (widget-get widget :args)))
|
|
2910 (widget-push-button-gui widget-editable-list-gui)
|
|
2911 child delete insert)
|
|
2912 (widget-specify-insert
|
|
2913 (save-excursion
|
|
2914 (and (widget-get widget :indent)
|
|
2915 (insert-char ?\ (widget-get widget :indent)))
|
|
2916 (insert (widget-get widget :entry-format)))
|
|
2917 ;; Parse % escapes in format.
|
|
2918 (while (re-search-forward "%\\(.\\)" nil t)
|
|
2919 (let ((escape (aref (match-string 1) 0)))
|
|
2920 (replace-match "" t t)
|
|
2921 (cond ((eq escape ?%)
|
|
2922 (insert "%"))
|
|
2923 ((eq escape ?i)
|
|
2924 (setq insert (apply 'widget-create-child-and-convert
|
|
2925 widget 'insert-button
|
|
2926 (widget-get widget :insert-button-args))))
|
|
2927 ((eq escape ?d)
|
|
2928 (setq delete (apply 'widget-create-child-and-convert
|
|
2929 widget 'delete-button
|
|
2930 (widget-get widget :delete-button-args))))
|
|
2931 ((eq escape ?v)
|
|
2932 (if conv
|
|
2933 (setq child (widget-create-child-value
|
|
2934 widget type value))
|
280
|
2935 (setq child (widget-create-child-value
|
|
2936 widget type (widget-default-get type)))))
|
209
|
2937 (t
|
227
|
2938 (signal 'error (list "Unknown escape" escape))))))
|
209
|
2939 (widget-put widget
|
|
2940 :buttons (cons delete
|
|
2941 (cons insert
|
|
2942 (widget-get widget :buttons))))
|
|
2943 (let ((entry-from (copy-marker (point-min)))
|
|
2944 (entry-to (copy-marker (point-max))))
|
|
2945 (set-marker-insertion-type entry-from t)
|
|
2946 (set-marker-insertion-type entry-to nil)
|
|
2947 (widget-put child :entry-from entry-from)
|
|
2948 (widget-put child :entry-to entry-to)))
|
|
2949 (widget-put insert :widget child)
|
|
2950 (widget-put delete :widget child)
|
|
2951 child))
|
|
2952
|
|
2953 ;;; The `group' Widget.
|
|
2954
|
|
2955 (define-widget 'group 'default
|
|
2956 "A widget which group other widgets inside."
|
|
2957 :convert-widget 'widget-types-convert-widget
|
|
2958 :format "%v"
|
|
2959 :value-create 'widget-group-value-create
|
|
2960 :value-delete 'widget-children-value-delete
|
|
2961 :value-get 'widget-editable-list-value-get
|
280
|
2962 :default-get 'widget-group-default-get
|
209
|
2963 :validate 'widget-children-validate
|
|
2964 :match 'widget-group-match
|
|
2965 :match-inline 'widget-group-match-inline)
|
|
2966
|
|
2967 (defun widget-group-value-create (widget)
|
|
2968 ;; Create each component.
|
|
2969 (let ((args (widget-get widget :args))
|
|
2970 (value (widget-get widget :value))
|
|
2971 arg answer children)
|
|
2972 (while args
|
|
2973 (setq arg (car args)
|
|
2974 args (cdr args)
|
|
2975 answer (widget-match-inline arg value)
|
|
2976 value (cdr answer))
|
|
2977 (and (eq (preceding-char) ?\n)
|
|
2978 (widget-get widget :indent)
|
|
2979 (insert-char ?\ (widget-get widget :indent)))
|
|
2980 (push (cond ((null answer)
|
|
2981 (widget-create-child widget arg))
|
|
2982 ((widget-get arg :inline)
|
|
2983 (widget-create-child-value widget arg (car answer)))
|
|
2984 (t
|
|
2985 (widget-create-child-value widget arg (car (car answer)))))
|
|
2986 children))
|
|
2987 (widget-put widget :children (nreverse children))))
|
|
2988
|
280
|
2989 (defun widget-group-default-get (widget)
|
|
2990 ;; Get the default of the components.
|
|
2991 (mapcar 'widget-default-get (widget-get widget :args)))
|
|
2992
|
209
|
2993 (defun widget-group-match (widget values)
|
|
2994 ;; Match if the components match.
|
|
2995 (and (listp values)
|
|
2996 (let ((match (widget-group-match-inline widget values)))
|
|
2997 (and match (null (cdr match))))))
|
|
2998
|
|
2999 (defun widget-group-match-inline (widget vals)
|
|
3000 ;; Match if the components match.
|
|
3001 (let ((args (widget-get widget :args))
|
|
3002 argument answer found)
|
|
3003 (while args
|
|
3004 (setq argument (car args)
|
|
3005 args (cdr args)
|
|
3006 answer (widget-match-inline argument vals))
|
|
3007 (if answer
|
|
3008 (setq vals (cdr answer)
|
|
3009 found (append found (car answer)))
|
|
3010 (setq vals nil
|
|
3011 args nil)))
|
|
3012 (if answer
|
|
3013 (cons found vals)
|
|
3014 nil)))
|
|
3015
|
|
3016 ;;; The `visibility' Widget.
|
|
3017
|
|
3018 (define-widget 'visibility 'item
|
|
3019 "An indicator and manipulator for hidden items."
|
|
3020 :format "%[%v%]"
|
|
3021 :button-prefix ""
|
|
3022 :button-suffix ""
|
|
3023 :on "Hide"
|
|
3024 :off "Show"
|
|
3025 :value-create 'widget-visibility-value-create
|
|
3026 :action 'widget-toggle-action
|
|
3027 :match (lambda (widget value) t))
|
|
3028
|
|
3029 (defun widget-visibility-value-create (widget)
|
|
3030 ;; Insert text representing the `on' and `off' states.
|
|
3031 (let ((on (widget-get widget :on))
|
|
3032 (off (widget-get widget :off)))
|
|
3033 (if on
|
|
3034 (setq on (concat widget-push-button-prefix
|
|
3035 on
|
|
3036 widget-push-button-suffix))
|
|
3037 (setq on ""))
|
|
3038 (if off
|
|
3039 (setq off (concat widget-push-button-prefix
|
|
3040 off
|
|
3041 widget-push-button-suffix))
|
|
3042 (setq off ""))
|
|
3043 (if (widget-value widget)
|
|
3044 (widget-glyph-insert widget on '("down" "down-pushed"))
|
|
3045 (widget-glyph-insert widget off '("right" "right-pushed")))))
|
|
3046
|
|
3047 ;;; The `documentation-link' Widget.
|
|
3048 ;;
|
|
3049 ;; This is a helper widget for `documentation-string'.
|
|
3050
|
|
3051 (define-widget 'documentation-link 'link
|
|
3052 "Link type used in documentation strings."
|
|
3053 :tab-order -1
|
|
3054 :help-echo 'widget-documentation-link-echo-help
|
|
3055 :action 'widget-documentation-link-action)
|
|
3056
|
|
3057 (defun widget-documentation-link-echo-help (widget)
|
|
3058 "Tell what this link will describe."
|
|
3059 (concat "Describe the `" (widget-get widget :value) "' symbol."))
|
|
3060
|
|
3061 (defun widget-documentation-link-action (widget &optional event)
|
|
3062 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
|
|
3063 (let* ((string (widget-get widget :value))
|
|
3064 (symbol (intern string)))
|
|
3065 (if (and (fboundp symbol) (boundp symbol))
|
|
3066 ;; If there are two doc strings, give the user a way to pick one.
|
|
3067 (apropos (concat "\\`" (regexp-quote string) "\\'"))
|
|
3068 (if (fboundp symbol)
|
|
3069 (describe-function symbol)
|
|
3070 (describe-variable symbol)))))
|
|
3071
|
|
3072 (defcustom widget-documentation-links t
|
|
3073 "Add hyperlinks to documentation strings when non-nil."
|
|
3074 :type 'boolean
|
|
3075 :group 'widget-documentation)
|
|
3076
|
|
3077 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
|
|
3078 "Regexp for matching potential links in documentation strings.
|
|
3079 The first group should be the link itself."
|
|
3080 :type 'regexp
|
|
3081 :group 'widget-documentation)
|
|
3082
|
|
3083 (defcustom widget-documentation-link-p 'intern-soft
|
|
3084 "Predicate used to test if a string is useful as a link.
|
|
3085 The value should be a function. The function will be called one
|
|
3086 argument, a string, and should return non-nil if there should be a
|
|
3087 link for that string."
|
|
3088 :type 'function
|
|
3089 :options '(widget-documentation-link-p)
|
|
3090 :group 'widget-documentation)
|
|
3091
|
|
3092 (defcustom widget-documentation-link-type 'documentation-link
|
|
3093 "Widget type used for links in documentation strings."
|
|
3094 :type 'symbol
|
|
3095 :group 'widget-documentation)
|
|
3096
|
|
3097 (defun widget-documentation-link-add (widget from to)
|
|
3098 (widget-specify-doc widget from to)
|
|
3099 (when widget-documentation-links
|
|
3100 (let ((regexp widget-documentation-link-regexp)
|
|
3101 (predicate widget-documentation-link-p)
|
|
3102 (type widget-documentation-link-type)
|
|
3103 (buttons (widget-get widget :buttons)))
|
|
3104 (save-excursion
|
|
3105 (goto-char from)
|
|
3106 (while (re-search-forward regexp to t)
|
|
3107 (let ((name (match-string 1))
|
|
3108 (begin (match-beginning 1))
|
|
3109 (end (match-end 1)))
|
|
3110 (when (funcall predicate name)
|
|
3111 (push (widget-convert-button type begin end :value name)
|
|
3112 buttons)))))
|
|
3113 (widget-put widget :buttons buttons)))
|
|
3114 (let ((indent (widget-get widget :indent)))
|
|
3115 (when (and indent (not (zerop indent)))
|
|
3116 (save-excursion
|
|
3117 (save-restriction
|
|
3118 (narrow-to-region from to)
|
|
3119 (goto-char (point-min))
|
|
3120 (while (search-forward "\n" nil t)
|
|
3121 (insert-char ?\ indent)))))))
|
|
3122
|
|
3123 ;;; The `documentation-string' Widget.
|
|
3124
|
|
3125 (define-widget 'documentation-string 'item
|
|
3126 "A documentation string."
|
|
3127 :format "%v"
|
|
3128 :action 'widget-documentation-string-action
|
|
3129 :value-delete 'widget-children-value-delete
|
|
3130 :value-create 'widget-documentation-string-value-create)
|
|
3131
|
|
3132 (defun widget-documentation-string-value-create (widget)
|
|
3133 ;; Insert documentation string.
|
|
3134 (let ((doc (widget-value widget))
|
|
3135 (indent (widget-get widget :indent))
|
|
3136 (shown (widget-get (widget-get widget :parent) :documentation-shown))
|
|
3137 (start (point)))
|
|
3138 (if (string-match "\n" doc)
|
|
3139 (let ((before (substring doc 0 (match-beginning 0)))
|
|
3140 (after (substring doc (match-beginning 0)))
|
|
3141 buttons)
|
|
3142 (insert before " ")
|
|
3143 (widget-documentation-link-add widget start (point))
|
|
3144 (push (widget-create-child-and-convert
|
|
3145 widget 'visibility
|
|
3146 :help-echo (lambda (widget)
|
|
3147 (concat
|
|
3148 (if (widget-value widget)
|
|
3149 "Hide" "Show")
|
|
3150 " the rest of the documentation"))
|
|
3151 :off "More"
|
|
3152 :action 'widget-parent-action
|
|
3153 shown)
|
|
3154 buttons)
|
|
3155 (when shown
|
|
3156 (setq start (point))
|
|
3157 (when indent
|
|
3158 (insert-char ?\ indent))
|
|
3159 (insert after)
|
|
3160 (widget-documentation-link-add widget start (point)))
|
|
3161 (widget-put widget :buttons buttons))
|
|
3162 (insert doc)
|
|
3163 (widget-documentation-link-add widget start (point))))
|
|
3164 (insert "\n"))
|
|
3165
|
|
3166 (defun widget-documentation-string-action (widget &rest ignore)
|
|
3167 ;; Toggle documentation.
|
|
3168 (let ((parent (widget-get widget :parent)))
|
|
3169 (widget-put parent :documentation-shown
|
|
3170 (not (widget-get parent :documentation-shown))))
|
|
3171 ;; Redraw.
|
|
3172 (widget-value-set widget (widget-value widget)))
|
|
3173
|
|
3174 ;;; The Sexp Widgets.
|
|
3175
|
|
3176 (define-widget 'const 'item
|
|
3177 "An immutable sexp."
|
|
3178 :prompt-value 'widget-const-prompt-value
|
|
3179 :format "%t\n%d")
|
|
3180
|
|
3181 (defun widget-const-prompt-value (widget prompt value unbound)
|
|
3182 ;; Return the value of the const.
|
|
3183 (widget-value widget))
|
|
3184
|
|
3185 (define-widget 'function-item 'const
|
|
3186 "An immutable function name."
|
|
3187 :format "%v\n%h"
|
|
3188 :documentation-property (lambda (symbol)
|
|
3189 (condition-case nil
|
|
3190 (documentation symbol t)
|
|
3191 (error nil))))
|
|
3192
|
|
3193 (define-widget 'variable-item 'const
|
|
3194 "An immutable variable name."
|
|
3195 :format "%v\n%h"
|
|
3196 :documentation-property 'variable-documentation)
|
|
3197
|
|
3198 (defvar widget-string-prompt-value-history nil
|
|
3199 "History of input to `widget-string-prompt-value'.")
|
|
3200
|
|
3201 (define-widget 'string 'editable-field
|
|
3202 "A string"
|
|
3203 :tag "String"
|
|
3204 :format "%{%t%}: %v"
|
|
3205 :complete-function 'ispell-complete-word
|
|
3206 :prompt-history 'widget-string-prompt-value-history)
|
|
3207
|
|
3208 (define-widget 'regexp 'string
|
|
3209 "A regular expression."
|
|
3210 :match 'widget-regexp-match
|
|
3211 :validate 'widget-regexp-validate
|
|
3212 ;; Doesn't work well with terminating newline.
|
|
3213 ;; :value-face 'widget-single-line-field-face
|
|
3214 :tag "Regexp")
|
|
3215
|
|
3216 (defun widget-regexp-match (widget value)
|
|
3217 ;; Match valid regexps.
|
|
3218 (and (stringp value)
|
|
3219 (condition-case nil
|
|
3220 (prog1 t
|
|
3221 (string-match value ""))
|
|
3222 (error nil))))
|
|
3223
|
|
3224 (defun widget-regexp-validate (widget)
|
|
3225 "Check that the value of WIDGET is a valid regexp."
|
|
3226 (let ((value (widget-value widget)))
|
|
3227 (condition-case data
|
|
3228 (prog1 nil
|
|
3229 (string-match value ""))
|
|
3230 (error (widget-put widget :error (error-message-string data))
|
|
3231 widget))))
|
|
3232
|
|
3233 (define-widget 'file 'string
|
|
3234 "A file widget.
|
|
3235 It will read a file name from the minibuffer when invoked."
|
|
3236 :complete-function 'widget-file-complete
|
|
3237 :prompt-value 'widget-file-prompt-value
|
|
3238 :format "%{%t%}: %v"
|
|
3239 ;; Doesn't work well with terminating newline.
|
|
3240 ;; :value-face 'widget-single-line-field-face
|
|
3241 :tag "File")
|
|
3242
|
|
3243 (defun widget-file-complete ()
|
|
3244 "Perform completion on file name preceding point."
|
|
3245 (interactive)
|
|
3246 (let* ((end (point))
|
|
3247 (beg (save-excursion
|
|
3248 (skip-chars-backward "^ ")
|
|
3249 (point)))
|
|
3250 (pattern (buffer-substring beg end))
|
|
3251 (name-part (file-name-nondirectory pattern))
|
|
3252 (directory (file-name-directory pattern))
|
|
3253 (completion (file-name-completion name-part directory)))
|
|
3254 (cond ((eq completion t))
|
|
3255 ((null completion)
|
|
3256 (message "Can't find completion for \"%s\"" pattern)
|
|
3257 (ding))
|
|
3258 ((not (string= name-part completion))
|
|
3259 (delete-region beg end)
|
|
3260 (insert (expand-file-name completion directory)))
|
|
3261 (t
|
|
3262 (message "Making completion list...")
|
|
3263 (let ((list (file-name-all-completions name-part directory)))
|
|
3264 (setq list (sort list 'string<))
|
|
3265 (with-output-to-temp-buffer "*Completions*"
|
|
3266 (display-completion-list list)))
|
|
3267 (message "Making completion list...%s" "done")))))
|
|
3268
|
|
3269 (defun widget-file-prompt-value (widget prompt value unbound)
|
|
3270 ;; Read file from minibuffer.
|
|
3271 (abbreviate-file-name
|
|
3272 (if unbound
|
|
3273 (read-file-name prompt)
|
|
3274 (let ((prompt2 (format "%s (default %s) " prompt value))
|
|
3275 (dir (file-name-directory value))
|
|
3276 (file (file-name-nondirectory value))
|
|
3277 (must-match (widget-get widget :must-match)))
|
|
3278 (read-file-name prompt2 dir nil must-match file)))))
|
|
3279
|
|
3280 ;;;(defun widget-file-action (widget &optional event)
|
|
3281 ;;; ;; Read a file name from the minibuffer.
|
|
3282 ;;; (let* ((value (widget-value widget))
|
|
3283 ;;; (dir (file-name-directory value))
|
|
3284 ;;; (file (file-name-nondirectory value))
|
|
3285 ;;; (menu-tag (widget-apply widget :menu-tag-get))
|
|
3286 ;;; (must-match (widget-get widget :must-match))
|
|
3287 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
|
|
3288 ;;; dir nil must-match file)))
|
|
3289 ;;; (widget-value-set widget (abbreviate-file-name answer))
|
|
3290 ;;; (widget-setup)
|
|
3291 ;;; (widget-apply widget :notify widget event)))
|
|
3292
|
|
3293 (define-widget 'directory 'file
|
|
3294 "A directory widget.
|
|
3295 It will read a directory name from the minibuffer when invoked."
|
|
3296 :tag "Directory")
|
|
3297
|
|
3298 (defvar widget-symbol-prompt-value-history nil
|
|
3299 "History of input to `widget-symbol-prompt-value'.")
|
|
3300
|
|
3301 (define-widget 'symbol 'editable-field
|
|
3302 "A lisp symbol."
|
|
3303 :value nil
|
|
3304 :tag "Symbol"
|
|
3305 :format "%{%t%}: %v"
|
|
3306 :match (lambda (widget value) (symbolp value))
|
|
3307 :complete-function 'lisp-complete-symbol
|
|
3308 :prompt-internal 'widget-symbol-prompt-internal
|
|
3309 :prompt-match 'symbolp
|
|
3310 :prompt-history 'widget-symbol-prompt-value-history
|
|
3311 :value-to-internal (lambda (widget value)
|
|
3312 (if (symbolp value)
|
|
3313 (symbol-name value)
|
|
3314 value))
|
|
3315 :value-to-external (lambda (widget value)
|
|
3316 (if (stringp value)
|
|
3317 (intern value)
|
|
3318 value)))
|
|
3319
|
|
3320 (defun widget-symbol-prompt-internal (widget prompt initial history)
|
|
3321 ;; Read file from minibuffer.
|
|
3322 (let ((answer (completing-read prompt obarray
|
|
3323 (widget-get widget :prompt-match)
|
|
3324 nil initial history)))
|
|
3325 (if (and (stringp answer)
|
|
3326 (not (zerop (length answer))))
|
|
3327 answer
|
|
3328 (error "No value"))))
|
|
3329
|
|
3330 (defvar widget-function-prompt-value-history nil
|
|
3331 "History of input to `widget-function-prompt-value'.")
|
|
3332
|
|
3333 (define-widget 'function 'sexp
|
|
3334 "A lisp function."
|
|
3335 :complete-function 'lisp-complete-symbol
|
|
3336 :prompt-value 'widget-field-prompt-value
|
|
3337 :prompt-internal 'widget-symbol-prompt-internal
|
|
3338 :prompt-match 'fboundp
|
|
3339 :prompt-history 'widget-function-prompt-value-history
|
|
3340 :action 'widget-field-action
|
|
3341 :tag "Function")
|
|
3342
|
|
3343 (defvar widget-variable-prompt-value-history nil
|
|
3344 "History of input to `widget-variable-prompt-value'.")
|
|
3345
|
|
3346 (define-widget 'variable 'symbol
|
|
3347 ;; Should complete on variables.
|
|
3348 "A lisp variable."
|
|
3349 :prompt-match 'boundp
|
|
3350 :prompt-history 'widget-variable-prompt-value-history
|
|
3351 :tag "Variable")
|
|
3352
|
|
3353 ;; This part issues a warning when compiling without Mule. Is there a
|
|
3354 ;; way of shutting it up?
|
|
3355 ;;
|
|
3356 ;; OK, I'll simply comment the whole thing out, until someone decides
|
|
3357 ;; to do something with it.
|
|
3358 ;(defvar widget-coding-system-prompt-value-history nil
|
|
3359 ; "History of input to `widget-coding-system-prompt-value'.")
|
|
3360
|
|
3361 ;(define-widget 'coding-system 'symbol
|
|
3362 ; "A MULE coding-system."
|
|
3363 ; :format "%{%t%}: %v"
|
|
3364 ; :tag "Coding system"
|
|
3365 ; :prompt-history 'widget-coding-system-prompt-value-history
|
|
3366 ; :prompt-value 'widget-coding-system-prompt-value
|
|
3367 ; :action 'widget-coding-system-action)
|
|
3368
|
|
3369 ;(defun widget-coding-system-prompt-value (widget prompt value unbound)
|
|
3370 ; ;; Read coding-system from minibuffer.
|
|
3371 ; (intern
|
|
3372 ; (completing-read (format "%s (default %s) " prompt value)
|
|
3373 ; (mapcar (lambda (sym)
|
|
3374 ; (list (symbol-name sym)))
|
|
3375 ; (coding-system-list)))))
|
|
3376
|
|
3377 ;(defun widget-coding-system-action (widget &optional event)
|
|
3378 ; ;; Read a file name from the minibuffer.
|
|
3379 ; (let ((answer
|
|
3380 ; (widget-coding-system-prompt-value
|
|
3381 ; widget
|
|
3382 ; (widget-apply widget :menu-tag-get)
|
|
3383 ; (widget-value widget)
|
|
3384 ; t)))
|
|
3385 ; (widget-value-set widget answer)
|
|
3386 ; (widget-apply widget :notify widget event)
|
|
3387 ; (widget-setup)))
|
|
3388
|
|
3389 (define-widget 'sexp 'editable-field
|
|
3390 "An arbitrary lisp expression."
|
|
3391 :tag "Lisp expression"
|
|
3392 :format "%{%t%}: %v"
|
|
3393 :value nil
|
|
3394 :validate 'widget-sexp-validate
|
|
3395 :match (lambda (widget value) t)
|
|
3396 :value-to-internal 'widget-sexp-value-to-internal
|
|
3397 :value-to-external (lambda (widget value) (read value))
|
|
3398 :prompt-history 'widget-sexp-prompt-value-history
|
|
3399 :prompt-value 'widget-sexp-prompt-value)
|
|
3400
|
|
3401 (defun widget-sexp-value-to-internal (widget value)
|
219
|
3402 ;; Use cl-prettyprint for printer representation.
|
209
|
3403 (let ((pp (if (symbolp value)
|
|
3404 (prin1-to-string value)
|
219
|
3405 (widget-prettyprint-to-string value))))
|
221
|
3406 (if (> (length pp) 40)
|
209
|
3407 (concat "\n" pp)
|
|
3408 pp)))
|
|
3409
|
|
3410 (defun widget-sexp-validate (widget)
|
|
3411 ;; Valid if we can read the string and there is no junk left after it.
|
|
3412 (save-excursion
|
|
3413 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
|
|
3414 (erase-buffer)
|
|
3415 (insert (widget-apply widget :value-get))
|
|
3416 (goto-char (point-min))
|
|
3417 (condition-case data
|
|
3418 (let ((value (read buffer)))
|
|
3419 (if (eobp)
|
|
3420 (if (widget-apply widget :match value)
|
|
3421 nil
|
|
3422 (widget-put widget :error (widget-get widget :type-error))
|
|
3423 widget)
|
|
3424 (widget-put widget
|
|
3425 :error (format "Junk at end of expression: %s"
|
|
3426 (buffer-substring (point)
|
|
3427 (point-max))))
|
|
3428 widget))
|
|
3429 (error (widget-put widget :error (error-message-string data))
|
|
3430 widget)))))
|
|
3431
|
|
3432 (defvar widget-sexp-prompt-value-history nil
|
|
3433 "History of input to `widget-sexp-prompt-value'.")
|
|
3434
|
|
3435 (defun widget-sexp-prompt-value (widget prompt value unbound)
|
|
3436 ;; Read an arbitrary sexp.
|
|
3437 (let ((found (read-string prompt
|
|
3438 (if unbound nil (cons (prin1-to-string value) 0))
|
|
3439 (widget-get widget :prompt-history))))
|
|
3440 (save-excursion
|
|
3441 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
|
|
3442 (erase-buffer)
|
|
3443 (insert found)
|
|
3444 (goto-char (point-min))
|
|
3445 (let ((answer (read buffer)))
|
|
3446 (unless (eobp)
|
227
|
3447 (signal 'error
|
|
3448 (list "Junk at end of expression"
|
|
3449 (buffer-substring (point) (point-max)))))
|
209
|
3450 answer)))))
|
|
3451
|
|
3452 (define-widget 'restricted-sexp 'sexp
|
|
3453 "A Lisp expression restricted to values that match.
|
|
3454 To use this type, you must define :match or :match-alternatives."
|
|
3455 :type-error "The specified value is not valid"
|
|
3456 :match 'widget-restricted-sexp-match
|
|
3457 :value-to-internal (lambda (widget value)
|
|
3458 (if (widget-apply widget :match value)
|
|
3459 (prin1-to-string value)
|
|
3460 value)))
|
|
3461
|
|
3462 (defun widget-restricted-sexp-match (widget value)
|
|
3463 (let ((alternatives (widget-get widget :match-alternatives))
|
|
3464 matched)
|
|
3465 (while (and alternatives (not matched))
|
|
3466 (if (cond ((functionp (car alternatives))
|
|
3467 (funcall (car alternatives) value))
|
|
3468 ((and (consp (car alternatives))
|
|
3469 (eq (car (car alternatives)) 'quote))
|
|
3470 (eq value (nth 1 (car alternatives)))))
|
|
3471 (setq matched t))
|
|
3472 (setq alternatives (cdr alternatives)))
|
|
3473 matched))
|
|
3474
|
|
3475 (define-widget 'integer 'restricted-sexp
|
|
3476 "An integer."
|
|
3477 :tag "Integer"
|
|
3478 :value 0
|
|
3479 :type-error "This field should contain an integer"
|
|
3480 :match-alternatives '(integerp))
|
|
3481
|
|
3482 (define-widget 'number 'restricted-sexp
|
|
3483 "A floating point number."
|
|
3484 :tag "Number"
|
|
3485 :value 0.0
|
|
3486 :type-error "This field should contain a number"
|
|
3487 :match-alternatives '(numberp))
|
|
3488
|
|
3489 (define-widget 'character 'editable-field
|
|
3490 "A character."
|
|
3491 :tag "Character"
|
|
3492 :value ?\0
|
|
3493 :format "%{%t%}: %v"
|
|
3494 :valid-regexp "\\`[\0-\377]\\'"
|
|
3495 :error "This field should contain a single character"
|
|
3496 :value-to-internal (lambda (widget value)
|
|
3497 (if (stringp value)
|
|
3498 value
|
|
3499 (char-to-string value)))
|
|
3500 :value-to-external (lambda (widget value)
|
|
3501 (if (stringp value)
|
|
3502 (aref value 0)
|
|
3503 value))
|
|
3504 :match (lambda (widget value)
|
|
3505 (characterp value)))
|
|
3506
|
|
3507 (define-widget 'list 'group
|
|
3508 "A lisp list."
|
|
3509 :tag "List"
|
|
3510 :format "%{%t%}:\n%v")
|
|
3511
|
|
3512 (define-widget 'vector 'group
|
|
3513 "A lisp vector."
|
|
3514 :tag "Vector"
|
|
3515 :format "%{%t%}:\n%v"
|
|
3516 :match 'widget-vector-match
|
|
3517 :value-to-internal (lambda (widget value) (append value nil))
|
|
3518 :value-to-external (lambda (widget value) (vconcat value)))
|
|
3519
|
|
3520 (defun widget-vector-match (widget value)
|
|
3521 (and (vectorp value)
|
|
3522 (widget-group-match widget
|
|
3523 (widget-apply widget :value-to-internal value))))
|
|
3524
|
|
3525 (define-widget 'cons 'group
|
|
3526 "A cons-cell."
|
|
3527 :tag "Cons-cell"
|
|
3528 :format "%{%t%}:\n%v"
|
|
3529 :match 'widget-cons-match
|
|
3530 :value-to-internal (lambda (widget value)
|
|
3531 (list (car value) (cdr value)))
|
|
3532 :value-to-external (lambda (widget value)
|
|
3533 (cons (car value) (cadr value))))
|
|
3534
|
|
3535 (defun widget-cons-match (widget value)
|
|
3536 (and (consp value)
|
|
3537 (widget-group-match widget
|
|
3538 (widget-apply widget :value-to-internal value))))
|
|
3539
|
|
3540 (define-widget 'choice 'menu-choice
|
|
3541 "A union of several sexp types."
|
|
3542 :tag "Choice"
|
|
3543 :format "%{%t%}: %[Value Menu%] %v"
|
|
3544 :button-prefix 'widget-push-button-prefix
|
|
3545 :button-suffix 'widget-push-button-suffix
|
|
3546 :prompt-value 'widget-choice-prompt-value)
|
|
3547
|
|
3548 (defun widget-choice-prompt-value (widget prompt value unbound)
|
|
3549 "Make a choice."
|
|
3550 (let ((args (widget-get widget :args))
|
|
3551 (completion-ignore-case (widget-get widget :case-fold))
|
|
3552 current choices old)
|
|
3553 ;; Find the first arg that match VALUE.
|
|
3554 (let ((look args))
|
|
3555 (while look
|
|
3556 (if (widget-apply (car look) :match value)
|
|
3557 (setq old (car look)
|
|
3558 look nil)
|
|
3559 (setq look (cdr look)))))
|
|
3560 ;; Find new choice.
|
|
3561 (setq current
|
|
3562 (cond ((= (length args) 0)
|
|
3563 nil)
|
|
3564 ((= (length args) 1)
|
|
3565 (nth 0 args))
|
|
3566 ((and (= (length args) 2)
|
|
3567 (memq old args))
|
|
3568 (if (eq old (nth 0 args))
|
|
3569 (nth 1 args)
|
|
3570 (nth 0 args)))
|
|
3571 (t
|
|
3572 (while args
|
|
3573 (setq current (car args)
|
|
3574 args (cdr args))
|
|
3575 (setq choices
|
|
3576 (cons (cons (widget-apply current :menu-tag-get)
|
|
3577 current)
|
|
3578 choices)))
|
|
3579 (let ((val (completing-read prompt choices nil t)))
|
|
3580 (if (stringp val)
|
|
3581 (let ((try (try-completion val choices)))
|
|
3582 (when (stringp try)
|
|
3583 (setq val try))
|
|
3584 (cdr (assoc val choices)))
|
|
3585 nil)))))
|
|
3586 (if current
|
|
3587 (widget-prompt-value current prompt nil t)
|
|
3588 value)))
|
|
3589
|
|
3590 (define-widget 'radio 'radio-button-choice
|
|
3591 "A union of several sexp types."
|
|
3592 :tag "Choice"
|
|
3593 :format "%{%t%}:\n%v"
|
|
3594 :prompt-value 'widget-choice-prompt-value)
|
|
3595
|
|
3596 (define-widget 'repeat 'editable-list
|
|
3597 "A variable length homogeneous list."
|
|
3598 :tag "Repeat"
|
|
3599 :format "%{%t%}:\n%v%i\n")
|
|
3600
|
|
3601 (define-widget 'set 'checklist
|
|
3602 "A list of members from a fixed set."
|
|
3603 :tag "Set"
|
|
3604 :format "%{%t%}:\n%v")
|
|
3605
|
|
3606 (define-widget 'boolean 'toggle
|
|
3607 "To be nil or non-nil, that is the question."
|
|
3608 :tag "Boolean"
|
|
3609 :prompt-value 'widget-boolean-prompt-value
|
|
3610 :button-prefix 'widget-push-button-prefix
|
|
3611 :button-suffix 'widget-push-button-suffix
|
|
3612 :format "%{%t%}: %[Toggle%] %v\n"
|
|
3613 :on "on (non-nil)"
|
|
3614 :off "off (nil)")
|
|
3615
|
|
3616 (defun widget-boolean-prompt-value (widget prompt value unbound)
|
|
3617 ;; Toggle a boolean.
|
|
3618 (y-or-n-p prompt))
|
|
3619
|
|
3620 ;;; The `color' Widget.
|
|
3621
|
|
3622 (define-widget 'color 'editable-field
|
|
3623 "Choose a color name (with sample)."
|
|
3624 :format "%[%t%]: %v (%{sample%})\n"
|
|
3625 :size 10
|
|
3626 :tag "Color"
|
|
3627 :value "black"
|
|
3628 :complete 'widget-color-complete
|
|
3629 :sample-face-get 'widget-color-sample-face-get
|
|
3630 :notify 'widget-color-notify
|
|
3631 :action 'widget-color-action)
|
|
3632
|
|
3633 (defun widget-color-complete (widget)
|
|
3634 "Complete the color in WIDGET."
|
|
3635 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
|
|
3636 (point)))
|
|
3637 (list (read-color-completion-table))
|
|
3638 (completion (try-completion prefix list)))
|
|
3639 (cond ((eq completion t)
|
|
3640 (message "Exact match"))
|
|
3641 ((null completion)
|
|
3642 (error "Can't find completion for \"%s\"" prefix))
|
|
3643 ((not (string-equal prefix completion))
|
|
3644 (insert (substring completion (length prefix))))
|
|
3645 (t
|
|
3646 (message "Making completion list...")
|
|
3647 (let ((list (all-completions prefix list nil)))
|
|
3648 (with-output-to-temp-buffer "*Completions*"
|
|
3649 (display-completion-list list)))
|
|
3650 (message "Making completion list...done")))))
|
|
3651
|
|
3652 (defun widget-color-sample-face-get (widget)
|
|
3653 (or (widget-get widget :sample-face)
|
|
3654 (let ((color (widget-value widget))
|
|
3655 (face (make-face (gensym "sample-face-") nil t)))
|
|
3656 ;; Use the face object, not its name, to prevent lossage if gc
|
|
3657 ;; happens before applying the face.
|
|
3658 (widget-put widget :sample-face face)
|
|
3659 (and color
|
|
3660 (not (equal color ""))
|
|
3661 (valid-color-name-p color)
|
|
3662 (set-face-foreground face color))
|
|
3663 face)))
|
|
3664
|
|
3665 (defvar widget-color-history nil
|
|
3666 "History of entered colors.")
|
|
3667
|
|
3668 (defun widget-color-action (widget &optional event)
|
|
3669 ;; Prompt for a color.
|
|
3670 (let* ((tag (widget-apply widget :menu-tag-get))
|
|
3671 (answer (read-color (concat tag ": "))))
|
|
3672 (unless (zerop (length answer))
|
|
3673 (widget-value-set widget answer)
|
|
3674 (widget-setup)
|
|
3675 (widget-apply widget :notify widget event))))
|
|
3676
|
|
3677 (defun widget-color-notify (widget child &optional event)
|
|
3678 "Update the sample, and notify the parent."
|
|
3679 (let* ((face (widget-apply widget :sample-face-get))
|
|
3680 (color (widget-value widget)))
|
|
3681 (if (valid-color-name-p color)
|
|
3682 (set-face-foreground face color)
|
|
3683 (remove-face-property face 'foreground)))
|
|
3684 (widget-default-notify widget child event))
|
|
3685
|
|
3686 ;; Is this a misnomer?
|
|
3687 (defun widget-at (pos)
|
|
3688 "The button or field at POS."
|
|
3689 (or (get-char-property pos 'button)
|
|
3690 (get-char-property pos 'field)))
|
|
3691
|
|
3692 (defun widget-echo-help (pos)
|
|
3693 "Display the help echo for widget at POS."
|
|
3694 (let* ((widget (widget-at pos))
|
|
3695 (help-echo (and widget (widget-get widget :help-echo))))
|
|
3696 (and (functionp help-echo)
|
|
3697 (setq help-echo (funcall help-echo widget)))
|
|
3698 (when (stringp help-echo)
|
|
3699 (display-message 'help-echo help-echo))))
|
|
3700
|
|
3701 ;;; The End:
|
|
3702
|
|
3703 (provide 'wid-edit)
|
|
3704
|
|
3705 ;; wid-edit.el ends here
|