unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Re: Number of open buffers?
       [not found] <bpilkc$pq1$0@pita.alt.net>
@ 2003-11-20 16:19 ` Eli Zaretskii
       [not found] ` <mailman.342.1069348789.399.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 12+ messages in thread
From: Eli Zaretskii @ 2003-11-20 16:19 UTC (permalink / raw)


> From: Ignoramus1904 <ignoramus1904@NOSPAM.1904.invalid>
> Newsgroups: gnu.emacs.help
> Date: 20 Nov 2003 15:12:12 GMT
> 
> Sometimes though I have just one file open for quick editing. Then I
> do want to exit on \C-x\C-c.
> 
> I want to modify it so that if the # of open file buffers is more than
> one, emacs would ask YES/NO, if less, I exit. I am a very bad lisp
> programmer, any suggestions?

The expression (length (buffer-list)) will return the number of
buffers in your Emacs session.  You will need to make allowances for
the minubuffer and buffers like *Messages* and *scratch* that are
always present.  There are usually 6 such buffers, so subtract 6 from
what the expression above returns and compare it with 1.

For a more bullet-proof code, walk the buffer list returned by the
function buffer-list, and filter out any buffer which doesn't have a
file associated with it (its buffer-file-name will be nil).  What is
left are the buffers which visit files.

HTH

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number of open buffers?
       [not found]   ` <bpirf4$8ee$2@pita.alt.net>
@ 2003-11-20 18:10     ` Micah Cowan
       [not found]       ` <bpja4e$89m$1@pita.alt.net>
  0 siblings, 1 reply; 12+ messages in thread
From: Micah Cowan @ 2003-11-20 18:10 UTC (permalink / raw)


Ignoramus1904 <ignoramus1904@NOSPAM.1904.invalid> writes:

> In article <mailman.342.1069348789.399.help-gnu-emacs@gnu.org>, Eli Zaretskii wrote:
> >> From: Ignoramus1904 <ignoramus1904@NOSPAM.1904.invalid>
> >> Newsgroups: gnu.emacs.help
> >> Date: 20 Nov 2003 15:12:12 GMT
> >> 
> >> Sometimes though I have just one file open for quick editing. Then I
> >> do want to exit on \C-x\C-c.
> >> 
> >> I want to modify it so that if the # of open file buffers is more than
> >> one, emacs would ask YES/NO, if less, I exit. I am a very bad lisp
> >> programmer, any suggestions?
> > 
> > The expression (length (buffer-list)) will return the number of
> > buffers in your Emacs session.  You will need to make allowances for
> > the minubuffer and buffers like *Messages* and *scratch* that are
> > always present.  There are usually 6 such buffers, so subtract 6 from
> > what the expression above returns and compare it with 1.

I seem to get more than 6. YMMV, I think, so it can't be relied
upon.

> > 
> > For a more bullet-proof code, walk the buffer list returned by the
> > function buffer-list, and filter out any buffer which doesn't have a
> > file associated with it (its buffer-file-name will be nil).  What is
> > left are the buffers which visit files.
> > 
> > HTH
> > 
> > 
> > 
> 
> Thanks Eli, sounds a little bit above my head to be honest. I do C++
> and Perl and do not know Lisp well. I just thought that there was a
> function like get-number-of-file-buffers, or something like that.

The following is what he's talking about, I think:

(defun get-number-of-file-buffers ()
  (interactive)
  (let (num)
    (setq num 0)
    (dolist (buf (buffer-list))
      (when (buffer-file-name buf)
          (setq num (+ num 1))
      ))
    num
    )
  )

Another possibility would be to have an alias to one or other
emacs that invokes emacs in the way you want (with or without the
"are you sure?" stuff): then just use one name for emacs when you
don't want to be asked, and another for when you do.

-- 
Micah J. Cowan
micah@cowan.name

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number of open buffers?
       [not found]       ` <bpja4e$89m$1@pita.alt.net>
