-
Notifications
You must be signed in to change notification settings - Fork 2
/
pikchr-mode.el
241 lines (215 loc) · 9.06 KB
/
pikchr-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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
;;; pikchr-mode.el --- A major mode for the pikchr diagram markup language -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Johann Klähn
;; Author: Johann Klähn <johann@jklaehn.de>
;; URL: https://github.com/kljohann/pikchr-mode
;; Keywords: languages
;; Version: 0
;; Package-Requires: ((emacs "27.1"))
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; A major mode for the pikchr (https://pikchr.org/) diagram markup language.
;;; Code:
(require 'ob)
(require 'regexp-opt)
(require 'rng-util)
(require 'rx)
(defgroup pikchr nil
"Pikchr support for Emacs."
:group 'languages)
(defcustom pikchr-executable
(or (executable-find "pikchr")
"pikchr")
"Location of the pikchr executable."
:group 'pikchr
:type '(file :must-match t)
:risky t)
(defconst pikchr-preview-buffer-name " *pikchr preview*")
(defun pikchr--face-to-style (face)
"Derive CSS declarations from FACE."
(format "font-family: %s;"
(face-attribute face :family)))
(defun pikchr-preview-region (start end)
"Preview the pikchr diagram in the region between START and END."
(interactive "r")
(let ((infile (make-temp-file "pikchr-mode-"))
(preview-buffer (get-buffer-create pikchr-preview-buffer-name)))
(unwind-protect
(progn
(write-region start end infile nil 'silent)
(let ((inhibit-read-only t))
(with-current-buffer preview-buffer
(erase-buffer)
(call-process pikchr-executable nil
t nil "--svg-only" infile))))
(delete-file infile))
(with-current-buffer preview-buffer
(goto-char (point-min))
(if (looking-at (rx "<svg" (group-n 1 (* (not ">"))) ">"))
(let* ((tag (save-match-data (xml-parse-tag)))
(attrs (xml-node-attributes tag))
(extra-style (pikchr--face-to-style 'variable-pitch))
(inhibit-read-only t))
;; Prepend extra CSS declarations to the style attribute.
(cl-callf2 concat extra-style (alist-get 'style attrs ""))
(replace-match "" 'fixed 'literal nil 1)
(cl-loop for (key . value) in attrs
do (insert ?\ (symbol-name key) "=\""
(xml-escape-string value) ?\"))
(image-mode))
(delete-trailing-whitespace)
(pikchr-mode)
(font-lock-ensure)
(view-mode)))
(display-buffer preview-buffer)))
(defun pikchr-preview-dwim ()
"Preview the pikchr diagram in the current buffer.
Uses the region if it is active. If a prefix argument is given
instead, the buffer up to and including the current line.
Else, the whole buffer is used."
(interactive)
(cond ((use-region-p)
(pikchr-preview-region (region-beginning) (region-end)))
(current-prefix-arg
(pikchr-preview-region (point-min) (line-end-position)))
(t (pikchr-preview-region (point-min) (point-max)))))
(defmacro pikchr-mode--rx-let (&rest body)
(declare (indent defun) (debug (body)))
`(rx-let ((ws* (* space))
(symb (| word (syntax symbol)))
(stmt-start (| bol ";"))
(place-name (: (any "A-Z") (* (any alnum "_"))))
(place-label (: stmt-start ws* (group place-name) ws* ":"))
(variable-def
(&rest extra)
(: stmt-start ws* (group (any extra "$@") (+ symb))
ws* (? (any "-+*/")) "="))
(macro-def (: stmt-start ws* "define" (+ space)
(group (any "a-z" "_$@") (+ symb)))))
,@body))
(defconst pikchr-mode-font-lock-keywords
(pikchr-mode--rx-let
`(
;; Ordinals
(,(rx bow (+ digit) (| "th" "rd" "nd" "st") eow)
. font-lock-builtin-face)
;; Hexadecimal integer constants
(,(rx bow "0x" (+ digit) eow)
. font-lock-constant-face)
;; Decimal integer or floating point constants
(,(rx
bow
(? (any "+-"))
(| (: (+ digit) (? "." (* digit)))
(: "." (+ digit)))
(? (any "eE")
(? any "+-")
(+ digit))
(? (| "%" "in" "cm" "mm" "pt" "px" "pc"))
eow)
. font-lock-constant-face)
;; Built-in variables
(,(regexp-opt
'("arcrad" "arrowhead" "arrowht" "arrowwid" "bottommargin" "boxht"
"boxrad" "boxwid" "charht" "charwid" "circlerad" "color" "cylht"
"cylrad" "cylwid" "dashwid" "dotrad" "ellipseht" "ellipsewid" "fileht"
"filerad" "filewid" "fill" "fontscale" "layer" "leftmargin" "lineht"
"linerad" "linewid" "margin" "movewid" "ovalht" "ovalwid" "rightmargin"
"scale" "textht" "textwid" "thickness" "topmargin") 'symbols)
. font-lock-builtin-face)
;; Variable definitions
(,(rx (variable-def "a-z"))
(1 font-lock-variable-name-face))
;; Place labels
(,(rx place-label)
(1 font-lock-function-name-face))
;; Macros
(,(rx macro-def)
(1 font-lock-function-name-face))
(,(regexp-opt '("define") 'symbols)
. font-lock-preprocessor-face)
;; Objects
(,(regexp-opt
'("arc" "arrow" "box" "circle" "cylinder" "dot" "ellipse" "file" "line"
"move" "oval" "spline" "text") 'symbols)
. font-lock-type-face)
;; Keywords
(,(regexp-opt
'("above" "abs" "aligned" "and" "as" "assert" "at" "behind" "below"
"between" "big" "bold" "bot" "bottom" "c" "ccw" "center" "chop" "close"
"color" "cos" "cw" "dashed" "diameter" "dist" "dotted" "down"
"e" "east" "end" "even" "fill" "first" "fit" "from" "go" "heading"
"height" "ht" "in" "int" "invis" "invisible" "italic" "last" "left"
"ljust" "max" "min" "n" "ne" "north" "nw" "of" "previous" "print" "rad"
"radius" "right" "rjust" "s" "same" "se" "sin" "small" "solid" "south"
"sqrt" "start" "sw" "t" "the" "then" "thick" "thickness" "thin" "to"
"top" "until" "up" "vertex" "w" "way" "west" "wid" "width" "with"
"x" "y" "<-" "->" "<->") 'symbols)
. font-lock-keyword-face))))
(defconst pikchr-mode-syntax-table
(let ((table (make-syntax-table)))
(modify-syntax-entry ?\n ">" table)
(modify-syntax-entry ?# "<" table)
(modify-syntax-entry ?$ "_" table)
(modify-syntax-entry '(?% . ?&) "." table)
(modify-syntax-entry ?* ". 23b" table)
(modify-syntax-entry '(?+ . ?.) "." table)
(modify-syntax-entry ?@ "_" table)
(modify-syntax-entry ?< "(>" table)
(modify-syntax-entry ?= "." table)
(modify-syntax-entry ?> ")<" table)
(modify-syntax-entry ?/ ". 124" table)
(modify-syntax-entry ?_ "." table)
(modify-syntax-entry ?| "." table)
table)
"Syntax table for `pikchr-mode'.")
(defconst pikchr-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") 'pikchr-preview-dwim)
map)
"Keymap for `pikchr-mode'.")
;;;###autoload
(define-derived-mode pikchr-mode prog-mode "pikchr mode"
"Major mode for the pikchr diagram markup language."
:syntax-table pikchr-mode-syntax-table
(setq-local comment-use-syntax t)
(setq-local comment-start "#")
(setq-local comment-end "")
(setq font-lock-defaults '(pikchr-mode-font-lock-keywords))
(setq-local imenu-generic-expression
(pikchr-mode--rx-let
`(("Places" ,(rx place-label) 1)
;; Only variable definitions beginning with $ and @.
("Variables" ,(rx (variable-def)) 1)
("Macros" ,(rx macro-def) 1)))))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.pikchr\\'" . pikchr-mode))
(defvar org-babel-default-header-args:pikchr
'((:results . "output file") (:exports . "results"))
"Default arguments to use when evaluating a pikchr source block.")
(defun org-babel-execute:pikchr (body params)
"Execute a block of pikchr code with org-babel.
This function is called by `org-babel-execute-src-block', which passes
the block contents as BODY and its header argumenst as PARAMS."
(unless (assq :file params)
(user-error "Missing :file header argument for pikchr block"))
(let ((in-file (org-babel-temp-file "pikchr-")))
(with-temp-file in-file
(insert body))
(org-babel-eval
(concat (org-babel-process-file-name pikchr-executable)
" --svg-only " (org-babel-process-file-name in-file))
"")))
(defun org-babel-prep-session:pikchr (_session _params)
"Return an error because pikchr does not support sessions."
(user-error "Sessions are not supported by pikchr"))
(provide 'pikchr-mode)
;;; pikchr-mode.el ends here