cmake-mode.el 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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-comment "#.*")
  32. (defconst cmake-regex-identifier "[A-Za-z][A-Za-z0-9_]*")
  33. (defconst cmake-regex-quoted "\"\\([^\n\"\\\\]\\|\\\\.\\)*\"")
  34. (defconst cmake-regex-arguments (concat "\\(" cmake-regex-quoted
  35. "\\|" "[^\n()#\"\\\\]"
  36. "\\|" "\\\\."
  37. "\\|" "\\$(" cmake-regex-identifier ")"
  38. "\\)*"))
  39. (defconst cmake-indent-comment-line (concat "^[ \t]*" cmake-regex-comment))
  40. (defconst cmake-indent-blank-regex "^[ \t]*$")
  41. (defconst cmake-indent-open-regex (concat "^[ \t]*" cmake-regex-identifier
  42. "[ \t]*(" cmake-regex-arguments
  43. "\\(" cmake-regex-comment "\\)?"
  44. "\n"))
  45. (defconst cmake-indent-close-regex (concat "^" cmake-regex-arguments
  46. ")[ \t]*"
  47. "\\(" cmake-regex-comment "\\)?"
  48. "\n"))
  49. (defconst cmake-indent-begin-regex "^[ \t]*\\(IF\\|MACRO\\|FOREACH\\|ELSE\\)[ \t]*(")
  50. (defconst cmake-indent-end-regex "^[ \t]*\\(ENDIF\\|ENDFOREACH\\|ENDMACRO\\|ELSE\\)[ \t]*(")
  51. ; Line indentation function.
  52. (defun cmake-indent ()
  53. "Indent current line as CMAKE code."
  54. (interactive)
  55. (beginning-of-line)
  56. (if (bobp)
  57. (indent-line-to 0)
  58. (let (cur-indent)
  59. ; Search back for previous non-blank line.
  60. (save-excursion
  61. (forward-line -1)
  62. (while (and (not (bobp)) (looking-at cmake-indent-blank-regex))
  63. (forward-line -1)
  64. )
  65. ; Start with previous non-blank line's indentation.
  66. (setq cur-indent (current-indentation))
  67. ; If previous line is a comment line, just use its
  68. ; indentation. Otherwise, adjust indentation based on the
  69. ; line's contents.
  70. (if (not (looking-at cmake-indent-comment-line))
  71. (progn
  72. ; If previous line begins a block, we indent this line.
  73. (if (looking-at cmake-indent-begin-regex)
  74. (setq cur-indent (+ cur-indent cmake-tab-width))
  75. )
  76. ; If previous line opens a command invocation, we indent
  77. ; this line.
  78. (if (looking-at cmake-indent-open-regex)
  79. (setq cur-indent (+ cur-indent cmake-tab-width))
  80. )
  81. ; If previous line closes a command invocation, we unindent
  82. ; this line.
  83. (if (looking-at cmake-indent-close-regex)
  84. (setq cur-indent (- cur-indent cmake-tab-width))
  85. )
  86. )
  87. )
  88. )
  89. ; If this line ends a block, we unindent it.
  90. (if (looking-at cmake-indent-end-regex)
  91. (setq cur-indent (- cur-indent cmake-tab-width))
  92. )
  93. ; Indent this line by the amount selected.
  94. (if (< cur-indent 0)
  95. (indent-line-to 0)
  96. (indent-line-to cur-indent)
  97. )
  98. )
  99. )
  100. )
  101. ; (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)
  102. ; run the above in the scatch buffer to generate the string that
  103. ; goes in (list '("the regexp string" . font-lock-function-name-face)
  104. ; Define keyword highlighting.
  105. (defconst cmake-font-lock-defaults
  106. (list
  107. ;; '("(" . font-lock-keyword-face)
  108. '("\\<\\(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)
  109. "Highlighting expressions for CMAKE mode.")
  110. )
  111. ; Define a variable to hold the syntax table.
  112. (defvar cmake-mode-syntax-table nil "Syntax table for cmake-mode.")
  113. ; If this mode file is reloaded, we want the syntax table to be
  114. ; regenerated when cmake-mode is called.
  115. (setq cmake-mode-syntax-table nil)
  116. ; Let users hook to this mode.
  117. (defvar cmake-mode-hook nil)
  118. ; Mode startup function.
  119. (defun cmake-mode ()
  120. "Major mode for editing CMake listfiles."
  121. (interactive)
  122. (kill-all-local-variables)
  123. (setq major-mode 'cmake-mode)
  124. (setq mode-name "CMAKE")
  125. ; Create the syntax table
  126. (setq cmake-mode-syntax-table (make-syntax-table))
  127. (set-syntax-table cmake-mode-syntax-table)
  128. (modify-syntax-entry ?_ "w" cmake-mode-syntax-table)
  129. (modify-syntax-entry ?\( "()" cmake-mode-syntax-table)
  130. (modify-syntax-entry ?\) ")(" cmake-mode-syntax-table)
  131. (modify-syntax-entry ?# "<" cmake-mode-syntax-table)
  132. (modify-syntax-entry ?\n ">" cmake-mode-syntax-table)
  133. ; Setup font-lock mode.
  134. (make-local-variable 'font-lock-defaults)
  135. (setq font-lock-defaults '(cmake-font-lock-defaults))
  136. ; Setup indentation function.
  137. (make-local-variable 'indent-line-function)
  138. (setq indent-line-function 'cmake-indent)
  139. ; Setup comment syntax.
  140. (make-local-variable 'comment-start)
  141. (setq comment-start "#")
  142. (run-hooks 'cmake-mode-hook))
  143. ; This file provides cmake-mode.
  144. (provide 'cmake-mode)