Narrowing means limiting the text addressable by SXEmacs editing commands to a limited range of characters in a buffer. The text that remains addressable is called the accessible portion of the buffer.
Narrowing is specified with two buffer positions which become the beginning and end of the accessible portion. For most editing commands and most SXEmacs primitives, these positions replace the values of the beginning and end of the buffer. While narrowing is in effect, no text outside the accessible portion is displayed, and point cannot move outside the accessible portion.
Values such as positions or line numbers, which usually count from the beginning of the buffer, do so despite narrowing, but the functions which use them refuse to operate on text that is inaccessible.
The commands for saving buffers are unaffected by narrowing; they save the entire buffer regardless of any narrowing.
This function sets the accessible portion of buffer to start at start and end at end. Both arguments should be character positions. buffer defaults to the current buffer if omitted.
In an interactive call, start and end are set to the bounds of the current region (point and the mark, with the smallest first).
This function sets the accessible portion of the current buffer to include just the current page. An optional first argument move-count non-
nilmeans to move forward or backward by move-count pages and then narrow. The variablepage-delimiterspecifies where pages start and end (see Standard Regexps).In an interactive call, move-count is set to the numeric prefix argument.
This function cancels any narrowing in buffer, so that the entire contents are accessible. This is called widening. It is equivalent to the following expression:
(narrow-to-region 1 (1+ (buffer-size)))buffer defaults to the current buffer if omitted.
This special form saves the current bounds of the accessible portion, evaluates the body forms, and finally restores the saved bounds, thus restoring the same state of narrowing (or absence thereof) formerly in effect. The state of narrowing is restored even in the event of an abnormal exit via
throwor error (see Nonlocal Exits). Therefore, this construct is a clean way to narrow a buffer temporarily.The value returned by
save-restrictionis that returned by the last form in body, ornilif no body forms were given.Caution: it is easy to make a mistake when using the
save-restrictionconstruct. Read the entire description here before you try it.If body changes the current buffer,
save-restrictionstill restores the restrictions on the original buffer (the buffer whose restrictions it saved from), but it does not restore the identity of the current buffer.
save-restrictiondoes not restore point and the mark; usesave-excursionfor that. If you use bothsave-restrictionandsave-excursiontogether,save-excursionshould come first (on the outside). Otherwise, the old point value would be restored with temporary narrowing still in effect. If the old point value were outside the limits of the temporary narrowing, this would fail to restore it accurately.The
save-restrictionspecial form records the values of the beginning and end of the accessible portion as distances from the beginning and end of the buffer. In other words, it records the amount of inaccessible text before and after the accessible portion.This method yields correct results if body does further narrowing. However,
save-restrictioncan become confused if the body widens and then make changes outside the range of the saved narrowing. When this is what you want to do,save-restrictionis not the right tool for the job. Here is what you must use instead:(let ((start (point-min-marker)) (end (point-max-marker))) (unwind-protect (progn body) (save-excursion (set-buffer (marker-buffer start)) (narrow-to-region start end))))Here is a simple example of correct use of
save-restriction:---------- Buffer: foo ---------- This is the contents of foo This is the contents of foo This is the contents of foo-!- ---------- Buffer: foo ---------- (save-excursion (save-restriction (goto-char 1) (forward-line 2) (narrow-to-region 1 (point)) (goto-char (point-min)) (replace-string "foo" "bar"))) ---------- Buffer: foo ---------- This is the contents of bar This is the contents of bar This is the contents of foo-!- ---------- Buffer: foo ----------