cmake-mode.el 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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\\)$")
  53. (defconst cmake-regex-block-close
  54. "^[ \t]*\\(ENDIF\\|ENDFOREACH\\|ENDMACRO\\|ELSE\\|ELSEIF\\|ENDWHILE\\)[ \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. (beginning-of-line)
  95. (if (cmake-line-starts-inside-string)
  96. ()
  97. (if (bobp)
  98. (indent-line-to 0)
  99. (let ((point-start (point))
  100. token cur-indent)
  101. (save-excursion
  102. ; Search back for the last indented line.
  103. (cmake-find-last-indented-line)
  104. ; Start with the indentation on this line.
  105. (setq cur-indent (current-indentation))
  106. ; Search forward counting tokens that adjust indentation.
  107. (while (re-search-forward cmake-regex-token point-start t)
  108. (setq token (match-string 0))
  109. (if (string-match (concat "^" cmake-regex-paren-left "$") token)
  110. (setq cur-indent (+ cur-indent cmake-tab-width))
  111. )
  112. (if (string-match (concat "^" cmake-regex-paren-right "$") token)
  113. (setq cur-indent (- cur-indent cmake-tab-width))
  114. )
  115. (if (and
  116. (string-match cmake-regex-block-open token)
  117. (looking-at (concat "[ \t]*" cmake-regex-paren-left))
  118. )
  119. (setq cur-indent (+ cur-indent cmake-tab-width))
  120. )
  121. )
  122. )
  123. ; If this is the end of a block, decrease indentation.
  124. (if (looking-at cmake-regex-block-close)
  125. (setq cur-indent (- cur-indent cmake-tab-width))
  126. )
  127. ; Indent this line by the amount selected.
  128. (if (< cur-indent 0)
  129. (indent-line-to 0)
  130. (indent-line-to cur-indent)
  131. )
  132. )
  133. )
  134. )
  135. )
  136. ;------------------------------------------------------------------------------
  137. ;;
  138. ;; Keyword highlighting regex-to-face map.
  139. ;;
  140. (defconst cmake-font-lock-keywords
  141. (list '("^[ \t]*\\(\\w+\\)[ \t]*(" 1 font-lock-function-name-face))
  142. "Highlighting expressions for CMAKE mode."
  143. )
  144. ;------------------------------------------------------------------------------
  145. ;;
  146. ;; Syntax table for this mode. Initialize to nil so that it is
  147. ;; regenerated when the cmake-mode function is called.
  148. ;;
  149. (defvar cmake-mode-syntax-table nil "Syntax table for cmake-mode.")
  150. (setq cmake-mode-syntax-table nil)
  151. ;;
  152. ;; User hook entry point.
  153. ;;
  154. (defvar cmake-mode-hook nil)
  155. ;;
  156. ;; Indentation increment.
  157. ;;
  158. (defvar cmake-tab-width 2)
  159. ;------------------------------------------------------------------------------
  160. ;;
  161. ;; CMake mode startup function.
  162. ;;
  163. (defun cmake-mode ()
  164. "Major mode for editing CMake listfiles."
  165. (interactive)
  166. (kill-all-local-variables)
  167. (setq major-mode 'cmake-mode)
  168. (setq mode-name "CMAKE")
  169. ; Create the syntax table
  170. (setq cmake-mode-syntax-table (make-syntax-table))
  171. (set-syntax-table cmake-mode-syntax-table)
  172. (modify-syntax-entry ?_ "w" cmake-mode-syntax-table)
  173. (modify-syntax-entry ?\( "()" cmake-mode-syntax-table)
  174. (modify-syntax-entry ?\) ")(" cmake-mode-syntax-table)
  175. (modify-syntax-entry ?# "<" cmake-mode-syntax-table)
  176. (modify-syntax-entry ?\n ">" cmake-mode-syntax-table)
  177. ; Setup font-lock mode.
  178. (make-local-variable 'font-lock-defaults)
  179. (setq font-lock-defaults '(cmake-font-lock-keywords))
  180. ; Setup indentation function.
  181. (make-local-variable 'indent-line-function)
  182. (setq indent-line-function 'cmake-indent)
  183. ; Setup comment syntax.
  184. (make-local-variable 'comment-start)
  185. (setq comment-start "#")
  186. ; Run user hooks.
  187. (run-hooks 'cmake-mode-hook))
  188. ; This file provides cmake-mode.
  189. (provide 'cmake-mode)
  190. ;;; cmake-mode.el ends here