forked from zwz/plantuml-mode
-
Notifications
You must be signed in to change notification settings - Fork 2
/
plantuml-mode.el
322 lines (292 loc) · 12.5 KB
/
plantuml-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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
;;; plantuml-mode.el --- Major mode for plantuml
;; Copyright (C) 2013 wildsoul
;; Copyright (C) 2012 Zhang Weize (zwz)
;; Filename: plantuml-mode.el
;; Description: This is a major mode for plantuml
;; Author: Zhang Weize (zwz)
;; wildsoul
;; Maintainer: wildsoul
;; Version: 0.3
;; Package-Requires: ((auto-complete "1.4"))
;; URL: https://github.com/wildsoul/plantuml-mode
;; Keywords: uml, ascii
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; A major mode for plantuml, see: http://plantuml.sourceforge.net/
;; Plantuml is an open-source tool in java that allows to quickly write :
;; - sequence diagram,
;; - use case diagram,
;; - class diagram,
;; - activity diagram,
;; - component diagram,
;; - state diagram
;; - object diagram
;; using a simple and intuitive language.
;;
;; to install:
;; (require 'plantuml-mode)
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change Log:
;;
;; version 0.3, 2013-10-30 wildsoul
;; Update regexp
;; Indent enabled
;; Auto-complete enabled
;;
;; version 0.2, 2010-09-20
;; Initialize the keywords from the -language output of plantuml.jar
;; instead of the hard-coded way.
;; version 0.1, 2010-08-25
;; First version
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'thingatpt)
(require 'auto-complete)
(defgroup plantuml-mode nil
"Major mode for editing plantuml file."
:group 'languages)
(defvar plantuml-jar-path nil )
(defvar plantuml-mode-hook nil "Standard hook for plantuml-mode.")
(defvar plantuml-mode-version nil "plantuml-mode version string.")
(defvar plantuml-mode-map nil "Keymap for plantuml-mode")
(defvar plantuml-indent-regexp-end "^[ \t]*\\(?:@enduml\\|endif\\|end\s+note\\|}\\)")
(defvar plantuml-indent-regexp-start"^[ \t]*\\(?:@startuml\\|\\(?:.*\\)?\s*\\(?:[<>.*a-z-|]+\\)?\s*\\(?:\\[[a-zA-Z]+\\]\\)?\s+if\\|note\s+over\\|note\s+\\(\\(?:\\(?:buttom\\|left\\|right\\|top\\)\\)\\)\\(?:\s+of\\)?\\|\\(?:class\\|enum\\)\s+.*{\\)")
(defvar plantuml-indent-regexp-arrow "^[ \t]*\\(?:\\(?:<\\|<|\\|o\\|\\*\\)\\(?:\\.\\|-\\)\\(?:down\\|up\\|left\\|right\\)?\\(?:\\.\\|-\\)\\|\\(?:-\\|\\.\\)\\(?:down\\|up\\|left\\|right\\)?\\(?:-\\|\\.\\)\\(?:>\\||>\\|\\*\\|o\\)\\)")
(defvar plantuml-indent-regexp-arrow-1 "\\(?:\\(?:<\\|<|\\|o\\|\\*\\)\\(?:\\.\\|-\\)\\(?:down\\|up\\|left\\|right\\)?\\(?:\\.\\|-\\)\\|\\(?:-\\|\\.\\)\\(?:down\\|up\\|left\\|right\\)?\\(?:-\\|\\.\\)\\(?:>\\||>\\|\\*\\|o\\)\\)")
(defvar plantuml-indent-regexp-arrow-2 "^\s*.+\s+\\(?:\\(?:<\\|<|\\|o\\|\\*\\)\\(?:\\.\\|-\\)\\(?:down\\|up\\|left\\|right\\)?\\(?:\\.\\|-\\)\\|\\(?:-\\|\\.\\)\\(?:down\\|up\\|left\\|right\\)?\\(?:-\\|\\.\\)\\(?:>\\||>\\|\\*\\|o\\)\\)")
(defvar plantuml-indent-offset 4)
;;; syntax table
(defvar plantuml-mode-syntax-table
(let ((synTable (make-syntax-table)))
(modify-syntax-entry ?' "< b" synTable)
(modify-syntax-entry ?\n "> b" synTable)
(modify-syntax-entry ?! "w" synTable)
(modify-syntax-entry ?@ "w" synTable)
(modify-syntax-entry ?# "'" synTable)
synTable)
"Syntax table for `plantuml-mode'.")
(defvar plantuml-types nil)
(defvar plantuml-keywords nil)
(defvar plantuml-preprocessors nil)
(defvar plantuml-builtins nil)
;; keyword completion
(defvar plantuml-kwdList nil "plantuml keywords.")
;;; font-lock
(defun plantuml-init ()
"Initialize the keywords or builtins from the cmdline language output"
(unless (file-exists-p plantuml-jar-path)
(error "Could not find plantuml.jar at %s" plantuml-jar-path))
(with-temp-buffer
(shell-command (concat "java -jar "
(shell-quote-argument plantuml-jar-path)
" -language") (current-buffer))
(goto-char (point-min))
(let ((found (search-forward ";" nil nil))
(word "")
(count 0)
(pos 0))
(while found
(forward-char)
(setq word (current-word))
(if (string= word "EOF") (setq found nil)
;; else
(forward-line)
(setq count (string-to-number (current-word)))
(beginning-of-line 2)
(setq pos (point))
(forward-line count)
(cond ((string= word "type")
(setq plantuml-types
(split-string
(buffer-substring-no-properties pos (point)))))
((string= word "keyword")
(setq plantuml-keywords
(split-string
(buffer-substring-no-properties pos (point)))))
((string= word "preprocessor")
(setq plantuml-preprocessors
(split-string
(buffer-substring-no-properties pos (point)))))
(t (setq plantuml-builtins
(append
plantuml-builtins
(split-string
(buffer-substring-no-properties pos (point)))))))
;; ((string= word "skinparameter")
;; ((string= word "color")))
(setq found (search-forward ";" nil nil)))))))
(unless plantuml-kwdList
(plantuml-init)
(defvar plantuml-types-regexp
(concat "^\\s *\\("
(regexp-opt plantuml-types 'words)
"\\|\\<\\(note\s+over\\|\\(?:end\s+note\\|note\s+\\(\\(?:\
\\(?:buttom\\|left\\|right\\|top\\)\\)\\)\\(?:\s+of\\)?\\)\\)\\>\\|\
\\<\\(\\(left\\|center\\|right\\)\s+\\(header\\|footer\\)\\)\\>\\)" ))
(defvar plantuml-keywords-regexp
(concat "^\\s *"
(regexp-opt plantuml-keywords 'words)
"\\|\\(?:<\\|<|\\|o\\|\\*\\)\\(?:\\.\\|-\\)\\(?:down\\|up\\|left\\|right\\)?\
\\(?:\\.\\|-\\)\\|\\(?:-\\|\\.\\)\\(?:down\\|up\\|left\\|right\\)?\\(?:-\\|\\.\\)\
\\(?:>\\||>\\|\\*\\|o\\)\\|--"))
(defvar plantuml-builtins-regexp (regexp-opt plantuml-builtins 'words))
(defvar plantuml-preprocessors-regexp (concat "^\\s *" (regexp-opt plantuml-preprocessors 'words)))
(setq plantuml-font-lock-keywords
`(
(,plantuml-types-regexp . font-lock-type-face)
(,plantuml-keywords-regexp . font-lock-keyword-face)
(,plantuml-builtins-regexp . font-lock-builtin-face)
(,plantuml-preprocessors-regexp . font-lock-preprocessor-face)))
(setq plantuml-kwdList (make-hash-table :test 'equal))
(mapc (lambda (x) (puthash x t plantuml-kwdList))
(append plantuml-types plantuml-keywords plantuml-builtins plantuml-preprocessors))
(put 'plantuml-kwdList 'risky-local-variable t))
(defun plantuml-indent-line ()
"Indent current line as plantuml code"
(interactive)
(beginning-of-line)
(if (bobp)
(indent-line-to 0)
(let ((not-indented t) cur-indent var-indent)
(if (looking-at plantuml-indent-regexp-end)
(progn
(save-excursion
(forward-line -1)
(if (looking-at plantuml-indent-regexp-start)
(setq cur-indent (current-indentation))
(setq cur-indent(- (current-indentation)
plantuml-indent-offset))))
(if (< cur-indent 0)
(setq cur-indent 0)))
(save-excursion
(while not-indented
(forward-line -1)
(cond
((looking-at plantuml-indent-regexp-start)
(setq cur-indent (+ (current-indentation)
plantuml-indent-offset)
not-indented nil))
((looking-at plantuml-indent-regexp-end)
(setq cur-indent (current-indentation)
not-indented nil))
((progn (forward-line 1)
(setq var-indent
(looking-at plantuml-indent-regexp-arrow))
(forward-line -1)
var-indent)
(cond
((> (setq var-indent
(string-match
(progn (string-match
plantuml-indent-regexp-arrow-1
(current-line-string))
(match-string-no-properties
0
(current-line-string)))
(current-line-string))) 0)
(setq cur-indent var-indent
not-indented nil))))
((progn (forward-line 1)
(setq var-indent
(looking-at plantuml-indent-regexp-arrow-2))
(forward-line -1)
var-indent)
(cond
('t
(let ((var-count 0) (var-flag t))
(while var-flag
(incf var-count)
(forward-line -1)
(cond ((bobp) (setq var-flag nil))
((looking-at plantuml-indent-regexp-arrow) nil)
((looking-at "^\s+$") nil)
((looking-at plantuml-indent-regexp-end) nil)
((looking-at plantuml-indent-regexp-start) nil)
('t (setq cur-indent (current-indentation)
not-indented nil
var-flag nil))))
(forward-line var-count)))))
((bobp) (setq not-indented nil))))))
(if cur-indent
(indent-line-to cur-indent)
(indent-line-to 0)))))
;;; auto-complete setup
(defvar ac-source-plantuml-sources
(append
'((candidates . (lambda () (append plantuml-types
plantuml-keywords
plantuml-builtins
plantuml-preprocessors)))
(symbol . "s"))))
(defun ac-plantuml-setup ()
"Add ac completion source to the front of `ac-sources'.
This affects only the current buffer."
(interactive)
(add-to-list 'ac-sources 'ac-source-plantuml-sources))
;;; setup
(add-hook 'plantuml-mode-hook 'ac-plantuml-setup)
(add-to-list 'auto-mode-alist '("\\.plu$" . plantuml-mode))
;;;###autoload
(defun plantuml-mode ()
"Major mode for plantuml.
Shortcuts Command Name
\\[plantuml-complete-symbol] `plantuml-complete-symbol'"
(interactive)
(kill-all-local-variables)
;; (python-mode) ; for indentation
(setq major-mode 'plantuml-mode
mode-name "plantuml")
(set-syntax-table plantuml-mode-syntax-table)
(use-local-map plantuml-mode-map)
(make-local-variable 'font-lock-defaults)
(setq-local indent-line-function 'plantuml-indent-line)
(setq font-lock-defaults '((plantuml-font-lock-keywords) nil t))
(run-mode-hooks 'plantuml-mode-hook))
(defun plantuml-complete-symbol ()
"Perform keyword completion on word before cursor."
(interactive)
(let ((posEnd (point))
(meat (thing-at-point 'symbol))
maxMatchResult)
(when (not meat) (setq meat ""))
(setq maxMatchResult (try-completion meat plantuml-kwdList))
(cond ((eq maxMatchResult t))
((null maxMatchResult)
(message "Can't find completion for \"%s\"" meat)
(ding))
((not (string= meat maxMatchResult))
(delete-region (- posEnd (length meat)) posEnd)
(insert maxMatchResult))
(t (message "Making completion list...")
(with-output-to-temp-buffer "*Completions*"
(display-completion-list
(all-completions meat plantuml-kwdList)
meat))
(message "Making completion list...%s" "done")))))
(provide 'plantuml-mode)
;;; plantuml-mode.el ends here