Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Auxiliary / cmake-mode.el
1 ;;; cmake-mode.el --- major-mode for editing CMake sources
2
3 ;; Package-Requires: ((emacs "24.1"))
4
5 ; Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
6 ; file Copyright.txt or https://cmake.org/licensing for details.
7
8 ;------------------------------------------------------------------------------
9
10 ;;; Commentary:
11
12 ;; Provides syntax highlighting and indentation for CMakeLists.txt and
13 ;; *.cmake source files.
14 ;;
15 ;; Add this code to your .emacs file to use the mode:
16 ;;
17 ;;  (setq load-path (cons (expand-file-name "/dir/with/cmake-mode") load-path))
18 ;;  (require 'cmake-mode)
19
20 ;------------------------------------------------------------------------------
21
22 ;;; Code:
23 ;;
24 ;; cmake executable variable used to run cmake --help-command
25 ;; on commands in cmake-mode
26 ;;
27 ;; cmake-command-help Written by James Bigler
28 ;;
29
30 (require 'rst)
31 (require 'rx)
32
33 (defcustom cmake-mode-cmake-executable "cmake"
34   "*The name of the cmake executable.
35
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\"))"
40   :type 'file
41   :group 'cmake)
42
43 ;; Keywords
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)))
48     (delete-dups kwds)))
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-closing-parens-line (concat "^[[:space:]]*\\("
57                                                   cmake-regex-paren-right
58                                                   "+\\)[[:space:]]*$"))
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)
66                             ?\( ?\)
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 "$"))
82
83 ;------------------------------------------------------------------------------
84
85 ;; Line indentation helper functions
86
87 (defun cmake-line-starts-inside-string ()
88   "Determine whether the beginning of the current line is in a string."
89   (save-excursion
90     (beginning-of-line)
91     (let ((parse-end (point)))
92       (goto-char (point-min))
93       (nth 3 (parse-partial-sexp (point) parse-end))
94       )
95     )
96   )
97
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))
101         region)
102     (forward-line -1)
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))))))
109       (forward-line -1)
110       (setq region (buffer-substring-no-properties (point) point-start))
111       )
112     )
113   )
114
115 ;------------------------------------------------------------------------------
116
117 ;;
118 ;; Indentation increment.
119 ;;
120 (defcustom cmake-tab-width 2
121   "Number of columns to indent cmake blocks"
122   :type 'integer
123   :group 'cmake)
124
125 ;;
126 ;; Line indentation function.
127 ;;
128 (defun cmake-indent ()
129   "Indent current line as CMake code."
130   (interactive)
131   (unless (cmake-line-starts-inside-string)
132     (if (bobp)
133         (cmake-indent-line-to 0)
134       (let (cur-indent)
135         (save-excursion
136           (beginning-of-line)
137           (let ((point-start (point))
138                 (closing-parens-only (looking-at cmake-regex-closing-parens-line))
139                 (case-fold-search t)  ;; case-insensitive
140                 token)
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))
149                     (cond
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
158               ;; parens only line'
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)))
169                   ))
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))
174                 )
175               )
176             )
177           )
178         ;; Indent this line by the amount selected.
179         (cmake-indent-line-to (max cur-indent 0))
180         )
181       )
182     )
183   )
184
185 (defun cmake-point-in-indendation ()
186   (string-match "^[ \\t]*$" (buffer-substring (point-at-bol) (point))))
187
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))))
195
196 ;------------------------------------------------------------------------------
197
198 ;;
199 ;; Helper functions for buffer
200 ;;
201 (defun cmake-unscreamify-buffer ()
202   "Convert all CMake commands to lowercase in buffer."
203   (interactive)
204   (save-excursion
205     (goto-char (point-min))
206     (while (re-search-forward "^\\([ \t]*\\)\\_<\\(\\(?:\\w\\|\\s_\\)+\\)\\_>\\([ \t]*(\\)" nil t)
207       (replace-match
208        (concat
209         (match-string 1)
210         (downcase (match-string 2))
211         (match-string 3))
212        t))
213     )
214   )
215
216
217 ;------------------------------------------------------------------------------
218
219 ;;
220 ;; Navigation / marking by function or macro
221 ;;
222
223 (defconst cmake--regex-defun-start
224   (rx line-start
225       (zero-or-more space)
226       (or "function" "macro")
227       (zero-or-more space)
228       "("))
229
230 (defconst cmake--regex-defun-end
231   (rx line-start
232       (zero-or-more space)
233       "end"
234       (or "function" "macro")
235       (zero-or-more space)
236       "(" (zero-or-more (not-char ")")) ")"))
237
238 (defun cmake-beginning-of-defun ()
239   "Move backward to the beginning of a CMake function or macro.
240
241 Return t unless search stops due to beginning of buffer."
242   (interactive)
243   (when (not (region-active-p))
244     (push-mark))
245   (let ((case-fold-search t))
246     (when (re-search-backward cmake--regex-defun-start nil 'move)
247       t)))
248
249 (defun cmake-end-of-defun ()
250   "Move forward to the end of a CMake function or macro.
251
252 Return t unless search stops due to end of buffer."
253   (interactive)
254   (when (not (region-active-p))
255     (push-mark))
256   (let ((case-fold-search t))
257     (when (re-search-forward cmake--regex-defun-end nil 'move)
258       (forward-line)
259       t)))
260
261 (defun cmake-mark-defun ()
262   "Mark the current CMake function or macro.
263
264 This puts the mark at the end, and point at the beginning."
265   (interactive)
266   (cmake-end-of-defun)
267   (push-mark nil :nomsg :activate)
268   (cmake-beginning-of-defun))
269
270
271 ;------------------------------------------------------------------------------
272
273 ;;
274 ;; Keyword highlighting regex-to-face map.
275 ;;
276 (defconst cmake-font-lock-keywords
277   `((,(rx-to-string `(and symbol-start
278                           (or ,@cmake-keywords
279                               ,@(mapcar #'downcase cmake-keywords))
280                           symbol-end))
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)
286     )
287   "Highlighting expressions for CMake mode.")
288
289 ;------------------------------------------------------------------------------
290
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
295   ;; opening brackets
296   (let* ((mb (match-beginning 2))
297          (me (match-end 2))
298          (cb (format "]%s]" (buffer-substring mb me))))
299     (save-match-data
300       (if (search-forward cb end 'move)
301           (progn
302             (setq me (match-end 0))
303             (put-text-property
304              (1- me)
305              me
306              'syntax-table
307              (string-to-syntax syntax)))
308         (setq me end)))
309     (put-text-property
310      (match-beginning 1)
311      me
312      'syntax-multiline
313      t)))
314
315 (defconst cmake--syntax-propertize-function
316   (syntax-propertize-rules
317    ("\\(#\\)\\[\\(=*\\)\\["
318     (1
319      (prog1 "!" (cmake--syntax-propertize-until-bracket-close "!"))))
320    ("\\(\\[\\)\\(=*\\)\\["
321     (1
322      (prog1 "|" (cmake--syntax-propertize-until-bracket-close "|"))))))
323
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)
335             table)))
336
337 ;;
338 ;; User hook entry point.
339 ;;
340 (defvar cmake-mode-hook nil)
341
342 ;;------------------------------------------------------------------------------
343 ;; Mode definition.
344 ;;
345 ;;;###autoload
346 (define-derived-mode cmake-mode prog-mode "CMake"
347   "Major mode for editing CMake source files."
348
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))
358
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)
363
364
365 ; Help mode starts here
366
367
368 ;;;###autoload
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."
372   (interactive "s")
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)
378          )
379     (shell-command command buffer)
380     (save-selected-window
381       (select-window (display-buffer buffer 'not-this-window))
382       (cmake-mode)
383       (read-only-mode 1)
384       (view-mode 1))
385     )
386   )
387
388 ;;;###autoload
389 (defun cmake-command-run-help (type &optional topic buffer)
390   "`cmake-command-run' but rendered in `rst-mode'."
391   (interactive "s")
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)
397          )
398     (shell-command command buffer)
399     (save-selected-window
400       (select-window (display-buffer buffer 'not-this-window))
401       (rst-mode)
402       (read-only-mode 1)
403       (view-mode 1))
404     )
405   )
406
407 ;;;###autoload
408 (defun cmake-help-list-commands ()
409   "Prints out a list of the cmake commands."
410   (interactive)
411   (cmake-command-run-help "--help-command-list")
412   )
413
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)
428     ))
429
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)
442       ))
443   )
444
445 (require 'thingatpt)
446 (defun cmake-symbol-at-point ()
447   (let ((symbol (symbol-at-point)))
448     (and (not (null symbol))
449          (symbol-name symbol))))
450
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
457                  nil ; predicate
458                  t   ; require-match
459                  default-entry ; initial-input
460                  history
461                  )))
462     (if (string= input "")
463         (error "No argument given")
464       input))
465   )
466
467 ;;;###autoload
468 (defun cmake-help-command ()
469   "Prints out the help message for the command the cursor is on."
470   (interactive)
471   (cmake-command-run-help "--help-command" (cmake-help-type "command") "*CMake Help*"))
472
473 ;;;###autoload
474 (defun cmake-help-module ()
475   "Prints out the help message for the module the cursor is on."
476   (interactive)
477   (cmake-command-run-help "--help-module" (cmake-help-type "module") "*CMake Help*"))
478
479 ;;;###autoload
480 (defun cmake-help-variable ()
481   "Prints out the help message for the variable the cursor is on."
482   (interactive)
483   (cmake-command-run-help "--help-variable" (cmake-help-type "variable") "*CMake Help*"))
484
485 ;;;###autoload
486 (defun cmake-help-property ()
487   "Prints out the help message for the property the cursor is on."
488   (interactive)
489   (cmake-command-run-help "--help-property" (cmake-help-type "property") "*CMake Help*"))
490
491 ;;;###autoload
492 (defun cmake-help ()
493   "Queries for any of the four available help topics and prints out the appropriate page."
494   (interactive)
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
504                  nil ; predicate
505                  t   ; require-match
506                  default-entry ; initial-input
507                  'cmake-help-complete-history
508                  )))
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
520               ))))))
521   )
522
523 ;;;###autoload
524 (progn
525   (add-to-list 'auto-mode-alist '("CMakeLists\\.txt\\'" . cmake-mode))
526   (add-to-list 'auto-mode-alist '("\\.cmake\\'" . cmake-mode)))
527
528 ; This file provides cmake-mode.
529 (provide 'cmake-mode)
530
531 ;;; cmake-mode.el ends here