Function: clone-buffer
clone-buffer is an interactive and byte-compiled function defined in
simple.el.gz.
Signature
(clone-buffer &optional NEWNAME DISPLAY-FLAG)
Documentation
Create and return a twin copy of the current buffer.
Unlike an indirect buffer, the new buffer can be edited
independently of the old one (if it is not read-only).
NEWNAME is the name of the new buffer. It may be modified by
adding or incrementing <N> at the end as necessary to create a
unique buffer name. If nil, it defaults to the name of the
current buffer, with the proper suffix. If DISPLAY-FLAG is
non-nil, the new buffer is shown with pop-to-buffer. Trying to
clone a file-visiting buffer, or a buffer whose major mode symbol
has a non-nil no-clone property, results in an error.
Interactively, DISPLAY-FLAG is t and NEWNAME is the name of the current buffer with appropriate suffix. However, if a prefix argument is given, then the command prompts for NEWNAME in the minibuffer.
This runs the normal hook clone-buffer-hook in the new buffer
after it has been set up properly in other respects.
Probably introduced at or before Emacs version 21.1.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/simple.el.gz
;; things to maybe add (currently partly covered by `funcall mode'):
;; - syntax-table
;; - overlays
(defun clone-buffer (&optional newname display-flag)
"Create and return a twin copy of the current buffer.
Unlike an indirect buffer, the new buffer can be edited
independently of the old one (if it is not read-only).
NEWNAME is the name of the new buffer. It may be modified by
adding or incrementing <N> at the end as necessary to create a
unique buffer name. If nil, it defaults to the name of the
current buffer, with the proper suffix. If DISPLAY-FLAG is
non-nil, the new buffer is shown with `pop-to-buffer'. Trying to
clone a file-visiting buffer, or a buffer whose major mode symbol
has a non-nil `no-clone' property, results in an error.
Interactively, DISPLAY-FLAG is t and NEWNAME is the name of the
current buffer with appropriate suffix. However, if a prefix
argument is given, then the command prompts for NEWNAME in the
minibuffer.
This runs the normal hook `clone-buffer-hook' in the new buffer
after it has been set up properly in other respects."
(interactive
(progn
(if buffer-file-name
(error "Cannot clone a file-visiting buffer"))
(if (get major-mode 'no-clone)
(error "Cannot clone a buffer in %s mode" mode-name))
(list (if current-prefix-arg
(read-buffer "Name of new cloned buffer: " (current-buffer)))
t)))
(if buffer-file-name
(error "Cannot clone a file-visiting buffer"))
(if (get major-mode 'no-clone)
(error "Cannot clone a buffer in %s mode" mode-name))
(setq newname (or newname (buffer-name)))
(if (string-match "<[0-9]+>\\'" newname)
(setq newname (substring newname 0 (match-beginning 0))))
(let ((buf (current-buffer))
(ptmin (point-min))
(ptmax (point-max))
(pt (point))
(mk (if mark-active (mark t)))
(modified (buffer-modified-p))
(mode major-mode)
(lvars (buffer-local-variables))
(process (get-buffer-process (current-buffer)))
(new (generate-new-buffer (or newname (buffer-name)))))
(save-restriction
(widen)
(with-current-buffer new
(insert-buffer-substring buf)))
(with-current-buffer new
(narrow-to-region ptmin ptmax)
(goto-char pt)
(if mk (set-mark mk))
(set-buffer-modified-p modified)
;; Clone the old buffer's process, if any.
(when process (clone-process process))
;; Now set up the major mode.
(funcall mode)
;; Set up other local variables.
(mapc (lambda (v)
(condition-case ()
(if (symbolp v)
(makunbound (make-local-variable v))
(set (make-local-variable (car v)) (cdr v)))
(setting-constant nil))) ;E.g. for enable-multibyte-characters.
lvars)
(setq mark-ring (mapcar (lambda (mk) (copy-marker (marker-position mk)))
mark-ring))
;; Run any hooks (typically set up by the major mode
;; for cloning to work properly).
(run-hooks 'clone-buffer-hook))
(if display-flag
;; Presumably the current buffer is shown in the selected frame, so
;; we want to display the clone elsewhere.
(let ((same-window-regexps nil)
(same-window-buffer-names))
(pop-to-buffer new)))
new))