cmake-mode.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 bow (or ,@(append cmake-keywords-block-open
  64. (mapcar 'downcase cmake-keywords-block-open))) eow)))
  65. (defconst cmake-regex-block-close
  66. (rx-to-string `(and bow (or ,@(append cmake-keywords-block-close
  67. (mapcar 'downcase cmake-keywords-block-close))) eow)))
  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. ;; Line indentation function.
  102. ;;
  103. (defun cmake-indent ()
  104. "Indent current line as CMake code."
  105. (interactive)
  106. (unless (cmake-line-starts-inside-string)
  107. (if (bobp)
  108. (cmake-indent-line-to 0)
  109. (let (cur-indent)
  110. (save-excursion
  111. (beginning-of-line)
  112. (let ((point-start (point))
  113. (case-fold-search t) ;; case-insensitive
  114. token)
  115. ; Search back for the last indented line.
  116. (cmake-find-last-indented-line)
  117. ; Start with the indentation on this line.
  118. (setq cur-indent (current-indentation))
  119. ; Search forward counting tokens that adjust indentation.
  120. (while (re-search-forward cmake-regex-token point-start t)
  121. (setq token (match-string 0))
  122. (when (or (string-match (concat "^" cmake-regex-paren-left "$") token)
  123. (and (string-match cmake-regex-block-open token)
  124. (looking-at (concat "[ \t]*" cmake-regex-paren-left))))
  125. (setq cur-indent (+ cur-indent cmake-tab-width)))
  126. (when (string-match (concat "^" cmake-regex-paren-right "$") token)
  127. (setq cur-indent (- cur-indent cmake-tab-width)))
  128. )
  129. (goto-char point-start)
  130. ;; If next token closes the block, decrease indentation
  131. (when (looking-at cmake-regex-close)
  132. (setq cur-indent (- cur-indent cmake-tab-width))
  133. )
  134. )
  135. )
  136. ; Indent this line by the amount selected.
  137. (cmake-indent-line-to (max cur-indent 0))
  138. )
  139. )
  140. )
  141. )
  142. (defun cmake-point-in-indendation ()
  143. (string-match "^[ \\t]*$" (buffer-substring (point-at-bol) (point))))
  144. (defun cmake-indent-line-to (column)
  145. "Indent the current line to COLUMN.
  146. If point is within the existing indentation it is moved to the end of
  147. the indentation. Otherwise it retains the same position on the line"
  148. (if (cmake-point-in-indendation)
  149. (indent-line-to column)
  150. (save-excursion (indent-line-to column))))
  151. ;------------------------------------------------------------------------------
  152. ;;
  153. ;; Helper functions for buffer
  154. ;;
  155. (defun cmake-unscreamify-buffer ()
  156. "Convert all CMake commands to lowercase in buffer."
  157. (interactive)
  158. (save-excursion
  159. (goto-char (point-min))
  160. (while (re-search-forward "^\\([ \t]*\\)\\(\\w+\\)\\([ \t]*(\\)" nil t)
  161. (replace-match
  162. (concat
  163. (match-string 1)
  164. (downcase (match-string 2))
  165. (match-string 3))
  166. t))
  167. )
  168. )
  169. ;------------------------------------------------------------------------------
  170. ;;
  171. ;; Keyword highlighting regex-to-face map.
  172. ;;
  173. (defconst cmake-font-lock-keywords
  174. `((,(rx-to-string `(and symbol-start
  175. (or ,@cmake-keywords
  176. ,@(mapcar #'downcase cmake-keywords))
  177. symbol-end))
  178. . font-lock-keyword-face)
  179. (,(rx symbol-start (group (+ (or word (syntax symbol)))) ?\()
  180. 1 font-lock-function-name-face)
  181. ("\\${?\\([[:alpha:]_][[:alnum:]_]*\\|[0-9]+\\|[$*_]\\)"
  182. 1 font-lock-variable-name-face t)
  183. )
  184. "Highlighting expressions for CMake mode.")
  185. ;------------------------------------------------------------------------------
  186. ;; Syntax table for this mode.
  187. (defvar cmake-mode-syntax-table nil
  188. "Syntax table for CMake mode.")
  189. (or cmake-mode-syntax-table
  190. (setq cmake-mode-syntax-table
  191. (let ((table (make-syntax-table)))
  192. (modify-syntax-entry ?\( "()" table)
  193. (modify-syntax-entry ?\) ")(" table)
  194. (modify-syntax-entry ?# "<" table)
  195. (modify-syntax-entry ?\n ">" table)
  196. (modify-syntax-entry ?$ "'" table)
  197. table)))
  198. ;;
  199. ;; User hook entry point.
  200. ;;
  201. (defvar cmake-mode-hook nil)
  202. ;;
  203. ;; Indentation increment.
  204. ;;
  205. (defvar cmake-tab-width 2)
  206. ;------------------------------------------------------------------------------
  207. ;; For compatibility with Emacs < 24
  208. (defalias 'cmake--parent-mode
  209. (if (fboundp 'prog-mode) 'prog-mode 'fundamental-mode))
  210. ;;------------------------------------------------------------------------------
  211. ;; Mode definition.
  212. ;;
  213. ;;;###autoload
  214. (define-derived-mode cmake-mode cmake--parent-mode "CMake"
  215. "Major mode for editing CMake source files."
  216. ; Setup font-lock mode.
  217. (set (make-local-variable 'font-lock-defaults) '(cmake-font-lock-keywords))
  218. ; Setup indentation function.
  219. (set (make-local-variable 'indent-line-function) 'cmake-indent)
  220. ; Setup comment syntax.
  221. (set (make-local-variable 'comment-start) "#"))
  222. ; Help mode starts here
  223. ;;;###autoload
  224. (defun cmake-command-run (type &optional topic buffer)
  225. "Runs the command cmake with the arguments specified. The
  226. optional argument topic will be appended to the argument list."
  227. (interactive "s")
  228. (let* ((bufname (if buffer buffer (concat "*CMake" type (if topic "-") topic "*")))
  229. (buffer (if (get-buffer bufname) (get-buffer bufname) (generate-new-buffer bufname)))
  230. (command (concat cmake-mode-cmake-executable " " type " " topic))
  231. ;; Turn of resizing of mini-windows for shell-command.
  232. (resize-mini-windows nil)
  233. )
  234. (shell-command command buffer)
  235. (save-selected-window
  236. (select-window (display-buffer buffer 'not-this-window))
  237. (cmake-mode)
  238. (toggle-read-only t))
  239. )
  240. )
  241. ;;;###autoload
  242. (defun cmake-help-list-commands ()
  243. "Prints out a list of the cmake commands."
  244. (interactive)
  245. (cmake-command-run "--help-command-list")
  246. )
  247. (defvar cmake-commands '() "List of available topics for --help-command.")
  248. (defvar cmake-help-command-history nil "Command read history.")
  249. (defvar cmake-modules '() "List of available topics for --help-module.")
  250. (defvar cmake-help-module-history nil "Module read history.")
  251. (defvar cmake-variables '() "List of available topics for --help-variable.")
  252. (defvar cmake-help-variable-history nil "Variable read history.")
  253. (defvar cmake-properties '() "List of available topics for --help-property.")
  254. (defvar cmake-help-property-history nil "Property read history.")
  255. (defvar cmake-help-complete-history nil "Complete help read history.")
  256. (defvar cmake-string-to-list-symbol
  257. '(("command" cmake-commands cmake-help-command-history)
  258. ("module" cmake-modules cmake-help-module-history)
  259. ("variable" cmake-variables cmake-help-variable-history)
  260. ("property" cmake-properties cmake-help-property-history)
  261. ))
  262. (defun cmake-get-list (listname)
  263. "If the value of LISTVAR is nil, run cmake --help-LISTNAME-list
  264. and store the result as a list in LISTVAR."
  265. (let ((listvar (car (cdr (assoc listname cmake-string-to-list-symbol)))))
  266. (if (not (symbol-value listvar))
  267. (let ((temp-buffer-name "*CMake Temporary*"))
  268. (save-window-excursion
  269. (cmake-command-run (concat "--help-" listname "-list") nil temp-buffer-name)
  270. (with-current-buffer temp-buffer-name
  271. (set listvar (cdr (split-string (buffer-substring-no-properties (point-min) (point-max)) "\n" t))))))
  272. (symbol-value listvar)
  273. ))
  274. )
  275. (require 'thingatpt)
  276. (defun cmake-symbol-at-point ()
  277. (let ((symbol (symbol-at-point)))
  278. (and (not (null symbol))
  279. (symbol-name symbol))))
  280. (defun cmake-help-type (type)
  281. (let* ((default-entry (cmake-symbol-at-point))
  282. (history (car (cdr (cdr (assoc type cmake-string-to-list-symbol)))))
  283. (input (completing-read
  284. (format "CMake %s: " type) ; prompt
  285. (cmake-get-list type) ; completions
  286. nil ; predicate
  287. t ; require-match
  288. default-entry ; initial-input
  289. history
  290. )))
  291. (if (string= input "")
  292. (error "No argument given")
  293. input))
  294. )
  295. ;;;###autoload
  296. (defun cmake-help-command ()
  297. "Prints out the help message for the command the cursor is on."
  298. (interactive)
  299. (cmake-command-run "--help-command" (cmake-help-type "command") "*CMake Help*"))
  300. ;;;###autoload
  301. (defun cmake-help-module ()
  302. "Prints out the help message for the module the cursor is on."
  303. (interactive)
  304. (cmake-command-run "--help-module" (cmake-help-type "module") "*CMake Help*"))
  305. ;;;###autoload
  306. (defun cmake-help-variable ()
  307. "Prints out the help message for the variable the cursor is on."
  308. (interactive)
  309. (cmake-command-run "--help-variable" (cmake-help-type "variable") "*CMake Help*"))
  310. ;;;###autoload
  311. (defun cmake-help-property ()
  312. "Prints out the help message for the property the cursor is on."
  313. (interactive)
  314. (cmake-command-run "--help-property" (cmake-help-type "property") "*CMake Help*"))
  315. ;;;###autoload
  316. (defun cmake-help ()
  317. "Queries for any of the four available help topics and prints out the approriate page."
  318. (interactive)
  319. (let* ((default-entry (cmake-symbol-at-point))
  320. (command-list (cmake-get-list "command"))
  321. (variable-list (cmake-get-list "variable"))
  322. (module-list (cmake-get-list "module"))
  323. (property-list (cmake-get-list "property"))
  324. (all-words (append command-list variable-list module-list property-list))
  325. (input (completing-read
  326. "CMake command/module/variable/property: " ; prompt
  327. all-words ; completions
  328. nil ; predicate
  329. t ; require-match
  330. default-entry ; initial-input
  331. 'cmake-help-complete-history
  332. )))
  333. (if (string= input "")
  334. (error "No argument given")
  335. (if (member input command-list)
  336. (cmake-command-run "--help-command" input "*CMake Help*")
  337. (if (member input variable-list)
  338. (cmake-command-run "--help-variable" input "*CMake Help*")
  339. (if (member input module-list)
  340. (cmake-command-run "--help-module" input "*CMake Help*")
  341. (if (member input property-list)
  342. (cmake-command-run "--help-property" input "*CMake Help*")
  343. (error "Not a know help topic.") ; this really should not happen
  344. ))))))
  345. )
  346. ;;;###autoload
  347. (progn
  348. (add-to-list 'auto-mode-alist '("CMakeLists\\.txt\\'" . cmake-mode))
  349. (add-to-list 'auto-mode-alist '("\\.cmake\\'" . cmake-mode)))
  350. ; This file provides cmake-mode.
  351. (provide 'cmake-mode)
  352. ;;; cmake-mode.el ends here