Skip to content

Instantly share code, notes, and snippets.

@trillioneyes
Created September 11, 2014 17:48
Show Gist options
  • Save trillioneyes/602c3e27dbaa808bbe4f to your computer and use it in GitHub Desktop.
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
;; 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))
@trillioneyes
Copy link
Author

hmmm, there should probably be a way to use which-function without requiring which-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 use which-function-mode anyway.

@PuercoPop
Copy link

you only need to require it to use, activation is not necesary so you can do (require 'which-func)

@trillioneyes
Copy link
Author

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