Function: c-or-c++-ts-mode

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

Signature

(c-or-c++-ts-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-ts-mode or c++-ts-mode.

Probably introduced at or before Emacs version 29.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/c-ts-mode.el.gz
;;;###autoload
(defun c-or-c++-ts-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-ts-mode' or `c++-ts-mode'."
  (interactive)
  (if (save-excursion
        (save-restriction
          (save-match-data ; Why `save-match-data'?
            (widen)
            (goto-char (point-min))
            (re-search-forward c-ts-mode--c-or-c++-regexp nil t))))
      (c++-ts-mode)
    (c-ts-mode)))