cmake-mode.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. (beginning-of-buffer)
  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. (setq save-point (point))
  166. (goto-char (point-min))
  167. (while (re-search-forward "^\\([ \t]*\\)\\(\\w+\\)\\([ \t]*(\\)" nil t)
  168. (replace-match
  169. (concat
  170. (match-string 1)
  171. (downcase (match-string 2))
  172. (match-string 3))
  173. t))
  174. (goto-char save-point)
  175. )
  176. ;------------------------------------------------------------------------------
  177. ;;
  178. ;; Keyword highlighting regex-to-face map.
  179. ;;
  180. (defconst cmake-font-lock-keywords
  181. (list '("^[ \t]*\\(\\w+\\)[ \t]*(" 1 font-lock-function-name-face))
  182. "Highlighting expressions for CMAKE mode."
  183. )
  184. ;------------------------------------------------------------------------------
  185. ;;
  186. ;; Syntax table for this mode. Initialize to nil so that it is
  187. ;; regenerated when the cmake-mode function is called.
  188. ;;
  189. (defvar cmake-mode-syntax-table nil "Syntax table for cmake-mode.")
  190. (setq cmake-mode-syntax-table nil)
  191. ;;
  192. ;; User hook entry point.
  193. ;;
  194. (defvar cmake-mode-hook nil)
  195. ;;
  196. ;; Indentation increment.
  197. ;;
  198. (defvar cmake-tab-width 2)
  199. ;------------------------------------------------------------------------------
  200. ;;
  201. ;; CMake mode startup function.
  202. ;;
  203. ;;;###autoload
  204. (defun cmake-mode ()
  205. "Major mode for editing CMake listfiles."
  206. (interactive)
  207. (kill-all-local-variables)
  208. (setq major-mode 'cmake-mode)
  209. (setq mode-name "CMAKE")
  210. ; Create the syntax table
  211. (setq cmake-mode-syntax-table (make-syntax-table))
  212. (set-syntax-table cmake-mode-syntax-table)
  213. (modify-syntax-entry ?_ "w" cmake-mode-syntax-table)
  214. (modify-syntax-entry ?\( "()" cmake-mode-syntax-table)
  215. (modify-syntax-entry ?\) ")(" cmake-mode-syntax-table)
  216. (modify-syntax-entry ?# "<" cmake-mode-syntax-table)
  217. (modify-syntax-entry ?\n ">" cmake-mode-syntax-table)
  218. ; Setup font-lock mode.
  219. (make-local-variable 'font-lock-defaults)
  220. (setq font-lock-defaults '(cmake-font-lock-keywords))
  221. ; Setup indentation function.
  222. (make-local-variable 'indent-line-function)
  223. (setq indent-line-function 'cmake-indent)
  224. ; Setup comment syntax.
  225. (make-local-variable 'comment-start)
  226. (setq comment-start "#")
  227. ; Run user hooks.
  228. (run-hooks 'cmake-mode-hook))
  229. ; Help mode starts here
  230. ;;;###autoload
  231. (defun cmake-command-run (type &optional topic)
  232. "Runs the command cmake with the arguments specified. The
  233. optional argument topic will be appended to the argument list."
  234. (interactive "s")
  235. (let* ((bufname (concat "*CMake" type (if topic "-") topic "*"))
  236. (buffer (get-buffer bufname))
  237. )
  238. (if buffer
  239. (display-buffer buffer 'not-this-window)
  240. ;; Buffer doesn't exist. Create it and fill it
  241. (setq buffer (generate-new-buffer bufname))
  242. (setq command (concat cmake-mode-cmake-executable " " type " " topic))
  243. (message "Running %s" command)
  244. ;; We don't want the contents of the shell-command running to the
  245. ;; minibuffer, so turn it off. A value of nil means don't automatically
  246. ;; resize mini-windows.
  247. (setq resize-mini-windows-save resize-mini-windows)
  248. (setq resize-mini-windows nil)
  249. (shell-command command buffer)
  250. ;; Save the original window, so that we can come back to it later.
  251. ;; save-excursion doesn't seem to work for this.
  252. (setq window (selected-window))
  253. ;; We need to select it so that we can apply special modes to it
  254. (select-window (display-buffer buffer 'not-this-window))
  255. (cmake-mode)
  256. (toggle-read-only t)
  257. ;; Restore the original window
  258. (select-window window)
  259. (setq resize-mini-windows resize-mini-windows-save)
  260. )
  261. )
  262. )
  263. ;;;###autoload
  264. (defun cmake-help-list-commands ()
  265. "Prints out a list of the cmake commands."
  266. (interactive)
  267. (cmake-command-run "--help-command-list")
  268. )
  269. (defvar cmake-help-command-history nil "Topic read history.")
  270. (require 'thingatpt)
  271. ;;;###autoload
  272. (defun cmake-get-topic (type)
  273. "Gets the topic from the minibuffer input. The default is the word the cursor is on."
  274. (interactive)
  275. (let* ((default-entry (word-at-point))
  276. (input (read-string
  277. (format "CMake %s (default %s): " type default-entry) ; prompt
  278. nil ; initial input
  279. 'cmake-help-command-history ; command history
  280. default-entry ; default-value
  281. )))
  282. (if (string= input "")
  283. (error "No argument given")
  284. input))
  285. )
  286. ;;;###autoload
  287. (defun cmake-help-command ()
  288. "Prints out the help message corresponding to the command the cursor is on."
  289. (interactive)
  290. (setq command (cmake-get-topic "command"))
  291. (cmake-command-run "--help-command" (downcase command))
  292. )
  293. ;;;###autoload
  294. (progn
  295. (add-to-list 'auto-mode-alist '("CMakeLists\\.txt\\'" . cmake-mode))
  296. (add-to-list 'auto-mode-alist '("\\.cmake\\'" . cmake-mode)))
  297. ; This file provides cmake-mode.
  298. (provide 'cmake-mode)
  299. ;;; cmake-mode.el ends here