emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

defaults in minibuffer prompts


From: Miles Bader
Subject: defaults in minibuffer prompts
Date: Mon, 30 Oct 2000 18:30:31 +0900 (JST)

Check out this hack.  It adds a post-command-hook to the minibuffer that
removes the " (default ...)" portion of prompts when the input string is
not equal to the initial input string (restoring it if it ever becomes
equal).  [I got this idea from Mutt.]

I think this is a very nice UI improvement, as it makes it very clear
when hitting RET will yield the default, and incidentally makes it
easier to edit after certain prompts that have very long default strings
(which normally force your input at the extreme end of the line,
wrapping to the next line).

Unfortunately the implementation is a bit hacky, using regexp to find
the default-specifying portion of the string; I couldn't think of any
better way to do it, as callers to read-string &c. construct their own
prompts, including that portion.

-Miles

;; --------------------------------------------------------
;; Frob to only show defaults in prompts when they're applicable
;; Author: Miles Bader <address@hidden>
      
(defvar minibuf-initial-input nil)
(defvar minibuf-initial-input-length nil)
(defvar minibuf-showing-default-in-prompt 'unknown)
(defvar minibuf-default-prompt nil)
(defvar minibuf-no-default-prompt nil)

(defvar minibuf-default-in-prompt-patterns
  '(("(default .*) \\'" . nil)
    (" (default .*): \\'" . ": ")))

(defun setup-minibuf-frob-default-in-prompt ()
  (let ((prompt (field-string-no-properties (point-min)))
        (patterns minibuf-default-in-prompt-patterns)
        (match nil))
    ;; See if PROMPT contains a default input indicator
    (while patterns
      (setq match (pop patterns))
      (if (string-match (car match) prompt)
          (setq patterns nil)
        (setq match nil)))
    (if (not match)
        ;; Nope, so just make sure our post-command-hook isn't left around.
        (remove-hook 'post-command-hook #'minibuf-frob-default-in-prompt t)
      ;; Yup; set things up so we can frob the prompt as the state of
      ;; the input string changes.
      (let ((no-default-prompt (substring prompt 0 (match-beginning 0))))
        (when (cdr match)
          (setq no-default-prompt (concat no-default-prompt (cdr match))))
        (set (make-local-variable 'minibuf-default-prompt) prompt)
        (set (make-local-variable 'minibuf-no-default-prompt)
             no-default-prompt)
        (set (make-local-variable 'minibuf-initial-input)
             (field-string-no-properties (point-max)))
        (set (make-local-variable 'minibuf-showing-default-in-prompt) t)
        (set (make-local-variable 'minibuf-initial-input-length)
             (- (point-max) (length prompt) 1))
        (add-hook 'post-command-hook #'minibuf-frob-default-in-prompt
                  nil t)))))

;; post-command-hook to swap prompts when necessary
(defun minibuf-frob-default-in-prompt ()
  (unless (eq minibuf-showing-default-in-prompt
              (and (= (- (point-max) (field-beginning (point-max)))
                      minibuf-initial-input-length)
                   (string-equal (field-string-no-properties (point-max))
                                 minibuf-initial-input)))
    ;; swap state
    (let ((inhibit-read-only t))
      (save-excursion
        (setq minibuf-showing-default-in-prompt
              (not minibuf-showing-default-in-prompt))
        (goto-char (point-min))
        (insert-and-inherit
         (if minibuf-showing-default-in-prompt
             minibuf-default-prompt
           minibuf-no-default-prompt))
        (delete-region (point) (field-end))))))

(add-hook 'minibuffer-setup-hook 'setup-minibuf-frob-default-in-prompt)

;; end
-- 
Love is a snowmobile racing across the tundra.  Suddenly it flips over,
pinning you underneath.  At night the ice weasels come.  --Nietzsche



reply via email to

[Prev in Thread] Current Thread [Next in Thread]