1 ;;; cmake-mode.el --- major-mode for editing CMake sources
3 ;; Package-Requires: ((emacs "24.1"))
5 ; Distributed under the OSI-approved BSD 3-Clause License. See accompanying
6 ; file Copyright.txt or https://cmake.org/licensing for details.
8 ;------------------------------------------------------------------------------
12 ;; Provides syntax highlighting and indentation for CMakeLists.txt and
13 ;; *.cmake source files.
15 ;; Add this code to your .emacs file to use the mode:
17 ;; (setq load-path (cons (expand-file-name "/dir/with/cmake-mode") load-path))
18 ;; (require 'cmake-mode)
20 ;------------------------------------------------------------------------------
24 ;; cmake executable variable used to run cmake --help-command
25 ;; on commands in cmake-mode
27 ;; cmake-command-help Written by James Bigler
33 (defcustom cmake-mode-cmake-executable
"cmake"
34 "*The name of the cmake executable.
36 This can be either absolute or looked up in $PATH. You can also
37 set the path with these commands:
38 (setenv \"PATH\" (concat (getenv \"PATH\") \";C:\\\\Program Files\\\\CMake 2.8\\\\bin\"))
39 (setenv \"PATH\" (concat (getenv \"PATH\") \":/usr/local/cmake/bin\"))"
44 (defconst cmake-keywords-block-open
'("IF" "MACRO" "FOREACH" "ELSE" "ELSEIF" "WHILE" "FUNCTION"))
45 (defconst cmake-keywords-block-close
'("ENDIF" "ENDFOREACH" "ENDMACRO" "ELSE" "ELSEIF" "ENDWHILE" "ENDFUNCTION"))
46 (defconst cmake-keywords
47 (let ((kwds (append cmake-keywords-block-open cmake-keywords-block-close nil
)))
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-closing-parens-line
(concat "^[[:space:]]*\\("
57 cmake-regex-paren-right
59 (defconst cmake-regex-argument-quoted
60 (rx ?
\" (* (or (not (any ?
\" ?
\\)) (and ?
\\ anything
))) ?
\"))
61 (defconst cmake-regex-argument-unquoted
62 (rx (or (not (any space
"()#\"\\\n")) (and ?
\\ nonl
))
63 (* (or (not (any space
"()#\\\n")) (and ?
\\ nonl
)))))
64 (defconst cmake-regex-token
65 (rx-to-string `(group (or (regexp ,cmake-regex-comment
)
67 (regexp ,cmake-regex-argument-unquoted
)
68 (regexp ,cmake-regex-argument-quoted
)))))
69 (defconst cmake-regex-indented
70 (rx-to-string `(and bol
(* (group (or (regexp ,cmake-regex-token
) (any space ?
\n)))))))
71 (defconst cmake-regex-block-open
72 (rx-to-string `(and symbol-start
(or ,@(append cmake-keywords-block-open
73 (mapcar 'downcase cmake-keywords-block-open
))) symbol-end
)))
74 (defconst cmake-regex-block-close
75 (rx-to-string `(and symbol-start
(or ,@(append cmake-keywords-block-close
76 (mapcar 'downcase cmake-keywords-block-close
))) symbol-end
)))
77 (defconst cmake-regex-close
78 (rx-to-string `(and bol
(* space
) (regexp ,cmake-regex-block-close
)
79 (* space
) (regexp ,cmake-regex-paren-left
))))
80 (defconst cmake-regex-token-paren-left
(concat "^" cmake-regex-paren-left
"$"))
81 (defconst cmake-regex-token-paren-right
(concat "^" cmake-regex-paren-right
"$"))
83 ;------------------------------------------------------------------------------
85 ;; Line indentation helper functions
87 (defun cmake-line-starts-inside-string ()
88 "Determine whether the beginning of the current line is in a string."
91 (let ((parse-end (point)))
92 (goto-char (point-min))
93 (nth 3 (parse-partial-sexp (point) parse-end
))
98 (defun cmake-find-last-indented-line ()
99 "Move to the beginning of the last line that has meaningful indentation."
100 (let ((point-start (point))
103 (setq region
(buffer-substring-no-properties (point) point-start
))
104 (while (and (not (bobp))
105 (or (looking-at cmake-regex-blank
)
106 (cmake-line-starts-inside-string)
107 (not (and (string-match cmake-regex-indented region
)
108 (= (length region
) (match-end 0))))))
110 (setq region
(buffer-substring-no-properties (point) point-start
))
115 ;------------------------------------------------------------------------------
118 ;; Indentation increment.
120 (defcustom cmake-tab-width
2
121 "Number of columns to indent cmake blocks"
126 ;; Line indentation function.
128 (defun cmake-indent ()
129 "Indent current line as CMake code."
131 (unless (cmake-line-starts-inside-string)
133 (cmake-indent-line-to 0)
137 (let ((point-start (point))
138 (closing-parens-only (looking-at cmake-regex-closing-parens-line
))
139 (case-fold-search t
) ;; case-insensitive
141 ;; Search back for the last indented line.
142 (cmake-find-last-indented-line)
143 ;; Start with the indentation on this line.
144 (setq cur-indent
(current-indentation))
145 (if closing-parens-only
146 (let ((open-parens 0))
147 (while (re-search-forward cmake-regex-token point-start t
)
148 (setq token
(match-string 0))
150 ((string-match cmake-regex-token-paren-left token
)
151 (setq open-parens
(+ open-parens
1)))
152 ((string-match cmake-regex-token-paren-right token
)
153 (setq open-parens
(- open-parens
1)))))
154 ;; Don't outdent if last indented line has open parens
155 (unless (> open-parens
0)
156 (setq cur-indent
(- cur-indent cmake-tab-width
))))
157 ;; Skip detailed analysis if last indented line is a 'closing
159 (unless (looking-at cmake-regex-closing-parens-line
)
160 ;; Search forward counting tokens that adjust indentation.
161 (while (re-search-forward cmake-regex-token point-start t
)
162 (setq token
(match-string 0))
163 (when (or (string-match cmake-regex-token-paren-left token
)
164 (and (string-match cmake-regex-block-open token
)
165 (looking-at (concat "[ \t]*" cmake-regex-paren-left
))))
166 (setq cur-indent
(+ cur-indent cmake-tab-width
)))
167 (when (string-match cmake-regex-token-paren-right token
)
168 (setq cur-indent
(- cur-indent cmake-tab-width
)))
170 (goto-char point-start
)
171 ;; If next token closes the block, decrease indentation
172 (when (looking-at cmake-regex-close
)
173 (setq cur-indent
(- cur-indent cmake-tab-width
))
178 ;; Indent this line by the amount selected.
179 (cmake-indent-line-to (max cur-indent
0))
185 (defun cmake-point-in-indendation ()
186 (string-match "^[ \\t]*$" (buffer-substring (point-at-bol) (point))))
188 (defun cmake-indent-line-to (column)
189 "Indent the current line to COLUMN.
190 If point is within the existing indentation it is moved to the end of
191 the indentation. Otherwise it retains the same position on the line"
192 (if (cmake-point-in-indendation)
193 (indent-line-to column
)
194 (save-excursion (indent-line-to column
))))
196 ;------------------------------------------------------------------------------
199 ;; Helper functions for buffer
201 (defun cmake-unscreamify-buffer ()
202 "Convert all CMake commands to lowercase in buffer."
205 (goto-char (point-min))
206 (while (re-search-forward "^\\([ \t]*\\)\\_<\\(\\(?:\\w\\|\\s_\\)+\\)\\_>\\([ \t]*(\\)" nil t
)
210 (downcase (match-string 2))
217 ;------------------------------------------------------------------------------
220 ;; Navigation / marking by function or macro
223 (defconst cmake--regex-defun-start
226 (or "function" "macro")
230 (defconst cmake--regex-defun-end
234 (or "function" "macro")
236 "(" (zero-or-more (not-char ")")) ")"))
238 (defun cmake-beginning-of-defun ()
239 "Move backward to the beginning of a CMake function or macro.
241 Return t unless search stops due to beginning of buffer."
243 (when (not (region-active-p))
245 (let ((case-fold-search t
))
246 (when (re-search-backward cmake--regex-defun-start nil
'move
)
249 (defun cmake-end-of-defun ()
250 "Move forward to the end of a CMake function or macro.
252 Return t unless search stops due to end of buffer."
254 (when (not (region-active-p))
256 (let ((case-fold-search t
))
257 (when (re-search-forward cmake--regex-defun-end nil
'move
)
261 (defun cmake-mark-defun ()
262 "Mark the current CMake function or macro.
264 This puts the mark at the end, and point at the beginning."
267 (push-mark nil
:nomsg
:activate
)
268 (cmake-beginning-of-defun))
271 ;------------------------------------------------------------------------------
274 ;; Keyword highlighting regex-to-face map.
276 (defconst cmake-font-lock-keywords
277 `((,(rx-to-string `(and symbol-start
279 ,@(mapcar #'downcase cmake-keywords
))
281 . font-lock-keyword-face
)
282 (,(rx symbol-start
(group (+ (or word
(syntax symbol
)))) (* blank
) ?\
()
283 1 font-lock-function-name-face
)
284 (,(rx "${" (group (+(any alnum
"-_+/."))) "}")
285 1 font-lock-variable-name-face t
)
287 "Highlighting expressions for CMake mode.")
289 ;------------------------------------------------------------------------------
291 ;; Syntax table for this mode.
292 (defvar cmake-mode-syntax-table nil
293 "Syntax table for CMake mode.")
294 (or cmake-mode-syntax-table
295 (setq cmake-mode-syntax-table
296 (let ((table (make-syntax-table)))
297 (modify-syntax-entry ?\
( "()" table
)
298 (modify-syntax-entry ?\
) ")(" table
)
299 (modify-syntax-entry ?
# "<" table
)
300 (modify-syntax-entry ?
\n ">" table
)
301 (modify-syntax-entry ?$
"'" table
)
305 ;; User hook entry point.
307 (defvar cmake-mode-hook nil
)
309 ;;------------------------------------------------------------------------------
313 (define-derived-mode cmake-mode prog-mode
"CMake"
314 "Major mode for editing CMake source files."
316 ; Setup font-lock mode.
317 (set (make-local-variable 'font-lock-defaults
) '(cmake-font-lock-keywords))
318 ; Setup indentation function.
319 (set (make-local-variable 'indent-line-function
) 'cmake-indent
)
320 ; Setup comment syntax.
321 (set (make-local-variable 'comment-start
) "#"))
323 ;; Default cmake-mode key bindings
324 (define-key cmake-mode-map
"\e\C-a" #'cmake-beginning-of-defun
)
325 (define-key cmake-mode-map
"\e\C-e" #'cmake-end-of-defun
)
326 (define-key cmake-mode-map
"\e\C-h" #'cmake-mark-defun
)
329 ; Help mode starts here
333 (defun cmake-command-run (type &optional topic buffer
)
334 "Runs the command cmake with the arguments specified. The
335 optional argument topic will be appended to the argument list."
337 (let* ((bufname (if buffer buffer
(concat "*CMake" type
(if topic
"-") topic
"*")))
338 (buffer (if (get-buffer bufname
) (get-buffer bufname
) (generate-new-buffer bufname
)))
339 (command (concat cmake-mode-cmake-executable
" " type
" " topic
))
340 ;; Turn of resizing of mini-windows for shell-command.
341 (resize-mini-windows nil
)
343 (shell-command command buffer
)
344 (save-selected-window
345 (select-window (display-buffer buffer
'not-this-window
))
353 (defun cmake-command-run-help (type &optional topic buffer
)
354 "`cmake-command-run' but rendered in `rst-mode'."
356 (let* ((bufname (if buffer buffer
(concat "*CMake" type
(if topic
"-") topic
"*")))
357 (buffer (if (get-buffer bufname
) (get-buffer bufname
) (generate-new-buffer bufname
)))
358 (command (concat cmake-mode-cmake-executable
" " type
" " topic
))
359 ;; Turn of resizing of mini-windows for shell-command.
360 (resize-mini-windows nil
)
362 (shell-command command buffer
)
363 (save-selected-window
364 (select-window (display-buffer buffer
'not-this-window
))
372 (defun cmake-help-list-commands ()
373 "Prints out a list of the cmake commands."
375 (cmake-command-run-help "--help-command-list")
378 (defvar cmake-commands
'() "List of available topics for --help-command.")
379 (defvar cmake-help-command-history nil
"Command read history.")
380 (defvar cmake-modules
'() "List of available topics for --help-module.")
381 (defvar cmake-help-module-history nil
"Module read history.")
382 (defvar cmake-variables
'() "List of available topics for --help-variable.")
383 (defvar cmake-help-variable-history nil
"Variable read history.")
384 (defvar cmake-properties
'() "List of available topics for --help-property.")
385 (defvar cmake-help-property-history nil
"Property read history.")
386 (defvar cmake-help-complete-history nil
"Complete help read history.")
387 (defvar cmake-string-to-list-symbol
388 '(("command" cmake-commands cmake-help-command-history
)
389 ("module" cmake-modules cmake-help-module-history
)
390 ("variable" cmake-variables cmake-help-variable-history
)
391 ("property" cmake-properties cmake-help-property-history
)
394 (defun cmake-get-list (listname)
395 "If the value of LISTVAR is nil, run cmake --help-LISTNAME-list
396 and store the result as a list in LISTVAR."
397 (let ((listvar (car (cdr (assoc listname cmake-string-to-list-symbol
)))))
398 (if (not (symbol-value listvar
))
399 (let ((temp-buffer-name "*CMake Temporary*"))
400 (save-window-excursion
401 (cmake-command-run-help (concat "--help-" listname
"-list") nil temp-buffer-name
)
402 (with-current-buffer temp-buffer-name
403 ; FIXME: Ignore first line if it is "cmake version ..." from CMake < 3.0.
404 (set listvar
(split-string (buffer-substring-no-properties (point-min) (point-max)) "\n" t
)))))
405 (symbol-value listvar
)
410 (defun cmake-symbol-at-point ()
411 (let ((symbol (symbol-at-point)))
412 (and (not (null symbol
))
413 (symbol-name symbol
))))
415 (defun cmake-help-type (type)
416 (let* ((default-entry (cmake-symbol-at-point))
417 (history (car (cdr (cdr (assoc type cmake-string-to-list-symbol
)))))
418 (input (completing-read
419 (format "CMake %s: " type
) ; prompt
420 (cmake-get-list type
) ; completions
423 default-entry
; initial-input
426 (if (string= input
"")
427 (error "No argument given")
432 (defun cmake-help-command ()
433 "Prints out the help message for the command the cursor is on."
435 (cmake-command-run-help "--help-command" (cmake-help-type "command") "*CMake Help*"))
438 (defun cmake-help-module ()
439 "Prints out the help message for the module the cursor is on."
441 (cmake-command-run-help "--help-module" (cmake-help-type "module") "*CMake Help*"))
444 (defun cmake-help-variable ()
445 "Prints out the help message for the variable the cursor is on."
447 (cmake-command-run-help "--help-variable" (cmake-help-type "variable") "*CMake Help*"))
450 (defun cmake-help-property ()
451 "Prints out the help message for the property the cursor is on."
453 (cmake-command-run-help "--help-property" (cmake-help-type "property") "*CMake Help*"))
457 "Queries for any of the four available help topics and prints out the appropriate page."
459 (let* ((default-entry (cmake-symbol-at-point))
460 (command-list (cmake-get-list "command"))
461 (variable-list (cmake-get-list "variable"))
462 (module-list (cmake-get-list "module"))
463 (property-list (cmake-get-list "property"))
464 (all-words (append command-list variable-list module-list property-list
))
465 (input (completing-read
466 "CMake command/module/variable/property: " ; prompt
467 all-words
; completions
470 default-entry
; initial-input
471 'cmake-help-complete-history
473 (if (string= input
"")
474 (error "No argument given")
475 (if (member input command-list
)
476 (cmake-command-run-help "--help-command" input
"*CMake Help*")
477 (if (member input variable-list
)
478 (cmake-command-run-help "--help-variable" input
"*CMake Help*")
479 (if (member input module-list
)
480 (cmake-command-run-help "--help-module" input
"*CMake Help*")
481 (if (member input property-list
)
482 (cmake-command-run-help "--help-property" input
"*CMake Help*")
483 (error "Not a know help topic.") ; this really should not happen
489 (add-to-list 'auto-mode-alist
'("CMakeLists\\.txt\\'" . cmake-mode
))
490 (add-to-list 'auto-mode-alist
'("\\.cmake\\'" . cmake-mode
)))
492 ; This file provides cmake-mode.
493 (provide 'cmake-mode
)
495 ;;; cmake-mode.el ends here