Emacs光标移动挂钩,类似于JavaScript鼠标移动事件



我想跟踪当前缓冲区中的"当前字"。另一个(联网的)应用程序将知道。

我可以通过在每次光标移动时发送请求来做到这一点,无论是通过点击、滚动还是箭头等,也就是说,每当光标在缓冲区中的位置发生变化时。

例如

(add-hook 'cursor-move-hook 'post-request-with-current-word)

使用post-command-hook,它将在每个命令(包括移动命令)之后运行。

显然,这会比你想要的更频繁地触发,所以在你的钩子中,你可以做一些事情,比如跟踪钩子运行时你所在的最后一个位置,并且只有在当前点与上一个点不同的情况下才会触发网络请求,比如:

(defvar last-post-command-position 0
  "Holds the cursor position from the last run of post-command-hooks.")
(make-variable-buffer-local 'last-post-command-position)
(defun do-stuff-if-moved-post-command ()
  (unless (equal (point) last-post-command-position)
    (let ((my-current-word (thing-at-point 'word)))
      ;; replace (message ...) with your code
      (message "%s" my-current-word)))
  (setq last-post-command-position (point)))
(add-to-list 'post-command-hook #'do-stuff-if-moved-post-command)

最新更新