Created
September 11, 2014 17:48
-
-
Save trillioneyes/602c3e27dbaa808bbe4f to your computer and use it in GitHub Desktop.
Define advice for `narrow-to-region` so that it will automatically create an indirect buffer
This file contains 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
;; Make narrowing nicer | |
; this makes sure we have a way to get the name of the current function | |
(which-function-mode) | |
(defadvice narrow-to-defun (before narrow-in-new-buffer activate) | |
"When using `narrow-to-defun', narrow in an indirect buffer whose | |
name is the original buffer's name augmented by the function | |
being focused." | |
(let ((parent-name (buffer-name)) | |
(name (concatenate 'string | |
(buffer-name) | |
":" | |
(which-function)))) | |
(make-indirect-buffer (buffer-name) name t) | |
(switch-to-buffer name) | |
(make-local-variable 'destroy-and-switch-to-on-widen) | |
(setq destroy-and-switch-to-on-widen parent-name))) | |
(defadvice widen (around destroy-indirect-narrow-buffer activate) | |
(if (and (boundp 'destroy-and-switch-to-on-widen) destroy-and-switch-to-on-widen) | |
(let ((next-buffer destroy-and-switch-to-on-widen)) | |
(kill-buffer) | |
(switch-to-buffer next-buffer)) | |
ad-do-it)) |
you only need to require it to use, activation is not necesary so you can do (require 'which-func)
Even if the module is loaded, I had some problems with it failing to automatically build the imenu index the first time and I'm not sure why. I blindly activated which-function-mode to hopefully force the index to be built. Maybe it was just a fluke-y error though.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hmmm, there should probably be a way to use
which-function
without requiringwhich-function-mode
to be activated (which displays the current function name in the modeline). I am not very motivated to find it though; I usually usewhich-function-mode
anyway.