I'd like to write
Elisp code that compares text character by character between two buffers.
To do that, I need to first assign buffer names to the variables
(defun set-source-buffer (&optional buffer) "Sets source-buffer to current-buffer. " (interactive "bSelect Source Buffer: \nr" buffer) (cond ((boundp 'buffer) buffer) (setq source-buffer (current-buffer))))
I would like to call this function by either activating the buffer to be selected as source and do
(setq source-buffer (current-buffer)) or use
(set-source-buffer buffer) in code, whereby
buffer is the name of the buffer to be selected.
The above code returns
nil when run on an active buffer.
How do I fix this?
I refer to the
if statement provided by @Hubisan below.
(defun my-set-source-buffer (&optional buffer-or-name) "Sets `my-source-buffer' to `current-buffer' unless BUFFER-OR-NAME is given. BUFFER-OR-NAME can be a string of an existing buffer or a buffer object." (interactive "bSelect Source Buffer:") (setq my-source-buffer (if (buffer-live-p (get-buffer buffer)) (get-buffer buffer) (current-buffer))))
When should we use
buffer-or-name for a variable and when to use just