Skip to content

Commit c23080d

Browse files
committed
feat: support real-time refresh for markdown file
- Provided new variable `grip-real-time-refresh` to enable such feature - Changed to remove preview file even the process is not existed
1 parent 26bdadf commit c23080d

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ Run `M-x customize-group RET grip RET` or set the variables.
121121
122122
;; Sleep seconds to ensure the server starts
123123
(setq grip-sleep-time 2)
124+
125+
;; Set to allow real-time refresh
126+
(setq grip-real-time-refresh t)
124127
```
125128

126129
If you don't set them you may have limitation to access Github APIs. Please

grip-mode.el

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ Only available for `grip'."
115115
:type 'integer
116116
:group 'grip)
117117

118+
(defcustom grip-real-time-refresh nil
119+
"Boolean value to allow real time refresh without saving the file. Support for markdown file only."
120+
:type 'boolean
121+
:group 'grip)
122+
118123

119124

120125
;; Externals
@@ -167,6 +172,10 @@ Use default browser unless `xwidget' is available."
167172
(format "http://%s:%d/%s" grip-preview-host grip--port
168173
(file-name-nondirectory grip--preview-file))))
169174

175+
(defun grip--refresh (&rest args)
176+
"Save into the temp file to trigger refresh."
177+
(write-region (point-min) (point-max) grip--preview-file nil 'quiet))
178+
170179
(defun grip-start-process ()
171180
"Render and preview."
172181
(unless (process-live-p grip--process)
@@ -204,7 +213,9 @@ Use default browser unless `xwidget' is available."
204213
(message "%s: Preview `%s' on %s"
205214
grip--command
206215
(abbreviate-file-name buffer-file-name)
207-
(grip--preview-url)))))
216+
(grip--preview-url))))
217+
;; Support real-time refresh
218+
(if grip-real-time-refresh (add-hook 'after-change-functions (function grip--refresh) nil t)))
208219
('go-grip
209220
(unless (executable-find "go-grip")
210221
(grip-mode -1)
@@ -273,15 +284,21 @@ Use default browser unless `xwidget' is available."
273284
(delete-process grip--process)
274285
(message "Process `%s' killed" grip--process)
275286
(setq grip--process nil)
276-
(setq grip--port 6418)
277-
;; Delete preview temporary file
278-
(when (and grip--preview-file
279-
(not (string-equal grip--preview-file buffer-file-name)))
280-
(delete-file grip--preview-file))))
287+
(setq grip--port 6418))
288+
;; Delete preview temporary file; As for kill-emacs case the process
289+
;; could be killed by other ways, process may not existed, hence
290+
;; deleting the file is separating out for the clean-up process.
291+
(when (and grip--preview-file
292+
(not (string-equal grip--preview-file buffer-file-name)))
293+
(delete-file grip--preview-file)))
281294

282295
(defun grip--preview-md ()
283296
"Render and preview markdown with grip."
284-
(setq grip--preview-file buffer-file-name)
297+
(if grip-real-time-refresh
298+
(progn
299+
(setq grip--preview-file (concat buffer-file-name ".temp.md"))
300+
(copy-file buffer-file-name grip--preview-file "overwrite"))
301+
(setq grip--preview-file buffer-file-name))
285302
(grip-start-process))
286303

287304
(defun grip-org-to-md (&rest _)

0 commit comments

Comments
 (0)