Skip to content

Latest commit

 

History

History
221 lines (158 loc) · 8.13 KB

File metadata and controls

221 lines (158 loc) · 8.13 KB

How to install the MATLAB tree-sitter grammar

The MATLAB tree-sitter grammar requires Emacs 30 or later. This is typically installed in ~/.emacs.d/tree-sitter/libtree-sitter-matlab.so (.dll on Windows, .dylib on Mac).

Install the Emacs-MATLAB-Mode pre-built matlab tree-sitter grammar

After installing matlab-mode,

M-x matlab-ts-grammar-install

This will download the pre-build matlab tree-sitter grammar from https://github.com/mathworks/Emacs-MATLAB-Mode/matlab-ts-bin and save to the tree-sitter subdirectory of `user-emacs-directory’. For example, ~/.emacs.d/tree-sitter/libtree-sitter-matlab.so (.dylib on Mac, .dll on Windows). You can visit https://github.com/mathworks/Emacs-MATLAB-Mode/matlab-ts-bin to see what source was used to produce these binaries.

In addition, M-x matlab-ts-grammar-install will save the customizations required for matlab-ts-mode which leverages the matlab tree-sitter grammar.

Alternative: Manual Installation

Below we use the location ~/emacs-projects. You can change this to another location if desired.

As described below, installation consists of two steps

  1. Install the MATLAB tree-sitter grammar
  2. Setup Emacs to use the tree-sitter grammar

For Emacs 30, you need to build the MATLAB tree-sitter grammar for ABI 14 because Emacs 30 uses version 14 of the tree-sitter application binary interface and as of Sep-2025, the latest tree-sitter ABI is 15. There are different methods for installing this.

Install MATLAB tree-sitter grammar for Emacs 30

Method 1 - Install from abi/14 branch

Oct-29-2025 matlab-ts-mode is known to work with matlab tree-sitter v1.2.3 branch abi/14 at 9a4e65d.

  • Unix

    Install the C compiler if not installed. When installing from source, you need to use the correct compiler.

    Debian Emacs was built using gcc and you can get gcc via:

    sudo apt install gcc
    gcc --version
     # gcc (Debian 12.2.0-14+deb12u1) 12.2.0
        

    Next, ask Emacs to build the grammar using the ABI 14 branch

    emacs
    M-x treesit-install-language-grammar
    Language: matlab
    There is no recipe for matlab, do you want to build it interactively? (y or n) y
    Enter the URL of the Git repository of the language grammar: https://github.com/acristoffers/tree-sitter-matlab
    Enter the tag or branch (default: default branch): abi/14
    Enter the subdirectory in which the parser.c file resides (default: "src"):
    Enter the C compiler to use (default: auto-detect):
    Enter the C++ compiler to use (default: auto-detect):
    Install to (default: ~/.emacs.d/tree-sitter):
        
  • Windows

    Here we assume you’ve set HOME=C:\Users\YourUserName (corresponding to ~ within filenames in Emacs).

    1. Create %HOME%\emacs-projects\tree-sitter-matlab on the abi/14 branch:
      cd %HOME%\emacs-projects
      git clone https://github.com/acristoffers/tree-sitter-matlab.git
      cd tree-sitter-matlab
      git checkout abi/14
              
    2. Install Visual Studio. The community version should work. Select “Desktop development with C++”.
    3. Build

      We run vcvars64.bat to put the compiler, cl.exe on the path. Adjust the location to vcvars64.bat as needed per your installation.

      mkdir %HOME%\.emacs.d\tree-sitter
      cd %HOME%\emacs-projects\tree-sitter-matlab
      "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
      cl /LD /I src\tree_sitter src\parser.c src\scanner.c /link /out:%HOME%\.emacs.d\tree-sitter\libtree-sitter-matlab.dll
              

Method 2 - Generate the grammar for ABI 14 and install

  1. Install C/C++ compiler if not installed. See Method 2 for tips on installing the C/C++ compiler.
  2. Install JavaScript Node.js and npm
    • Debian
      sudo apt update
      sudo apt install nodejs npm
      node --version
      # v18.19.0
              
    • Windows

      See https://nodejs.org/ to install Node.js and npm.

  3. Download and extract the tree-sitter CLI, https://github.com/tree-sitter/tree-sitter/releases into some location. For example, place it in:
    ~/emacs-projects/tree-sitter-cli/            (Linux)
    %HOME%\emacs-projects\tree-sitter-cli\       (Windows)
        

    Here we assume you’ve set HOME=C:\Users\YourUserName (corresponding to ~ within filenames in Emacs).

  4. Generate the grammar for ABI 14:
    cd ~/emacs-projects           # Unix
    cd %HOME%\emacs-projects      # Windows
    git clone https://github.com/acristoffers/tree-sitter-matlab.git
    
    cd tree-sitter-matlab
    ~/emacs-projects/tree-sitter-cli/tree-sitter generate --abi 14            # Unix
    %HOME%/emacs-projects/tree-sitter-cli/tree-sitter generate --abi 14       # Windows
        
  5. Build
    • Unix

      Ask Emacs to build it:

      emacs
      M-x treesit-install-language-grammar
      Language: matlab
      There is no recipe for matlab, do you want to build it interactively? (y or n) y
      Enter the URL of the Git repository of the language grammar: ~/emacs-projects/tree-sitter-matlab
      Enter the tag or branch (default: default branch):
      Enter the subdirectory in which the parser.c file resides (default: "src"):
      Enter the C compiler to use (default: auto-detect):
      Enter the C++ compiler to use (default: auto-detect):
      Install to (default: ~/.emacs.d/tree-sitter):
              

      Alternatively, you can manually build it. For example, on Linux

      cd tree-sitter-matlab/src
      cc -fPIC -O -c -I. parser.c
      cc -fPIC -O -c -I. scanner.c
      cc -fPIC -shared parser.o scanner.o -o ~/.emacs.d/tree-sitter/libtree-sitter-matlab.so
              
    • Windows

      Follow the same Windows build step as in Method 1.

Setup Emacs to use the MATLAB tree-sitter grammar

Tell Emacs to use matlab-ts-mode for MATLAB files by adding the following to your user-init-file which is typically ~/.emacs, or add it to your site-run-file

  1. Map matlab-mode to matlab-ts-mode
    M-x customize-variable RET major-mode-remap-alist RET
        

    and INS (insert):

    Key: matlab-mode
       Value: matlab-ts-mode
        
  2. Tell org-mode that #+begin_src matlab ... #end_src blocks should use matlab-ts-mode
    M-x customize-variable RET org-src-lang-modes RET
        

    and map matlab to matlab-ts:

    Language name: matlab
    Major mode: matlab-ts