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.

glucas
  • 20,175
  • 1
  • 51
  • 83
  • 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 Answers2

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • @phils: Thx; done. Actually, it was mentioned on the [Zones](http://www.emacswiki.org/emacs/Zones) page, but I've added it to the [Multiple Narrowings](http://www.emacswiki.org/emacs/MultipleNarrowings) page too. – Drew Sep 08 '15 at 02:19
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.

Jordon Biondo
  • 12,332
  • 2
  • 41
  • 62
  • 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