cmake-mode.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. ;;
  39. ;; Regular expressions used by line indentation function.
  40. ;;
  41. (defconst cmake-regex-blank "^[ \t]*$")
  42. (defconst cmake-regex-comment "#.*")
  43. (defconst cmake-regex-paren-left "(")
  44. (defconst cmake-regex-paren-right ")")
  45. (defconst cmake-regex-argument-quoted
  46. "\"\\([^\"\\\\]\\|\\\\\\(.\\|\n\\)\\)*\"")
  47. (defconst cmake-regex-argument-unquoted
  48. "\\([^ \t\r\n()#\"\\\\]\\|\\\\.\\)\\([^ \t\r\n()#\\\\]\\|\\\\.\\)*")
  49. (defconst cmake-regex-token (concat "\\(" cmake-regex-comment
  50. "\\|" cmake-regex-paren-left
  51. "\\|" cmake-regex-paren-right
  52. "\\|" cmake-regex-argument-unquoted
  53. "\\|" cmake-regex-argument-quoted
  54. "\\)"))
  55. (defconst cmake-regex-indented (concat "^\\("
  56. cmake-regex-token
  57. "\\|" "[ \t\r\n]"
  58. "\\)*"))
  59. (defconst cmake-regex-block-open
  60. "^\\(if\\|macro\\|foreach\\|else\\|elseif\\|while\\|function\\)$")
  61. (defconst cmake-regex-block-close
  62. "^[ \t]*\\(endif\\|endforeach\\|endmacro\\|else\\|elseif\\|endwhile\\|endfunction\\)[ \t]*(")
  63. ;------------------------------------------------------------------------------
  64. ;;
  65. ;; Helper functions for line indentation function.
  66. ;;
  67. (defun cmake-line-starts-inside-string ()
  68. "Determine whether the beginning of the current line is in a string."
  69. (if (save-excursion
  70. (beginning-of-line)
  71. (let ((parse-end (point)))
  72. (goto-char (point-min))
  73. (nth 3 (parse-partial-sexp (point) parse-end))
  74. )
  75. )
  76. t
  77. nil
  78. )
  79. )
  80. (defun cmake-find-last-indented-line ()
  81. "Move to the beginning of the last line that has meaningful indentation."
  82. (let ((point-start (point))
  83. region)
  84. (forward-line -1)
  85. (setq region (buffer-substring-no-properties (point) point-start))
  86. (while (and (not (bobp))
  87. (or (looking-at cmake-regex-blank)
  88. (cmake-line-starts-inside-string)
  89. (not (and (string-match cmake-regex-indented region)
  90. (= (length region) (match-end 0))))))
  91. (forward-line -1)
  92. (setq region (buffer-substring-no-properties (point) point-start))
  93. )
  94. )
  95. )
  96. ;------------------------------------------------------------------------------
  97. ;;
  98. ;; Line indentation function.
  99. ;;
  100. (defun cmake-indent ()
  101. "Indent current line as CMAKE code."
  102. (interactive)
  103. (if (cmake-line-starts-inside-string)
  104. ()
  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. (if (string-match (concat "^" cmake-regex-paren-left "$") token)
  121. (setq cur-indent (+ cur-indent cmake-tab-width))
  122. )
  123. (if (string-match (concat "^" cmake-regex-paren-right "$") token)
  124. (setq cur-indent (- cur-indent cmake-tab-width))
  125. )
  126. (if (and
  127. (string-match cmake-regex-block-open token)
  128. (looking-at (concat "[ \t]*" cmake-regex-paren-left))
  129. )
  130. (setq cur-indent (+ cur-indent cmake-tab-width))
  131. )
  132. )
  133. (goto-char point-start)
  134. ; If this is the end of a block, decrease indentation.
  135. (if (looking-at cmake-regex-block-close)
  136. (setq cur-indent (- cur-indent cmake-tab-width))
  137. )
  138. )
  139. )
  140. ; Indent this line by the amount selected.
  141. (if (< cur-indent 0)
  142. (cmake-indent-line-to 0)
  143. (cmake-indent-line-to cur-indent)
  144. )
  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 unscreamify-cmake-buffer ()
  163. "Convert all CMake commands to lowercase in buffer."
  164. (interactive)
  165. (goto-char (point-min))
  166. (while (re-search-forward "^\\([ \t]*\\)\\(\\w+\\)\\([ \t]*(\\)" nil t)
  167. (replace-match
  168. (concat
  169. (match-string 1)
  170. (downcase (match-string 2))
  171. (match-string 3))
  172. t))
  173. )
  174. ;------------------------------------------------------------------------------
  175. ;;
  176. ;; Keyword highlighting regex-to-face map.
  177. ;;
  178. (defconst cmake-font-lock-keywords
  179. (list '("^[ \t]*\\([[:word:]_]+\\)[ \t]*(" 1 font-lock-function-name-face))
  180. "Highlighting expressions for CMAKE mode."
  181. )
  182. ;------------------------------------------------------------------------------
  183. ;;
  184. ;; Syntax table for this mode. Initialize to nil so that it is
  185. ;; regenerated when the cmake-mode function is called.
  186. ;;
  187. (defvar cmake-mode-syntax-table nil "Syntax table for cmake-mode.")
  188. (setq cmake-mode-syntax-table nil)
  189. ;;
  190. ;; User hook entry point.
  191. ;;
  192. (defvar cmake-mode-hook nil)
  193. ;;
  194. ;; Indentation increment.
  195. ;;
  196. (defvar cmake-tab-width 2)
  197. ;------------------------------------------------------------------------------
  198. ;;
  199. ;; CMake mode startup function.
  200. ;;
  201. ;;;###autoload
  202. (defun cmake-mode ()
  203. "Major mode for editing CMake listfiles."
  204. (interactive)
  205. (kill-all-local-variables)
  206. (setq major-mode 'cmake-mode)
  207. (setq mode-name "CMAKE")
  208. ; Create the syntax table
  209. (setq cmake-mode-syntax-table (make-syntax-table))
  210. (set-syntax-table cmake-mode-syntax-table)
  211. (modify-syntax-entry ?\( "()" cmake-mode-syntax-table)
  212. (modify-syntax-entry ?\) ")(" cmake-mode-syntax-table)
  213. (modify-syntax-entry ?# "<" cmake-mode-syntax-table)
  214. (modify-syntax-entry ?\n ">" cmake-mode-syntax-table)
  215. ; Setup font-lock mode.
  216. (make-local-variable 'font-lock-defaults)
  217. (setq font-lock-defaults '(cmake-font-lock-keywords))
  218. ; Setup indentation function.
  219. (make-local-variable 'indent-line-function)
  220. (setq indent-line-function 'cmake-indent)
  221. ; Setup comment syntax.
  222. (make-local-variable 'comment-start)
  223. (setq comment-start "#")
  224. ; Run user hooks.
  225. (run-hooks 'cmake-mode-hook))
  226. ; Help mode starts here
  227. ;;;###autoload
  228. (defun cmake-command-run (type &optional topic buffer)
  229. "Runs the command cmake with the arguments specified. The
  230. optional argument topic will be appended to the argument list."
  231. (interactive "s")
  232. (let* ((bufname (if buffer buffer (concat "*CMake" type (if topic "-") topic "*")))
  233. (buffer (if (get-buffer bufname) (get-buffer bufname) (generate-new-buffer bufname)))
  234. (command (concat cmake-mode-cmake-executable " " type " " topic))
  235. ;; Turn of resizing of mini-windows for shell-command.
  236. (resize-mini-windows nil)
  237. )
  238. (shell-command command buffer)
  239. (save-selected-window
  240. (select-window (display-buffer buffer 'not-this-window))
  241. (cmake-mode)
  242. (toggle-read-only t))
  243. )
  244. )
  245. ;;;###autoload
  246. (defun cmake-help-list-commands ()
  247. "Prints out a list of the cmake commands."
  248. (interactive)
  249. (cmake-command-run "--help-command-list")
  250. )
  251. (defvar cmake-commands '() "List of available topics for --help-command.")
  252. (defvar cmake-help-command-history nil "Command read history.")
  253. (defvar cmake-modules '() "List of available topics for --help-module.")
  254. (defvar cmake-help-module-history nil "Module read history.")
  255. (defvar cmake-variables '() "List of available topics for --help-variable.")
  256. (defvar cmake-help-variable-history nil "Variable read history.")
  257. (defvar cmake-properties '() "List of available topics for --help-property.")
  258. (defvar cmake-help-property-history nil "Property read history.")
  259. (defvar cmake-help-complete-history nil "Complete help read history.")
  260. (defvar cmake-string-to-list-symbol
  261. '(("command" cmake-commands cmake-help-command-history)
  262. ("module" cmake-modules cmake-help-module-history)
  263. ("variable" cmake-variables cmake-help-variable-history)
  264. ("property" cmake-properties cmake-help-property-history)
  265. ))
  266. (defun cmake-get-list (listname)
  267. "If the value of LISTVAR is nil, run cmake --help-LISTNAME-list
  268. and store the result as a list in LISTVAR."
  269. (let ((listvar (car (cdr (assoc listname cmake-string-to-list-symbol)))))
  270. (if (not (symbol-value listvar))
  271. (let ((temp-buffer-name "*CMake Temporary*"))
  272. (save-window-excursion
  273. (cmake-command-run (concat "--help-" listname "-list") nil temp-buffer-name)
  274. (with-current-buffer temp-buffer-name
  275. (set listvar (cdr (split-string (buffer-substring-no-properties (point-min) (point-max)) "\n" t))))))
  276. (symbol-value listvar)
  277. ))
  278. )
  279. (require 'thingatpt)
  280. (defun cmake-help-type (type)
  281. (let* ((default-entry (word-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 (word-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