gpt4 book ai didi

emacs - 在 transient 标记模式下如何在 elisp 中使区域 transient

转载 作者:行者123 更新时间:2023-12-01 12:41:14 26 4
gpt4 key购买 nike

我写了一个 elisp 宏,在 transient-mark-mode 中保留区域:

(defmacro keep-region (command)
"Wrap command in code that saves and restores the region"
(letrec ((command-name (symbol-name command))
(advice-name (concat command-name "-keep-region")))
`(progn
(defadvice ,command (around ,(intern advice-name))
(let (deactivate-mark)
(save-excursion
ad-do-it)))
(ad-activate (quote ,command)))))

(keep-region replace-string)
(keep-region replace-regexp)

这会为使用 keep-region 宏建议的命令保留区域;当您想在选定 block 中进行多次替换时非常有用。

问题是,在运行一个被建议使用这个宏的命令后,该区域失去了它的 transient 特性;随后的移动命令会扩展该区域,而不是取消选择它。

如何以编程方式重新启用标记区域的瞬变?

最佳答案

来自 C-h f transient-mark-mode:

Transient Mark mode is a global minor mode. When enabled, the region is highlighted whenever the mark is active. The mark is "deactivated" by changing the buffer, and after certain other operations that set the mark but whose main purpose is something else--for example, incremental search, <, and >.

因此,exchange-point-and-mark 之后的activate-mark 应该恢复标记的 transient 特性。

不过,我不确定您为什么要在这里使用 exchange-point-and-mark,以及为什么要调用它两次。在我看来,只需将 (point)(mark) 保存在 let 中 - 在 ad-do- 之后绑定(bind)并恢复它们它会更容易。 push-markpop-mark 也可能有帮助,因为后者无论如何都会自动重新激活标记。

关于emacs - 在 transient 标记模式下如何在 elisp 中使区域 transient ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24488766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com