Emacs cursor move hook, like JavaScript mousemove event

I want to keep track of the current word in the current buffer. which the other (network) application will know about.

I could do this by sending a request every time the cursor moves, be it a press or scroll or an arrow or so on, i.e. anytime the position of the cursor in the buffer changes.

eg.

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

      

+3


source to share


1 answer


Use post-command-hook

this will run after every command including move commands.

Obviously this will fire more often than you want, so in your hook you can do something like keeping track of the last position you were at when the hook was fired and only run a network request if the current point is different from the last one like this :



(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)

      

+5


source







All Articles