1 ;=============================================================================
2 ; CMake - Cross Platform Makefile Generator
3 ; Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 ; Distributed under the OSI-approved BSD License (the "License");
6 ; see accompanying file Copyright.txt for details.
8 ; This software is distributed WITHOUT ANY WARRANTY; without even the
9 ; implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 ; See the License for more information.
11 ;=============================================================================
12 ;;; cmake-mode.el --- major-mode for editing CMake sources
14 ;------------------------------------------------------------------------------
18 ;; Provides syntax highlighting and indentation for CMakeLists.txt and
19 ;; *.cmake source files.
21 ;; Add this code to your .emacs file to use the mode:
23 ;; (setq load-path (cons (expand-file-name "/dir/with/cmake-mode") load-path))
24 ;; (require 'cmake-mode)
25 ;; (setq auto-mode-alist
26 ;; (append '(("CMakeLists\\.txt\\'" . cmake-mode)
27 ;; ("\\.cmake\\'" . cmake-mode))
30 ;------------------------------------------------------------------------------
34 ;; cmake executable variable used to run cmake --help-command
35 ;; on commands in cmake-mode
37 ;; cmake-command-help Written by James Bigler
40 (defcustom cmake-mode-cmake-executable "cmake"
41 "*The name of the cmake executable.
43 This can be either absolute or looked up in $PATH. You can also
44 set the path with these commands:
45 (setenv \"PATH\" (concat (getenv \"PATH\") \";C:\\\\Program Files\\\\CMake 2.8\\\\bin\"))
46 (setenv \"PATH\" (concat (getenv \"PATH\") \":/usr/local/cmake/bin\"))"
50 ;; Regular expressions used by line indentation function.
52 (defconst cmake-regex-blank "^[ \t]*$")
53 (defconst cmake-regex-comment "#.*")
54 (defconst cmake-regex-paren-left "(")
55 (defconst cmake-regex-paren-right ")")
56 (defconst cmake-regex-argument-quoted
57 "\"\\([^\"\\\\]\\|\\\\\\(.\\|\n\\)\\)*\"")
58 (defconst cmake-regex-argument-unquoted
59 "\\([^ \t\r\n()#\"\\\\]\\|\\\\.\\)\\([^ \t\r\n()#\\\\]\\|\\\\.\\)*")
60 (defconst cmake-regex-token (concat "\\(" cmake-regex-comment
61 "\\|" cmake-regex-paren-left
62 "\\|" cmake-regex-paren-right
63 "\\|" cmake-regex-argument-unquoted
64 "\\|" cmake-regex-argument-quoted
66 (defconst cmake-regex-indented (concat "^\\("
70 (defconst cmake-regex-block-open
71 "^\\(IF\\|MACRO\\|FOREACH\\|ELSE\\|ELSEIF\\|WHILE\\|FUNCTION\\)$")
72 (defconst cmake-regex-block-close
73 "^[ \t]*\\(ENDIF\\|ENDFOREACH\\|ENDMACRO\\|ELSE\\|ELSEIF\\|ENDWHILE\\|ENDFUNCTION\\)[ \t]*(")
75 ;------------------------------------------------------------------------------
78 ;; Helper functions for line indentation function.
80 (defun cmake-line-starts-inside-string ()
81 "Determine whether the beginning of the current line is in a string."
84 (let ((parse-end (point)))
86 (nth 3 (parse-partial-sexp (point) parse-end))
94 (defun cmake-find-last-indented-line ()
95 "Move to the beginning of the last line that has meaningful indentation."
96 (let ((point-start (point))
99 (setq region (buffer-substring-no-properties (point) point-start))
100 (while (and (not (bobp))
101 (or (looking-at cmake-regex-blank)
102 (not (and (string-match cmake-regex-indented region)
103 (= (length region) (match-end 0))))))
105 (setq region (buffer-substring-no-properties (point) point-start))
110 ;------------------------------------------------------------------------------
113 ;; Line indentation function.
115 (defun cmake-indent ()
116 "Indent current line as CMAKE code."
118 (if (cmake-line-starts-inside-string)
121 (cmake-indent-line-to 0)
127 (let ((point-start (point))
130 ; Search back for the last indented line.
131 (cmake-find-last-indented-line)
133 ; Start with the indentation on this line.
134 (setq cur-indent (current-indentation))
136 ; Search forward counting tokens that adjust indentation.
137 (while (re-search-forward cmake-regex-token point-start t)
138 (setq token (match-string 0))
139 (if (string-match (concat "^" cmake-regex-paren-left "$") token)
140 (setq cur-indent (+ cur-indent cmake-tab-width))
142 (if (string-match (concat "^" cmake-regex-paren-right "$") token)
143 (setq cur-indent (- cur-indent cmake-tab-width))
146 (string-match cmake-regex-block-open token)
147 (looking-at (concat "[ \t]*" cmake-regex-paren-left))
149 (setq cur-indent (+ cur-indent cmake-tab-width))
152 (goto-char point-start)
154 ; If this is the end of a block, decrease indentation.
155 (if (looking-at cmake-regex-block-close)
156 (setq cur-indent (- cur-indent cmake-tab-width))
161 ; Indent this line by the amount selected.
163 (cmake-indent-line-to 0)
164 (cmake-indent-line-to cur-indent)
171 (defun cmake-point-in-indendation ()
172 (string-match "^[ \\t]*$" (buffer-substring (point-at-bol) (point))))
174 (defun cmake-indent-line-to (column)
175 "Indent the current line to COLUMN.
176 If point is within the existing indentation it is moved to the end of
177 the indentation. Otherwise it retains the same position on the line"
178 (if (cmake-point-in-indendation)
179 (indent-line-to column)
180 (save-excursion (indent-line-to column))))
182 ;------------------------------------------------------------------------------
185 ;; Helper functions for buffer
187 (defun unscreamify-cmake-buffer ()
188 "Convert all CMake commands to lowercase in buffer."
190 (setq save-point (point))
191 (goto-char (point-min))
192 (while (re-search-forward "^\\([ \t]*\\)\\(\\w+\\)\\([ \t]*(\\)" nil t)
196 (downcase (match-string 2))
199 (goto-char save-point)
202 ;------------------------------------------------------------------------------
205 ;; Keyword highlighting regex-to-face map.
207 (defconst cmake-font-lock-keywords
208 (list '("^[ \t]*\\(\\w+\\)[ \t]*(" 1 font-lock-function-name-face))
209 "Highlighting expressions for CMAKE mode."
212 ;------------------------------------------------------------------------------
215 ;; Syntax table for this mode. Initialize to nil so that it is
216 ;; regenerated when the cmake-mode function is called.
218 (defvar cmake-mode-syntax-table nil "Syntax table for cmake-mode.")
219 (setq cmake-mode-syntax-table nil)
222 ;; User hook entry point.
224 (defvar cmake-mode-hook nil)
227 ;; Indentation increment.
229 (defvar cmake-tab-width 2)
231 ;------------------------------------------------------------------------------
234 ;; CMake mode startup function.
237 "Major mode for editing CMake listfiles."
239 (kill-all-local-variables)
240 (setq major-mode 'cmake-mode)
241 (setq mode-name "CMAKE")
243 ; Create the syntax table
244 (setq cmake-mode-syntax-table (make-syntax-table))
245 (set-syntax-table cmake-mode-syntax-table)
246 (modify-syntax-entry ?_ "w" cmake-mode-syntax-table)
247 (modify-syntax-entry ?\( "()" cmake-mode-syntax-table)
248 (modify-syntax-entry ?\) ")(" cmake-mode-syntax-table)
249 (modify-syntax-entry ?# "<" cmake-mode-syntax-table)
250 (modify-syntax-entry ?\n ">" cmake-mode-syntax-table)
252 ; Setup font-lock mode.
253 (make-local-variable 'font-lock-defaults)
254 (setq font-lock-defaults '(cmake-font-lock-keywords))
256 ; Setup indentation function.
257 (make-local-variable 'indent-line-function)
258 (setq indent-line-function 'cmake-indent)
260 ; Setup comment syntax.
261 (make-local-variable 'comment-start)
262 (setq comment-start "#")
265 (run-hooks 'cmake-mode-hook))
267 ; Help mode starts here
270 (defun cmake-command-run (type &optional topic)
271 "Runs the command cmake with the arguments specified. The
272 optional argument topic will be appended to the argument list."
274 (let* ((bufname (concat "*CMake" type (if topic "-") topic "*"))
275 (buffer (get-buffer bufname))
278 (display-buffer buffer 'not-this-window)
279 ;; Buffer doesn't exist. Create it and fill it
280 (setq buffer (generate-new-buffer bufname))
281 (setq command (concat cmake-mode-cmake-executable " " type " " topic))
282 (message "Running %s" command)
283 ;; We don't want the contents of the shell-command running to the
284 ;; minibuffer, so turn it off. A value of nil means don't automatically
285 ;; resize mini-windows.
286 (setq resize-mini-windows-save resize-mini-windows)
287 (setq resize-mini-windows nil)
288 (shell-command command buffer)
289 ;; Save the original window, so that we can come back to it later.
290 ;; save-excursion doesn't seem to work for this.
291 (setq window (selected-window))
292 ;; We need to select it so that we can apply special modes to it
293 (select-window (display-buffer buffer 'not-this-window))
296 ;; Restore the original window
297 (select-window window)
298 (setq resize-mini-windows resize-mini-windows-save)
303 (defun cmake-help-list-commands ()
304 "Prints out a list of the cmake commands."
306 (cmake-command-run "--help-command-list")
309 (defvar cmake-help-command-history nil "Topic read history.")
312 (defun cmake-get-topic (type)
313 "Gets the topic from the minibuffer input. The default is the word the cursor is on."
315 (let* ((default-entry (word-at-point))
317 (format "CMake %s (default %s): " type default-entry) ; prompt
319 'cmake-help-command-history ; command history
320 default-entry ; default-value
322 (if (string= input "")
323 (error "No argument given")
328 (defun cmake-help-command ()
329 "Prints out the help message corresponding to the command the cursor is on."
331 (setq command (cmake-get-topic "command"))
332 (cmake-command-run "--help-command" (downcase command))
336 ; This file provides cmake-mode.
337 (provide 'cmake-mode)
339 ;;; cmake-mode.el ends here