Skip to content

Instantly share code, notes, and snippets.

@mookid
Last active November 7, 2017 08:35
Show Gist options
  • Save mookid/eae6fbd0fedde9cfd3c8ad7d46b8031e to your computer and use it in GitHub Desktop.
Save mookid/eae6fbd0fedde9cfd3c8ad7d46b8031e to your computer and use it in GitHub Desktop.
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))))
my-narrowed-buffers))
(kill-buffer)))
(advice-add 'widen :after #'my-kill-buffer-on-widen)
(defun my-narrow-to-sexp ()
(interactive)
(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