unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to know is a buffer is a temporary one?
@ 2013-07-29 15:26 Sebastien Vauban
  2013-07-29 16:19 ` Thorsten Jolitz
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Sebastien Vauban @ 2013-07-29 15:26 UTC (permalink / raw)
  To: help-gnu-emacs-mXXj517/zsQ

Hello,

I want to attach some code on first-change-hook, but I noticed *a lot* of
temporary buffers are created (and modified), hence my code running in many
situations where it doesn't need to.

So, is there a way for my code not to be applied for temp buffers? Function to
use in a `(when ...)' construct? Or do I have to look for the pattern `^ *' in
the name of the buffer (that is, a name beginning with a space and a star)?

Best regards,
  Seb

-- 
Sebastien Vauban


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

* Re: How to know is a buffer is a temporary one?
  2013-07-29 15:26 How to know is a buffer is a temporary one? Sebastien Vauban
@ 2013-07-29 16:19 ` Thorsten Jolitz
  2013-07-29 18:34 ` Stefan Monnier
       [not found] ` <mailman.2054.1375122929.12400.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 10+ messages in thread
From: Thorsten Jolitz @ 2013-07-29 16:19 UTC (permalink / raw)
  To: help-gnu-emacs

"Sebastien Vauban" <sva-news@mygooglest.com>
writes:

> Hello,
>
> I want to attach some code on first-change-hook, but I noticed *a lot*
> of temporary buffers are created (and modified), hence my code running
> in many situations where it doesn't need to.
>
> So, is there a way for my code not to be applied for temp buffers?
> Function to use in a `(when ...)' construct? Or do I have to look for
> the pattern `^ *' in the name of the buffer (that is, a name beginning
> with a space and a star)?

#+begin_quote
...with-temp-buffer creates a buffer named ␣*temp* (note the leading
whitespace)...
#+end_quote

maybe its enough to identify all tmp buffers created by
`with-current-buffer' by their naming convention?

cheers,
Thorsten




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

* Re: How to know is a buffer is a temporary one?
  2013-07-29 15:26 How to know is a buffer is a temporary one? Sebastien Vauban
  2013-07-29 16:19 ` Thorsten Jolitz
@ 2013-07-29 18:34 ` Stefan Monnier
       [not found] ` <mailman.2054.1375122929.12400.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 10+ messages in thread
From: Stefan Monnier @ 2013-07-29 18:34 UTC (permalink / raw)
  To: help-gnu-emacs

> So, is there a way for my code not to be applied for temp buffers?

Emacs doesn't really have a formal definition of "temp buffer".
Maybe you're better off trying to define "useful buffer" for your
particular case.  E.g. maybe check `buffer-file-name'.


        Stefan




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

* Re: How to know is a buffer is a temporary one?
       [not found] ` <mailman.2054.1375122929.12400.help-gnu-emacs@gnu.org>
@ 2013-07-29 19:00   ` Sebastien Vauban
  2013-07-29 20:06     ` Sebastien Vauban
  0 siblings, 1 reply; 10+ messages in thread
From: Sebastien Vauban @ 2013-07-29 19:00 UTC (permalink / raw)
  To: help-gnu-emacs-mXXj517/zsQ

Hello Stefan,

Stefan Monnier wrote:
>> So, is there a way for my code not to be applied for temp buffers?
>
> Emacs doesn't really have a formal definition of "temp buffer".

That was my (unknown) point.

> Maybe you're better off trying to define "useful buffer" for your
> particular case.  E.g. maybe check `buffer-file-name'.

Thanks for you answer!

Best regards,
  Seb

-- 
Sebastien Vauban


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

* Re: How to know is a buffer is a temporary one?
  2013-07-29 19:00   ` Sebastien Vauban
@ 2013-07-29 20:06     ` Sebastien Vauban
  2013-07-29 21:09       ` Drew Adams
                         ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Sebastien Vauban @ 2013-07-29 20:06 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs-mXXj517/zsQ

Stefan Monnier wrote:
> Maybe you're better off trying to define "useful buffer" for your
> particular case.  E.g. maybe check `buffer-file-name'.

