cmake-mode.el 8.1 KB

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