Skip to content

Commit 6702e69

Browse files
author
Danny McClanahan
committed
select eww window during each eww render
`eww-open-file' (and whatever other rendering tool might be used) uses the size of the selected window to determine the width of lines in its rendered output. this commit ensures that (even though eww can only choose one window among all the windows that may be displaying it) if a window is displaying an eww buffer, eww will select one of those windows' sizes to render into, thus ensuring that at least one window will be rendered correctly. in the common case, only a single window is used to preview the output, which means this works perfectly. in other words, the failing case was that if the window displaying a markdown buffer was a different width than the window displaying the eww (or other function) buffer, the eww window would render a page suited for the width of the markdown buffer window, NOT the eww buffer window. this commit ensures the existing eww buffer window is used to determine the width of the rendered output, and adds a test to that effect. for testing, we could modify the width of the eww buffer window, and ensure that eww renders to that changed width, which would be more appropriate, but i don't know how to check what width eww renders into. the given test should accomplish the same goal, by checking that the eww buffer window is selected when eww renders into it.
1 parent 458c12d commit 6702e69

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

markdown-mode.el

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5373,6 +5373,9 @@ alive."
53735373
(setq markdown-live-preview-currently-exporting-process nil))
53745374
(message msg))
53755375

5376+
(defun markdown-live-preview-get-displaying-window (window-data-list)
5377+
(or (caar window-data-list) (selected-window)))
5378+
53765379
(defun markdown-live-preview-async-create-view-display (src-buf out-file msg)
53775380
(unwind-protect
53785381
(let (had-view-buffer window-data-list)
@@ -5382,8 +5385,11 @@ alive."
53825385
markdown-live-preview-view-buffer)))
53835386
(let ((view-buf
53845387
(save-window-excursion
5385-
(funcall
5386-
markdown-live-preview-window-function out-file))))
5388+
(with-selected-window
5389+
(markdown-live-preview-get-displaying-window
5390+
window-data-list)
5391+
(funcall
5392+
markdown-live-preview-window-function out-file)))))
53875393
(with-current-buffer view-buf
53885394
(add-hook 'kill-buffer-hook
53895395
#'markdown-live-preview-teardown-view t t))
@@ -5456,11 +5462,15 @@ the rendered output."
54565462
(export-file (markdown-export (markdown-live-preview-get-filename)))
54575463
;; get positions in all windows currently displaying output buffer
54585464
(window-data
5459-
(markdown-live-preview-window-serialize
5460-
markdown-live-preview-view-buffer))
5465+
(with-current-buffer src-buf
5466+
(markdown-live-preview-window-serialize
5467+
markdown-live-preview-view-buffer)))
54615468
(view-buf
54625469
(save-window-excursion
5463-
(funcall markdown-live-preview-window-function export-file))))
5470+
(with-selected-window
5471+
(markdown-live-preview-get-displaying-window window-data)
5472+
(funcall markdown-live-preview-window-function
5473+
export-file)))))
54645474
(markdown-live-preview-link-source-view-buffers src-buf view-buf)
54655475
(with-current-buffer view-buf
54665476
(add-hook 'kill-buffer-hook

tests/markdown-test.el

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3363,6 +3363,61 @@ indented the same amount."
33633363
(ert-deftest test-markdown-ext/live-preview-delete-exports-async ()
33643364
(markdown-test/live-preview-delete-exports))
33653365

3366+
(defvar markdown-test-eww-window nil)
3367+
(defvar markdown-test-hit-advice nil)
3368+
3369+
(defadvice eww-open-file (before markdown-set-window-width disable)
3370+
(setq markdown-test-hit-advice t)
3371+
(should (eq (selected-window) markdown-test-eww-window)))
3372+
3373+
(defmacro markdown-eww-open-file-advice (&rest body)
3374+
`(progn
3375+
(ad-enable-advice #'eww-open-file 'before 'markdown-set-window-width)
3376+
(ad-activate #'eww-open-file)
3377+
,@body
3378+
(ad-disable-advice #'eww-open-file 'before 'markdown-set-window-width)
3379+
(ad-activate #'eww-open-file)))
3380+
3381+
(defun markdown-test/test-window-size-eww ()
3382+
(setq markdown-test-hit-advice nil)
3383+
(save-window-excursion
3384+
(markdown-test-temp-file "inline.text"
3385+
(let ((markdown-live-preview-idle-delay .01)
3386+
(windows (window-list))
3387+
(md-buf (current-buffer)))
3388+
(cl-loop for win in windows
3389+
unless (eq win (selected-window))
3390+
do (delete-window win))
3391+
(markdown-temp-eww
3392+
;; below should create separate window to display eww
3393+
(markdown-live-preview-mode)
3394+
(markdown-test/live-preview-wait)
3395+
(setq markdown-test-eww-window
3396+
(get-buffer-window (get-buffer "*eww*")))
3397+
(should markdown-live-preview-view-buffer)
3398+
(with-selected-window (get-buffer-window md-buf)
3399+
(markdown-eww-open-file-advice
3400+
(if markdown-live-preview-do-sync
3401+
(markdown-live-preview-sync-export)
3402+
(markdown-live-preview-async-export))
3403+
(markdown-test/live-preview-wait)
3404+
(sit-for .1)
3405+
;; at this point, `eww-render' should have finished, and eww should
3406+
;; have redisplayed. the advice checks that, since there was a
3407+
;; single window displaying the *eww* buffer, that window was used
3408+
;; as the selected window so that eww renders with a width
3409+
;; equal to the width of its window
3410+
(should (eq t markdown-test-hit-advice))))))))
3411+
(setq markdown-test-hit-advice nil))
3412+
3413+
(ert-deftest test-markdown-ext/live-preview-window-size-sync ()
3414+
(let ((markdown-live-preview-do-sync t))
3415+
(markdown-test/test-window-size-eww)))
3416+
3417+
(ert-deftest test-markdown-ext/live-preview-window-size-async ()
3418+
(let ((markdown-live-preview-do-sync nil))
3419+
(markdown-test/test-window-size-eww)))
3420+
33663421
(provide 'markdown-test)
33673422

33683423
;;; markdown-test.el ends here

0 commit comments

Comments
 (0)