colorful faces to make it more obvious to the user what the state is.
See the 'vc-faces' customization group.
+** CC mode
+
+*** Opening a .h file will turn C or C++ mode depending on language used.
+This is done with the help of 'c-or-c++-mode' function which analyses
+contents of the buffer to determine whether it's a C or C++ source
+file.
+
\f
* New Modes and Packages in Emacs 25.2
(defun c-extend-region-for-CPP (beg end)
;; Adjust `c-new-BEG', `c-new-END' respectively to the beginning and end of
- ;; any preprocessor construct they may be in.
+ ;; any preprocessor construct they may be in.
;;
;; Point is undefined both before and after this function call; the buffer
;; has already been widened, and match-data saved. The return value is
;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
-;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c-mode))
+;;;###autoload (add-to-list 'auto-mode-alist '("\\.c\\'" . c-mode))
+;;;###autoload (add-to-list 'auto-mode-alist '("\\.h\\'" . c-or-c++-mode))
;; NB: The following two associate yacc and lex files to C Mode, which
;; is not really suitable for those formats. Anyway, afaik there's
(cc-imenu-init cc-imenu-c-generic-expression)
(c-run-mode-hooks 'c-mode-common-hook))
+(defconst c-or-c++-mode--regexp
+ (eval-when-compile
+ (let ((id "[a-zA-Z0-9_]+") (ws "[ \t\r]+") (ws-maybe "[ \t\r]*"))
+ (concat "^" ws-maybe "\\(?:"
+ "using" ws "\\(?:namespace" ws "std;\\|std::\\)"
+ "\\|" "namespace" "\\(:?" ws id "\\)?" ws-maybe "{"
+ "\\|" "class" ws id ws-maybe "[:{\n]"
+ "\\|" "template" ws-maybe "<.*>"
+ "\\|" "#include" ws-maybe "<\\(?:string\\|iostream\\|map\\)>"
+ "\\)")))
+ "A regexp applied to C header files to check if they are really C++.")
+
+;;;###autoload
+(defun c-or-c++-mode ()
+ "Analyse buffer and enable either C or C++ mode.
+
+Some people and projects use .h extension for C++ header files
+which is also the one used for C header files. This makes
+matching on file name insufficient for detecting major mode that
+should be used.
+
+This function attempts to use file contents to determine whether
+the code is C or C++ and based on that chooses whether to enable
+`c-mode' or `c++-mode'."
+ (if (save-excursion
+ (save-restriction
+ (save-match-data
+ (widen)
+ (goto-char (point-min))
+ (re-search-forward c-or-c++-mode--regexp
+ (+ (point) c-guess-region-max) t))))
+ (c++-mode)
+ (c-mode)))
+
\f
;; Support for C++
--- /dev/null
+;;; cc-mode-tests.el --- Test suite for cc-mode. -*- lexical-binning: t -*-
+
+;; Copyright (C) 2016 Free Software Foundation, Inc.
+
+;; Author: Michal Nazarewicz <mina86@mina86.com>
+;; Keywords: internal
+;; Human-Keywords: internal
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(require 'ert)
+(require 'ert-x)
+(require 'cc-mode)
+
+(ert-deftest c-or-c++-mode ()
+ "Test c-or-c++-mode language detection."
+ (cl-letf* ((mode nil)
+ (do-test (lambda (content expected)
+ (delete-region (point-min) (point-max))
+ (insert content)
+ (setq mode nil)
+ (c-or-c++-mode)
+ (unless(eq expected mode)
+ (ert-fail
+ (format "expected %s but got %s when testing '%s'"
+ expected mode content)))))
+ ((symbol-function 'c-mode) (lambda () (setq mode 'c-mode)))
+ ((symbol-function 'c++-mode) (lambda () (setq mode 'c++-mode))))
+ (with-temp-buffer
+ (mapc (lambda (content)
+ (funcall do-test content 'c++-mode)
+ (funcall do-test (concat "// " content) 'c-mode)
+ (funcall do-test (concat " * " content) 'c-mode))
+ '("using \t namespace \t std;"
+ "using \t std::string;"
+ "namespace \t {"
+ "namespace \t foo \t {"
+ "class \t Blah_42 \t {"
+ "class \t Blah_42 \t \n"
+ "class \t _42_Blah:public Foo {"
+ "template \t < class T >"
+ "template< class T >"
+ "#include <string>"
+ "#include<iostream>"
+ "#include \t <map>"))
+
+ (mapc (lambda (content) (funcall do-test content 'c-mode))
+ '("struct \t Blah_42 \t {"
+ "struct template {"
+ "#include <string.h>")))))