TZIVI-254: IVI needs a newer version of cmake
[profile/ivi/cmake.git] / Docs / cmake-mode.el
1 ;=============================================================================
2 ; CMake - Cross Platform Makefile Generator
3 ; Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4 ;
5 ; Distributed under the OSI-approved BSD License (the "License");
6 ; see accompanying file Copyright.txt for details.
7 ;
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
13
14 ;------------------------------------------------------------------------------
15
16 ;;; Commentary:
17
18 ;; Provides syntax highlighting and indentation for CMakeLists.txt and
19 ;; *.cmake source files.
20 ;;
21 ;; Add this code to your .emacs file to use the mode:
22 ;;
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))
28 ;;                auto-mode-alist))
29
30 ;------------------------------------------------------------------------------
31
32 ;;; Code:
33 ;;
34 ;; cmake executable variable used to run cmake --help-command
35 ;; on commands in cmake-mode
36 ;;
37 ;; cmake-command-help Written by James Bigler 
38 ;;
39
40 (defcustom cmake-mode-cmake-executable "cmake"
41   "*The name of the cmake executable.
42
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\"))"
47   :type 'file
48   :group 'cmake)
49 ;;
50 ;; Regular expressions used by line indentation function.
51 ;;
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
65                                     "\\)"))
66 (defconst cmake-regex-indented (concat "^\\("
67                                        cmake-regex-token
68                                        "\\|" "[ \t\r\n]"
69                                        "\\)*"))
70 (defconst cmake-regex-block-open
71   "^\\([iI][fF]\\|[mM][aA][cC][rR][oO]\\|[fF][oO][rR][eE][aA][cC][hH]\\|[eE][lL][sS][eE]\\|[eE][lL][sS][eE][iI][fF]\\|[wW][hH][iI][lL][eE]\\|[fF][uU][nN][cC][tT][iI][oO][nN]\\)$")
72 (defconst cmake-regex-block-close
73   "^[ \t]*\\([eE][nN][dD][iI][fF]\\|[eE][nN][dD][fF][oO][rR][eE][aA][cC][hH]\\|[eE][nN][dD][mM][aA][cC][rR][oO]\\|[eE][lL][sS][eE]\\|[eE][lL][sS][eE][iI][fF]\\|[eE][nN][dD][wW][hH][iI][lL][eE]\\|[eE][nN][dD][fF][uU][nN][cC][tT][iI][oO][nN]\\)[ \t]*(")
74
75 ;------------------------------------------------------------------------------
76
77 ;;
78 ;; Helper functions for line indentation function.
79 ;;
80 (defun cmake-line-starts-inside-string ()
81   "Determine whether the beginning of the current line is in a string."
82   (if (save-excursion
83         (beginning-of-line)
84         (let ((parse-end (point)))
85           (beginning-of-buffer)
86           (nth 3 (parse-partial-sexp (point) parse-end))
87           )
88         )
89       t
90     nil
91     )
92   )
93
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))
97         region)
98     (forward-line -1)
99     (setq region (buffer-substring-no-properties (point) point-start))
100     (while (and (not (bobp))
101                 (or (looking-at cmake-regex-blank)
102                     (cmake-line-starts-inside-string)
103                     (not (and (string-match cmake-regex-indented region)
104                               (= (length region) (match-end 0))))))
105       (forward-line -1)
106       (setq region (buffer-substring-no-properties (point) point-start))
107       )
108     )
109   )
110
111 ;------------------------------------------------------------------------------
112
113 ;;
114 ;; Line indentation function.
115 ;;
116 (defun cmake-indent ()
117   "Indent current line as CMAKE code."
118   (interactive)
119   (if (cmake-line-starts-inside-string)
120       ()
121     (if (bobp)
122         (cmake-indent-line-to 0)
123       (let (cur-indent)
124
125         (save-excursion
126           (beginning-of-line)
127
128           (let ((point-start (point))
129                 token)
130
131             ; Search back for the last indented line.
132             (cmake-find-last-indented-line)
133
134             ; Start with the indentation on this line.
135             (setq cur-indent (current-indentation))
136
137             ; Search forward counting tokens that adjust indentation.
138             (while (re-search-forward cmake-regex-token point-start t)
139               (setq token (match-string 0))
140               (if (string-match (concat "^" cmake-regex-paren-left "$") token)
141                   (setq cur-indent (+ cur-indent cmake-tab-width))
142                 )
143               (if (string-match (concat "^" cmake-regex-paren-right "$") token)
144                   (setq cur-indent (- cur-indent cmake-tab-width))
145                 )
146               (if (and
147                    (string-match cmake-regex-block-open token)
148                    (looking-at (concat "[ \t]*" cmake-regex-paren-left))
149                    )
150                   (setq cur-indent (+ cur-indent cmake-tab-width))
151                 )
152               )
153             (goto-char point-start)
154
155             ; If this is the end of a block, decrease indentation.
156             (if (looking-at cmake-regex-block-close)
157                 (setq cur-indent (- cur-indent cmake-tab-width))
158               )
159             )
160           )
161
162         ; Indent this line by the amount selected.
163         (if (< cur-indent 0)
164             (cmake-indent-line-to 0)
165           (cmake-indent-line-to cur-indent)
166           )
167         )
168       )
169     )
170   )
171
172 (defun cmake-point-in-indendation ()
173   (string-match "^[ \\t]*$" (buffer-substring (point-at-bol) (point))))
174
175 (defun cmake-indent-line-to (column)
176   "Indent the current line to COLUMN.
177 If point is within the existing indentation it is moved to the end of
178 the indentation.  Otherwise it retains the same position on the line"
179   (if (cmake-point-in-indendation)
180       (indent-line-to column)
181     (save-excursion (indent-line-to column))))
182
183 ;------------------------------------------------------------------------------
184
185 ;;
186 ;; Helper functions for buffer
187 ;;
188 (defun unscreamify-cmake-buffer ()
189   "Convert all CMake commands to lowercase in buffer."
190   (interactive)
191   (setq save-point (point))
192   (goto-char (point-min))
193   (while (re-search-forward "^\\([ \t]*\\)\\(\\w+\\)\\([ \t]*(\\)" nil t)
194     (replace-match 
195      (concat 
196       (match-string 1) 
197       (downcase (match-string 2)) 
198       (match-string 3)) 
199      t))
200   (goto-char save-point)
201   )
202
203 ;------------------------------------------------------------------------------
204
205 ;;
206 ;; Keyword highlighting regex-to-face map.
207 ;;
208 (defconst cmake-font-lock-keywords
209   (list '("^[ \t]*\\(\\w+\\)[ \t]*(" 1 font-lock-function-name-face))
210   "Highlighting expressions for CMAKE mode."
211   )
212
213 ;------------------------------------------------------------------------------
214
215 ;;
216 ;; Syntax table for this mode.  Initialize to nil so that it is
217 ;; regenerated when the cmake-mode function is called.
218 ;;
219 (defvar cmake-mode-syntax-table nil "Syntax table for cmake-mode.")
220 (setq cmake-mode-syntax-table nil)
221
222 ;;
223 ;; User hook entry point.
224 ;;
225 (defvar cmake-mode-hook nil)
226
227 ;;
228 ;; Indentation increment.
229 ;;
230 (defvar cmake-tab-width 2)
231
232 ;------------------------------------------------------------------------------
233
234 ;;
235 ;; CMake mode startup function.
236 ;;
237 (defun cmake-mode ()
238   "Major mode for editing CMake listfiles."
239   (interactive)
240   (kill-all-local-variables)
241   (setq major-mode 'cmake-mode)
242   (setq mode-name "CMAKE")
243
244   ; Create the syntax table
245   (setq cmake-mode-syntax-table (make-syntax-table))
246   (set-syntax-table cmake-mode-syntax-table)
247   (modify-syntax-entry ?_  "w" cmake-mode-syntax-table)
248   (modify-syntax-entry ?\(  "()" cmake-mode-syntax-table)
249   (modify-syntax-entry ?\)  ")(" cmake-mode-syntax-table)
250   (modify-syntax-entry ?# "<" cmake-mode-syntax-table)
251   (modify-syntax-entry ?\n ">" cmake-mode-syntax-table)
252
253   ; Setup font-lock mode.
254   (make-local-variable 'font-lock-defaults)
255   (setq font-lock-defaults '(cmake-font-lock-keywords))
256
257   ; Setup indentation function.
258   (make-local-variable 'indent-line-function)
259   (setq indent-line-function 'cmake-indent)
260
261   ; Setup comment syntax.
262   (make-local-variable 'comment-start)
263   (setq comment-start "#")
264
265   ; Run user hooks.
266   (run-hooks 'cmake-mode-hook))
267
268 ; Help mode starts here
269
270
271 (defun cmake-command-run (type &optional topic)
272   "Runs the command cmake with the arguments specified.  The
273 optional argument topic will be appended to the argument list."
274   (interactive "s")
275   (let* ((bufname (concat "*CMake" type (if topic "-") topic "*"))
276          (buffer  (get-buffer bufname))
277          )
278     (if buffer
279         (display-buffer buffer 'not-this-window)
280       ;; Buffer doesn't exist.  Create it and fill it
281       (setq buffer (generate-new-buffer bufname))
282       (setq command (concat cmake-mode-cmake-executable " " type " " topic))
283       (message "Running %s" command)
284       ;; We don't want the contents of the shell-command running to the
285       ;; minibuffer, so turn it off.  A value of nil means don't automatically
286       ;; resize mini-windows.
287       (setq resize-mini-windows-save resize-mini-windows)
288       (setq resize-mini-windows nil)
289       (shell-command command buffer)
290       ;; Save the original window, so that we can come back to it later.
291       ;; save-excursion doesn't seem to work for this.
292       (setq window (selected-window))
293       ;; We need to select it so that we can apply special modes to it
294       (select-window (display-buffer buffer 'not-this-window))
295       (cmake-mode)
296       (toggle-read-only t)
297       ;; Restore the original window
298       (select-window window)
299       (setq resize-mini-windows resize-mini-windows-save)
300       )
301     )
302   )
303
304 (defun cmake-help-list-commands ()
305   "Prints out a list of the cmake commands."
306   (interactive)
307   (cmake-command-run "--help-command-list")
308   )
309
310 (defvar cmake-help-command-history nil "Topic read history.")
311
312 (require 'thingatpt)
313 (defun cmake-get-topic (type)
314   "Gets the topic from the minibuffer input.  The default is the word the cursor is on."
315   (interactive)
316   (let* ((default-entry (word-at-point))
317          (input (read-string
318                  (format "CMake %s (default %s): " type default-entry) ; prompt
319                  nil ; initial input
320                  'cmake-help-command-history ; command history
321                  default-entry ; default-value
322                  )))
323     (if (string= input "")
324         (error "No argument given")
325       input))
326   )
327
328
329 (defun cmake-help-command ()
330   "Prints out the help message corresponding to the command the cursor is on."
331   (interactive)
332   (setq command (cmake-get-topic "command"))
333   (cmake-command-run "--help-command" (downcase command))
334   )
335
336
337 ; This file provides cmake-mode.
338 (provide 'cmake-mode)
339
340 ;;; cmake-mode.el ends here