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