cmake-mode.el 7.58 KB
Newer Older
Brad King's avatar
Brad King committed
1
2
3
4
5
;=============================================================================
;
;  Program:   CMake - Cross-Platform Makefile Generator
;  Module:    $RCSfile$
;
6
;  Copyright (c) 2000-$Date$ Kitware, Inc., Insight Consortium.  All rights reserved.
Brad King's avatar
Brad King committed
7
8
;  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
;
9
10
;     This software is distributed WITHOUT ANY WARRANTY; without even
;     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
Brad King's avatar
Brad King committed
11
12
13
;     PURPOSE.  See the above copyright notices for more information.
;
;=============================================================================
14
;;; cmake-mode.el --- major-mode for editing CMake sources
Ken Martin's avatar
Ken Martin committed
15

16
;------------------------------------------------------------------------------
17

18
19
20
21
22
23
24
25
26
27
;;; Commentary:

;; Provides syntax highlighting and indentation for CMakeLists.txt and
;; *.cmake source files.
;;
;; Add this code to your .emacs file to use the mode:
;;
;;  (setq load-path (cons (expand-file-name "/dir/with/cmake-mode") load-path))
;;  (require 'cmake-mode)
;;  (setq auto-mode-alist
28
29
;;        (append '(("CMakeLists\\.txt\\'" . cmake-mode)
;;                  ("\\.cmake\\'" . cmake-mode))
30
;;                auto-mode-alist))
31

32
33
34
35
36
37
38
39
;------------------------------------------------------------------------------

;;; Code:

;;
;; Regular expressions used by line indentation function.
;;
(defconst cmake-regex-blank "^[ \t]*$")
40
(defconst cmake-regex-comment "#.*")
41
42
(defconst cmake-regex-paren-left "(")
(defconst cmake-regex-paren-right ")")
43
44
45
46
(defconst cmake-regex-argument-quoted
  "\"\\([^\"\\\\]\\|\\\\\\(.\\|\n\\)\\)*\"")
(defconst cmake-regex-argument-unquoted
  "\\([^ \t\r\n()#\"\\\\]\\|\\\\.\\)\\([^ \t\r\n()#\\\\]\\|\\\\.\\)*")
47
48
49
50
51
52
53
54
55
56
(defconst cmake-regex-token (concat "\\(" cmake-regex-comment
                                    "\\|" cmake-regex-paren-left
                                    "\\|" cmake-regex-paren-right
                                    "\\|" cmake-regex-argument-unquoted
                                    "\\|" cmake-regex-argument-quoted
                                    "\\)"))
(defconst cmake-regex-indented (concat "^\\("
                                       cmake-regex-token
                                       "\\|" "[ \t\r\n]"
                                       "\\)*"))
57
(defconst cmake-regex-block-open
58
  "^\\(IF\\|MACRO\\|FOREACH\\|ELSE\\|ELSEIF\\|WHILE\\|FUNCTION\\)$")
59
(defconst cmake-regex-block-close
60
  "^[ \t]*\\(ENDIF\\|ENDFOREACH\\|ENDMACRO\\|ELSE\\|ELSEIF\\|ENDWHILE\\|ENDFUNCTION\\)[ \t]*(")
61

62
;------------------------------------------------------------------------------
63

64
65
66
;;
;; Helper functions for line indentation function.
;;
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(defun cmake-line-starts-inside-string ()
  "Determine whether the beginning of the current line is in a string."
  (if (save-excursion
        (beginning-of-line)
        (let ((parse-end (point)))
          (beginning-of-buffer)
          (nth 3 (parse-partial-sexp (point) parse-end))
          )
        )
      t
    nil
    )
  )

(defun cmake-find-last-indented-line ()
  "Move to the beginning of the last line that has meaningful indentation."
  (let ((point-start (point))
        region)
    (forward-line -1)
    (setq region (buffer-substring-no-properties (point) point-start))
    (while (and (not (bobp))
                (or (looking-at cmake-regex-blank)
                    (not (and (string-match cmake-regex-indented region)
                              (= (length region) (match-end 0))))))
      (forward-line -1)
      (setq region (buffer-substring-no-properties (point) point-start))
      )
    )
  )
Ken Martin's avatar
Ken Martin committed
96

97
98
99
100
101
;------------------------------------------------------------------------------

