# MultipleNarrowings

## Problem: Narrowing Is Fine-Grained, But Widening Is Not

Did you ever narrow a buffer to some restriction (region), and then later narrow it some more? Ever want to then widen back to the first (larger) restriction, rather than just widening to the whole buffer? You cannot.

Narrowing always narrows to the current region, which can be anywhere and have any size up to the buffer size. But command `‘widen’` (`‘C-x n w’`) always restores the full buffer. There can be many different narrowings (one at a time), but there is only one widening: the whole buffer.

Library Wide-N solves this problem. It gives you the same fine-grained behavior for widening as for narrowing. In fact, the possible widening targets are the same restrictions that you created by previous narrowings (plus the whole buffer). Instead of only a single widening, you have N of them.

## A Ring of Narrowings

Library Wide-N advises `‘narrow-to-region’`, the basic narrowing command, so that each time you narrow a buffer interactively the buffer restriction is recorded in a buffer-local ring. You can return to any of these narrowings selectively at any time.

`‘C-x n x’` is bound to command `‘wide-n-repeat’`, which restores the last recorded narrowing for the current buffer.

The narrowings can be nested, but they need not be. They can be any regions at all.

• You can repeat the `‘x’` to cycle quickly among narrowings: `‘C-x n x x x’`
• You can go directly to the Nth previous narrowing by using a numeric prefix argument. (This is the N in Wide-N.) If you use a negative prefix arg, then (its absolute value is used and) the latest entries through the abs(N)th one are popped off the ring. Thus, if you use only negative args then the ring acts like a stack.
• You can widen the buffer completely by using a plain prefix arg (`‘C-u’`) or a zero prefix arg (`‘C-0’`). A zero prefix arg also resets (empties) the ring completely for the current buffer.
• The whole buffer (widened completely) is also at the end of the ring, so you can cycle to it.
• You can also use `‘C-x n w’` (standard command `‘widen’`) to widen the buffer completely, as usual.

Emacs markers are used to record restriction limits, so the same restriction is available even if you modify its context. If for any reason `‘wide-n-restrictions’` ever has any entries that use buffer positions (numbers) instead of markers, invoking `‘wide-n’` corrects this by changing the positions to markers.

This means that you can serialize `‘wide-n-restrictions’`, converting all markers to positions, save the value persistently, and restore it later. Library Bookmark+’ (bookmark+.el) does this in order to let you bookmark and restore a list of restrictions.

In normal use, only the interactive use of commands `‘narrow-to-region’`, `‘narrow-to-defun’`, and `‘narrow-to-page’` is affected by this library. When these functions are called non-interactively there is normally no change to the value of variable `‘wide-n-restrictions’`. However, if for some reason you want to add entries to the restrictions ring when narrowing with some EmacsLisp code (i.e. non-interactively), you can do so by binding variable `‘wide-n-push-anyway-p’` around the narrowing call.