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 '("BLOCK" "IF" "MACRO" "FOREACH" "ELSE" "ELSEIF" "WHILE" "FUNCTION"))
45 (defconst cmake-keywords-block-close '("ENDBLOCK" "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 (defun cmake--syntax-propertize-until-bracket-close (syntax)
292 ;; This function assumes that a previous search has matched the
293 ;; beginning of a bracket_comment or bracket_argument and that the
294 ;; second capture group has matched the equal signs between the two
296 (let* ((mb (match-beginning 2))
298 (cb (format "]%s]" (buffer-substring mb me))))
300 (if (search-forward cb end 'move)
302 (setq me (match-end 0))
307 (string-to-syntax syntax)))
315 (defconst cmake--syntax-propertize-function
316 (syntax-propertize-rules
317 ("\\(#\\)\\[\\(=*\\)\\["
319 (prog1 "!" (cmake--syntax-propertize-until-bracket-close "!"))))
320 ("\\(\\[\\)\\(=*\\)\\["
322 (prog1 "|" (cmake--syntax-propertize-until-bracket-close "|"))))))
324 ;; Syntax table for this mode.
325 (defvar cmake-mode-syntax-table nil
326 "Syntax table for CMake mode.")
327 (or cmake-mode-syntax-table
328 (setq cmake-mode-syntax-table
329 (let ((table (make-syntax-table)))
330 (modify-syntax-entry ?\( "()" table)
331 (modify-syntax-entry ?\) ")(" table)
332 (modify-syntax-entry ?# "<" table)
333 (modify-syntax-entry ?\n ">" table)
334 (modify-syntax-entry ?$ "'" table)
338 ;; User hook entry point.
340 (defvar cmake-mode-hook nil)
342 ;;------------------------------------------------------------------------------
346 (define-derived-mode cmake-mode prog-mode "CMake"
347 "Major mode for editing CMake source files."
349 ; Setup font-lock mode.
350 (set (make-local-variable 'font-lock-defaults) '(cmake-font-lock-keywords))
351 ; Setup indentation function.
352 (set (make-local-variable 'indent-line-function) 'cmake-indent)
353 ; Setup comment syntax.
354 (set (make-local-variable 'comment-start) "#")
355 ;; Setup syntax propertization
356 (set (make-local-variable 'syntax-propertize-function) cmake--syntax-propertize-function)
357 (add-hook 'syntax-propertize-extend-region-functions #'syntax-propertize-multiline nil t))
359 ;; Default cmake-mode key bindings
360 (define-key cmake-mode-map "\e\C-a" #'cmake-beginning-of-defun)
361 (define-key cmake-mode-map "\e\C-e" #'cmake-end-of-defun)
362 (define-key cmake-mode-map "\e\C-h" #'cmake-mark-defun)
365 ; Help mode starts here
369 (defun cmake-command-run (type &optional topic buffer)
370 "Runs the command cmake with the arguments specified. The
371 optional argument topic will be appended to the argument list."
373 (let* ((bufname (if buffer buffer (concat "*CMake" type (if topic "-") topic "*")))
374 (buffer (if (get-buffer bufname) (get-buffer bufname) (generate-new-buffer bufname)))
375 (command (concat cmake-mode-cmake-executable " " type " " topic))
376 ;; Turn of resizing of mini-windows for shell-command.
377 (resize-mini-windows nil)
379 (shell-command command buffer)
380 (save-selected-window
381 (select-window (display-buffer buffer 'not-this-window))
389 (defun cmake-command-run-help (type &optional topic buffer)
390 "`cmake-command-run' but rendered in `rst-mode'."
392 (let* ((bufname (if buffer buffer (concat "*CMake" type (if topic "-") topic "*")))
393 (buffer (if (get-buffer bufname) (get-buffer bufname) (generate-new-buffer bufname)))
394 (command (concat cmake-mode-cmake-executable " " type " " topic))
395 ;; Turn of resizing of mini-windows for shell-command.
396 (resize-mini-windows nil)
398 (shell-command command buffer)
399 (save-selected-window
400 (select-window (display-buffer buffer 'not-this-window))
408 (defun cmake-help-list-commands ()
409 "Prints out a list of the cmake commands."
411 (cmake-command-run-help "--help-command-list")
414 (defvar cmake-commands '() "List of available topics for --help-command.")
415 (defvar cmake-help-command-history nil "Command read history.")
416 (defvar cmake-modules '() "List of available topics for --help-module.")
417 (defvar cmake-help-module-history nil "Module read history.")
418 (defvar cmake-variables '() "List of available topics for --help-variable.")
419 (defvar cmake-help-variable-history nil "Variable read history.")
420 (defvar cmake-properties '() "List of available topics for --help-property.")
421 (defvar cmake-help-property-history nil "Property read history.")
422 (defvar cmake-help-complete-history nil "Complete help read history.")
423 (defvar cmake-string-to-list-symbol
424 '(("command" cmake-commands cmake-help-command-history)
425 ("module" cmake-modules cmake-help-module-history)
426 ("variable" cmake-variables cmake-help-variable-history)
427 ("property" cmake-properties cmake-help-property-history)
430 (defun cmake-get-list (listname)
431 "If the value of LISTVAR is nil, run cmake --help-LISTNAME-list
432 and store the result as a list in LISTVAR."
433 (let ((listvar (car (cdr (assoc listname cmake-string-to-list-symbol)))))
434 (if (not (symbol-value listvar))
435 (let ((temp-buffer-name "*CMake Temporary*"))
436 (save-window-excursion
437 (cmake-command-run-help (concat "--help-" listname "-list") nil temp-buffer-name)
438 (with-current-buffer temp-buffer-name
439 ; FIXME: Ignore first line if it is "cmake version ..." from CMake < 3.0.
440 (set listvar (split-string (buffer-substring-no-properties (point-min) (point-max)) "\n" t)))))
441 (symbol-value listvar)
446 (defun cmake-symbol-at-point ()
447 (let ((symbol (symbol-at-point)))
448 (and (not (null symbol))
449 (symbol-name symbol))))
451 (defun cmake-help-type (type)
452 (let* ((default-entry (cmake-symbol-at-point))
453 (history (car (cdr (cdr (assoc type cmake-string-to-list-symbol)))))
454 (input (completing-read
455 (format "CMake %s: " type) ; prompt
456 (cmake-get-list type) ; completions
459 default-entry ; initial-input
462 (if (string= input "")
463 (error "No argument given")
468 (defun cmake-help-command ()
469 "Prints out the help message for the command the cursor is on."
471 (cmake-command-run-help "--help-command" (cmake-help-type "command") "*CMake Help*"))
474 (defun cmake-help-module ()
475 "Prints out the help message for the module the cursor is on."
477 (cmake-command-run-help "--help-module" (cmake-help-type "module") "*CMake Help*"))
480 (defun cmake-help-variable ()
481 "Prints out the help message for the variable the cursor is on."
483 (cmake-command-run-help "--help-variable" (cmake-help-type "variable") "*CMake Help*"))
486 (defun cmake-help-property ()
487 "Prints out the help message for the property the cursor is on."
489 (cmake-command-run-help "--help-property" (cmake-help-type "property") "*CMake Help*"))
493 "Queries for any of the four available help topics and prints out the appropriate page."
495 (let* ((default-entry (cmake-symbol-at-point))
496 (command-list (cmake-get-list "command"))
497 (variable-list (cmake-get-list "variable"))
498 (module-list (cmake-get-list "module"))
499 (property-list (cmake-get-list "property"))
500 (all-words (append command-list variable-list module-list property-list))
501 (input (completing-read
502 "CMake command/module/variable/property: " ; prompt
503 all-words ; completions
506 default-entry ; initial-input
507 'cmake-help-complete-history
509 (if (string= input "")
510 (error "No argument given")
511 (if (member input command-list)
512 (cmake-command-run-help "--help-command" input "*CMake Help*")
513 (if (member input variable-list)
514 (cmake-command-run-help "--help-variable" input "*CMake Help*")
515 (if (member input module-list)
516 (cmake-command-run-help "--help-module" input "*CMake Help*")
517 (if (member input property-list)
518 (cmake-command-run-help "--help-property" input "*CMake Help*")
519 (error "Not a know help topic.") ; this really should not happen
525 (add-to-list 'auto-mode-alist '("CMakeLists\\.txt\\'" . cmake-mode))
526 (add-to-list 'auto-mode-alist '("\\.cmake\\'" . cmake-mode)))
528 ; This file provides cmake-mode.
529 (provide 'cmake-mode)
531 ;;; cmake-mode.el ends here