2014-09-15 07:30:13 +02:00
|
|
|
;;; markdown-preview-mode.el --- markdown realtime preview minor mode.
|
2014-09-14 11:18:24 +02:00
|
|
|
|
|
|
|
;; Copyright (C) 2014 <igor.shimko@gmail.com>
|
|
|
|
|
2014-09-14 14:39:16 +02:00
|
|
|
;; Author: Igor Shymko <igor.shimko@gmail.com>
|
2014-09-14 11:18:24 +02:00
|
|
|
;; URL: https://github.com/ancane/markdown-preview-mode
|
|
|
|
;; Keywords: markdown, preview
|
2016-01-06 06:02:31 +01:00
|
|
|
;; Package-Requires: ((websocket "1.3") (markdown-mode "2.0") (cl-lib "0.5"))
|
2014-09-14 11:18:24 +02:00
|
|
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
;; any later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-01-06 06:02:31 +01:00
|
|
|
;;; Code:
|
2014-09-14 11:18:24 +02:00
|
|
|
|
2016-01-06 06:02:31 +01:00
|
|
|
(require 'cl-lib)
|
2014-09-14 11:18:24 +02:00
|
|
|
(require 'websocket)
|
|
|
|
(require 'markdown-mode)
|
|
|
|
|
2014-09-15 21:39:14 +02:00
|
|
|
(defgroup markdown-preview nil
|
2014-09-14 11:18:24 +02:00
|
|
|
"Markdown preview mode"
|
|
|
|
:group 'text
|
2014-09-15 21:39:14 +02:00
|
|
|
:prefix "mdpm:"
|
|
|
|
:link '(url-link "https://github.com/ancane/markdown-preview-mode"))
|
|
|
|
|
|
|
|
(defcustom markdown-preview-port 7379
|
|
|
|
"Markdown preview websocket server port"
|
|
|
|
:group 'markdown-preview
|
|
|
|
:type 'integer)
|
|
|
|
|
|
|
|
(defcustom markdown-preview-style "http://thomasf.github.io/solarized-css/solarized-dark.min.css"
|
|
|
|
"Markdown preview style URI"
|
|
|
|
:group 'markdown-preview
|
|
|
|
:type 'string)
|
2014-09-14 11:18:24 +02:00
|
|
|
|
|
|
|
(defvar mdpm:websocket-server nil)
|
2014-09-14 14:39:16 +02:00
|
|
|
(defvar mdpm:local-client nil)
|
|
|
|
(defvar mdpm:remote-clients nil)
|
2014-09-14 16:50:39 +02:00
|
|
|
(defvar mdpm:preview-url (concat (file-name-directory load-file-name) "preview.html"))
|
2014-10-25 22:19:58 +02:00
|
|
|
(defvar mdpm:idle-timer nil "Preview idle timer")
|
|
|
|
|
|
|
|
(defun mdpm:stop-idle-timer ()
|
|
|
|
(when (timerp mdpm:idle-timer)
|
|
|
|
(cancel-timer mdpm:idle-timer)))
|
2014-09-14 11:18:24 +02:00
|
|
|
|
2014-09-14 14:39:16 +02:00
|
|
|
(defun mdpm:open-browser-preview ()
|
2014-09-14 16:50:39 +02:00
|
|
|
(browse-url mdpm:preview-url))
|
2014-09-14 14:39:16 +02:00
|
|
|
|
|
|
|
(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)))
|
2014-09-14 11:18:24 +02:00
|
|
|
|
2014-09-14 15:15:48 +02:00
|
|
|
(defun mdpm:drop-closed-clients ()
|
|
|
|
(setq mdpm:remote-clients
|
2016-01-06 06:02:31 +01:00
|
|
|
(cl-remove-if-not #'websocket-openp mdpm:remote-clients)))
|
2014-09-14 15:15:48 +02:00
|
|
|
|
2014-09-14 11:18:24 +02:00
|
|
|
(defun mdpm:start-websocket-server ()
|
|
|
|
(when (not mdpm:websocket-server)
|
|
|
|
(setq mdpm:websocket-server
|
|
|
|
(websocket-server
|
2014-09-15 21:39:14 +02:00
|
|
|
markdown-preview-port
|
2014-09-14 11:18:24 +02:00
|
|
|
:on-message (lambda (websocket frame)
|
|
|
|
(mapc (lambda (ws)
|
2014-09-14 14:39:16 +02:00
|
|
|
(websocket-send-text ws
|
2014-09-14 15:15:48 +02:00
|
|
|
(websocket-frame-payload frame)))
|
2014-09-14 14:39:16 +02:00
|
|
|
mdpm:remote-clients))
|
2014-09-14 17:06:33 +02:00
|
|
|
:on-open (lambda (websocket)
|
|
|
|
(push websocket mdpm:remote-clients)
|
2014-10-25 22:19:58 +02:00
|
|
|
(mdpm:send-preview-to websocket))
|
2014-09-14 16:41:52 +02:00
|
|
|
:on-error (lambda (websocket type err) (message (concat "====> Error:" err)))
|
2014-09-14 17:06:33 +02:00
|
|
|
:on-close (lambda (websocket) (mdpm:drop-closed-clients))))
|
2014-09-14 14:39:16 +02:00
|
|
|
(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
|
|
|
|
(websocket-open
|
2014-09-15 21:39:14 +02:00
|
|
|
(format "ws://localhost:%d" markdown-preview-port)
|
2014-09-14 14:39:16 +02:00
|
|
|
:on-error (lambda (ws type err)
|
|
|
|
(message "error connecting"))
|
|
|
|
:on-close (lambda (websocket)
|
|
|
|
(setq mdpm:local-client nil))))))
|
2014-09-14 11:18:24 +02:00
|
|
|
|
2014-09-14 14:39:16 +02:00
|
|
|
(defun mdpm:send-preview ()
|
2014-10-27 12:11:21 +01:00
|
|
|
(when (bound-and-true-p markdown-preview-mode)
|
|
|
|
(mdpm:send-preview-to mdpm:local-client)))
|
2014-09-14 17:06:33 +02:00
|
|
|
|
2014-10-25 22:19:58 +02:00
|
|
|
(defun mdpm:send-preview-to (websocket)
|
2014-10-23 01:14:12 +02:00
|
|
|
(let ((mark-position-percent
|
|
|
|
(number-to-string
|
|
|
|
(truncate
|
|
|
|
(* 100
|
|
|
|
(/
|
2014-10-27 12:11:21 +01:00
|
|
|
(float (- (line-number-at-pos) (/ (count-screen-lines (window-start) (point)) 2)))
|
2014-10-23 01:14:12 +02:00
|
|
|
(count-lines (point-min) (point-max))))))))
|
|
|
|
(when markdown-preview-mode
|
|
|
|
(markdown markdown-output-buffer-name))
|
|
|
|
(with-current-buffer (get-buffer markdown-output-buffer-name)
|
|
|
|
(websocket-send-text websocket
|
|
|
|
(concat
|
|
|
|
"<div>"
|
|
|
|
"<span id='style'>"
|
|
|
|
markdown-preview-style
|
|
|
|
"</span>"
|
|
|
|
"<span id='position-percentage'>"
|
|
|
|
mark-position-percent
|
|
|
|
"</span>"
|
|
|
|
"<div id='content'>"
|
|
|
|
(buffer-substring-no-properties (point-min) (point-max))
|
|
|
|
"</div>"
|
|
|
|
"</div>")
|
|
|
|
))))
|
2014-09-14 11:18:24 +02:00
|
|
|
|
|
|
|
(defun mdpm:start ()
|
|
|
|
(mdpm:start-websocket-server)
|
2014-09-14 14:39:16 +02:00
|
|
|
(mdpm:start-local-client)
|
2014-10-25 22:19:58 +02:00
|
|
|
(setq mdpm:idle-timer
|
|
|
|
(run-with-idle-timer 2 t 'mdpm:send-preview))
|
|
|
|
(add-hook 'after-save-hook 'mdpm:send-preview nil t)
|
2014-10-27 12:20:43 +01:00
|
|
|
(add-hook 'kill-buffer-hook 'mdpm:stop))
|
2014-09-14 11:18:24 +02:00
|
|
|
|
|
|
|
(defun mdpm:stop ()
|
2014-10-25 22:19:58 +02:00
|
|
|
(remove-hook 'after-save-hook 'mdpm:send-preview t)
|
|
|
|
(mdpm:stop-idle-timer))
|
2014-09-14 11:18:24 +02:00
|
|
|
|
2014-09-14 12:19:03 +02:00
|
|
|
(defun markdown-preview-open-browser ()
|
|
|
|
(interactive)
|
2014-09-14 14:39:16 +02:00
|
|
|
(mdpm:open-browser-preview))
|
2014-09-14 12:19:03 +02:00
|
|
|
|
2014-09-14 17:06:33 +02:00
|
|
|
(defun markdown-preview-cleanup ()
|
2014-09-14 12:19:03 +02:00
|
|
|
(interactive)
|
|
|
|
(mdpm:stop-websocket-server))
|
|
|
|
|
2014-09-14 11:18:24 +02:00
|
|
|
(define-minor-mode markdown-preview-mode
|
|
|
|
"Markdown preview mode"
|
|
|
|
:group 'markdown-preview-mode
|
|
|
|
:init-value nil
|
2015-03-31 23:01:37 +02:00
|
|
|
(when (not (or
|
|
|
|
(equal major-mode 'markdown-mode)
|
|
|
|
(equal major-mode 'gfm-mode)))
|
2014-09-14 11:18:24 +02:00
|
|
|
(markdown-mode))
|
2014-09-14 12:19:03 +02:00
|
|
|
(if markdown-preview-mode
|
2014-09-14 11:18:24 +02:00
|
|
|
(mdpm:start)
|
|
|
|
(mdpm:stop)))
|
|
|
|
|
|
|
|
(provide 'markdown-preview-mode)
|
|
|
|
|
|
|
|
;;; markdown-preview-mode.el ends here
|