erase-buffer插入新内容后save-excursion不回到之前的point

(save-excursion
  (erase-buffer)
  (insert data))

我这样为啥光标不能回去原来的point呢?data跟原来的内容差不多

(info “(elisp) Excursions”)

Warning: Ordinary insertion of text adjacent to the saved point value relocates the saved value, just as it relocates all markers. More precisely, the saved value is a marker with insertion type nil . See Marker Insertion Types. Therefore, when the saved point value is restored, it normally comes before the inserted text.

暂时使用 (point) 先拿到point,然后使用 goto-char 来解决了

save-excursion保存的是一个marker,当marker存在的上下文还存在的时候,save-excursion是可以正常工作的,但是当erase-buffer被调用之后,所有的文字全都被抹掉了,marker也就被替换掉了,所以需要手动存point的位置,然后再goto-char过去。

1 个赞