一个类似于JavaScript的mousemove事件的Emacs光标移动挂钩。

4
我希望追踪当前缓冲区中的“当前单词”,以便另一个(联网的)应用程序能够意识到。
我可以通过在光标移动时发送请求来实现此目的,无论是通过点击、滚动、箭头键等方式。也就是说,每当缓冲区中光标的位置发生变化时都会发送请求。
例如:
(add-hook 'cursor-move-hook 'post-request-with-current-word)

1
文档会让我相信你可以使用 point-enteredpoint-left 属性来实现,但事实并非如此。有关详细信息,请参见此错误报告。您应该通过确保缓冲区中每个潜在的“当前单词”具有不同的文本属性,以便触发 point-entered 钩子来使其正常工作,但我不确定这在性能方面是否实用... - Carl Groner
1个回答

6

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

这是一个好主意。我认为我的状态应该同时包括行位置和缓冲区名称。 - sam boosalis
1
buffer-name 已经得到处理,因为 last-post-command-position 是缓冲区本地的,所以比较总是针对特定缓冲区。每个缓冲区都有一个不同的 last-post-command-position 值。 - Jordon Biondo
阅读 https://www.gnu.org/software/emacs/manual/html_node/elisp/Command-Overview.html#Command-Overview,并向钩子添加记录器,似乎命令可以是以下之一:键盘/鼠标事件、交互式函数、Emacs 服务器请求。这是否完整? - sam boosalis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接