Function: narrow-to-region

narrow-to-region is an interactive function defined in editfns.c.

Signature

(narrow-to-region START END)

Documentation

Restrict editing in this buffer to the current region.

The rest of the text becomes temporarily invisible and untouchable but is not deleted; if you save the buffer in a file, the invisible text is included in the file. C-x n w (widen) makes all visible again. See also save-restriction.

When calling from Lisp, pass two arguments START and END: positions (integers or markers) bounding the text that should remain visible.

However, when restrictions have been set by with-restriction with a label, narrow-to-region can be used only within the limits of these restrictions. If the START or END arguments are outside these limits, the corresponding limit set by with-restriction is used instead of the argument. To gain access to other portions of the buffer, use without-restriction with the same label.

View in manual

Probably introduced at or before Emacs version 15.

Key Bindings

Source Code

// Defined in /usr/src/emacs/src/editfns.c
{
  EMACS_INT s = fix_position (start), e = fix_position (end);

  if (e < s)
    {
      EMACS_INT tem = s; s = e; e = tem;
    }

  if (!(BEG <= s && s <= e && e <= Z))
    args_out_of_range (start, end);

  Lisp_Object buf = Fcurrent_buffer ();
  if (! NILP (labeled_restrictions_peek_label (buf)))
    {
      /* Limit the start and end positions to those of the innermost
	 labeled restriction.  */
      Lisp_Object begv = labeled_restrictions_get_bound (buf, true, false);
      Lisp_Object zv = labeled_restrictions_get_bound (buf, false, false);
      eassert (! NILP (begv) && ! NILP (zv));
      ptrdiff_t begv_charpos = marker_position (begv);
      ptrdiff_t zv_charpos = marker_position (zv);
      if (s < begv_charpos) s = begv_charpos;
      if (s > zv_charpos) s = zv_charpos;
      if (e < begv_charpos) e = begv_charpos;
      if (e > zv_charpos) e = zv_charpos;
    }

  if (BEGV != s || ZV != e)
    current_buffer->clip_changed = 1;

  SET_BUF_BEGV (current_buffer, s);
  SET_BUF_ZV (current_buffer, e);

  if (PT < s)
    SET_PT (s);
  if (e < PT)
    SET_PT (e);
  /* Changing the buffer bounds invalidates any recorded current column.  */
  invalidate_current_column ();
  return Qnil;
}