cmake-mode.el 16 KB

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