Then a version checking with a regexp seems slightly more efficient (1.19 s vs
1.14 s) than the one checking the name with a substring:

--8<---------------cut here---------------start------------->8---
(setq i 0)

;; 1.19 s on i7 on mains
;; (while (< i 1000)
;;   (message "%s" i)
;;   (if (equal (substring (buffer-name) 0 2) " *")
;;       (message "this is a temp buffer")
;;     (message "this is not a temp buffer"))
;;   (setq i (+ i 1)))

;; 1.14 s on i7 on mains
(while (< i 1000)
  (message "%s" i)
  (if (string-match "^ \*" (buffer-name))
      (message "this is a temp buffer")
    (message "this is not a temp buffer"))
  (setq i (+ i 1)))
--8<---------------cut here---------------end--------------->8---

Best regards,
  Seb

-- 
Sebastien Vauban



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

* RE: How to know is a buffer is a temporary one?
  2013-07-29 20:06     ` Sebastien Vauban
@ 2013-07-29 21:09       ` Drew Adams
  2013-07-29 21:24       ` Stefan Monnier
       [not found]       ` <mailman.2061.1375133109.12400.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 10+ messages in thread
From: Drew Adams @ 2013-07-29 21:09 UTC (permalink / raw)
  To: Sebastien Vauban; +Cc: help-gnu-emacs

FWIW, buffers whose names begin with " *" are not necessarily temporary.

The doc says only that such a buffer "does not show up in `M-x list-buffers'.



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

* Re: How to know is a buffer is a temporary one?
  2013-07-29 20:06     ` Sebastien Vauban
  2013-07-29 21:09       ` Drew Adams
@ 2013-07-29 21:24       ` Stefan Monnier
       [not found]       ` <mailman.2061.1375133109.12400.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 10+ messages in thread
From: Stefan Monnier @ 2013-07-29 21:24 UTC (permalink / raw)
  To: Sebastien Vauban; +Cc: help-gnu-emacs

>> Maybe you're better off trying to define "useful buffer" for your
>> particular case.  E.g. maybe check `buffer-file-name'.
> Then a version checking with a regexp seems slightly more efficient
> (1.19 s vs 1.14 s) than the one checking the name with a substring:

> ;; (while (< i 1000)
> ;;   (message "%s" i)
> ;;   (if (equal (substring (buffer-name) 0 2) " *")
> ;;       (message "this is a temp buffer")
> ;;     (message "this is not a temp buffer"))
> ;;   (setq i (+ i 1)))

should be

   (while (< i 1000)
     (message "%s" i)
     (message (if (eq (aref (buffer-name) 0) ?\s)
         "this is a temp buffer"
       "this is not a temp buffer"))
     (setq i (+ i 1)))

Note that some buffers that start with " *" are shown to the user.
So, the notion of temporary/internal depends on the particular situation
(which is why it's not formally defined).  In some cases
buffer-file-name is a good test, in other cases (get-buffer-window <buf>
t) is a better test.

Sometimes, testing (listp buffer-undo-list) may also be a good idea.


        Stefan



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

* Re: How to know is a buffer is a temporary one?
       [not found]         ` <mailman.2061.1375133109.12400.help-gnu-emacs-mXXj517/zsQ@public.gmane.org>
@ 2013-07-30  7:33           ` Sebastien Vauban
  2013-07-30 13:26             ` Stefan Monnier
       [not found]             ` <mailman.2130.1375190796.12400.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 10+ messages in thread
From: Sebastien Vauban @ 2013-07-30  7:33 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs-mXXj517/zsQ

Stefan Monnier wrote:
>>> Maybe you're better off trying to define "useful buffer" for your
>>> particular case.  E.g. maybe check `buffer-file-name'.
>> Then a version checking with a regexp seems slightly more efficient
>> (1.19 s vs 1.14 s) than the one checking the name with a substring:
>
>> ;; (while (< i 1000)
>> ;;   (message "%s" i)
>> ;;   (if (equal (substring (buffer-name) 0 2) " *")
>> ;;       (message "this is a temp buffer")
>> ;;     (message "this is not a temp buffer"))
>> ;;   (setq i (+ i 1)))
>
> should be
>
>    (while (< i 1000)
>      (message "%s" i)
>      (message (if (eq (aref (buffer-name) 0) ?\s)
>          "this is a temp buffer"
>        "this is not a temp buffer"))
>      (setq i (+ i 1)))

