cmake-mode.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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-help-command-history nil "Topic read history.")
  253. (defvar cmake-help-commands '() "List of available topics for --help-command.")
  254. (defun cmake-command-list-as-list ()
  255. "Run cmake --help-command-list and return a list where each element is a cmake command."
  256. (let ((temp-buffer-name "*CMake Commands Temporary*"))
  257. (save-window-excursion
  258. (cmake-command-run "--help-command-list" nil temp-buffer-name)
  259. (with-current-buffer temp-buffer-name
  260. (cdr (split-string (buffer-substring-no-properties (point-min) (point-max)) "\n" t)))))
  261. )
  262. (require 'thingatpt)
  263. ;;;###autoload
  264. (defun cmake-get-command ()
  265. "Gets the topic from the minibuffer input. The default is the word the cursor is on."
  266. (let* ((default-entry (word-at-point))
  267. (input (completing-read
  268. "CMake command: " ; prompt
  269. ((lambda ()
  270. (if cmake-help-commands cmake-help-commands
  271. (setq cmake-help-commands (cmake-command-list-as-list))))) ; completions
  272. nil ; predicate
  273. t ; require-match
  274. default-entry ; initial-input
  275. 'cmake-help-command-history ; command history
  276. )))
  277. (if (string= input "")
  278. (error "No argument given")
  279. input))
  280. )
  281. ;;;###autoload
  282. (defun cmake-help-command ()
  283. "Prints out the help message corresponding to the command the cursor is on."
  284. (interactive)
  285. (cmake-command-run "--help-command" (downcase (cmake-get-command)) "*CMake Help*"))
  286. ;;;###autoload
  287. (progn
  288. (add-to-list 'auto-mode-alist '("CMakeLists\\.txt\\'" . cmake-mode))
  289. (add-to-list 'auto-mode-alist '("\\.cmake\\'" . cmake-mode)))
  290. ; This file provides cmake-mode.
  291. (provide 'cmake-mode)
  292. ;;; cmake-mode.el ends here