Initial revision
[bpt/emacs.git] / lisp / progmodes / octave-mod.el
CommitLineData
f2727dfb
RS
1;; octave-mod.el --- editing Octave source files under Emacs
2
3;;; Copyright (C) 1997 Free Software Foundation, Inc.
4
5;; Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
6;; Author: John Eaton <jwe@bevo.che.wisc.edu>
7;; Maintainer: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
8;; Keywords: languages
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
26
27;;; Commentary:
28
d1e49742
RS
29;; This package provides Emacs support for Octave.
30;; It defines Octave mode, a major mode for editing
31;; Octave code.
32
33;; The file octave-hlp.el provides `octave-help', a facility for looking up
34;; documentation on a symbol in the Octave info files.
35
36;; The file octave-inf.el contains code for interacting with an inferior
37;; Octave process using comint.
f2727dfb 38
d1e49742
RS
39;; See the documentation of `octave-mode', `octave-help' and
40;; `run-octave' for further information on usage and customization.
f2727dfb 41
d1e49742 42;;; Code:
f2727dfb 43
e7017ef9
RS
44(defconst octave-maintainer-address
45 "Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>, bug-gnu-emacs@prep.ai.mit.edu"
46 "Current maintainer of the Emacs Octave package.")
47
48(defvar octave-abbrev-table nil
49 "Abbrev table for Octave's reserved words.
50Used in octave-mode and inferior-octave-mode buffers.
51All Octave abbrevs start with a grave accent (`).")
52(if octave-abbrev-table
53 ()
54 (let ((ac abbrevs-changed))
55 (define-abbrev-table 'octave-abbrev-table ())
56 (define-abbrev octave-abbrev-table "`a" "all_va_args" nil)
57 (define-abbrev octave-abbrev-table "`b" "break" nil)
6e6d2764 58 (define-abbrev octave-abbrev-table "`cs" "case" nil)
e7017ef9
RS
59 (define-abbrev octave-abbrev-table "`ca" "catch" nil)
60 (define-abbrev octave-abbrev-table "`c" "continue" nil)
61 (define-abbrev octave-abbrev-table "`el" "else" nil)
62 (define-abbrev octave-abbrev-table "`eli" "elseif" nil)
63 (define-abbrev octave-abbrev-table "`et" "end_try_catch" nil)
64 (define-abbrev octave-abbrev-table "`eu" "end_unwind_protect" nil)
65 (define-abbrev octave-abbrev-table "`ef" "endfor" nil)
66 (define-abbrev octave-abbrev-table "`efu" "endfunction" nil)
67 (define-abbrev octave-abbrev-table "`ei" "endif" nil)
6e6d2764 68 (define-abbrev octave-abbrev-table "`es" "endswitch" nil)
e7017ef9
RS
69 (define-abbrev octave-abbrev-table "`ew" "endwhile" nil)
70 (define-abbrev octave-abbrev-table "`f" "for" nil)
71 (define-abbrev octave-abbrev-table "`fu" "function" nil)
72 (define-abbrev octave-abbrev-table "`gl" "global" nil)
73 (define-abbrev octave-abbrev-table "`gp" "gplot" nil)
74 (define-abbrev octave-abbrev-table "`gs" "gsplot" nil)
75 (define-abbrev octave-abbrev-table "`if" "if ()" nil)
6e6d2764 76 (define-abbrev octave-abbrev-table "`o" "otherwise" nil)
e7017ef9
RS
77 (define-abbrev octave-abbrev-table "`rp" "replot" nil)
78 (define-abbrev octave-abbrev-table "`r" "return" nil)
6e6d2764 79 (define-abbrev octave-abbrev-table "`s" "switch" nil)
e7017ef9
RS
80 (define-abbrev octave-abbrev-table "`t" "try" nil)
81 (define-abbrev octave-abbrev-table "`up" "unwind_protect" nil)
82 (define-abbrev octave-abbrev-table "`upc" "unwind_protect_cleanup" nil)
83 (define-abbrev octave-abbrev-table "`w" "while ()" nil)
84 (setq abbrevs-changed ac)))
85
86(defvar octave-comment-char ?#
87 "Character to start an Octave comment.")
88(defvar octave-comment-start
89 (concat (make-string 1 octave-comment-char) " ")
90 "String to insert to start a new Octave in-line comment.")
91(defvar octave-comment-start-skip "\\s<+\\s-*"
92 "Regexp to match the start of an Octave comment up to its body.")
93
94(defvar octave-begin-keywords
6e6d2764 95 '("for" "function" "if" "switch" "try" "unwind_protect" "while"))
e7017ef9 96(defvar octave-else-keywords
6e6d2764 97 '("case" "catch" "else" "elseif" "otherwise" "unwind_protect_cleanup"))
e7017ef9 98(defvar octave-end-keywords
6e6d2764 99 '("end" "endfor" "endfunction" "endif" "endswitch" "end_try_catch"
e7017ef9
RS
100 "end_unwind_protect" "endwhile"))
101
102(defvar octave-reserved-words
6e6d2764
KH
103 (append octave-begin-keywords
104 octave-else-keywords
105 octave-end-keywords
e7017ef9
RS
106 '("all_va_args" "break" "continue" "global" "gplot" "gsplot"
107 "replot" "return"))
108 "Reserved words in Octave.")
109
110(defvar octave-text-functions
111 '("casesen" "cd" "chdir" "clear" "diary" "dir" "document" "echo"
112 "edit_history" "format" "gset" "gshow" "help" "history" "hold"
113 "load" "ls" "more" "run_history" "save" "set" "show" "type"
114 "which" "who" "whos")
115 "Text functions in Octave (these names are also reserved).")
116
117(defvar octave-variables
118 '("EDITOR" "EXEC_PATH" "F_DUPFD" "F_GETFD" "F_GETFL" "F_SETFD"
119 "F_SETFL" "I" "IMAGEPATH" "INFO_FILE" "INFO_PROGRAM" "Inf" "J"
120 "LOADPATH" "NaN" "OCTAVE_VERSION" "O_APPEND" "O_CREAT" "O_EXCL"
121 "O_NONBLOCK" "O_RDONLY" "O_RDWR" "O_TRUNC" "O_WRONLY" "PAGER" "PS1"
122 "PS2" "PS4" "PWD" "SEEK_CUR" "SEEK_END" "SEEK_SET" "__F_DUPFD__"
123 "__F_GETFD__" "__F_GETFL__" "__F_SETFD__" "__F_SETFL__" "__I__"
124 "__Inf__" "__J__" "__NaN__" "__OCTAVE_VERSION__" "__O_APPEND__"
125 "__O_CREAT__" "__O_EXCL__" "__O_NONBLOCK__" "__O_RDONLY__"
126 "__O_RDWR__" "__O_TRUNC__" "__O_WRONLY__" "__PWD__" "__SEEK_CUR__"
127 "__SEEK_END__" "__SEEK_SET__" "__argv__" "__e__" "__eps__"
128 "__error_text__" "__i__" "__inf__" "__j__" "__nan__" "__pi__"
129 "__program_invocation_name__" "__program_name__" "__realmax__"
130 "__realmin__" "__stderr__" "__stdin__" "__stdout__" "ans" "argv"
131 "automatic_replot" "beep_on_error" "completion_append_char"
132 "default_return_value" "default_save_format"
133 "define_all_return_values" "do_fortran_indexing" "e"
134 "echo_executing_commands" "empty_list_elements_ok" "eps"
135 "error_text" "gnuplot_binary" "gnuplot_has_multiplot" "history_file"
136 "history_size" "ignore_function_time_stamp" "implicit_str_to_num_ok"
137 "inf" "nan" "nargin" "ok_to_lose_imaginary_part"
138 "output_max_field_width" "output_precision"
139 "page_output_immediately" "page_screen_output" "pi"
140 "prefer_column_vectors" "prefer_zero_one_indexing"
141 "print_answer_id_name" "print_empty_dimensions"
142 "program_invocation_name" "program_name" "propagate_empty_matrices"
143 "realmax" "realmin" "resize_on_range_error"
144 "return_last_computed_value" "save_precision" "saving_history"
145 "silent_functions" "split_long_rows" "stderr" "stdin" "stdout"
146 "string_fill_char" "struct_levels_to_print"
147 "suppress_verbose_help_message" "treat_neg_dim_as_zero"
148 "warn_assign_as_truth_value" "warn_comma_in_global_decl"
149 "warn_divide_by_zero" "warn_function_name_clash"
150 "warn_missing_semicolon" "whitespace_in_literal_matrix")
151 "Builtin variables in Octave.")
152
153(defvar octave-function-header-regexp
154 (concat "^\\s-*\\<\\(function\\)\\>"
155 "\\([^=;\n]*=[ \t]*\\|[ \t]*\\)\\(\\w+\\)\\>")
156 "Regexp to match an Octave function header.
157The string `function' and its name are given by the first and third
158parenthetical grouping.")
159
160(defvar octave-font-lock-keywords
161 (list
162 ;; Fontify all builtin keywords.
163 (cons (concat "\\<\\("
164 (mapconcat 'identity octave-reserved-words "\\|")
165 (mapconcat 'identity octave-text-functions "\\|")
166 "\\)\\>")
167 'font-lock-keyword-face)
168 ;; Fontify all builtin operators.
169 (cons "\\(&\\||\\|<=\\|>=\\|==\\|<\\|>\\|!=\\|!\\)"
170 'font-lock-reference-face)
171 ;; Fontify all builtin variables.
172 (cons (concat "\\<\\("
173 (mapconcat 'identity octave-variables "\\|")
174 "\\)\\>")
175 'font-lock-variable-name-face)
176 ;; Fontify all function declarations.
177 (list octave-function-header-regexp
178 '(1 font-lock-keyword-face)
179 '(3 font-lock-function-name-face nil t)))
180 "Additional Octave expressions to highlight.")
181
182(defvar inferior-octave-buffer "*Inferior Octave*"
183 "*Name of buffer for running an inferior Octave process.")
184
185(defvar inferior-octave-process nil)
186\f
f2727dfb
RS
187(defvar octave-mode-map nil
188 "Keymap used in Octave mode.")
189(if octave-mode-map
190 ()
191 (let ((map (make-sparse-keymap)))
192 (define-key map "`" 'octave-abbrev-start)
193 (define-key map ";" 'octave-electric-semi)
194 (define-key map " " 'octave-electric-space)
195 (define-key map "\n" 'octave-reindent-then-newline-and-indent)
196 (define-key map "\t" 'indent-according-to-mode)
197 (define-key map "\e;" 'octave-indent-for-comment)
198 (define-key map "\e\n" 'octave-indent-new-comment-line)
199 (define-key map "\e\t" 'octave-complete-symbol)
200 (define-key map "\M-\C-a" 'octave-beginning-of-defun)
201 (define-key map "\M-\C-e" 'octave-end-of-defun)
202 (define-key map "\M-\C-h" 'octave-mark-defun)
203 (define-key map "\M-\C-q" 'octave-indent-defun)
204 (define-key map "\C-c;" 'octave-comment-region)
205 (define-key map "\C-c:" 'octave-uncomment-region)
206 (define-key map "\C-c\C-b" 'octave-submit-bug-report)
207 (define-key map "\C-c\C-p" 'octave-previous-code-line)
208 (define-key map "\C-c\C-n" 'octave-next-code-line)
209 (define-key map "\C-c\C-a" 'octave-beginning-of-line)
210 (define-key map "\C-c\C-e" 'octave-end-of-line)
211 (define-key map "\C-c\M-\C-n" 'octave-forward-block)
212 (define-key map "\C-c\M-\C-p" 'octave-backward-block)
213 (define-key map "\C-c\M-\C-u" 'octave-backward-up-block)
214 (define-key map "\C-c\M-\C-d" 'octave-down-block)
215 (define-key map "\C-c\M-\C-h" 'octave-mark-block)
216 (define-key map "\C-c]" 'octave-close-block)
217 (define-key map "\C-cf" 'octave-insert-defun)
218 (define-key map "\C-c\C-h" 'octave-help)
219 (define-key map "\C-cil" 'octave-send-line)
220 (define-key map "\C-cib" 'octave-send-block)
221 (define-key map "\C-cif" 'octave-send-defun)
222 (define-key map "\C-cir" 'octave-send-region)
223 (define-key map "\C-cis" 'octave-show-process-buffer)
224 (define-key map "\C-cih" 'octave-hide-process-buffer)
225 (define-key map "\C-cik" 'octave-kill-process)
226 (setq octave-mode-map map)))
227
228(defvar octave-mode-menu
229 (list "Octave"
230 (list "Lines"
231 ["Previous Code Line" octave-previous-code-line t]
232 ["Next Code Line" octave-next-code-line t]
233 ["Begin of Continuation" octave-beginning-of-line t]
234 ["End of Continuation" octave-end-of-line t]
235 ["Split Line at Point" octave-indent-new-comment-line t])
236 (list "Blocks"
237 ["Next Block" octave-forward-block t]
238 ["Previous Block" octave-backward-block t]
239 ["Down Block" octave-down-block t]
240 ["Up Block" octave-backward-up-block t]
241 ["Mark Block" octave-mark-block t]
242 ["Close Block" octave-close-block t])
243 (list "Functions"
244 ["Begin of Function" octave-beginning-of-defun t]
245 ["End of Function" octave-end-of-defun t]
246 ["Mark Function" octave-mark-defun t]
247 ["Indent Function" octave-indent-defun t]
248 ["Insert Function" octave-insert-defun t])
249 "-"
250 (list "Debug"
251 ["Send Current Line" octave-send-line t]
252 ["Send Current Block" octave-send-block t]
253 ["Send Current Function" octave-send-defun t]
254 ["Send Region" octave-send-region t]
255 ["Show Process Buffer" octave-show-process-buffer t]
256 ["Hide Process Buffer" octave-hide-process-buffer t]
257 ["Kill Process" octave-kill-process t])
258 "-"
259 ["Indent Line" indent-according-to-mode t]
260 ["Complete Symbol" octave-complete-symbol t]
261 "-"
262 ["Toggle Abbrev Mode" abbrev-mode t]
263 ["Toggle Auto-Fill Mode" auto-fill-mode t]
264 "-"
265 ["Submit Bug Report" octave-submit-bug-report t]
266 "-"
267 ["Describe Octave Mode" octave-describe-major-mode t]
268 ["Lookup Octave Index" octave-help t])
269 "Menu for Octave mode.")
270
271(defvar octave-mode-syntax-table nil
272 "Syntax table in use in octave-mode buffers.")
273(if octave-mode-syntax-table
274 ()
275 (let ((table (make-syntax-table)))
276 (modify-syntax-entry ?\r " " table)
277 (modify-syntax-entry ?+ "." table)
278 (modify-syntax-entry ?- "." table)
279 (modify-syntax-entry ?= "." table)
280 (modify-syntax-entry ?* "." table)
281 (modify-syntax-entry ?/ "." table)
282 (modify-syntax-entry ?> "." table)
283 (modify-syntax-entry ?< "." table)
284 (modify-syntax-entry ?& "." table)
285 (modify-syntax-entry ?| "." table)
286 (modify-syntax-entry ?! "." table)
287 (modify-syntax-entry ?\\ "\\" table)
288 (modify-syntax-entry ?\' "." table)
289 (modify-syntax-entry ?\` "w" table)
290 (modify-syntax-entry ?\" "\"" table)
291 (modify-syntax-entry ?. "w" table)
292 (modify-syntax-entry ?_ "w" table)
293 (modify-syntax-entry ?\% "." table)
294 (modify-syntax-entry ?\# "<" table)
295 (modify-syntax-entry ?\n ">" table)
296 (setq octave-mode-syntax-table table)))
297
298(defvar octave-auto-newline nil
299 "*Non-nil means automatically newline after a semicolon in Octave mode.")
300
301(defvar octave-blink-matching-block t
302 "*Control the blinking of matching Octave block keywords.
303Non-nil means show matching begin of block when inserting a space,
304newline or semicolon after an else or end keyword.")
305(defvar octave-block-offset 2
306 "*Extra indentation applied to statements in Octave block structures.")
307
308(defvar octave-block-begin-regexp
309 (concat "\\<\\("
310 (mapconcat 'identity octave-begin-keywords "\\|")
311 "\\)\\>"))
312(defvar octave-block-else-regexp
313 (concat "\\<\\("
314 (mapconcat 'identity octave-else-keywords "\\|")
315 "\\)\\>"))
316(defvar octave-block-end-regexp
317 (concat "\\<\\("
318 (mapconcat 'identity octave-end-keywords "\\|")
319 "\\)\\>"))
320(defvar octave-block-begin-or-end-regexp
321 (concat octave-block-begin-regexp "\\|" octave-block-end-regexp))
322(defvar octave-block-else-or-end-regexp
323 (concat octave-block-else-regexp "\\|" octave-block-end-regexp))
324(defvar octave-block-match-alist
325 '(("for" . ("end" "endfor"))
326 ("function" . ("end" "endfunction"))
327 ("if" . ("else" "elseif" "end" "endif"))
6e6d2764 328 ("switch" . ("case" "end" "endswitch" "otherwise"))
f2727dfb
RS
329 ("try" . ("catch" "end" "end_try_catch"))
330 ("unwind_protect" . ("unwind_protect_cleanup" "end"
331 "end_unwind_protect"))
332 ("while" . ("end" "endwhile")))
333 "Alist with Octave's matching block keywords.
334Has Octave's begin keywords as keys and a list of the matching else or
335end keywords as associated values.")
336
337(defvar octave-block-comment-start
338 (concat (make-string 2 octave-comment-char) " ")
339 "String to insert to start a new Octave comment on an empty line.")
340
341(defvar octave-continuation-offset 4
342 "*Extra indentation applied to Octave continuation lines.")
343(defvar octave-continuation-regexp
344 "[^#%\n]*\\(\\\\\\|\\.\\.\\.\\)\\s-*\\(\\s<.*\\)?$")
345(defvar octave-continuation-string "\\"
346 "*Character string used for Octave continuation lines. Normally \\.")
347
348(defvar octave-completion-alist nil
349 "Alist of Octave symbols for completion in Octave mode.
350Each element looks like (VAR . VAR), where the car and cdr are the same
351symbol (an Octave command or variable name).
352Currently, only builtin variables can be completed.")
353
354(defvar octave-mode-imenu-generic-expression
355 (list
356 ;; Functions
357 (list nil octave-function-header-regexp 3))
358 "Imenu expression for Octave mode. See `imenu-generic-expression'.")
359
360(defvar octave-mode-startup-message t
361 "*Nil means do not display the Octave mode startup message.")
362
363(defvar octave-mode-hook nil
364 "*Hook to be run when Octave mode is started.")
365
366(defvar octave-send-show-buffer t
367 "*Non-nil means display `inferior-octave-buffer' after sending to it.")
368(defvar octave-send-line-auto-forward t
369 "*Control auto-forward after sending to the inferior Octave process.
370Non-nil means always go to the next Octave code line after sending.")
371(defvar octave-send-echo-input t
372 "*Non-nil means echo input sent to the inferior Octave process.")
373
374\f
375;;;###autoload
376(defun octave-mode ()
377 "Major mode for editing Octave code.
378
379This mode makes it easier to write Octave code by helping with
380indentation, doing some of the typing for you (with Abbrev mode) and by
381showing keywords, comments, strings, etc. in different faces (with
382Font Lock mode on terminals that support it).
383
384Octave itself is a high-level language, primarily intended for numerical
385computations. It provides a convenient command line interface for
386solving linear and nonlinear problems numerically. Function definitions
387can also be stored in files, and it can be used in a batch mode (which
388is why you need this mode!).
389
390The latest released version of Octave is always available via anonymous
391ftp from bevo.che.wisc.edu in the directory `/pub/octave'. Complete
392source and binaries for several popular systems are available.
393
394Type \\[list-abbrevs] to display the built-in abbrevs for Octave keywords.
395
396Keybindings
397===========
398
399\\{octave-mode-map}
400
401Variables you can use to customize Octave mode
402==============================================
403
404octave-auto-newline
405 Non-nil means auto-insert a newline and indent after a semicolon.
406 Default is nil.
407
408octave-blink-matching-block
409 Non-nil means show matching begin of block when inserting a space,
410 newline or semicolon after an else or end keyword. Default is t.
411
412octave-block-offset
413 Extra indentation applied to statements in block structures.
414 Default is 2.
415
416octave-continuation-offset
417 Extra indentation applied to Octave continuation lines.
418 Default is 4.
419
420octave-continuation-string
421 String used for Octave continuation lines.
422 Default is a backslash.
423
424octave-mode-startup-message
425 Nil means do not display the Octave mode startup message.
426 Default is t.
427
428octave-send-echo-input
429 Non-nil means always display `inferior-octave-buffer' after sending a
430 command to the inferior Octave process.
431
432octave-send-line-auto-forward
433 Non-nil means always go to the next unsent line of Octave code after
434 sending a line to the inferior Octave process.
435
436octave-send-echo-input
437 Non-nil means echo input sent to the inferior Octave process.
438
439Turning on Octave mode runs the hook `octave-mode-hook'.
440
441To begin using this mode for all `.m' files that you edit, add the
442following lines to your `.emacs' file:
443
444 (autoload 'octave-mode \"octave-mod\" nil t)
445 (setq auto-mode-alist
446 (cons '(\"\\\\.m$\" . octave-mode) auto-mode-alist))
447
448To automatically turn on the abbrev, auto-fill and font-lock features,
449add the following lines to your `.emacs' file as well:
450
451 (add-hook 'octave-mode-hook
452 (lambda ()
453 (abbrev-mode 1)
454 (auto-fill-mode 1)
455 (if (eq window-system 'x)
456 (font-lock-mode 1))))
457
458To submit a problem report, enter \\[octave-submit-bug-report] from \
459an Octave mode buffer.
460This automatically sets up a mail buffer with version information
461already added. You just need to add a description of the problem,
462including a reproducible test case and send the message."
463 (interactive)
464 (kill-all-local-variables)
465
466 (use-local-map octave-mode-map)
467 (setq major-mode 'octave-mode)
468 (setq mode-name "Octave")
469 (setq local-abbrev-table octave-abbrev-table)
470 (set-syntax-table octave-mode-syntax-table)
471
472 (make-local-variable 'indent-line-function)
473 (setq indent-line-function 'octave-indent-line)
474
475 (make-local-variable 'comment-start)
476 (setq comment-start octave-comment-start)
477 (make-local-variable 'comment-end)
478 (setq comment-end "")
479 (make-local-variable 'comment-column)
480 (setq comment-column 32)
481 (make-local-variable 'comment-start-skip)
482 (setq comment-start-skip "\\s<+\\s-*")
483 (make-local-variable 'comment-indent-function)
484 (setq comment-indent-function 'octave-comment-indent)
485
486 (make-local-variable 'parse-sexp-ignore-comments)
487 (setq parse-sexp-ignore-comments t)
488 (make-local-variable 'paragraph-start)
489 (setq paragraph-start (concat "\\s-*$\\|" page-delimiter))
490 (make-local-variable 'paragraph-separate)
491 (setq paragraph-separate paragraph-start)
492 (make-local-variable 'paragraph-ignore-fill-prefix)
493 (setq paragraph-ignore-fill-prefix t)
494 (make-local-variable 'fill-paragraph-function)
495 (setq fill-paragraph-function 'octave-fill-paragraph)
496 (make-local-variable 'adaptive-fill-regexp)
497 (setq adaptive-fill-regexp nil)
498 (make-local-variable 'fill-column)
499 (setq fill-column 72)
500 (make-local-variable 'normal-auto-fill-function)
501 (setq normal-auto-fill-function 'octave-auto-fill)
502
503 (make-local-variable 'font-lock-defaults)
504 (setq font-lock-defaults '(octave-font-lock-keywords nil nil))
505
506 (make-local-variable 'imenu-generic-expression)
507 (setq imenu-generic-expression octave-mode-imenu-generic-expression)
508
509 (octave-add-octave-menu)
510 (octave-initialize-completions)
777874d0 511 (run-hooks 'octave-mode-hook))
f2727dfb
RS
512\f
513;;; Miscellaneous useful functions
514(defun octave-describe-major-mode ()
515 "Describe the current major mode."
516 (interactive)
517 (describe-function major-mode))
518
519(defun octave-point (position)
520 "Returns the value of point at certain positions."
521 (save-excursion
522 (cond
523 ((eq position 'bol) (beginning-of-line))
524 ((eq position 'eol) (end-of-line))
525 ((eq position 'boi) (back-to-indentation))
526 ((eq position 'bonl) (forward-line 1))
527 ((eq position 'bopl) (forward-line -1))
528 (t (error "unknown buffer position requested: %s" position)))
529 (point)))
530
531(defsubst octave-in-comment-p ()
532 "Returns t if point is inside an Octave comment, nil otherwise."
533 (interactive)
534 (save-excursion
535 (nth 4 (parse-partial-sexp (octave-point 'bol) (point)))))
536
537(defsubst octave-in-string-p ()
538 "Returns t if point is inside an Octave string, nil otherwise."
539 (interactive)
540 (save-excursion
541 (nth 3 (parse-partial-sexp (octave-point 'bol) (point)))))
542
543(defsubst octave-not-in-string-or-comment-p ()
544 "Returns t iff point is not inside an Octave string or comment."
545 (let ((pps (parse-partial-sexp (octave-point 'bol) (point))))
546 (not (or (nth 3 pps) (nth 4 pps)))))
547
548(defun octave-in-block-p ()
549 "Returns t if point is inside an Octave block, nil otherwise.
550The block is taken to start at the first letter of the begin keyword and
551to end after the end keyword."
552 (let ((pos (point)))
553 (save-excursion
554 (condition-case nil
555 (progn
556 (skip-syntax-forward "w")
557 (octave-up-block -1)
558 (octave-forward-block)
559 t)
560 (error nil))
561 (< pos (point)))))
562
563(defun octave-in-defun-p ()
564 "Returns t iff point is inside an Octave function declaration.
565The function is taken to start at the `f' of `function' and to end after
566the end keyword."
567 (let ((pos (point)))
568 (save-excursion
569 (or (and (looking-at "\\<function\\>")
570 (octave-not-in-string-or-comment-p))
571 (and (octave-beginning-of-defun)
572 (condition-case nil
573 (progn
574 (octave-forward-block)
575 t)
576 (error nil))
577 (< pos (point)))))))
578
579;;; Comments
580(defun octave-comment-region (beg end &optional arg)
581 "Comment or uncomment each line in the region as Octave code.
582See `comment-region'."
583 (interactive "r\nP")
584 (let ((comment-start (char-to-string octave-comment-char)))
585 (comment-region beg end arg)))
586
587(defun octave-uncomment-region (beg end &optional arg)
588 "Uncomment each line in the region as Octave code."
589 (interactive "r\nP")
590 (or arg (setq arg 1))
591 (octave-comment-region beg end (- arg)))
592
593\f
594;;; Indentation
595(defun calculate-octave-indent ()
596 "Return appropriate indentation for current line as Octave code.
597Returns an integer (the column to indent to) unless the line is a
598comment line with fixed goal golumn. In that case, returns a list whose
599car is the column to indent to, and whose cdr is the current indentation
600level."
601 (let ((is-continuation-line
602 (save-excursion
603 (if (zerop (octave-previous-code-line))
604 (looking-at octave-continuation-regexp))))
605 (icol 0))
606 (save-excursion
607 (beginning-of-line)
608 ;; If we can move backward out one level of parentheses, take 1
609 ;; plus the indentation of that parenthesis. Otherwise, go back
610 ;; to the beginning of the previous code line, and compute the
611 ;; offset this line gives.
612 (if (condition-case nil
613 (progn
614 (up-list -1)
615 t)
616 (error nil))
617 (setq icol (+ 1 (current-column)))
618 (if (zerop (octave-previous-code-line))
619 (progn
620 (octave-beginning-of-line)
621 (back-to-indentation)
622 (setq icol (current-column))
623 (let ((bot (point))
624 (eol (octave-point 'eol)))
625 (while (< (point) eol)
626 (if (octave-not-in-string-or-comment-p)
627 (cond
6e6d2764
KH
628 ((looking-at "\\<switch\\>")
629 (setq icol (+ icol (* 2 octave-block-offset))))
f2727dfb
RS
630 ((looking-at octave-block-begin-regexp)
631 (setq icol (+ icol octave-block-offset)))
632 ((looking-at octave-block-else-regexp)
633 (if (= bot (point))
634 (setq icol (+ icol octave-block-offset))))
635 ((looking-at octave-block-end-regexp)
636 (if (not (= bot (point)))
6e6d2764
KH
637 (setq icol (- icol
638 (octave-block-end-offset)))))))
f2727dfb
RS
639 (forward-char)))
640 (if is-continuation-line
641 (setq icol (+ icol octave-continuation-offset)))))))
642 (save-excursion
643 (back-to-indentation)
644 (cond
6e6d2764 645 ((and (looking-at octave-block-else-regexp)
f2727dfb
RS
646 (octave-not-in-string-or-comment-p))
647 (setq icol (- icol octave-block-offset)))
6e6d2764
KH
648 ((and (looking-at octave-block-end-regexp)
649 (octave-not-in-string-or-comment-p))
650 (setq icol (- icol (octave-block-end-offset))))
f2727dfb
RS
651 ((looking-at "\\s<\\s<\\s<\\S<")
652 (setq icol (list 0 icol)))
653 ((looking-at "\\s<\\S<")
654 (setq icol (list comment-column icol)))))
655 icol))
656
6e6d2764
KH
657(defun octave-block-end-offset ()
658 (save-excursion
659 (octave-backward-up-block 1)
660 (* octave-block-offset
661 (if (string-match (match-string 0) "switch") 2 1))))
662
f2727dfb
RS
663(defun octave-comment-indent ()
664 (if (looking-at "\\s<\\s<\\s<")
665 0
666 (if (looking-at "\\s<\\s<")
667 (calculate-octave-indent)
668 (skip-syntax-backward " ")
6e6d2764 669 (max (if (bolp) 0 (+ 1 (current-column)))
f2727dfb
RS
670 comment-column))))
671
672(defun octave-indent-for-comment ()
673 "Maybe insert and indent an Octave comment.
674If there is no comment already on this line, create a code-level comment
675(started by two comment characters) if the line is empty, or an in-line
676comment (started by one comment character) otherwise.
677Point is left after the start of the comment which is properly aligned."
678 (interactive)
679 (indent-for-comment)
680 (indent-according-to-mode))
681
682(defun octave-indent-line (&optional arg)
683 "Indent current line as Octave code.
684With optional ARG, use this as offset unless this line is a comment with
685fixed goal column."
686 (interactive)
687 (or arg (setq arg 0))
688 (let ((icol (calculate-octave-indent))
689 (relpos (- (current-column) (current-indentation))))
690 (if (listp icol)
691 (setq icol (car icol))
692 (setq icol (+ icol arg)))
693 (if (< icol 0)
694 (error "Unmatched end keyword")
695 (indent-line-to icol)
696 (if (> relpos 0)
697 (move-to-column (+ icol relpos))))))
698
699(defun octave-indent-new-comment-line ()
700 "Break Octave line at point, continuing comment if within one.
701If within code, insert `octave-continuation-string' before breaking the
702line. If within a string, signal an error.
703The new line is properly indented."
704 (interactive)
705 (delete-horizontal-space)
706 (cond
707 ((octave-in-comment-p)
708 (indent-new-comment-line))
709 ((octave-in-string-p)
710 (error "Cannot split a code line inside a string"))
711 (t
712 (insert (concat " " octave-continuation-string))
713 (octave-reindent-then-newline-and-indent))))
714
715(defun octave-indent-defun ()
716 "Properly indents the Octave function which contains point."
717 (interactive)
718 (save-excursion
719 (octave-mark-defun)
720 (message "Indenting function...")
721 (indent-region (point) (mark) nil))
722 (message "Indenting function...done."))
723
724\f
725;;; Motion
726(defun octave-next-code-line (&optional arg)
727 "Move ARG lines of Octave code forward (backward if ARG is negative).
728Skips past all empty and comment lines. Default for ARG is 1.
729
730On success, return 0. Otherwise, go as far as possible and return -1."
731 (interactive "p")
732 (or arg (setq arg 1))
733 (beginning-of-line)
734 (let ((n 0)
735 (inc (if (> arg 0) 1 -1)))
736 (while (and (/= arg 0) (= n 0))
737 (setq n (forward-line inc))
738 (while (and (= n 0)
739 (looking-at "\\s-*\\($\\|\\s<\\)"))
740 (setq n (forward-line inc)))
741 (setq arg (- arg inc)))
742 n))
743
744(defun octave-previous-code-line (&optional arg)
745 "Move ARG lines of Octave code backward (forward if ARG is negative).
746Skips past all empty and comment lines. Default for ARG is 1.
747
748On success, return 0. Otherwise, go as far as possible and return -1."
749 (interactive "p")
750 (or arg (setq arg 1))
751 (octave-next-code-line (- arg)))
752
753(defun octave-beginning-of-line ()
754 "Move point to beginning of current Octave line.
755If on an empty or comment line, go to the beginning of that line.
756Otherwise, move backward to the beginning of the first Octave code line
757which is not inside a continuation statement, i.e., which does not
758follow a code line ending in `...' or `\\', or is inside an open
759parenthesis list."
760 (interactive)
761 (beginning-of-line)
762 (if (not (looking-at "\\s-*\\($\\|\\s<\\)"))
763 (while (or (condition-case nil
764 (progn
765 (up-list -1)
766 (beginning-of-line)
767 t)
768 (error nil))
769 (and (or (looking-at "\\s-*\\($\\|\\s<\\)")
770 (save-excursion
771 (if (zerop (octave-previous-code-line))
772 (looking-at octave-continuation-regexp))))
773 (zerop (forward-line -1)))))))
774
775(defun octave-end-of-line ()
776 "Move point to end of current Octave line.
777If on an empty or comment line, go to the end of that line.
778Otherwise, move forward to the end of the first Octave code line which
779does not end in `...' or `\\' or is inside an open parenthesis list."
780 (interactive)
781 (end-of-line)
782 (if (save-excursion
783 (beginning-of-line)
784 (looking-at "\\s-*\\($\\|\\s<\\)"))
785 ()
786 (while (or (condition-case nil
787 (progn
788 (up-list 1)
789 (end-of-line)
790 t)
791 (error nil))
792 (and (save-excursion
793 (beginning-of-line)
794 (or (looking-at "\\s-*\\($\\|\\s<\\)")
795 (looking-at octave-continuation-regexp)))
796 (zerop (forward-line 1)))))
797 (end-of-line)))
798
799(defun octave-scan-blocks (from count depth)
800 "Scan from character number FROM by COUNT Octave begin-end blocks.
801Returns the character number of the position thus found.
802
803If DEPTH is nonzero, block depth begins counting from that value.
804Only places where the depth in blocks becomes zero are candidates for
805stopping; COUNT such places are counted.
806
807If the beginning or end of the buffer is reached and the depth is wrong,
808an error is signaled."
809 (let ((min-depth (if (> depth 0) 0 depth))
810 (inc (if (> count 0) 1 -1)))
811 (save-excursion
812 (while (/= count 0)
813 (catch 'foo
814 (while (or (re-search-forward
815 octave-block-begin-or-end-regexp nil 'move inc)
816 (if (/= depth 0)
817 (error "Unbalanced block")))
818 (if (octave-not-in-string-or-comment-p)
819 (progn
820 (cond
821 ((match-end 1)
822 (setq depth (+ depth inc)))
823 ((match-end 2)
824 (setq depth (- depth inc))))
825 (if (< depth min-depth)
826 (error "Containing expression ends prematurely"))
827 (if (= depth 0)
828 (throw 'foo nil))))))
829 (setq count (- count inc)))
830 (point))))
831
832(defun octave-forward-block (&optional arg)
833 "Move forward across one balanced Octave begin-end block.
834With argument, do it that many times.
835Negative arg -N means move backward across N blocks."
836 (interactive "p")
837 (or arg (setq arg 1))
838 (goto-char (or (octave-scan-blocks (point) arg 0) (buffer-end arg))))
839
840(defun octave-backward-block (&optional arg)
841 "Move backward across one balanced Octave begin-end block.
842With argument, do it that many times.
843Negative arg -N means move forward across N blocks."
844 (interactive "p")
845 (or arg (setq arg 1))
846 (octave-forward-block (- arg)))
847
848(defun octave-down-block (arg)
849 "Move forward down one begin-end block level of Octave code.
850With argument, do this that many times.
851A negative argument means move backward but still go down a level.
852In Lisp programs, an argument is required."
853 (interactive "p")
854 (let ((inc (if (> arg 0) 1 -1)))
855 (while (/= arg 0)
856 (goto-char (or (octave-scan-blocks (point) inc -1)
857 (buffer-end arg)))
858 (setq arg (- arg inc)))))
859
860(defun octave-backward-up-block (arg)
861 "Move backward out of one begin-end block level of Octave code.
862With argument, do this that many times.
863A negative argument means move forward but still to a less deep spot.
864In Lisp programs, an argument is required."
865 (interactive "p")
866 (octave-up-block (- arg)))
867
868(defun octave-up-block (arg)
869 "Move forward out of one begin-end block level of Octave code.
870With argument, do this that many times.
871A negative argument means move backward but still to a less deep spot.
872In Lisp programs, an argument is required."
873 (interactive "p")
874 (let ((inc (if (> arg 0) 1 -1)))
875 (while (/= arg 0)
876 (goto-char (or (octave-scan-blocks (point) inc 1)
877 (buffer-end arg)))
878 (setq arg (- arg inc)))))
879
880(defun octave-mark-block ()
881 "Put point at the beginning of this Octave block, mark at the end.
882The block marked is the one that contains point or follows point."
883 (interactive)
884 (let ((pos (point)))
885 (if (or (and (octave-in-block-p)
886 (skip-syntax-forward "w"))
887 (condition-case nil
888 (progn
889 (octave-down-block 1)
890 (octave-in-block-p))
891 (error nil)))
892 (progn
893 (octave-up-block -1)
894 (push-mark (point))
895 (octave-forward-block)
896 (exchange-point-and-mark))
897 (goto-char pos)
898 (message "No block to mark found"))))
899
900(defun octave-close-block ()
901 "Close the current Octave block on a separate line.
902An error is signaled if no block to close is found."
903 (interactive)
904 (let (bb-keyword)
905 (condition-case nil
906 (progn
907 (save-excursion
908 (octave-backward-up-block 1)
909 (setq bb-keyword (buffer-substring-no-properties
910 (match-beginning 1) (match-end 1))))
911 (if (save-excursion
912 (beginning-of-line)
913 (looking-at "^\\s-*$"))
914 (indent-according-to-mode)
915 (octave-reindent-then-newline-and-indent))
916 (insert (car (reverse
917 (assoc bb-keyword
918 octave-block-match-alist))))
919 (octave-reindent-then-newline-and-indent)
920 t)
921 (error (message "No block to close found")))))
922
923(defun octave-blink-matching-block-open ()
924 "Blink the matching Octave begin block keyword.
925If point is right after an Octave else or end type block keyword, move
926cursor momentarily to the corresponding begin keyword.
927Signal an error if the keywords are incompatible."
928 (interactive)
929 (let (bb-keyword bb-arg eb-keyword pos eol)
930 (if (and (octave-not-in-string-or-comment-p)
931 (looking-at "\\>")
932 (save-excursion
933 (skip-syntax-backward "w")
934 (looking-at octave-block-else-or-end-regexp)))
935 (save-excursion
936 (cond
937 ((match-end 1)
938 (setq eb-keyword
939 (buffer-substring-no-properties
940 (match-beginning 1) (match-end 1)))
941 (octave-backward-up-block 1))
942 ((match-end 2)
943 (setq eb-keyword
944 (buffer-substring-no-properties
945 (match-beginning 2) (match-end 2)))
946 (octave-backward-block)))
947 (setq pos (match-end 0)
948 bb-keyword
949 (buffer-substring-no-properties
950 (match-beginning 0) pos)
951 pos (+ pos 1)
952 eol (octave-point 'eol)
953 bb-arg
954 (save-excursion
955 (save-restriction
956 (goto-char pos)
957 (while (and (skip-syntax-forward "^<" eol)
958 (octave-in-string-p)
959 (not (forward-char 1))))
960 (skip-syntax-backward " ")
961 (buffer-substring-no-properties pos (point)))))
962 (if (member eb-keyword
963 (cdr (assoc bb-keyword octave-block-match-alist)))
964 (progn
965 (message "Matches `%s %s'" bb-keyword bb-arg)
966 (if (pos-visible-in-window-p)
967 (sit-for blink-matching-delay)))
968 (error "Block keywords `%s' and `%s' do not match"
969 bb-keyword eb-keyword))))))
970
971(defun octave-beginning-of-defun (&optional arg)
972 "Move backward to the beginning of an Octave function.
973With positive ARG, do it that many times. Negative argument -N means
974move forward to Nth following beginning of a function.
975Returns t unless search stops at the beginning or end of the buffer."
976 (interactive "p")
977 (let* ((arg (or arg 1))
978 (inc (if (> arg 0) 1 -1))
979 (found))
980 (and (not (eobp))
981 (not (and (> arg 0) (looking-at "\\<function\\>")))
982 (skip-syntax-forward "w"))
983 (while (and (/= arg 0)
984 (setq found
985 (re-search-backward "\\<function\\>" nil 'move inc)))
986 (if (octave-not-in-string-or-comment-p)
987 (setq arg (- arg inc))))
988 (if found
989 (progn
990 (and (< inc 0) (goto-char (match-beginning 0)))
991 t))))
992
993(defun octave-end-of-defun (&optional arg)
994 "Move forward to the end of an Octave function.
995With positive ARG, do it that many times. Negative argument -N means
996move back to Nth preceding end of a function.
997
998An end of a function occurs right after the end keyword matching the
999`function' keyword that starts the function."
1000 (interactive "p")
1001 (or arg (setq arg 1))
1002 (and (< arg 0) (skip-syntax-backward "w"))
1003 (and (> arg 0) (skip-syntax-forward "w"))
1004 (if (octave-in-defun-p)
1005 (setq arg (- arg 1)))
1006 (if (= arg 0) (setq arg -1))
1007 (if (octave-beginning-of-defun (- arg))
1008 (octave-forward-block)))
1009
1010(defun octave-mark-defun ()
1011 "Put point at the beginning of this Octave function, mark at its end.
1012The function marked is the one containing point or following point."
1013 (interactive)
1014 (let ((pos (point)))
1015 (if (or (octave-in-defun-p)
1016 (and (octave-beginning-of-defun -1)
1017 (octave-in-defun-p)))
1018 (progn
1019 (skip-syntax-forward "w")
1020 (octave-beginning-of-defun)
1021 (push-mark (point))
1022 (octave-end-of-defun)
1023 (exchange-point-and-mark))
1024 (goto-char pos)
1025 (message "No function to mark found"))))
1026
1027\f
1028;;; Filling
1029(defun octave-auto-fill ()
1030 "Perform auto-fill in Octave mode."
1031 (if (> (current-column) (current-fill-column))
1032 (if (octave-in-comment-p)
1033 (do-auto-fill)
1034 (if (> (current-column) (current-fill-column))
1035 (let ((fill-column (- (current-fill-column)
1036 (length octave-continuation-string))))
1037 (do-auto-fill)
1038 (save-excursion
1039 (forward-line -1)
1040 (end-of-line)
1041 (insert (concat " " octave-continuation-string)))
1042 (indent-according-to-mode))))))
1043
1044(defun octave-fill-paragraph (&optional arg)
1045 "Fill paragraph of Octave code, handling Octave comments."
1046 (interactive "P")
1047 (save-excursion
1048 (let ((end (progn (forward-paragraph) (point)))
1049 (beg (progn
1050 (forward-paragraph -1)
1051 (skip-chars-forward " \t\n")
1052 (beginning-of-line)
1053 (point)))
1054 (cfc (current-fill-column))
1055 (ind (calculate-octave-indent))
1056 comment-prefix)
1057 (save-restriction
1058 (goto-char beg)
1059 (narrow-to-region beg end)
1060 (if (listp ind) (setq ind (nth 1 ind)))
1061 (while (not (eobp))
1062 (condition-case nil
1063 (octave-indent-line ind)
1064 (error nil))
1065 (if (and (> ind 0)
1066 (not
1067 (save-excursion
1068 (beginning-of-line)
1069 (looking-at "^\\s-*\\($\\|\\s<+\\)"))))
1070 (setq ind 0))
1071 (move-to-column cfc)
1072 ;; First check whether we need to combine non-empty comment lines
1073 (if (and (< (current-column) cfc)
1074 (octave-in-comment-p)
1075 (not (save-excursion
1076 (beginning-of-line)
1077 (looking-at "^\\s-*\\s<+\\s-*$"))))
1078 ;; This is a nonempty comment line which does not extend
1079 ;; past the fill column. If it is followed by an nonempty
1080 ;; comment line with the same comment prefix, try to
1081 ;; combine them, and repeat this until either we reach the
1082 ;; fill-column or there is nothing more to combine.
1083 (progn
1084 ;; Get the comment prefix
1085 (save-excursion
1086 (beginning-of-line)
1087 (while (and (re-search-forward "\\s<+")
1088 (not (octave-in-comment-p))))
1089 (setq comment-prefix (match-string 0)))
1090 ;; And keep combining ...
1091 (while (and (< (current-column) cfc)
1092 (save-excursion
1093 (forward-line 1)
1094 (and (looking-at
1095 (concat "^\\s-*"
1096 comment-prefix
1097 "\\S<"))
1098 (not (looking-at
1099 (concat "^\\s-*"
1100 comment-prefix
1101 "\\s-*$"))))))
1102 (delete-char 1)
1103 (re-search-forward comment-prefix)
1104 (delete-region (match-beginning 0) (match-end 0))
1105 (fixup-whitespace)
1106 (move-to-column cfc))))
1107 (skip-chars-forward "^ \t\n")
1108 (delete-horizontal-space)
1109 (if (or (< (current-column) cfc)
1110 (and (= (current-column) cfc) (eolp)))
1111 (forward-line 1)
1112 (if (not (eolp)) (insert " "))
1113 (octave-auto-fill))))
1114 t)))
1115
1116\f
1117;;; Completions
1118(defun octave-initialize-completions ()
1119 "Create an alist for Octave completions."
1120 (if octave-completion-alist
1121 ()
1122 (setq octave-completion-alist
1123 (mapcar '(lambda (var) (cons var var))
1124 (append octave-reserved-words
1125 octave-text-functions
1126 octave-variables)))))
1127
1128(defun octave-complete-symbol ()
1129 "Perform completion on Octave symbol preceding point.
1130Compare that symbol against Octave's reserved words and builtin
1131variables."
1132 ;; This code taken from lisp-complete-symbol
1133 (interactive)
1134 (let* ((end (point))
1135 (beg (save-excursion (backward-sexp 1) (point)))
1136 (string (buffer-substring-no-properties beg end))
1137 (completion (try-completion string octave-completion-alist)))
1138 (cond ((eq completion t)) ; ???
1139 ((null completion)
1140 (message "Can't find completion for \"%s\"" string)
1141 (ding))
1142 ((not (string= string completion))
1143 (delete-region beg end)
1144 (insert completion))
1145 (t
1146 (let ((list (all-completions string octave-completion-alist))
1147 (conf (current-window-configuration)))
1148 ;; Taken from comint.el
1149 (message "Making completion list...")
1150 (with-output-to-temp-buffer "*Completions*"
1151 (display-completion-list list))
1152 (message "Hit space to flush")
1153 (let (key first)
1154 (if (save-excursion
1155 (set-buffer (get-buffer "*Completions*"))
1156 (setq key (read-key-sequence nil)
1157 first (aref key 0))
1158 (and (consp first) (consp (event-start first))
1159 (eq (window-buffer (posn-window (event-start
1160 first)))
1161 (get-buffer "*Completions*"))
1162 (eq (key-binding key) 'mouse-choose-completion)))
1163 (progn
1164 (mouse-choose-completion first)
1165 (set-window-configuration conf))
1166 (if (eq first ?\ )
1167 (set-window-configuration conf)
1168 (setq unread-command-events
1169 (listify-key-sequence key))))))))))
1170
1171\f
1172;;; Electric characters && friends
1173(defun octave-reindent-then-newline-and-indent ()
1174 "Reindent current Octave line, insert newline, and indent the new line.
1175If Abbrev mode is on, expand abbrevs first."
1176 (interactive)
1177 (if abbrev-mode (expand-abbrev))
1178 (if octave-blink-matching-block
1179 (octave-blink-matching-block-open))
1180 (save-excursion
1181 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
1182 (indent-according-to-mode))
1183 (insert "\n")
1184 (indent-according-to-mode))
1185
1186(defun octave-electric-semi ()
1187 "Insert a semicolon in Octave mode.
1188Always reindent the line. Insert a newline if `octave-auto-newline' is
1189non-nil."
1190 (interactive)
1191 (if (not (octave-not-in-string-or-comment-p))
1192 (insert ";")
1193 (if abbrev-mode (expand-abbrev))
1194 (if octave-blink-matching-block
1195 (octave-blink-matching-block-open))
1196 (indent-according-to-mode)
1197 (insert ";")
1198 (if octave-auto-newline
1199 (newline-and-indent))))
1200
1201(defun octave-electric-space ()
1202 "Insert a space in Octave mode.
1203Maybe expand abbrevs and blink matching block open keywords."
1204 (interactive)
1205 (setq last-command-char ? )
1206 (if (not (octave-not-in-string-or-comment-p))
1207 (progn
1208 (indent-according-to-mode)
1209 (self-insert-command 1))
1210 (if abbrev-mode (expand-abbrev))
1211 (if octave-blink-matching-block
1212 (octave-blink-matching-block-open))
1213 (if (save-excursion
1214 (skip-syntax-backward " ")
1215 (not (bolp)))
1216 (indent-according-to-mode))
1217 (self-insert-command 1)))
1218
1219(defun octave-abbrev-start ()
1220 "Start entering an Octave abbreviation.
1221If Abbrev mode is turned on, typing ` (grave accent) followed by ? or
1222\\[help-command] lists all Octave abbrevs. Any other key combination is
1223executed normally.
1224Note that all Octave mode abbrevs start with a grave accent."
1225 (interactive)
1226 (if (not abbrev-mode)
1227 (self-insert-command 1)
1228 (let (c)
1229 (insert last-command-char)
1230 (if (or (eq (setq c (read-event)) ??)
1231 (eq c help-char))
1232 (let ((abbrev-table-name-list '(octave-mode-abbrev-table)))
1233 (list-abbrevs))
1234 (setq unread-command-events (list c))))))
1235
1236(defun octave-insert-defun (name args vals)
1237 "Insert an Octave function skeleton.
1238Prompt for the function's name, arguments and return values (to be
1239entered without parens)."
1240 (interactive
1241 (list
1242 (read-from-minibuffer "Function name: "
1243 (substring (buffer-name) 0 -2))
1244 (read-from-minibuffer "Arguments: ")
1245 (read-from-minibuffer "Return values: ")))
1246 (let ((string (format "%s %s (%s)"
1247 (cond
1248 ((string-equal vals "")
1249 vals)
1250 ((string-match "[ ,]" vals)
1251 (concat " [" vals "] ="))
1252 (t
1253 (concat " " vals " =")))
1254 name
1255 args))
1256 (prefix octave-block-comment-start))
1257 (if (not (bobp)) (newline))
1258 (insert "function" string)
1259 (indent-according-to-mode)
1260 (newline 2)
1261 (insert prefix "usage: " string)
1262 (reindent-then-newline-and-indent)
1263 (insert prefix)
1264 (reindent-then-newline-and-indent)
1265 (insert prefix)
1266 (indent-according-to-mode)
1267 (save-excursion
1268 (newline 2)
1269 (insert "endfunction")
1270 (indent-according-to-mode))))
1271
1272\f
1273;;; Menu
1274(defun octave-add-octave-menu ()
1275 "Adds the `Octave' menu to the menu bar in Octave mode."
1276 (require 'easymenu)
1277 (easy-menu-define octave-mode-menu-map octave-mode-map
1278 "Menu keymap for Octave mode." octave-mode-menu)
1279 (easy-menu-add octave-mode-menu-map octave-mode-map))
1280
1281\f
1282;;; Communication with the inferior Octave process
1283(defun octave-kill-process ()
1284 "Kill inferior Octave process and its buffer."
1285 (interactive)
1286 (if inferior-octave-process
1287 (progn
1288 (process-send-string inferior-octave-process "quit;\n")
1289 (accept-process-output inferior-octave-process)))
1290 (if inferior-octave-buffer
1291 (kill-buffer inferior-octave-buffer)))
1292
1293(defun octave-show-process-buffer ()
1294 "Make sure that `inferior-octave-buffer' is displayed."
1295 (interactive)
1296 (if (get-buffer inferior-octave-buffer)
1297 (display-buffer inferior-octave-buffer)
1298 (message "No buffer named %s" inferior-octave-buffer)))
1299
1300(defun octave-hide-process-buffer ()
1301 "Delete all windows that display `inferior-octave-buffer'."
1302 (interactive)
1303 (if (get-buffer inferior-octave-buffer)
1304 (delete-windows-on inferior-octave-buffer)
1305 (message "No buffer named %s" inferior-octave-buffer)))
1306
1307(defun octave-send-region (beg end)
1308 "Send current region to the inferior Octave process."
1309 (interactive "r")
1310 (inferior-octave t)
1311 (let ((proc inferior-octave-process)
1312 (string (buffer-substring-no-properties beg end))
1313 line)
1314 (save-excursion
1315 (set-buffer inferior-octave-buffer)
1316 (setq inferior-octave-output-list nil)
1317 (while (not (string-equal string ""))
1318 (if (string-match "\n" string)
1319 (setq line (substring string 0 (match-beginning 0))
1320 string (substring string (match-end 0)))
1321 (setq line string string ""))
1322 (setq inferior-octave-receive-in-progress t)
1323 (inferior-octave-send-list-and-digest (list (concat line "\n")))
1324 (while inferior-octave-receive-in-progress
1325 (accept-process-output proc))
1326 (insert-before-markers
1327 (mapconcat 'identity
1328 (append
1329 (if octave-send-echo-input (list line) (list ""))
1330 (mapcar 'inferior-octave-strip-ctrl-g
1331 inferior-octave-output-list)
1332 (list inferior-octave-output-string))
1333 "\n")))))
1334 (if octave-send-show-buffer
1335 (display-buffer inferior-octave-buffer)))
1336
1337(defun octave-send-block ()
1338 "Send current Octave block to the inferior Octave process."
1339 (interactive)
1340 (save-excursion
1341 (octave-mark-block)
1342 (octave-send-region (point) (mark))))
1343
1344(defun octave-send-defun ()
1345 "Send current Octave function to the inferior Octave process."
1346 (interactive)
1347 (save-excursion
1348 (octave-mark-defun)
1349 (octave-send-region (point) (mark))))
1350
1351(defun octave-send-line (&optional arg)
1352 "Send current Octave code line to the inferior Octave process.
1353With positive prefix ARG, send that many lines.
1354If `octave-send-line-auto-forward' is non-nil, go to the next unsent
1355code line."
1356 (interactive "P")
1357 (or arg (setq arg 1))
1358 (if (> arg 0)
1359 (let (beg end)
1360 (beginning-of-line)
1361 (setq beg (point))
1362 (octave-next-code-line (- arg 1))
1363 (end-of-line)
1364 (setq end (point))
1365 (if octave-send-line-auto-forward
1366 (octave-next-code-line 1))
1367 (octave-send-region beg end))))
1368
1369(defun octave-eval-print-last-sexp ()
1370 "Evaluate Octave sexp before point and print value into current buffer."
1371 (interactive)
1372 (inferior-octave t)
1373 (let ((standard-output (current-buffer))
1374 (print-escape-newlines nil)
1375 (opoint (point)))
1376 (terpri)
1377 (prin1
1378 (save-excursion
1379 (forward-sexp -1)
1380 (inferior-octave-send-list-and-digest
1381 (list (concat (buffer-substring-no-properties (point) opoint)
1382 "\n")))
1383 (mapconcat 'identity inferior-octave-output-list "\n")))
1384 (terpri)))
d1e49742 1385\f
d1e49742
RS
1386;;; Bug reporting
1387(defun octave-submit-bug-report ()
1388 "Submit a bug report on the Emacs Octave package via mail."
1389 (interactive)
1390 (require 'reporter)
1391 (and
1392 (y-or-n-p "Do you want to submit a bug report? ")
1393 (reporter-submit-bug-report
1394 octave-maintainer-address
1395 (concat "Emacs version " emacs-version)
1396 (list
1397 'octave-auto-newline
1398 'octave-blink-matching-block
1399 'octave-block-offset
1400 'octave-comment-char
1401 'octave-continuation-offset
1402 'octave-continuation-string
1403 'octave-help-files
1404 'octave-mode-startup-message
1405 'octave-send-echo-input
1406 'octave-send-line-auto-forward
1407 'octave-send-show-buffer))))
1408
1409;;; provide ourself
1410
e7017ef9 1411(provide 'octave-mod)
d1e49742
RS
1412
1413;;; octave-mod.el ends here