abo-abo / hydra

make Emacs bindings that stick around
1.85k stars 113 forks source link

Get wrong window size when hydra is active #409

Open twlz0ne opened 2 years ago

twlz0ne commented 2 years ago

That makes scroll-{up,down} unable to reach the top and bottom of the buffer.

emacs-hydra-window-size-issue

Reproduction script:

(setq user-emacs-directory (make-temp-file "temp-emacs-dir--" 'tmpdir "/"))
(setq package-user-dir (concat user-emacs-directory "elpa/"))

;; Add hydra source code
(add-to-list 'load-path "~/repos/emacs-hydra")

;; ---------------------------------------------------------------------------

(require 'hydra)

(defvar hydra-active-p nil)

(define-advice scroll-up (:before (&optional arg) debug)
  (message "==> [scroll up] buffer: %s, window height: %s, hydra-active-p: %s"
           (current-buffer) (window-height) hydra-active-p))

(define-advice scroll-down (:before (&optional arg) debug)
  (message "==> [scroll down] buffer: %s, window height: %s, hydra-active-p: %s"
           (current-buffer) (window-height) hydra-active-p))

(defhydra hydra-scroll (:hint nil)
    "
  1
  2
  3
  4
  5               _C-v_: Scroll up
  6               _M-v_: Scrool down
  7
  8
  9
 10
"
    ("C-v" (let ((hydra-active-p t)) (scroll-up)))
    ("M-v" (let ((hydra-active-p t)) (scroll-down))))

(global-set-key (kbd "C-c h") #'hydra-scroll/body)

(add-hook 'emacs-startup-hook
          (lambda ()
            (display-line-numbers-mode 1)
            (view-echo-area-messages)
            (save-excursion
              (insert "\
Steps to reproduce:

1. Press C-v to scroll window
2. Check the window height printed in *Messages* buffer.
3. Press C-c h to active hydra
4. Press C-v to scroll window
5. Check the window height printed in *Messages* buffer.

The window height of the two prints should be different."))))
twlz0ne commented 2 years ago

The correct window size can be get in the timer:

-   ("C-v" (let ((hydra-active-p t)) (scroll-up)))
+   ("C-v" (run-with-timer 0 nil (lambda ()
+                                  (let ((hydra-active-p t))
+                                    (scroll-up)))))

emacs-hydra-window-size-issue-2