X-Git-Url: http://git.hcoop.net/bpt/emacs.git/blobdiff_plain/08b3caa982199bd7939d9d6877203ada5d0083b5..d8fb8cce84b923a3289b69549e30958710ac3ebb:/lisp/minibuf-eldef.el diff --git a/lisp/minibuf-eldef.el b/lisp/minibuf-eldef.el index 8e0f750981..a36df99d4a 100644 --- a/lisp/minibuf-eldef.el +++ b/lisp/minibuf-eldef.el @@ -1,7 +1,6 @@ -;;; minibuf-eldef.el --- Only show defaults in prompts when applicable +;;; minibuf-eldef.el --- Only show defaults in prompts when applicable -*- lexical-binding: t -*- ;; -;; Copyright (C) 2000, 2001, 2002, 2003, 2004, -;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. +;; Copyright (C) 2000-2013 Free Software Foundation, Inc. ;; ;; Author: Miles Bader ;; Keywords: convenience @@ -34,16 +33,34 @@ ;;; Code: +(defvar minibuffer-eldef-shorten-default) + +(defun minibuffer-default--in-prompt-regexps () + `(("\\( (default\\(?: is\\)? \\(.*\\))\\):? \\'" + 1 ,(if minibuffer-eldef-shorten-default " [\\2]")) + ("\\( \\[.*\\]\\):? *\\'" 1))) + +(defcustom minibuffer-eldef-shorten-default nil + "If non-nil, shorten \"(default ...)\" to \"[...]\" in minibuffer prompts." + :set (lambda (symbol value) + (set-default symbol value) + (setq-default minibuffer-default-in-prompt-regexps + (minibuffer-default--in-prompt-regexps))) + :type 'boolean + :group 'minibuffer + :version "24.3") + (defvar minibuffer-default-in-prompt-regexps - '(("\\( (default\\>.*)\\):? \\'" . 1) ("\\( \\[.*\\]\\):? *\\'" . 1)) - "*A list of regexps matching the parts of minibuffer prompts showing defaults. + (minibuffer-default--in-prompt-regexps) + "A list of regexps matching the parts of minibuffer prompts showing defaults. When `minibuffer-electric-default-mode' is active, these regexps are used to identify the portions of prompts to elide. -Each entry is either a string, which should be a regexp matching the -default portion of the prompt, or a cons cell, who's car is a regexp -matching the default part of the prompt, and who's cdr indicates the -regexp subexpression that matched.") +Each entry is of the form (REGEXP MATCH-NUM &optional REWRITE), +where REGEXP should match the default part of the prompt, +MATCH-NUM is the subgroup that matched the actual default indicator, +and REWRITE, if present, is a string to pass to `replace-match' that +should be displayed in its place.") ;;; Internal variables @@ -80,21 +97,42 @@ The prompt and initial input should already have been inserted." (inhibit-point-motion-hooks t)) (save-excursion (save-restriction - ;; Narrow to only the prompt + ;; Narrow to only the prompt. (goto-char (point-min)) (narrow-to-region (point) (minibuffer-prompt-end)) - ;; See the prompt contains a default input indicator + ;; See if the prompt contains a default input indicator. (while regexps (setq match (pop regexps)) - (if (re-search-forward (if (stringp match) match (car match)) nil t) - (setq regexps nil) - (setq match nil))))) + (cond + ((not (re-search-forward (if (stringp match) match (car match)) + nil t)) + ;; No match yet, try the next rule. + (setq match nil)) + ((and (consp (cdr-safe match)) (nth 2 match)) + ;; Matched a replacement rule. + (let* ((inhibit-read-only t) + (buffer-undo-list t) + (submatch (nth 1 match)) + (replacement (nth 2 match)) + (props (text-properties-at (match-beginning submatch)))) + (replace-match replacement nil nil nil submatch) + (set-text-properties (match-beginning submatch) + (match-end submatch) + props) + ;; Replacement done, now keep trying with subsequent rules. + (setq match nil) + (goto-char (point-min)))) + ;; Matched a non-replacement (i.e. electric hide) rule, no need to + ;; keep trying. + (t (setq regexps nil)))))) (if (not match) - ;; Nope, so just make sure our post-command-hook isn't left around. + ;; No match for electric hiding, so just make sure our + ;; post-command-hook isn't left around. (remove-hook 'post-command-hook #'minibuf-eldef-update-minibuffer t) ;; Yup; set things up so we can frob the prompt as the state of ;; the input string changes. (setq match (if (consp match) (cdr match) 0)) + (setq match (if (consp match) (car match) match)) (setq minibuf-eldef-overlay (make-overlay (match-beginning match) (match-end match))) (setq minibuf-eldef-showing-default-in-prompt t) @@ -125,21 +163,19 @@ been set up by `minibuf-eldef-setup-minibuffer'." (overlay-put minibuf-eldef-overlay 'intangible t))))) -;;; Note this definition must be at the end of the file, because -;;; `define-minor-mode' actually calls the mode-function if the -;;; associated variable is non-nil, which requires that all needed -;;; functions be already defined. [This is arguably a bug in d-m-m] ;;;###autoload (define-minor-mode minibuffer-electric-default-mode "Toggle Minibuffer Electric Default mode. -When active, minibuffer prompts that show a default value only show the -default when it's applicable -- that is, when hitting RET would yield -the default value. If the user modifies the input such that hitting RET -would enter a non-default value, the prompt is modified to remove the -default indication. - -With prefix argument ARG, turn on if positive, otherwise off. -Returns non-nil if the new state is enabled." +With a prefix argument ARG, enable Minibuffer Electric Default +mode if ARG is positive, and disable it otherwise. If called +from Lisp, enable the mode if ARG is omitted or nil. + +Minibuffer Electric Default mode is a global minor mode. When +enabled, minibuffer prompts that show a default value only show +the default when it's applicable -- that is, when hitting RET +would yield the default value. If the user modifies the input +such that hitting RET would enter a non-default value, the prompt +is modified to remove the default indication." :global t :group 'minibuffer (if minibuffer-electric-default-mode @@ -156,5 +192,4 @@ Returns non-nil if the new state is enabled." (provide 'minibuf-eldef) -;; arch-tag: 7e421fae-c275-4729-b0da-7836af377d3d ;;; minibuf-eldef.el ends here