cmake-mode.el 14 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 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. (list '("^[ \t]*\\([[:word:]_]+\\)[ \t]*(" 1 font-lock-function-name-face))
  175. "Highlighting expressions for CMAKE mode."
  176. )
  177. ;------------------------------------------------------------------------------
  178. ;;
  179. ;; Syntax table for this mode. Initialize to nil so that it is
  180. ;; regenerated when the cmake-mode function is called.
  181. ;;
  182. (defvar cmake-mode-syntax-table nil "Syntax table for cmake-mode.")
  183. (setq cmake-mode-syntax-table nil)
  184. ;;
  185. ;; User hook entry point.
  186. ;;
  187. (defvar cmake-mode-hook nil)
  188. ;;
  189. ;; Indentation increment.
  190. ;;
  191. (defvar cmake-tab-width 2)
  192. ;------------------------------------------------------------------------------
  193. ;;
  194. ;; CMake mode startup function.
  195. ;;
  196. ;;;###autoload
  197. (defun cmake-mode ()
  198. "Major mode for editing CMake listfiles."
  199. (interactive)
  200. (kill-all-local-variables)
  201. (setq major-mode 'cmake-mode)
  202. (setq mode-name "CMAKE")
  203. ; Create the syntax table
  204. (setq cmake-mode-syntax-table (make-syntax-table))
  205. (set-syntax-table cmake-mode-syntax-table)
  206. (modify-syntax-entry ?\( "()" cmake-mode-syntax-table)
  207. (modify-syntax-entry ?\) ")(" cmake-mode-syntax-table)
  208. (modify-syntax-entry ?# "<" cmake-mode-syntax-table)
  209. (modify-syntax-entry ?\n ">" cmake-mode-syntax-table)
  210. ; Setup font-lock mode.
  211. (make-local-variable 'font-lock-defaults)
  212. (setq font-lock-defaults '(cmake-font-lock-keywords))
  213. ; Setup indentation function.
  214. (make-local-variable 'indent-line-function)
  215. (setq indent-line-function 'cmake-indent)
  216. ; Setup comment syntax.
  217. (make-local-variable 'comment-start)
  218. (setq comment-start "#")
  219. ; Run user hooks.
  220. (if (boundp 'prog-mode-hook)
  221. (run-hooks 'prog-mode-hook 'cmake-mode-hook)
  222. (run-hooks 'cmake-mode-hook)))
  223. ; Help mode starts here
  224. ;;;###autoload
  225. (defun cmake-command-run (type &optional topic buffer)
  226. "Runs the command cmake with the arguments specified. The
  227. optional argument topic will be appended to the argument list."
  228. (interactive "s")
  229. (let* ((bufname (if buffer buffer (concat "*CMake" type (if topic "-") topic "*")))
  230. (buffer (if (get-buffer bufname) (get-buffer bufname) (generate-new-buffer bufname)))
  231. (command (concat cmake-mode-cmake-executable " " type " " topic))
  232. ;; Turn of resizing of mini-windows for shell-command.
  233. (resize-mini-windows nil)
  234. )
  235. (shell-command command buffer)
  236. (save-selected-window
  237. (select-window (display-buffer buffer 'not-this-window))
  238. (cmake-mode)
  239. (toggle-read-only t))
  240. )
  241. )
  242. ;;;###autoload
  243. (defun cmake-help-list-commands ()
  244. "Prints out a list of the cmake commands."
  245. (interactive)
  246. (cmake-command-run "--help-command-list")
  247. )
  248. (defvar cmake-commands '() "List of available topics for --help-command.")
  249. (defvar cmake-help-command-history nil "Command read history.")
  250. (defvar cmake-modules '() "List of available topics for --help-module.")
  251. (defvar cmake-help-module-history nil "Module read history.")
  252. (defvar cmake-variables '() "List of available topics for --help-variable.")
  253. (defvar cmake-help-variable-history nil "Variable read history.")
  254. (defvar cmake-properties '() "List of available topics for --help-property.")
  255. (defvar cmake-help-property-history nil "Property read history.")
  256. (defvar cmake-help-complete-history nil "Complete help read history.")
  257. (defvar cmake-string-to-list-symbol
  258. '(("command" cmake-commands cmake-help-command-history)
  259. ("module" cmake-modules cmake-help-module-history)
  260. ("variable" cmake-variables cmake-help-variable-history)
  261. ("property" cmake-properties cmake-help-property-history)
  262. ))
  263. (defun cmake-get-list (listname)
  264. "If the value of LISTVAR is nil, run cmake --help-LISTNAME-list
  265. and store the result as a list in LISTVAR."
  266. (let ((listvar (car (cdr (assoc listname cmake-string-to-list-symbol)))))
  267. (if (not (symbol-value listvar))
  268. (let ((temp-buffer-name "*CMake Temporary*"))
  269. (save-window-excursion
  270. (cmake-command-run (concat "--help-" listname "-list") nil temp-buffer-name)
  271. (with-current-buffer temp-buffer-name
  272. (set listvar (cdr (split-string (buffer-substring-no-properties (point-min) (point-max)) "\n" t))))))
  273. (symbol-value listvar)
  274. ))
  275. )
  276. (require 'thingatpt)
  277. (defun cmake-symbol-at-point ()
  278. (let ((symbol (symbol-at-point)))
  279. (and (not (null symbol))
  280. (symbol-name symbol))))
  281. (defun cmake-help-type (type)
  282. (let* ((default-entry (cmake-symbol-at-point))
  283. (history (car (cdr (cdr (assoc type cmake-string-to-list-symbol)))))
  284. (input (completing-read
  285. (format "CMake %s: " type) ; prompt
  286. (cmake-get-list type) ; completions
  287. nil ; predicate
  288. t ; require-match
  289. default-entry ; initial-input
  290. history
  291. )))
  292. (if (string= input "")
  293. (error "No argument given")
  294. input))
  295. )
  296. ;;;###autoload
  297. (defun cmake-help-command ()
  298. "Prints out the help message for the command the cursor is on."
  299. (interactive)
  300. (cmake-command-run "--help-command" (cmake-help-type "command") "*CMake Help*"))
  301. ;;;###autoload
  302. (defun cmake-help-module ()
  303. "Prints out the help message for the module the cursor is on."
  304. (interactive)
  305. (cmake-command-run "--help-module" (cmake-help-type "module") "*CMake Help*"))
  306. ;;;###autoload
  307. (defun cmake-help-variable ()
  308. "Prints out the help message for the variable the cursor is on."
  309. (interactive)
  310. (cmake-command-run "--help-variable" (cmake-help-type "variable") "*CMake Help*"))
  311. ;;;###autoload
  312. (defun cmake-help-property ()
  313. "Prints out the help message for the property the cursor is on."
  314. (interactive)
  315. (cmake-command-run "--help-property" (cmake-help-type "property") "*CMake Help*"))
  316. ;;;###autoload
  317. (defun cmake-help ()
  318. "Queries for any of the four available help topics and prints out the approriate page."
  319. (interactive)
  320. (let* ((default-entry (cmake-symbol-at-point))
  321. (command-list (cmake-get-list "command"))
  322. (variable-list (cmake-get-list "variable"))
  323. (module-list (cmake-get-list "module"))
  324. (property-list (cmake-get-list "property"))
  325. (all-words (append command-list variable-list module-list property-list))
  326. (input (completing-read
  327. "CMake command/module/variable/property: " ; prompt
  328. all-words ; completions
  329. nil ; predicate
  330. t ; require-match
  331. default-entry ; initial-input
  332. 'cmake-help-complete-history
  333. )))
  334. (if (string= input "")
  335. (error "No argument given")
  336. (if (member input command-list)
  337. (cmake-command-run "--help-command" input "*CMake Help*")
  338. (if (member input variable-list)
  339. (cmake-command-run "--help-variable" input "*CMake Help*")
  340. (if (member input module-list)
  341. (cmake-command-run "--help-module" input "*CMake Help*")
  342. (if (member input property-list)
  343. (cmake-command-run "--help-property" input "*CMake Help*")
  344. (error "Not a know help topic.") ; this really should not happen
  345. ))))))
  346. )
  347. ;;;###autoload
  348. (progn
  349. (add-to-list 'auto-mode-alist '("CMakeLists\\.txt\\'" . cmake-mode))
  350. (add-to-list 'auto-mode-alist '("\\.cmake\\'" . cmake-mode)))
  351. ; This file provides cmake-mode.
  352. (provide 'cmake-mode)
  353. ;;; cmake-mode.el ends here