Function: c-or-c++-mode

c-or-c++-mode is an autoloaded, interactive and byte-compiled function defined in cc-mode.el.gz.

Signature

(c-or-c++-mode)

Documentation

Analyze 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.

Probably introduced at or before Emacs version 26.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/cc-mode.el.gz
;;;###autoload
(defun c-or-c++-mode ()
  "Analyze 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'."
  (interactive)
  (let ((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)))
    (funcall (if (fboundp 'major-mode-remap)
		 (major-mode-remap mode)
	       mode))))