forked from andorsk/d2-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd2-mode.el
226 lines (185 loc) · 7.39 KB
/
d2-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
;;; d2-mode.el --- Major mode for working with d2 graphs -*- lexical-binding: t; -*-
;; Author: Andor Kesselman <[email protected]>
;; Copyright (C) 2022, Andor Kesselman
;; Heavily inspired by Mermaid Mode
;; https://github.com/abrochard/mermaid-mode
;;
;; 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 of the License, 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 this program. If not, see <http://www.gnu.org/licenses/>.
;; Version: 1.0
;; Author: Andor Kesselman
;; Keywords: d2 graphs tools processes
;; URL: https://github.com/andorsk/d2-mode
;; License: GNU General Public License >= 3
;; Package-Requires: ((emacs "26.1"))
;;; Commentary:
;; Major mode for working with d2 graphs.
;; See https://github.com/terrastruct/d2
;;; Usage:
;; Currently supporting flow charts and sequence diagrams with syntax coloring and indentation.
;; C-c C-c to compile to an image
;; C-c C-f to compile file to an image
;; C-c C-r to compile region to an image
;; C-c C-b to compile buffer to an image
;; C-c C-o to open in the live editor
;; C-c C-d to open the official doc
;;; Customization:
;; You can specify the location of `d2` with the variable `d2-bin-location`,
;; the default assumes you have the binary in your exec PATH.
;; By default `d2` will compile to `svg` format.
;; You can change that by setting the variable `d2-output-format`.
;; By default `d2` will use `/tmp` to store tmp-files.
;; You can change that by setting the variable `d2-tmp-dir`.
;; This code was heavily inspired by mermaid-mode @
;; https://github.com/abrochard/mermaid-mode
;; STATE: Alpha. Up for review by Melpa.
;;
(require 'browse-url)
(require 'ob)
(require 'ob-eval)
;;; Code:
(defgroup d2 nil
"Major mode for working with d2 graphs."
:group 'extensions
:link '(url-link :tag "Repository" "https://github.com/andorsk/d2-mode"))
(defcustom d2-location "d2"
"D2 binary location."
:type 'string
:group 'd2)
(defcustom d2-output-format ".svg"
"D2 output format."
:group 'd2
:type 'string)
(defcustom d2-tmp-dir (temporary-file-directory)
"Dir for tmp files."
:group 'd2
:type 'string)
(defcustom d2-flags ""
"Additional flags to pass to the d2-cli."
:group 'd2
:type 'string)
(defconst d2-font-lock-keywords
`((,(regexp-opt '("shape" "md" ) 'words) . font-lock-keyword-face)
("---\\|-?->*\\+?\\|==>\\|===|->" . font-lock-variable-name-face)
(":\\|{\\|}\\|\|\\|+" . font-lock-builtin-face)
(,(regexp-opt '("go" "js") 'lang) . font-lock-preprocessor-face)
(,(regexp-opt '("class" "string" ) 'words2) . font-lock-type-face)))
(defvar d2-syntax-table
(let ((syntax-table (make-syntax-table)))
;; Comment style "%% ..."
(modify-syntax-entry ?% ". 124" syntax-table)
(modify-syntax-entry ?\n ">" syntax-table)
syntax-table)
"Syntax table for `d2-mode'.")
(defvar org-babel-default-header-args:d2
'((:results . "file") (:exports . "results"))
"Default arguments for evaluating a d2 source block.")
(defun org-babel-execute:d2 (body params)
"Execute command with BODY and PARAMS from src block."
(let* ((out-file (or (cdr (assoc :file params))
(error "D2 requires a \":file\" header argument")))
(temp-file (org-babel-temp-file "d2-"))
(cmd (concat (shell-quote-argument d2-location)
" " temp-file
" " (org-babel-process-file-name out-file)
" " d2-flags)))
(with-temp-file temp-file (insert body))
(org-babel-eval cmd "")
nil))
(defun d2--locate-declaration (str)
"Locate a certain declaration and return the line difference and indentation.
STR is the declaration."
(let ((l (line-number-at-pos)))
(save-excursion
(if (re-search-backward str (point-min) t)
(cons (- l (line-number-at-pos)) (current-indentation))
(cons -1 -1)))))
(defun d2-compile ()
"Compile the current d2 file using d2."
(interactive)
(d2-compile-file (buffer-file-name)))
(defun d2-compile-buffer ()
"Compile the current d2 buffer using d2."
(interactive)
(let* ((tmp-file-name (concat d2-tmp-dir "current-buffer.d2")))
(write-region (point-min) (point-max) tmp-file-name)
(d2-compile-file-with-options tmp-file-name)))
(defun d2-compile-region (&optional browse)
"Compile the current d2 region using d2.
Optional argument BROWSE locate a certain declaration."
(interactive)
(let* ((tmp-file-name (concat d2-tmp-dir "current-region.d2")))
(when (use-region-p)
(write-region (region-beginning) (region-end) tmp-file-name)
(d2-compile-file tmp-file-name browse))))
(defun d2-compile-region-and-browse ()
"Compile the current d2 region using d2 and browse on browser."
(interactive)
(d2-compile-region t))
(defun d2-compile-file (file-name &optional browse)
"Compile the given d2 file using d2. BROWSE option determine opening method.
Argument FILE-NAME the input file."
(interactive "fFilename: ")
(let* ((input file-name)
(output (concat (file-name-sans-extension input) d2-output-format)))
(apply #'call-process d2-location nil "*d2*" nil (list input output))
(if (equal browse t)
(progn
(d2-browse-file output))
(progn
(display-buffer (find-file-noselect output t))))))
(defun d2-view-current-svg ()
"View the current svg in the browser."
(interactive)
(d2-browse-file buffer-file-name))
(defun d2-browse-file (file-name)
"View a file in the browser.
Argument FILE-NAME the input file."
(interactive "fFilename: ")
(let ((url file-name))
(browse-url url)))
(defun d2-compile-file-with-options (file-name &optional browse)
"Compile file with options.
Argument FILE-NAME the input file.
Optional argument BROWSE whether to open the browser."
(interactive "fFilename: ")
(message "compiling with current options")
(d2-compile-file file-name browse))
(defun d2-compile-buffer-and-browse()
"Compile buffer and browse."
(interactive)
(d2-compile-file-with-options buffer-file-name t))
(defun d2-open-doc ()
"Open the d2 home page and doc."
(interactive)
(browse-url "https://github.com/terrastruct/d2"))
(defvar d2-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") 'd2-compile)
(define-key map (kbd "C-c C-f") 'd2-compile-file)
(define-key map (kbd "C-c C-b") 'd2-compile-buffer)
(define-key map (kbd "C-c C-r") 'd2-compile-region)
(define-key map (kbd "C-c C-m") 'd2-compile-file-and-browse)
(define-key map (kbd "C-c C-j") 'd2-compile-buffer-and-browse)
(define-key map (kbd "C-c C-k") 'd2-compile-region-and-browse)
(define-key map (kbd "C-c C-o") 'd2-open-browser)
(define-key map (kbd "C-x C-o") 'd2-view-current-svg)
(define-key map (kbd "C-c C-d") 'd2-open-doc)
map))
;;;###autoload
(define-derived-mode d2-mode prog-mode "d2"
:syntax-table d2-syntax-table
(setq-local font-lock-defaults '(d2-font-lock-keywords))
(setq-local comment-start "%%")
(setq-local comment-end "")
(setq-local comment-start-skip "%%+ *"))
(provide 'd2-mode)
;;; d2-mode.el ends here