The previous post shows the following command to automatically take the selected region, add asterisks around it and rename the buffer accordingly:
(defun autorename-buffer-1 () "Give the current buffer a new name based on the selected string, with asterisks." (interactive) (let ((string (progn (copy-region-as-kill (region-beginning) (region-end)) (with-temp-buffer (insert (current-kill 0)) (goto-char (point-min)) (insert "*") (goto-char (point-max)) (insert "*") (buffer-string))))) (rename-buffer string t) ;; Clean up kill ring (when kill-ring (setq kill-ring (cdr kill-ring)))))
The above command copies the region and pastes it in temporary buffer, which is a good trick to know about and use when doing complex processing on massive strings. However, there are two issues. One is that the copying affects the kill ring, which needs to be cleaned up. The second is that buffer names are never massive.
Therefore it was nice to discover a simpler function to do the same:
(defun autorename-buffer-3 () "Give the current buffer a new name based on the selected string, with asterisks." (interactive) (rename-buffer (concat "*" (buffer-substring-no-properties (region-beginning) (region-end)) "*") t))
In the above command, the selected region is wrapped in
concat, an Emacs command whose documentation reads “Concatenate all the arguments and make the result a string.” It turns out that this command’s arguments can simply be bits of string and the number of arguments is unlimited.