Skip to content

Commit

Permalink
make live-preview follow min or max point
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny McClanahan committed Feb 23, 2016
1 parent 3e88d58 commit f5dc0f2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
42 changes: 36 additions & 6 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -5868,21 +5868,51 @@ 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-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
non-nil."
(when buf
(mapcar (lambda (win) (list win (window-point win) (window-start win)))
(get-buffer-window-list buf))))
(with-current-buffer buf
(mapcar
(lambda (win)
(let* ((pt (window-point win))
(pt-or-sym (cond ((= pt (point-min)) 'min)
((= pt (point-max)) 'max)
(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'."
(cl-destructuring-bind (win pt start) window-posns
(cl-destructuring-bind (win pt-or-sym start) window-posns
(when (window-live-p win)
(set-window-buffer win markdown-live-preview-buffer)
(set-window-point win pt)
(set-window-start win start))))
(with-current-buffer markdown-live-preview-buffer
(set-window-buffer win (current-buffer))
(cl-destructuring-bind (actual-pt actual-diff)
(cl-case pt-or-sym
(min (list (point-min) 0))
(max (list (point-max) start))
(t (list pt-or-sym start)))
(set-window-start
win (markdown-get-point-back-lines actual-pt actual-diff))
(set-window-point win actual-pt))))))

(defun markdown-live-preview-export ()
"Export to XHTML using `markdown-export' and browse the resulting file within
Expand Down
45 changes: 44 additions & 1 deletion 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 @@ -3787,6 +3787,49 @@ Detail: https://github.com/jrblevin/markdown-mode/issues/79"
(should (file-exists-p file-output)))
(delete-file file-output)))))

(ert-deftest test-markdown-ext/live-preview-follow-min-max ()
(markdown-temp-eww
(markdown-test-temp-file "inline.text"
(markdown-live-preview-mode)
(should (buffer-live-p markdown-live-preview-buffer))
(should (window-live-p (get-buffer-window markdown-live-preview-buffer)))
(with-selected-window (get-buffer-window markdown-live-preview-buffer)
(goto-char (point-min)))
(goto-char (point-min))
(insert "a ")
(markdown-live-preview-export)
(let (final-pt final-win-st-diff)
;; test that still starts at point-min
(with-selected-window (get-buffer-window markdown-live-preview-buffer)
(should (= (window-point) 1))
(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)
(with-selected-window (get-buffer-window markdown-live-preview-buffer)
(should (= (window-point) (+ final-pt (length "this is "))))
(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-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-visual-lines-between-points
(window-start) (window-point))
final-win-st-diff)))))))

(provide 'markdown-test)

;;; markdown-test.el ends here

0 comments on commit f5dc0f2

Please sign in to comment.