From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tim McNamara Newsgroups: gmane.emacs.help Subject: Re: elisp question Date: Thu, 01 Dec 2005 13:29:11 -0600 Organization: ipHouse - Welcome Home! Message-ID: References: <87iru9qug6.fsf-monnier+gnu.emacs.help@gnu.org> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1133465677 10874 80.91.229.2 (1 Dec 2005 19:34:37 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 1 Dec 2005 19:34:37 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Dec 01 20:34:37 2005 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1Ehu9A-0006Ee-A0 for geh-help-gnu-emacs@m.gmane.org; Thu, 01 Dec 2005 20:31:13 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ehu95-000566-LM for geh-help-gnu-emacs@m.gmane.org; Thu, 01 Dec 2005 14:31:07 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!postnews.google.com!news3.google.com!news.glorb.com!green.octanews.net!news-out.octanews.net!news-2.mpls.iphouse.net.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (darwin) Cancel-Lock: sha1:mJlUiT4zY6Tb7+P15Y2ZW8twyPg= Original-Lines: 31 Original-NNTP-Posting-Date: 01 Dec 2005 13:29:12 CST Original-NNTP-Posting-Host: 753a659a.newsreader.iphouse.net Original-X-Trace: DXC=I>lKWZBZcj:hkL@X0LQX08Vn\Tj8`^Bb; eHS>e[L>8304V09HFNfSN3m:@8]TdIgg5f@e7N90MVd=Uh?jdWg@OU5 Original-X-Complaints-To: abuse@iphouse.net Original-Xref: shelby.stanford.edu gnu.emacs.help:135977 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:31584 Archived-At: Stefan Monnier 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!