cmake-mode.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. ;;; cmake-mode.el --- major-mode for editing CMake sources
  2. ;; Package-Requires: ((emacs "24.1"))
  3. ; Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  4. ; file Copyright.txt or https://cmake.org/licensing for details.
  5. ;------------------------------------------------------------------------------
  6. ;;; Commentary:
  7. ;; Provides syntax highlighting and indentation for CMakeLists.txt and
  8. ;; *.cmake source files.
  9. ;;
  10. ;; Add this code to your .emacs file to use the mode:
  11. ;;
  12. ;; (setq load-path (cons (expand-file-name "/dir/with/cmake-mode") load-path))
  13. ;; (require 'cmake-mode)
  14. ;------------------------------------------------------------------------------
  15. ;;; Code:
  16. ;;
  17. ;; cmake executable variable used to run cmake --help-command
  18. ;; on commands in cmake-mode
  19. ;;
  20. ;; cmake-command-help Written by James Bigler
  21. ;;
  22. (require 'rst)
  23. (require 'rx)
  24. (defcustom cmake-mode-cmake-executable "cmake"
  25. "*The name of the cmake executable.
  26. This can be either absolute or looked up in $PATH. You can also
  27. set the path with these commands:
  28. (setenv \"PATH\" (concat (getenv \"PATH\") \";C:\\\\Program Files\\\\CMake 2.8\\\\bin\"))
  29. (setenv \"PATH\" (concat (getenv \"PATH\") \":/usr/local/cmake/bin\"))"
  30. :type 'file
  31. :group 'cmake)
  32. ;; Keywords
  33. (defconst cmake-keywords-block-open '("IF" "MACRO" "FOREACH" "ELSE" "ELSEIF" "WHILE" "FUNCTION"))
  34. (defconst cmake-keywords-block-close '("ENDIF" "ENDFOREACH" "ENDMACRO" "ELSE" "ELSEIF" "ENDWHILE" "ENDFUNCTION"))
  35. (defconst cmake-keywords
  36. (let ((kwds (append cmake-keywords-block-open cmake-keywords-block-close nil)))
  37. (delete-dups kwds)))
  38. ;; Regular expressions used by line indentation function.
  39. ;;
  40. (defconst cmake-regex-blank "^[ \t]*$")
  41. (defconst cmake-regex-comment "#.*")
  42. (defconst cmake-regex-paren-left "(")
  43. (defconst cmake-regex-paren-right ")")
  44. (defconst cmake-regex-closing-parens-line (concat "^[[:space:]]*\\("
  45. cmake-regex-paren-right
  46. "+\\)[[:space:]]*$"))
  47. (defconst cmake-regex-argument-quoted
  48. (rx ?\" (* (or (not (any ?\" ?\\)) (and ?\\ anything))) ?\"))
  49. (defconst cmake-regex-argument-unquoted
  50. (rx (or (not (any space "()#\"\\\n")) (and ?\\ nonl))
  51. (* (or (not (any space "()#\\\n")) (and ?\\ nonl)))))
  52. (defconst cmake-regex-token
  53. (rx-to-string `(group (or (regexp ,cmake-regex-comment)
  54. ?\( ?\)
  55. (regexp ,cmake-regex-argument-unquoted)
  56. (regexp ,cmake-regex-argument-quoted)))))
  57. (defconst cmake-regex-indented
  58. (rx-to-string `(and bol (* (group (or (regexp ,cmake-regex-token) (any space ?\n)))))))
  59. (defconst cmake-regex-block-open
  60. (rx-to-string `(and symbol-start (or ,@(append cmake-keywords-block-open
  61. (mapcar 'downcase cmake-keywords-block-open))) symbol-end)))
  62. (defconst cmake-regex-block-close
  63. (rx-to-string `(and symbol-start (or ,@(append cmake-keywords-block-close
  64. (mapcar 'downcase cmake-keywords-block-close))) symbol-end)))
  65. (defconst cmake-regex-close
  66. (rx-to-string `(and bol (* space) (regexp ,cmake-regex-block-close)
  67. (* space) (regexp ,cmake-regex-paren-left))))
  68. (defconst cmake-regex-token-paren-left (concat "^" cmake-regex-paren-left "$"))
  69. (defconst cmake-regex-token-paren-right (concat "^" cmake-regex-paren-right "$"))
  70. ;------------------------------------------------------------------------------
  71. ;; Line indentation helper functions
  72. (defun cmake-line-starts-inside-string ()
  73. "Determine whether the beginning of the current line is in a string."
  74. (save-excursion
  75. (beginning-of-line)
  76. (let ((parse-end (point)))
  77. (goto-char (point-min))
  78. (nth 3 (parse-partial-sexp (point) parse-end))
  79. )
  80. )
  81. )
  82. (defun cmake-find-last-indented-line ()
  83. "Move to the beginning of the last line that has meaningful indentation."
  84. (let ((point-start (point))
  85. region)
  86. (forward-line -1)
  87. (setq region (buffer-substring-no-properties (point) point-start))
  88. (while (and (not (bobp))
  89. (or (looking-at cmake-regex-blank)
  90. (cmake-line-starts-inside-string)
  91. (not (and (string-match cmake-regex-indented region)
  92. (= (length region) (match-end 0))))))
  93. (forward-line -1)
  94. (setq region (buffer-substring-no-properties (point) point-start))
  95. )
  96. )
  97. )
  98. ;------------------------------------------------------------------------------
  99. ;;
  100. ;; Indentation increment.
  101. ;;
  102. (defcustom cmake-tab-width 2
  103. "Number of columns to indent cmake blocks"
  104. :type 'integer
  105. :group 'cmake)
  106. ;;
  107. ;; Line indentation function.
  108. ;;
  109. (defun cmake-indent ()
  110. "Indent current line as CMake code."
  111. (interactive)
  112. (unless (cmake-line-starts-inside-string)
  113. (if (bobp)
  114. (cmake-indent-line-to 0)
  115. (let (cur-indent)
  116. (save-excursion
  117. (beginning-of-line)
  118. (let ((point-start (point))
  119. (closing-parens-only (looking-at cmake-regex-closing-parens-line))
  120. (case-fold-search t) ;; case-insensitive
  121. token)
  122. ;; Search back for the last indented line.
  123. (cmake-find-last-indented-line)
  124. ;; Start with the indentation on this line.
  125. (setq cur-indent (current-indentation))
  126. (if closing-parens-only
  127. (let ((open-parens 0))
  128. (while (re-search-forward cmake-regex-token point-start t)
  129. (setq token (match-string 0))
  130. (cond
  131. ((string-match cmake-regex-token-paren-left token)
  132. (setq open-parens (+ open-parens 1)))
  133. ((string-match cmake-regex-token-paren-right token)
  134. (setq open-parens (- open-parens 1)))))
  135. ;; Don't outdent if last indented line has open parens
  136. (unless (> open-parens 0)
  137. (setq cur-indent (- cur-indent cmake-tab-width))))
  138. ;; Skip detailed analysis if last indented line is a 'closing
  139. ;; parens only line'
  140. (unless (looking-at cmake-regex-closing-parens-line)
  141. ;; Search forward counting tokens that adjust indentation.
  142. (while (re-search-forward cmake-regex-token point-start t)
  143. (setq token (match-string 0))
  144. (when (or (string-match cmake-regex-token-paren-left token)
  145. (and (string-match cmake-regex-block-open token)
  146. (looking-at (concat "[ \t]*" cmake-regex-paren-left))))
  147. (setq cur-indent (+ cur-indent cmake-tab-width)))
  148. (when (string-match cmake-regex-token-paren-right token)
  149. (setq cur-indent (- cur-indent cmake-tab-width)))
  150. ))
  151. (goto-char point-start)
  152. ;; If next token closes the block, decrease indentation
  153. (when (looking-at cmake-regex-close)
  154. (setq cur-indent (- cur-indent cmake-tab-width))
  155. )
  156. )
  157. )
  158. )
  159. ;; Indent this line by the amount selected.
  160. (cmake-indent-line-to (max cur-indent 0))
  161. )
  162. )
  163. )
  164. )
  165. (defun cmake-point-in-indendation ()
  166. (string-match "^[ \\t]*$" (buffer-substring (point-at-bol) (point))))
  167. (defun cmake-indent-line-to (column)
  168. "Indent the current line to COLUMN.
  169. If point is within the existing indentation it is moved to the end of
  170. the indentation. Otherwise it retains the same position on the line"
  171. (if (cmake-point-in-indendation)
  172. (indent-line-to column)
  173. (save-excursion (indent-line-to column))))
  174. ;------------------------------------------------------------------------------
  175. ;;
  176. ;; Helper functions for buffer
  177. ;;
  178. (defun cmake-unscreamify-buffer ()
  179. "Convert all CMake commands to lowercase in buffer."
  180. (interactive)
  181. (save-excursion
  182. (goto-char (point-min))
  183. (while (re-search-forward "^\\([ \t]*\\)\\_<\\(\\(?:\\w\\|\\s_\\)+\\)\\_>\\([ \t]*(\\)" nil t)
  184. (replace-match
  185. (concat
  186. (match-string 1)
  187. (downcase (match-string 2))
  188. (match-string 3))
  189. t))
  190. )
  191. )
  192. ;------------------------------------------------------------------------------
  193. ;;
  194. ;; Navigation / marking by function or macro
  195. ;;
  196. (defconst cmake--regex-defun-start
  197. (rx line-start
  198. (zero-or-more space)
  199. (or "function" "macro")
  200. (zero-or-more space)
  201. "("))
  202. (defconst cmake--regex-defun-end
  203. (rx line-start
  204. (zero-or-more space)
  205. "end"
  206. (or "function" "macro")
  207. (zero-or-more space)
  208. "(" (zero-or-more (not-char ")")) ")"))
  209. (defun cmake-beginning-of-defun ()
  210. "Move backward to the beginning of a CMake function or macro.
  211. Return t unless search stops due to beginning of buffer."
  212. (interactive)
  213. (when (not (region-active-p))
  214. (push-mark))
  215. (let ((case-fold-search t))
  216. (when (re-search-backward cmake--regex-defun-start nil 'move)
  217. t)))
  218. (defun cmake-end-of-defun ()
  219. "Move forward to the end of a CMake function or macro.
  220. Return t unless search stops due to end of buffer."
  221. (interactive)
  222. (when (not (region-active-p))
  223. (push-mark))
  224. (let ((case-fold-search t))
  225. (when (re-search-forward cmake--regex-defun-end nil 'move)
  226. (forward-line)
  227. t)))
  228. (defun cmake-mark-defun ()
  229. "Mark the current CMake function or macro.
  230. This puts the mark at the end, and point at the beginning."
  231. (interactive)
  232. (cmake-end-of-defun)
  233. (push-mark nil :nomsg :activate)
  234. (cmake-beginning-of-defun))
  235. ;------------------------------------------------------------------------------
  236. ;;
  237. ;; Keyword highlighting regex-to-face map.
  238. ;;
  239. (defconst cmake-font-lock-keywords
  240. `((,(rx-to-string `(and symbol-start
  241. (or ,@cmake-keywords
  242. ,@(mapcar #'downcase cmake-keywords))
  243. symbol-end))
  244. . font-lock-keyword-face)
  245. (,(rx symbol-start (group (+ (or word (syntax symbol)))) (* blank) ?\()
  246. 1 font-lock-function-name-face)
  247. (,(rx "${" (group (+(any alnum "-_+/."))) "}")
  248. 1 font-lock-variable-name-face t)
  249. )
  250. "Highlighting expressions for CMake mode.")
  251. ;------------------------------------------------------------------------------
  252. ;; Syntax table for this mode.
  253. (defvar cmake-mode-syntax-table nil
  254. "Syntax table for CMake mode.")
  255. (or cmake-mode-syntax-table
  256. (setq cmake-mode-syntax-table
  257. (let ((table (make-syntax-table)))
  258. (modify-syntax-entry ?\( "()" table)
  259. (modify-syntax-entry ?\) ")(" table)
  260. (modify-syntax-entry ?# "<" table)
  261. (modify-syntax-entry ?\n ">" table)
  262. (modify-syntax-entry ?$ "'" table)
  263. table)))
  264. ;;
  265. ;; User hook entry point.
  266. ;;
  267. (defvar cmake-mode-hook nil)
  268. ;;------------------------------------------------------------------------------
  269. ;; Mode definition.
  270. ;;
  271. ;;;###autoload
  272. (define-derived-mode cmake-mode prog-mode "CMake"
  273. "Major mode for editing CMake source files."
  274. ; Setup font-lock mode.
  275. (set (make-local-variable 'font-lock-defaults) '(cmake-font-lock-keywords))
  276. ; Setup indentation function.
  277. (set (make-local-variable 'indent-line-function) 'cmake-indent)
  278. ; Setup comment syntax.
  279. (set (make-local-variable 'comment-start) "#"))
  280. ;; Default cmake-mode key bindings
  281. (define-key cmake-mode-map "\e\C-a" #'cmake-beginning-of-defun)
  282. (define-key cmake-mode-map "\e\C-e" #'cmake-end-of-defun)
  283. (define-key cmake-mode-map "\e\C-h" #'cmake-mark-defun)
  284. ; Help mode starts here
  285. ;;;###autoload
  286. (defun cmake-command-run (type &optional topic buffer)
  287. "Runs the command cmake with the arguments specified. The
  288. optional argument topic will be appended to the argument list."
  289. (interactive "s")
  290. (let* ((bufname (if buffer buffer (concat "*CMake" type (if topic "-") topic "*")))
  291. (buffer (if (get-buffer bufname) (get-buffer bufname) (generate-new-buffer bufname)))
  292. (command (concat cmake-mode-cmake-executable " " type " " topic))
  293. ;; Turn of resizing of mini-windows for shell-command.
  294. (resize-mini-windows nil)
  295. )
  296. (shell-command command buffer)
  297. (save-selected-window
  298. (select-window (display-buffer buffer 'not-this-window))
  299. (cmake-mode)
  300. (read-only-mode 1)
  301. (view-mode 1))
  302. )
  303. )
  304. ;;;###autoload
  305. (defun cmake-command-run-help (type &optional topic buffer)
  306. "`cmake-command-run' but rendered in `rst-mode'."
  307. (interactive "s")
  308. (let* ((bufname (if buffer buffer (concat "*CMake" type (if topic "-") topic "*")))
  309. (buffer (if (get-buffer bufname) (get-buffer bufname) (generate-new-buffer bufname)))
  310. (command (concat cmake-mode-cmake-executable " " type " " topic))
  311. ;; Turn of resizing of mini-windows for shell-command.
  312. (resize-mini-windows nil)
  313. )
  314. (shell-command command buffer)
  315. (save-selected-window
  316. (select-window (display-buffer buffer 'not-this-window))
  317. (rst-mode)
  318. (read-only-mode 1)
  319. (view-mode 1))
  320. )
  321. )
  322. ;;;###autoload
  323. (defun cmake-help-list-commands ()
  324. "Prints out a list of the cmake commands."
  325. (interactive)
  326. (cmake-command-run-help "--help-command-list")
  327. )
  328. (defvar cmake-commands '() "List of available topics for --help-command.")
  329. (defvar cmake-help-command-history nil "Command read history.")
  330. (defvar cmake-modules '() "List of available topics for --help-module.")
  331. (defvar cmake-help-module-history nil "Module read history.")
  332. (defvar cmake-variables '() "List of available topics for --help-variable.")
  333. (defvar cmake-help-variable-history nil "Variable read history.")
  334. (defvar cmake-properties '() "List of available topics for --help-property.")
  335. (defvar cmake-help-property-history nil "Property read history.")
  336. (defvar cmake-help-complete-history nil "Complete help read history.")
  337. (defvar cmake-string-to-list-symbol
  338. '(("command" cmake-commands cmake-help-command-history)
  339. ("module" cmake-modules cmake-help-module-history)
  340. ("variable" cmake-variables cmake-help-variable-history)
  341. ("property" cmake-properties cmake-help-property-history)
  342. ))
  343. (defun cmake-get-list (listname)
  344. "If the value of LISTVAR is nil, run cmake --help-LISTNAME-list
  345. and store the result as a list in LISTVAR."
  346. (let ((listvar (car (cdr (assoc listname cmake-string-to-list-symbol)))))
  347. (if (not (symbol-value listvar))
  348. (let ((temp-buffer-name "*CMake Temporary*"))
  349. (save-window-excursion
  350. (cmake-command-run-help (concat "--help-" listname "-list") nil temp-buffer-name)
  351. (with-current-buffer temp-buffer-name
  352. ; FIXME: Ignore first line if it is "cmake version ..." from CMake < 3.0.
  353. (set listvar (split-string (buffer-substring-no-properties (point-min) (point-max)) "\n" t)))))
  354. (symbol-value listvar)
  355. ))
  356. )
  357. (require 'thingatpt)
  358. (defun cmake-symbol-at-point ()
  359. (let ((symbol (symbol-at-point)))
  360. (and (not (null symbol))
  361. (symbol-name symbol))))
  362. (defun cmake-help-type (type)
  363. (let* ((default-entry (cmake-symbol-at-point))
  364. (history (car (cdr (cdr (assoc type cmake-string-to-list-symbol)))))
  365. (input (completing-read
  366. (format "CMake %s: " type) ; prompt
  367. (cmake-get-list type) ; completions
  368. nil ; predicate
  369. t ; require-match
  370. default-entry ; initial-input
  371. history
  372. )))
  373. (if (string= input "")
  374. (error "No argument given")
  375. input))
  376. )
  377. ;;;###autoload
  378. (defun cmake-help-command ()
  379. "Prints out the help message for the command the cursor is on."
  380. (interactive)
  381. (cmake-command-run-help "--help-command" (cmake-help-type "command") "*CMake Help*"))
  382. ;;;###autoload
  383. (defun cmake-help-module ()
  384. "Prints out the help message for the module the cursor is on."
  385. (interactive)
  386. (cmake-command-run-help "--help-module" (cmake-help-type "module") "*CMake Help*"))
  387. ;;;###autoload
  388. (defun cmake-help-variable ()
  389. "Prints out the help message for the variable the cursor is on."
  390. (interactive)
  391. (cmake-command-run-help "--help-variable" (cmake-help-type "variable") "*CMake Help*"))
  392. ;;;###autoload
  393. (defun cmake-help-property ()
  394. "Prints out the help message for the property the cursor is on."
  395. (interactive)
  396. (cmake-command-run-help "--help-property" (cmake-help-type "property") "*CMake Help*"))
  397. ;;;###autoload
  398. (defun cmake-help ()
  399. "Queries for any of the four available help topics and prints out the appropriate page."
  400. (interactive)
  401. (let* ((default-entry (cmake-symbol-at-point))
  402. (command-list (cmake-get-list "command"))
  403. (variable-list (cmake-get-list "variable"))
  404. (module-list (cmake-get-list "module"))
  405. (property-list (cmake-get-list "property"))
  406. (all-words (append command-list variable-list module-list property-list))
  407. (input (completing-read
  408. "CMake command/module/variable/property: " ; prompt
  409. all-words ; completions
  410. nil ; predicate
  411. t ; require-match
  412. default-entry ; initial-input
  413. 'cmake-help-complete-history
  414. )))
  415. (if (string= input "")
  416. (error "No argument given")
  417. (if (member input command-list)
  418. (cmake-command-run-help "--help-command" input "*CMake Help*")
  419. (if (member input variable-list)
  420. (cmake-command-run-help "--help-variable" input "*CMake Help*")
  421. (if (member input module-list)
  422. (cmake-command-run-help "--help-module" input "*CMake Help*")
  423. (if (member input property-list)
  424. (cmake-command-run-help "--help-property" input "*CMake Help*")
  425. (error "Not a know help topic.") ; this really should not happen
  426. ))))))
  427. )
  428. ;;;###autoload
  429. (progn
  430. (add-to-list 'auto-mode-alist '("CMakeLists\\.txt\\'" . cmake-mode))
  431. (add-to-list 'auto-mode-alist '("\\.cmake\\'" . cmake-mode)))
  432. ; This file provides cmake-mode.
  433. (provide 'cmake-mode)
  434. ;;; cmake-mode.el ends here