emacs-devel
[Top][All Lists]
Advanced

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

Re: Default value in previous-matching-history-element


From: Juri Linkov
Subject: Re: Default value in previous-matching-history-element
Date: Sun, 30 May 2004 23:25:19 +0300
User-agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3.50 (gnu/linux)

Stephan Stahl <address@hidden> writes:
> Since i learned that M-n while doing completing-read and friends
> brings up the default value and lets you edit it, i used that feature
> very often.

One restriction of M-n feature is that it allows only one default element
accessible by M-n.  The following patch extends it to allow a list of
default values available for choosing by repeatedly typing M-n in the
minibuffer.

A list of default values might be useful, for example, for
`dired-guess-shell-command' which currently pushes all command guesses
temporarily into history.  If the user wants to repeat the last command
he must scroll all guesses by M-p before he reaches the first history
item.  This is inconvenient.  The second patch below places command guesses
to a list of default values accessible by M-n.

There are other places where a list of default value is useful.  For
example, `grep' provides only one default value based on the current
tag around point.  But it could provide additionally commands composed
with the text from the kill ring, from active region, etc.

Index: emacs/lisp/simple.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/simple.el,v
retrieving revision 1.645
diff -u -r1.645 simple.el
--- emacs/lisp/simple.el        28 May 2004 21:00:14 -0000      1.645
+++ emacs/lisp/simple.el        30 May 2004 18:37:31 -0000
@@ -1039,7 +1110,11 @@
   (interactive "p")
   (or (zerop n)
       (let ((narg (- minibuffer-history-position n))
-           (minimum (if minibuffer-default -1 0))
+           (minimum (if minibuffer-default
+                         (- (if (listp minibuffer-default)
+                                (length minibuffer-default)
+                              1))
+                       0))
            elt minibuffer-returned-to-present)
        (if (and (zerop minibuffer-history-position)
                 (null minibuffer-text-before-history))
@@ -1061,8 +1136,10 @@
        (goto-char (point-max))
        (delete-minibuffer-contents)
        (setq minibuffer-history-position narg)
-       (cond ((= narg -1)
-              (setq elt minibuffer-default))
+       (cond ((< narg 0)
+              (setq elt (if (listp minibuffer-default)
+                             (nth (1- (abs narg)) minibuffer-default)
+                           minibuffer-default)))
              ((= narg 0)
               (setq elt (or minibuffer-text-before-history ""))
               (setq minibuffer-returned-to-present t)

Index: emacs/lisp/dired-x.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/dired-x.el,v
retrieving revision 1.54
diff -u -r1.54 dired-x.el
--- emacs/lisp/dired-x.el       31 Mar 2004 16:09:18 -0000      1.54
+++ emacs/lisp/dired-x.el       30 May 2004 18:37:32 -0000
@@ -1091,56 +1105,28 @@
 
 (defun dired-guess-shell-command (prompt files)
   "Ask user with PROMPT for a shell command, guessing a default from FILES."
-
   (let ((default (dired-guess-default files))
-        default-list old-history val (failed t))
-
+        default-list val)
     (if (null default)
         ;; Nothing to guess
         (read-from-minibuffer prompt nil nil nil 'dired-shell-command-history)
-
-      ;; Save current history list
-      (setq old-history dired-shell-command-history)
-
       (if (listp default)
-
           ;; More than one guess
           (setq default-list default
                 default (car default)
                 prompt (concat
                         prompt
                         (format "{%d guesses} " (length default-list))))
-
         ;; Just one guess
         (setq default-list (list default)))
-
-      ;; Push all guesses onto history so that they can be retrieved with M-p
-      ;; and put the first guess in the prompt but not in the initial value.
-      (setq dired-shell-command-history
-            (append default-list dired-shell-command-history)
-            prompt (concat prompt (format "[%s] " default)))
-
-      ;; The unwind-protect returns VAL, and we too.
-      (unwind-protect
-          ;; BODYFORM
-          (progn
-            (setq val (read-from-minibuffer prompt nil nil nil
-                                            'dired-shell-command-history)
-                  failed nil)
-            ;; If we got a return, then use default.
-            (if (equal val "")
-                (setq val default))
-            val)
-
-        ;; UNWINDFORMS
-        ;; Undo pushing onto the history list so that an aborted
-        ;; command doesn't get the default in the next command.
-        (setq dired-shell-command-history old-history)
-        (if (not failed)
-            (or (equal val (car-safe dired-shell-command-history))
-                (setq dired-shell-command-history
-                      (cons val dired-shell-command-history))))))))
-
+      ;; Put the first guess in the prompt but not in the initial value.
+      (setq prompt (concat prompt (format "[%s] " default)))
+      ;; All guesses can be retrieved with M-n
+      (setq val (read-from-minibuffer prompt nil nil nil
+                                      'dired-shell-command-history
+                                      default-list))
+      ;; If we got a return, then return default.
+      (if (equal val "") default val))))
 
 ;;; REDEFINE.
 ;;; Redefine dired-aux.el's version:

-- 
Juri Linkov
http://www.jurta.org/emacs/





reply via email to

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