cmake-mode.el 14 KB

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