]> git.donarmstrong.com Git - lib.git/blob - emacs_el/cmake-mode.el
ess configuration is now in don-configuration.org
[lib.git] / emacs_el / 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   "^\\(IF\\|MACRO\\|FOREACH\\|ELSE\\|ELSEIF\\|WHILE\\|FUNCTION\\)$")
72 (defconst cmake-regex-block-close
73   "^[ \t]*\\(ENDIF\\|ENDFOREACH\\|ENDMACRO\\|ELSE\\|ELSEIF\\|ENDWHILE\\|ENDFUNCTION\\)[ \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                     (not (and (string-match cmake-regex-indented region)
103                               (= (length region) (match-end 0))))))
104       (forward-line -1)
105       (setq region (buffer-substring-no-properties (point) point-start))
106       )
107     )
108   )
109
110 ;------------------------------------------------------------------------------
111
112 ;;
113 ;; Line indentation function.
114 ;;
115 (defun cmake-indent ()
116   "Indent current line as CMAKE code."
117   (interactive)
118   (if (cmake-line-starts-inside-string)
119       ()
120     (if (bobp)
121         (cmake-indent-line-to 0)
122       (let (cur-indent)
123
124         (save-excursion
125           (beginning-of-line)
126
127           (let ((point-start (point))
128                 token)
129
130             ; Search back for the last indented line.
131             (cmake-find-last-indented-line)
132
133             ; Start with the indentation on this line.
134             (setq cur-indent (current-indentation))
135
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))
141                 )
142               (if (string-match (concat "^" cmake-regex-paren-right "$") token)
143                   (setq cur-indent (- cur-indent cmake-tab-width))
144                 )
145               (if (and
146                    (string-match cmake-regex-block-open token)
147                    (looking-at (concat "[ \t]*" cmake-regex-paren-left))
148                    )
149                   (setq cur-indent (+ cur-indent cmake-tab-width))
150                 )
151               )
152             (goto-char point-start)
153
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))
157               )
158             )
159           )
160
161         ; Indent this line by the amount selected.
162         (if (< cur-indent 0)
163             (cmake-indent-line-to 0)
164           (cmake-indent-line-to cur-indent)
165           )
166         )
167       )
168     )
169   )
170
171 (defun cmake-point-in-indendation ()
172   (string-match "^[ \\t]*$" (buffer-substring (point-at-bol) (point))))
173
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))))
181
182 ;------------------------------------------------------------------------------
183
184 ;;
185 ;; Helper functions for buffer
186 ;;
187 (defun unscreamify-cmake-buffer ()
188   "Convert all CMake commands to lowercase in buffer."
189   (interactive)
190   (setq save-point (point))
191   (goto-char (point-min))
192   (while (re-search-forward "^\\([ \t]*\\)\\(\\w+\\)\\([ \t]*(\\)" nil t)
193     (replace-match 
194      (concat 
195       (match-string 1) 
196       (downcase (match-string 2)) 
197       (match-string 3)) 
198      t))
199   (goto-char save-point)
200   )
201
202 ;------------------------------------------------------------------------------
203
204 ;;
205 ;; Keyword highlighting regex-to-face map.
206 ;;
207 (defconst cmake-font-lock-keywords
208   (list '("^[ \t]*\\(\\w+\\)[ \t]*(" 1 font-lock-function-name-face))
209   "Highlighting expressions for CMAKE mode."
210   )
211
212 ;------------------------------------------------------------------------------
213
214 ;;
215 ;; Syntax table for this mode.  Initialize to nil so that it is
216 ;; regenerated when the cmake-mode function is called.
217 ;;
218 (defvar cmake-mode-syntax-table nil "Syntax table for cmake-mode.")
219 (setq cmake-mode-syntax-table nil)
220
221 ;;
222 ;; User hook entry point.
223 ;;
224 (defvar cmake-mode-hook nil)
225
226 ;;
227 ;; Indentation increment.
228 ;;
229 (defvar cmake-tab-width 2)
230
231 ;------------------------------------------------------------------------------
232
233 ;;
234 ;; CMake mode startup function.
235 ;;
236 (defun cmake-mode ()
237   "Major mode for editing CMake listfiles."
238   (interactive)
239   (kill-all-local-variables)
240   (setq major-mode 'cmake-mode)
241   (setq mode-name "CMAKE")
242
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)
251
252   ; Setup font-lock mode.
253   (make-local-variable 'font-lock-defaults)
254   (setq font-lock-defaults '(cmake-font-lock-keywords))
255
256   ; Setup indentation function.
257   (make-local-variable 'indent-line-function)
258   (setq indent-line-function 'cmake-indent)
259
260   ; Setup comment syntax.
261   (make-local-variable 'comment-start)
262   (setq comment-start "#")
263
264   ; Run user hooks.
265   (run-hooks 'cmake-mode-hook))
266
267 ; Help mode starts here
268
269
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."
273   (interactive "s")
274   (let* ((bufname (concat "*CMake" type (if topic "-") topic "*"))
275          (buffer  (get-buffer bufname))
276          )
277     (if buffer
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))
294       (cmake-mode)
295       (toggle-read-only t)
296       ;; Restore the original window
297       (select-window window)
298       (setq resize-mini-windows resize-mini-windows-save)
299       )
300     )
301   )
302
303 (defun cmake-help-list-commands ()
304   "Prints out a list of the cmake commands."
305   (interactive)
306   (cmake-command-run "--help-command-list")
307   )
308
309 (defvar cmake-help-command-history nil "Topic read history.")
310
311 (require 'thingatpt)
312 (defun cmake-get-topic (type)
313   "Gets the topic from the minibuffer input.  The default is the word the cursor is on."
314   (interactive)
315   (let* ((default-entry (word-at-point))
316          (input (read-string
317                  (format "CMake %s (default %s): " type default-entry) ; prompt
318                  nil ; initial input
319                  'cmake-help-command-history ; command history
320                  default-entry ; default-value
321                  )))
322     (if (string= input "")
323         (error "No argument given")
324       input))
325   )
326
327
328 (defun cmake-help-command ()
329   "Prints out the help message corresponding to the command the cursor is on."
330   (interactive)
331   (setq command (cmake-get-topic "command"))
332   (cmake-command-run "--help-command" (downcase command))
333   )
334
335
336 ; This file provides cmake-mode.
337 (provide 'cmake-mode)
338
339 ;;; cmake-mode.el ends here