Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Auxiliary/cmake-mode.el
5012 views
1
;;; cmake-mode.el --- major-mode for editing CMake sources -*- lexical-binding: t; -*-
2
3
;; Package-Requires: ((emacs "24.1"))
4
5
; Distributed under the OSI-approved BSD 3-Clause License. See accompanying
6
; file LICENSE.rst 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-indentation ()
186
(string-match "^[ \\t]*$" (buffer-substring (line-beginning-position) (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-indentation)
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
262
;------------------------------------------------------------------------------
263
264
;;
265
;; Keyword highlighting regex-to-face map.
266
;;
267
(defconst cmake-font-lock-keywords
268
`((,(rx-to-string `(and symbol-start
269
(or ,@cmake-keywords
270
,@(mapcar #'downcase cmake-keywords))
271
symbol-end))
272
. font-lock-keyword-face)
273
(,(rx symbol-start (group (+ (or word (syntax symbol)))) (* blank) ?\()
274
1 font-lock-function-name-face)
275
(,(rx "${" (group (+(any alnum "-_+/."))) "}")
276
1 font-lock-variable-name-face t)
277
)
278
"Highlighting expressions for CMake mode.")
279
280
;------------------------------------------------------------------------------
281
282
(defun cmake--syntax-propertize-until-bracket-close (syntax end)
283
;; This function assumes that a previous search has matched the
284
;; beginning of a bracket_comment or bracket_argument and that the
285
;; second capture group has matched the equal signs between the two
286
;; opening brackets
287
(let* ((mb (match-beginning 2))
288
(me (match-end 2))
289
(cb (format "]%s]" (buffer-substring mb me))))
290
(save-match-data
291
(if (search-forward cb end 'move)
292
(progn
293
(setq me (match-end 0))
294
(put-text-property
295
(1- me)
296
me
297
'syntax-table
298
(string-to-syntax syntax)))
299
(setq me end)))
300
(put-text-property
301
(match-beginning 1)
302
me
303
'syntax-multiline
304
t)))
305
306
(defconst cmake--syntax-propertize-function
307
(syntax-propertize-rules
308
("\\(#\\)\\[\\(=*\\)\\["
309
(1
310
(prog1 "!" (cmake--syntax-propertize-until-bracket-close "!" end))))
311
("\\(\\[\\)\\(=*\\)\\["
312
(1
313
(prog1 "|" (cmake--syntax-propertize-until-bracket-close "|" end))))))
314
315
;; Syntax table for this mode.
316
(defvar cmake-mode-syntax-table nil
317
"Syntax table for CMake mode.")
318
(or cmake-mode-syntax-table
319
(setq cmake-mode-syntax-table
320
(let ((table (make-syntax-table)))
321
(modify-syntax-entry ?\( "()" table)
322
(modify-syntax-entry ?\) ")(" table)
323
(modify-syntax-entry ?# "<" table)
324
(modify-syntax-entry ?\n ">" table)
325
(modify-syntax-entry ?$ "'" table)
326
table)))
327
328
;;
329
;; User hook entry point.
330
;;
331
(defvar cmake-mode-hook nil)
332
333
;;------------------------------------------------------------------------------
334
;; Mode definition.
335
;;
336
;;;###autoload
337
(define-derived-mode cmake-mode prog-mode "CMake"
338
"Major mode for editing CMake source files."
339
340
;; Setup jumping to beginning/end of a CMake function/macro.
341
(set (make-local-variable 'beginning-of-defun-function) #'cmake-beginning-of-defun)
342
(set (make-local-variable 'end-of-defun-function) #'cmake-end-of-defun)
343
344
; Setup font-lock mode.
345
(set (make-local-variable 'font-lock-defaults) '(cmake-font-lock-keywords))
346
; Setup indentation function.
347
(set (make-local-variable 'indent-line-function) 'cmake-indent)
348
; Setup comment syntax.
349
(set (make-local-variable 'comment-start) "#")
350
;; Setup syntax propertization
351
(set (make-local-variable 'syntax-propertize-function) cmake--syntax-propertize-function)
352
(add-hook 'syntax-propertize-extend-region-functions #'syntax-propertize-multiline nil t))
353
354
355
; Help mode starts here
356
357
358
;;;###autoload
359
(defun cmake-command-run (type &optional topic buffer)
360
"Runs the command cmake with the arguments specified. The
361
optional argument topic will be appended to the argument list."
362
(interactive "s")
363
(let* ((bufname (if buffer buffer (concat "*CMake" type (if topic "-") topic "*")))
364
(buffer (if (get-buffer bufname) (get-buffer bufname) (generate-new-buffer bufname)))
365
(command (concat cmake-mode-cmake-executable " " type " " (if topic (shell-quote-argument topic) topic)))
366
;; Turn of resizing of mini-windows for shell-command.
367
(resize-mini-windows nil)
368
)
369
(shell-command command buffer)
370
(save-selected-window
371
(select-window (display-buffer buffer 'not-this-window))
372
(cmake-mode)
373
(read-only-mode 1)
374
(view-mode 1))
375
)
376
)
377
378
;;;###autoload
379
(defun cmake-command-run-help (type &optional topic buffer)
380
"`cmake-command-run' but rendered in `rst-mode'."
381
(interactive "s")
382
(let* ((bufname (if buffer buffer (concat "*CMake" type (if topic "-") topic "*")))
383
(buffer (if (get-buffer bufname) (get-buffer bufname) (generate-new-buffer bufname)))
384
(command (concat cmake-mode-cmake-executable " " type " " (if topic (shell-quote-argument topic) topic)))
385
;; Turn of resizing of mini-windows for shell-command.
386
(resize-mini-windows nil)
387
)
388
(shell-command command buffer)
389
(save-selected-window
390
(select-window (display-buffer buffer 'not-this-window))
391
(rst-mode)
392
(read-only-mode 1)
393
(view-mode 1))
394
)
395
)
396
397
;;;###autoload
398
(defun cmake-help-list-commands ()
399
"Prints out a list of the cmake commands."
400
(interactive)
401
(cmake-command-run-help "--help-command-list")
402
)
403
404
(defvar cmake-commands '() "List of available topics for --help-command.")
405
(defvar cmake-help-command-history nil "Command read history.")
406
(defvar cmake-modules '() "List of available topics for --help-module.")
407
(defvar cmake-help-module-history nil "Module read history.")
408
(defvar cmake-variables '() "List of available topics for --help-variable.")
409
(defvar cmake-help-variable-history nil "Variable read history.")
410
(defvar cmake-properties '() "List of available topics for --help-property.")
411
(defvar cmake-help-property-history nil "Property read history.")
412
(defvar cmake-help-complete-history nil "Complete help read history.")
413
(defvar cmake-string-to-list-symbol
414
'(("command" cmake-commands cmake-help-command-history)
415
("module" cmake-modules cmake-help-module-history)
416
("variable" cmake-variables cmake-help-variable-history)
417
("property" cmake-properties cmake-help-property-history)
418
))
419
420
(defun cmake-get-list (listname)
421
"If the value of LISTVAR is nil, run cmake --help-LISTNAME-list
422
and store the result as a list in LISTVAR."
423
(let ((listvar (car (cdr (assoc listname cmake-string-to-list-symbol)))))
424
(if (not (symbol-value listvar))
425
(let ((temp-buffer-name "*CMake Temporary*"))
426
(save-window-excursion
427
(cmake-command-run-help (concat "--help-" listname "-list") nil temp-buffer-name)
428
(with-current-buffer temp-buffer-name
429
; FIXME: Ignore first line if it is "cmake version ..." from CMake < 3.0.
430
(set listvar (split-string (buffer-substring-no-properties (point-min) (point-max)) "\n" t)))))
431
(symbol-value listvar)
432
))
433
)
434
435
(require 'thingatpt)
436
(defun cmake-symbol-at-point ()
437
(let ((symbol (symbol-at-point)))
438
(and (not (null symbol))
439
(symbol-name symbol))))
440
441
(defun cmake-help-type (type)
442
(let* ((default-entry (cmake-symbol-at-point))
443
(history (car (cdr (cdr (assoc type cmake-string-to-list-symbol)))))
444
(input (completing-read
445
(format "CMake %s: " type) ; prompt
446
(cmake-get-list type) ; completions
447
nil ; predicate
448
t ; require-match
449
default-entry ; initial-input
450
history
451
)))
452
(if (string= input "")
453
(error "No argument given")
454
input))
455
)
456
457
;;;###autoload
458
(defun cmake-help-command ()
459
"Prints out the help message for the command the cursor is on."
460
(interactive)
461
(cmake-command-run-help "--help-command" (cmake-help-type "command") "*CMake Help*"))
462
463
;;;###autoload
464
(defun cmake-help-module ()
465
"Prints out the help message for the module the cursor is on."
466
(interactive)
467
(cmake-command-run-help "--help-module" (cmake-help-type "module") "*CMake Help*"))
468
469
;;;###autoload
470
(defun cmake-help-variable ()
471
"Prints out the help message for the variable the cursor is on."
472
(interactive)
473
(cmake-command-run-help "--help-variable" (cmake-help-type "variable") "*CMake Help*"))
474
475
;;;###autoload
476
(defun cmake-help-property ()
477
"Prints out the help message for the property the cursor is on."
478
(interactive)
479
(cmake-command-run-help "--help-property" (cmake-help-type "property") "*CMake Help*"))
480
481
;;;###autoload
482
(defun cmake-help ()
483
"Queries for any of the four available help topics and prints out the
484
appropriate page."
485
(interactive)
486
(let* ((default-entry (cmake-symbol-at-point))
487
(command-list (cmake-get-list "command"))
488
(variable-list (cmake-get-list "variable"))
489
(module-list (cmake-get-list "module"))
490
(property-list (cmake-get-list "property"))
491
(all-words (append command-list variable-list module-list property-list))
492
(input (completing-read
493
"CMake command/module/variable/property: " ; prompt
494
all-words ; completions
495
nil ; predicate
496
t ; require-match
497
default-entry ; initial-input
498
'cmake-help-complete-history
499
)))
500
(if (string= input "")
501
(error "No argument given")
502
(if (member input command-list)
503
(cmake-command-run-help "--help-command" input "*CMake Help*")
504
(if (member input variable-list)
505
(cmake-command-run-help "--help-variable" input "*CMake Help*")
506
(if (member input module-list)
507
(cmake-command-run-help "--help-module" input "*CMake Help*")
508
(if (member input property-list)
509
(cmake-command-run-help "--help-property" input "*CMake Help*")
510
(error "Not a know help topic.") ; this really should not happen
511
))))))
512
)
513
514
;;;###autoload
515
(progn
516
(add-to-list 'auto-mode-alist '("CMakeLists\\.txt\\'" . cmake-mode))
517
(add-to-list 'auto-mode-alist '("\\.cmake\\'" . cmake-mode)))
518
519
; This file provides cmake-mode.
520
(provide 'cmake-mode)
521
522
;;; cmake-mode.el ends here
523
524