Skip to content

Commit fa94196

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. check for libxml when trying to use eww ensure eww renders to correct size at first export also refactor duplicated code to display exported buffer into shared function for sync and async export
1 parent a488a8a commit fa94196

File tree

2 files changed

+146
-59
lines changed

2 files changed

+146
-59
lines changed

markdown-mode.el

Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5907,9 +5907,13 @@ buffer. Inverse of `markdown-live-preview-view-buffer'.")
59075907
"Status of sync or async live preview in current buffer.")
59085908
(make-variable-buffer-local 'markdown-live-preview-current-buffer-sync-async)
59095909

5910+
(defun markdown-live-preview-has-eww-p ()
5911+
(and (require 'eww nil t)
5912+
(fboundp 'libxml-parse-html-region)))
5913+
59105914
(defun markdown-live-preview-window-eww (file)
59115915
"A `markdown-live-preview-window-function' for previewing with `eww'."
5912-
(if (require 'eww nil t)
5916+
(if (markdown-live-preview-has-eww-p)
59135917
(progn
59145918
(eww-open-file file)
59155919
(get-buffer "*eww*"))
@@ -5953,28 +5957,41 @@ alive."
59535957
(setq markdown-live-preview-currently-exporting-process nil))
59545958
(message msg))
59555959

5960+
(defun markdown-live-preview-get-displaying-window (window-data-list)
5961+
(or (caar window-data-list)
5962+
;; if not displaying buf in any window, then make a new window and display
5963+
;; it there
5964+
(markdown-display-buffer-other-window (window-buffer))))
5965+
5966+
(defun markdown-live-preview-create-display (out-file &optional src-buf)
5967+
(let* ((src-buf (or src-buf (current-buffer)))
5968+
(had-view-buffer
5969+
(with-current-buffer src-buf markdown-live-preview-view-buffer))
5970+
(window-data-list
5971+
(with-current-buffer src-buf
5972+
(markdown-live-preview-window-serialize
5973+
markdown-live-preview-view-buffer)))
5974+
(display-win
5975+
(markdown-live-preview-get-displaying-window window-data-list))
5976+
(view-buf
5977+
(save-window-excursion
5978+
(with-selected-window display-win
5979+
(funcall markdown-live-preview-window-function out-file)))))
5980+
(markdown-live-preview-link-source-view-buffers src-buf view-buf)
5981+
(with-current-buffer view-buf
5982+
(add-hook 'kill-buffer-hook #'markdown-live-preview-teardown-view t t))
5983+
(with-current-buffer src-buf
5984+
(if had-view-buffer
5985+
(if window-data-list
5986+
(cl-loop for window-data in window-data-list
5987+
do (markdown-live-preview-window-deserialize
5988+
window-data view-buf))
5989+
(delete-window display-win))
5990+
(set-window-buffer display-win view-buf)))))
5991+
59565992
(defun markdown-live-preview-async-create-view-display (src-buf out-file msg)
59575993
(unwind-protect
5958-
(let (had-view-buffer window-data-list)
5959-
(with-current-buffer src-buf
5960-
(setq had-view-buffer markdown-live-preview-view-buffer
5961-
window-data-list (markdown-live-preview-window-serialize
5962-
markdown-live-preview-view-buffer)))
5963-
(let ((view-buf
5964-
(save-window-excursion
5965-
(funcall
5966-
markdown-live-preview-window-function out-file))))
5967-
(with-current-buffer view-buf
5968-
(add-hook 'kill-buffer-hook
5969-
#'markdown-live-preview-teardown-view t t))
5970-
(markdown-live-preview-link-source-view-buffers
5971-
src-buf view-buf)
5972-
(with-current-buffer src-buf
5973-
(if had-view-buffer
5974-
(cl-loop for window-data in window-data-list
5975-
do (markdown-live-preview-window-deserialize
5976-
window-data view-buf))
5977-
(markdown-display-buffer-other-window view-buf)))))
5994+
(markdown-live-preview-create-display out-file src-buf)
59785995
(markdown-live-preview-async-cleanup-export src-buf msg)))
59795996

59805997
(defmacro markdown-silence-messages (&rest body)
@@ -6032,33 +6049,16 @@ Emacs using `markdown-live-preview-window-function' Return the buffer displaying
60326049
the rendered output."
60336050
(interactive)
60346051
(let* ((markdown-live-preview-currently-exporting t)
6035-
(src-buf (current-buffer))
6036-
(export-file (markdown-export (markdown-live-preview-get-filename)))
6037-
;; get positions in all windows currently displaying output buffer
6038-
(window-data
6039-
(markdown-live-preview-window-serialize
6040-
markdown-live-preview-view-buffer))
6041-
(view-buf
6042-
(save-window-excursion
6043-
(funcall markdown-live-preview-window-function export-file))))
6044-
(markdown-live-preview-link-source-view-buffers src-buf view-buf)
6045-
(with-current-buffer view-buf
6046-
(add-hook 'kill-buffer-hook
6047-
#'markdown-live-preview-teardown-view t t))
6048-
(with-current-buffer src-buf
6049-
;; reset all windows displaying output buffer to where they were,
6050-
;; now with the new output
6051-
(mapc (lambda (data)
6052-
(markdown-live-preview-window-deserialize data view-buf))
6053-
window-data)
6054-
;; delete html editing buffer
6055-
(let ((buf (get-file-buffer export-file)))
6056-
(when buf (kill-buffer buf)))
6057-
(when (and export-file (file-exists-p export-file)
6058-
(eq markdown-live-preview-delete-export
6059-
'delete-on-export))
6060-
(delete-file export-file))
6061-
markdown-live-preview-view-buffer)))
6052+
(out-file (markdown-export (markdown-live-preview-get-filename))))
6053+
(markdown-live-preview-create-display out-file)
6054+
;; delete html editing buffer
6055+
(let ((buf (get-file-buffer out-file)))
6056+
(when buf (kill-buffer buf)))
6057+
(when (and out-file (file-exists-p out-file)
6058+
(eq markdown-live-preview-delete-export
6059+
'delete-on-export))
6060+
(delete-file out-file))
6061+
markdown-live-preview-view-buffer))
60626062

60636063
(defun markdown-live-preview-update (buf)
60646064
(lexical-let ((buf buf))
@@ -6072,8 +6072,7 @@ the rendered output."
60726072
(progn
60736073
(add-hook
60746074
'after-save-hook #'markdown-live-preview-do-sync-preview t t)
6075-
(markdown-display-buffer-other-window
6076-
(markdown-live-preview-sync-export)))
6075+
(markdown-live-preview-sync-export))
60776076
(setq markdown-live-preview-idle-timer
60786077
(run-with-idle-timer
60796078
markdown-live-preview-idle-delay t
@@ -6120,9 +6119,8 @@ the rendered output."
61206119
(markdown-live-preview-teardown-async)))
61216120

61226121
(defun markdown-display-buffer-other-window (buf)
6123-
(let ((cur-buf (current-buffer)))
6124-
(switch-to-buffer-other-window buf)
6125-
(set-buffer cur-buf)))
6122+
(let ((pop-up-windows t))
6123+
(select-window (display-buffer buf t))))
61266124

61276125
(defun markdown-live-preview-switch-to-output ()
61286126
(interactive)
@@ -6145,10 +6143,7 @@ or `markdown-live-preview-sync-export' and update this buffer's contents."
61456143

61466144
(defun markdown-live-preview-do-sync-preview ()
61476145
(unless markdown-live-preview-currently-exporting
6148-
(if (buffer-live-p markdown-live-preview-view-buffer)
6149-
(markdown-live-preview-sync-export)
6150-
(markdown-display-buffer-other-window
6151-
(markdown-live-preview-sync-export)))))
6146+
(markdown-live-preview-sync-export)))
61526147

61536148

61546149
;;; Links =====================================================================

tests/markdown-test.el

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3739,7 +3739,7 @@ Detail: https://github.com/jrblevin/markdown-mode/issues/79"
37393739

37403740
(defmacro markdown-temp-eww (&rest body)
37413741
`(progn
3742-
,@(if (require 'eww nil t) body
3742+
,@(if (markdown-live-preview-has-eww-p) body
37433743
`((ad-enable-advice #'markdown-live-preview-window-eww
37443744
'around 'markdown-create-fake-eww)
37453745
(ad-activate #'markdown-live-preview-window-eww)
@@ -3773,7 +3773,7 @@ Detail: https://github.com/jrblevin/markdown-mode/issues/79"
37733773

37743774
(ert-deftest test-markdown-ext/live-preview-exports-sync ()
37753775
(let ((markdown-live-preview-do-sync t))
3776-
(unless (require 'eww nil t)
3776+
(unless (markdown-live-preview-has-eww-p)
37773777
(should-error (markdown-live-preview-sync-export)))
37783778
(markdown-test/live-preview-exports)))
37793779

@@ -3812,6 +3812,98 @@ Detail: https://github.com/jrblevin/markdown-mode/issues/79"
38123812
(ert-deftest test-markdown-ext/live-preview-delete-exports-async ()
38133813
(markdown-test/live-preview-delete-exports))
38143814

3815+
(defvar markdown-test-eww-window nil)
3816+
(defvar markdown-test-hit-advice nil)
3817+
3818+
(defadvice eww-open-file (before markdown-test-set-window-width disable)
3819+
(setq markdown-test-hit-advice t)
3820+
(should (eq (selected-window) markdown-test-eww-window)))
3821+
3822+
(defadvice get-buffer-create (before markdown-set-window-width-mock disable)
3823+
(when (let ((buf (ad-get-arg 0))) (and (stringp buf) (string= buf "*eww*")))
3824+
(setq markdown-test-hit-advice t)
3825+
(should (eq (selected-window) markdown-test-eww-window))))
3826+
3827+
(defmacro markdown-eww-open-file-advice (&rest body)
3828+
(if (markdown-live-preview-has-eww-p)
3829+
`(progn
3830+
(ad-enable-advice #'eww-open-file 'before
3831+
'markdown-test-set-window-width)
3832+
(ad-activate #'eww-open-file)
3833+
,@body
3834+
(ad-disable-advice #'eww-open-file 'before
3835+
'markdown-test-set-window-width)
3836+
(ad-activate #'eww-open-file))
3837+
`(progn
3838+
(ad-enable-advice #'get-buffer-create 'before
3839+
'markdown-set-window-width-mock)
3840+
(ad-activate #'get-buffer-create)
3841+
,@body
3842+
(ad-disable-advice #'get-buffer-create 'before
3843+
'markdown-set-window-width-mock)
3844+
(ad-activate #'get-buffer-create))))
3845+
3846+
(defadvice markdown-live-preview-get-displaying-window
3847+
(after markdown-test-get-test-window disable)
3848+
(setq markdown-test-eww-window ad-return-value))
3849+
3850+
(defmacro markdown-initial-window-create-advice (&rest body)
3851+
`(progn
3852+
(ad-enable-advice #'markdown-live-preview-get-displaying-window
3853+
'after 'markdown-test-get-test-window)
3854+
(ad-activate #'markdown-live-preview-get-displaying-window)
3855+
,@body
3856+
(ad-disable-advice #'markdown-live-preview-get-displaying-window
3857+
'after 'markdown-test-get-test-window)
3858+
(ad-activate #'markdown-live-preview-get-displaying-window)))
3859+
3860+
(defun markdown-test/test-window-usage-live-preview ()
3861+
(setq markdown-test-hit-advice nil)
3862+
(save-window-excursion
3863+
(markdown-test-temp-file "inline.text"
3864+
(let ((markdown-live-preview-idle-delay .01)
3865+
(windows (window-list))
3866+
(md-buf (current-buffer)))
3867+
(cl-loop for win in windows
3868+
unless (eq win (selected-window))
3869+
do (delete-window win))
3870+
;; note that advices are used in this test to supply `should' conditions
3871+
(markdown-temp-eww
3872+
;; test that first window created by
3873+
;; `markdown-live-preview-get-displaying-window' is the same window
3874+
;; that is used to finally render the buffer, on the first try
3875+
(markdown-eww-open-file-advice
3876+
(markdown-initial-window-create-advice
3877+
(if markdown-live-preview-do-sync
3878+
(markdown-live-preview-sync-export)
3879+
(markdown-live-preview-async-export))
3880+
(markdown-test/live-preview-wait)
3881+
(should (eq t markdown-test-hit-advice))
3882+
(setq markdown-test-hit-advice nil))
3883+
(setq markdown-test-eww-window
3884+
(get-buffer-window (get-buffer "*eww*")))
3885+
(should markdown-live-preview-view-buffer)
3886+
(with-selected-window (get-buffer-window md-buf)
3887+
(if markdown-live-preview-do-sync
3888+
(markdown-live-preview-sync-export)
3889+
(markdown-live-preview-async-export))
3890+
(markdown-test/live-preview-wait)
3891+
;; at this point, `eww-render' should have finished, and eww should
3892+
;; have redisplayed. the advice checks that, since there was a
3893+
;; single window displaying the *eww* buffer, that window was used
3894+
;; as the selected window so that eww renders with a width
3895+
;; equal to the width of its window
3896+
(should (eq t markdown-test-hit-advice))))))))
3897+
(setq markdown-test-hit-advice nil))
3898+
3899+
(ert-deftest test-markdown-ext/live-preview-window-size-sync ()
3900+
(let ((markdown-live-preview-do-sync t))
3901+
(markdown-test/test-window-usage-live-preview)))
3902+
3903+
(ert-deftest test-markdown-ext/live-preview-window-size-async ()
3904+
(let ((markdown-live-preview-do-sync nil))
3905+
(markdown-test/test-window-usage-live-preview)))
3906+
38153907
(provide 'markdown-test)
38163908

38173909
;;; markdown-test.el ends here

0 commit comments

Comments
 (0)