unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Tim McNamara <timmcn@bitstream.net>
Subject: Re: elisp question
Date: Thu, 01 Dec 2005 13:29:11 -0600	[thread overview]
Message-ID: <m2r78whb08.fsf@Althea.local> (raw)
In-Reply-To: 87iru9qug6.fsf-monnier+gnu.emacs.help@gnu.org

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!

  reply	other threads:[~2005-12-01 19:29 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-30 23:13 elisp question Tim McNamara
2005-11-30 23:22 ` Lennart Borgman
2005-12-01  5:07 ` Stefan Monnier
2005-12-01 19:29   ` Tim McNamara [this message]
2005-12-01 21:00     ` Stefan Monnier
2005-12-02  1:02       ` Tim McNamara
2005-12-02 17:51   ` Kevin Rodgers
2005-12-01  6:24 ` N. Raghavendra
2005-12-01 19:32   ` Tim McNamara
2005-12-01 21:37     ` N. Raghavendra
2005-12-01 23:40       ` Thien-Thi Nguyen
2005-12-02  7:44         ` N. Raghavendra
2005-12-02 14:43           ` Thien-Thi Nguyen
2005-12-02  1:05       ` Tim McNamara
     [not found] ` <mailman.17404.1133392955.20277.help-gnu-emacs@gnu.org>
2005-12-01 19:25   ` Tim McNamara
  -- strict thread matches above, loose matches on Subject: below --
2009-09-27 14:32 Elisp Question magicus
2009-09-27 15:26 ` Giorgos Keramidas
2009-09-27 17:10   ` magicus
     [not found] <mailman.941.1223918781.25473.help-gnu-emacs@gnu.org>
2008-10-13 18:11 ` elisp question Niels Giesen
2008-10-13 19:09   ` Seweryn Kokot
     [not found]   ` <mailman.948.1223924333.25473.help-gnu-emacs@gnu.org>
2008-10-13 19:40     ` Pascal J. Bourguignon
2008-10-13 20:47       ` Drew Adams
2008-10-13 21:11       ` Seweryn Kokot
2008-10-13 17:36 Seweryn Kokot
2008-04-30 21:23 harven
2008-05-01 13:08 ` Kevin Rodgers
     [not found] ` <mailman.11020.1209647315.18990.help-gnu-emacs@gnu.org>
2008-05-01 13:54   ` harven
2008-05-03 15:38 ` Bastien
2003-08-20  7:37 Mike Ballard
2003-08-20  8:07 ` Joakim Hove
2003-08-22  5:52   ` Mike Ballard
2003-08-22  6:38     ` Joakim Hove
2003-08-20 15:07 ` Peter Lee

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2r78whb08.fsf@Althea.local \
    --to=timmcn@bitstream.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).