Skip to content

Instantly share code, notes, and snippets.

Last active Nov 7, 2017
What would you like to do?
Noarrowing in other indirect clone buffer
;; Narrow to the current buffer is somethimes weird because you loose
;; the ability to see other parts of the buffer. One way is to first
;; create a clone of the current buffer. However, when this is
;; repeated, clones accumulate.
;; This hack just allows to kill clones when ending the narrowing,
;; allowing transparent workflow without manual buffer bookkeeping.
(defvar my-narrowed-buffers nil)
(defun my-kill-buffer-on-widen ()
(when (member (current-buffer) my-narrowed-buffers)
(setq my-narrowed-buffers
(cl-remove-if (lambda (buf) (or (equal (current-buffer) buf)
(not (buffer-live-p buf))))
(advice-add 'widen :after #'my-kill-buffer-on-widen)
(defun my-narrow-to-sexp ()
(let ((start (point))
(end (progn (forward-sexp) (point))))
(goto-char start)
(move-beginning-of-line 1)
(let ((clone-buffer (clone-indirect-buffer nil nil))
(narrow-start (point)))
(with-current-buffer clone-buffer
(goto-char start)
(narrow-to-region narrow-start end)
(push clone-buffer my-narrowed-buffers)
(switch-to-buffer clone-buffer)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment