一个Emacs游标移动钩子,就像JavaScript mousemove事件一样

时间:2014-10-24 09:00:14

标签: emacs

我想跟踪"当前的字词"在当前缓冲区中。另一个(联网)应用程序将会注意到。

我可以通过每次光标移动时发送请求来完成此操作,无论是通过点击还是滚动还是箭头等,即光标在缓冲区中的位置发生变化。

e.g。

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

1 个答案:

答案 0 :(得分:5)

使用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)