jfarrell | 38ddc8f | 2013-09-10 12:12:41 -0400 | [diff] [blame] | 1 | ;;; thrift.el --- Major mode for Apache Thrift files |
| 2 | |
| 3 | ;; Keywords: files |
| 4 | |
David Reiss | eaaf45c | 2009-03-30 22:52:46 +0000 | [diff] [blame] | 5 | ;; 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 | |
jfarrell | 38ddc8f | 2013-09-10 12:12:41 -0400 | [diff] [blame] | 23 | ;;; Commentary: |
| 24 | |
| 25 | ;; |
| 26 | |
| 27 | ;;; Code: |
| 28 | |
Mark Slee | b5f93b0 | 2007-05-11 23:46:42 +0000 | [diff] [blame] | 29 | (require 'font-lock) |
| 30 | |
| 31 | (defvar thrift-mode-hook nil) |
jfarrell | 38ddc8f | 2013-09-10 12:12:41 -0400 | [diff] [blame] | 32 | ;;;###autoload |
Mark Slee | b5f93b0 | 2007-05-11 23:46:42 +0000 | [diff] [blame] | 33 | (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 |
David Reiss | cecbed8 | 2009-03-24 20:02:22 +0000 | [diff] [blame] | 41 | '("\\<\\(include\\|struct\\|exception\\|typedef\\|const\\|enum\\|service\\|extends\\|void\\|oneway\\|throws\\|optional\\|required\\)\\>" . font-lock-keyword-face) ;; keywords |
Mark Slee | b5f93b0 | 2007-05-11 23:46:42 +0000 | [diff] [blame] | 42 | '("\\<\\(bool\\|byte\\|i16\\|i32\\|i64\\|double\\|string\\|binary\\|map\\|list\\|set\\)\\>" . font-lock-type-face) ;; built-in types |
| 43 | '("\\<\\([0-9]+\\)\\>" . font-lock-variable-name-face) ;; ordinals |
| 44 | '("\\<\\(\\w+\\)\\s-*(" (1 font-lock-function-name-face)) ;; functions |
| 45 | ) |
jfarrell | 38ddc8f | 2013-09-10 12:12:41 -0400 | [diff] [blame] | 46 | "Thrift Keywords.") |
Mark Slee | b5f93b0 | 2007-05-11 23:46:42 +0000 | [diff] [blame] | 47 | |
| 48 | ;; indentation |
| 49 | (defun thrift-indent-line () |
| 50 | "Indent current line as Thrift code." |
| 51 | (interactive) |
| 52 | (beginning-of-line) |
| 53 | (if (bobp) |
| 54 | (indent-line-to 0) |
| 55 | (let ((not-indented t) cur-indent) |
| 56 | (if (looking-at "^[ \t]*\\(}\\|throws\\)") |
| 57 | (if (looking-at "^[ \t]*}") |
| 58 | (progn |
| 59 | (save-excursion |
| 60 | (forward-line -1) |
| 61 | (setq cur-indent (- (current-indentation) thrift-indent-level))) |
| 62 | (if (< cur-indent 0) |
| 63 | (setq cur-indent 0))) |
| 64 | (progn |
| 65 | (save-excursion |
| 66 | (forward-line -1) |
| 67 | (if (looking-at "^[ \t]*[\\.<>[:word:]]+[ \t]+[\\.<>[:word:]]+[ \t]*(") |
| 68 | (setq cur-indent (+ (current-indentation) thrift-indent-level)) |
| 69 | (setq cur-indent (current-indentation)))))) |
| 70 | (save-excursion |
| 71 | (while not-indented |
| 72 | (forward-line -1) |
| 73 | (if (looking-at "^[ \t]*}") |
| 74 | (progn |
| 75 | (setq cur-indent (current-indentation)) |
| 76 | (setq not-indented nil)) |
| 77 | (if (looking-at "^.*{[^}]*$") |
| 78 | (progn |
| 79 | (setq cur-indent (+ (current-indentation) thrift-indent-level)) |
| 80 | (setq not-indented nil)) |
| 81 | (if (bobp) |
| 82 | (setq not-indented nil))) |
| 83 | (if (looking-at "^[ \t]*throws") |
| 84 | (progn |
| 85 | (setq cur-indent (- (current-indentation) thrift-indent-level)) |
| 86 | (if (< cur-indent 0) |
| 87 | (setq cur-indent 0)) |
| 88 | (setq not-indented nil)) |
| 89 | (if (bobp) |
| 90 | (setq not-indented nil))) |
| 91 | (if (looking-at "^[ \t]*[\\.<>[:word:]]+[ \t]+[\\.<>[:word:]]+[ \t]*([^)]*$") |
| 92 | (progn |
| 93 | (setq cur-indent (+ (current-indentation) thrift-indent-level)) |
| 94 | (setq not-indented nil)) |
| 95 | (if (bobp) |
| 96 | (setq not-indented nil))) |
| 97 | (if (looking-at "^[ \t]*\\/\\*") |
| 98 | (progn |
| 99 | (setq cur-indent (+ (current-indentation) 1)) |
| 100 | (setq not-indented nil)) |
| 101 | (if (bobp) |
| 102 | (setq not-indented nil))) |
| 103 | (if (looking-at "^[ \t]*\\*\\/") |
| 104 | (progn |
| 105 | (setq cur-indent (- (current-indentation) 1)) |
| 106 | (setq not-indented nil)) |
| 107 | (if (bobp) |
| 108 | (setq not-indented nil))) |
| 109 | )))) |
| 110 | (if cur-indent |
| 111 | (indent-line-to cur-indent) |
| 112 | (indent-line-to 0))))) |
| 113 | |
Carl Yeksigian | 98bebac | 2014-03-12 16:48:01 -0400 | [diff] [blame] | 114 | ;; C/C++- and sh-style comments; also allowing underscore in words |
Mark Slee | b5f93b0 | 2007-05-11 23:46:42 +0000 | [diff] [blame] | 115 | (defvar thrift-mode-syntax-table |
| 116 | (let ((thrift-mode-syntax-table (make-syntax-table))) |
| 117 | (modify-syntax-entry ?_ "w" thrift-mode-syntax-table) |
Carl Yeksigian | 98bebac | 2014-03-12 16:48:01 -0400 | [diff] [blame] | 118 | (modify-syntax-entry ?# "<" thrift-mode-syntax-table) ; sh-style comments |
| 119 | (modify-syntax-entry ?/ ". 124" thrift-mode-syntax-table) ; c/c++-style comments |
| 120 | (modify-syntax-entry ?* ". 23b" thrift-mode-syntax-table) |
| 121 | (modify-syntax-entry ?\n ">" thrift-mode-syntax-table) |
Mark Slee | b5f93b0 | 2007-05-11 23:46:42 +0000 | [diff] [blame] | 122 | thrift-mode-syntax-table) |
| 123 | "Syntax table for thrift-mode") |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 124 | |
jfarrell | 38ddc8f | 2013-09-10 12:12:41 -0400 | [diff] [blame] | 125 | ;;;###autoload |
Mark Slee | b5f93b0 | 2007-05-11 23:46:42 +0000 | [diff] [blame] | 126 | (defun thrift-mode () |
jfarrell | 38ddc8f | 2013-09-10 12:12:41 -0400 | [diff] [blame] | 127 | "Mode for editing Thrift files." |
Mark Slee | b5f93b0 | 2007-05-11 23:46:42 +0000 | [diff] [blame] | 128 | (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 | ) |
jfarrell | 38ddc8f | 2013-09-10 12:12:41 -0400 | [diff] [blame] | 137 | |
| 138 | (provide 'thrift) |
| 139 | ;;; thrift.el ends here |
| 140 | |