如何在按下前缀键 Ch 的同时执行 lisp 函数,然后让 Emacs 继续正常处理 Ch?

发布于 2024-12-23 13:29:44 字数 66 浏览 1 评论 0原文

我想在按下前缀键 Ch 的同时执行 lisp 函数,然后让 Emacs 继续正常处理 Ch。 我该怎么做呢? 谢谢你!

I want to execute a lisp function while pressing prefix key C-h and then let Emacs continue the normal processing of C-h.
How can I do it?
Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

氛圍 2024-12-30 13:29:44

评估后

(key-binding [(control h)])

我发现命令绑定是help-command。您可以使用“around”默认设置来运行您的代码。请参阅手册

Evaluating

(key-binding [(control h)])

I found out the command bound is help-command. You can use an "around" defadvice to run your code. See manual.

春风十里 2024-12-30 13:29:44
(defvar smart-ime--state 'normal)
(defvar smart-ime--debug nil)
(defvar smart-ime--ena-prefix-override-keymap nil)
(defvar smart-ime--prefix-override-keymap (make-sparse-keymap))
(defvar smart-ime--keymaps-initialized nil)
(defvar smart-ime--keymap-alist
  `(
    (smart-ime--ena-prefix-override-keymap . ,smart-ime--prefix-override-keymap)
    )
  )

(defun smart-ime--init-keymaps ()
  (define-key smart-ime--prefix-override-keymap [(control x)] 'smart-ime--prefix-override-handler)
  (define-key smart-ime--prefix-override-keymap [(control c)] 'smart-ime--prefix-override-handler)
  (define-key smart-ime--prefix-override-keymap [(control h)] 'smart-ime--prefix-override-handler)
)

(defun smart-ime--prefix-override-handler (arg)
  (interactive "P")
  (smart-ime--prefix-override-replay arg))

;; the most important part    
(defun smart-ime--prefix-override-replay (arg)
  (let* ((keys (this-command-keys))
         (i (length keys))
         (key (aref keys (1- i))))

    (ime-save-and-set-status 0)
    (add-hook 'post-command-hook 'smart-ime--post-command-handler)
    (setq smart-ime--state 'prefix)
    (setq smart-ime--ena-prefix-override-keymap nil)

    ;; Don't record this command
    (setq this-command last-command)
    ;; Restore the prefix arg
    (setq prefix-arg arg)
    (reset-this-command-lengths)
    ;; Push the key back on the event queue
    (setq unread-command-events (cons key unread-command-events))))


(defun smart-ime--post-command-handler-1 ()
  (cond ((eq smart-ime--state 'prefix)
         (setq smart-ime--state 'sequence))


        ((eq smart-ime--state 'sequence)
         (ime-restore-status)
         (setq smart-ime--ena-prefix-override-keymap t)
         (setq smart-ime--state 'normal)

         (remove-hook 'post-command-hook 'smart-ime--post-command-handler)))

        (t
         (error "error state")))


(defun smart-ime--post-command-handler ()
  (when smart-ime-mode
    (condition-case nil
        (smart-ime--post-command-handler-1)
      (error nil))))


(define-minor-mode smart-ime-mode
  "Toggle Smart IME mode."
  :init-value nil
  :lighter " SmartIME"
  :global t

  (unless smart-ime--keymaps-initialized
    (smart-ime--init-keymaps)
    (setq smart-ime--keymaps-initialized t))

  (unless smart-ime-mode
    (remove-hook 'post-command-hook 'smart-ime--post-command-handler))


  (if (not smart-ime-mode)
      (setq emulation-mode-map-alists (delq 'smart-ime--keymap-alist emulation-mode-map-alists))
    (add-to-ordered-list 'emulation-mode-map-alists 'smart-ime--keymap-alist 400)
    (setq smart-ime--ena-prefix-override-keymap t))

  )

;;; Announce

(provide 'smart-ime)
(defvar smart-ime--state 'normal)
(defvar smart-ime--debug nil)
(defvar smart-ime--ena-prefix-override-keymap nil)
(defvar smart-ime--prefix-override-keymap (make-sparse-keymap))
(defvar smart-ime--keymaps-initialized nil)
(defvar smart-ime--keymap-alist
  `(
    (smart-ime--ena-prefix-override-keymap . ,smart-ime--prefix-override-keymap)
    )
  )

(defun smart-ime--init-keymaps ()
  (define-key smart-ime--prefix-override-keymap [(control x)] 'smart-ime--prefix-override-handler)
  (define-key smart-ime--prefix-override-keymap [(control c)] 'smart-ime--prefix-override-handler)
  (define-key smart-ime--prefix-override-keymap [(control h)] 'smart-ime--prefix-override-handler)
)

(defun smart-ime--prefix-override-handler (arg)
  (interactive "P")
  (smart-ime--prefix-override-replay arg))

;; the most important part    
(defun smart-ime--prefix-override-replay (arg)
  (let* ((keys (this-command-keys))
         (i (length keys))
         (key (aref keys (1- i))))

    (ime-save-and-set-status 0)
    (add-hook 'post-command-hook 'smart-ime--post-command-handler)
    (setq smart-ime--state 'prefix)
    (setq smart-ime--ena-prefix-override-keymap nil)

    ;; Don't record this command
    (setq this-command last-command)
    ;; Restore the prefix arg
    (setq prefix-arg arg)
    (reset-this-command-lengths)
    ;; Push the key back on the event queue
    (setq unread-command-events (cons key unread-command-events))))


(defun smart-ime--post-command-handler-1 ()
  (cond ((eq smart-ime--state 'prefix)
         (setq smart-ime--state 'sequence))


        ((eq smart-ime--state 'sequence)
         (ime-restore-status)
         (setq smart-ime--ena-prefix-override-keymap t)
         (setq smart-ime--state 'normal)

         (remove-hook 'post-command-hook 'smart-ime--post-command-handler)))

        (t
         (error "error state")))


(defun smart-ime--post-command-handler ()
  (when smart-ime-mode
    (condition-case nil
        (smart-ime--post-command-handler-1)
      (error nil))))


(define-minor-mode smart-ime-mode
  "Toggle Smart IME mode."
  :init-value nil
  :lighter " SmartIME"
  :global t

  (unless smart-ime--keymaps-initialized
    (smart-ime--init-keymaps)
    (setq smart-ime--keymaps-initialized t))

  (unless smart-ime-mode
    (remove-hook 'post-command-hook 'smart-ime--post-command-handler))


  (if (not smart-ime-mode)
      (setq emulation-mode-map-alists (delq 'smart-ime--keymap-alist emulation-mode-map-alists))
    (add-to-ordered-list 'emulation-mode-map-alists 'smart-ime--keymap-alist 400)
    (setq smart-ime--ena-prefix-override-keymap t))

  )

;;; Announce

(provide 'smart-ime)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文