The development of Octave code can greatly be facilitated using Emacs with Octave mode, a major mode for editing Octave files which can e.g. automatically indent the code, do some of the typing (with Abbrev mode) and show keywords, comments, strings, etc. in different faces (with Font-lock mode on devices that support it).
It is also possible to run Octave from within Emacs, either by directly entering commands at the prompt in a buffer in Inferior Octave mode, or by interacting with Octave from within a file with Octave code. This is useful in particular for debugging Octave code.
Finally, you can convince Octave to use the Emacs info reader for help -i.
All functionality is provided by the Emacs Lisp package `octave'. This chapter describes how to set up and use this package.
Please contact <Kurt.Hornik@ci.tuwien.ac.at> if you have any questions or suggestions on using Emacs with Octave.
The Emacs package `octave' consists of the three files `octave-mod.el', `octave-inf.el', and `octave-hlp.el'. These files, or better yet their byte-compiled versions, should be somewhere in your Emacs load-path.
If you have GNU Emacs with a version number at least as high as 19.35, you are all set up, because the package is respectively will be part of GNU Emacs as of version 19.35.
Otherwise, copy the three files from the `emacs' subdirectory of the Octave distribution to a place where Emacs can find them (this depends on how your Emacs was installed). Byte-compile them for speed if you want.
If you are lucky, your sysadmins have already arranged everything so that Emacs automatically goes into Octave mode whenever you visit an Octave code file as characterized by its extension `.m'. If not, proceed as follows.
(autoload 'octave-mode "octave-mod" nil t) (setq auto-mode-alist (cons '(\"\\\\.m$\" . octave-mode) auto-mode-alist))
(add-hook 'octave-mode-hook (lambda () (abbrev-mode 1) (auto-fill-mode 1) (if (eq window-system 'x) (font-lock-mode 1))))See the Emacs manual for more information about how to customize Font-lock mode.
In Octave mode, the following special Emacs commands can be used in addition to the standard Emacs commands.
octave-reindent-then-newline-and-indent). An abbrev before point is expanded if
nil, typing a `;' automatically reindents the current line, inserts a newline and indents the new line.
octave-abbrev-start). If Abbrev mode is turned on, typing `C-h or `? lists all abbrevs. Any other key combination is executed normally. Note that all Octave abbrevs start with a grave accent.
octave-beginning-of-defun). With prefix argument N, do it that many times if N is positive; otherwise, move forward to the N-th following beginning of a function.
octave-end-of-defun). With prefix argument N, do it that many times if N is positive; otherwise, move back to the N-th preceding end of a function.
octave-indent-for-comment). Point is left after the start of the comment which is properly aligned.
octave-comment-start) at the beginning of every line in the region (
octave-comment-region). With just C-u prefix argument, uncomment each line in the region. A numeric prefix argument N means use N comment characters.
octave-previous-code-line). With numeric prefix argument N, move that many code lines backward (forward if N is negative).
octave-next-code-line). With numeric prefix argument N, move that many code lines forward (backward if N is negative).
octave-beginning-of-line). If point is in an empty or comment line, simply go to its beginning; otherwise, move backwards to the beginning of the first code line which is not inside a continuation statement, i.e., which does not follow a code line ending in `...' or `\', or is inside an open parenthesis list.
octave-end-of-line). If point is in a code line, move forward to the end of the first Octave code line which does not end in `...' or `\' or is inside an open parenthesis list. Otherwise, simply go to the end of the current line.
octave-forward-block). With numeric prefix argument N, move forward across n such blocks (backward if N is negative).
octave-backward-block). With numeric prefix argument N, move backward across N such blocks (forward if N is negative).
octave-down-block). With numeric prefix argument, do it that many times; a negative argument means move backward, but still go down one level.
octave-backward-up-block). With numeric prefix argument, do it that many times; a negative argument means move forward, but still to a less deep spot.
octave-mark-block). The block marked is the one that contains point or follows point.
octave-close-block). An error is signaled if no block to close is found.
octave-help). If used interactively, the entry is prompted for with completion. If multiple matches are found, one can cycle through them using the standard `,' (
Info-index-next) command of the Info reader. The variable
octave-help-filesis a list of files to search through and defaults to
'("octave"). If there is also an Octave Local Guide with corresponding info file, say, `octave-LG', you can have
octave-helpsearch both files by
(setq octave-help-files '("octave" "octave-LG"))in one of your Emacs startup files.
A common problem is that the RET key does not indent the line to where the new text should go after inserting the newline. This is because the standard Emacs convention is that RET (aka C-m) just adds a newline, whereas LFD (aka C-j) adds a newline and indents it. This is particularly inconvenient for users with keyboards which do not have a special LFD key at all; in such cases, it is typically more convenient to use RET as the LFD key (rather than typing C-j).
You can make RET do this by adding
(define-key octave-mode-map "\C-m" 'octave-reindent-then-newline-and-indent)
to one of your Emacs startup files. Another, more generally applicable solution is
(defun RET-behaves-as-LFD () (let ((x (key-binding "\C-j"))) (local-set-key "\C-m" x))) (add-hook 'octave-mode-hook 'return-behaves-as-LFD)
(this works for all modes by adding to the startup hooks, without having
to know the particular binding of RET in that mode!). Similar
considerations apply for using M-RET as M-LFD. As Barry
A. Warsaw <email@example.com> says in the documentation for his
cc-mode, "This is a very common question.
:-) If you want
this to be the default behavior, don't lobby me, lobby RMS!"
The following variables can be used to customize Octave mode.
nilmeans auto-insert a newline and indent after semicolons are typed. The default value is
nilmeans show matching begin of block when inserting a space, newline or `;' after an else or end keyword. Default is
t. This is an extremely useful feature for automatically verifying that the keywords match--if they don't, an error message is displayed.
t(default), a startup message is displayed when Octave mode is called.
If Font Lock mode is enabled, Octave mode will display
There is also rudimentary support for Imenu (currently, function names can be indexed).
Customization of Octave mode can be performed by modification of the
octave-mode-hook. It the value of this variable is
nil, turning on Octave mode calls its value.
If you discover a problem with Octave mode, you can conveniently send a
bug report using C-c C-b (
automatically sets up a mail buffer with version information already
added. You just need to add a description of the problem, including a
reproducible test case and send the message.
The package `octave' provides commands for running an inferior Octave process in a special Emacs buffer. Use
to directly start an inferior Octave process. If Emacs does not know about this command, add the line
(autoload 'run-octave "octave-inf" nil t)
to your `.emacs' file.
This will start Octave in a special buffer the name of which is
specified by the variable
inferior-octave-buffer and defaults to
"*Inferior Octave*". From within this buffer, you can
interact with the inferior Octave process `as usual', i.e., by entering
Octave commands at the prompt. The buffer is in Inferior Octave mode,
which is derived from the standard Comint mode, a major mode for
interacting with an inferior interpreter. See the documentation for
comint-mode for more details, and use C-h b to find out
about available special keybindings.
You can also communicate with an inferior Octave process from within files with Octave code (i.e., buffers in Octave mode), using the following commands.
octave-send-line). With positive prefix argument N, send that many lines. If
nil, go to the next unsent code line.
The effect of the commands which send code to the Octave process can be customized by the following variables.
nilmeans echo input sent to the inferior Octave process. Default is
nilmeans display the buffer running the Octave process after sending a command (but without selecting it). Default is
If you send code and there is no inferior Octave process yet, it will be started automatically.
The startup of the inferior Octave process is highly customizable.
inferior-octave-startup-args can be used for
specifying command lines arguments to be passed to Octave on startup
as a list of strings. For example, to suppress the startup message and
use `traditional' mode, set this to
You can also specify a startup file of Octave commands to be loaded on
startup; note that these commands will not produce any visible output
in the process buffer. Which file to use is controlled by the variable
inferior-octave-startup-file. If this is
nil, the file
`~/.emacs-octave' is used if it exists.
inferior-octave-mode-hook is run after starting the
process and putting its buffer into Inferior Octave mode. Hence, if you
like the up and down arrow keys to behave in the interaction buffer as
in the shell, and you want this buffer to use nice colors, add
(add-hook 'inferior-octave-mode-hook (lambda () (turn-on-font-lock) (define-key inferior-octave-mode-map [up] 'comint-previous-input) (define-key inferior-octave-mode-map [down] 'comint-next-input)))
to your `.emacs' file. You could also swap the roles of C-a
C-c C-a (
Note: If you set your Octave prompts to something different from the defaults, make sure that
inferior-octave-promptmatches them. Otherwise, nothing will work, because Emacs will have no idea when Octave is waiting for input, or done sending output.
You can also set up the Emacs Info reader for dealing with the results of Octave's `help -i'. For this, the package `gnuserv' needs to be installed, which unfortunately still does not come with GNU Emacs (it does with XEmacs). It can be retrieved from any GNU Emacs Lisp Code Directory archive, e.g. `ftp://ftp.cis.ohio-state.edu/pub/gnu/emacs/elisp-archive', in the `packages' subdirectory. The alpha version of an enhanced version of gnuserv is available at `ftp://ftp.wellfleet.com/netman/psmith/emacs/gnuserv-2.1alpha.tar.gz'.
If `gnuserv' is installed, add the lines
(autoload 'octave-help "octave-hlp" nil t) (require 'gnuserv) (gnuserv-start)
to your `.emacs' file.
You can use either `plain' Emacs Info or the function
as your Octave info reader (for `help -i'). In the former case,
set the Octave variable
The latter is perhaps more attractive because it allows to look up keys
in the indices of several info files related to Octave (provided
that the Emacs variable
octave-help-files is set correctly). In
this case, set
If you use Octave from within Emacs, these settings are best done in the
`~/.emacs-octave' startup file (or the file pointed to by the Emacs
Go to the first, previous, next, last section, table of contents.