cmake-mode.el 15 KB

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