2012-07-21 89 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