cmake-mode.el 14 KB

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