You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
5.6 KiB

8 years ago
;;; markdown-preview-mode.el --- markdown realtime preview minor mode.
;; Copyright (C) 2014 <>
;; Author: Igor Shymko <>
;; URL:
;; Keywords: markdown, preview
;; Package-Requires: ((websocket "1.3") (markdown-mode "2.0") (cl-lib "0.5"))
;; 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
;; 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 <>.
;;; Code:
(require 'cl-lib)
(require 'websocket)
(require 'markdown-mode)
(defgroup markdown-preview nil
"Markdown preview mode"
:group 'text
:prefix "mdpm:"
:link '(url-link ""))
(defcustom markdown-preview-port 7379
"Markdown preview websocket server port"
:group 'markdown-preview
:type 'integer)
(defcustom markdown-preview-style ""
"Markdown preview style URI"
:group 'markdown-preview
:type 'string)
(defvar mdpm:websocket-server nil)
(defvar mdpm:local-client nil)
(defvar mdpm:remote-clients nil)
8 years ago
(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 ()
8 years ago
(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
:on-message (lambda (websocket frame)
(mapc (lambda (ws)
(websocket-send-text ws
(websocket-frame-payload frame)))
:on-open (lambda (websocket)
(push websocket mdpm:remote-clients)
(mdpm: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)
(defun mdpm:start-local-client ()
(when (not mdpm:local-client)
(setq mdpm:local-client
(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))))))
(defun mdpm:send-preview ()
(when (bound-and-true-p markdown-preview-mode)
(mdpm:send-preview-to mdpm:local-client)))
(defun mdpm:send-preview-to (websocket)
(let ((mark-position-percent
(* 100
(float (- (line-number-at-pos) (/ (count-screen-lines (window-start) (point)) 2)))
(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
"<span id='style'>"
"<span id='position-percentage'>"
"<div id='content'>"
(buffer-substring-no-properties (point-min) (point-max))
(defun mdpm:start ()
(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 mdpm:stop ()
(remove-hook 'after-save-hook 'mdpm:send-preview t)
(defun markdown-preview-open-browser ()
(defun markdown-preview-cleanup ()
(define-minor-mode markdown-preview-mode
"Markdown preview mode"
:group 'markdown-preview-mode
:init-value nil
(when (not (or
(equal major-mode 'markdown-mode)
(equal major-mode 'gfm-mode)))
(if markdown-preview-mode
(provide 'markdown-preview-mode)
;;; markdown-preview-mode.el ends here