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 22, 2016
1 parent 3e88d58 commit 532ba23
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
36 changes: 30 additions & 6 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -5868,21 +5868,45 @@ 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-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))))
(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))))
(get-buffer-window-list buf)))))

(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)))
(let ((start-pt
(save-excursion
(goto-char actual-pt)
(forward-line (- actual-diff))
(point))))
(set-window-start win start-pt))
(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
40 changes: 40 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -3787,6 +3787,46 @@ 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))
(goto-char (point-max))
(setq final-pt (point)
final-win-st-diff (markdown-get-lines-between-points
(window-start) final-pt)))
(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
(window-start) (window-point))
final-win-st-diff))
(goto-char (floor (/ (float (- (point-max) (point-min))) 2)))
(setq final-pt (point)
final-win-st-diff (markdown-get-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
(window-start) (window-point))
final-win-st-diff)))))))

(provide 'markdown-test)

;;; markdown-test.el ends here

0 comments on commit 532ba23

Please sign in to comment.