From 532ba23ec0ba03aedb6b0228ad32d5d4ee6433fc Mon Sep 17 00:00:00 2001 From: Danny McClanahan Date: Mon, 22 Feb 2016 07:43:14 -0600 Subject: [PATCH] make live-preview follow min or max point --- markdown-mode.el | 36 ++++++++++++++++++++++++++++++------ tests/markdown-test.el | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/markdown-mode.el b/markdown-mode.el index 22a152dd..781a1c54 100644 --- a/markdown-mode.el +++ b/markdown-mode.el @@ -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 diff --git a/tests/markdown-test.el b/tests/markdown-test.el index c61127d7..1719b696 100644 --- a/tests/markdown-test.el +++ b/tests/markdown-test.el @@ -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