繁体   English   中英

应该将`copy-region-as-kill`修改为包含`(setq transient-mark-mode nil)`?

[英]Should `copy-region-as-kill` be modified to include `(setq transient-mark-mode nil)`?

当使用post-command-hook以下copy-region-as-killpost-command-hook包括用于测试region-active-p因为测试返回正transient-mark-mode还没有被返回到nil

post-command-hook是设计用于执行this-command 之前存在的内容,还是post-command-hook应该看着这个世界,好像this-command已经运行了?


编辑 :我很抱歉没有说明我使用的是最新版本的Emacs Trunk。 以下是执行测试的当前函数:

(defun region-active-p ()
  (and transient-mark-mode mark-active (mark)))

以下是来自Mx emacs-version的消息:

GNU Emacs 24.4.50.1 (x86_64-apple-darwin10.8.0,
  NS appkit-1038.36 Version 10.6.8 (Build 10K549)) of 2014-06-01 on MP.local 

下面是一个活动区域的示例,假设可能需要使用copy-region-as-kill ,并在post-command-hook运行时进行测试。 [那样,假设没有活动区域,新的叠加可以放在计算中。]在这个例子的上下文中,当this-command等于copy-region-as-kill时,我需要测试返回nil实现这个目标是修改copy-region-as-kill以在函数的尾端包含(setq transient-mark-mode nil) 但是,我犹豫是否修改了像copy-region-as-kill这样的主要功能。 [在我看来,包括条件,表明如果region-active-pthis-command等于copy-region-as-kill ,那么假装该区域不活动就没有多大意义。]

例
(来源: lawlist.com

transient-mark-mode与它无关。 请检查deactivate-mark

也就是说,您不能在post-command-hook中使用region-active-p ,而是检查deactivate-mark变量的值,请参阅docs

为了确保在运行后没有活动区域

(deactivate-mark t)

应该做的伎俩。 为什么不用它来完成有问题的命令,而不是用作post-command-hook

初始草案 (2014年6月4日):在查看了本文中的文档和有用的注释和答案之后,我非常倾向于专门设计用于在post-command-hook使用的新测试。 这将避免修改kill-regioncopy-region-as-killyank的核心/主要功能。 由于新函数将包含在我自己的小模式库中,因此没有理由不能对所述函数进行一些基本计算。 功能名为deactivate-marksimple.el用途(setq mark-active nil)(setq transient-mark-mode nil) ,所以没有理由我不能也用他们自己的自定义功能。

  • 特别感谢@AndreasRöhler,@ event_jr和@Stefan--非常感谢!

编辑 (2014年6月5日):修改初始草案仅仅测试条件,而不是将任何一个变量( mark-activetransient-mark-mode )设置nil

(defun lawlist-region-active-p ()
"Custom test to determine whether the region is presently active; AND,
whether the region will be active when the `post-command-hook` finishes."
  (cond
    ((memq this-command '(
        self-insert-command
        delete-backward-char
        delete-forward-char
        kill-region
        delete-region
        copy-region-as-kill
        yank
        kill-word
        lawlist-copy-selected-region
        lawlist-kill-word
        lawlist-yank ))
      nil)
    ((and transient-mark-mode mark-active (mark)))))

(defun test-lawlist-region-active-p ()
  (cond
    ((lawlist-region-active-p)
      (message "ACTIVE region."))
    ((not (lawlist-region-active-p))
      (message "NOT active region."))))

(add-hook 'post-command-hook 'test-lawlist-region-active-p)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM