r1277@opsdev009 (orig r69359): mcslee | 2007-11-12 21:35:34 -0800
[amiethrift.git] / thrift.el
blob2e79051671d53d070db67c7d2eec9180df9dfbd8
1 (require 'font-lock)
3 (defvar thrift-mode-hook nil)
4 (add-to-list 'auto-mode-alist '("\\.thrift\\'" . thrift-mode))
6 (defvar thrift-indent-level 2
7 "Defines 2 spaces for thrift indentation.")
9 ;; syntax coloring
10 (defconst thrift-font-lock-keywords
11 (list
12 '("#.*$" . font-lock-comment-face) ;; perl style comments
13 '("\\<\\(include\\|struct\\|exception\\|typedef\\|cpp_namespace\\|java_package\\|cocoa_prefix\\|php_namespace\\|ruby_namespace\\|py_module\\|perl_package\\|const\\|enum\\|service\\|extends\\|void\\|async\\|throws\\|optional\\|required\\)\\>" . font-lock-keyword-face) ;; keywords
14 '("\\<\\(bool\\|byte\\|i16\\|i32\\|i64\\|double\\|string\\|binary\\|map\\|list\\|set\\)\\>" . font-lock-type-face) ;; built-in types
15 '("\\<\\([0-9]+\\)\\>" . font-lock-variable-name-face) ;; ordinals
16 '("\\<\\(\\w+\\)\\s-*(" (1 font-lock-function-name-face)) ;; functions
18 "Thrift Keywords")
20 ;; indentation
21 (defun thrift-indent-line ()
22 "Indent current line as Thrift code."
23 (interactive)
24 (beginning-of-line)
25 (if (bobp)
26 (indent-line-to 0)
27 (let ((not-indented t) cur-indent)
28 (if (looking-at "^[ \t]*\\(}\\|throws\\)")
29 (if (looking-at "^[ \t]*}")
30 (progn
31 (save-excursion
32 (forward-line -1)
33 (setq cur-indent (- (current-indentation) thrift-indent-level)))
34 (if (< cur-indent 0)
35 (setq cur-indent 0)))
36 (progn
37 (save-excursion
38 (forward-line -1)
39 (if (looking-at "^[ \t]*[\\.<>[:word:]]+[ \t]+[\\.<>[:word:]]+[ \t]*(")
40 (setq cur-indent (+ (current-indentation) thrift-indent-level))
41 (setq cur-indent (current-indentation))))))
42 (save-excursion
43 (while not-indented
44 (forward-line -1)
45 (if (looking-at "^[ \t]*}")
46 (progn
47 (setq cur-indent (current-indentation))
48 (setq not-indented nil))
49 (if (looking-at "^.*{[^}]*$")
50 (progn
51 (setq cur-indent (+ (current-indentation) thrift-indent-level))
52 (setq not-indented nil))
53 (if (bobp)
54 (setq not-indented nil)))
55 (if (looking-at "^[ \t]*throws")
56 (progn
57 (setq cur-indent (- (current-indentation) thrift-indent-level))
58 (if (< cur-indent 0)
59 (setq cur-indent 0))
60 (setq not-indented nil))
61 (if (bobp)
62 (setq not-indented nil)))
63 (if (looking-at "^[ \t]*[\\.<>[:word:]]+[ \t]+[\\.<>[:word:]]+[ \t]*([^)]*$")
64 (progn
65 (setq cur-indent (+ (current-indentation) thrift-indent-level))
66 (setq not-indented nil))
67 (if (bobp)
68 (setq not-indented nil)))
69 (if (looking-at "^[ \t]*\\/\\*")
70 (progn
71 (setq cur-indent (+ (current-indentation) 1))
72 (setq not-indented nil))
73 (if (bobp)
74 (setq not-indented nil)))
75 (if (looking-at "^[ \t]*\\*\\/")
76 (progn
77 (setq cur-indent (- (current-indentation) 1))
78 (setq not-indented nil))
79 (if (bobp)
80 (setq not-indented nil)))
81 ))))
82 (if cur-indent
83 (indent-line-to cur-indent)
84 (indent-line-to 0)))))
86 ;; C/C++ comments; also allowing underscore in words
87 (defvar thrift-mode-syntax-table
88 (let ((thrift-mode-syntax-table (make-syntax-table)))
89 (modify-syntax-entry ?_ "w" thrift-mode-syntax-table)
90 (modify-syntax-entry ?/ ". 124b" thrift-mode-syntax-table)
91 (modify-syntax-entry ?* ". 23" thrift-mode-syntax-table)
92 (modify-syntax-entry ?\n "> b" thrift-mode-syntax-table)
93 thrift-mode-syntax-table)
94 "Syntax table for thrift-mode")
96 (defun thrift-mode ()
97 "Mode for editing Thrift files"
98 (interactive)
99 (kill-all-local-variables)
100 (set-syntax-table thrift-mode-syntax-table)
101 (set (make-local-variable 'font-lock-defaults) '(thrift-font-lock-keywords))
102 (setq major-mode 'thrift-mode)
103 (setq mode-name "Thrift")
104 (run-hooks 'thrift-mode-hook)
105 (set (make-local-variable 'indent-line-function) 'thrift-indent-line)
107 (provide 'thrift-mode)