Skip to content

Commit effa6c0

Browse files
author
Danny McClanahan
committed
make live-preview follow min or max point
1 parent f7d7983 commit effa6c0

File tree

2 files changed

+105
-15
lines changed

2 files changed

+105
-15
lines changed

markdown-mode.el

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5872,21 +5872,63 @@ buffer. Inverse of `markdown-live-preview-buffer'.")
58725872
(get-buffer "*eww*"))
58735873
(error "eww is not present or not loaded on this version of emacs")))
58745874

5875+
(defun markdown-visual-lines-between-points (beg end)
5876+
(save-excursion
5877+
(goto-char beg)
5878+
(cl-loop with count = 0
5879+
while (progn (end-of-visual-line)
5880+
(and (< (point) end) (line-move-visual 1 t)))
5881+
do (cl-incf count)
5882+
finally return count)))
5883+
58755884
(defun markdown-live-preview-window-serialize (buf)
58765885
"Get window point and scroll data for all windows displaying BUF if BUF is
5877-
non-nil."
5878-
(when buf
5879-
(mapcar (lambda (win) (list win (window-point win) (window-start win)))
5880-
(get-buffer-window-list buf))))
5886+
live."
5887+
(when (buffer-live-p buf)
5888+
(with-current-buffer buf
5889+
(mapcar
5890+
(lambda (win)
5891+
(with-selected-window win
5892+
(let* ((start (window-start))
5893+
(pt (window-point))
5894+
(pt-or-sym (cond ((= pt (point-min)) 'min)
5895+
((= pt (point-max)) 'max)
5896+
(t pt)))
5897+
(diff (markdown-visual-lines-between-points
5898+
start pt)))
5899+
(message "serialize: start=%d, pt=%d, pt-or-sym=%S, diff=%d"
5900+
start pt pt-or-sym diff)
5901+
(list win pt-or-sym diff))))
5902+
(get-buffer-window-list buf)))))
5903+
5904+
(defun markdown-get-point-back-lines (pt num-lines)
5905+
(save-excursion
5906+
(goto-char pt)
5907+
(line-move-visual (- num-lines) t)
5908+
;; in testing, can occasionally overshoot the number of lines to traverse
5909+
(let ((actual-num-lines (markdown-visual-lines-between-points (point) pt)))
5910+
(when (> actual-num-lines num-lines)
5911+
(line-move-visual (- actual-num-lines num-lines) t)))
5912+
(point)))
58815913

58825914
(defun markdown-live-preview-window-deserialize (window-posns)
58835915
"Apply window point and scroll data from WINDOW-POSNS, given by
58845916
`markdown-live-preview-window-serialize'."
5885-
(cl-destructuring-bind (win pt start) window-posns
5917+
(cl-destructuring-bind (win pt-or-sym diff) window-posns
58865918
(when (window-live-p win)
5887-
(set-window-buffer win markdown-live-preview-buffer)
5888-
(set-window-point win pt)
5889-
(set-window-start win start))))
5919+
(with-current-buffer markdown-live-preview-buffer
5920+
(set-window-buffer win (current-buffer))
5921+
(cl-destructuring-bind (actual-pt actual-diff)
5922+
(cl-case pt-or-sym
5923+
(min (list (point-min) 0))
5924+
(max (list (point-max) diff))
5925+
(t (list pt-or-sym diff)))
5926+
(message "deserialize: pt-or-sym=%S, diff=%d" pt-or-sym actual-diff)
5927+
(message "point-back-lines: %d"
5928+
(markdown-get-point-back-lines actual-pt actual-diff))
5929+
(set-window-start
5930+
win (markdown-get-point-back-lines actual-pt actual-diff))
5931+
(set-window-point win actual-pt))))))
58905932

58915933
(defun markdown-live-preview-export ()
58925934
"Export to XHTML using `markdown-export' and browse the resulting file within

tests/markdown-test.el

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3767,25 +3767,30 @@ Detail: https://github.com/jrblevin/markdown-mode/issues/79"
37673767
(kill-buffer)))))
37683768

37693769
(defadvice markdown-live-preview-window-eww
3770-
(around markdown-create-fake-eww disable)
3770+
(around markdown-test-create-fake-eww disable)
37713771
(setq ad-return-value (get-buffer-create "*eww*")))
37723772

