Merge pull request #5 from AdrieanKhisbe/master

Minor README improvement
pull/8/head
Igor Shymko 7 years ago
commit e251b74523
  1. 7
      README.md
  2. 133
      markdown-preview-mode.el

@ -1,15 +1,18 @@
Markdown preview mode
===========================
> **Markdown realtime preview minor mode**
## Description
Opens a preview in a browser, updated upon buffer save.
Same preview window is valid across multiple markdown buffers.
Scrolls browser window to keep your editing position visible.
## Dependencies
* markdown-mode.el
* websocket.el
* [markdown-mode.el](https://github.com/defunkt/markdown-mode)
* [websocket.el](https://github.com/ahyatt/emacs-websocket)
Makes use of `markdown-mode`, which already can transform markdown into html
and `websocket.el` to deliver html to browser.

@ -31,7 +31,7 @@
(defgroup markdown-preview nil
"Markdown preview mode"
:group 'text
:prefix "mdpm:"
:prefix "markdown-preview-"
:link '(url-link "https://github.com/ancane/markdown-preview-mode"))
(defcustom markdown-preview-port 7379
@ -44,64 +44,77 @@
:group 'markdown-preview
:type 'string)
(defvar mdpm:websocket-server nil)
(defvar mdpm:local-client nil)
(defvar mdpm:remote-clients nil)
(defvar mdpm:preview-url (concat (file-name-directory load-file-name) "preview.html"))
(defvar mdpm:idle-timer nil "Preview idle timer")
(defun mdpm:stop-idle-timer ()
(when (timerp mdpm:idle-timer)
(cancel-timer mdpm:idle-timer)))
(defun mdpm:open-browser-preview ()
(browse-url mdpm:preview-url))
(defun mdpm:stop-websocket-server ()
(when mdpm:local-client
(websocket-close mdpm:local-client))
(when mdpm:websocket-server
(delete-process mdpm:websocket-server)
(setq mdpm:websocket-server nil
mdpm:remote-clients nil)))
(defun mdpm:drop-closed-clients ()
(setq mdpm:remote-clients
(cl-remove-if-not #'websocket-openp mdpm:remote-clients)))
(defun mdpm:start-websocket-server ()
(when (not mdpm:websocket-server)
(setq mdpm:websocket-server
(defvar markdown-preview--websocket-server nil
"`markdown-preview' Websocket server.")
(defvar markdown-preview--local-client nil
"`markdown-preview' local client.")
(defvar markdown-preview--remote-clients nil
"List of `markdown-preview' websocket remote clients.")
(defvar markdown-preview--preview-url
(concat (file-name-directory load-file-name) "preview.html")
"Location of `markdown-preview' html.")
(defvar markdown-preview--idle-timer nil "Preview idle timer")
(defun markdown-preview--stop-idle-timer ()
"Stop the `markdown-preview' idle timer."
(when (timerp markdown-preview--idle-timer)
(cancel-timer markdown-preview--idle-timer)))
(defun markdown-preview--open-browser-preview ()
"Open the markdown preview in the browser."
(browse-url markdown-preview--preview-url))
(defun markdown-preview--stop-websocket-server ()
"Stop the `markdown-preview' websocket server."
(when markdown-preview--local-client
(websocket-close markdown-preview--local-client))
(when markdown-preview--websocket-server
(delete-process markdown-preview--websocket-server)
(setq markdown-preview--websocket-server nil
markdown-preview--remote-clients nil)))
(defun markdown-preview--drop-closed-clients ()
"Clean closed clients in `markdown-preview--remote-clients' list."
(setq markdown-preview--remote-clients
(cl-remove-if-not #'websocket-openp markdown-preview--remote-clients)))
(defun markdown-preview--start-websocket-server ()
"Start `markdown-preview' websocket server."
(when (not markdown-preview--websocket-server)
(setq markdown-preview--websocket-server
(websocket-server
markdown-preview-port
:on-message (lambda (websocket frame)
(mapc (lambda (ws)
(websocket-send-text ws
(websocket-frame-payload frame)))
mdpm:remote-clients))
markdown-preview--remote-clients))
:on-open (lambda (websocket)
(push websocket mdpm:remote-clients)
(mdpm:send-preview-to websocket))
(push websocket markdown-preview--remote-clients)
(markdown-preview--send-preview-to websocket))
:on-error (lambda (websocket type err) (message (concat "====> Error:" err)))
:on-close (lambda (websocket) (mdpm:drop-closed-clients))))
(add-hook 'kill-emacs-hook 'mdpm:stop-websocket-server)
(mdpm:open-browser-preview)))
(defun mdpm:start-local-client ()
(when (not mdpm:local-client)
(setq mdpm:local-client
:on-close (lambda (websocket) (markdown-preview--drop-closed-clients))))
(add-hook 'kill-emacs-hook 'markdown-preview--stop-websocket-server)
(markdown-preview--open-browser-preview)))
(defun markdown-preview--start-local-client ()
"Start the `markdown-preview' local client."
(when (not markdown-preview--local-client)
(setq markdown-preview--local-client
(websocket-open
(format "ws://localhost:%d" markdown-preview-port)
:on-error (lambda (ws type err)
(message "error connecting"))
:on-close (lambda (websocket)
(setq mdpm:local-client nil))))))
(setq markdown-preview--local-client nil))))))
(defun mdpm:send-preview ()
(defun markdown-preview--send-preview ()
"Send the `markdown-preview' preview to clients."
(when (bound-and-true-p markdown-preview-mode)
(mdpm:send-preview-to mdpm:local-client)))
(markdown-preview--send-preview-to markdown-preview--local-client)))
(defun mdpm:send-preview-to (websocket)
(defun markdown-preview--send-preview-to (websocket)
"Send the `markdown-preview' to a specific WEBSOCKET."
(let ((mark-position-percent
(number-to-string
(truncate
@ -127,29 +140,33 @@
"</div>")
))))
(defun mdpm:start ()
(mdpm:start-websocket-server)
(mdpm:start-local-client)
(setq mdpm:idle-timer
(run-with-idle-timer 2 t 'mdpm:send-preview))
(add-hook 'after-save-hook 'mdpm:send-preview nil t)
(add-hook 'kill-buffer-hook 'mdpm:stop))
(defun markdown-preview--start ()
"Start `markdown-preview' mode."
(markdown-preview--start-websocket-server)
(markdown-preview--start-local-client)
(setq markdown-preview--idle-timer
(run-with-idle-timer 2 t 'markdown-preview--send-preview))
(add-hook 'after-save-hook 'markdown-preview--send-preview nil t)
(add-hook 'kill-buffer-hook 'markdown-preview--stop))
(defun mdpm:stop ()
(remove-hook 'after-save-hook 'mdpm:send-preview t)
(mdpm:stop-idle-timer))
(defun markdown-preview--stop ()
"Stop `markdown-preview' mode."
(remove-hook 'after-save-hook 'markdown-preview--send-preview t)
(markdown-preview--stop-idle-timer))
(defun markdown-preview-open-browser ()
"Open the `markdown-preview' in the browser."
(interactive)
(mdpm:open-browser-preview))
(markdown-preview--open-browser-preview))
(defun markdown-preview-cleanup ()
"Cleanup `markdown-preview' mode."
(interactive)
(mdpm:stop-websocket-server))
(markdown-preview--stop-websocket-server))
;;;###autoload
(define-minor-mode markdown-preview-mode
"Markdown preview mode"
"Markdown preview mode."
:group 'markdown-preview-mode
:init-value nil
(when (not (or
@ -157,8 +174,8 @@
(equal major-mode 'gfm-mode)))
(markdown-mode))
(if markdown-preview-mode
(mdpm:start)
(mdpm:stop)))
(markdown-preview--start)
(markdown-preview--stop)))
(provide 'markdown-preview-mode)

Loading…
Cancel
Save