Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

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