cmake-mode.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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]*\\(\\w+\\)[ \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 ?_ "w" cmake-mode-syntax-table)
  212. (modify-syntax-entry ?\( "()" cmake-mode-syntax-table)
  213. (modify-syntax-entry ?\) ")(" cmake-mode-syntax-table)
  214. (modify-syntax-entry ?# "<" cmake-mode-syntax-table)
  215. (modify-syntax-entry ?\n ">" cmake-mode-syntax-table)
  216. ; Setup font-lock mode.
  217. (make-local-variable 'font-lock-defaults)
  218. (setq font-lock-defaults '(cmake-font-lock-keywords))
  219. ; Setup indentation function.
  220. (make-local-variable 'indent-line-function)
  221. (setq indent-line-function 'cmake-indent)
  222. ; Setup comment syntax.
  223. (make-local-variable 'comment-start)
  224. (setq comment-start "#")
  225. ; Run user hooks.
  226. (run-hooks 'cmake-mode-hook))
  227. ; Help mode starts here
  228. ;;;###autoload
  229. (defun cmake-command-run (type &optional topic buffer)
  230. "Runs the command cmake with the arguments specified. The
  231. optional argument topic will be appended to the argument list."
  232. (interactive "s")
  233. (let* ((bufname (if buffer buffer (concat "*CMake" type (if topic "-") topic "*")))
  234. (buffer (if (get-buffer bufname) (get-buffer bufname) (generate-new-buffer bufname)))
  235. (command (concat cmake-mode-cmake-executable " " type " " topic))
  236. ;; Turn of resizing of mini-windows for shell-command.
  237. (resize-mini-windows nil)
  238. )
  239. (shell-command command buffer)
  240. (save-selected-window
  241. (select-window (display-buffer buffer 'not-this-window))
  242. (cmake-mode)
  243. (toggle-read-only t))
  244. )
  245. )
  246. ;;;###autoload
  247. (defun cmake-help-list-commands ()
  248. "Prints out a list of the cmake commands."
  249. (interactive)
  250. (cmake-command-run "--help-command-list")
  251. )
  252. (defvar cmake-commands '() "List of available topics for --help-command.")
  253. (defvar cmake-help-command-history nil "Command read history.")
  254. (defvar cmake-modules '() "List of available topics for --help-module.")
  255. (defvar cmake-help-module-history nil "Module read history.")
  256. (defvar cmake-variables '() "List of available topics for --help-variable.")
  257. (defvar cmake-help-variable-history nil "Variable read history.")
  258. (defvar cmake-properties '() "List of available topics for --help-property.")
  259. (defvar cmake-help-property-history nil "Property read history.")
  260. (defvar cmake-help-complete-history nil "Complete help read history.")
  261. (defvar cmake-string-to-list-symbol
  262. '(("command" cmake-commands cmake-help-command-history)
  263. ("module" cmake-modules cmake-help-module-history)
  264. ("variable" cmake-variables cmake-help-variable-history)
  265. ("property" cmake-properties cmake-help-property-history)
  266. ))
  267. (defun cmake-get-list (listname)
  268. "If the value of LISTVAR is nil, run cmake --help-LISTNAME-list
  269. and store the result as a list in LISTVAR."
  270. (let ((listvar (car (cdr (assoc listname cmake-string-to-list-symbol)))))
  271. (if (not (symbol-value listvar))
  272. (let ((temp-buffer-name "*CMake Temporary*"))
  273. (save-window-excursion
  274. (cmake-command-run (concat "--help-" listname "-list") nil temp-buffer-name)
  275. (with-current-buffer temp-buffer-name
  276. (set listvar (cdr (split-string (buffer-substring-no-properties (point-min) (point-max)) "\n" t))))))
  277. (symbol-value listvar)
  278. ))
  279. )
  280. (require 'thingatpt)
  281. (defun cmake-help-type (type)
  282. (let* ((default-entry (word-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 (word-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