3773-
(defmacro markdown-temp-eww (&rest body)
3773+
(defmacro markdown-test-fake-eww (&rest body)
37743774
`(progn
3775-
,@(if (featurep 'eww) body
3775+
,@(if (require 'eww nil t) body
37763776
`((ad-enable-advice #'markdown-live-preview-window-eww
3777-
'around 'markdown-create-fake-eww)
3777+
'around 'markdown-test-create-fake-eww)
37783778
(ad-activate #'markdown-live-preview-window-eww)
37793779
,@body
37803780
(ad-disable-advice #'markdown-live-preview-window-eww
3781-
'around 'markdown-create-fake-eww)
3781+
'around 'markdown-test-create-fake-eww)
37823782
(ad-activate #'markdown-live-preview-window-eww)))))
37833783

3784+
(defmacro markdown-test-eww-or-nothing (test &rest body)
3785+
(if (require 'eww nil t) `(progn ,@body)
3786+
(message "no eww found: skipping %s" test)
3787+
nil))
3788+
37843789
(ert-deftest test-markdown-ext/live-preview-exports ()
37853790
(markdown-test-temp-file "inline.text"
37863791
(unless (require 'eww nil t)
37873792
(should-error (markdown-live-preview-mode)))
3788-
(markdown-temp-eww
3793+
(markdown-test-fake-eww
37893794
(markdown-live-preview-mode)
37903795
(should (buffer-live-p markdown-live-preview-buffer))
37913796
(should (eq (current-buffer)
@@ -3798,7 +3803,7 @@ Detail: https://github.com/jrblevin/markdown-mode/issues/79"
37983803
(should (buffer-live-p markdown-live-preview-buffer)))))
37993804

38003805
(ert-deftest test-markdown-ext/live-preview-delete-exports ()
3801-
(markdown-temp-eww
3806+
(markdown-test-fake-eww
38023807
(let ((markdown-live-preview-delete-export 'delete-on-destroy)
38033808
file-output)
38043809
(markdown-test-temp-file "inline.text"
@@ -3820,6 +3825,49 @@ Detail: https://github.com/jrblevin/markdown-mode/issues/79"
38203825
(should (file-exists-p file-output)))
38213826
(delete-file file-output)))))
38223827

3828+
(ert-deftest test-markdown-ext/live-preview-follow-min-max ()
3829+
(markdown-test-eww-or-nothing "live-preview-follow-min-max"
3830+
(markdown-test-temp-file "inline.text"
3831+
(markdown-live-preview-mode)
3832+
(should (buffer-live-p markdown-live-preview-buffer))
3833+
(should (window-live-p (get-buffer-window markdown-live-preview-buffer)))
3834+
(with-selected-window (get-buffer-window markdown-live-preview-buffer)
3835+
(goto-char (point-min)))
3836+
(goto-char (point-min))
3837+
(insert "a test ")
3838+
(markdown-live-preview-export)
3839+
(let (final-pt final-win-st-diff)
3840+
;; test that still starts at point-min
3841+
(with-selected-window (get-buffer-window markdown-live-preview-buffer)
3842+
(should (= (window-point) 1))
3843+
(should (= (markdown-visual-lines-between-points
3844+
(window-start) (window-point))
3845+
0))
3846+
(set-window-point (selected-window) (point-max))
3847+
(setq final-pt (window-point)
3848+
final-win-st-diff (markdown-visual-lines-between-points
3849+
(window-start) (window-point))))
3850+
(goto-char (point-min))
3851+
(insert "this is ")
3852+
(markdown-live-preview-export)
3853+
(with-selected-window (get-buffer-window markdown-live-preview-buffer)
3854+
(should (= (window-point) (+ final-pt (length "this is "))))
3855+
(should (= (markdown-visual-lines-between-points
3856+
(window-start) (window-point))
3857+
final-win-st-diff))
3858+
;; test that still starts at point-max, with correct line difference
3859+
(goto-char (floor (/ (float (- (point-max) (point-min))) 2)))
3860+
(setq final-pt (window-point)
3861+
final-win-st-diff (markdown-visual-lines-between-points
3862+
(window-start) final-pt)))
3863+
(markdown-live-preview-export)
3864+
;; test that still starts at same point, with correct line difference
3865+
(with-selected-window (get-buffer-window markdown-live-preview-buffer)
3866+
(should (= (window-point) final-pt))
3867+
(should (= (markdown-visual-lines-between-points
3868+
(window-start) (window-point))
3869+
final-win-st-diff)))))))
3870+
38233871
(provide 'markdown-test)
38243872

38253873
;;; markdown-test.el ends here

0 commit comments

Comments
 (0)