cmake-mode.el 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. ;=============================================================================
  2. ;
  3. ; Program: CMake - Cross-Platform Makefile Generator
  4. ; Module: $RCSfile$
  5. ; Language: C++
  6. ; Date: $Date$
  7. ; Version: $Revision$
  8. ;
  9. ; Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  10. ; See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  11. ;
  12. ; This software is distributed WITHOUT ANY WARRANTY; without even
  13. ; the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14. ; PURPOSE. See the above copyright notices for more information.
  15. ;
  16. ;=============================================================================
  17. ; cmake-mode.el = Emacs major mode for editing CMake listfiles.
  18. ; Add cmake listfile names to the mode list.
  19. (setq auto-mode-alist
  20. (append
  21. '(("CMakeLists\\.txt\\'" . cmake-mode))
  22. '(("\\.cmake\\'" . cmake-mode))
  23. auto-mode-alist))
  24. ; Note, add the above setq auto-mode-alist to your .emacs
  25. ; along with the following to have the mode loaded when
  26. ; a CMakeLists.txt or *.cmake file is loaded in emacs.
  27. ; (autoload 'cmake-mode "~/CMake/Docs/cmake-mode.el" t)
  28. ; Default indentation increment.
  29. (defvar cmake-tab-width 2)
  30. ; Regular expressions used by line indentation function.
  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 "\"\\([^\"\\\\]\\|\\\\\\(.\\|\n\\)\\)*\"")
  36. (defconst cmake-regex-argument-unquoted "\\([^ \t\r\n()#\"\\\\]\\|\\\\.\\)\\([^ \t\r\n()#\\\\]\\|\\\\.\\)*")
  37. (defconst cmake-regex-token (concat "\\(" cmake-regex-comment
  38. "\\|" cmake-regex-paren-left
  39. "\\|" cmake-regex-paren-right
  40. "\\|" cmake-regex-argument-unquoted
  41. "\\|" cmake-regex-argument-quoted
  42. "\\)"))
  43. (defconst cmake-regex-indented (concat "^\\("
  44. cmake-regex-token
  45. "\\|" "[ \t\r\n]"
  46. "\\)*"))
  47. (defconst cmake-regex-block-open "^\\(IF\\|MACRO\\|FOREACH\\|ELSE\\)$")
  48. (defconst cmake-regex-block-close "^[ \t]*\\(ENDIF\\|ENDFOREACH\\|ENDMACRO\\|ELSE\\)[ \t]*(")
  49. (defun cmake-line-starts-inside-string ()
  50. "Determine whether the beginning of the current line is in a string."
  51. (if (save-excursion
  52. (beginning-of-line)
  53. (let ((parse-end (point)))
  54. (beginning-of-buffer)
  55. (nth 3 (parse-partial-sexp (point) parse-end))
  56. )
  57. )
  58. t
  59. nil
  60. )
  61. )
  62. (defun cmake-find-last-indented-line ()
  63. "Move to the beginning of the last line that has meaningful indentation."
  64. (let ((point-start (point))
  65. region)
  66. (forward-line -1)
  67. (setq region (buffer-substring-no-properties (point) point-start))
  68. (while (and (not (bobp))
  69. (or (looking-at cmake-regex-blank)
  70. (not (and (string-match cmake-regex-indented region)
  71. (= (length region) (match-end 0))))))
  72. (forward-line -1)
  73. (setq region (buffer-substring-no-properties (point) point-start))
  74. )
  75. )
  76. )
  77. ; Line indentation function.
  78. (defun cmake-indent ()
  79. "Indent current line as CMAKE code."
  80. (interactive)
  81. (beginning-of-line)
  82. (if (cmake-line-starts-inside-string)
  83. ()
  84. (if (bobp)
  85. (indent-line-to 0)
  86. (let ((point-start (point))
  87. token cur-indent)
  88. (save-excursion
  89. ; Search back for the last indented line.
  90. (cmake-find-last-indented-line)
  91. ; Start with the indentation on this line.
  92. (setq cur-indent (current-indentation))
  93. ; Search forward counting tokens that adjust indentation.
  94. (while (re-search-forward cmake-regex-token point-start t)
  95. (setq token (match-string 0))
  96. (if (string-match (concat "^" cmake-regex-paren-left "$") token)
  97. (setq cur-indent (+ cur-indent cmake-tab-width))
  98. )
  99. (if (string-match (concat "^" cmake-regex-paren-right "$") token)
  100. (setq cur-indent (- cur-indent cmake-tab-width))
  101. )
  102. (if (and
  103. (string-match cmake-regex-block-open token)
  104. (looking-at (concat "[ \t]*" cmake-regex-paren-left))
  105. )
  106. (setq cur-indent (+ cur-indent cmake-tab-width))
  107. )
  108. )
  109. )
  110. ; If this is the end of a block, decrease indentation.
  111. (if (looking-at cmake-regex-block-close)
  112. (setq cur-indent (- cur-indent cmake-tab-width))
  113. )
  114. ; Indent this line by the amount selected.
  115. (if (< cur-indent 0)
  116. (indent-line-to 0)
  117. (indent-line-to cur-indent)
  118. )
  119. )
  120. )
  121. )
  122. )
  123. ; (regexp-opt '("ABSTRACT_FILES" "ADD_CUSTOM_COMMAND" "ADD_CUSTOM_TARGET" "ADD_DEFINITIONS" "ADD_DEPENDENCIES" "ADD_EXECUTABLE" "ADD_LIBRARY" "ADD_TEST" "AUX_SOURCE_DIRECTORY" "BUILD_COMMAND" "BUILD_NAME" "CMAKE_MINIMUM_REQUIRED" "CONFIGURE_FILE" "CREATE_TEST_SOURCELIST" "CREATE_TEST_SOURCELIST " "ELSE" "ENABLE_TESTING" "ENABLE_TESTING " "ENDFOREACH" "ENDIF" "EXEC_PROGRAM" "EXPORT_LIBRARY_DEPENDENCIES" "FIND_FILE" "FIND_LIBRARY" "FIND_PACKAGE" "FIND_PATH" "FIND_PROGRAM" "FLTK_WRAP_UI" "FOREACH" "GET_CMAKE_PROPERTY" "GET_FILENAME_COMPONENT" "GET_SOURCE_FILE_PROPERTY" "GET_TARGET_PROPERTY" "IF" "INCLUDE" "INCLUDE_DIRECTORIES" "INCLUDE_EXTERNAL_MSPROJECT" "INCLUDE_REGULAR_EXPRESSION" "INSTALL_FILES" "INSTALL_PROGRAMS" "INSTALL_TARGETS" "ITK_WRAP_TCL" "LINK_DIRECTORIES" "LINK_LIBRARIES" "LOAD_CACHE" "LOAD_COMMAND" "MACRO" "MAKE_DIRECTORY" "MARK_AS_ADVANCED" "MESSAGE" "OPTION" "OUTPUT_REQUIRED_FILES" "PROJECT" "QT_WRAP_CPP" "QT_WRAP_UI" "REMOVE" "SEPARATE_ARGUMENTS" "SET" "SET_SOURCE_FILES_PROPERTIES" "SET_TARGET_PROPERTIES" "SITE_NAME" "SOURCE_FILES" "SOURCE_FILES_REMOVE" "SOURCE_GROUP" "STRING" "SUBDIRS" "SUBDIR_DEPENDS" "TARGET_LINK_LIBRARIES" "TRY_COMPILE" "TRY_RUN" "USE_MANGLED_MESA" "UTILITY_SOURCE" "VARIABLE_REQUIRES" "VTK_MAKE_INSTANTIATOR" "VTK_MAKE_INSTANTIATOR " "VTK_WRAP_JAVA" "VTK_WRAP_PYTHON" "VTK_WRAP_TCL" "WRAP_EXCLUDE_FILES" "WRITE_FILE" ) t)
  124. ; run the above in the scatch buffer to generate the string that
  125. ; goes in (list '("the regexp string" . font-lock-function-name-face)
  126. ; Define keyword highlighting.
  127. (defconst cmake-font-lock-defaults
  128. (list
  129. ;; '("(" . font-lock-keyword-face)
  130. '("\\<\\(A\\(?:BSTRACT_FILES\\|DD_\\(?:CUSTOM_\\(?:COMMAND\\|TARGET\\)\\|DE\\(?:\\(?:FINITION\\|PENDENCIE\\)S\\)\\|EXECUTABLE\\|LIBRARY\\|TEST\\)\\|UX_SOURCE_DIRECTORY\\)\\|BUILD_\\(?:COMMAND\\|NAME\\)\\|C\\(?:MAKE_MINIMUM_REQUIRED\\|ONFIGURE_FILE\\|REATE_TEST_SOURCELIST ?\\)\\|E\\(?:LSE\\|N\\(?:ABLE_TESTING ?\\|D\\(?:FOREACH\\|IF\\|MACRO\\)\\)\\|X\\(?:EC_PROGRAM\\|PORT_LIBRARY_DEPENDENCIES\\)\\)\\|F\\(?:IND_\\(?:FILE\\|LIBRARY\\|P\\(?:A\\(?:CKAGE\\|TH\\)\\|ROGRAM\\)\\)\\|LTK_WRAP_UI\\|OREACH\\)\\|GET_\\(?:CMAKE_PROPERTY\\|FILENAME_COMPONENT\\|\\(?:SOURCE_FILE\\|TARGET\\)_PROPERTY\\)\\|I\\(?:F\\|N\\(?:CLUDE\\(?:_\\(?:DIRECTORIES\\|EXTERNAL_MSPROJECT\\|REGULAR_EXPRESSION\\)\\)?\\|STALL_\\(?:\\(?:FILE\\|PROGRAM\\|TARGET\\)S\\)\\)\\|TK_WRAP_TCL\\)\\|L\\(?:INK_\\(?:\\(?:DIRECTO\\|LIBRA\\)RIES\\)\\|OAD_C\\(?:ACHE\\|OMMAND\\)\\)\\|M\\(?:A\\(?:CRO\\|KE_DIRECTORY\\|RK_AS_ADVANCED\\)\\|ESSAGE\\)\\|O\\(?:PTION\\|UTPUT_REQUIRED_FILES\\)\\|PROJECT\\|QT_WRAP_\\(?:CPP\\|UI\\)\\|REMOVE\\|S\\(?:E\\(?:PARATE_ARGUMENTS\\|T\\(?:_\\(?:\\(?:SOURCE_FILES\\|TARGET\\)_PROPERTIES\\)\\)?\\)\\|ITE_NAME\\|OURCE_\\(?:FILES\\(?:_REMOVE\\)?\\|GROUP\\)\\|TRING\\|UBDIR\\(?:\\(?:_DEPEND\\)?S\\)\\)\\|T\\(?:ARGET_LINK_LIBRARIES\\|RY_\\(?:COMPILE\\|RUN\\)\\)\\|U\\(?:SE_MANGLED_MESA\\|TILITY_SOURCE\\)\\|V\\(?:ARIABLE_REQUIRES\\|TK_\\(?:MAKE_INSTANTIATOR ?\\|WRAP_\\(?:JAVA\\|PYTHON\\|TCL\\)\\)\\)\\|WR\\(?:AP_EXCLUDE_FILES\\|ITE_FILE\\)\\)\\>" . font-lock-function-name-face)
  131. "Highlighting expressions for CMAKE mode.")
  132. )
  133. ; Define a variable to hold the syntax table.
  134. (defvar cmake-mode-syntax-table nil "Syntax table for cmake-mode.")
  135. ; If this mode file is reloaded, we want the syntax table to be
  136. ; regenerated when cmake-mode is called.
  137. (setq cmake-mode-syntax-table nil)
  138. ; Let users hook to this mode.
  139. (defvar cmake-mode-hook nil)
  140. ; Mode startup function.
  141. (defun cmake-mode ()
  142. "Major mode for editing CMake listfiles."
  143. (interactive)
  144. (kill-all-local-variables)
  145. (setq major-mode 'cmake-mode)
  146. (setq mode-name "CMAKE")
  147. ; Create the syntax table
  148. (setq cmake-mode-syntax-table (make-syntax-table))
  149. (set-syntax-table cmake-mode-syntax-table)
  150. (modify-syntax-entry ?_ "w" cmake-mode-syntax-table)
  151. (modify-syntax-entry ?\( "()" cmake-mode-syntax-table)
  152. (modify-syntax-entry ?\) ")(" cmake-mode-syntax-table)
  153. (modify-syntax-entry ?# "<" cmake-mode-syntax-table)
  154. (modify-syntax-entry ?\n ">" cmake-mode-syntax-table)
  155. ; Setup font-lock mode.
  156. (make-local-variable 'font-lock-defaults)
  157. (setq font-lock-defaults '(cmake-font-lock-defaults))
  158. ; Setup indentation function.
  159. (make-local-variable 'indent-line-function)
  160. (setq indent-line-function 'cmake-indent)
  161. ; Setup comment syntax.
  162. (make-local-variable 'comment-start)
  163. (setq comment-start "#")
  164. (run-hooks 'cmake-mode-hook))
  165. ; This file provides cmake-mode.
  166. (provide 'cmake-mode)