Create a gist now

Instantly share code, notes, and snippets.

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.
(defun my-kill-buffer-on-widen ()
(kill-buffer)
(advice-remove 'widen #'my-kill-buffer-on-widen))
(defun my-narrow-to-sexp ()
(interactive)
(let ((orig-point (point))
start end)
(cond ((region-active-p)
(setq deactivate-mark t)
(unless (< (point) (mark))
(exchange-point-and-mark))
(setq start (point)
end (mark)))
(t
(setq end (progn (forward-sexp) (point))
start orig-point)
(goto-char orig-point)))
(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)
(advice-add 'widen :after #'my-kill-buffer-on-widen)
(switch-to-buffer clone-buffer)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment