help-gnu-emacs
[Top][All Lists]
Advanced

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

RE: lisp read-from-minibuffer propels deep questions


From: Drew Adams
Subject: RE: lisp read-from-minibuffer propels deep questions
Date: Mon, 2 Apr 2012 22:35:49 -0700

> (read-from-minibuffer
>  (format "Directory (default %s):" default-directory)
>  default-directory )
> 
> prompt user to enter a dir, with default at current dir.

Try `read-directory-name'.  (Its doc too is a bit confusing, IMO.)  Here are a
couple ways to read the name of a directory.  The first requires the input to be
an existing directory; the second does not.

(expand-file-name
 (read-directory-name
  (format "Directory (default %s): " default-directory)
  default-directory default-directory t))

(let ((insert-default-directory  t))
  (read-directory-name "Directory: " default-directory))

You can also use `read-file-name'.  Both of those functions let the user use
completion: they are file/dir-name aware.

`read-from-minibuffer' on the other hand just reads a string (and optionally
calls the Lisp reader on it).

> however, according to inline doc of read-from-minibuffer, the second
> arg for default input is obsolete. 

(FWIW, I disagree with that pronouncement by Emacs Devel, but so be it.)

The doc actually says, at the very end, that you can use the second arg if the
HIST arg is a cons, i.e., is a non-nil history list.

Example:

(read-from-minibuffer "Directory: " default-directory
                     nil nil file-name-history)

or just (read-from-minibuffer "Directory: " default-directory) if you don't care
about using the file-name history (`M-p').

If you avoid using the "deprecated" INITIAL-CONTENTS arg (like a good boy), then
you must test the string input by the user, and if it is "" then return
DEFAULT-VALUE:

(let ((input  (read-from-minibuffer
                   (format "Directory (default %s): " default-directory)
                   nil nil nil nil default-directory)))
  (when (string= "" input) (setq input  default-directory))
  input)

But again, `read-from-minibuffer' does not know anything about file or dir names
or how to complete them (unless you jump through hoops, providing it a
file/dir-name completion keymap).  

> Instead, you have to use the 6th arg.

Nope, as you discovered, nothing gets inserted in the minibuffer, and empty
input means "" is returned.

> Read the doc again, it turns out that the 4th arg must
> be t in order for the default value to work, else you get empty string
> if the user just press Enter.

Nope, as you discovered.  That's something else again.

> woops! no go! because if the 4th arg is t, it means the input as a
> string will be fed to lisp reader, then interpreted as a lisp object.
> Hot damn. This means, if you want a string, you have to feed it
> ?"\"mystring\""?. (the outter string makes it a lisp string to be fed
> to lisp reader, then, the inner string gets you a lisp string object)

No, don't even think of going down that road.

`read-from-minibuffer' is a very general function, which is one reason its doc
is confusing (not that that is an excuse).  It is used to implement
`completing-read', `read-file-name', and so on.  The 4th arg is only for
situations where you want to get user input as a Lisp sexp and then evaluate it.

> But no! ... WTF?
> This line is supposed to be done in 20 seconds. Now i've spent 40min
> on this. Now, my mind wanders to the deep question of humanity..

Try using `read-file-name' or `read-directory-name'.  They should help.




reply via email to

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