Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 189 lines (156 sloc) 6.914 kB
b4bd346 @yrashk Adds plantuml-mode
authored
1 ;; plantuml-mode.el -- Major mode for plantuml
2
3 ;; Author: Zhang Weize (zwz)
4 ;; Keywords: uml ascii
5
6 ;; You can redistribute this program and/or modify it under the terms
7 ;; of the GNU General Public License as published by the Free Software
8 ;; Foundation; either version 2, or (at your option) any later
9 ;; version.
10
11 ;;; DESCRIPTION
12
13 ;; A major mode for plantuml, see: http://plantuml.sourceforge.net/
14 ;; Plantuml is an open-source tool in java that allows to quickly write :
15 ;; - sequence diagram,
16 ;; - use case diagram,
17 ;; - class diagram,
18 ;; - activity diagram,
19 ;; - component diagram,
20 ;; - state diagram
21 ;; - object diagram
22 ;; using a simple and intuitive language.
23
24 ;;; HISTORY
25 ;; version 0.2, 2010-09-20 Initialize the keywords from the -language output of plantuml.jar instead of the hard-coded way.
26 ;; version 0.1, 2010-08-25 First version
27
28
29 (require 'thingatpt)
30
31 (defgroup plantuml-mode nil
32 "Major mode for editing plantuml file."
33 :group 'languages)
34
35 (defvar plantuml-jar-path (expand-file-name "~/.emacs.d/plantuml.jar"))
36
37 (defvar plantuml-mode-hook nil "Standard hook for plantuml-mode.")
38
39 (defvar plantuml-mode-version nil "plantuml-mode version string.")
40
41 (defvar plantuml-mode-map nil "Keymap for plantuml-mode")
42
43 ;;; syntax table
44 (defvar plantuml-mode-syntax-table
45 (let ((synTable (make-syntax-table)))
46 (modify-syntax-entry ?' "< b" synTable)
47 (modify-syntax-entry ?\n "> b" synTable)
48 (modify-syntax-entry ?! "w" synTable)
49 (modify-syntax-entry ?@ "w" synTable)
50 (modify-syntax-entry ?# "'" synTable)
51 synTable)
52 "Syntax table for `plantuml-mode'.")
53
54 (defvar plantuml-types nil)
55 (defvar plantuml-keywords nil)
56 (defvar plantuml-preprocessors nil)
57 (defvar plantuml-builtins nil)
58
59 ;; keyword completion
60 (defvar plantuml-kwdList nil "plantuml keywords.")
61
62 ;;; font-lock
63
64 (defun plantuml-init ()
65 "Initialize the keywords or builtins from the cmdline language output"
66 (unless (file-exists-p plantuml-jar-path)
67 (error "Could not find plantuml.jar at %s" plantuml-jar-path))
68 (with-temp-buffer
69 (shell-command (concat "java -jar "
70 (shell-quote-argument plantuml-jar-path)
71 " -language") (current-buffer))
72 (goto-char (point-min))
73 (let ((found (search-forward ";" nil nil))
74 (word "")
75 (count 0)
76 (pos 0))
77 (while found
78 (forward-char)
79 (setq word (current-word))
80 (if (string= word "EOF") (setq found nil)
81 ;; else
82 (forward-line)
83 (setq count (string-to-number (current-word)))
84 (beginning-of-line 2)
85 (setq pos (point))
86 (forward-line count)
87 (cond ((string= word "type")
88 (setq plantuml-types
89 (split-string
90 (buffer-substring-no-properties pos (point)))))
91 ((string= word "keyword")
92 (setq plantuml-keywords
93 (split-string
94 (buffer-substring-no-properties pos (point)))))
95 ((string= word "preprocessor")
96 (setq plantuml-preprocessors
97 (split-string
98 (buffer-substring-no-properties pos (point)))))
99 (t (setq plantuml-builtins
100 (append
101 plantuml-builtins
102 (split-string
103 (buffer-substring-no-properties pos (point)))))))
104 ;; ((string= word "skinparameter")
105 ;; ((string= word "color")))
106 (setq found (search-forward ";" nil nil)))))))
107
108 (unless plantuml-kwdList
109 (plantuml-init)
110 (defvar plantuml-types-regexp (concat "^\\s *\\(" (regexp-opt plantuml-types 'words) "\\|\\<\\(note\\s +over\\|note\\s +\\(left\\|right\\|bottom\\|top\\)\\s +\\(of\\)?\\)\\>\\|\\<\\(\\(left\\|center\\|right\\)\\s +\\(header\\|footer\\)\\)\\>\\)"))
111 (defvar plantuml-keywords-regexp (concat "^\\s *" (regexp-opt plantuml-keywords 'words) "\\|\\(<\\|<|\\|\\*\\|o\\)\\(\\.+\\|-+\\)\\|\\(\\.+\\|-+\\)\\(>\\||>\\|\\*\\|o\\)\\|\\.\\{2,\\}\\|-\\{2,\\}"))
112 (defvar plantuml-builtins-regexp (regexp-opt plantuml-builtins 'words))
113 (defvar plantuml-preprocessors-regexp (concat "^\\s *" (regexp-opt plantuml-preprocessors 'words)))
114
115 (setq plantuml-font-lock-keywords
116 `(
117 (,plantuml-types-regexp . font-lock-type-face)
118 (,plantuml-keywords-regexp . font-lock-keyword-face)
119 (,plantuml-builtins-regexp . font-lock-builtin-face)
120 (,plantuml-preprocessors-regexp . font-lock-preprocessor-face)
121 ;; note: order matters
122 ))
123
124 (setq plantuml-kwdList (make-hash-table :test 'equal))
125 (mapc (lambda (x) (puthash x t plantuml-kwdList)) plantuml-types)
126 (mapc (lambda (x) (puthash x t plantuml-kwdList)) plantuml-keywords)
127 (mapc (lambda (x) (puthash x t plantuml-kwdList)) plantuml-builtins)
128 (mapc (lambda (x) (puthash x t plantuml-kwdList)) plantuml-preprocessors)
129 (put 'plantuml-kwdList 'risky-local-variable t)
130
131 ;; clear memory
132 (setq plantuml-types nil)
133 (setq plantuml-keywords nil)
134 (setq plantuml-builtins nil)
135 (setq plantuml-preprocessors nil)
136 (setq plantuml-types-regexp nil)
137 (setq plantuml-keywords-regexp nil)
138 (setq plantuml-builtins-regexp nil)
139 (setq plantuml-preprocessors-regexp nil))
140
141 (defun plantuml-complete-symbol ()
142 "Perform keyword completion on word before cursor."
143 (interactive)
144 (let ((posEnd (point))
145 (meat (thing-at-point 'symbol))
146 maxMatchResult)
147
148 (when (not meat) (setq meat ""))
149
150 (setq maxMatchResult (try-completion meat plantuml-kwdList))
151 (cond ((eq maxMatchResult t))
152 ((null maxMatchResult)
153 (message "Can't find completion for \"%s\"" meat)
154 (ding))
155 ((not (string= meat maxMatchResult))
156 (delete-region (- posEnd (length meat)) posEnd)
157 (insert maxMatchResult))
158 (t (message "Making completion list...")
159 (with-output-to-temp-buffer "*Completions*"
160 (display-completion-list
161 (all-completions meat plantuml-kwdList)
162 meat))
163 (message "Making completion list...%s" "done")))))
164
165 (add-to-list 'auto-mode-alist '("\\.plu$" . plantuml-mode))
166
167 ;;;###autoload
168 (defun plantuml-mode ()
169 "Major mode for plantuml.
170
171 Shortcuts Command Name
172 \\[plantuml-complete-symbol] `plantuml-complete-symbol'"
173
174 (interactive)
175 (kill-all-local-variables)
176
177 ;; (python-mode) ; for indentation
178 (setq major-mode 'plantuml-mode
179 mode-name "plantuml")
180 (set-syntax-table plantuml-mode-syntax-table)
181 (use-local-map plantuml-mode-map)
182
183 (make-local-variable 'font-lock-defaults)
184 (setq font-lock-defaults '((plantuml-font-lock-keywords) nil t))
185
186 (run-mode-hooks 'plantuml-mode-hook))
187
188 (provide 'plantuml-mode)
Something went wrong with that request. Please try again.