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

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

RE: `read-file-name' with history?


From: Drew Adams
Subject: RE: `read-file-name' with history?
Date: Fri, 10 Dec 2010 07:39:49 -0800

> >> unlike `read-string', `read-file-name' lacks an history 
> >> parameter.  Is there a way to achieve such behavior?
> >
> > Correct, there is no history parameter.  But it does use a 
> > history list: `file-name-history'.
> >
> > To achieve a file-name reading behavior with an arbitrary 
> > history list, you would need to write your own file-name
> > reading function and bind it to the variable
> > `read-file-name-function'.  Then let-bind that var around
> > a call to `read-file-name.
> >
> >> Since `read-string' is a built-in function in C source 
> >> code, I can't examine it (well, I could examine the C
> >> sources, but I think it would be a little difficult).
> >
> > What is the question here?  What is the relation to your 
> > question about `read-file-name''s history parameter?
> 
> My problem is that writing such a function is beyond my current
> capabilities.  I usually manage to achieve such goals by modifying
> some existing Lisp code.  In this case, however, no Lisp code is
> available.  That's why I was asking for pointers.

OK, but the Lisp code for `read-file-name' is available.
>From there you can see that `file-name-history' is used.

Instead of defining your own function for `read-file-name-function', you can
also just define a function that explicitly adds whatever file name is read to a
history that you pass it.  In order to not also add that name to the regular
`file-name-history' you need to bind that var so it gets restored when your
function exits.  For example:

(defvar my-history () "Once upon a time...")

(defun my-read-file-name (prompt &optional dir default-filename
                          mustmatch initial predicate history)
  (let* ((file-name-history  file-name-history)
           (file  (read-file-name prompt dir default-filename
                                      mustmatch initial predicate)))
    (add-to-list history file)
    file))

Now try:
(my-read-file-name "File: " nil nil t nil nil 'my-history)

C-h v my-history
C-h v file-name-history

And compare with: (read-file-name "File: " nil nil t)







reply via email to

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