2012-07-21 86 views
3

当我在Emacs中搜索“save-restriction”的描述时,它有一个关于“缓冲区限制”的句子 - 我已经包含了下面的完整描述。这个词是什么意思?保存限制如何工作以及何时应该考虑使用它?“缓冲区限制”在保存限制中意味着什么?

(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. 

回答

2

保存限制被narrow- *函数用来保存当前缓冲区,然后隐藏它,以便能够恢复它。 'save-restriction'记忆所有的'buffer'数据结构,特别是point-min,point-max,point-max-marker等。例如,在窄函数修改缓冲区的可见性之前,它会记住旧的配置,以便能够使用widen()来恢复它。

+0

THX队友,所以他们应该总是走到一起呢?我的意思是narrow- *和save-restriction? – Daniel 2012-07-21 21:40:52

+0

我通过编辑prev回答。回答。 – alinsoar 2012-07-21 22:05:00

+0

一般来说,我想在我的代码中保存缓冲区时使用save-excursion。 – alinsoar 2012-07-21 22:27:48

9

除非您的代码的目的是修改限制,当前缓冲区,点或窗口配置,那么您应该使用适当的save方法来记住状态并自动为您恢复。

  • save-current-buffer保存当前缓冲区,以便您可以切换到另一个缓冲区而不必记住切换回来。
  • save-excursion保存当前缓冲区及其当前点并标记,因此您可以移动点而不必记住恢复它。
  • save-restriction保存限制,以便您可以缩小或widen而不必记住恢复它。
  • save-window-excursion保存帧中所有窗口的完整配置,除了当前缓冲区中的点的值。

(旁注:当我最后一次使用save-window-excursion没有window-configuration-p方法。)

+0

非常好的解释.. – Arash 2012-07-21 22:31:35

+1

还有'保存选择窗口',它可以“保存和恢复所选窗口以及每个窗口中的选定窗口”以及“还保存和恢复当前缓冲区”。 – phils 2012-07-21 23:41:35

+0

@phils非常有用 – 2017-07-27 12:38:03