cmake-mode.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. ;=============================================================================
  2. ; CMake - Cross Platform Makefile Generator
  3. ; Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. ;
  5. ; Distributed under the OSI-approved BSD License (the "License");
  6. ; see accompanying file Copyright.txt for details.
  7. ;
  8. ; This software is distributed WITHOUT ANY WARRANTY; without even the
  9. ; implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. ; See the License for more information.
  11. ;=============================================================================
  12. ;;; cmake-mode.el --- major-mode for editing CMake sources
  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. ;; (setq auto-mode-alist
  23. ;; (append '(("CMakeLists\\.txt\\'" . cmake-mode)
  24. ;; ("\\.cmake\\'" . cmake-mode))
  25. ;; auto-mode-alist))
  26. ;------------------------------------------------------------------------------
  27. ;;; Code:
  28. ;;
  29. ;; cmake executable variable used to run cmake --help-command
  30. ;; on commands in cmake-mode
  31. ;;
  32. ;; cmake-command-help Written by James Bigler
  33. ;;
  34. (defcustom cmake-mode-cmake-executable "cmake"
  35. "*The name of the cmake executable.
  36. This can be either absolute or looked up in $PATH. You can also
  37. set the path with these commands:
  38. (setenv \"PATH\" (concat (getenv \"PATH\") \";C:\\\\Program Files\\\\CMake 2.8\\\\bin\"))
  39. (setenv \"PATH\" (concat (getenv \"PATH\") \":/usr/local/cmake/bin\"))"
  40. :type 'file
  41. :group 'cmake)
  42. ;;
  43. ;; Regular expressions used by line indentation function.
  44. ;;
  45. (defconst cmake-regex-blank "^[ \t]*$")
  46. (defconst cmake-regex-comment "#.*")
  47. (defconst cmake-regex-paren-left "(")
  48. (defconst cmake-regex-paren-right ")")
  49. (defconst cmake-regex-argument-quoted
  50. "\"\\([^\"\\\\]\\|\\\\\\(.\\|\n\\)\\)*\"")
  51. (defconst cmake-regex-argument-unquoted
  52. "\\([^ \t\r\n()#\"\\\\]\\|\\\\.\\)\\([^ \t\r\n()#\\\\]\\|\\\\.\\)*")
  53. (defconst cmake-regex-token (concat "\\(" cmake-regex-comment
  54. "\\|" cmake-regex-paren-left
  55. "\\|" cmake-regex-paren-right
  56. "\\|" cmake-regex-argument-unquoted
  57. "\\|" cmake-regex-argument-quoted
  58. "\\)"))
  59. (defconst cmake-regex-indented (concat "^\\("
  60. cmake-regex-token
  61. "\\|" "[ \t\r\n]"
  62. "\\)*"))
  63. (defconst cmake-regex-block-open
  64. "^\\([iI][fF]\\|[mM][aA][cC][rR][oO]\\|[fF][oO][rR][eE][aA][cC][hH]\\|[eE][lL][sS][eE]\\|[eE][lL][sS][eE][iI][fF]\\|[wW][hH][iI][lL][eE]\\|[fF][uU][nN][cC][tT][iI][oO][nN]\\)$")
  65. (defconst cmake-regex-block-close
  66. "^[ \t]*\\([eE][nN][dD][iI][fF]\\|[eE][nN][dD][fF][oO][rR][eE][aA][cC][hH]\\|[eE][nN][dD][mM][aA][cC][rR][oO]\\|[eE][lL][sS][eE]\\|[eE][lL][sS][eE][iI][fF]\\|[eE][nN][dD][wW][hH][iI][lL][eE]\\|[eE][nN][dD][fF][uU][nN][cC][tT][iI][oO][nN]\\)[ \t]*(")
  67. ;------------------------------------------------------------------------------
  68. ;;
  69. ;; Helper functions for line indentation function.
  70. ;;
  71. (defun cmake-line-starts-inside-string ()
  72. "Determine whether the beginning of the current line is in a string."
  73. (if (save-excursion
  74. (beginning-of-line)
  75. (let ((parse-end (point)))
  76. (beginning-of-buffer)
  77. (nth 3 (parse-partial-sexp (point) parse-end))
  78. )
  79. )
  80. t
  81. nil
  82. )
  83. )
  84. (defun cmake-find-last-indented-line ()
  85. "Move to the beginning of the last line that has meaningful indentation."
  86. (let ((point-start (point))
  87. region)
  88. (forward-line -1)
  89. (setq region (buffer-substring-no-properties (point) point-start))
  90. (while (and (not (bobp))
  91. (or (looking-at cmake-regex-blank)
  92. (cmake-line-starts-inside-string)
  93. (not (and (string-match cmake-regex-indented region)
  94. (= (length region) (match-end 0))))))
  95. (forward-line -1)
  96. (setq region (buffer-substring-no-properties (point) point-start))
  97. )
  98. )
  99. )
  100. ;------------------------------------------------------------------------------
  101. ;;
  102. ;; Line indentation function.
  103. ;;
  104. (defun cmake-indent ()
  105. "Indent current line as CMAKE code."
  106. (interactive)
  107. (if (cmake-line-starts-inside-string)
  108. ()
  109. (if (bobp)
  110. (cmake-indent-line-to 0)
  111. (let (cur-indent)
  112. (save-excursion
  113. (beginning-of-line)
  114. (let ((point-start (point))
  115. token)
  116. ; Search back for the last indented line.
  117. (cmake-find-last-indented-line)
  118. ; Start with the indentation on this line.
  119. (setq cur-indent (current-indentation))
  120. ; Search forward counting tokens that adjust indentation.
  121. (while (re-search-forward cmake-regex-token point-start t)
  122. (setq token (match-string 0))
  123. (if (string-match (concat "^" cmake-regex-paren-left "$") token)
  124. (setq cur-indent (+ cur-indent cmake-tab-width))
  125. )
  126. (if (string-match (concat "^" cmake-regex-paren-right "$") token)
  127. (setq cur-indent (- cur-indent cmake-tab-width))
  128. )
  129. (if (and
  130. (string-match cmake-regex-block-open token)
  131. (looking-at (concat "[ \t]*" cmake-regex-paren-left))
  132. )
  133. (setq cur-indent (+ cur-indent cmake-tab-width))
  134. )
  135. )
  136. (goto-char point-start)
  137. ; If this is the end of a block, decrease indentation.
  138. (if (looking-at cmake-regex-block-close)
  139. (setq cur-indent (- cur-indent cmake-tab-width))
  140. )
  141. )
  142. )
  143. ; Indent this line by the amount selected.
  144. (if (< cur-indent 0)
  145. (cmake-indent-line-to 0)
  146. (cmake-indent-line-to cur-indent)
  147. )
  148. )
  149. )
  150. )
  151. )
  152. (defun cmake-point-in-indendation ()
  153. (string-match "^[ \\t]*$" (buffer-substring (point-at-bol) (point))))
  154. (defun cmake-indent-line-to (column)
  155. "Indent the current line to COLUMN.
  156. If point is within the existing indentation it is moved to the end of
  157. the indentation. Otherwise it retains the same position on the line"
  158. (if (cmake-point-in-indendation)
  159. (indent-line-to column)
  160. (save-excursion (indent-line-to column))))
  161. ;------------------------------------------------------------------------------
  162. ;;
  163. ;; Helper functions for buffer
  164. ;;
  165. (defun unscreamify-cmake-buffer ()
  166. "Convert all CMake commands to lowercase in buffer."
  167. (interactive)
  168. (setq save-point (point))
  169. (goto-char (point-min))
  170. (while (re-search-forward "^\\([ \t]*\\)\\(\\w+\\)\\([ \t]*(\\)" nil t)
  171. (replace-match
  172. (concat
  173. (match-string 1)
  174. (downcase (match-string 2))
  175. (match-string 3))
  176. t))
  177. (goto-char save-point)
  178. )
  179. ;------------------------------------------------------------------------------
  180. ;;
  181. ;; Keyword highlighting regex-to-face map.
  182. ;;
  183. (defconst cmake-font-lock-keywords
  184. (list '("^[ \t]*\\(\\w+\\)[ \t]*(" 1 font-lock-function-name-face))
  185. "Highlighting expressions for CMAKE mode."
  186. )
  187. ;------------------------------------------------------------------------------
  188. ;;
  189. ;; Syntax table for this mode. Initialize to nil so that it is
  190. ;; regenerated when the cmake-mode function is called.
  191. ;;
  192. (defvar cmake-mode-syntax-table nil "Syntax table for cmake-mode.")
  193. (setq cmake-mode-syntax-table nil)
  194. ;;
  195. ;; User hook entry point.
  196. ;;
  197. (defvar cmake-mode-hook nil)
  198. ;;
  199. ;; Indentation increment.
  200. ;;
  201. (defvar cmake-tab-width 2)
  202. ;------------------------------------------------------------------------------
  203. ;;
  204. ;; CMake mode startup function.
  205. ;;
  206. (defun cmake-mode ()
  207. "Major mode for editing CMake listfiles."
  208. (interactive)
  209. (kill-all-local-variables)
  210. (setq major-mode 'cmake-mode)
  211. (setq mode-name "CMAKE")
  212. ; Create the syntax table
  213. (setq cmake-mode-syntax-table (make-syntax-table))
  214. (set-syntax-table cmake-mode-syntax-table)
  215. (modify-syntax-entry ?_ "w" cmake-mode-syntax-table)
  216. (modify-syntax-entry ?\( "()" cmake-mode-syntax-table)
  217. (modify-syntax-entry ?\) ")(" cmake-mode-syntax-table)
  218. (modify-syntax-entry ?# "<" cmake-mode-syntax-table)
  219. (modify-syntax-entry ?\n ">" cmake-mode-syntax-table)
  220. ; Setup font-lock mode.
  221. (make-local-variable 'font-lock-defaults)
  222. (setq font-lock-defaults '(cmake-font-lock-keywords))
  223. ; Setup indentation function.
  224. (make-local-variable 'indent-line-function)
  225. (setq indent-line-function 'cmake-indent)
  226. ; Setup comment syntax.
  227. (make-local-variable 'comment-start)
  228. (setq comment-start "#")
  229. ; Run user hooks.
  230. (run-hooks 'cmake-mode-hook))
  231. ; Help mode starts here
  232. (defun cmake-command-run (type &optional topic)
  233. "Runs the command cmake with the arguments specified. The
  234. optional argument topic will be appended to the argument list."
  235. (interactive "s")
  236. (let* ((bufname (concat "*CMake" type (if topic "-") topic "*"))
  237. (buffer (get-buffer bufname))
  238. )
  239. (if buffer
  240. (display-buffer buffer 'not-this-window)
  241. ;; Buffer doesn't exist. Create it and fill it
  242. (setq buffer (generate-new-buffer bufname))
  243. (setq command (concat cmake-mode-cmake-executable " " type " " topic))
  244. (message "Running %s" command)
  245. ;; We don't want the contents of the shell-command running to the
  246. ;; minibuffer, so turn it off. A value of nil means don't automatically
  247. ;; resize mini-windows.
  248. (setq resize-mini-windows-save resize-mini-windows)
  249. (setq resize-mini-windows nil)
  250. (shell-command command buffer)
  251. ;; Save the original window, so that we can come back to it later.
  252. ;; save-excursion doesn't seem to work for this.
  253. (setq window (selected-window))
  254. ;; We need to select it so that we can apply special modes to it
  255. (select-window (display-buffer buffer 'not-this-window))
  256. (cmake-mode)
  257. (toggle-read-only t)
  258. ;; Restore the original window
  259. (select-window window)
  260. (setq resize-mini-windows resize-mini-windows-save)
  261. )
  262. )
  263. )
  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. (defun cmake-get-topic (type)
  272. "Gets the topic from the minibuffer input. The default is the word the cursor is on."
  273. (interactive)
  274. (let* ((default-entry (word-at-point))
  275. (input (read-string
  276. (format "CMake %s (default %s): " type default-entry) ; prompt
  277. nil ; initial input
  278. 'cmake-help-command-history ; command history
  279. default-entry ; default-value
  280. )))
  281. (if (string= input "")
  282. (error "No argument given")
  283. input))
  284. )
  285. (defun cmake-help-command ()
  286. "Prints out the help message corresponding to the command the cursor is on."
  287. (interactive)
  288. (setq command (cmake-get-topic "command"))
  289. (cmake-command-run "--help-command" (downcase command))
  290. )
  291. ; This file provides cmake-mode.
  292. (provide 'cmake-mode)
  293. ;;; cmake-mode.el ends here