Skip to content

Commit

Permalink
fix interactive eww issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny McClanahan committed Feb 23, 2016
1 parent 532ba23 commit 17015f8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
32 changes: 19 additions & 13 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -5868,8 +5868,13 @@ buffer. Inverse of `markdown-live-preview-buffer'.")
(get-buffer "*eww*"))
(error "eww is not present or not loaded on this version of emacs")))

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

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

(defun markdown-get-point-back-lines (pt num-lines)
(save-excursion
(goto-char pt)
(line-move-visual (- num-lines) t)
(point)))

(defun markdown-live-preview-window-deserialize (window-posns)
"Apply window point and scroll data from WINDOW-POSNS, given by
`markdown-live-preview-window-serialize'."
Expand All @@ -5900,12 +5910,8 @@ non-nil."
(min (list (point-min) 0))
(max (list (point-max) start))
(t (list pt-or-sym start)))
(let ((start-pt
(save-excursion
(goto-char actual-pt)
(forward-line (- actual-diff))
(point))))
(set-window-start win start-pt))
(set-window-start
win (markdown-get-point-back-lines actual-pt actual-diff))
(set-window-point win actual-pt))))))

(defun markdown-live-preview-export ()
Expand Down
21 changes: 12 additions & 9 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -3739,7 +3739,7 @@ Detail: https://github.com/jrblevin/markdown-mode/issues/79"

(defmacro markdown-temp-eww (&rest body)
`(progn
,@(if (featurep 'eww) body
,@(if (require 'eww nil t) body
`((ad-enable-advice #'markdown-live-preview-window-eww
'around 'markdown-create-fake-eww)
(ad-activate #'markdown-live-preview-window-eww)
Expand Down Expand Up @@ -3802,28 +3802,31 @@ Detail: https://github.com/jrblevin/markdown-mode/issues/79"
;; test that still starts at point-min
(with-selected-window (get-buffer-window markdown-live-preview-buffer)
(should (= (window-point) 1))
(goto-char (point-max))
(setq final-pt (point)
final-win-st-diff (markdown-get-lines-between-points
(window-start) final-pt)))
(should (= (markdown-visual-lines-between-points
(window-start) (window-point))
0))
(set-window-point (selected-window) (point-max))
(setq final-pt (window-point)
final-win-st-diff (markdown-visual-lines-between-points
(window-start) (window-point))))
(goto-char (point-min))
(insert "this is ")
(markdown-live-preview-export)
;; test that still starts at point-max, with correct line difference
(with-selected-window (get-buffer-window markdown-live-preview-buffer)
(should (= (window-point) (+ final-pt (length "this is "))))
(should (= (markdown-get-lines-between-points
(should (= (markdown-visual-lines-between-points
(window-start) (window-point))
final-win-st-diff))
;; test that still starts at point-max, with correct line difference
(goto-char (floor (/ (float (- (point-max) (point-min))) 2)))
(setq final-pt (point)
final-win-st-diff (markdown-get-lines-between-points
final-win-st-diff (markdown-visual-lines-between-points
(window-start) final-pt)))
(markdown-live-preview-export)
;; test that still starts at same point, with correct line difference
(with-selected-window (get-buffer-window markdown-live-preview-buffer)
(should (= (window-point) final-pt))
(should (= (markdown-get-lines-between-points
(should (= (markdown-visual-lines-between-points
(window-start) (window-point))
final-win-st-diff)))))))

Expand Down

0 comments on commit 17015f8

Please sign in to comment.