【问题标题】:Emacs -- creating an interactive only quasi-post-command-hookEmacs——创建一个仅交互式的准后命令挂钩
【发布时间】:2014-05-03 07:22:32
【问题描述】:

我有兴趣创建一个基于用户定义标准运行的仅交互式准命令后挂钩,例如:

  • 向上(交互式);

  • 向下(交互式);

  • 左(交互式);

  • 右(交互式);

  • 将文本插入缓冲区的任何(交互式)键——例如,aA-zZ; 0-9;空间; !@#$%^&*()-+=.;、回车/回车、删除/退格等

  • 鼠标滚轮(上/下)(交互式)。

我相信 post-command-hook 包含更多,我想限制/控制何时激活该钩子。

任何关于如何创建这样一个钩子的指导将不胜感激。


2013 年 5 月 3 日:根据下面@phils 的回答草拟示例。

(add-hook 'post-command-hook 'quasi-post-command-hook)

(defvar quasi-this-command-functions '(next-line previous-line left-char right-char
  self-insert-command newline delete-backward-char delete-forward-char
  indent-for-tab-command mwheel-scroll lawlist-mwheel-scroll end-of-visual-line
  beginning-of-visual-line end-of-buffer beginning-of-buffer lawlist-forward-entity
  lawlist-backward-entity left-word right-word forward-word backward-word)
"Variable list of functions that trigger the `quasi-post-command-hook`.")

(defvar quasi-major-mode-inclusions '(text-mode emacs-lisp-mode)
"Variable list of major modes where the `quasi-post-command-hook` operates.")

(defun quasi-post-command-hook ()
  (unless (minibufferp)
    (when
      (and
        (memq major-mode quasi-major-mode-inclusions)
        (memq this-command quasi-this-command-functions))
      (message "this-command:  %s" this-command))))

【问题讨论】:

    标签: emacs elisp


    【解决方案1】:

    您可能想要测试this-command 变量。

    例如C-u M-x apropos-variable RET this RET:

    real-this-command
       This is like `this-command', except that commands should never
       modify it.
    this-command
       The command now being executed.
    this-command-keys-shift-translated
       Non-nil if the key sequence activating this command was
       shift-translated.
    this-original-command
       The command bound to the current key sequence before remapping.
    

    【讨论】:

      【解决方案2】:

      枚举你知道应该触发这个钩子的命令(使用this-command)当然是一种选择,但“它不能扩展”。如果您尝试描述这些命令彼此之间的共同点以及其他命令没有的共同点,也许您可​​以做得更好。

      【讨论】:

      • 这两组本质上是:point由于用户按下交互键而手动移动;或者,缓冲区被手动向上或向下滚动。
      • 所以也许你可以记住lawlist-last-point中的上一个点,然后将它与当前点进行比较,看看最后一个命令是否移动了点。
      • 感谢您的建议 - 如果时间允许,我将在接下来的几天内继续处理此问题,我会向您报告。
      猜你喜欢
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      相关资源
      最近更新 更多