mathworks / Emacs-MATLAB-Mode

Edit, lint, debug, and run MATLAB in Emacs
GNU General Public License v3.0
7 stars 0 forks source link

+startup: showall

+options: toc:nil

Copyright 2024 The MathWorks, Inc.

[[https://mathworks.com][MathWorks]] MATLAB® and [[https://www.gnu.org/software/emacs/][GNU Emacs]] integration:

  1. matlab-mode for editing *.m files.

    • Edit MATLAB code with syntax highlighting and smart indentation.
    • Lint MATLAB code with fix-it's using the MATLAB Code Analyzer.
  2. ~M-x matlab-shell~ for running and debugging MATLAB within Emacs (Unix-only).

    • matlab-shell uses company-mode for completions.
  3. MATLAB and [[http://orgmode.org]] for creation of scientific papers, thesis's, etc.

    Code plus text is combined together in .org files. The code in, code blocks, can be executed and the results placed in the .org file. This is know as literate programming. Org mode supports combining multiple *.org files into one final document, thus enabling larger scientific papers, thesis's, etc.

    See [[file:examples/matlab-and-org-mode]] to get started. In this directory, you will see an example [[file:examples/matlab-and-org-mode/matlab-and-org-mode.pdf][PDF]] generated from an [[file:examples/matlab-and-org-mode/matlab-and-org-mode.org][org]] file.

  4. tlc-mode for editing *.tlc files. The Target Language Compiler (TLC) part of Simulink® Coder™.

** Install via MELPA

Installing via [[https://melpa.org][MELPA]] is recommended because MELPA will contain the latest validated release.

Add to your =~/.emacs=:

+begin_src emacs-lisp

(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/") t)

+end_src

then run

: M-x RET package-list-packages RET

** Install from this repository

Build:

+begin_src bash

cd /path/to/Emacs-MATLAB-mode

Build lisp and run tests (requires MATLAB executable):

make

Alternatively, build lisp and run tests using a specific MATLAB executable:

make MATLAB_EXE=/path/to/matlab

If desired, you can separate the building of lisp and running tests using:

make lisp make tests make tests MATLAB_EXE=/path/to/matlab # if using a specific MATLAB executable

+end_src

Add the following to your =~/.emacs= file:

+begin_src emacs-lisp

(add-to-list 'load-path "/path/to/Emacs-MATLAB-mode") (load-library "matlab-autoload")

+end_src

Emacs MATLAB-mode is designed to be compatible with the last six years of MathWorks products and may support even older versions of MathWorks products.

GPL3, https://www.gnu.org/licenses/gpl-3.0.en.html (see [[file:License.txt][License.txt]])

[[https://www.mathworks.com/matlabcentral][MATLAB Central]]

** How do I customize matlab-mode?

You can configure matlab-emacs using the "matlab" or "matlab-shell" customization groups:

: Emacs -> Options -> Customize Emacs -> Specific Group

** How do I customize "edit file.m" behavior?

By default when you run

+begin_example

M-x matlab-shell

edit file.m

+end_example

file.m will open in emacs using 'emacsclient -n'. matlab-shell achieve this behavior by instructing MATLAB to use 'emacsclient -n' as the external text editor.

You can customize this by setting `matlab-shell-emacsclient-command' in the matlab-shell customization group. You can change this command to what's appropriate. If you set it to the empty string, 'edit file.m' will use the default MATLAB editor setting.

The default MATLAB editor setting is controlled in the MATLAB preferences, (e.g. R2018a Home tab, Environment section, Preferences) where you can select which editor you want to edit a text file. MATLAB Editor or an external text editor. If you always want to use Emacs as your matlab editor even when running MATLAB outside of emacs, select Text editor and set it to the appropriate 'emacsclient -n' command.

** The code-sections are not highlighted properly. What do I do?

There can be several reasons for this. One reason would be if you are using syntax highlighting from a different package (such as tree-sitter) which is over-riding the font-lock provided by matlab-mode.

In this case, add the following hook to your config:

+begin_src emacs-lisp

(add-hook 'matlab-sections-mode-hook (lambda () (interactive) (font-lock-add-keywords nil `((,matlab-sections-section-break-regexp 1 'matlab-sections-section-break-face prepend))) (font-lock-flush)))

+end_src

Ensure that this is included after matlab-mode as well as your syntax highlighter are initialized in your config.

matlab-mode has a history dating back many years. Older contributions can be found in [[https://sourceforge.net/projects/matlab-emacs/][https://sourceforge.net/projects/matlab-emacs/]].

LocalWords: showall nodesktop melpa emacsclient