8

Is there a convenient way to restore the previous restriction? So after the following sequence of events:

  • Mark a region
  • Call narrow-to-region and do some work.
  • Call widen to jump around and look for something

I'd like to narrow to the original region again without having to go mark it again.

  • This is tricky because of course the contents of the buffer could change between the call to widen and the call to a hypothetical renarrow command. Perhaps the best approach is to save the boundaries of the region somewhere and have a command to mark the last saved region. – glucas Mar 18 '15 at 15:53
2

Instead of narrowing the main buffer, you could open a new Indirect Buffer and narrow that:

(defun indirect-region (beg end name)
  "Open a new named indirect buffer of the current buffer,
narrowed to region [BEG, END]."
  (interactive "r\nsname of narrowed buffer: ")
  (let ((new-buff
         (make-indirect-buffer (current-buffer)
                               (generate-new-buffer-name name)
                               t)))
    (switch-to-buffer new-buff nil t)
    (narrow-to-region beg end)))

This will open a new indirect buffer of your current buffer narrowed to your current region, editing this buffer is just like editing the main buffer, and you'll be able to switch back and forth between them.

Like so:

enter image description here

You could expand on this with nice features like

  • automatically narrowing to the current function
  • naming the new buffer after the current function
  • a keybinding to jump between a function's indirect buffer and the main buffer
  • not creating a new indirect buffer for functions that already have one open

That is all up to you though.

  • Thanks, this may be the way to go. I could see the inverse as well: from a narrowed buffer, create a widened indirect buffer to look up something. – glucas Mar 18 '15 at 16:52
7

Sorry I didn't see your question sooner.

This is precisely one of the reasons for library Zones (zones.el).

When you narrow the buffer, each narrowing is added to a ring of narrowings, which includes the full (unnarrowed) buffer. The narrowings can be nested, but they need not be. They can be any regions at all.

You can cycle among the narrowings (using C-x n x x x...). You can go directly to the Nth previous narrowing by using a numeric prefix arg; e.g., C-3 C-x n x. Plain C-u widens completely (same as C-x n w or cycling to the unnarrowed ring entry). C-0 empties the ring.

The mode line tells you which narrowing (buffer restriction) is current.

You can even save the ring of current buffer restrictions persistently and restore it later. If you use library Bookmark+ then you can bookmark a ring of restrictions and then restore it later by jumping to the bookmark.

For more information, see Multiple Narrowings.


See also Narrow Indirect (library narrow-indirect.el). It lets you easily create any number of indirect buffers that are clones of the main buffer and that are narrowed to particular portions of it.

  • @phils: Thx; done. Actually, it was mentioned on the Zones page, but I've added it to the Multiple Narrowings page too. – Drew Sep 8 '15 at 2:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.