Skip to content

Commit b50a8c3

Browse files
author
Danny McClanahan
committed
add sync live preview back
1 parent 7092964 commit b50a8c3

File tree

2 files changed

+115
-26
lines changed

2 files changed

+115
-26
lines changed

markdown-mode.el

Lines changed: 99 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ update the buffer containing the preview and return the buffer."
10961096
:type 'function)
10971097

10981098
(defcustom markdown-live-preview-delete-export 'delete-on-destroy
1099-
"Delete exported html file when using `markdown-live-preview-export' on every
1099+
"Delete exported html file when using `markdown-live-preview-sync-export' on every
11001100
export by setting to 'delete-on-export, when quitting
11011101
`markdown-live-preview-mode' by setting to 'delete-on-destroy, or not at all
11021102
when nil."
@@ -1108,6 +1108,12 @@ when nil."
11081108
:group 'markdown
11091109
:type 'integer)
11101110

1111+
(defcustom markdown-live-preview-do-sync nil
1112+
"Whether to do live preview exports synchronously on save, or asynchronously
1113+
with an idle timer."
1114+
:group 'markdown
1115+
:type 'boolean)
1116+
11111117
(defcustom markdown-list-indent-width 4
11121118
"Depth of indentation for markdown lists. Used in `markdown-demote-list-item'
11131119
and `markdown-promote-list-item'."
@@ -5871,7 +5877,8 @@ current filename, but with the extension removed and replaced with .html."
58715877
;;; Live Preview ===============================================================
58725878

58735879
(defvar markdown-live-preview-view-buffer nil
5874-
"Buffer used to preview markdown output in `markdown-live-preview-export'.")
5880+
"Buffer used to preview markdown output in
5881+
`markdown-live-preview-sync-export' and `markdown-live-preview-async-export'.")
58755882
(make-variable-buffer-local 'markdown-live-preview-view-buffer)
58765883

58775884
(defvar markdown-live-preview-source-buffer nil
@@ -5889,6 +5896,10 @@ buffer. Inverse of `markdown-live-preview-view-buffer'.")
58895896
`markdown-live-preview-async-export'.")
58905897
(make-variable-buffer-local 'markdown-live-preview-idle-timer)
58915898

5899+
(defvar markdown-live-preview-current-buffer-sync-async nil
5900+
"Status of sync or async live preview in current buffer.")
5901+
(make-variable-buffer-local 'markdown-live-preview-current-buffer-sync-async)
5902+
58925903
(defun markdown-live-preview-window-eww (file)
58935904
"A `markdown-live-preview-window-function' for previewing with `eww'."
58945905
(if (require 'eww nil t)
@@ -5928,14 +5939,14 @@ alive."
59285939
(set-window-point win pt)
59295940
(set-window-start win start))))
59305941

5931-
(defun markdown-live-preview-cleanup-export (buf msg)
5942+
(defun markdown-live-preview-async-cleanup-export (buf msg)
59325943
(with-current-buffer buf
59335944
(when (eq markdown-live-preview-delete-export 'delete-on-export)
59345945
(markdown-live-preview-delete-export))
59355946
(setq markdown-live-preview-currently-exporting-process nil))
59365947
(message msg))
59375948

5938-
(defun markdown-live-preview-create-view-display (src-buf out-file msg)
5949+
(defun markdown-live-preview-async-create-view-display (src-buf out-file msg)
59395950
(unwind-protect
59405951
(let (had-view-buffer window-data-list)
59415952
(with-current-buffer src-buf
@@ -5957,7 +5968,7 @@ alive."
59575968
do (markdown-live-preview-window-deserialize
59585969
window-data view-buf))
59595970
(markdown-display-buffer-other-window view-buf)))))
5960-
(markdown-live-preview-cleanup-export src-buf msg)))
5971+
(markdown-live-preview-async-cleanup-export src-buf msg)))
59615972

59625973
(defmacro markdown-silence-messages (&rest body)
59635974
`(cl-letf (((symbol-function #'message) (lambda (&rest _)))) ,@body))
@@ -5968,10 +5979,11 @@ alive."
59685979
(let ((cur-msg (current-message))
59695980
(proc-buf (process-buffer proc)))
59705981
(kill-buffer proc-buf)
5971-
(markdown-live-preview-create-view-display src-buf out-file cur-msg))))
5982+
(markdown-live-preview-async-create-view-display
5983+
src-buf out-file cur-msg))))
59725984

5973-
(defun markdown-live-preview-make-sentinel (src-buf out-file)
5974-
(lambda (proc msg)
5985+
(defun markdown-live-preview-make-async-sentinel (src-buf out-file)
5986+
(lambda (proc _)
59755987
(markdown-live-preview-do-sentinel src-buf out-file proc)))
59765988

59775989
(defconst markdown-live-preview-proc-name "*markdown-live-preview*")
@@ -5984,7 +5996,8 @@ alive."
59845996
(let* ((cur-buf (current-buffer))
59855997
(mode major-mode)
59865998
(out-file (markdown-live-preview-get-filename))
5987-
(sentinel (markdown-live-preview-make-sentinel cur-buf out-file))
5999+
(sentinel (markdown-live-preview-make-async-sentinel
6000+
cur-buf out-file))
59886001
(proc
59896002
(start-process-shell-command
59906003
markdown-live-preview-proc-name markdown-live-preview-buf-name
@@ -6004,23 +6017,64 @@ alive."
60046017
(run-hooks 'markdown-after-export-hook)
60056018
proc)))))
60066019

6020+
(defvar markdown-live-preview-currently-exporting nil)
6021+
6022+
(defun markdown-live-preview-sync-export ()
6023+
"Export to XHTML using `markdown-export' and browse the resulting file within
6024+
Emacs using `markdown-live-preview-window-function' Return the buffer displaying
6025+
the rendered output."
6026+
(interactive)
6027+
(let* ((markdown-live-preview-currently-exporting t)
6028+
(src-buf (current-buffer))
6029+
(export-file (markdown-export (markdown-live-preview-get-filename)))
6030+
;; get positions in all windows currently displaying output buffer
6031+
(window-data
6032+
(markdown-live-preview-window-serialize
6033+
markdown-live-preview-view-buffer)))
6034+
(save-window-excursion
6035+
(let ((view-buf
6036+
(funcall markdown-live-preview-window-function export-file)))
6037+
(markdown-live-preview-link-source-view-buffers src-buf view-buf)
6038+
(with-current-buffer view-buf
6039+
(add-hook 'kill-buffer-hook
6040+
#'markdown-live-preview-teardown-view t t))))
6041+
(with-current-buffer src-buf
6042+
;; reset all windows displaying output buffer to where they were,
6043+
;; now with the new output
6044+
(mapc #'markdown-live-preview-window-deserialize window-data)
6045+
;; delete html editing buffer
6046+
(let ((buf (get-file-buffer export-file))) (when buf (kill-buffer buf)))
6047+
(when (and export-file (file-exists-p export-file)
6048+
(eq markdown-live-preview-delete-export
6049+
'delete-on-export))
6050+
(delete-file export-file))
6051+
markdown-live-preview-view-buffer)))
6052+
60076053
(defun markdown-live-preview-update (buf)
60086054
(lexical-let ((buf buf))
60096055
(lambda () (with-current-buffer buf (markdown-live-preview-async-export)))))
60106056

60116057
(defun markdown-live-preview-setup ()
6058+
(setq markdown-live-preview-current-buffer-sync-async
6059+
markdown-live-preview-do-sync)
60126060
(add-hook 'kill-buffer-hook #'markdown-live-preview-teardown-source t t)
6013-
(markdown-live-preview-async-export)
6014-
(setq markdown-live-preview-idle-timer
6015-
(run-with-idle-timer
6016-
markdown-live-preview-idle-delay t
6017-
(markdown-live-preview-update (current-buffer)))))
6061+
(if markdown-live-preview-current-buffer-sync-async
6062+
(progn
6063+
(add-hook
6064+
'after-save-hook #'markdown-live-preview-do-sync-preview t t)
6065+
(markdown-live-preview-sync-export))
6066+
(setq markdown-live-preview-idle-timer
6067+
(run-with-idle-timer
6068+
markdown-live-preview-idle-delay t
6069+
(markdown-live-preview-update (current-buffer))))
6070+
(markdown-live-preview-async-export)))
60186071

60196072
(defun markdown-live-preview-teardown-view ()
60206073
(with-current-buffer markdown-live-preview-source-buffer
6021-
(setq markdown-live-preview-view-buffer nil)))
6074+
(setq markdown-live-preview-view-buffer nil))
6075+
(setq markdown-live-preview-source-buffer nil))
60226076

6023-
(defun markdown-live-preview-teardown-source ()
6077+
(defun markdown-live-preview-teardown-async ()
60246078
(when (buffer-live-p markdown-live-preview-view-buffer)
60256079
;; calls `kill-buffer-hook' within `markdown-live-preview-view-buffer'
60266080
(kill-buffer markdown-live-preview-view-buffer))
@@ -6036,6 +6090,22 @@ alive."
60366090
(setq markdown-live-preview-idle-timer nil))
60376091
(remove-hook 'kill-buffer-hook #'markdown-live-preview-teardown-source t))
60386092

6093+
(defun markdown-live-preview-teardown-sync ()
6094+
(when (buffer-live-p markdown-live-preview-view-buffer)
6095+
(kill-buffer markdown-live-preview-view-buffer))
6096+
(setq markdown-live-preview-view-buffer nil)
6097+
(remove-hook 'after-save-hook #'markdown-live-preview-do-sync-preview t)
6098+
;; if set to 'delete-on-export, the output has already been deleted
6099+
(when (eq markdown-live-preview-delete-export 'delete-on-destroy)
6100+
(let ((outfile-name (markdown-live-preview-get-filename)))
6101+
(when (file-exists-p outfile-name)
6102+
(delete-file outfile-name)))))
6103+
6104+
(defun markdown-live-preview-teardown-source ()
6105+
(if markdown-live-preview-current-buffer-sync-async
6106+
(markdown-live-preview-teardown-sync)
6107+
(markdown-live-preview-teardown-async)))
6108+
60396109
(defun markdown-display-buffer-other-window (buf)
60406110
(let ((cur-buf (current-buffer)))
60416111
(switch-to-buffer-other-window buf)
@@ -6046,21 +6116,26 @@ alive."
60466116
"Turn on `markdown-live-preview-mode' if not already on, and switch to its
60476117
output buffer in another window."
60486118
(if markdown-live-preview-mode
6049-
(if markdown-live-preview-view-buffer
6050-
(markdown-display-buffer-other-window
6051-
markdown-live-preview-view-buffer)
6052-
;; TODO: better error message
6053-
(message "%s" "Error: no view buffer was created!")))
6119+
(markdown-display-buffer-other-window markdown-live-preview-view-buffer))
60546120
(markdown-live-preview-mode))
60556121

60566122
(defun markdown-live-preview-re-export ()
60576123
(interactive)
60586124
"If the current buffer is a buffer displaying the exported version of a
6059-
`markdown-live-preview-mode' buffer, call `markdown-live-preview-export' and
6060-
update this buffer's contents."
6125+
`markdown-live-preview-mode' buffer, call `markdown-live-preview-sync-export'
6126+
and update this buffer's contents."
60616127
(when markdown-live-preview-source-buffer
60626128
(with-current-buffer markdown-live-preview-source-buffer
6063-
(markdown-live-preview-async-export))))
6129+
(if markdown-live-preview-current-buffer-sync-async
6130+
(markdown-live-preview-sync-export)
6131+
(markdown-live-preview-async-export)))))
6132+
6133+
(defun markdown-live-preview-do-sync-preview ()
6134+
(unless markdown-live-preview-currently-exporting
6135+
(if (buffer-live-p markdown-live-preview-view-buffer)
6136+
(markdown-live-preview-sync-export)
6137+
(markdown-display-buffer-other-window
6138+
(markdown-live-preview-sync-export)))))
60646139

