;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
;;
-;; Copyright (C) 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
+;; Copyright (C) 1996, 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
;;
;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
;; Maintainer: FSF
:type 'face
:group 'widget-faces)
-(defface widget-field-face '((((class grayscale color)
+;; TTY gets special definitions here and in the next defface, because
+;; the gray colors defined for other displays cause black text on a black
+;; background, at least on light-background TTYs.
+(defface widget-field-face '((((type tty))
+ (:background "yellow3"))
+ (((class grayscale color)
(background light))
(:background "gray85"))
(((class grayscale color)
"Face used for editable fields."
:group 'widget-faces)
-(defface widget-single-line-field-face '((((class grayscale color)
+(defface widget-single-line-field-face '((((type tty))
+ (:background "green3"))
+ (((class grayscale color)
(background light))
(:background "gray85"))
(((class grayscale color)
`widget-menu-max-size', a popup menu will be used, otherwise the
minibuffer."
(cond ((and (< (length items) widget-menu-max-size)
- event (display-mouse-p))
+ event (display-popup-menus-p))
;; Mouse click.
(x-popup-menu event
(list title (cons "" items))))
;; that corresponds to it.
(save-window-excursion
(let ((buf (get-buffer " widget-choose")))
- (display-buffer buf)
+ (fit-window-to-buffer (display-buffer buf))
(let ((cursor-in-echo-area t)
keys
(char 0)
(widget-field-add-space
(insert-and-inherit " ")))
(setq to (point)))
- (let ((overlay (make-overlay from to nil
- nil (or (not widget-field-add-space)
- (widget-get widget :size)))))
- (widget-put widget :field-overlay overlay)
- ;;(overlay-put overlay 'detachable nil)
- (overlay-put overlay 'field widget)
- (overlay-put overlay 'keymap (widget-get widget :keymap))
- (overlay-put overlay 'face (or (widget-get widget :value-face)
- 'widget-field-face))
- (overlay-put overlay 'help-echo (widget-get widget :help-echo)))
+ (let ((keymap (widget-get widget :keymap))
+ (face (or (widget-get widget :value-face) 'widget-field-face))
+ (help-echo (widget-get widget :help-echo))
+ (rear-sticky
+ (or (not widget-field-add-space) (widget-get widget :size))))
+ (if (functionp help-echo)
+ (setq help-echo 'widget-mouse-help))
+ (when (= (char-before to) ?\n)
+ ;; When the last character in the field is a newline, we want to
+ ;; give it a `field' char-property of `boundary', which helps the
+ ;; C-n/C-p act more naturally when entering/leaving the field. We
+ ;; do this by making a small secondary overlay to contain just that
+ ;; one character.
+ (let ((overlay (make-overlay (1- to) to nil t nil)))
+ (overlay-put overlay 'field 'boundary)
+ ;; Use `local-map' here, not `keymap', so that normal editing
+ ;; works in the field when, say, Custom uses `suppress-keymap'.
+ (overlay-put overlay 'local-map keymap)
+ (overlay-put overlay 'face face)
+ (overlay-put overlay 'help-echo help-echo))
+ (setq to (1- to))
+ (setq rear-sticky t))
+ (let ((overlay (make-overlay from to nil nil rear-sticky)))
+ (widget-put widget :field-overlay overlay)
+ ;;(overlay-put overlay 'detachable nil)
+ (overlay-put overlay 'field widget)
+ (overlay-put overlay 'local-map keymap)
+ (overlay-put overlay 'face face)
+ (overlay-put overlay 'help-echo help-echo)))
(widget-specify-secret widget))
(defun widget-specify-secret (field)
(defun widget-specify-button (widget from to)
"Specify button for WIDGET between FROM and TO."
- (let ((overlay (make-overlay from to nil t nil)))
+ (let ((overlay (make-overlay from to nil t nil))
+ (help-echo (widget-get widget :help-echo)))
(widget-put widget :button-overlay overlay)
+ (if (functionp help-echo)
+ (setq help-echo 'widget-mouse-help))
(overlay-put overlay 'button widget)
(overlay-put overlay 'keymap (widget-get widget :keymap))
;; We want to avoid the face with image buttons.
(unless (widget-get widget :suppress-face)
(overlay-put overlay 'face (widget-apply widget :button-face-get))
(overlay-put overlay 'mouse-face widget-mouse-face))
- (overlay-put overlay 'help-echo (widget-get widget :help-echo))))
+ (overlay-put overlay 'help-echo help-echo)))
+
+(defun widget-mouse-help (window overlay point)
+ "Help-echo callback for widgets whose :help-echo is a function."
+ (with-current-buffer (overlay-buffer overlay)
+ (let* ((widget (widget-at (overlay-start overlay)))
+ (help-echo (if widget (widget-get widget :help-echo))))
+ (if (functionp help-echo)
+ (funcall help-echo widget)
+ help-echo))))
(defun widget-specify-sample (widget from to)
"Specify sample for WIDGET between FROM and TO."
Recommended as a parent keymap for modes using widgets.")
(defvar widget-global-map global-map
- "Keymap used for events the widget does not handle themselves.")
+ "Keymap used for events a widget does not handle itself.")
(make-variable-buffer-local 'widget-global-map)
(defvar widget-field-keymap
(let ((map (copy-keymap widget-keymap)))
- (define-key map [menu-bar] nil)
- (define-key map [tool-bar] nil)
(define-key map "\C-k" 'widget-kill-line)
(define-key map "\M-\t" 'widget-complete)
(define-key map "\C-m" 'widget-field-activate)
- (define-key map "\C-a" 'widget-beginning-of-line)
+ ;; Since the widget code uses a `field' property to identify fields,
+ ;; ordinary beginning-of-line does the right thing.
+ ;; (define-key map "\C-a" 'widget-beginning-of-line)
(define-key map "\C-e" 'widget-end-of-line)
- (set-keymap-parent map global-map)
map)
"Keymap used inside an editable field.")
(defvar widget-text-keymap
(let ((map (copy-keymap widget-keymap)))
- (define-key map [menu-bar] nil)
- (define-key map [tool-bar] nil)
- (define-key map "\C-a" 'widget-beginning-of-line)
+ ;; Since the widget code uses a `field' property to identify fields,
+ ;; ordinary beginning-of-line does the right thing.
+ ;; (define-key map "\C-a" 'widget-beginning-of-line)
(define-key map "\C-e" 'widget-end-of-line)
- (set-keymap-parent map global-map)
map)
"Keymap used inside a text field.")
(defun widget-field-activate (pos &optional event)
- "Invoke the ediable field at point."
+ "Invoke the editable field at point."
(interactive "@d")
- (let ((field (get-char-property pos 'field)))
+ (let ((field (widget-field-at pos)))
(if field
(widget-apply-action field event)
(call-interactively
"Invoke the button that the mouse is pointing at."
(interactive "@e")
(if (widget-event-point event)
- (save-excursion
- (mouse-set-point event)
- (let* ((pos (widget-event-point event))
- (button (get-char-property pos 'button)))
- (if button
- (let* ((overlay (widget-get button :button-overlay))
- (face (overlay-get overlay 'face))
- (mouse-face (overlay-get overlay 'mouse-face)))
- (unwind-protect
- (let ((track-mouse t))
- (save-excursion
- (when face ; avoid changing around image
- (overlay-put overlay
- 'face widget-button-pressed-face)
- (overlay-put overlay
- 'mouse-face widget-button-pressed-face))
- (unless (widget-apply button :mouse-down-action event)
- (while (not (widget-button-release-event-p event))
- (setq event (read-event)
- pos (widget-event-point event))
- (if (and pos
- (eq (get-char-property pos 'button)
- button))
- (when face
- (overlay-put overlay
- 'face
- widget-button-pressed-face)
- (overlay-put overlay
- 'mouse-face
- widget-button-pressed-face))
- (overlay-put overlay 'face face)
- (overlay-put overlay 'mouse-face mouse-face))))
- (when (and pos
- (eq (get-char-property pos 'button) button))
- (widget-apply-action button event))))
- (overlay-put overlay 'face face)
- (overlay-put overlay 'mouse-face mouse-face)))
- (let ((up t)
- command)
- ;; Find the global command to run, and check whether it
- ;; is bound to an up event.
+ (let* ((pos (widget-event-point event))
+ (button (get-char-property pos 'button)))
+ (if button
+ ;; Mouse click on a widget button. Do the following
+ ;; in a save-excursion so that the click on the button
+ ;; doesn't change point.
+ (save-selected-window
+ (save-excursion
+ (mouse-set-point event)
+ (let* ((overlay (widget-get button :button-overlay))
+ (face (overlay-get overlay 'face))
+ (mouse-face (overlay-get overlay 'mouse-face)))
+ (unwind-protect
+ ;; Read events, including mouse-movement events
+ ;; until we receive a release event. Highlight/
+ ;; unhighlight the button the mouse was initially
+ ;; on when we move over it.
+ (let ((track-mouse t))
+ (save-excursion
+ (when face ; avoid changing around image
+ (overlay-put overlay
+ 'face widget-button-pressed-face)
+ (overlay-put overlay
+ 'mouse-face widget-button-pressed-face))
+ (unless (widget-apply button :mouse-down-action event)
+ (while (not (widget-button-release-event-p event))
+ (setq event (read-event)
+ pos (widget-event-point event))
+ (if (and pos
+ (eq (get-char-property pos 'button)
+ button))
+ (when face
+ (overlay-put overlay
+ 'face
+ widget-button-pressed-face)
+ (overlay-put overlay
+ 'mouse-face
+ widget-button-pressed-face))
+ (overlay-put overlay 'face face)
+ (overlay-put overlay 'mouse-face mouse-face))))
+
+ ;; When mouse is released over the button, run
+ ;; its action function.
+ (when (and pos
+ (eq (get-char-property pos 'button) button))
+ (widget-apply-action button event))))
+ (overlay-put overlay 'face face)
+ (overlay-put overlay 'mouse-face mouse-face))))
+
+ (unless (pos-visible-in-window-p (widget-event-point event))
+ (mouse-set-point event)
+ (beginning-of-line)
+ (recenter)))
+
+ (let ((up t) command)
+ ;; Mouse click not on a widget button. Find the global
+ ;; command to run, and check whether it is bound to an
+ ;; up event.
+ (mouse-set-point event)
(if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
(cond ((setq command ;down event
(lookup-key widget-global-map [down-mouse-1]))
(setq event (read-event))))
(when command
(call-interactively command)))))
- (unless (pos-visible-in-window-p (widget-event-point event))
- (mouse-set-point event)
- (beginning-of-line)
- (recenter)))
(message "You clicked somewhere weird.")))
(defun widget-button-press (pos &optional event)
(defun widget-tabable-at (&optional pos)
"Return the tabable widget at POS, or nil.
POS defaults to the value of (point)."
- (unless pos
- (setq pos (point)))
- (let ((widget (or (get-char-property pos 'button)
- (get-char-property pos 'field))))
+ (let ((widget (widget-at pos)))
(if widget
(let ((order (widget-get widget :tab-order)))
(if order
(run-hooks 'widget-backward-hook)
(widget-move (- arg)))
-(defun widget-beginning-of-line ()
- "Go to beginning of field or beginning of line, whichever is first."
- (interactive)
- (let* ((field (widget-field-find (point)))
- (start (and field (widget-field-start field))))
- (goto-char (if start
- (max start (line-beginning-position))
- (line-beginning-position)))))
+;; Since the widget code uses a `field' property to identify fields,
+;; ordinary beginning-of-line does the right thing.
+(defalias 'widget-beginning-of-line 'beginning-of-line)
(defun widget-end-of-line ()
- "Go to end of field or end of line, whichever is first."
+ "Go to end of field or end of line, whichever is first.
+Trailing spaces at the end of padded fields are not considered part of
+the field."
(interactive)
- (let* ((field (widget-field-find (point)))
- (end (and field (widget-field-end field))))
- (goto-char (if end
- (min end (line-end-position))
- (line-end-position)))))
+ ;; Ordinary end-of-line does the right thing, because we're inside
+ ;; text with a `field' property.
+ (end-of-line)
+ (unless (eolp)
+ ;; ... except that we want to ignore trailing spaces in fields that
+ ;; aren't terminated by a newline, because they are used as padding,
+ ;; and ignored when extracting the entered value of the field.
+ (skip-chars-backward " " (field-beginning (1- (point))))))
(defun widget-kill-line ()
"Kill to end of field or end of line, whichever is first."
;; List of all editable fields in the buffer.
(make-variable-buffer-local 'widget-field-list)
+(defun widget-at (&optional pos)
+ "The button or field at POS (default, point)."
+ (or (get-char-property (or pos (point)) 'button)
+ (widget-field-at pos)))
+
(defun widget-setup ()
"Setup current buffer so editing string widgets works."
(let ((inhibit-read-only t)
;; The widget data before the change.
(make-variable-buffer-local 'widget-field-was)
+(defun widget-field-at (pos)
+ "Return the widget field at POS, or nil if none."
+ (let ((field (get-char-property (or pos (point)) 'field)))
+ (if (eq field 'boundary)
+ nil
+ field)))
+
(defun widget-field-buffer (widget)
"Return the start of WIDGET's editing field."
(let ((overlay (widget-get widget :field-overlay)))
- (and overlay (overlay-buffer overlay))))
+ (cond ((overlayp overlay)
+ (overlay-buffer overlay))
+ ((consp overlay)
+ (marker-buffer (car overlay))))))
(defun widget-field-start (widget)
"Return the start of WIDGET's editing field."
(let ((overlay (widget-get widget :field-overlay)))
- (and overlay (overlay-start overlay))))
+ (if (overlayp overlay)
+ (overlay-start overlay)
+ (car overlay))))
(defun widget-field-end (widget)
"Return the end of WIDGET's editing field."
(let ((overlay (widget-get widget :field-overlay)))
- ;; Don't subtract one if local-map works at the end of the overlay.
- (and overlay (if (or widget-field-add-space
- (null (widget-get widget :size)))
- (1- (overlay-end overlay))
- (overlay-end overlay)))))
+ ;; Don't subtract one if local-map works at the end of the overlay,
+ ;; or if a special `boundary' field has been added after the widget
+ ;; field.
+ (if (overlayp overlay)
+ (if (and (not (eq (get-char-property (overlay-end overlay)
+ 'field
+ (widget-field-buffer widget))
+ 'boundary))
+ (or widget-field-add-space
+ (null (widget-get widget :size))))
+ (1- (overlay-end overlay))
+ (overlay-end overlay))
+ (cdr overlay))))
(defun widget-field-find (pos)
"Return the field at POS.
(widget-apply from-field :notify from-field))))))
(defun widget-add-change ()
- (make-local-hook 'post-command-hook)
(remove-hook 'post-command-hook 'widget-add-change t)
- (make-local-hook 'before-change-functions)
(add-hook 'before-change-functions 'widget-before-change nil t)
- (make-local-hook 'after-change-functions)
(add-hook 'after-change-functions 'widget-after-change nil t))
(defun widget-after-change (from to old)
(defun widget-field-value-delete (widget)
"Remove the widget from the list of active editing fields."
(setq widget-field-list (delq widget widget-field-list))
+ (setq widget-field-new (delq widget widget-field-new))
;; These are nil if the :format string doesn't contain `%v'.
(let ((overlay (widget-get widget :field-overlay)))
- (when overlay
+ (when (overlayp overlay)
(delete-overlay overlay))))
(defun widget-field-value-get (widget)
;;; The `text' Widget.
(define-widget 'text 'editable-field
- :keymap widget-text-keymap
- "A multiline text area.")
+ "A multiline text area."
+ :keymap widget-text-keymap)
;;; The `menu-choice' Widget.
(defun widget-toggle-value-create (widget)
"Insert text representing the `on' and `off' states."
(if (widget-value widget)
- (widget-image-insert widget
- (widget-get widget :on)
- (widget-get widget :on-glyph))
+ (progn
+ (and (display-graphic-p)
+ (listp (widget-get widget :on-glyph))
+ (widget-put widget :on-glyph
+ (eval (widget-get widget :on-glyph))))
+ (widget-image-insert widget
+ (widget-get widget :on)
+ (widget-get widget :on-glyph)))
+ (and (display-graphic-p)
+ (listp (widget-get widget :off-glyph))
+ (widget-put widget :off-glyph
+ (eval (widget-get widget :off-glyph))))
(widget-image-insert widget
(widget-get widget :off)
(widget-get widget :off-glyph))))
;; We could probably do the same job as the images using single
;; space characters in a boxed face with a stretch specification to
;; make them square.
- :on-glyph (create-image (make-bool-vector 49 1)
- 'xbm t :width 7 :height 7
- :foreground "grey75" ; like default mode line
- :relief -3 :ascent 'center)
- :off "[ ]"
- :off-glyph (create-image (make-bool-vector 49 1)
+ :on-glyph '(create-image "\000\066\076\034\076\066\000"
'xbm t :width 7 :height 7
- :foreground "grey75"
- :relief 3 :ascent 'center)
+ :background "grey75" ; like default mode line
+ :foreground "black"
+ :relief -3
+ :ascent 'center)
+ :off "[ ]"
+ :off-glyph '(create-image (make-string 7 0)
+ 'xbm t :width 7 :height 7
+ :background "grey75"
+ :foreground "black"
+ :relief 3
+ :ascent 'center)
:help-echo "Toggle this item."
:action 'widget-checkbox-action)
:format "%v"
:offset 4
:entry-format "%b %v"
- :menu-tag "checklist"
:greedy nil
:value-create 'widget-checklist-value-create
:value-delete 'widget-children-value-delete
:offset 4
:format "%v"
:entry-format "%b %v"
- :menu-tag "radio"
:value-create 'widget-radio-value-create
:value-delete 'widget-children-value-delete
:value-get 'widget-radio-value-get
:format "%v%i\n"
:format-handler 'widget-editable-list-format-handler
:entry-format "%i %d %v"
- :menu-tag "editable-list"
:value-create 'widget-editable-list-value-create
:value-delete 'widget-children-value-delete
:value-get 'widget-editable-list-value-get
;;; (widget-setup)
;;; (widget-apply widget :notify widget event)))
+;; Fixme: use file-name-as-directory.
(define-widget 'directory 'file
"A directory widget.
It will read a directory name from the minibuffer when invoked."
:prompt-match 'fboundp
:prompt-history 'widget-function-prompt-value-history
:action 'widget-field-action
+ :match-alternatives '(functionp)
:validate (lambda (widget)
(unless (functionp (widget-value widget))
(widget-put widget :error (format "Invalid function: %S"
"History of input to `widget-variable-prompt-value'.")
(define-widget 'variable 'symbol
- ;; Should complete on variables.
"A Lisp variable."
:prompt-match 'boundp
:prompt-history 'widget-variable-prompt-value-history
(defun widget-plist-convert-widget (widget)
;; Handle `:options'.
(let* ((options (widget-get widget :options))
+ (widget-plist-value-type (widget-get widget :value-type))
(other `(editable-list :inline t
(group :inline t
,(widget-get widget :key-type)
- ,(widget-get widget :value-type))))
+ ,widget-plist-value-type)))
(args (if options
(list `(checklist :inline t
:greedy t
(defun widget-alist-convert-widget (widget)
;; Handle `:options'.
(let* ((options (widget-get widget :options))
+ (widget-alist-value-type (widget-get widget :value-type))
(other `(editable-list :inline t
(cons :format "%v"
,(widget-get widget :key-type)
- ,(widget-get widget :value-type))))
+ ,widget-alist-value-type)))
(args (if options
(list `(checklist :inline t
:greedy t
\f
;;; The `color' Widget.
+;; Fixme: match
(define-widget 'color 'editable-field
"Choose a color name (with sample)."
:format "%t: %v (%{sample%})\n"
(defun widget-color-sample-face-get (widget)
(let* ((value (condition-case nil
(widget-value widget)
- (error (widget-get widget :value))))
- (symbol (intern (concat "fg:" value))))
- (condition-case nil
- (facemenu-get-face symbol)
- (error 'default))))
+ (error (widget-get widget :value)))))
+ (if (color-defined-p value)
+ (list (cons 'foreground-color value))
+ 'default)))
(defun widget-color-action (widget &optional event)
- ;; Prompt for a color.
+ "Prompt for a color."
(let* ((tag (widget-apply widget :menu-tag-get))
(prompt (concat tag ": "))
(value (widget-value widget))
\f
;;; The Help Echo
-(defun widget-at (&optional pos)
- "The button or field at POS (default, point)."
- (unless pos
- (setq pos (point)))
- (or (get-char-property pos 'button)
- (get-char-property pos 'field)))
-
(defun widget-echo-help (pos)
- "Display the help echo for widget at POS."
+ "Display help-echo text for widget at POS."
(let* ((widget (widget-at pos))
(help-echo (and widget (widget-get widget :help-echo))))
- (if (or (stringp help-echo)
- (and (functionp help-echo)
- ;; Kluge: help-echo originally could be a function of
- ;; one arg -- the widget. It is more useful in Emacs
- ;; 21 to have it as a function usable also as a
- ;; help-echo property, when it can sort out its own
- ;; widget if necessary. Try both calling sequences
- ;; (rather than messing around to get the function's
- ;; arity).
- (stringp
- (setq help-echo
- (condition-case nil
- (funcall help-echo
- (selected-window) (current-buffer)
- (point))
- (error (funcall help-echo widget))))))
- (stringp (eval help-echo)))
+ (if (functionp help-echo)
+ (setq help-echo (funcall help-echo widget)))
+ (if (stringp help-echo)
(message "%s" help-echo))))
;;; The End: