[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: `read-file-name' with history?
From: |
egarrulo |
Subject: |
Re: `read-file-name' with history? |
Date: |
Fri, 10 Dec 2010 16:05:19 +0000 |
Thank you Drew. Another guy kindly sent me another implementation of
`read-file-name' with history. I'll study both examples.
Indeed you are right: `read-file-name' is defined into "minibuffer.el"
in Emacs 23.2. I'm using Emacs 22.3, where it is defined in C source,
therefore I didn't notice.
Have a nice day ;-)
Elena
2010/12/10 Drew Adams <drew.adams@oracle.com>:
>> >> 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)
>
>
>
>
>