60656140

60666141
;;; Links =====================================================================

tests/markdown-test.el

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3748,7 +3748,7 @@ Detail: https://github.com/jrblevin/markdown-mode/issues/79"
37483748
'around 'markdown-create-fake-eww)
37493749
(ad-activate #'markdown-live-preview-window-eww)))))
37503750

3751-
(ert-deftest test-markdown-ext/live-preview-exports ()
3751+
(defun markdown-test/live-preview-exports ()
37523752
(markdown-test-temp-file "inline.text"
37533753
(unless (require 'eww nil t)
37543754
(should-error (markdown-live-preview-mode)))
@@ -3768,7 +3768,14 @@ Detail: https://github.com/jrblevin/markdown-mode/issues/79"
37683768
(accept-process-output)
37693769
(should (buffer-live-p markdown-live-preview-view-buffer))))))
37703770

3771-
(ert-deftest test-markdown-ext/live-preview-delete-exports ()
3771+
(ert-deftest test-markdown-ext/live-preview-exports-sync ()
3772+
(let ((markdown-live-preview-do-sync t))
3773+
(markdown-test/live-preview-exports)))
3774+
3775+
(ert-deftest test-markdown-ext/live-preview-exports-async ()
3776+
(markdown-test/live-preview-exports))
3777+
3778+
(defun markdown-test/live-preview-delete-exports ()
37723779
(let ((markdown-live-preview-idle-delay .01)
37733780
file-output)
37743781
(markdown-temp-eww
@@ -3796,6 +3803,13 @@ Detail: https://github.com/jrblevin/markdown-mode/issues/79"
37963803
(should (file-exists-p file-output)))
37973804
(delete-file file-output)))))
37983805

3806+
(ert-deftest test-markdown-ext/live-preview-delete-exports-sync ()
3807+
(let ((markdown-live-preview-do-sync t))
3808+
(markdown-test/live-preview-delete-exports)))
3809+
3810+
(ert-deftest test-markdown-ext/live-preview-delete-exports-async ()
3811+
(markdown-test/live-preview-delete-exports))
3812+
37993813
(provide 'markdown-test)
38003814

38013815
;;; markdown-test.el ends here

0 commit comments

Comments
 (0)