* lisp/emacs-lisp/cursor-sensor.el (cursor-sensor--detect): Change last fix

Make sure we always work in the selected-window's buffer.
This commit is contained in:
Stefan Monnier 2020-02-24 09:55:09 -05:00
parent 3bce7ec382
commit e74fb4688b

View file

@ -141,61 +141,63 @@ By convention, this is a list of symbols where each symbol stands for the
;;; Detect cursor movement. ;;; Detect cursor movement.
(defun cursor-sensor--detect (&optional window) (defun cursor-sensor--detect (&optional window)
(unless cursor-sensor-inhibit (with-current-buffer (window-buffer window)
(let* ((point (window-point window)) (unless cursor-sensor-inhibit
;; It's often desirable to make the cursor-sensor-functions property (let* ((point (window-point window))
;; non-sticky on both ends, but that means get-pos-property might ;; It's often desirable to make the
;; never see it. ;; cursor-sensor-functions property non-sticky on both
(new (and (eq (current-buffer) (window-buffer)) ;; ends, but that means get-pos-property might never
(or (get-char-property point 'cursor-sensor-functions) ;; see it.
(unless (<= (point-min) point) (new (or (get-char-property point 'cursor-sensor-functions)
(get-char-property (1- point) 'cursor-sensor-functions))))) (unless (<= (point-min) point)
(old (window-parameter window 'cursor-sensor--last-state)) (get-char-property (1- point)
(oldposmark (car old)) 'cursor-sensor-functions))))
(oldpos (or (if oldposmark (marker-position oldposmark)) (old (window-parameter window 'cursor-sensor--last-state))
(point-min))) (oldposmark (car old))
(start (min oldpos point)) (oldpos (or (if oldposmark (marker-position oldposmark))
(end (max oldpos point))) (point-min)))
(unless (or (null old) (eq (marker-buffer oldposmark) (current-buffer))) (start (min oldpos point))
;; `window' does not display the same buffer any more! (end (max oldpos point)))
(setcdr old nil)) (unless (or (null old) (eq (marker-buffer oldposmark) (current-buffer)))
(if (or (and (null new) (null (cdr old))) ;; `window' does not display the same buffer any more!
(and (eq new (cdr old)) (setcdr old nil))
(eq (next-single-char-property-change (if (or (and (null new) (null (cdr old)))
start 'cursor-sensor-functions nil end) (and (eq new (cdr old))
end))) (eq (next-single-char-property-change
;; Clearly nothing to do. start 'cursor-sensor-functions nil end)
nil end)))
;; Maybe something to do. Let's see exactly what needs to run. ;; Clearly nothing to do.
(let* ((missing-p nil
(lambda (f) ;; Maybe something to do. Let's see exactly what needs to run.
"Non-nil if F is missing somewhere between START and END." (let* ((missing-p
(let ((pos start) (lambda (f)
(missing nil)) "Non-nil if F is missing somewhere between START and END."
(while (< pos end) (let ((pos start)
(setq pos (next-single-char-property-change (missing nil))
pos 'cursor-sensor-functions (while (< pos end)
nil end)) (setq pos (next-single-char-property-change
(unless (memq f (get-char-property pos 'cursor-sensor-functions
pos 'cursor-sensor-functions)) nil end))
(setq missing t))) (unless (memq f (get-char-property
missing))) pos 'cursor-sensor-functions))
(window (selected-window))) (setq missing t)))
(dolist (f (cdr old)) missing)))
(unless (and (memq f new) (not (funcall missing-p f))) (window (selected-window)))
(funcall f window oldpos 'left))) (dolist (f (cdr old))
(dolist (f new) (unless (and (memq f new) (not (funcall missing-p f)))
(unless (and (memq f (cdr old)) (not (funcall missing-p f))) (funcall f window oldpos 'left)))
(funcall f window oldpos 'entered))))) (dolist (f new)
(unless (and (memq f (cdr old)) (not (funcall missing-p f)))
(funcall f window oldpos 'entered)))))
;; Remember current state for next time. ;; Remember current state for next time.
;; Re-read cursor-sensor-functions since the functions may have moved ;; Re-read cursor-sensor-functions since the functions may have moved
;; window-point! ;; window-point!
(if old (if old
(progn (move-marker (car old) point) (progn (move-marker (car old) point)
(setcdr old new)) (setcdr old new))
(set-window-parameter window 'cursor-sensor--last-state (set-window-parameter window 'cursor-sensor--last-state
(cons (copy-marker point) new)))))) (cons (copy-marker point) new)))))))
;;;###autoload ;;;###autoload
(define-minor-mode cursor-sensor-mode (define-minor-mode cursor-sensor-mode