Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "buffer's restrictions" mean in save-restriction?

Tags:

emacs

elisp

When I search for description of "save-restriction" in Emacs, it has a sentence about "The buffer's restrictions" - I have included the complete description below. What does this term mean? how does save-restriction work and when it should be used considering this?

(save-restriction &rest BODY) 

Execute BODY, saving and restoring current buffer's restrictions.
The buffer's restrictions make parts of the beginning and end invisible. 
(They are set up with `narrow-to-region' and eliminated with `widen'.)
This special form, `save-restriction', saves the current buffer's restrictions
when it is entered, and restores them when it is exited.
So any `narrow-to-region' within BODY lasts only until the end of the form.
The old restrictions settings are restored
even in case of abnormal exit (throw or error).

The value returned is the value of the last form in BODY.
like image 947
Daniel Avatar asked Jul 21 '12 21:07

Daniel


2 Answers

Unless the purpose of your code is to modify the restriction, current buffer, point or window configuration, then you should use the appropriate save method to remember the state and automatically restore it for you.

  • save-current-buffer saves the current buffer, so that you can switch to another buffer without having to remember to switch back.
  • save-excursion saves the current buffer and its current point and mark too, so you can move point without having to remember to restore it.
  • save-restriction saves the restriction so that you can narrow or widen it without having to remember to restore it.
  • save-window-excursion saves the complete configuration of all the windows on the frame, except for the value of point in the current buffer.

(Side note: when I last used save-window-excursion there was no window-configuration-p method.)

like image 119
Neil Avatar answered Nov 02 '22 23:11

Neil


save-restriction is used by narrow-* functions to save the current buffer , before to hide it, in order to be able to restore it.

'save-restriction' memorizes all 'buffer' data strucuture , in particular point-min, point-max, point-max-marker, etc . For example, before a narrow-function modifies the visibility of a buffer, it memorizes the old configuration, in order to be able to restore it using widen().

like image 34
alinsoar Avatar answered Nov 03 '22 01:11

alinsoar