[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: elisp question
From: |
Tim McNamara |
Subject: |
Re: elisp question |
Date: |
Thu, 01 Dec 2005 13:29:11 -0600 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (darwin) |
Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> The files are in a subdirectory (called kaya), numbered 1 to
>> whatever, so I want to randomly pick an integer from 1 to whatever.
>> I thought that (find-file) or perhaps (find-file-noselect) could be
>> used in conjunction with (random), but I can't figure out how to do
>> that. I've probably missed something really bloody obvious in the
>> elisp manual...
>
> (pop-to-buffer
> (find-file-noselect (expand-file-name (number-to-string (random N))
> "foo/bar/kaya")))
>
> This assumes the file names go from 0 to N-1.
>
> You could also do
>
> (let ((files (directory-files "foo/bar/kaya" 'full "[^.]\\|...")))
> (pop-to-buffer (find-file-noselect (nth (random (length files)) files))))
>
> and just select any random file in the directory, without any assumption on
> the file names used.
That looks simpler than what I was thinking, although I'll have to go
through it argument by argument to make sure I understand it. I was
expecting to have to count the number of files in the directory, then
use that number as the limit to (random), and somehow be able to make
(find-files) use the result from (random) as the name of the file to
be read into the buffer. Simpler is better!
I'll test and post the results. Thanks!