Skip to content

Commit 17015f8

Browse files
author
Danny McClanahan
committed
fix interactive eww issue
1 parent 532ba23 commit 17015f8

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

markdown-mode.el

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5868,8 +5868,13 @@ buffer. Inverse of `markdown-live-preview-buffer'.")
58685868
(get-buffer "*eww*"))
58695869
(error "eww is not present or not loaded on this version of emacs")))
58705870

5871-
(defun markdown-get-lines-between-points (beg end)
5872-
(cl-count ?\n (buffer-substring-no-properties beg end)))
5871+
(defun markdown-visual-lines-between-points (beg end)
5872+
(save-excursion
5873+
(goto-char beg)
5874+
(cl-loop with count = 0
5875+
while (and (< (point) end) (line-move-visual 1 t))
5876+
do (cl-incf count)
5877+
finally return count)))
58735878

58745879
(defun markdown-live-preview-window-serialize (buf)
58755880
"Get window point and scroll data for all windows displaying BUF if BUF is
@@ -5881,13 +5886,18 @@ non-nil."
58815886
(let* ((pt (window-point win))
58825887
(pt-or-sym (cond ((= pt (point-min)) 'min)
58835888
((= pt (point-max)) 'max)
5884-
(t pt))))
5885-
(list win pt-or-sym
5886-
;; should use visual lines, not physical lines, but eww fits
5887-
;; line widths so there are no differences
5888-
(markdown-get-lines-between-points (window-start win) pt))))
5889+
(t pt)))
5890+
(diff (markdown-visual-lines-between-points
5891+
(window-start win) pt)))
5892+
(list win pt-or-sym diff)))
58895893
(get-buffer-window-list buf)))))
58905894

5895+
(defun markdown-get-point-back-lines (pt num-lines)
5896+
(save-excursion
5897+
(goto-char pt)
5898+
(line-move-visual (- num-lines) t)
5899+
(point)))
5900+
58915901
(defun markdown-live-preview-window-deserialize (window-posns)
58925902
"Apply window point and scroll data from WINDOW-POSNS, given by
58935903
`markdown-live-preview-window-serialize'."
@@ -5900,12 +5910,8 @@ non-nil."
59005910
(min (list (point-min) 0))
59015911
(max (list (point-max) start))
59025912
(t (list pt-or-sym start)))
5903-
(let ((start-pt
5904-
(save-excursion
5905-
(goto-char actual-pt)
5906-
(forward-line (- actual-diff))
5907-
(point))))
5908-
(set-window-start win start-pt))
5913+
(set-window-start
5914+
win (markdown-get-point-back-lines actual-pt actual-diff))
59095915
(set-window-point win actual-pt))))))
59105916

59115917
(defun markdown-live-preview-export ()

tests/markdown-test.el

Lines changed: 12 additions & 9 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 (featurep 'eww) body
3742+
,@(if (require 'eww nil t) body
37433743
`((ad-enable-advice #'markdown-live-preview-window-eww
37443744
'around 'markdown-create-fake-eww)
37453745
(ad-activate #'markdown-live-preview-window-eww)
@@ -3802,28 +3802,31 @@ Detail: https://github.com/jrblevin/markdown-mode/issues/79"
38023802
;; test that still starts at point-min
38033803
(with-selected-window (get-buffer-window markdown-live-preview-buffer)
38043804
(should (= (window-point) 1))
3805-
(goto-char (point-max))
3806-
(setq final-pt (point)
3807-
final-win-st-diff (markdown-get-lines-between-points
3808-
(window-start) final-pt)))
3805+
(should (= (markdown-visual-lines-between-points
3806+
(window-start) (window-point))
3807+
0))
3808+
(set-window-point (selected-window) (point-max))
3809+
(setq final-pt (window-point)
3810+
final-win-st-diff (markdown-visual-lines-between-points
3811+
(window-start) (window-point))))
38093812
(goto-char (point-min))
38103813
(insert "this is ")
38113814
(markdown-live-preview-export)
3812-
;; test that still starts at point-max, with correct line difference
38133815
(with-selected-window (get-buffer-window markdown-live-preview-buffer)
38143816
(should (= (window-point) (+ final-pt (length "this is "))))
3815-
(should (= (markdown-get-lines-between-points
3817+
(should (= (markdown-visual-lines-between-points
38163818
(window-start) (window-point))
38173819
final-win-st-diff))
3820+
;; test that still starts at point-max, with correct line difference
38183821
(goto-char (floor (/ (float (- (point-max) (point-min))) 2)))
38193822
(setq final-pt (point)
3820-
final-win-st-diff (markdown-get-lines-between-points
3823+
final-win-st-diff (markdown-visual-lines-between-points
38213824
(window-start) final-pt)))
38223825
(markdown-live-preview-export)
38233826
;; test that still starts at same point, with correct line difference
38243827
(with-selected-window (get-buffer-window markdown-live-preview-buffer)
38253828
(should (= (window-point) final-pt))
3826-
(should (= (markdown-get-lines-between-points
3829+
(should (= (markdown-visual-lines-between-points
38273830
(window-start) (window-point))
38283831
final-win-st-diff)))))))
38293832

0 commit comments

Comments
 (0)