Skip to content

Commit 610574e

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 458c12d commit 610574e

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
@@ -5327,9 +5327,13 @@ buffer. Inverse of `markdown-live-preview-view-buffer'.")
53275327
"Status of sync or async live preview in current buffer.")
53285328
(make-variable-buffer-local 'markdown-live-preview-current-buffer-sync-async)
53295329

5330+
(defun markdown-live-preview-has-eww-p ()
5331+
(and (require 'eww nil t)
5332+
(fboundp 'libxml-parse-html-region)))
5333+
53305334
(defun markdown-live-preview-window-eww (file)
53315335
"A `markdown-live-preview-window-function' for previewing with `eww'."
5332-
(if (require 'eww nil t)
5336+
(if (markdown-live-preview-has-eww-p)
53335337
(progn
53345338
(eww-open-file file)
53355339
(get-buffer "*eww*"))
@@ -5373,28 +5377,41 @@ alive."
53735377
(setq markdown-live-preview-currently-exporting-process nil))
53745378
(message msg))
53755379

5380+
(defun markdown-live-preview-get-displaying-window (window-data-list)
5381+
(or (caar window-data-list)
5382+
;; if not displaying buf in any window, then make a new window and display
5383+
;; it there
5384+
(markdown-display-buffer-other-window (window-buffer))))
5385+
5386+
(defun markdown-live-preview-create-display (out-file &optional src-buf)
5387+
(let* ((src-buf (or src-buf (current-buffer)))
5388+
(had-view-buffer
5389+
(with-current-buffer src-buf markdown-live-preview-view-buffer))
5390+
(window-data-list
5391+
(with-current-buffer src-buf
5392+
(markdown-live-preview-window-serialize
5393+
markdown-live-preview-view-buffer)))
5394+
(display-win
5395+
(markdown-live-preview-get-displaying-window window-data-list))
5396+
(view-buf
5397+
(save-window-excursion
5398+
(with-selected-window display-win
5399+
(funcall markdown-live-preview-window-function out-file)))))
5400+
(markdown-live-preview-link-source-view-buffers src-buf view-buf)
5401+
(with-current-buffer view-buf
5402+
(add-hook 'kill-buffer-hook #'markdown-live-preview-teardown-view t t))
5403+
(with-current-buffer src-buf
5404+
(if had-view-buffer
5405+
(if window-data-list
5406+
(cl-loop for window-data in window-data-list
5407+
do (markdown-live-preview-window-deserialize
5408+
window-data view-buf))
5409+
(delete-window display-win))
5410+
(set-window-buffer display-win view-buf)))))
5411+
53765412
(defun markdown-live-preview-async-create-view-display (src-buf out-file msg)
53775413
(unwind-protect
5378-
(let (had-view-buffer window-data-list)
5379-
(with-current-buffer src-buf
5380-
(setq had-view-buffer markdown-live-preview-view-buffer
5381-
window-data-list (markdown-live-preview-window-serialize
5382-
markdown-live-preview-view-buffer)))
5383-
(let ((view-buf
5384-
(save-window-excursion
5385-
(funcall
5386-
markdown-live-preview-window-function out-file))))
5387-
(with-current-buffer view-buf
5388-
(add-hook 'kill-buffer-hook
5389-
#'markdown-live-preview-teardown-view t t))
5390-
(markdown-live-preview-link-source-view-buffers
5391-
src-buf view-buf)
5392-
(with-current-buffer src-buf
5393-
(if had-view-buffer
5394-
(cl-loop for window-data in window-data-list
5395-
do (markdown-live-preview-window-deserialize
5396-
window-data view-buf))
5397-
(markdown-display-buffer-other-window view-buf)))))
5414+
(markdown-live-preview-create-display out-file src-buf)
53985415
(markdown-live-preview-async-cleanup-export src-buf msg)))
53995416

54005417
(defmacro markdown-silence-messages (&rest body)
@@ -5452,33 +5469,16 @@ Emacs using `markdown-live-preview-window-function' Return the buffer displaying
54525469
the rendered output."
54535470
(interactive)
54545471
(let* ((markdown-live-preview-currently-exporting t)
5455-
(src-buf (current-buffer))
5456-
(export-file (markdown-export (markdown-live-preview-get-filename)))
5457-
;; get positions in all windows currently displaying output buffer
5458-
(window-data
5459-
(markdown-live-preview-window-serialize
5460-
markdown-live-preview-view-buffer))
5461-
(view-buf
5462-
(save-window-excursion
5463-
(funcall markdown-live-preview-window-function export-file))))
5464-
(markdown-live-preview-link-source-view-buffers src-buf view-buf)
5465-
(with-current-buffer view-buf
5466-
(add-hook 'kill-buffer-hook
5467-
#'markdown-live-preview-teardown-view t t))
5468-
(with-current-buffer src-buf
5469-
;; reset all windows displaying output buffer to where they were,
5470-
;; now with the new output
5471-
(mapc (lambda (data)
5472-
(markdown-live-preview-window-deserialize data view-buf))
5473-
window-data)
5474-
;; delete html editing buffer
5475-
(let ((buf (get-file-buffer export-file)))
5476-
(when buf (kill-buffer buf)))
5477-
(when (and export-file (file-exists-p export-file)
5478-
(eq markdown-live-preview-delete-export
5479-
'delete-on-export))
5480-
(delete-file export-file))
5481-
markdown-live-preview-view-buffer)))
5472+
(out-file (markdown-export (markdown-live-preview-get-filename))))
5473+
(markdown-live-preview-create-display out-file)
5474+
;; delete html editing buffer
5475+
(let ((buf (get-file-buffer out-file)))
5476+
(when buf (kill-buffer buf)))
5477+
(when (and out-file (file-exists-p out-file)
5478+
(eq markdown-live-preview-delete-export
5479+
'delete-on-export))
5480+
(delete-file out-file))
5481+
markdown-live-preview-view-buffer))
54825482

54835483
(defun markdown-live-preview-update (buf)
54845484
(lambda () (with-current-buffer buf (markdown-live-preview-async-export))))
@@ -5491,8 +5491,7 @@ the rendered output."
54915491
(progn
54925492
(add-hook
54935493
'after-save-hook #'markdown-live-preview-do-sync-preview t t)
5494-
(markdown-display-buffer-other-window
5495-
(markdown-live-preview-sync-export)))
5494+
(markdown-live-preview-sync-export))
54965495
(setq markdown-live-preview-idle-timer
54975496
(run-with-idle-timer
54985497
markdown-live-preview-idle-delay t
@@ -5539,9 +5538,8 @@ the rendered output."
55395538
(markdown-live-preview-teardown-async)))
55405539

55415540
(defun markdown-display-buffer-other-window (buf)
5542-
(let ((cur-buf (current-buffer)))
5543-
(switch-to-buffer-other-window buf)
5544-
(set-buffer cur-buf)))
5541+
(let ((pop-up-windows t))
5542+
(select-window (display-buffer buf t))))
55455543

55465544
(defun markdown-live-preview-switch-to-output ()
55475545
(interactive)
@@ -5564,10 +5562,7 @@ or `markdown-live-preview-sync-export' and update this buffer's contents."
55645562

55655563
(defun markdown-live-preview-do-sync-preview ()
55665564
(unless markdown-live-preview-currently-exporting
5567-
(if (buffer-live-p markdown-live-preview-view-buffer)
5568-
(markdown-live-preview-sync-export)
5569-
(markdown-display-buffer-other-window
5570-
(markdown-live-preview-sync-export)))))
5565+
(markdown-live-preview-sync-export)))
55715566

55725567

55735568
;;; Links =====================================================================

tests/markdown-test.el

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3290,7 +3290,7 @@ indented the same amount."
32903290

32913291
(defmacro markdown-temp-eww (&rest body)
32923292
`(progn
3293-
,@(if (require 'eww nil t) body
3293+
,@(if (markdown-live-preview-has-eww-p) body
32943294
`((ad-enable-advice #'markdown-live-preview-window-eww
32953295
'around 'markdown-create-fake-eww)
32963296
(ad-activate #'markdown-live-preview-window-eww)
@@ -3324,7 +3324,7 @@ indented the same amount."
33243324

33253325
(ert-deftest test-markdown-ext/live-preview-exports-sync ()
33263326
(let ((markdown-live-preview-do-sync t))
3327-
(unless (require 'eww nil t)
3327+
(unless (markdown-live-preview-has-eww-p)
33283328
(should-error (markdown-live-preview-sync-export)))
33293329
(markdown-test/live-preview-exports)))
33303330

@@ -3363,6 +3363,98 @@ 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-test-set-window-width disable)
3370+
(setq markdown-test-hit-advice t)
3371+
(should (eq (selected-window) markdown-test-eww-window)))
3372+
3373+
(defadvice get-buffer-create (before markdown-set-window-width-mock disable)
3374+
(when (let ((buf (ad-get-arg 0))) (and (stringp buf) (string= buf "*eww*")))
3375+
(setq markdown-test-hit-advice t)
3376+
(should (eq (selected-window) markdown-test-eww-window))))
3377+
3378+
(defmacro markdown-eww-open-file-advice (&rest body)
3379+
(if (markdown-live-preview-has-eww-p)
3380+
`(progn
3381+
(ad-enable-advice #'eww-open-file 'before
3382+
'markdown-test-set-window-width)
3383+
(ad-activate #'eww-open-file)
3384+
,@body
3385+
(ad-disable-advice #'eww-open-file 'before
3386+
'markdown-test-set-window-width)
3387+
(ad-activate #'eww-open-file))
3388+
`(progn
3389+
(ad-enable-advice #'get-buffer-create 'before
3390+
'markdown-set-window-width-mock)
3391+
(ad-activate #'get-buffer-create)
3392+
,@body
3393+
(ad-disable-advice #'get-buffer-create 'before
3394+
'markdown-set-window-width-mock)
3395+
(ad-activate #'get-buffer-create))))
3396+
3397+
(defadvice markdown-live-preview-get-displaying-window
3398+
(after markdown-test-get-test-window disable)
3399+
(setq markdown-test-eww-window ad-return-value))
3400+
3401+
(defmacro markdown-initial-window-create-advice (&rest body)
3402+
`(progn
3403+
(ad-enable-advice #'markdown-live-preview-get-displaying-window
3404+
'after 'markdown-test-get-test-window)
3405+
(ad-activate #'markdown-live-preview-get-displaying-window)
3406+
,@body
3407+
(ad-disable-advice #'markdown-live-preview-get-displaying-window
3408+
'after 'markdown-test-get-test-window)
3409+
(ad-activate #'markdown-live-preview-get-displaying-window)))
3410+
3411+
(defun markdown-test/test-window-usage-live-preview ()
3412+
(setq markdown-test-hit-advice nil)
3413+
(save-window-excursion
3414+
(markdown-test-temp-file "inline.text"
3415+
(let ((markdown-live-preview-idle-delay .01)
3416+
(windows (window-list))
3417+
(md-buf (current-buffer)))
3418+
(cl-loop for win in windows
3419+
unless (eq win (selected-window))
3420+
do (delete-window win))
3421+
;; note that advices are used in this test to supply `should' conditions
3422+
(markdown-temp-eww
3423+
;; test that first window created by
3424+
;; `markdown-live-preview-get-displaying-window' is the same window
3425+
;; that is used to finally render the buffer, on the first try
3426+
(markdown-eww-open-file-advice
3427+
(markdown-initial-window-create-advice
3428+
(if markdown-live-preview-do-sync
3429+
(markdown-live-preview-sync-export)
3430+
(markdown-live-preview-async-export))
3431+
(markdown-test/live-preview-wait)
3432+
(should (eq t markdown-test-hit-advice))
3433+
(setq markdown-test-hit-advice nil))
3434+
(setq markdown-test-eww-window
3435+
(get-buffer-window (get-buffer "*eww*")))
3436+
(should markdown-live-preview-view-buffer)
3437+
(with-selected-window (get-buffer-window md-buf)
3438+
(if markdown-live-preview-do-sync
3439+
(markdown-live-preview-sync-export)
3440+
(markdown-live-preview-async-export))
3441+
(markdown-test/live-preview-wait)
3442+
;; at this point, `eww-render' should have finished, and eww should
3443+
;; have redisplayed. the advice checks that, since there was a
3444+
;; single window displaying the *eww* buffer, that window was used
3445+
;; as the selected window so that eww renders with a width
3446+
;; equal to the width of its window
3447+
(should (eq t markdown-test-hit-advice))))))))
3448+
(setq markdown-test-hit-advice nil))
3449+
3450+
(ert-deftest test-markdown-ext/live-preview-window-size-sync ()
3451+
(let ((markdown-live-preview-do-sync t))
3452+
(markdown-test/test-window-usage-live-preview)))
3453+
3454+
(ert-deftest test-markdown-ext/live-preview-window-size-async ()
3455+
(let ((markdown-live-preview-do-sync nil))
3456+
(markdown-test/test-window-usage-live-preview)))
3457+
33663458
(provide 'markdown-test)
33673459

33683460
;;; markdown-test.el ends here

0 commit comments

Comments
 (0)