Skip to content

Commit

Permalink
Don't error when previewing buffer without file
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongtx committed Jun 15, 2017
1 parent 0e2d596 commit 96f9d4f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
61 changes: 33 additions & 28 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -7088,33 +7088,35 @@ Browse the resulting file within Emacs using
`markdown-live-preview-window-function' Return the buffer
displaying the rendered output."
(interactive)
(let* ((markdown-live-preview-currently-exporting t)
(cur-buf (current-buffer))
(export-file (markdown-export (markdown-live-preview-get-filename)))
;; get positions in all windows currently displaying output buffer
(window-data
(markdown-live-preview-window-serialize
markdown-live-preview-buffer)))
(save-window-excursion
(let ((output-buffer
(funcall markdown-live-preview-window-function export-file)))
(with-current-buffer output-buffer
(setq markdown-live-preview-source-buffer cur-buf)
(add-hook 'kill-buffer-hook
#'markdown-live-preview-remove-on-kill t t))
(let ((filename (markdown-live-preview-get-filename)))
(when filename
(let* ((markdown-live-preview-currently-exporting t)
(cur-buf (current-buffer))
(export-file (markdown-export filename))
;; get positions in all windows currently displaying output buffer
(window-data
(markdown-live-preview-window-serialize
markdown-live-preview-buffer)))
(save-window-excursion
(let ((output-buffer
(funcall markdown-live-preview-window-function export-file)))
(with-current-buffer output-buffer
(setq markdown-live-preview-source-buffer cur-buf)
(add-hook 'kill-buffer-hook
#'markdown-live-preview-remove-on-kill t t))
(with-current-buffer cur-buf
(setq markdown-live-preview-buffer output-buffer))))
(with-current-buffer cur-buf
(setq markdown-live-preview-buffer output-buffer))))
(with-current-buffer cur-buf
;; reset all windows displaying output buffer to where they were,
;; now with the new output
(mapc #'markdown-live-preview-window-deserialize window-data)
;; delete html editing buffer
(let ((buf (get-file-buffer export-file))) (when buf (kill-buffer buf)))
(when (and export-file (file-exists-p export-file)
(eq markdown-live-preview-delete-export
'delete-on-export))
(delete-file export-file))
markdown-live-preview-buffer)))
;; reset all windows displaying output buffer to where they were,
;; now with the new output
(mapc #'markdown-live-preview-window-deserialize window-data)
;; delete html editing buffer
(let ((buf (get-file-buffer export-file))) (when buf (kill-buffer buf)))
(when (and export-file (file-exists-p export-file)
(eq markdown-live-preview-delete-export
'delete-on-export))
(delete-file export-file))
markdown-live-preview-buffer)))))

(defun markdown-live-preview-remove ()
(when (buffer-live-p markdown-live-preview-buffer)
Expand All @@ -7123,7 +7125,7 @@ displaying the rendered output."
;; if set to 'delete-on-export, the output has already been deleted
(when (eq markdown-live-preview-delete-export 'delete-on-destroy)
(let ((outfile-name (markdown-live-preview-get-filename)))
(when (file-exists-p outfile-name)
(when (and outfile-name (file-exists-p outfile-name))
(delete-file outfile-name)))))

(defun markdown-get-other-window ()
Expand Down Expand Up @@ -8281,7 +8283,10 @@ position."
"Toggle native previewing on save for a specific markdown file."
:lighter " MD-Preview"
(if markdown-live-preview-mode
(markdown-display-buffer-other-window (markdown-live-preview-export))
(if (markdown-live-preview-get-filename)
(markdown-display-buffer-other-window (markdown-live-preview-export))
(markdown-live-preview-mode -1)
(user-error "Buffer %s does not visit a file" (current-buffer)))
(markdown-live-preview-remove)))


Expand Down
17 changes: 17 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -4701,6 +4701,23 @@ this is not header line
(message "no eww, no libxml2, or no %s found: skipping %s" markdown-command test)
nil))

(ert-deftest test-markdown-ext/live-preview-no-file ()
"Live-preview a `markdown-mode' buffer without a file."
(with-temp-buffer
(markdown-mode)

;; Activating `markdown-live-preview-mode' signals user error
(should-error (markdown-live-preview-mode) :type 'user-error)

;; After trying to activate live preview mode, mode is not activated
(should-not markdown-live-preview-mode)

;; `markdown-live-preview-export' does nothing
(should-not (markdown-live-preview-export))

;; `markdown-live-preview-remove' does nothing
(should-not (markdown-live-preview-remove))))

(ert-deftest test-markdown-ext/live-preview-exports ()
(markdown-test-temp-file "inline.text"
(unless (and (fboundp 'libxml-parse-html-region) (require 'eww nil t))
Expand Down

0 comments on commit 96f9d4f

Please sign in to comment.