Performance-wise, both versions are equal (1.19s). Still slightly behind the
`string-match' version (1.14s).

Why are you saying that that version somehow better is? Was it for perf
reasons, or for other reasons? I'm asking because I want to learn more...

> Note that some buffers that start with " *" are shown to the user.
> So, the notion of temporary/internal depends on the particular situation
> (which is why it's not formally defined).  In some cases
> buffer-file-name is a good test, in other cases (get-buffer-window <buf>
> t) is a better test.
>
> Sometimes, testing (listp buffer-undo-list) may also be a good idea.

Thanks for the extra info!

Best regards,
  Seb

-- 
Sebastien Vauban



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

* Re: How to know is a buffer is a temporary one?
  2013-07-30  7:33           ` Sebastien Vauban
@ 2013-07-30 13:26             ` Stefan Monnier
       [not found]             ` <mailman.2130.1375190796.12400.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 10+ messages in thread
From: Stefan Monnier @ 2013-07-30 13:26 UTC (permalink / raw)
  To: Sebastien Vauban; +Cc: help-gnu-emacs

> Why are you saying that that version somehow better is?

It eliminates a redundancy (the two calls to `message' are reduced to
one), and it uses the same test as is used elsewhere in Emacs ("buffer
name starts with a space" is used for example to hide buffers in
completion or to decide whether buffer-undo-list should be initially
set to nil or the t).


        Stefan



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

* Re: How to know is a buffer is a temporary one?
       [not found]               ` <mailman.2130.1375190796.12400.help-gnu-emacs-mXXj517/zsQ@public.gmane.org>
@ 2013-07-30 13:58                 ` Sebastien Vauban
  0 siblings, 0 replies; 10+ messages in thread
From: Sebastien Vauban @ 2013-07-30 13:58 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs-mXXj517/zsQ

Stefan Monnier wrote:
>> Why are you saying that that version somehow better is?
>
> It eliminates a redundancy (the two calls to `message' are reduced to one),

I did not catch that!

> and it uses the same test as is used elsewhere in Emacs ("buffer name starts
> with a space" is used for example to hide buffers in completion or to decide
> whether buffer-undo-list should be initially set to nil or the t).

OK, good reasons... Thanks for explaining!

Best regards,
  Seb

-- 
Sebastien Vauban



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

end of thread, other threads:[~2013-07-30 13:58 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-07-29 15:26 How to know is a buffer is a temporary one? Sebastien Vauban
2013-07-29 16:19 ` Thorsten Jolitz
2013-07-29 18:34 ` Stefan Monnier
     [not found] ` <mailman.2054.1375122929.12400.help-gnu-emacs@gnu.org>
2013-07-29 19:00   ` Sebastien Vauban
2013-07-29 20:06     ` Sebastien Vauban
2013-07-29 21:09       ` Drew Adams
2013-07-29 21:24       ` Stefan Monnier
     [not found]       ` <mailman.2061.1375133109.12400.help-gnu-emacs@gnu.org>
     [not found]         ` <mailman.2061.1375133109.12400.help-gnu-emacs-mXXj517/zsQ@public.gmane.org>
2013-07-30  7:33           ` Sebastien Vauban
2013-07-30 13:26             ` Stefan Monnier
     [not found]             ` <mailman.2130.1375190796.12400.help-gnu-emacs@gnu.org>
     [not found]               ` <mailman.2130.1375190796.12400.help-gnu-emacs-mXXj517/zsQ@public.gmane.org>
2013-07-30 13:58                 ` Sebastien Vauban

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