;;
;; Line indentation function.
;;
Ken Martin's avatar
Ken Martin committed
102
103
(defun cmake-indent ()
  "Indent current line as CMAKE code."
104
  (interactive)
Ken Martin's avatar
Ken Martin committed
105
  (beginning-of-line)
106
107
108
109
110
111
112
113
114
115
  (if (cmake-line-starts-inside-string)
      ()
    (if (bobp)
        (indent-line-to 0)
      (let ((point-start (point))
            token cur-indent)

        (save-excursion
          ; Search back for the last indented line.
          (cmake-find-last-indented-line)
Brad King's avatar
Brad King committed
116

117
118
119
120
121
122
          ; Start with the indentation on this line.
          (setq cur-indent (current-indentation))

          ; Search forward counting tokens that adjust indentation.
          (while (re-search-forward cmake-regex-token point-start t)
            (setq token (match-string 0))
123
            (if (string-match (concat "^" cmake-regex-paren-left "$") token)
124
125
                (setq cur-indent (+ cur-indent cmake-tab-width))
              )
126
            (if (string-match (concat "^" cmake-regex-paren-right "$") token)
127
128
                (setq cur-indent (- cur-indent cmake-tab-width))
              )
129
130
131
132
            (if (and
                 (string-match cmake-regex-block-open token)
                 (looking-at (concat "[ \t]*" cmake-regex-paren-left))
                 )
133
                (setq cur-indent (+ cur-indent cmake-tab-width))
Brad King's avatar
Brad King committed
134
              )
135
            )
Ken Martin's avatar
Ken Martin committed
136
          )
137

138
139
140
141
        ; If this is the end of a block, decrease indentation.
        (if (looking-at cmake-regex-block-close)
            (setq cur-indent (- cur-indent cmake-tab-width))
          )
142

143
144
145
146
147
        ; Indent this line by the amount selected.
        (if (< cur-indent 0)
            (indent-line-to 0)
          (indent-line-to cur-indent)
          )
Ken Martin's avatar
Ken Martin committed
148
149
150
151
        )
      )
    )
  )
152

153
;------------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
154

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
;;
;; Helper functions for buffer
;;
(defun unscreamify-cmake-buffer ()
  "Convert all CMake commands to lowercase in buffer."
  (interactive)
  (setq save-point (point))
  (goto-char (point-min))
  (while (re-search-forward "^\\([ \t]*\\)\\(\\w+\\)\\([ \t]*(\\)" nil t)
    (replace-match 
     (concat 
      (match-string 1) 
      (downcase (match-string 2)) 
      (match-string 3)) 
     t))
  (goto-char save-point)
  )

;------------------------------------------------------------------------------

175
176
177
;;
;; Keyword highlighting regex-to-face map.
;;
178
(defconst cmake-font-lock-keywords
179
  (list '("^[ \t]*\\(\\w+\\)[ \t]*(" 1 font-lock-function-name-face))
180
  "Highlighting expressions for CMAKE mode."
Ken Martin's avatar
Ken Martin committed
181
182
  )

183
;------------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
184

185
186
187
188
189
;;
;; Syntax table for this mode.  Initialize to nil so that it is
;; regenerated when the cmake-mode function is called.
;;
(defvar cmake-mode-syntax-table nil "Syntax table for cmake-mode.")
Ken Martin's avatar
Ken Martin committed
190
191
(setq cmake-mode-syntax-table nil)

192
193
194
;;
;; User hook entry point.
;;
Ken Martin's avatar
Ken Martin committed
195
196
(defvar cmake-mode-hook nil)

197
198
199
200
201
202
203
204
205
206
;;
;; Indentation increment.
;;
(defvar cmake-tab-width 2)

;------------------------------------------------------------------------------

;;
;; CMake mode startup function.
;;
Ken Martin's avatar
Ken Martin committed
207
208
209
210
211
212
(defun cmake-mode ()
  "Major mode for editing CMake listfiles."
  (interactive)
  (kill-all-local-variables)
  (setq major-mode 'cmake-mode)
  (setq mode-name "CMAKE")
213

214
  ; Create the syntax table
215
216
217
218
219
220
221
  (setq cmake-mode-syntax-table (make-syntax-table))
  (set-syntax-table cmake-mode-syntax-table)
  (modify-syntax-entry ?_  "w" cmake-mode-syntax-table)
  (modify-syntax-entry ?\(  "()" cmake-mode-syntax-table)
  (modify-syntax-entry ?\)  ")(" cmake-mode-syntax-table)
  (modify-syntax-entry ?# "<" cmake-mode-syntax-table)
  (modify-syntax-entry ?\n ">" cmake-mode-syntax-table)
222

Ken Martin's avatar
Ken Martin committed
223
224
  ; Setup font-lock mode.
  (make-local-variable 'font-lock-defaults)
225
  (setq font-lock-defaults '(cmake-font-lock-keywords))
226

Ken Martin's avatar
Ken Martin committed
227
228
229
  ; Setup indentation function.
  (make-local-variable 'indent-line-function)
  (setq indent-line-function 'cmake-indent)
230

Brad King's avatar
Brad King committed
231
232
233
  ; Setup comment syntax.
  (make-local-variable 'comment-start)
  (setq comment-start "#")
234

235
  ; Run user hooks.
Ken Martin's avatar
Ken Martin committed
236
237
238
239
  (run-hooks 'cmake-mode-hook))

; This file provides cmake-mode.
(provide 'cmake-mode)
240
241

;;; cmake-mode.el ends here