3294
|
1 @c Copyright (C) 1996, 1997 John W. Eaton |
5428
|
2 @c Written by Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> on 1996/05/17. |
3294
|
3 @c Last updated by KH on 1997/07/31. |
|
4 @c This is part of the Octave manual. |
|
5 @c For copying conditions, see the file gpl.texi. |
|
6 |
4167
|
7 @node Emacs |
3294
|
8 @appendix Emacs Octave Support |
|
9 |
|
10 The development of Octave code can greatly be facilitated using Emacs |
|
11 with Octave mode, a major mode for editing Octave files which can e.g.@: |
|
12 automatically indent the code, do some of the typing (with Abbrev mode) |
|
13 and show keywords, comments, strings, etc.@: in different faces (with |
|
14 Font-lock mode on devices that support it). |
|
15 |
|
16 It is also possible to run Octave from within Emacs, either by directly |
|
17 entering commands at the prompt in a buffer in Inferior Octave mode, or |
|
18 by interacting with Octave from within a file with Octave code. This is |
|
19 useful in particular for debugging Octave code. |
|
20 |
|
21 Finally, you can convince Octave to use the Emacs info reader for |
|
22 @kbd{help -i}. |
|
23 |
|
24 All functionality is provided by the Emacs Lisp package EOS (for ``Emacs |
|
25 Octave Support''). This chapter describes how to set up and use this |
|
26 package. |
|
27 |
5428
|
28 Please contact <Kurt.Hornik@@wu-wien.ac.at> if you have any questions |
3294
|
29 or suggestions on using EOS. |
|
30 |
|
31 @menu |
|
32 * Installing EOS:: |
|
33 * Using Octave Mode:: |
|
34 * Running Octave From Within Emacs:: |
|
35 * Using the Emacs Info Reader for Octave:: |
|
36 @end menu |
|
37 |
4167
|
38 @node Installing EOS |
3294
|
39 @appendixsec Installing EOS |
|
40 |
|
41 The Emacs package EOS consists of the three files @file{octave-mod.el}, |
|
42 @file{octave-inf.el}, and @file{octave-hlp.el}. These files, or better |
|
43 yet their byte-compiled versions, should be somewhere in your Emacs |
|
44 load-path. |
|
45 |
|
46 If you have GNU Emacs with a version number at least as high as 19.35, |
|
47 you are all set up, because EOS is respectively will be part of GNU |
|
48 Emacs as of version 19.35. |
|
49 |
|
50 Otherwise, copy the three files from the @file{emacs} subdirectory of |
|
51 the Octave distribution to a place where Emacs can find them (this |
|
52 depends on how your Emacs was installed). Byte-compile them for speed |
|
53 if you want. |
|
54 |
4167
|
55 @node Using Octave Mode |
3294
|
56 @appendixsec Using Octave Mode |
|
57 |
|
58 If you are lucky, your sysadmins have already arranged everything so |
|
59 that Emacs automatically goes into Octave mode whenever you visit an |
|
60 Octave code file as characterized by its extension @file{.m}. If not, |
|
61 proceed as follows. |
|
62 |
|
63 @enumerate |
|
64 @item |
|
65 To begin using Octave mode for all @file{.m} files you visit, add the |
|
66 following lines to a file loaded by Emacs at startup time, typically |
|
67 your @file{~/.emacs} file: |
|
68 |
|
69 @lisp |
|
70 (autoload 'octave-mode "octave-mod" nil t) |
|
71 (setq auto-mode-alist |
|
72 (cons '("\\.m$" . octave-mode) auto-mode-alist)) |
|
73 @end lisp |
|
74 |
|
75 @item |
|
76 Finally, to turn on the abbrevs, auto-fill and font-lock features |
|
77 automatically, also add the following lines to one of the Emacs startup |
|
78 files: |
|
79 @lisp |
|
80 (add-hook 'octave-mode-hook |
|
81 (lambda () |
|
82 (abbrev-mode 1) |
|
83 (auto-fill-mode 1) |
|
84 (if (eq window-system 'x) |
|
85 (font-lock-mode 1)))) |
|
86 @end lisp |
|
87 See the Emacs manual for more information about how to customize |
|
88 Font-lock mode. |
|
89 @end enumerate |
|
90 |
|
91 In Octave mode, the following special Emacs commands can be used in |
|
92 addition to the standard Emacs commands. |
|
93 |
|
94 @table @kbd |
|
95 @item C-h m |
|
96 Describe the features of Octave mode. |
|
97 |
|
98 @item LFD |
|
99 Reindent the current Octave line, insert a newline and indent the new |
|
100 line (@code{octave-reindent-then-newline-and-indent}). An abbrev before |
|
101 point is expanded if @code{abbrev-mode} is non-@code{nil}. |
|
102 |
|
103 @item TAB |
|
104 Indents current Octave line based on its contents and on previous |
|
105 lines (@code{indent-according-to-mode}). |
|
106 |
|
107 @item ; |
|
108 Insert an ``electric'' semicolon (@code{octave-electric-semi}). If |
|
109 @code{octave-auto-indent} is non-@code{nil}, reindent the current line. |
|
110 If @code{octave-auto-newline} is non-@code{nil}, automagically insert a |
|
111 newline and indent the new line. |
|
112 |
|
113 @item ` |
|
114 Start entering an abbreviation (@code{octave-abbrev-start}). If Abbrev |
|
115 mode is turned on, typing @kbd{`C-h} or @kbd{`?} lists all abbrevs. |
|
116 Any other key combination is executed normally. Note that all Octave |
|
117 abbrevs start with a grave accent. |
|
118 |
|
119 @item M-LFD |
|
120 Break line at point and insert continuation marker and alignment |
|
121 (@code{octave-split-line}). |
|
122 |
|
123 @item M-TAB |
|
124 Perform completion on Octave symbol preceding point, comparing that |
|
125 symbol against Octave's reserved words and builtin variables |
|
126 (@code{octave-complete-symbol}). |
|
127 |
|
128 @item M-C-a |
|
129 Move backward to the beginning of a function |
|
130 (@code{octave-beginning-of-defun}). |
|
131 With prefix argument @var{N}, do it that many times if @var{N} is |
|
132 positive; otherwise, move forward to the @var{N}-th following beginning |
|
133 of a function. |
|
134 |
|
135 @item M-C-e |
|
136 Move forward to the end of a function (@code{octave-end-of-defun}). |
|
137 With prefix argument @var{N}, do it that many times if @var{N} is |
|
138 positive; otherwise, move back to the @var{N}-th preceding end of a |
|
139 function. |
|
140 |
|
141 @item M-C-h |
|
142 Puts point at beginning and mark at the end of the current Octave |
|
143 function, i.e., the one containing point or following point |
|
144 (@code{octave-mark-defun}). |
|
145 |
|
146 @item M-C-q |
|
147 Properly indents the Octave function which contains point |
|
148 (@code{octave-indent-defun}). |
|
149 |
|
150 @item M-; |
|
151 If there is no comment already on this line, create a code-level comment |
|
152 (started by two comment characters) if the line is empty, or an in-line |
|
153 comment (started by one comment character) otherwise |
|
154 (@code{octave-indent-for-comment}). |
|
155 Point is left after the start of the comment which is properly aligned. |
|
156 |
|
157 @item C-c ; |
|
158 Puts the comment character @samp{#} (more precisely, the string value of |
|
159 @code{octave-comment-start}) at the beginning of every line in the |
|
160 region (@code{octave-comment-region}). With just @kbd{C-u} prefix |
|
161 argument, uncomment each line in the region. A numeric prefix argument |
|
162 @var{N} means use @var{N} comment characters. |
|
163 |
|
164 @item C-c : |
|
165 Uncomments every line in the region (@code{octave-uncomment-region}). |
|
166 |
|
167 @item C-c C-p |
|
168 Move one line of Octave code backward, skipping empty and comment lines |
|
169 (@code{octave-previous-code-line}). With numeric prefix argument |
|
170 @var{N}, move that many code lines backward (forward if @var{N} is |
|
171 negative). |
|
172 |
|
173 @item C-c C-n |
|
174 Move one line of Octave code forward, skipping empty and comment lines |
|
175 (@code{octave-next-code-line}). With numeric prefix argument @var{N}, |
|
176 move that many code lines forward (backward if @var{N} is negative). |
|
177 |
|
178 @item C-c C-a |
|
179 Move to the `real' beginning of the current line |
|
180 (@code{octave-beginning-of-line}). If point is in an empty or comment |
|
181 line, simply go to its beginning; otherwise, move backwards to the |
|
182 beginning of the first code line which is not inside a continuation |
|
183 statement, i.e., which does not follow a code line ending in @samp{...} |
|
184 or @samp{\}, or is inside an open parenthesis list. |
|
185 |
|
186 @item C-c C-e |
|
187 Move to the `real' end of the current line (@code{octave-end-of-line}). |
|
188 If point is in a code line, move forward to the end of the first Octave |
|
189 code line which does not end in @samp{...} or @samp{\} or is inside an |
|
190 open parenthesis list. Otherwise, simply go to the end of the current |
|
191 line. |
|
192 |
|
193 @item C-c M-C-n |
|
194 Move forward across one balanced begin-end block of Octave code |
|
195 (@code{octave-forward-block}). With numeric prefix argument @var{N}, |
|
196 move forward across @var{n} such blocks (backward if @var{N} is |
|
197 negative). |
|
198 |
|
199 @item C-c M-C-p |
|
200 Move back across one balanced begin-end block of Octave code |
|
201 (@code{octave-backward-block}). With numeric prefix argument @var{N}, |
|
202 move backward across @var{N} such blocks (forward if @var{N} is |
|
203 negative). |
|
204 |
|
205 @item C-c M-C-d |
|
206 Move forward down one begin-end block level of Octave code |
|
207 (@code{octave-down-block}). With numeric prefix argument, do it that |
|
208 many times; a negative argument means move backward, but still go down |
|
209 one level. |
|
210 |
|
211 @item C-c M-C-u |
|
212 Move backward out of one begin-end block level of Octave code |
|
213 (@code{octave-backward-up-block}). With numeric prefix argument, do it |
|
214 that many times; a negative argument means move forward, but still to a |
|
215 less deep spot. |
|
216 |
|
217 @item C-c M-C-h |
|
218 Put point at the beginning of this block, mark at the end |
|
219 (@code{octave-mark-block}). |
|
220 The block marked is the one that contains point or follows point. |
|
221 |
|
222 @item C-c ] |
|
223 Close the current block on a separate line (@code{octave-close-block}). |
|
224 An error is signaled if no block to close is found. |
|
225 |
|
226 @item C-c f |
|
227 Insert a function skeleton, prompting for the function's name, arguments |
|
228 and return values which have to be entered without parens |
|
229 (@code{octave-insert-defun}). |
|
230 |
|
231 @item C-c C-h |
|
232 Search the function, operator and variable indices of all info files |
|
233 with documentation for Octave for entries (@code{octave-help}). If used |
|
234 interactively, the entry is prompted for with completion. If multiple |
|
235 matches are found, one can cycle through them using the standard |
|
236 @samp{,} (@code{Info-index-next}) command of the Info reader. |
|
237 |
|
238 The variable @code{octave-help-files} is a list of files to search |
|
239 through and defaults to @code{'("octave")}. If there is also an Octave |
|
240 Local Guide with corresponding info file, say, @file{octave-LG}, you can |
|
241 have @code{octave-help} search both files by |
|
242 @lisp |
|
243 (setq octave-help-files '("octave" "octave-LG")) |
|
244 @end lisp |
|
245 @noindent |
|
246 in one of your Emacs startup files. |
|
247 |
|
248 @end table |
|
249 |
|
250 A common problem is that the @key{RET} key does @emph{not} indent the |
|
251 line to where the new text should go after inserting the newline. This |
|
252 is because the standard Emacs convention is that @key{RET} (aka |
|
253 @kbd{C-m}) just adds a newline, whereas @key{LFD} (aka @kbd{C-j}) adds a |
|
254 newline and indents it. This is particularly inconvenient for users with |
|
255 keyboards which do not have a special @key{LFD} key at all; in such |
|
256 cases, it is typically more convenient to use @key{RET} as the @key{LFD} |
|
257 key (rather than typing @kbd{C-j}). |
|
258 |
|
259 You can make @key{RET} do this by adding |
|
260 @lisp |
|
261 (define-key octave-mode-map "\C-m" |
|
262 'octave-reindent-then-newline-and-indent) |
|
263 @end lisp |
|
264 @noindent |
|
265 to one of your Emacs startup files. Another, more generally applicable |
|
266 solution is |
|
267 @lisp |
|
268 (defun RET-behaves-as-LFD () |
|
269 (let ((x (key-binding "\C-j"))) |
|
270 (local-set-key "\C-m" x))) |
|
271 (add-hook 'octave-mode-hook 'RET-behaves-as-LFD) |
|
272 @end lisp |
|
273 @noindent |
|
274 (this works for all modes by adding to the startup hooks, without having |
|
275 to know the particular binding of @key{RET} in that mode!). Similar |
|
276 considerations apply for using @key{M-RET} as @key{M-LFD}. As Barry |
|
277 A. Warsaw <bwarsaw@@cnri.reston.va.us> says in the documentation for his |
|
278 @code{cc-mode}, ``This is a very common question. @code{:-)} If you want |
|
279 this to be the default behavior, don't lobby me, lobby RMS!'' |
|
280 |
|
281 The following variables can be used to customize Octave mode. |
|
282 |
|
283 @table @code |
|
284 @item octave-auto-indent |
|
285 Non-@code{nil} means auto-indent the current line after a semicolon or |
|
286 space. Default is @code{nil}. |
|
287 |
|
288 @item octave-auto-newline |
|
289 Non-@code{nil} means auto-insert a newline and indent after semicolons |
|
290 are typed. The default value is @code{nil}. |
|
291 |
|
292 @item octave-blink-matching-block |
|
293 Non-@code{nil} means show matching begin of block when inserting a space, |
|
294 newline or @samp{;} after an else or end keyword. Default is @code{t}. |
|
295 This is an extremely useful feature for automatically verifying that the |
|
296 keywords match---if they don't, an error message is displayed. |
|
297 |
|
298 @item octave-block-offset |
|
299 Extra indentation applied to statements in block structures. |
|
300 Default is 2. |
|
301 |
|
302 @item octave-continuation-offset |
|
303 Extra indentation applied to Octave continuation lines. |
|
304 Default is 4. |
|
305 |
|
306 @item octave-continuation-string |
|
307 String used for Octave continuation lines. |
|
308 Normally @samp{\}. |
|
309 |
|
310 @item octave-mode-startup-message |
|
311 If @code{t} (default), a startup message is displayed when Octave mode |
|
312 is called. |
|
313 |
|
314 @end table |
|
315 |
|
316 If Font Lock mode is enabled, Octave mode will display |
|
317 @itemize @bullet |
|
318 @item |
|
319 strings in @code{font-lock-string-face} |
|
320 @item |
|
321 comments in @code{font-lock-comment-face} |
|
322 @item |
|
323 the Octave reserved words (such as all block keywords) and the text |
|
324 functions (such as @samp{cd} or @samp{who}) which are also reserved |
|
325 using @code{font-lock-keyword-face} |
|
326 @item |
|
327 the builtin operators (@samp{&&}, @samp{<>}, @dots{}) using |
|
328 @code{font-lock-reference-face} |
|
329 @item |
4455
|
330 the builtin variables (such as @samp{warn_fortran_indexing}, @samp{NaN} |
3294
|
331 or @samp{LOADPATH}) in @code{font-lock-variable-name-face} |
|
332 @item |
|
333 and the function names in function declarations in |
|
334 @code{font-lock-function-name-face}. |
|
335 @end itemize |
|
336 |
|
337 There is also rudimentary support for Imenu (currently, function names |
|
338 can be indexed). |
|
339 |
|
340 @cindex TAGS |
|
341 @cindex Emacs TAGS files |
|
342 @cindex @code{otags} |
|
343 You can generate TAGS files for Emacs from Octave @file{.m} files using |
|
344 the shell script @code{otags} that is installed alongside your copy of |
|
345 Octave. |
|
346 |
|
347 Customization of Octave mode can be performed by modification of the |
|
348 variable @code{octave-mode-hook}. If the value of this variable is |
|
349 non-@code{nil}, turning on Octave mode calls its value. |
|
350 |
|
351 If you discover a problem with Octave mode, you can conveniently send a |
|
352 bug report using @kbd{C-c C-b} (@code{octave-submit-bug-report}). This |
|
353 automatically sets up a mail buffer with version information already |
|
354 added. You just need to add a description of the problem, including a |
|
355 reproducible test case and send the message. |
|
356 |
4167
|
357 @node Running Octave From Within Emacs |
3294
|
358 @appendixsec Running Octave From Within Emacs |
|
359 |
|
360 The package @file{octave} provides commands for running an inferior |
|
361 Octave process in a special Emacs buffer. Use |
|
362 @lisp |
|
363 M-x run-octave |
|
364 @end lisp |
|
365 @noindent |
|
366 to directly start an inferior Octave process. If Emacs does not know |
|
367 about this command, add the line |
|
368 @lisp |
|
369 (autoload 'run-octave "octave-inf" nil t) |
|
370 @end lisp |
|
371 @noindent |
|
372 to your @file{.emacs} file. |
|
373 |
|
374 This will start Octave in a special buffer the name of which is |
|
375 specified by the variable @code{inferior-octave-buffer} and defaults to |
|
376 @code{"*Inferior Octave*"}. From within this buffer, you can |
|
377 interact with the inferior Octave process `as usual', i.e., by entering |
|
378 Octave commands at the prompt. The buffer is in Inferior Octave mode, |
|
379 which is derived from the standard Comint mode, a major mode for |
|
380 interacting with an inferior interpreter. See the documentation for |
|
381 @code{comint-mode} for more details, and use @kbd{C-h b} to find out |
|
382 about available special keybindings. |
|
383 |
|
384 You can also communicate with an inferior Octave process from within |
|
385 files with Octave code (i.e., buffers in Octave mode), using the |
|
386 following commands. |
|
387 |
|
388 @table @kbd |
|
389 @item C-c i l |
|
390 Send the current line to the inferior Octave process |
|
391 (@code{octave-send-line}). |
|
392 With positive prefix argument @var{N}, send that many lines. |
|
393 If @code{octave-send-line-auto-forward} is non-@code{nil}, go to the |
|
394 next unsent code line. |
|
395 @item C-c i b |
|
396 Send the current block to the inferior Octave process |
|
397 (@code{octave-send-block}). |
|
398 @item C-c i f |
|
399 Send the current function to the inferior Octave process |
|
400 (@code{octave-send-defun}). |
|
401 @item C-c i r |
|
402 Send the region to the inferior Octave process |
|
403 (@code{octave-send-region}). |
|
404 @item C-c i s |
|
405 Make sure that `inferior-octave-buffer' is displayed |
|
406 (@code{octave-show-process-buffer}). |
|
407 @item C-c i h |
|
408 Delete all windows that display the inferior Octave buffer |
|
409 (@code{octave-hide-process-buffer}). |
|
410 @item C-c i k |
|
411 Kill the inferior Octave process and its buffer |
|
412 (@code{octave-kill-process}). |
|
413 @end table |
|
414 |
|
415 The effect of the commands which send code to the Octave process can be |
|
416 customized by the following variables. |
|
417 @table @code |
|
418 @item octave-send-echo-input |
|
419 Non-@code{nil} means echo input sent to the inferior Octave process. |
|
420 Default is @code{t}. |
|
421 |
|
422 @item octave-send-show-buffer |
|
423 Non-@code{nil} means display the buffer running the Octave process after |
|
424 sending a command (but without selecting it). |
|
425 Default is @code{t}. |
|
426 @end table |
|
427 |
|
428 If you send code and there is no inferior Octave process yet, it will be |
|
429 started automatically. |
|
430 |
|
431 The startup of the inferior Octave process is highly customizable. |
|
432 The variable @code{inferior-octave-startup-args} can be used for |
|
433 specifying command lines arguments to be passed to Octave on startup |
|
434 as a list of strings. For example, to suppress the startup message and |
|
435 use `traditional' mode, set this to @code{'("-q" "--traditional")}. |
|
436 You can also specify a startup file of Octave commands to be loaded on |
|
437 startup; note that these commands will not produce any visible output |
|
438 in the process buffer. Which file to use is controlled by the variable |
|
439 @code{inferior-octave-startup-file}. If this is @code{nil}, the file |
|
440 @file{~/.emacs-octave} is used if it exists. |
|
441 |
|
442 And finally, @code{inferior-octave-mode-hook} is run after starting the |
|
443 process and putting its buffer into Inferior Octave mode. Hence, if you |
|
444 like the up and down arrow keys to behave in the interaction buffer as |
|
445 in the shell, and you want this buffer to use nice colors, add |
|
446 @lisp |
|
447 (add-hook 'inferior-octave-mode-hook |
|
448 (lambda () |
|
449 (turn-on-font-lock) |
|
450 (define-key inferior-octave-mode-map [up] |
|
451 'comint-previous-input) |
|
452 (define-key inferior-octave-mode-map [down] |
|
453 'comint-next-input))) |
|
454 @end lisp |
|
455 @noindent |
|
456 to your @file{.emacs} file. You could also swap the roles of @kbd{C-a} |
|
457 (@code{beginning-of-line}) and @code{C-c C-a} (@code{comint-bol}) using |
|
458 this hook. |
|
459 |
|
460 @quotation |
5479
|
461 @strong{Note} that if you set your Octave prompts to something different |
|
462 from the defaults, make sure that @code{inferior-octave-prompt} matches |
|
463 them. Otherwise, @emph{nothing} will work, because Emacs will not know |
3294
|
464 when Octave is waiting for input, or done sending output. |
|
465 @end quotation |
|
466 |
4167
|
467 @node Using the Emacs Info Reader for Octave |
3294
|
468 @appendixsec Using the Emacs Info Reader for Octave |
|
469 |
|
470 You can also set up the Emacs Info reader for dealing with the results |
|
471 of Octave's @samp{help -i}. For this, the package @file{gnuserv} needs |
|
472 to be installed, which unfortunately still does not come with GNU Emacs |
|
473 (it does with XEmacs). It can be retrieved from any GNU Emacs Lisp Code |
|
474 Directory archive, e.g.@: |
|
475 @url{ftp://ftp.cis.ohio-state.edu/pub/gnu/emacs/elisp-archive}, |
3803
|
476 in the @file{packages} subdirectory. A recent version of gnuserv is |
|
477 available from |
|
478 @url{http://www.meltin.net/hacks/emacs/src/gnuserv-3.12.2.tar.gz}. |
3294
|
479 |
|
480 If @file{gnuserv} is installed, add the lines |
|
481 @lisp |
|
482 (autoload 'octave-help "octave-hlp" nil t) |
|
483 (require 'gnuserv) |
|
484 (gnuserv-start) |
|
485 @end lisp |
|
486 @noindent |
|
487 to your @file{.emacs} file. |
|
488 |
|
489 You can use either `plain' Emacs Info or the function @code{octave-help} |
|
490 as your Octave info reader (for @samp{help -i}). In the former case, |
|
491 set the Octave variable @code{INFO_PROGRAM} to @code{"info-emacs-info"}. |
|
492 The latter is perhaps more attractive because it allows to look up keys |
|
493 in the indices of @emph{several} info files related to Octave (provided |
|
494 that the Emacs variable @code{octave-help-files} is set correctly). In |
|
495 this case, set @code{INFO_PROGRAM} to @code{"info-emacs-octave-help"}. |
|
496 |
|
497 If you use Octave from within Emacs, these settings are best done in the |
|
498 @file{~/.emacs-octave} startup file (or the file pointed to by the Emacs |
|
499 variable @code{inferior-octave-startup-file}). |
|
500 |
|
501 @c Local Variables: |
|
502 @c TeX-command-default: "Texinfo" |
|
503 @c End: |