简体   繁体   English

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

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

When using the post-command-hook following copy-region-as-kill and the post-command-hook includes a test for region-active-p , that test returns positive because transient-mark-mode has not yet been returned to nil . 当使用post-command-hook以下copy-region-as-killpost-command-hook包括用于测试region-active-p因为测试返回正transient-mark-mode还没有被返回到nil

Is the post-command-hook designed to act upon what was in existence before this-command was executed, or should the post-command-hook be looking at the world as if this-command has already run its course? post-command-hook是设计用于执行this-command 之前存在的内容,还是post-command-hook应该看着这个世界,好像this-command已经运行了?


EDIT : My apologies for not specifying that I am using the latest version of Emacs Trunk. 编辑 :我很抱歉没有说明我使用的是最新版本的Emacs Trunk。 Here is the current function that performs the test: 以下是执行测试的当前函数:

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

Here is the message from Mx emacs-version : 以下是来自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 

Here is an example of the active region that hypothetically may need to be copied using copy-region-as-kill , and tested for while the post-command-hook is running. 下面是一个活动区域的示例,假设可能需要使用copy-region-as-kill ,并在post-command-hook运行时进行测试。 [That way, the new overlays can be placed with calculations assuming there is no active region.] In the context of this example, I need the test to return nil when this-command equals copy-region-as-kill and one way to accomplish that goal is to modify copy-region-as-kill to include (setq transient-mark-mode nil) at the tail end of the function. [那样,假设没有活动区域,新的叠加可以放在计算中。]在这个例子的上下文中,当this-command等于copy-region-as-kill时,我需要测试返回nil实现这个目标是修改copy-region-as-kill以在函数的尾端包含(setq transient-mark-mode nil) However, I hesitate to modify a staple function like copy-region-as-kill . 但是,我犹豫是否修改了像copy-region-as-kill这样的主要功能。 [It wouldn't make much sense (in my opinion) to include conditions that state if region-active-p and this-command equals copy-region-as-kill , then pretend the region isn't active.] [在我看来,包括条件,表明如果region-active-pthis-command等于copy-region-as-kill ,那么假装该区域不活动就没有多大意义。]

例
(source: lawlist.com ) (来源: lawlist.com

transient-mark-mode has nothing to do with it. transient-mark-mode与它无关。 Check deactivate-mark instead. 请检查deactivate-mark

To wit, you can't use region-active-p in a post-command-hook, but instead check the value of the deactivate-mark variable see docs . 也就是说,您不能在post-command-hook中使用region-active-p ,而是检查deactivate-mark变量的值,请参阅docs

In order to make sure there is no active region after running 为了确保在运行后没有活动区域

(deactivate-mark t)

should do the trick. 应该做的伎俩。 Why not finish the command in question with it, instead employing as a post-command-hook ? 为什么不用它来完成有问题的命令,而不是用作post-command-hook

INITIAL DRAFT (June 4, 2014): After looking at the documentation and the helpful comments and answers in this thread, I'm strongly leaning in favor of a new test specifically designed to be used inside the post-command-hook . 初始草案 (2014年6月4日):在查看了本文中的文档和有用的注释和答案之后,我非常倾向于专门设计用于在post-command-hook使用的新测试。 This will avoid modifying the core / staple functions of kill-region , copy-region-as-kill , and yank . 这将避免修改kill-regioncopy-region-as-killyank的核心/主要功能。 Since the new function will be included in my own library for the minor-mode, there is no reason why I can't base some of the basic calculations on said function. 由于新函数将包含在我自己的小模式库中,因此没有理由不能对所述函数进行一些基本计算。 The function entitled deactivate-mark in simple.el uses (setq mark-active nil) and (setq transient-mark-mode nil) , so there is no reason why I can't use them also in my own custom function. 功能名为deactivate-marksimple.el用途(setq mark-active nil)(setq transient-mark-mode nil) ,所以没有理由我不能也用他们自己的自定义功能。

  • Special thanks to @Andreas Röhler, @event_jr, and @Stefan -- greatly appreciated! 特别感谢@AndreasRöhler,@ event_jr和@Stefan--非常感谢!

EDIT (June 5, 2014): Revised initial draft to merely test conditions, rather than setting either of the variables ( mark-active or transient-mark-mode ) to nil . 编辑 (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