{"id":332,"date":"2009-09-14T13:11:05","date_gmt":"2009-09-14T13:11:05","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2009\/09\/14\/matlab-emacs-integration-is-back\/"},"modified":"2024-09-03T15:13:23","modified_gmt":"2024-09-03T19:13:23","slug":"matlab-emacs-integration-is-back","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2009\/09\/14\/matlab-emacs-integration-is-back\/","title":{"rendered":"MATLAB-Emacs integration is back"},"content":{"rendered":"<i>\r\nI'd like to welcome guest blogger Eric Ludlam from the MATLAB Graphics team.\r\n<\/i>\r\n<h3>Little Things<\/h3>\r\nBack in April of this year, Mike wrote a Desktop Blog article stating that <a href=\"https:\/\/blogs.mathworks.com\/community\/2009\/04\/20\/its-the-little-things-that-count\/\"> it's the little things that count<\/a>. In the comments to this article several people who were long time users of EmacsLink wrote in after discovering that the R2009a Editor interface stopped supporting EmacsLink.\r\n\r\nAs an author of a lot of Emacs code, and the current maintainer of the <a href=\"http:\/\/matlab-emacs.sourceforge.net\/\">matlab-emacs<\/a> project on Source Forge, and the guy who helped bring the original Emacs Link into existence, I was both glad to know that so many folks had found it useful, and sad that they had never joined the <a href=\"http:\/\/lists.sourceforge.net\/lists\/listinfo\/matlab-emacs-discuss\">matlab-emacs mailing list<\/a> over on Source Forge, where the many questions could be easily answered.\r\n<h3>What is the <em>matlab-emacs<\/em> project?<\/h3>\r\nThe <em>matlab-emacs<\/em> project is a MATLAB mode for Emacs that consists of Emacs Lisp code that implements a <tt>major-mode<\/tt> for Emacs that assists in the editing of MATLAB scripts.\r\n\r\nIt also allows Emacs to be used in place of the MATLAB Editor for editing your MATLAB source code, debugging MATLAB code, and syntax\/semantic checking of your MATLAB code with mlint.\r\n\r\nThe MATLAB mode for Emacs does NOT include \"EmacsLink\", which was a tool that more tightly integrated MATLAB and Emacs for purposes of debugging M code. \"EmacsLink\" only included the communication interface between MATLAB and Emacs. The editing mode has always been a part of the matlab-emacs project.\r\n\r\n<tt>matlab-mode<\/tt>, which forms the root of the matlab-emacs project was first written by Matt Wette in 1991.\r\n<h3>Where to get <em>matlab-emacs<\/em>.<\/h3>\r\nThe <a href=\"https:\/\/sourceforge.net\/p\/matlab-emacs\/src\/ci\/master\/tree\/\">matlab-emacs<\/a> project is hosted on <a href=\"http:\/\/www.sourceforge.net\/\">SourceForge<\/a> where the Emacs Lisp code can be downloaded from <a href=\"http:\/\/sourceforge.net\/scm\/?type=cvs&amp;group_id=154105\">git<\/a>.\r\n\r\nDetailed download and installation instructions are available <a href=\"https:\/\/sourceforge.net\/p\/matlab-emacs\/src\/ci\/master\/tree\/\">here<\/a>.\r\n\r\nBe sure to join the <a href=\"http:\/\/lists.sourceforge.net\/lists\/listinfo\/matlab-emacs-discuss\">mailing list<\/a> to get help on setting up and using the matlab-emacs project.\r\n\r\nOnce you have downloaded the code using your preferred method, read the INSTALL file for full details.\r\n\r\nThe matlab-emacs project has been expanded by it's users to include support for several features in <a href=\"http:\/\/cedet.sf.net\">CEDET<\/a>. CEDET is an Emacs package that supplies an object system needed by the mlint support, and a complex completion\/navigation package.\r\n<h3>Writing MATLAB code in Emacs<\/h3>\r\nWriting MATLAB code in Emacs is much like writing any other code in Emacs. The matlab-mode for Emacs has several features users have come to expect from Emacs, plus several features that make editing MATLAB code even better.\r\n\r\nWhat I'm sure many users think of as a single \"EmacsLink\", as it shipped with MATLAB, had two parts. One part was communication between MATLAB and Emacs for debugging. The other was the MATLAB script editing mode.\r\n\r\nNothing has changed with the editing mode, except to make these tools even better, such as supporting the latest indentation styles, cell-mode, and programming constructs, such as the MATLAB Object System. As such, your editing experience in Emacs has only gotten better.\r\n\r\nSetting up <tt>matlab-mode<\/tt> for Emacs is pretty easy. Here is an example simple setup:\r\n<blockquote>\r\n<pre>    (add-to-list 'load-path \"~\/path\/to\/matlab_dot_el\")\r\n    (load-library \"matlab-load\")\r\n    ;; Enable CEDET feature support for MATLAB code. (Optional)\r\n    (matlab-cedet-setup)\r\n<\/pre>\r\n<\/blockquote>\r\n<div align=\"center\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/eric_ludlam_matlab_and_emacs\/mode-example.jpg\" border=\"0\" \/><\/div>\r\nIn this screenshot done with Emacs 23 on Linux, you can see some syntax highlighting, and mlint support for nested functions, and bold-face cross-function variables. The blue line over the nested function, and TAGS menu is part of the CEDET support that includes function browsing and completion. It also shows if\/end block matching, and CEDET summary mode showing function details.\r\n\r\nCurrenlty, the matlab-mode for Emacs supports these features:\r\n<ol>\r\n \t<li><b>Syntax Highlighting.<\/b> There are three coloring levels supported.<\/li>\r\n \t<li><b>Smart indentation.<\/b> The <tt>TAB<\/tt> key, and <tt>RETURN<\/tt> key will both indent your code lines. Indent region <tt>(C-M-\\)<\/tt> for larger areas.<\/li>\r\n \t<li><b>Code auto-fill &amp; Paragraph fill.<\/b> <tt>M-q<\/tt> does an excellent job on large comments. Autofill is syntax aware, and can find nice places to put line breaks in code.<\/li>\r\n \t<li><b>Syntax aware block movement. <\/b>Forward\/Backward sexp (<tt>C-M-f,C-M-b<\/tt>), and Begin\/End of function (<tt>C-M-a,C-M-e<\/tt>) correctly moves over begin\/end syntax.<\/li>\r\n \t<li><b>Paren style matching for blocks.<\/b> Highlight a matching <tt>end<\/tt> when the cursor is on an <tt>if<\/tt>, and many other matching constructs.<\/li>\r\n \t<li><b>mlint support.<\/b> Highlight mlint warings in your code, and a convenient conext menu for tagging and navigating through errors. Some errors can be automatically fixed by Emacs.<\/li>\r\n \t<li><b>Highlight nested variables.<\/b> Using mlint, nested variables are highlighted, assiting when working with nested functions.<\/li>\r\n \t<li><b>Completion engine.<\/b> Using tools in CEDET, Emacs will parse your M files and provide pretty good completion and code decoration.<\/li>\r\n<\/ol>\r\n<h3>How to use Emacs instead of the MATLAB Editor<\/h3>\r\nOK, now to answer many of the questions that have shown up in the comments to the Desktop blog. The most common question seemed to be \"Why did you take EmacsLink away?\", and Mike and Ken have done a fine job answering that. The real question should be \"What should I do now?\"\r\n\r\nThankfully, I have two answers, and you can choose the one that best suites your development style.\r\n<h4>Using Emacs as an external editor.<\/h4>\r\nYou can use Emacs as an external editor now in a way that is similar to what EmacsLink provided.\r\n\r\nOne of the questions that showed up on the blog was was regarding using the preference panel to set Emacs as the default editor, and the complaint was:\r\n<blockquote>\r\n<ol>\r\n \t<li>This creates a new emacs process every time I \"edit\" or \"open\" a different function. What should happen is that only the first \"edit\" should start a new session, and subsequent \"edit\"s should open the file in the existing session. I can pretty quickly have about 30 extra windows open on my desktop.<\/li>\r\n \t<li>Upon an error, clicking on the error link in the matlab command window opens the official editor, not emacs, even though I specified emacs in the preferences.<\/li>\r\n<\/ol>\r\n<\/blockquote>\r\nThe answer to these problems is to use an old Emacs tool called <tt>emacsclient<\/tt>. Emacslient lets you tell an already running Emacs to load a file in for editing. To enable emacsclient in your emacs, add this line to your <tt>~\/.emacs<\/tt> file:\r\n<pre>    (server-start)\r\n<\/pre>\r\n&nbsp;\r\n\r\nFrom the unix command prompt, you can now call emacsclient like this:\r\n<pre>    emacsclient -n myfile.m\r\n<\/pre>\r\n&nbsp;\r\n\r\nand <tt>myfile.m<\/tt> will be loaded into Emacs for editing.\r\n\r\nAfter researching this problem with the Ken and Mike, I have provided some simple tools for getting this setup. The matlab-emacs project now has a toolbox directory. If you were to install matlab-emacs in the directory <tt>~\/lisp\/matlab-emacs<\/tt>, you can now place code like this in your startup.m file:\r\n<pre>    addpath('~\/lisp\/matlab-emacs\/toolbox','-begin');\r\n    rehash;\r\n    emacsinit;\r\n<\/pre>\r\n&nbsp;\r\n\r\nThis will add the matlab-emacs project toolbox to your path, and the emacsinit script will configure MATLAB to tell an already running Emacs what to do when you type <tt>edit<\/tt>, or click on a link in the command window.\r\n<div align=\"center\"><a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/eric_ludlam_matlab_and_emacs\/matlab-w32.jpg\">\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/eric_ludlam_matlab_and_emacs\/matlab-w32.jpg\" width=\"75%\" \/>\r\n<\/a><\/div>\r\nThis screenshot shows a hand-setup where the location of <tt>emacsclient<\/tt> is specified as an argument to <tt>emacsinit<\/tt>. This is important on Windows because it may not be on your path. Now, when stopped in the debugger, you can click on the blue line number to jump to that location in some file. The file being debugged is a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/7270-snake-toy\"> rubik snake<\/a> demo I wrote a few years back.\r\n\r\nThis is a great step forward from just sticking the word \"emacs\" in the provided text field, but does not answer this question:\r\n<blockquote>\r\n<ol>\r\n \t<li value=\"3\">And, of course, none of the other interactivity features are working .. no evaling code from emacs, no debugging, etc.<\/li>\r\n<\/ol>\r\n<\/blockquote>\r\nFor this, you will need to use the Emacs command <tt>matlab-shell<\/tt>.\r\n<h4>Using <em>matlab-shell<\/em><\/h4>\r\nThe Emacs command <tt>M-x matlab-shell RET<\/tt>, on unix, will start MATLAB as a subprocess under Emacs. The command prompt will be available in an Emacs buffer the same way the unix prompt shows up if you type <tt>M-x shell<\/tt>.\r\n\r\nIf you use <tt>matlab-shell<\/tt> it will automatically configure your MATLAB as described in the previous section, allowing \"edit\" command to open files in a currently running Emacs. In addtion, Emacs will be able to send text from M files to the running MATLAB for execution.\r\n\r\nOnce the <tt>matlab-shell<\/tt> has been started, you can select <tt>\"Run Region\"<\/tt> or <tt>\"Run Cell\"<\/tt> from the menu while editing <tt>.m<\/tt> files. This will send the text from your source file to <tt>matlab-shell<\/tt> for execution.\r\n\r\nIf you just want to move the cursor to the shell, you can do <tt>M-x matlab-shell RET<\/tt> a second time to bring an already running shell forward.\r\n<div align=\"center\"><a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/eric_ludlam_matlab_and_emacs\/aquamacs-matlab.jpg\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/eric_ludlam_matlab_and_emacs\/aquamacs-matlab.jpg\" width=\"75%\" \/><\/a><\/div>\r\nIn the above image, you can see the latest Aquamacs running <tt>matlab-shell<\/tt>. Selecting \"Go to Last Error\" or clicking the links moves the cursor to the typo that was introduced in my catapult modeling program.\r\n\r\nTo debug, you can use the toolbar to set breakpoints, or use the the db commands. For example:\r\n<pre>    dbstop in ls\r\n    ls\r\n    dbstep\r\n    dbstep\r\n    dbquit\r\n<\/pre>\r\n&nbsp;\r\n\r\nThis will cause the <tt>ls.m<\/tt> file to popup in an Emacs buffer, with the debug arrow pointing at the correct line. Stepping through this file, or into new files will move the arrow to the correct place.\r\n<h4>For windows users<\/h4>\r\nMATLAB does not support a tty command window on Windows so you cannot use <tt>matlab-shell<\/tt>. You will still be able to use <tt>emacsclient<\/tt> however. To do so, you will need to modify the setup configuration like this:\r\n<pre>    addpath('~\/lisp\/matlab-emacs\/toolbox','-begin');\r\n    rehash;\r\n    emacsinit('c:\/applications\/Emacs\/lib-src\/emacsclient -n');\r\n<\/pre>\r\n&nbsp;\r\n\r\nwhere you would replace the argument to emacsinit with the real path to your emacsclient binary on your system. The <tt>-n<\/tt> argument specifies that emacsclient should not wait for the user to finish editing the file.\r\n\r\nWindows configuration of <tt>emacsclient<\/tt> is also not as easy as it is on Unix. You may need to read up on configuring your Emacsclient to work propertly before starting with the matlab-emacs configuration.\r\n<h3>Make matlab-emacs better<\/h3>\r\nThe <tt>matlab-emacs<\/tt> project has been around for a long time, and its many contributors have made it a powerful way to work with MATLAB. If you want to Emacs as you editor, join the <a href=\"http:\/\/lists.sourceforge.net\/lists\/listinfo\/matlab-emacs-discuss\">matlab-emacs mailing list<\/a>, and help make it better.\r\n<h3>Gratuitous Screenshot<\/h3>\r\nThe below screenshot includes <tt>matlab-mode<\/tt> with CEDET support enabled, matlab-shell running, editing, and debugging <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/764-sliceomatic\"> sliceomatic<\/a> with <a href=\"http:\/\/ecb.sf.net\">ECB, the Emacs Code browser<\/a> all running together.\r\n<div align=\"center\"><a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/eric_ludlam_matlab_and_emacs\/matlab-ecb.jpg\">\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/eric_ludlam_matlab_and_emacs\/matlab-ecb.jpg\" width=\"75%\" \/><\/a><\/div>\r\n<h3>Vim Users<\/h3>\r\nFor the <tt>vim<\/tt> users who also posted comments, the techniques used here for Emacs will also work with vim. Modifying the two MATLAB script files to call to vim instead ought to make it easy to start using vim, or just about any other editor, with MATLAB.\r\n\r\n<i>-by Eric Ludlam, The MathWorks<\/i>\r\n<h3><\/h3>\r\n<h3>NOTE:<\/h3>\r\nEmacs matlab-mode is now located at <a href=\"https:\/\/github.com\/MathWorks\/Emacs-MATLAB-Mode\">https:\/\/github.com\/MathWorks\/Emacs-MATLAB-Mode<\/a>","protected":false},"excerpt":{"rendered":"<p>\r\nI'd like to welcome guest blogger Eric Ludlam from the MATLAB Graphics team.\r\n\r\nLittle Things\r\nBack in April of this year, Mike wrote a Desktop Blog article stating that  it's the little things... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2009\/09\/14\/matlab-emacs-integration-is-back\/\">read more >><\/a><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[33,9,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/332"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=332"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/332\/revisions"}],"predecessor-version":[{"id":9952,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/332\/revisions\/9952"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}