Skip to content

Commit 40f3b37

Browse files
committed
feat: new option grip-command
add grip-command: auto, grip, go-grip or mdopn. delete grip-binary-path, grip-mdopen-path, grip-gogrip-path. delete grip-use-mdopen and grip-use-gogrip.
1 parent 1bd6913 commit 40f3b37

File tree

2 files changed

+106
-101
lines changed

2 files changed

+106
-101
lines changed

README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<!-- markdown-toc end -->
2424

2525
Instant Github-flavored Markdown/Org preview using [Grip](https://github.com/joeyespo/grip)
26-
(GitHub Readme Instant Preview) or [mdopen](https://github.com/immanelg/mdopen) or [go-grip](https://github.com/chrishrb/go-grip).
26+
(GitHub Readme Instant Preview), [mdopen](https://github.com/immanelg/mdopen) or [go-grip](https://github.com/chrishrb/go-grip).
2727

2828
## Prerequisite
2929

@@ -55,14 +55,14 @@ From melpa, `M-x package-install RET grip-mode RET`.
5555
;; Use keybindings
5656
(use-package grip-mode
5757
:ensure t
58-
:config (setq grip-use-mdopen t) ;; to use `mdopen` instead of `grip`
58+
:config (setq grip-command 'auto) ;; auto, grip, go-grip or mdopen
5959
:bind (:map markdown-mode-command-map
6060
("g" . grip-mode)))
6161
6262
;; Or using hooks
6363
(use-package grip-mode
6464
:ensure t
65-
:config (setq grip-use-gogrip t) ;; to use `go-grip` instead of `grip`
65+
:config (setq grip-command 'go-grip) ;; auto, grip, go-grip or mdopen
6666
:hook ((markdown-mode org-mode) . grip-mode))
6767
```
6868

@@ -84,11 +84,12 @@ Enjoy! :smile:
8484
Run `M-x customize-group RET grip RET` or set the variables.
8585

8686
```emacs-lisp
87-
;; Path to the grip binary
88-
(setq grip-binary-path "/path/to/grip")
87+
;; Command: auto, grip, go-grip or mdopen
88+
(setq grip-command 'auto)
8989
9090
;; Use embedded webkit to preview
9191
;; This requires GNU/Emacs version >= 26 and built with the `--with-xwidgets` option.
92+
;; mdopen doesn't support webkit preview.
9293
(setq grip-preview-use-webkit t)
9394
9495
;; You can use this variable to define another browser
@@ -101,21 +102,25 @@ Run `M-x customize-group RET grip RET` or set the variables.
101102
(setq grip-url-args '("arg1" "arg2" "etc"))
102103
103104
;; A base URL to another GitHub API.
105+
;; Only available for `grip'.
104106
(setq grip-github-api-url "")
105107
106108
;; A GitHub username for API authentication
109+
;; Only available for `grip'.
107110
(setq grip-github-user "")
108111
109112
;; A GitHub password or auth token for API auth
113+
;; Only available for `grip'.
110114
(setq grip-github-password "")
111115
116+
;; Preview hostname
117+
;; Only available for `grip'.
118+
(setq grip-preview-host "localhost")
119+
112120
;; When nil, update the preview after file saves only, instead of also
113121
;; after every text change
114122
(setq grip-update-after-change nil)
115123
116-
;; Preview hostname
117-
(setq grip-preview-host "localhost")
118-
119124
;; Sleep seconds to ensure the server starts
120125
(setq grip-sleep-time 2)
121126
```

grip-mode.el

Lines changed: 93 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -51,44 +51,28 @@
5151
:group 'markdown
5252
:link '(url-link :tag "Homepage" "https://github.com/seagle0128/grip-mode"))
5353

54-
(defcustom grip-binary-path "grip"
55-
"Path to the grip binary."
56-
:type 'file
57-
:group 'grip)
58-
59-
(defcustom grip-mdopen-path "mdopen"
60-
"Path to the mdopen binary."
61-
:type 'file
62-
:group 'grip)
63-
64-
(defcustom grip-use-mdopen nil
65-
"Use mdopen instead of grip if non-nil."
66-
:type 'boolean
67-
:group 'grip)
68-
69-
(defcustom grip-use-gogrip nil
70-
"Use go-grip instead of grip if non-nil."
71-
:type 'boolean
72-
:group 'grip)
73-
74-
(defcustom grip-gogrip-path "go-grip"
75-
"Path to the go-grip binary."
76-
:type 'file
54+
(defcustom grip-command 'auto
55+
"The command of grip."
56+
:type '(choice
57+
(const :tag "Automatic" auto)
58+
(const :tag "Grip" grip)
59+
(const :tag "Go-grip" go-grip)
60+
(const :tag "Mdopen" mdopen))
7761
:group 'grip)
7862

79-
(defcustom grip-gogrip-theme "auto"
63+
(defcustom grip-gogrip-theme 'auto
8064
"Theme choice for go-grip."
8165
:type '(choice
82-
(const :tag "Automatic" "auto")
83-
(const :tag "Dark" "dark")
84-
(const :tag "Light" "light"))
66+
(const :tag "Automatic" auto)
67+
(const :tag "Dark" dark)
68+
(const :tag "Light" light))
8569
:group 'grip)
8670

8771
(defcustom grip-preview-use-webkit t
8872
"Use embedded webkit to preview.
8973
9074
This requires Emacs GUI version >= 26 and built with the `--with-xwidgets`
91-
option."
75+
option. mdopen doesn't support webkit preview."
9276
:type 'boolean
9377
:group 'grip)
9478

@@ -103,17 +87,26 @@ Use default browser if nil. It respects `grip-preview-use-webkit'."
10387
:type '(repeat (string :tag "Argument")))
10488

10589
(defcustom grip-github-api-url ""
106-
"A base URL to another GitHub API."
90+
"A base URL to another GitHub API.
91+
Only available for `grip'."
10792
:type 'string
10893
:group 'grip)
10994

11095
(defcustom grip-github-user ""
111-
"A GitHub username for API authentication."
96+
"A GitHub username for API authentication.
97+
Only available for `grip'."
11298
:type 'string
11399
:group 'grip)
114100

115101
(defcustom grip-github-password ""
116-
"A GitHub password or auth token for API auth."
102+
"A GitHub password or auth token for API auth.
103+
Only available for `grip'."
104+
:type 'string
105+
:group 'grip)
106+
107+
(defcustom grip-preview-host "localhost"
108+
"Preview hostname.
109+
Only available for `grip'."
117110
:type 'string
118111
:group 'grip)
119112

@@ -124,11 +117,6 @@ When nil, only update the preview on file save."
124117
:type 'boolean
125118
:group 'grip)
126119

127-
(defcustom grip-preview-host "localhost"
128-
"Preview hostname."
129-
:type 'string
130-
:group 'grip)
131-
132120
(defcustom grip-sleep-time 2
133121
"Sleep seconds to ensure the server starts successfully."
134122
:type 'integer
@@ -182,67 +170,79 @@ Use default browser unless `xwidget' is available."
182170
(file-name-nondirectory grip--preview-file))
183171
(format "http://%s:%d" grip-preview-host grip--port)))
184172

173+
(defvar grip--command nil)
185174
(defun grip-start-process ()
186-
"Render and preview with grip or mdopen."
175+
"Render and preview."
187176
(unless (processp grip--process)
188-
(cond (grip-use-mdopen
189-
(progn
190-
(unless (and grip-mdopen-path (executable-find grip-mdopen-path))
191-
(grip-mode -1) ; Force to disable
192-
(user-error "The `mdopen' is not available in PATH environment"))
193-
(when grip--preview-file
194-
(setq grip--process
195-
(start-process "mdopen" "*mdopen*"
196-
grip-mdopen-path
197-
grip--preview-file))
198-
(message "Preview `%s' on %s" buffer-file-name (grip--preview-url)))))
199-
(grip-use-gogrip
200-
(progn
201-
(unless (and grip-gogrip-path (executable-find grip-gogrip-path))
202-
(grip-mode -1) ; Force to disable
203-
(user-error "The `go-grip' is not available in PATH environment"))
204-
;; Generate random port
205-
(while (< grip--port 6419)
206-
(setq grip--port (random 65535)))
207-
;; Start a new grip process
208-
(when grip--preview-file
209-
(setq grip--process
210-
(start-process (format "grip-%d" grip--port)
211-
(format " *grip-%d*" grip--port)
212-
grip-gogrip-path
213-
(format "--port=%d" grip--port)
214-
(format "--theme=%s" grip-gogrip-theme)
215-
"--browser=false"
216-
grip--preview-file))
217-
(message "Preview `%s' on %s" buffer-file-name (grip--preview-url))
218-
(sleep-for grip-sleep-time)
219-
(grip--browse-url (grip--preview-url)))))
220-
(t
221-
(progn
222-
(unless (and grip-binary-path (executable-find grip-binary-path))
223-
(grip-mode -1) ; Force to disable
224-
(user-error "The `grip' is not available in PATH environment"))
225-
;; Generate random port
226-
(while (< grip--port 6419)
227-
(setq grip--port (random 65535)))
228-
;; Start a new grip process
229-
(when grip--preview-file
230-
(setq grip--process
231-
(start-process (format "grip-%d" grip--port)
232-
(format " *grip-%d*" grip--port)
233-
grip-binary-path
234-
(format "--api-url=%s" grip-github-api-url)
235-
(format "--user=%s" grip-github-user)
236-
(format "--pass=%s" grip-github-password)
237-
(format "--title=%s - Grip" (buffer-name))
238-
grip--preview-file
239-
(number-to-string grip--port)))
240-
(message "Preview `%s' on %s" buffer-file-name (grip--preview-url))
241-
(sleep-for grip-sleep-time)
242-
(grip--browse-url (grip--preview-url))))))))
177+
(setq grip--command grip-command)
178+
(when (eq grip--command 'auto)
179+
(setq grip--command
180+
(cond
181+
((executable-find "go-grip") 'go-grip)
182+
((executable-find "mdopen") 'mdopen)
183+
((executable-find "grip") 'grip)
184+
(t (user-error "No grip comamnd is available in PATH environment")))))
185+
186+
(pcase grip--command
187+
('mdopen
188+
(progn
189+
(unless (executable-find "mdopen")
190+
(grip-mode -1) ; Force to disable
191+
(user-error "The `mdopen' is not available in PATH environment"))
192+
(when grip--preview-file
193+
(setq grip--process
194+
(start-process "grip" "*grip*" "mdopen" grip--preview-file))
195+
(message "Preview `%s' on %s" buffer-file-name (grip--preview-url)))))
196+
('go-grip
197+
(progn
198+
(unless (executable-find "go-grip")
199+
(grip-mode -1) ; Force to disable
200+
(user-error "The `go-grip' is not available in PATH environment"))
201+
;; Generate random port
202+
(while (< grip--port 6419)
203+
(setq grip--port (random 65535)))
204+
;; Start a new grip process
205+
(when grip--preview-file
206+
(setq grip--process
207+
(start-process (format "grip-%d" grip--port)
208+
(format " *grip-%d*" grip--port)
209+
"go-grip"
210+
(format "--port=%d" grip--port)
211+
(format "--theme=%s" grip-gogrip-theme)
212+
"--browser=false"
213+
grip--preview-file))
214+
(message "Preview `%s' on %s" buffer-file-name (grip--preview-url))
215+
(grip--browse-url (format "%s/%s"
216+
(grip--preview-url)
217+
(file-name-nondirectory grip--preview-file))))))
218+
('grip
219+
(progn
220+
(unless (executable-find "grip")
221+
(grip-mode -1) ; Force to disable
222+
(user-error "The `grip' is not available in PATH environment"))
223+
;; Generate random port
224+
(while (< grip--port 6419)
225+
(setq grip--port (random 65535)))
226+
;; Start a new grip process
227+
(when grip--preview-file
228+
(setq grip--process
229+
(start-process (format "grip-%d" grip--port)
230+
(format " *grip-%d*" grip--port)
231+
"grip"
232+
(format "--api-url=%s" grip-github-api-url)
233+
(format "--user=%s" grip-github-user)
234+
(format "--pass=%s" grip-github-password)
235+
(format "--title=%s - Grip" (buffer-name))
236+
grip--preview-file
237+
(number-to-string grip--port)))
238+
(message "Preview `%s' on %s" buffer-file-name (grip--preview-url))
239+
(sleep-for grip-sleep-time)
240+
(grip--browse-url (grip--preview-url)))))
241+
(_
242+
(user-error "No grip comamnd is available in PATH environment")))))
243243

244244
(defun grip--kill-process ()
245-
"Kill grip or mdopen or go-grip process."
245+
"Kill the preview process."
246246
(when grip--process
247247
;; Delete xwidget buffer
248248
(when (and grip-preview-use-webkit

0 commit comments

Comments
 (0)