unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Joost Kremers <joostkremers@fastmail.fm>
To: Christopher Dimech <dimech@gmx.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Writing text to a dedicated buffer
Date: Sun, 02 May 2021 16:23:27 +0200	[thread overview]
Message-ID: <87lf8xkxoc.fsf@fastmail.fm> (raw)
In-Reply-To: <trinity-1f80a056-a6f7-4b43-9e24-a3516d826457-1619961907806@3c-app-mailcom-bs08>


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	other threads:[~2021-05-02 14:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-02 12:48 Writing text to a dedicated buffer Christopher Dimech
2021-05-02 13:05 ` Christopher Dimech
2021-05-02 17:08   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-02 13:25 ` Christopher Dimech
2021-05-02 14:23   ` Joost Kremers [this message]
2021-05-02 17:06 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-04 13:07 ` Filipp Gunbin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87lf8xkxoc.fsf@fastmail.fm \
    --to=joostkremers@fastmail.fm \
    --cc=dimech@gmx.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).