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

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

Re: check to see if a buffer with a certain name exists?


From: Ian Eure
Subject: Re: check to see if a buffer with a certain name exists?
Date: Thu, 13 Nov 2008 13:03:10 -0800

On Nov 13, 2008, at 11:50 AM, Nikolaj Schumacher wrote:

Matt Price <matt.price@utoronto.ca> wrote:

in lisp i'm not seeing a quick way to test for something like this.
best i can see right now is to use dolist thus:

(dolist (name (buffer-list) foundit)
 (let ((foundit 0))
   (if (string-match "*scratch*" name)
   (setq foundit 1))
)
)

Drew already gave you the cleaner way. Let me also tell you, why yours
is not working.

First, you bind foundit inside the `dolist' body. The value is lost when
the `let' block exits, and the variable is undefined.

Second, `buffer-list' returns buffer "objects", not strings.

Here's how it would work:

(let ((foundit 0))
 (dolist (name (buffer-list) foundit)
   (if (string-match "*scratch*" (buffer-name name))
       (setq foundit 1))))

Or the Awesome Lisp Way(TM):

(member "*scratch*" (mapcar 'buffer-name (buffer-list)))

Is there something wrong with:

(get-buffer "*Mutt*")

...which returns the #buffer object?

Or if you really care about t/nil, you can do:

(bufferp (get-buffer "*Mutt*"))

 - Ian




reply via email to

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