Function: c++-ts-mode

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

Signature

(c++-ts-mode)

Documentation

Major mode for editing C++, powered by tree-sitter.

This mode is independent from the classic cc-mode.el based c++-mode, so configuration variables of that mode, like c-basic-offset, don't affect this mode.

To use tree-sitter C/C++ modes by default, evaluate

    (add-to-list 'major-mode-remap-alist '(c-mode . c-ts-mode))
    (add-to-list 'major-mode-remap-alist '(c++-mode . c++-ts-mode))
    (add-to-list 'major-mode-remap-alist
                 '(c-or-c++-mode . c-or-c++-ts-mode))

in your init files.

Since this mode uses a parser, unbalanced brackets might cause some breakage in indentation/fontification. Therefore, it's recommended to enable electric-pair-mode(var)/electric-pair-mode(fun) with this mode.

In addition to any hooks its parent mode c-ts-base-mode might have run, this mode runs the hook c++-ts-mode-hook, as the final or penultimate step during initialization.

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
(define-derived-mode c++-ts-mode c-ts-base-mode "C++"
  "Major mode for editing C++, powered by tree-sitter.

This mode is independent from the classic cc-mode.el based
`c++-mode', so configuration variables of that mode, like
`c-basic-offset', don't affect this mode.

To use tree-sitter C/C++ modes by default, evaluate

    (add-to-list \\='major-mode-remap-alist \\='(c-mode . c-ts-mode))
    (add-to-list \\='major-mode-remap-alist \\='(c++-mode . c++-ts-mode))
    (add-to-list \\='major-mode-remap-alist
                 \\='(c-or-c++-mode . c-or-c++-ts-mode))

in your init files.

Since this mode uses a parser, unbalanced brackets might cause
some breakage in indentation/fontification.  Therefore, it's
recommended to enable `electric-pair-mode' with this mode."
  :group 'c++
  :after-hook (c-ts-mode-set-modeline)

  (when (treesit-ready-p 'cpp)

    (treesit-parser-create 'cpp)

    ;; Syntax.
    (setq-local syntax-propertize-function
                #'c-ts-mode--syntax-propertize)

    ;; Indent.
    (setq-local treesit-simple-indent-rules
                (c-ts-mode--get-indent-style 'cpp))

    ;; Font-lock.
    (setq-local treesit-font-lock-settings (c-ts-mode--font-lock-settings 'cpp))
    (treesit-major-mode-setup)

    (when c-ts-mode-emacs-sources-support
      (setq-local add-log-current-defun-function
                  #'c-ts-mode--emacs-current-defun-name))))