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

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

Re: Writing text to a dedicated buffer


From: Joost Kremers
Subject: Re: Writing text to a dedicated buffer
Date: Sun, 02 May 2021 16:23:27 +0200
User-agent: mu4e 1.5.12; emacs 27.2

On Sun, May 02 2021, Christopher Dimech wrote:
> I have done the writing to a dedicated buffer as follows,
> using "gungadin-bufr-insert" for writing to the Gungadin buffer.
>
> -------- code --------
>
> (setq gungadin-bufr (generate-new-buffer "*Gungadin*"))

This should be a `defvar`. Personally, I would probably initialise it to
`nil` and create a buffer on the first call to `gungadin-bufr-insert`. But more
likely, I would probably do something like this:

(defvar gungadin-buffer-name "*Gungadin*")

(defun gungadin-buffer-insert (message)
  (with-current-buffer (get-buffer-create gungadin-buffer-name)
    ...)) 

There is no problem calling `get-buffer-create` on a buffer that already exists.
Just make sure you pass it the name of the buffer, not the buffer object itself,
because in the latter case you may get back a dead buffer which you cannot write
to anymore. (You never know when a user accidentally kills the Gungadin buffer.)
Do `C-h f get-buffer-create RET` for details.

> (defun gungadin-bufr-insert (format-string)
>    "Display a message at the bottom of the Gungadin Buffer."
>
>    (set-buffer gungadin-bufr)
>    (with-current-buffer gungadin-bufr
>      (insert format-string)) )

If you check the source code of `with-current-buffer`, you'll notice that it
uses `set-buffer`. There is no need to call `set-buffer` if you're going to use
`with-current-buffer`. (And you should be using `with-current-buffer`.)


-- 
Joost Kremers
Life has its moments



reply via email to

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