Make narrow/widen commands incremental

Doom's narrow/widen commands will now narrow/widen incrementally (using
indirect buffer clones). If the prefix arg is passed to the widen
command, kill all indirect buffers and widen the parent buffer.
This commit is contained in:
Henrik Lissner 2019-09-13 01:58:27 -04:00
parent c3997730dd
commit 8ad8b5d8ad
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -169,13 +169,14 @@ OPACITY is an integer between 0 to 100, inclusive."
100)))) 100))))
(set-frame-parameter nil 'alpha opacity)) (set-frame-parameter nil 'alpha opacity))
(defvar doom--narrowed-base-buffer nil)
;;;###autoload ;;;###autoload
(defun doom/narrow-buffer-indirectly (beg end &optional clone-p) (defun doom/narrow-buffer-indirectly (beg end &optional clone-p)
"Restrict editing in this buffer to the current region, indirectly. "Restrict editing in this buffer to the current region, indirectly.
This creates an indirect clone of the buffer, so that the narrowing doesn't This recursively creates indirect clones of the current buffer so that the
affect other windows displaying the same buffer. Call narrowing doesn't affect other windows displaying the same buffer. Call
`doom/widen-indirectly-narrowed-buffer' to undo it. `doom/widen-indirectly-narrowed-buffer' to undo it (incrementally).
Inspired from http://demonastery.org/2013/04/emacs-evil-narrow-region/" Inspired from http://demonastery.org/2013/04/emacs-evil-narrow-region/"
(interactive (interactive
@ -186,26 +187,36 @@ Inspired from http://demonastery.org/2013/04/emacs-evil-narrow-region/"
(setq beg (line-beginning-position) (setq beg (line-beginning-position)
end (line-end-position))) end (line-end-position)))
(deactivate-mark) (deactivate-mark)
(with-current-buffer (switch-to-buffer (clone-indirect-buffer nil nil)) (let ((orig-buffer (current-buffer)))
(narrow-to-region beg end))) (with-current-buffer (switch-to-buffer (clone-indirect-buffer nil nil))
(narrow-to-region beg end)
(setq-local doom--narrowed-base-buffer orig-buffer))))
;;;###autoload ;;;###autoload
(defun doom/widen-indirectly-narrowed-buffer (&optional dontkill) (defun doom/widen-indirectly-narrowed-buffer (&optional arg)
"Widens narrowed indirect buffer, created with "Widens narrowed buffers.
Mainly used for indirectly narrowed buffers created by This command will incrementally kill indirect buffers (under the assumption they
`doom/narrow-buffer-indirectly', but will work with `narrow-to-region' and were created by `doom/narrow-buffer-indirectly') and switch to their base
others." buffer.
If ARG, then kill all indirect buffers, return the base buffer and widen it.
If the current buffer is not an indirect buffer, it is `widen'ed."
(interactive "P") (interactive "P")
(unless (buffer-narrowed-p) (unless (buffer-narrowed-p)
(user-error "Buffer isn't narrowed")) (user-error "Buffer isn't narrowed"))
(let ((buffers (list (current-buffer))) (let ((orig-buffer (current-buffer))
buffer) (base-buffer doom--narrowed-base-buffer))
(while (setq buffer (buffer-base-buffer buffer)) (cond ((or (not base-buffer)
(when (buffer-live-p buffer) (not (buffer-live-p base-buffer)))
(push buffer buffers))) (widen))
(when buffers (arg
(switch-to-buffer (car buffers)) (let ((buffer orig-buffer)
(unless dontkill (buffers-to-kill (list orig-buffer)))
(mapc #'kill-buffer (cdr buffers)))) (while (setq buffer (buffer-local-value 'doom--narrowed-base-buffer buffer))
(widen))) (push buffer buffers-to-kill))
(switch-to-buffer (buffer-base-buffer))
(mapc #'kill-buffer (remove (current-buffer) buffers-to-kill))))
((switch-to-buffer base-buffer)
(kill-buffer orig-buffer)))))