@ 2003-11-20 21:15         ` David Kastrup
  2003-11-20 21:50           ` lawrence mitchell
  2003-11-20 22:42           ` Johan Bockgård
  2003-11-20 21:43         ` Lucas
       [not found]         ` <mailman.369.1069368465.399.help-gnu-emacs@gnu.org>
  2 siblings, 2 replies; 12+ messages in thread
From: David Kastrup @ 2003-11-20 21:15 UTC (permalink / raw)


Ignoramus1904 <ignoramus1904@NOSPAM.1904.invalid> writes:

> In article <m31xs2c3na.fsf@localhost.localdomain>, Micah Cowan wrote:
> > Ignoramus1904 <ignoramus1904@NOSPAM.1904.invalid> writes:
> > 
> >> Eli Zaretskii wrote:
> >> >> From: Ignoramus1904 <ignoramus1904@NOSPAM.1904.invalid>
> >> >> 
> >> >> I want to modify it so that if the # of open file buffers is more than
> >> >> one, emacs would ask YES/NO, if less, I exit. I am a very bad lisp
> >> >> programmer, any suggestions?
> >> > 
> >> > 
> >> > For a more bullet-proof code, walk the buffer list returned by the
> >> > function buffer-list, and filter out any buffer which doesn't have a
> >> > file associated with it (its buffer-file-name will be nil).  What is
> >> > left are the buffers which visit files.
> >> > 
> >> 
> >> Thanks Eli, sounds a little bit above my head to be honest. I do C++
> >> and Perl and do not know Lisp well. I just thought that there was a
> >> function like get-number-of-file-buffers, or something like that.
> > 
> > The following is what he's talking about, I think:
> > 
> > (defun get-number-of-file-buffers ()
> >   (interactive)
> >   (let (num)
> >     (setq num 0)
> >     (dolist (buf (buffer-list))
> >       (when (buffer-file-name buf)
> >           (setq num (+ num 1))
> >       ))
> >     num
> >     )
> >   )

Somewhat shorter:

(defun get-number-of-file-buffers ()
  (interactive)
  (let ((num 0))
    (dolist (buf (buffer-list) num)
      (when (buffer-file-name buf)
          (setq num (1+ num))))))

> Thanks. When I try to run it via eval-expr, saying
> 
> (insert get-number-of-file-buffers)
> 
> It says: Error: void-variable. Data: get-number-of-file-buffers

Yes.  One calls a function by putting it after an opening
parenthesis.  The way you tried to "call" it, just a variable of that
name got referenced, and there is no such variable.  In addition, the
function returns a numeric expression, and `insert' expects a string.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number of open buffers?
       [not found]       ` <bpja4e$89m$1@pita.alt.net>
  2003-11-20 21:15         ` David Kastrup
@ 2003-11-20 21:43         ` Lucas
       [not found]         ` <mailman.369.1069368465.399.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 12+ messages in thread
From: Lucas @ 2003-11-20 21:43 UTC (permalink / raw)


Un beau jour, Ignoramus1904 <ignoramus1904@NOSPAM.1904.invalid> nous a dit:

>
> Thanks. When I try to run it via eval-expr, saying
>
> (insert get-number-of-file-buffers)

(insert (get-number-of-file-buffers))

should do the trick, the functions are surrounded by parens :)


-- 
Lucas

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number of open buffers?
  2003-11-20 21:15         ` David Kastrup
@ 2003-11-20 21:50           ` lawrence mitchell
  2003-11-20 22:14             ` David Kastrup
  2003-11-20 22:42           ` Johan Bockgård
  1 sibling, 1 reply; 12+ messages in thread
From: lawrence mitchell @ 2003-11-20 21:50 UTC (permalink / raw)


David Kastrup wrote:

[...]

> (defun get-number-of-file-buffers ()
>   (interactive)
>   (let ((num 0))
>     (dolist (buf (buffer-list) num)
>       (when (buffer-file-name buf)
>           (setq num (1+ num))))))

Assuming cl is loaded:

(defun get-number-of-file-buffers ()
  (loop for b in (buffer-list) count (buffer-file-name b)))

:)

>> Thanks. When I try to run it via eval-expr, saying

>> (insert get-number-of-file-buffers)

>> It says: Error: void-variable. Data: get-number-of-file-buffers

> Yes.  One calls a function by putting it after an opening
> parenthesis.  The way you tried to "call" it, just a variable of that
> name got referenced, and there is no such variable.  In addition, the
> function returns a numeric expression, and `insert' expects a string.

The OP may find reading the Emacs Lisp Introduction a useful
exercise, it's not that long, and, I think, explains the basics
of emacs lisp quite nicely.

-- 
lawrence mitchell <wence@gmx.li>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number of open buffers?
  2003-11-20 21:50           ` lawrence mitchell
@ 2003-11-20 22:14             ` David Kastrup
  2003-11-20 22:46               ` lawrence mitchell
  0 siblings, 1 reply; 12+ messages in thread
From: David Kastrup @ 2003-11-20 22:14 UTC (permalink / raw)


lawrence mitchell <wence@gmx.li> writes:

> David Kastrup wrote:
> 
> [...]
> 
> > (defun get-number-of-file-buffers ()
> >   (interactive)
> >   (let ((num 0))
> >     (dolist (buf (buffer-list) num)
> >       (when (buffer-file-name buf)
> >           (setq num (1+ num))))))
> 
> Assuming cl is loaded:
> 
> (defun get-number-of-file-buffers ()
>   (loop for b in (buffer-list) count (buffer-file-name b)))
> 

Wel, if we want to squeeze characters of source, we can do so without
cl, too.

(defun get-number-of-file-buffers ()
  (apply '+ (mapcar (lambda(x)(if(buffer-file-name x)1 0)) (buffer-list))))



-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number of open buffers?
  2003-11-20 21:15         ` David Kastrup
  2003-11-20 21:50           ` lawrence mitchell
@ 2003-11-20 22:42           ` Johan Bockgård
  1 sibling, 0 replies; 12+ messages in thread
From: Johan Bockgård @ 2003-11-20 22:42 UTC (permalink / raw)


David Kastrup <dak@gnu.org> writes:

> Somewhat shorter:
>
> (defun get-number-of-file-buffers ()
>   (interactive)
>   (let ((num 0))
>     (dolist (buf (buffer-list) num)
>       (when (buffer-file-name buf)
>           (setq num (1+ num))))))

If you are allowed to use `cl':

    ...
    (count-if 'buffer-file-name (buffer-list))

-- 
Johan Bockgård

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number of open buffers?
  2003-11-20 22:14             ` David Kastrup
@ 2003-11-20 22:46               ` lawrence mitchell
  2003-11-20 23:40                 ` David Kastrup
  0 siblings, 1 reply; 12+ messages in thread
From: lawrence mitchell @ 2003-11-20 22:46 UTC (permalink / raw)


David Kastrup wrote:

[...]

> Wel, if we want to squeeze characters of source, we can do so without
> cl, too.

> (defun get-number-of-file-buffers ()
>   (apply '+ (mapcar (lambda(x)(if(buffer-file-name x)1 0)) (buffer-list))))

I see your apply and raise you a:

(defun get-number-of-file-buffers ()
  (length (delq nil (mapcar 'buffer-file-name (buffer-list)))))

:P
-- 
lawrence mitchell <wence@gmx.li>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number of open buffers?
  2003-11-20 22:46               ` lawrence mitchell
@ 2003-11-20 23:40                 ` David Kastrup
  0 siblings, 0 replies; 12+ messages in thread
From: David Kastrup @ 2003-11-20 23:40 UTC (permalink / raw)


lawrence mitchell <wence@gmx.li> writes:

> David Kastrup wrote:
> 
> [...]
> 
> > Wel, if we want to squeeze characters of source, we can do so without
> > cl, too.
> 
> > (defun get-number-of-file-buffers ()
> >   (apply '+ (mapcar (lambda(x)(if(buffer-file-name x)1 0)) (buffer-list))))
> 
> I see your apply and raise you a:
> 
> (defun get-number-of-file-buffers ()
>   (length (delq nil (mapcar 'buffer-file-name (buffer-list)))))

I was looking for something like that, but the delq escaped me.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number of open buffers?
       [not found]           ` <bplam3$nrj$1@pita.alt.net>
@ 2003-11-21 15:36             ` David Kastrup
  0 siblings, 0 replies; 12+ messages in thread
From: David Kastrup @ 2003-11-21 15:36 UTC (permalink / raw)


Ignoramus15351 <ignoramus15351@NOSPAM.15351.invalid> writes:

> people, thank you, you are all beautiful.
> 
> The following works like a charm:
> 
> (defun get-number-of-file-buffers ()
>   (interactive)

Delete the (interactive) line: the function does nothing useful when
called interactively and thus should not clutter up command
completion.

>   (let (num)
>     (setq num 0)
>     (dolist (buf (buffer-list))
>       (when (buffer-file-name buf)
>         (setq num (+ num 1))
>         ))
>     num
>     )
>   )

Pffft.  The most verbose version.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number of open buffers?
       [not found]         ` <mailman.369.1069368465.399.help-gnu-emacs@gnu.org>
       [not found]           ` <bplam3$nrj$1@pita.alt.net>
@ 2003-11-21 15:52           ` Stefan Monnier
  2003-11-21 20:21             ` Lucas
  1 sibling, 1 reply; 12+ messages in thread
From: Stefan Monnier @ 2003-11-21 15:52 UTC (permalink / raw)


> (insert (get-number-of-file-buffers))
> should do the trick, the functions are surrounded by parens :)

Not the functions: the function *calls*.


        Stefan

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number of open buffers?
  2003-11-21 15:52           ` Stefan Monnier
@ 2003-11-21 20:21             ` Lucas
  0 siblings, 0 replies; 12+ messages in thread
From: Lucas @ 2003-11-21 20:21 UTC (permalink / raw)




> Not the functions: the function *calls*.

Yeah, that's what i wanted to say.

-- 
Lucas

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2003-11-21 20:21 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <bpilkc$pq1$0@pita.alt.net>
2003-11-20 16:19 ` Number of open buffers? Eli Zaretskii
     [not found] ` <mailman.342.1069348789.399.help-gnu-emacs@gnu.org>
     [not found]   ` <bpirf4$8ee$2@pita.alt.net>
2003-11-20 18:10     ` Micah Cowan
     [not found]       ` <bpja4e$89m$1@pita.alt.net>
2003-11-20 21:15         ` David Kastrup
2003-11-20 21:50           ` lawrence mitchell
2003-11-20 22:14             ` David Kastrup
2003-11-20 22:46               ` lawrence mitchell
2003-11-20 23:40                 ` David Kastrup
2003-11-20 22:42           ` Johan Bockgård
2003-11-20 21:43         ` Lucas
     [not found]         ` <mailman.369.1069368465.399.help-gnu-emacs@gnu.org>
     [not found]           ` <bplam3$nrj$1@pita.alt.net>
2003-11-21 15:36             ` David Kastrup
2003-11-21 15:52           ` Stefan Monnier
2003-11-21 20:21             ` Lucas

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).