0

Have been playing for some time with buffers. With this code I can make a new buffer called Corona and insert some text in it. Now I would like to pass the buffer name to bufr-message rather than having it hardwired to use corona-bufr. Together with a function that appends text to an existing buffer. Would it be better to have a single function that writes to a specific buffer and if the buffer does not exist, it is made?

(defvar coronas-bufr "*Corona*")

(defun bufr-message (message)
   "Inserts formatted string into the dedicated buffer."

   (with-current-buffer
       (get-buffer-create corona-bufr)
     (insert message)) )

1 Answer 1

0

get-buffer-create can take a string as the buffer name, thus you can pass the buffer name to the function.

get-buffer-create is a built-in function in ‘C source code’.

(get-buffer-create BUFFER-OR-NAME &optional INHIBIT-BUFFER-HOOKS)

Return the buffer specified by BUFFER-OR-NAME, creating a new one if needed. If BUFFER-OR-NAME is a string and a live buffer with that name exists, return that buffer. If no such buffer exists, create a new buffer with that name and return it.

(defun bufr-message (bufname message)
   "Inserts formatted string MESSAGE into the dedicated buffer named BUFNAME."

   (with-current-buffer
     (get-buffer-create bufname)
     (insert message)))

I don't know wether there is a built-in function that inserts text to a buffer (and creates the buffer if it does not exist), but I think you can write one if you like. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.