1
0

cmake-mode.el 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ;=============================================================================
  2. ;
  3. ; Program: CMake - Cross-Platform Makefile Generator
  4. ; Module: $RCSfile$
  5. ;
  6. ; Copyright (c) 2000-$Date$ Kitware, Inc., Insight Consortium. All rights reserved.
  7. ; See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  8. ;
  9. ; This software is distributed WITHOUT ANY WARRANTY; without even
  10. ; the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. ; PURPOSE. See the above copyright notices for more information.
  12. ;
  13. ;=============================================================================
  14. ;;; cmake-mode.el --- major-mode for editing CMake sources
  15. ;------------------------------------------------------------------------------
  16. ;;; Commentary:
  17. ;; Provides syntax highlighting and indentation for CMakeLists.txt and
  18. ;; *.cmake source files.
  19. ;;
  20. ;; Add this code to your .emacs file to use the mode:
  21. ;;
  22. ;; (setq load-path (cons (expand-file-name "/dir/with/cmake-mode") load-path))
  23. ;; (require 'cmake-mode)
  24. ;; (setq auto-mode-alist
  25. ;; (append '(("CMakeLists\\.txt\\'" . cmake-mode)
  26. ;; ("\\.cmake\\'" . cmake-mode))
  27. ;; auto-mode-alist))
  28. ;------------------------------------------------------------------------------
  29. ;;; Code:
  30. ;;
  31. ;; Regular expressions used by line indentation function.
  32. ;;
  33. (defconst cmake-regex-blank "^[ \t]*$")
  34. (defconst cmake-regex-comment "#.*")
  35. (defconst cmake-regex-paren-left "(")
  36. (defconst cmake-regex-paren-right ")")
  37. (defconst cmake-regex-argument-quoted
  38. "\"\\([^\"\\\\]\\|\\\\\\(.\\|\n\\)\\)*\"")
  39. (defconst cmake-regex-argument-unquoted
  40. "\\([^ \t\r\n()#\"\\\\]\\|\\\\.\\)\\([^ \t\r\n()#\\\\]\\|\\\\.\\)*")
  41. (defconst cmake-regex-token (concat "\\(" cmake-regex-comment
  42. "\\|" cmake-regex-paren-left
  43. "\\|" cmake-regex-paren-right
  44. "\\|" cmake-regex-argument-unquoted
  45. "\\|" cmake-regex-argument-quoted
  46. "\\)"))
  47. (defconst cmake-regex-indented (concat "^\\("
  48. cmake-regex-token
  49. "\\|" "[ \t\r\n]"
  50. "\\)*"))
  51. (defconst cmake-regex-block-open
  52. "^\\(IF\\|MACRO\\|FOREACH\\|ELSE\\|ELSEIF\\|WHILE\\|FUNCTION\\)$")
  53. (defconst cmake-regex-block-close
  54. "^[ \t]*\\(ENDIF\\|ENDFOREACH\\|ENDMACRO\\|ELSE\\|ELSEIF\\|ENDWHILE\\|ENDFUNCTION\\)[ \t]*(")
  55. ;------------------------------------------------------------------------------
  56. ;;
  57. ;; Helper functions for line indentation function.
  58. ;;
  59. (defun cmake-line-starts-inside-string ()
  60. "Determine whether the beginning of the current line is in a string."
  61. (if (save-excursion
  62. (beginning-of-line)
  63. (let ((parse-end (point)))
  64. (beginning-of-buffer)
  65. (nth 3 (parse-partial-sexp (point) parse-end))
  66. )
  67. )
  68. t
  69. nil
  70. )
  71. )
  72. (defun cmake-find-last-indented-line ()
  73. "Move to the beginning of the last line that has meaningful indentation."
  74. (let ((point-start (point))
  75. region)
  76. (forward-line -1)
  77. (setq region (buffer-substring-no-properties (point) point-start))
  78. (while (and (not (bobp))
  79. (or (looking-at cmake-regex-blank)
  80. (not (and (string-match cmake-regex-indented region)
  81. (= (length region) (match-end 0))))))
  82. (forward-line -1)
  83. (setq region (buffer-substring-no-properties (point) point-start))
  84. )
  85. )
  86. )
  87. ;------------------------------------------------------------------------------
  88. ;;
  89. ;; Line indentation function.
  90. ;;
  91. (defun cmake-indent ()
  92. "Indent current line as CMAKE code."
  93. (interactive)
  94. (if (cmake-line-starts-inside-string)
  95. ()
  96. (if (bobp)
  97. (cmake-indent-line-to 0)
  98. (let (cur-indent)
  99. (save-excursion
  100. (beginning-of-line)
  101. (let ((point-start (point))
  102. token)
  103. ; Search back for the last indented line.
  104. (cmake-find-last-indented-line)
  105. ; Start with the indentation on this line.
  106. (setq cur-indent (current-indentation))
  107. ; Search forward counting tokens that adjust indentation.
  108. (while (re-search-forward cmake-regex-token point-start t)
  109. (setq token (match-string 0))
  110. (if (string-match (concat "^" cmake-regex-paren-left "$") token)
  111. (setq cur-indent (+ cur-indent cmake-tab-width))
  112. )
  113. (if (string-match (concat "^" cmake-regex-paren-right "$") token)
  114. (setq cur-indent (- cur-indent cmake-tab-width))
  115. )
  116. (if (and
  117. (string-match cmake-regex-block-open token)
  118. (looking-at (concat "[ \t]*" cmake-regex-paren-left))
  119. )
  120. (setq cur-indent (+ cur-indent cmake-tab-width))
  121. )
  122. )
  123. (goto-char point-start)
  124. ; If this is the end of a block, decrease indentation.
  125. (if (looking-at cmake-regex-block-close)
  126. (setq cur-indent (- cur-indent cmake-tab-width))
  127. )
  128. )
  129. )
  130. ; Indent this line by the amount selected.
  131. (if (< cur-indent 0)
  132. (cmake-indent-line-to 0)
  133. (cmake-indent-line-to cur-indent)
  134. )
  135. )
  136. )
  137. )
  138. )
  139. (defun cmake-point-in-indendation ()
  140. (string-match "^[ \\t]*$" (buffer-substring (point-at-bol) (point))))
  141. (defun cmake-indent-line-to (column)
  142. "Indent the current line to COLUMN.
  143. If point is within the existing indentation it is moved to the end of
  144. the indentation. Otherwise it retains the same position on the line"
  145. (if (cmake-point-in-indendation)
  146. (indent-line-to column)
  147. (save-excursion (indent-line-to column))))
  148. ;------------------------------------------------------------------------------
  149. ;;
  150. ;; Helper functions for buffer
  151. ;;
  152. (defun unscreamify-cmake-buffer ()
  153. "Convert all CMake commands to lowercase in buffer."
  154. (interactive)
  155. (setq save-point (point))
  156. (goto-char (point-min))
  157. (while (re-search-forward "^\\([ \t]*\\)\\(\\w+\\)\\([ \t]*(\\)" nil t)
  158. (replace-match
  159. (concat
  160. (match-string 1)
  161. (downcase (match-string 2))
  162. (match-string 3))
  163. t))
  164. (goto-char save-point)
  165. )
  166. ;------------------------------------------------------------------------------
  167. ;;
  168. ;; Keyword highlighting regex-to-face map.
  169. ;;
  170. (defconst cmake-font-lock-keywords
  171. (list '("^[ \t]*\\(\\w+\\)[ \t]*(" 1 font-lock-function-name-face))
  172. "Highlighting expressions for CMAKE mode."
  173. )
  174. ;------------------------------------------------------------------------------
  175. ;;
  176. ;; Syntax table for this mode. Initialize to nil so that it is
  177. ;; regenerated when the cmake-mode function is called.
  178. ;;
  179. (defvar cmake-mode-syntax-table nil "Syntax table for cmake-mode.")
  180. (setq cmake-mode-syntax-table nil)
  181. ;;
  182. ;; User hook entry point.
  183. ;;
  184. (defvar cmake-mode-hook nil)
  185. ;;
  186. ;; Indentation increment.
  187. ;;
  188. (defvar cmake-tab-width 2)
  189. ;------------------------------------------------------------------------------
  190. ;;
  191. ;; CMake mode startup function.
  192. ;;
  193. (defun cmake-mode ()
  194. "Major mode for editing CMake listfiles."
  195. (interactive)
  196. (kill-all-local-variables)
  197. (setq major-mode 'cmake-mode)
  198. (setq mode-name "CMAKE")
  199. ; Create the syntax table
  200. (setq cmake-mode-syntax-table (make-syntax-table))
  201. (set-syntax-table cmake-mode-syntax-table)
  202. (modify-syntax-entry ?_ "w" cmake-mode-syntax-table)
  203. (modify-syntax-entry ?\( "()" cmake-mode-syntax-table)
  204. (modify-syntax-entry ?\) ")(" cmake-mode-syntax-table)
  205. (modify-syntax-entry ?# "<" cmake-mode-syntax-table)
  206. (modify-syntax-entry ?\n ">" cmake-mode-syntax-table)
  207. ; Setup font-lock mode.
  208. (make-local-variable 'font-lock-defaults)
  209. (setq font-lock-defaults '(cmake-font-lock-keywords))
  210. ; Setup indentation function.
  211. (make-local-variable 'indent-line-function)
  212. (setq indent-line-function 'cmake-indent)
  213. ; Setup comment syntax.
  214. (make-local-variable 'comment-start)
  215. (setq comment-start "#")
  216. ; Run user hooks.
  217. (run-hooks 'cmake-mode-hook))
  218. ; This file provides cmake-mode.
  219. (provide 'cmake-mode)
  220. ;;; cmake-mode.el ends here