ENH: use .exe on vms
[cmake.git] / Docs / cmake-mode.el
blob9bac1cdf24fc03597f6e029dd23d0e87f07d9f59
1 ;=============================================================================
3 ; Program: CMake - Cross-Platform Makefile Generator
4 ; Module: $RCSfile: cmake-mode.el,v $
6 ; Copyright (c) 2000-$Date: 2009-02-26 18:28:01 $ Kitware, Inc., Insight Consortium. All rights reserved.
7 ; See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
9 ; This software is distributed WITHOUT ANY WARRANTY; without even
10 ; the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 ; PURPOSE. See the above copyright notices for more information.
13 ;=============================================================================
14 ;;; cmake-mode.el --- major-mode for editing CMake sources
16 ;------------------------------------------------------------------------------
18 ;;; Commentary:
20 ;; Provides syntax highlighting and indentation for CMakeLists.txt and
21 ;; *.cmake source files.
23 ;; Add this code to your .emacs file to use the mode:
25 ;; (setq load-path (cons (expand-file-name "/dir/with/cmake-mode") load-path))
26 ;; (require 'cmake-mode)
27 ;; (setq auto-mode-alist
28 ;; (append '(("CMakeLists\\.txt\\'" . cmake-mode)
29 ;; ("\\.cmake\\'" . cmake-mode))
30 ;; auto-mode-alist))
32 ;------------------------------------------------------------------------------
34 ;;; Code:
37 ;; Regular expressions used by line indentation function.
39 (defconst cmake-regex-blank "^[ \t]*$")
40 (defconst cmake-regex-comment "#.*")
41 (defconst cmake-regex-paren-left "(")
42 (defconst cmake-regex-paren-right ")")
43 (defconst cmake-regex-argument-quoted
44 "\"\\([^\"\\\\]\\|\\\\\\(.\\|\n\\)\\)*\"")
45 (defconst cmake-regex-argument-unquoted
46 "\\([^ \t\r\n()#\"\\\\]\\|\\\\.\\)\\([^ \t\r\n()#\\\\]\\|\\\\.\\)*")
47 (defconst cmake-regex-token (concat "\\(" cmake-regex-comment
48 "\\|" cmake-regex-paren-left
49 "\\|" cmake-regex-paren-right
50 "\\|" cmake-regex-argument-unquoted
51 "\\|" cmake-regex-argument-quoted
52 "\\)"))
53 (defconst cmake-regex-indented (concat "^\\("
54 cmake-regex-token
55 "\\|" "[ \t\r\n]"
56 "\\)*"))
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]*(")
62 ;------------------------------------------------------------------------------
65 ;; Helper functions for line indentation function.
67 (defun cmake-line-starts-inside-string ()
68 "Determine whether the beginning of the current line is in a string."
69 (if (save-excursion
70 (beginning-of-line)
71 (let ((parse-end (point)))
72 (beginning-of-buffer)
73 (nth 3 (parse-partial-sexp (point) parse-end))
77 nil
81 (defun cmake-find-last-indented-line ()
82 "Move to the beginning of the last line that has meaningful indentation."
83 (let ((point-start (point))
84 region)
85 (forward-line -1)
86 (setq region (buffer-substring-no-properties (point) point-start))
87 (while (and (not (bobp))
88 (or (looking-at cmake-regex-blank)
89 (not (and (string-match cmake-regex-indented region)
90 (= (length region) (match-end 0))))))
91 (forward-line -1)
92 (setq region (buffer-substring-no-properties (point) point-start))
97 ;------------------------------------------------------------------------------
100 ;; Line indentation function.
102 (defun cmake-indent ()
103 "Indent current line as CMAKE code."
104 (interactive)
105 (if (cmake-line-starts-inside-string)
107 (if (bobp)
108 (cmake-indent-line-to 0)
109 (let (cur-indent)
111 (save-excursion
112 (beginning-of-line)
114 (let ((point-start (point))
115 token)
117 ; Search back for the last indented line.
118 (cmake-find-last-indented-line)
120 ; Start with the indentation on this line.
121 (setq cur-indent (current-indentation))
123 ; Search forward counting tokens that adjust indentation.
124 (while (re-search-forward cmake-regex-token point-start t)
125 (setq token (match-string 0))
126 (if (string-match (concat "^" cmake-regex-paren-left "$") token)
127 (setq cur-indent (+ cur-indent cmake-tab-width))
129 (if (string-match (concat "^" cmake-regex-paren-right "$") token)
130 (setq cur-indent (- cur-indent cmake-tab-width))
132 (if (and
133 (string-match cmake-regex-block-open token)
134 (looking-at (concat "[ \t]*" cmake-regex-paren-left))
136 (setq cur-indent (+ cur-indent cmake-tab-width))
139 (goto-char point-start)
141 ; If this is the end of a block, decrease indentation.
142 (if (looking-at cmake-regex-block-close)
143 (setq cur-indent (- cur-indent cmake-tab-width))
148 ; Indent this line by the amount selected.
149 (if (< cur-indent 0)
150 (cmake-indent-line-to 0)
151 (cmake-indent-line-to cur-indent)
158 (defun cmake-point-in-indendation ()
159 (string-match "^[ \\t]*$" (buffer-substring (point-at-bol) (point))))
161 (defun cmake-indent-line-to (column)
162 "Indent the current line to COLUMN.
163 If point is within the existing indentation it is moved to the end of
164 the indentation. Otherwise it retains the same position on the line"
165 (if (cmake-point-in-indendation)
166 (indent-line-to column)
167 (save-excursion (indent-line-to column))))
169 ;------------------------------------------------------------------------------
172 ;; Helper functions for buffer
174 (defun unscreamify-cmake-buffer ()
175 "Convert all CMake commands to lowercase in buffer."
176 (interactive)
177 (setq save-point (point))
178 (goto-char (point-min))
179 (while (re-search-forward "^\\([ \t]*\\)\\(\\w+\\)\\([ \t]*(\\)" nil t)
180 (replace-match
181 (concat
182 (match-string 1)
183 (downcase (match-string 2))
184 (match-string 3))
186 (goto-char save-point)
189 ;------------------------------------------------------------------------------
192 ;; Keyword highlighting regex-to-face map.
194 (defconst cmake-font-lock-keywords
195 (list '("^[ \t]*\\(\\w+\\)[ \t]*(" 1 font-lock-function-name-face))
196 "Highlighting expressions for CMAKE mode."
199 ;------------------------------------------------------------------------------
202 ;; Syntax table for this mode. Initialize to nil so that it is
203 ;; regenerated when the cmake-mode function is called.
205 (defvar cmake-mode-syntax-table nil "Syntax table for cmake-mode.")
206 (setq cmake-mode-syntax-table nil)
209 ;; User hook entry point.
211 (defvar cmake-mode-hook nil)
214 ;; Indentation increment.
216 (defvar cmake-tab-width 2)
218 ;------------------------------------------------------------------------------
221 ;; CMake mode startup function.
223 (defun cmake-mode ()
224 "Major mode for editing CMake listfiles."
225 (interactive)
226 (kill-all-local-variables)
227 (setq major-mode 'cmake-mode)
228 (setq mode-name "CMAKE")
230 ; Create the syntax table
231 (setq cmake-mode-syntax-table (make-syntax-table))
232 (set-syntax-table cmake-mode-syntax-table)
233 (modify-syntax-entry ?_ "w" cmake-mode-syntax-table)
234 (modify-syntax-entry ?\( "()" cmake-mode-syntax-table)
235 (modify-syntax-entry ?\) ")(" cmake-mode-syntax-table)
236 (modify-syntax-entry ?# "<" cmake-mode-syntax-table)
237 (modify-syntax-entry ?\n ">" cmake-mode-syntax-table)
239 ; Setup font-lock mode.
240 (make-local-variable 'font-lock-defaults)
241 (setq font-lock-defaults '(cmake-font-lock-keywords))
243 ; Setup indentation function.
244 (make-local-variable 'indent-line-function)
245 (setq indent-line-function 'cmake-indent)
247 ; Setup comment syntax.
248 (make-local-variable 'comment-start)
249 (setq comment-start "#")
251 ; Run user hooks.
252 (run-hooks 'cmake-mode-hook))
254 ; This file provides cmake-mode.
255 (provide 'cmake-mode)
257 ;;; cmake-mode.el ends here