Add custom time variable.

This commit is contained in:
jenchieh 2020-08-30 15:01:46 +08:00
parent f98d9114ca
commit 77fe9d4b69
1 changed files with 20 additions and 14 deletions

View File

@ -79,6 +79,11 @@
(const :tag "Via http" http) (const :tag "Via http" http)
(const :tag "Off" nil))) (const :tag "Off" nil)))
(defcustom markdown-preview-delay-time 2.0
"Refresh preview after this certain of time."
:group 'markdown-preview
:type 'float)
(defvar markdown-preview-javascript '() (defvar markdown-preview-javascript '()
"List of client javascript libs for preview.") "List of client javascript libs for preview.")
@ -146,26 +151,26 @@
(with-temp-file preview-file (with-temp-file preview-file
(insert-file-contents (expand-file-name "preview.html" markdown-preview--home-dir)) (insert-file-contents (expand-file-name "preview.html" markdown-preview--home-dir))
(when (search-forward "${MD_STYLE}" nil t) (when (search-forward "${MD_STYLE}" nil t)
(replace-match (markdown-preview--css) t)) (replace-match (markdown-preview--css) t))
(when (search-forward "${MD_JS}" nil t) (when (search-forward "${MD_JS}" nil t)
(replace-match (markdown-preview--scripts) t)) (replace-match (markdown-preview--scripts) t))
(when (search-forward "${WS_HOST}" nil t) (when (search-forward "${WS_HOST}" nil t)
(replace-match markdown-preview-host t)) (replace-match markdown-preview-host t))
(when (search-forward "${WS_PORT}" nil t) (when (search-forward "${WS_PORT}" nil t)
(replace-match (format "%s" markdown-preview-ws-port) t)) (replace-match (format "%s" markdown-preview-ws-port) t))
(when (search-forward "${MD_UUID}" nil t) (when (search-forward "${MD_UUID}" nil t)
(replace-match (format "%s" preview-uuid) t)) (replace-match (format "%s" preview-uuid) t))
(buffer-string))) (buffer-string)))
;; Emacs 26 async network workaround ;; Emacs 26 async network workaround
(defun markdown-preview--fix-network-process-wait (plist) (defun markdown-preview--fix-network-process-wait (plist)
"Ensure PLIST contain :nowait nil." "Ensure PLIST contain :nowait nil."
(if (and (>= emacs-major-version 26) (if (and (>= emacs-major-version 26)
(equal (plist-get plist :name) "ws-server") (equal (plist-get plist :name) "ws-server")
(plist-get plist :server) (plist-get plist :server)
(plist-get plist :nowait)) (plist-get plist :nowait))
(plist-put plist :nowait nil) (plist-put plist :nowait nil)
plist)) plist))
(defun markdown-preview--start-http-server (port) (defun markdown-preview--start-http-server (port)
"Start http server at PORT to serve preview file via http." "Start http server at PORT to serve preview file via http."
@ -254,8 +259,8 @@
:on-message (lambda (websocket frame) :on-message (lambda (websocket frame)
(let ((ws-frame-text (websocket-frame-payload frame))) (let ((ws-frame-text (websocket-frame-payload frame)))
(if (and (if (and
(stringp ws-frame-text) (stringp ws-frame-text)
(string-prefix-p "MDPM-Register-UUID: " ws-frame-text)) (string-prefix-p "MDPM-Register-UUID: " ws-frame-text))
(let ((ws-uuid (substring ws-frame-text 20))) (let ((ws-uuid (substring ws-frame-text 20)))
(puthash ws-uuid websocket markdown-preview--remote-clients) (puthash ws-uuid websocket markdown-preview--remote-clients)
(markdown-preview--send-preview-to websocket ws-uuid)) (markdown-preview--send-preview-to websocket ws-uuid))
@ -327,9 +332,10 @@
(markdown-preview--start-local-client) (markdown-preview--start-local-client)
(markdown-preview--start-http-server markdown-preview-http-port) (markdown-preview--start-http-server markdown-preview-http-port)
(setq markdown-preview--idle-timer (setq markdown-preview--idle-timer
(run-with-idle-timer 2 t (lambda () (run-with-idle-timer markdown-preview-delay-time t
(when markdown-preview--uuid (lambda ()
(markdown-preview--send-preview markdown-preview--uuid))))) (when markdown-preview--uuid
(markdown-preview--send-preview markdown-preview--uuid)))))
(add-hook 'after-save-hook (lambda () (add-hook 'after-save-hook (lambda ()
(when markdown-preview--uuid (when markdown-preview--uuid
(markdown-preview--send-preview markdown-preview--uuid))) nil t)) (markdown-preview--send-preview markdown-preview--uuid))) nil t))