1;;; thrift.el --- Major mode for Apache Thrift files 2 3;; Keywords: files 4 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 23;;; Commentary: 24 25;; 26 27;;; Code: 28 29(require 'font-lock) 30 31(defvar thrift-mode-hook nil) 32;;;###autoload 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 41 '("\\<\\(include\\|struct\\|exception\\|typedef\\|const\\|enum\\|service\\|extends\\|void\\|oneway\\|throws\\|optional\\|required\\)\\>" . font-lock-keyword-face) ;; keywords 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 ) 46 "Thrift Keywords.") 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 114;; C/C++- and sh-style comments; also allowing underscore in words 115(defvar thrift-mode-syntax-table 116 (let ((thrift-mode-syntax-table (make-syntax-table))) 117 (modify-syntax-entry ?_ "w" thrift-mode-syntax-table) 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) 122 thrift-mode-syntax-table) 123 "Syntax table for thrift-mode") 124 125;;;###autoload 126(defun thrift-mode () 127 "Mode for editing Thrift files." 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 ) 137 138(provide 'thrift) 139;;; thrift.el ends here 140 141