[英]Should `copy-region-as-kill` be modified to include `(setq transient-mark-mode nil)`?
當使用post-command-hook
以下copy-region-as-kill
和post-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-p
和this-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-region
, copy-region-as-kill
和yank
的核心/主要功能。 由於新函數將包含在我自己的小模式庫中,因此沒有理由不能對所述函數進行一些基本計算。 該功能名為deactivate-mark
在simple.el
用途(setq mark-active nil)
和(setq transient-mark-mode nil)
,所以沒有理由我不能也用他們自己的自定義功能。
編輯 (2014年6月5日):修改初始草案僅僅測試條件,而不是將任何一個變量( mark-active
或transient-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.