blob: 2dc49b03c191d9f45e5734dc7e2503e1de33bb6b [file] [log] [blame]
jfarrell38ddc8f2013-09-10 12:12:41 -04001;;; thrift.el --- Major mode for Apache Thrift files
2
3;; Keywords: files
4
David Reisseaaf45c2009-03-30 22:52:46 +00005;; Licensed to the Apache Software Foundation (ASF) under one
6;; or more contributor license agreements. See the NOTICE file
7;; distributed with this work for additional information
8;; regarding copyright ownership. The ASF licenses this file
9;; to you under the Apache License, Version 2.0 (the
10;; "License"); you may not use this file except in compliance
11;; with the License. You may obtain a copy of the License at
12;;
13;; http://www.apache.org/licenses/LICENSE-2.0
14;;
15;; Unless required by applicable law or agreed to in writing,
16;; software distributed under the License is distributed on an
17;; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18;; KIND, either express or implied. See the License for the
19;; specific language governing permissions and limitations
20;; under the License.
21;;
22
jfarrell38ddc8f2013-09-10 12:12:41 -040023;;; Commentary:
24
25;;
26
27;;; Code:
28
Mark Sleeb5f93b02007-05-11 23:46:42 +000029(require 'font-lock)
30
31(defvar thrift-mode-hook nil)
jfarrell38ddc8f2013-09-10 12:12:41 -040032;;;###autoload
Mark Sleeb5f93b02007-05-11 23:46:42 +000033(add-to-list 'auto-mode-alist '("\\.thrift\\'" . thrift-mode))
34
35(defvar thrift-indent-level 2
36 "Defines 2 spaces for thrift indentation.")
37
38;; syntax coloring
39(defconst thrift-font-lock-keywords
40 (list
41 '("#.*$" . font-lock-comment-face) ;; perl style comments
David Reisscecbed82009-03-24 20:02:22 +000042 '("\\<\\(include\\|struct\\|exception\\|typedef\\|const\\|enum\\|service\\|extends\\|void\\|oneway\\|throws\\|optional\\|required\\)\\>" . font-lock-keyword-face) ;; keywords
Mark Sleeb5f93b02007-05-11 23:46:42 +000043 '("\\<\\(bool\\|byte\\|i16\\|i32\\|i64\\|double\\|string\\|binary\\|map\\|list\\|set\\)\\>" . font-lock-type-face) ;; built-in types
44 '("\\<\\([0-9]+\\)\\>" . font-lock-variable-name-face) ;; ordinals
45 '("\\<\\(\\w+\\)\\s-*(" (1 font-lock-function-name-face)) ;; functions
46 )
jfarrell38ddc8f2013-09-10 12:12:41 -040047 "Thrift Keywords.")
Mark Sleeb5f93b02007-05-11 23:46:42 +000048
49;; indentation
50(defun thrift-indent-line ()
51 "Indent current line as Thrift code."
52 (interactive)
53 (beginning-of-line)
54 (if (bobp)
55 (indent-line-to 0)
56 (let ((not-indented t) cur-indent)
57 (if (looking-at "^[ \t]*\\(}\\|throws\\)")
58 (if (looking-at "^[ \t]*}")
59 (progn
60 (save-excursion
61 (forward-line -1)
62 (setq cur-indent (- (current-indentation) thrift-indent-level)))
63 (if (< cur-indent 0)
64 (setq cur-indent 0)))
65 (progn
66 (save-excursion
67 (forward-line -1)
68 (if (looking-at "^[ \t]*[\\.<>[:word:]]+[ \t]+[\\.<>[:word:]]+[ \t]*(")
69 (setq cur-indent (+ (current-indentation) thrift-indent-level))
70 (setq cur-indent (current-indentation))))))
71 (save-excursion
72 (while not-indented
73 (forward-line -1)
74 (if (looking-at "^[ \t]*}")
75 (progn
76 (setq cur-indent (current-indentation))
77 (setq not-indented nil))
78 (if (looking-at "^.*{[^}]*$")
79 (progn
80 (setq cur-indent (+ (current-indentation) thrift-indent-level))
81 (setq not-indented nil))
82 (if (bobp)
83 (setq not-indented nil)))
84 (if (looking-at "^[ \t]*throws")
85 (progn
86 (setq cur-indent (- (current-indentation) thrift-indent-level))
87 (if (< cur-indent 0)
88 (setq cur-indent 0))
89 (setq not-indented nil))
90 (if (bobp)
91 (setq not-indented nil)))
92 (if (looking-at "^[ \t]*[\\.<>[:word:]]+[ \t]+[\\.<>[:word:]]+[ \t]*([^)]*$")
93 (progn
94 (setq cur-indent (+ (current-indentation) thrift-indent-level))
95 (setq not-indented nil))
96 (if (bobp)
97 (setq not-indented nil)))
98 (if (looking-at "^[ \t]*\\/\\*")
99 (progn
100 (setq cur-indent (+ (current-indentation) 1))
101 (setq not-indented nil))
102 (if (bobp)
103 (setq not-indented nil)))
104 (if (looking-at "^[ \t]*\\*\\/")
105 (progn
106 (setq cur-indent (- (current-indentation) 1))
107 (setq not-indented nil))
108 (if (bobp)
109 (setq not-indented nil)))
110 ))))
111 (if cur-indent
112 (indent-line-to cur-indent)
113 (indent-line-to 0)))))
114
115;; C/C++ comments; also allowing underscore in words
116(defvar thrift-mode-syntax-table
117 (let ((thrift-mode-syntax-table (make-syntax-table)))
118 (modify-syntax-entry ?_ "w" thrift-mode-syntax-table)
Bryan Duxbury261441c2009-03-24 00:39:27 +0000119 (modify-syntax-entry ?/ ". 1456" thrift-mode-syntax-table)
Mark Sleeb5f93b02007-05-11 23:46:42 +0000120 (modify-syntax-entry ?* ". 23" thrift-mode-syntax-table)
121 (modify-syntax-entry ?\n "> b" thrift-mode-syntax-table)
122 thrift-mode-syntax-table)
123 "Syntax table for thrift-mode")
David Reiss0c90f6f2008-02-06 22:18:40 +0000124
jfarrell38ddc8f2013-09-10 12:12:41 -0400125;;;###autoload
Mark Sleeb5f93b02007-05-11 23:46:42 +0000126(defun thrift-mode ()
jfarrell38ddc8f2013-09-10 12:12:41 -0400127 "Mode for editing Thrift files."
Mark Sleeb5f93b02007-05-11 23:46:42 +0000128 (interactive)
129 (kill-all-local-variables)
130 (set-syntax-table thrift-mode-syntax-table)
131 (set (make-local-variable 'font-lock-defaults) '(thrift-font-lock-keywords))
132 (setq major-mode 'thrift-mode)
133 (setq mode-name "Thrift")
134 (run-hooks 'thrift-mode-hook)
135 (set (make-local-variable 'indent-line-function) 'thrift-indent-line)
136 )
jfarrell38ddc8f2013-09-10 12:12:41 -0400137
138(provide 'thrift)
139;;; thrift.el ends here
140