Search code examples
emacsmacrosrustspacemacsrust-macros

Does emacs/spacemacs support nested C++ mode embedded in Rust Macro?


Reference: rust-cpp

Can emacs/spacemacs support nested mode in major mode? I'm used to vim and new to emacs/spacemacs.


Solution

  • General information

    You have access to a wiki listing some solutions to run several major modes at once:

    In your case, you will need to run 2 major modes in order to recognize C++ and Rust in the same buffer:

    A practical example with mmm-mode

    I suppose your Rust environment is already configured in your Emacs. The following will add c++-mode while the Rust major mode is running. In your Emacs configuration file, add the following snippet:

    (require 'mmm-mode)
    (setq mmm-global-mode 'maybe)
    
    (mmm-add-classes
     '((rust-cpp ; Name of the mmm class
        :submode c++-mode ; Additional major mode, here it is C++
        :front "^cpp! {[\n\r]+" ; Start tag for c++-mode
        :back "^}$"))) ; Stop tag for c++-mode
    
    (mmm-add-mode-ext-class 'rust-mode nil 'rust-cpp)
    

    In your Rust code, c++-mode will be activated when the following pattern is present:

    cpp! {
        // your C++ code...
    }
    

    I'll let you fine-tune the regular expression since I do not know the rules of rust-cpp when mixing Rust and C++.