如何标记(在函数内),做东西(设置其他标记),然后返回原始标记

时间:2013-08-17 20:59:28

标签: emacs elisp

有人可以给我一个设置标记的函数示例,然后做一些在缓冲区的其他地方设置其他标记的东西,然后返回到在函数开头标记的原始位置。

默认情况下启用

transient-mark-mode。我尝试使用(activate-mark)然后(deactivate-mark)设置标记以将标记推送到mark-ring,然后我的函数在缓冲区中移动并存档todo并执行一些组织内容并暂停一个{ {1}}(在todo存档的新位置)让我看到所有内容都已正确完成,然后我使用read-event返回到所有开始的位置。但是,(set-mark-command t)并没有让我回到函数开头的原始标记。相反,(set-mark-command t)将我带到另一个标记,该标记在函数运行时无意中设置在其他位置。

(set-mark-command t)

2 个答案:

答案 0 :(得分:1)

听起来save-excursion正是您正在寻找的东西 - 它会保存您在缓冲区中的位置(以及其他信息),执行正文并返回原始位置。

答案 1 :(得分:1)

这也可能有用:您可能 NOT 想要在Lisp代码中设置标记。

这就是Elisp手册中关于set-mark所说的内容:

 -- Function: set-mark position

 This function sets the mark to POSITION, and activates the mark.
 The old value of the mark is _not_ pushed onto the mark ring.

 *Please note:* Use this function only if you want the user to see
 that the mark has moved, and you want the previous mark position to
 be lost.  Normally, when a new mark is set, the old one should go
 on the `mark-ring'.  For this reason, most applications should use
 `push-mark' and `pop-mark', not `set-mark'.

 Novice Emacs Lisp programmers often try to use the mark for the
 wrong purposes.  The mark saves a location for the user's
 convenience.  An editing command should not alter the mark unless
 altering the mark is part of the user-level functionality of the
 command.  (And, in that case, this effect should be documented.)
 To remember a location for internal use in the Lisp program, store
 it in a Lisp variable.  For example:

      (let ((beg (point)))
        (forward-line 1)
        (delete-region beg (point))).

这就是set-mark-commandpush-mark的文字字符串所说的内容:

Novice Emacs Lisp programmers often try to use the mark for the wrong
purposes.  See the documentation of `set-mark' for more information.