The source of the error was not keeping track of which buffer focus should
operate on.

Also ensured that all hooks are local, which might also effect #13.
This commit is contained in:
Lars Tveito 2017-06-12 16:43:59 +02:00
parent 155da77a32
commit a84ade00a2

View File

@ -76,6 +76,9 @@ Things that are defined include `symbol', `list', `sexp',
(defvar focus-current-thing nil (defvar focus-current-thing nil
"Overrides the choice of thing dictated by `focus-mode-to-thing' if set.") "Overrides the choice of thing dictated by `focus-mode-to-thing' if set.")
(defvar focus-buffer nil
"Local reference to the buffer focus functions operate on.")
(defvar focus-pre-overlay nil (defvar focus-pre-overlay nil
"The overlay that dims the text prior to the current-point.") "The overlay that dims the text prior to the current-point.")
@ -89,6 +92,7 @@ The timer calls `focus-read-only-hide-cursor' after
;; Use make-local-variable for backwards compatibility. ;; Use make-local-variable for backwards compatibility.
(dolist (var '(focus-current-thing (dolist (var '(focus-current-thing
focus-buffer
focus-pre-overlay focus-pre-overlay
focus-post-overlay focus-post-overlay
focus-read-only-blink-timer)) focus-read-only-blink-timer))
@ -138,9 +142,10 @@ argument."
If `focus-mode' is enabled, this command fires after each If `focus-mode' is enabled, this command fires after each
command." command."
(let* ((bounds (focus-bounds))) (with-current-buffer focus-buffer
(when bounds (let* ((bounds (focus-bounds)))
(focus-move-overlays (car bounds) (cdr bounds))))) (when bounds
(focus-move-overlays (car bounds) (cdr bounds))))))
(defun focus-move-overlays (low high) (defun focus-move-overlays (low high)
"Move `focus-pre-overlay' and `focus-post-overlay'." "Move `focus-pre-overlay' and `focus-post-overlay'."
@ -155,12 +160,13 @@ overlays; these are invisible until `focus-move-focus' is run. It
adds `focus-move-focus' to `post-command-hook'." adds `focus-move-focus' to `post-command-hook'."
(unless (or focus-pre-overlay focus-post-overlay) (unless (or focus-pre-overlay focus-post-overlay)
(setq focus-pre-overlay (make-overlay (point-min) (point-min)) (setq focus-pre-overlay (make-overlay (point-min) (point-min))
focus-post-overlay (make-overlay (point-max) (point-max))) focus-post-overlay (make-overlay (point-max) (point-max))
focus-buffer (current-buffer))
(let ((color (focus-make-dim-color))) (let ((color (focus-make-dim-color)))
(mapc (lambda (o) (overlay-put o 'face (cons 'foreground-color color))) (mapc (lambda (o) (overlay-put o 'face (cons 'foreground-color color)))
(list focus-pre-overlay focus-post-overlay))) (list focus-pre-overlay focus-post-overlay)))
(add-hook 'post-command-hook 'focus-move-focus nil t) (add-hook 'post-command-hook 'focus-move-focus nil t)
(add-hook 'change-major-mode-hook 'focus-terminate))) (add-hook 'change-major-mode-hook 'focus-terminate nil t)))
(defun focus-terminate () (defun focus-terminate ()
"This function is run when command `focus-mode' is disabled. "This function is run when command `focus-mode' is disabled.
@ -223,11 +229,11 @@ if active."
(interactive "p") (interactive "p")
(focus-next-thing (- n))) (focus-next-thing (- n)))
(defun focus-read-only-hide-cursor (&optional buffer) (defun focus-read-only-hide-cursor ()
"Hide the cursor. "Hide the cursor.
This function is triggered by the `focus-read-only-blink-timer', This function is triggered by the `focus-read-only-blink-timer',
when `focus-read-only-mode' is activated." when `focus-read-only-mode' is activated."
(with-current-buffer (or buffer (current-buffer)) (with-current-buffer focus-buffer
(when (and focus-read-only-mode (not (null focus-read-only-blink-timer))) (when (and focus-read-only-mode (not (null focus-read-only-blink-timer)))
(setq focus-read-only-blink-timer nil) (setq focus-read-only-blink-timer nil)
(setq cursor-type nil)))) (setq cursor-type nil))))
@ -236,13 +242,14 @@ when `focus-read-only-mode' is activated."
"Make the cursor visible for `focus-read-only-blink-seconds'. "Make the cursor visible for `focus-read-only-blink-seconds'.
This is added to the `pre-command-hook' when This is added to the `pre-command-hook' when
`focus-read-only-mode' is active." `focus-read-only-mode' is active."
(when (and focus-read-only-mode (with-current-buffer focus-buffer
(not (member last-command '(focus-next-thing focus-prev-thing)))) (when (and focus-read-only-mode
(when focus-read-only-blink-timer (cancel-timer focus-read-only-blink-timer)) (not (member last-command '(focus-next-thing focus-prev-thing))))
(setq cursor-type focus-cursor-type) (when focus-read-only-blink-timer (cancel-timer focus-read-only-blink-timer))
(setq focus-read-only-blink-timer (setq cursor-type focus-cursor-type)
(run-at-time focus-read-only-blink-seconds nil (setq focus-read-only-blink-timer
'focus-read-only-hide-cursor (current-buffer))))) (run-at-time focus-read-only-blink-seconds nil
'focus-read-only-hide-cursor)))))
(defun focus-read-only-init () (defun focus-read-only-init ()
"Run when `focus-read-only-mode' is activated. "Run when `focus-read-only-mode' is activated.
@ -250,9 +257,10 @@ Enables `read-only-mode', hides the cursor and adds
`focus-read-only-cursor-blink' to `pre-command-hook'. Also `focus-read-only-cursor-blink' to `pre-command-hook'. Also
`focus-read-only-terminate' is added to the `kill-buffer-hook'." `focus-read-only-terminate' is added to the `kill-buffer-hook'."
(read-only-mode 1) (read-only-mode 1)
(setq cursor-type nil) (setq cursor-type nil
focus-buffer (current-buffer))
(add-hook 'pre-command-hook 'focus-read-only-cursor-blink nil t) (add-hook 'pre-command-hook 'focus-read-only-cursor-blink nil t)
(add-hook 'kill-buffer-hook 'focus-read-only-terminate t)) (add-hook 'kill-buffer-hook 'focus-read-only-terminate nil t))
(defun focus-read-only-terminate () (defun focus-read-only-terminate ()
"Run when `focus-read-only-mode' is deactivated. "Run when `focus-read-only-mode' is deactivated.