Last active
November 13, 2024 03:51
-
-
Save jamescherti/686d8cb3d636614cefe76c6fc7b76e55 to your computer and use it in GitHub Desktop.
Emacs: Create an indirect buffer and switch to it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; clone-indirect-buffer-current-window.el --- Create an indirect buffer and switch to it.. -*- lexical-binding: t; -*- | |
;; Gits URL: https://gist.github.com/jamescherti/686d8cb3d636614cefe76c6fc7b76e55 | |
;; License: MIT | |
;; Author: James Cherti | |
(defun my-clone-indirect-buffer-current-window (&optional newname norecord) | |
"Create an indirect buffer and switch to it. | |
Creates a new indirect buffer with the same content as the current buffer, | |
preserving point position, window start, and horizontal scroll position. The | |
original buffer remains unchanged. | |
The optional argument NEWNAME specifies the indirect buffer name. If NEWNAME is | |
nil, the name defaults to the current buffer's name with a <N> suffix added, | |
or by incrementing N in an existing suffix. An error occurs when attempting to | |
clone a buffer whose major mode symbol has a non-nil `no-clone-indirect' | |
property. | |
The optional argument NORECORD, when non-nil, prevents putting this buffer at | |
the front of the list of recently selected ones. | |
Returns the newly created indirect buffer." | |
(interactive) | |
(let ((point (point)) | |
(window-start (window-start)) | |
(window-hscroll (window-hscroll)) | |
(indirect-buffer (clone-indirect-buffer newname nil norecord))) | |
(when (buffer-live-p indirect-buffer) | |
(switch-to-buffer indirect-buffer) | |
(with-current-buffer indirect-buffer | |
(goto-char point) | |
(let ((selected-window (selected-window)) | |
(buffer-window (get-buffer-window indirect-buffer))) | |
(when (and buffer-window | |
(eq selected-window buffer-window)) | |
(set-window-start selected-window window-start) | |
(set-window-hscroll selected-window window-hscroll)))) | |
indirect-buffer))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment