1 ;;; octave-mod.el --- editing Octave source files under Emacs
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
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>
10 ;; This file is part of GNU Emacs.
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)
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.
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.
29 ;; This package provides Emacs support for Octave.
30 ;; It defines Octave mode, a major mode for editing
33 ;; The file octave-hlp.el provides `octave-help', a facility for looking up
34 ;; documentation on a symbol in the Octave info files.
36 ;; The file octave-inf.el contains code for interacting with an inferior
37 ;; Octave process using comint.
39 ;; See the documentation of `octave-mode', `octave-help' and
40 ;; `run-octave' for further information on usage and customization.
44 (defvar inferior-octave-output-list nil)
45 (defvar inferior-octave-output-string nil)
46 (defvar inferior-octave-receive-in-progress nil)
48 (defconst octave-maintainer-address
49 "Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>, bug-gnu-emacs@prep.ai.mit.edu"
50 "Current maintainer of the Emacs Octave package.")
52 (defvar octave-abbrev-table nil
53 "Abbrev table for Octave's reserved words.
54 Used in octave-mode and inferior-octave-mode buffers.
55 All Octave abbrevs start with a grave accent (`).")
56 (if octave-abbrev-table
58 (let ((ac abbrevs-changed))
59 (define-abbrev-table 'octave-abbrev-table ())
60 (define-abbrev octave-abbrev-table "`a" "all_va_args" nil)
61 (define-abbrev octave-abbrev-table "`b" "break" nil)
62 (define-abbrev octave-abbrev-table "`cs" "case" nil)
63 (define-abbrev octave-abbrev-table "`ca" "catch" nil)
64 (define-abbrev octave-abbrev-table "`c" "continue" nil)
65 (define-abbrev octave-abbrev-table "`el" "else" nil)
66 (define-abbrev octave-abbrev-table "`eli" "elseif" nil)
67 (define-abbrev octave-abbrev-table "`et" "end_try_catch" nil)
68 (define-abbrev octave-abbrev-table "`eu" "end_unwind_protect" nil)
69 (define-abbrev octave-abbrev-table "`ef" "endfor" nil)
70 (define-abbrev octave-abbrev-table "`efu" "endfunction" nil)
71 (define-abbrev octave-abbrev-table "`ei" "endif" nil)
72 (define-abbrev octave-abbrev-table "`es" "endswitch" nil)
73 (define-abbrev octave-abbrev-table "`ew" "endwhile" nil)
74 (define-abbrev octave-abbrev-table "`f" "for" nil)
75 (define-abbrev octave-abbrev-table "`fu" "function" nil)
76 (define-abbrev octave-abbrev-table "`gl" "global" nil)
77 (define-abbrev octave-abbrev-table "`gp" "gplot" nil)
78 (define-abbrev octave-abbrev-table "`gs" "gsplot" nil)
79 (define-abbrev octave-abbrev-table "`if" "if ()" nil)
80 (define-abbrev octave-abbrev-table "`o" "otherwise" nil)
81 (define-abbrev octave-abbrev-table "`rp" "replot" nil)
82 (define-abbrev octave-abbrev-table "`r" "return" nil)
83 (define-abbrev octave-abbrev-table "`s" "switch" nil)
84 (define-abbrev octave-abbrev-table "`t" "try" nil)
85 (define-abbrev octave-abbrev-table "`up" "unwind_protect" nil)
86 (define-abbrev octave-abbrev-table "`upc" "unwind_protect_cleanup" nil)
87 (define-abbrev octave-abbrev-table "`w" "while ()" nil)
88 (setq abbrevs-changed ac)))
90 (defvar octave-comment-char ?#
91 "Character to start an Octave comment.")
92 (defvar octave-comment-start
93 (concat (make-string 1 octave-comment-char) " ")
94 "String to insert to start a new Octave in-line comment.")
95 (defvar octave-comment-start-skip "\\s<+\\s-*"
96 "Regexp to match the start of an Octave comment up to its body.")
98 (defvar octave-begin-keywords
99 '("for" "function" "if" "switch" "try" "unwind_protect" "while"))
100 (defvar octave-else-keywords
101 '("case" "catch" "else" "elseif" "otherwise" "unwind_protect_cleanup"))
102 (defvar octave-end-keywords
103 '("end" "endfor" "endfunction" "endif" "endswitch" "end_try_catch"
104 "end_unwind_protect" "endwhile"))
106 (defvar octave-reserved-words
107 (append octave-begin-keywords
110 '("all_va_args" "break" "continue" "global" "gplot" "gsplot"
112 "Reserved words in Octave.")
114 (defvar octave-text-functions
115 '("casesen" "cd" "chdir" "clear" "diary" "dir" "document" "echo"
116 "edit_history" "format" "gset" "gshow" "help" "history" "hold"
117 "load" "ls" "more" "run_history" "save" "set" "show" "type"
118 "which" "who" "whos")
119 "Text functions in Octave (these names are also reserved).")
121 (defvar octave-variables
122 '("EDITOR" "EXEC_PATH" "F_DUPFD" "F_GETFD" "F_GETFL" "F_SETFD"
123 "F_SETFL" "I" "IMAGEPATH" "INFO_FILE" "INFO_PROGRAM" "Inf" "J"
124 "LOADPATH" "NaN" "OCTAVE_VERSION" "O_APPEND" "O_CREAT" "O_EXCL"
125 "O_NONBLOCK" "O_RDONLY" "O_RDWR" "O_TRUNC" "O_WRONLY" "PAGER" "PS1"
126 "PS2" "PS4" "PWD" "SEEK_CUR" "SEEK_END" "SEEK_SET" "__F_DUPFD__"
127 "__F_GETFD__" "__F_GETFL__" "__F_SETFD__" "__F_SETFL__" "__I__"
128 "__Inf__" "__J__" "__NaN__" "__OCTAVE_VERSION__" "__O_APPEND__"
129 "__O_CREAT__" "__O_EXCL__" "__O_NONBLOCK__" "__O_RDONLY__"
130 "__O_RDWR__" "__O_TRUNC__" "__O_WRONLY__" "__PWD__" "__SEEK_CUR__"
131 "__SEEK_END__" "__SEEK_SET__" "__argv__" "__e__" "__eps__"
132 "__error_text__" "__i__" "__inf__" "__j__" "__nan__" "__pi__"
133 "__program_invocation_name__" "__program_name__" "__realmax__"
134 "__realmin__" "__stderr__" "__stdin__" "__stdout__" "ans" "argv"
135 "automatic_replot" "beep_on_error" "completion_append_char"
136 "default_return_value" "default_save_format"
137 "define_all_return_values" "do_fortran_indexing" "e"
138 "echo_executing_commands" "empty_list_elements_ok" "eps"
139 "error_text" "gnuplot_binary" "gnuplot_has_multiplot" "history_file"
140 "history_size" "ignore_function_time_stamp" "implicit_str_to_num_ok"
141 "inf" "nan" "nargin" "ok_to_lose_imaginary_part"
142 "output_max_field_width" "output_precision"
143 "page_output_immediately" "page_screen_output" "pi"
144 "prefer_column_vectors" "prefer_zero_one_indexing"
145 "print_answer_id_name" "print_empty_dimensions"
146 "program_invocation_name" "program_name" "propagate_empty_matrices"
147 "realmax" "realmin" "resize_on_range_error"
148 "return_last_computed_value" "save_precision" "saving_history"
149 "silent_functions" "split_long_rows" "stderr" "stdin" "stdout"
150 "string_fill_char" "struct_levels_to_print"
151 "suppress_verbose_help_message" "treat_neg_dim_as_zero"
152 "warn_assign_as_truth_value" "warn_comma_in_global_decl"
153 "warn_divide_by_zero" "warn_function_name_clash"
154 "warn_missing_semicolon" "whitespace_in_literal_matrix")
155 "Builtin variables in Octave.")
157 (defvar octave-function-header-regexp
158 (concat "^\\s-*\\<\\(function\\)\\>"
159 "\\([^=;\n]*=[ \t]*\\|[ \t]*\\)\\(\\w+\\)\\>")
160 "Regexp to match an Octave function header.
161 The string `function' and its name are given by the first and third
162 parenthetical grouping.")
164 (defvar octave-font-lock-keywords
166 ;; Fontify all builtin keywords.
167 (cons (concat "\\<\\("
168 (mapconcat 'identity octave-reserved-words "\\|")
169 (mapconcat 'identity octave-text-functions "\\|")
171 'font-lock-keyword-face)
172 ;; Fontify all builtin operators.
173 (cons "\\(&\\||\\|<=\\|>=\\|==\\|<\\|>\\|!=\\|!\\)"
174 'font-lock-reference-face)
175 ;; Fontify all builtin variables.
176 (cons (concat "\\<\\("
177 (mapconcat 'identity octave-variables "\\|")
179 'font-lock-variable-name-face)
180 ;; Fontify all function declarations.
181 (list octave-function-header-regexp
182 '(1 font-lock-keyword-face)
183 '(3 font-lock-function-name-face nil t)))
184 "Additional Octave expressions to highlight.")
186 (defvar inferior-octave-buffer "*Inferior Octave*"
187 "*Name of buffer for running an inferior Octave process.")
189 (defvar inferior-octave-process nil)
191 (defvar octave-mode-map nil
192 "Keymap used in Octave mode.")
195 (let ((map (make-sparse-keymap)))
196 (define-key map "`" 'octave-abbrev-start)
197 (define-key map ";" 'octave-electric-semi)
198 (define-key map " " 'octave-electric-space)
199 (define-key map "\n" 'octave-reindent-then-newline-and-indent)
200 (define-key map "\t" 'indent-according-to-mode)
201 (define-key map "\e;" 'octave-indent-for-comment)
202 (define-key map "\e\n" 'octave-indent-new-comment-line)
203 (define-key map "\e\t" 'octave-complete-symbol)
204 (define-key map "\M-\C-a" 'octave-beginning-of-defun)
205 (define-key map "\M-\C-e" 'octave-end-of-defun)
206 (define-key map "\M-\C-h" 'octave-mark-defun)
207 (define-key map "\M-\C-q" 'octave-indent-defun)
208 (define-key map "\C-c;" 'octave-comment-region)
209 (define-key map "\C-c:" 'octave-uncomment-region)
210 (define-key map "\C-c\C-b" 'octave-submit-bug-report)
211 (define-key map "\C-c\C-p" 'octave-previous-code-line)
212 (define-key map "\C-c\C-n" 'octave-next-code-line)
213 (define-key map "\C-c\C-a" 'octave-beginning-of-line)
214 (define-key map "\C-c\C-e" 'octave-end-of-line)
215 (define-key map "\C-c\M-\C-n" 'octave-forward-block)
216 (define-key map "\C-c\M-\C-p" 'octave-backward-block)
217 (define-key map "\C-c\M-\C-u" 'octave-backward-up-block)
218 (define-key map "\C-c\M-\C-d" 'octave-down-block)
219 (define-key map "\C-c\M-\C-h" 'octave-mark-block)
220 (define-key map "\C-c]" 'octave-close-block)
221 (define-key map "\C-cf" 'octave-insert-defun)
222 (define-key map "\C-c\C-h" 'octave-help)
223 (define-key map "\C-cil" 'octave-send-line)
224 (define-key map "\C-cib" 'octave-send-block)
225 (define-key map "\C-cif" 'octave-send-defun)
226 (define-key map "\C-cir" 'octave-send-region)
227 (define-key map "\C-cis" 'octave-show-process-buffer)
228 (define-key map "\C-cih" 'octave-hide-process-buffer)
229 (define-key map "\C-cik" 'octave-kill-process)
230 (setq octave-mode-map map)))
232 (defvar octave-mode-menu
235 ["Previous Code Line" octave-previous-code-line t]
236 ["Next Code Line" octave-next-code-line t]
237 ["Begin of Continuation" octave-beginning-of-line t]
238 ["End of Continuation" octave-end-of-line t]
239 ["Split Line at Point" octave-indent-new-comment-line t])
241 ["Next Block" octave-forward-block t]
242 ["Previous Block" octave-backward-block t]
243 ["Down Block" octave-down-block t]
244 ["Up Block" octave-backward-up-block t]
245 ["Mark Block" octave-mark-block t]
246 ["Close Block" octave-close-block t])
248 ["Begin of Function" octave-beginning-of-defun t]
249 ["End of Function" octave-end-of-defun t]
250 ["Mark Function" octave-mark-defun t]
251 ["Indent Function" octave-indent-defun t]
252 ["Insert Function" octave-insert-defun t])
255 ["Send Current Line" octave-send-line t]
256 ["Send Current Block" octave-send-block t]
257 ["Send Current Function" octave-send-defun t]
258 ["Send Region" octave-send-region t]
259 ["Show Process Buffer" octave-show-process-buffer t]
260 ["Hide Process Buffer" octave-hide-process-buffer t]
261 ["Kill Process" octave-kill-process t])
263 ["Indent Line" indent-according-to-mode t]
264 ["Complete Symbol" octave-complete-symbol t]
266 ["Toggle Abbrev Mode" abbrev-mode t]
267 ["Toggle Auto-Fill Mode" auto-fill-mode t]
269 ["Submit Bug Report" octave-submit-bug-report t]
271 ["Describe Octave Mode" octave-describe-major-mode t]
272 ["Lookup Octave Index" octave-help t])
273 "Menu for Octave mode.")
275 (defvar octave-mode-syntax-table nil
276 "Syntax table in use in octave-mode buffers.")
277 (if octave-mode-syntax-table
279 (let ((table (make-syntax-table)))
280 (modify-syntax-entry ?\r " " 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 ?| "." table)
290 (modify-syntax-entry ?! "." table)
291 (modify-syntax-entry ?\\ "\\" table)
292 (modify-syntax-entry ?\' "." table)
293 (modify-syntax-entry ?\` "w" table)
294 (modify-syntax-entry ?\" "\"" table)
295 (modify-syntax-entry ?. "w" table)
296 (modify-syntax-entry ?_ "w" table)
297 (modify-syntax-entry ?\% "." table)
298 (modify-syntax-entry ?\# "<" table)
299 (modify-syntax-entry ?\n ">" table)
300 (setq octave-mode-syntax-table table)))
302 (defvar octave-auto-newline nil
303 "*Non-nil means automatically newline after a semicolon in Octave mode.")
305 (defvar octave-blink-matching-block t
306 "*Control the blinking of matching Octave block keywords.
307 Non-nil means show matching begin of block when inserting a space,
308 newline or semicolon after an else or end keyword.")
309 (defvar octave-block-offset 2
310 "*Extra indentation applied to statements in Octave block structures.")
312 (defvar octave-block-begin-regexp
314 (mapconcat 'identity octave-begin-keywords "\\|")
316 (defvar octave-block-else-regexp
318 (mapconcat 'identity octave-else-keywords "\\|")
320 (defvar octave-block-end-regexp
322 (mapconcat 'identity octave-end-keywords "\\|")
324 (defvar octave-block-begin-or-end-regexp
325 (concat octave-block-begin-regexp "\\|" octave-block-end-regexp))
326 (defvar octave-block-else-or-end-regexp
327 (concat octave-block-else-regexp "\\|" octave-block-end-regexp))
328 (defvar octave-block-match-alist
329 '(("for" . ("end" "endfor"))
330 ("function" . ("end" "endfunction"))
331 ("if" . ("else" "elseif" "end" "endif"))
332 ("switch" . ("case" "end" "endswitch" "otherwise"))
333 ("try" . ("catch" "end" "end_try_catch"))
334 ("unwind_protect" . ("unwind_protect_cleanup" "end"
335 "end_unwind_protect"))
336 ("while" . ("end" "endwhile")))
337 "Alist with Octave's matching block keywords.
338 Has Octave's begin keywords as keys and a list of the matching else or
339 end keywords as associated values.")
341 (defvar octave-block-comment-start
342 (concat (make-string 2 octave-comment-char) " ")
343 "String to insert to start a new Octave comment on an empty line.")
345 (defvar octave-continuation-offset 4
346 "*Extra indentation applied to Octave continuation lines.")
347 (defvar octave-continuation-regexp
348 "[^#%\n]*\\(\\\\\\|\\.\\.\\.\\)\\s-*\\(\\s<.*\\)?$")
349 (defvar octave-continuation-string "\\"
350 "*Character string used for Octave continuation lines. Normally \\.")
352 (defvar octave-completion-alist nil
353 "Alist of Octave symbols for completion in Octave mode.
354 Each element looks like (VAR . VAR), where the car and cdr are the same
355 symbol (an Octave command or variable name).
356 Currently, only builtin variables can be completed.")
358 (defvar octave-mode-imenu-generic-expression
361 (list nil octave-function-header-regexp 3))
362 "Imenu expression for Octave mode. See `imenu-generic-expression'.")
364 (defvar octave-mode-startup-message t
365 "*Nil means do not display the Octave mode startup message.")
367 (defvar octave-mode-hook nil
368 "*Hook to be run when Octave mode is started.")
370 (defvar octave-send-show-buffer t
371 "*Non-nil means display `inferior-octave-buffer' after sending to it.")
372 (defvar octave-send-line-auto-forward t
373 "*Control auto-forward after sending to the inferior Octave process.
374 Non-nil means always go to the next Octave code line after sending.")
375 (defvar octave-send-echo-input t
376 "*Non-nil means echo input sent to the inferior Octave process.")
380 (defun octave-mode ()
381 "Major mode for editing Octave code.
383 This mode makes it easier to write Octave code by helping with
384 indentation, doing some of the typing for you (with Abbrev mode) and by
385 showing keywords, comments, strings, etc. in different faces (with
386 Font Lock mode on terminals that support it).
388 Octave itself is a high-level language, primarily intended for numerical
389 computations. It provides a convenient command line interface for
390 solving linear and nonlinear problems numerically. Function definitions
391 can also be stored in files, and it can be used in a batch mode (which
392 is why you need this mode!).
394 The latest released version of Octave is always available via anonymous
395 ftp from bevo.che.wisc.edu in the directory `/pub/octave'. Complete
396 source and binaries for several popular systems are available.
398 Type \\[list-abbrevs] to display the built-in abbrevs for Octave keywords.
405 Variables you can use to customize Octave mode
406 ==============================================
409 Non-nil means auto-insert a newline and indent after a semicolon.
412 octave-blink-matching-block
413 Non-nil means show matching begin of block when inserting a space,
414 newline or semicolon after an else or end keyword. Default is t.
417 Extra indentation applied to statements in block structures.
420 octave-continuation-offset
421 Extra indentation applied to Octave continuation lines.
424 octave-continuation-string
425 String used for Octave continuation lines.
426 Default is a backslash.
428 octave-mode-startup-message
429 Nil means do not display the Octave mode startup message.
432 octave-send-echo-input
433 Non-nil means always display `inferior-octave-buffer' after sending a
434 command to the inferior Octave process.
436 octave-send-line-auto-forward
437 Non-nil means always go to the next unsent line of Octave code after
438 sending a line to the inferior Octave process.
440 octave-send-echo-input
441 Non-nil means echo input sent to the inferior Octave process.
443 Turning on Octave mode runs the hook `octave-mode-hook'.
445 To begin using this mode for all `.m' files that you edit, add the
446 following lines to your `.emacs' file:
448 (autoload 'octave-mode \"octave-mod\" nil t)
449 (setq auto-mode-alist
450 (cons '(\"\\\\.m$\" . octave-mode) auto-mode-alist))
452 To automatically turn on the abbrev, auto-fill and font-lock features,
453 add the following lines to your `.emacs' file as well:
455 (add-hook 'octave-mode-hook
459 (if (eq window-system 'x)
460 (font-lock-mode 1))))
462 To submit a problem report, enter \\[octave-submit-bug-report] from \
463 an Octave mode buffer.
464 This automatically sets up a mail buffer with version information
465 already added. You just need to add a description of the problem,
466 including a reproducible test case and send the message."
468 (kill-all-local-variables)
470 (use-local-map octave-mode-map)
471 (setq major-mode 'octave-mode)
472 (setq mode-name "Octave")
473 (setq local-abbrev-table octave-abbrev-table)
474 (set-syntax-table octave-mode-syntax-table)
476 (make-local-variable 'indent-line-function)
477 (setq indent-line-function 'octave-indent-line)
479 (make-local-variable 'comment-start)
480 (setq comment-start octave-comment-start)
481 (make-local-variable 'comment-end)
482 (setq comment-end "")
483 (make-local-variable 'comment-column)
484 (setq comment-column 32)
485 (make-local-variable 'comment-start-skip)
486 (setq comment-start-skip "\\s<+\\s-*")
487 (make-local-variable 'comment-indent-function)
488 (setq comment-indent-function 'octave-comment-indent)
490 (make-local-variable 'parse-sexp-ignore-comments)
491 (setq parse-sexp-ignore-comments t)
492 (make-local-variable 'paragraph-start)
493 (setq paragraph-start (concat "\\s-*$\\|" page-delimiter))
494 (make-local-variable 'paragraph-separate)
495 (setq paragraph-separate paragraph-start)
496 (make-local-variable 'paragraph-ignore-fill-prefix)
497 (setq paragraph-ignore-fill-prefix t)
498 (make-local-variable 'fill-paragraph-function)
499 (setq fill-paragraph-function 'octave-fill-paragraph)
500 (make-local-variable 'adaptive-fill-regexp)
501 (setq adaptive-fill-regexp nil)
502 (make-local-variable 'fill-column)
503 (setq fill-column 72)
504 (make-local-variable 'normal-auto-fill-function)
505 (setq normal-auto-fill-function 'octave-auto-fill)
507 (make-local-variable 'font-lock-defaults)
508 (setq font-lock-defaults '(octave-font-lock-keywords nil nil))
510 (make-local-variable 'imenu-generic-expression)
511 (setq imenu-generic-expression octave-mode-imenu-generic-expression)
513 (octave-add-octave-menu)
514 (octave-initialize-completions)
515 (run-hooks 'octave-mode-hook))
517 ;;; Miscellaneous useful functions
518 (defun octave-describe-major-mode ()
519 "Describe the current major mode."
521 (describe-function major-mode))
523 (defun octave-point (position)
524 "Returns the value of point at certain positions."
527 ((eq position 'bol) (beginning-of-line))
528 ((eq position 'eol) (end-of-line))
529 ((eq position 'boi) (back-to-indentation))
530 ((eq position 'bonl) (forward-line 1))
531 ((eq position 'bopl) (forward-line -1))
532 (t (error "unknown buffer position requested: %s" position)))
535 (defsubst octave-in-comment-p ()
536 "Returns t if point is inside an Octave comment, nil otherwise."
539 (nth 4 (parse-partial-sexp (octave-point 'bol) (point)))))
541 (defsubst octave-in-string-p ()
542 "Returns t if point is inside an Octave string, nil otherwise."
545 (nth 3 (parse-partial-sexp (octave-point 'bol) (point)))))
547 (defsubst octave-not-in-string-or-comment-p ()
548 "Returns t iff point is not inside an Octave string or comment."
549 (let ((pps (parse-partial-sexp (octave-point 'bol) (point))))
550 (not (or (nth 3 pps) (nth 4 pps)))))
552 (defun octave-in-block-p ()
553 "Returns t if point is inside an Octave block, nil otherwise.
554 The block is taken to start at the first letter of the begin keyword and
555 to end after the end keyword."
560 (skip-syntax-forward "w")
562 (octave-forward-block)
567 (defun octave-in-defun-p ()
568 "Returns t iff point is inside an Octave function declaration.
569 The function is taken to start at the `f' of `function' and to end after
573 (or (and (looking-at "\\<function\\>")
574 (octave-not-in-string-or-comment-p))
575 (and (octave-beginning-of-defun)
578 (octave-forward-block)
584 (defun octave-comment-region (beg end &optional arg)
585 "Comment or uncomment each line in the region as Octave code.
586 See `comment-region'."
588 (let ((comment-start (char-to-string octave-comment-char)))
589 (comment-region beg end arg)))
591 (defun octave-uncomment-region (beg end &optional arg)
592 "Uncomment each line in the region as Octave code."
594 (or arg (setq arg 1))
595 (octave-comment-region beg end (- arg)))
599 (defun calculate-octave-indent ()
600 "Return appropriate indentation for current line as Octave code.
601 Returns an integer (the column to indent to) unless the line is a
602 comment line with fixed goal golumn. In that case, returns a list whose
603 car is the column to indent to, and whose cdr is the current indentation
605 (let ((is-continuation-line
607 (if (zerop (octave-previous-code-line))
608 (looking-at octave-continuation-regexp))))
612 ;; If we can move backward out one level of parentheses, take 1
613 ;; plus the indentation of that parenthesis. Otherwise, go back
614 ;; to the beginning of the previous code line, and compute the
615 ;; offset this line gives.
616 (if (condition-case nil
621 (setq icol (+ 1 (current-column)))
622 (if (zerop (octave-previous-code-line))
624 (octave-beginning-of-line)
625 (back-to-indentation)
626 (setq icol (current-column))
628 (eol (octave-point 'eol)))
629 (while (< (point) eol)
630 (if (octave-not-in-string-or-comment-p)
632 ((looking-at "\\<switch\\>")
633 (setq icol (+ icol (* 2 octave-block-offset))))
634 ((looking-at octave-block-begin-regexp)
635 (setq icol (+ icol octave-block-offset)))
636 ((looking-at octave-block-else-regexp)
638 (setq icol (+ icol octave-block-offset))))
639 ((looking-at octave-block-end-regexp)
640 (if (not (= bot (point)))
642 (octave-block-end-offset)))))))
644 (if is-continuation-line
645 (setq icol (+ icol octave-continuation-offset)))))))
647 (back-to-indentation)
649 ((and (looking-at octave-block-else-regexp)
650 (octave-not-in-string-or-comment-p))
651 (setq icol (- icol octave-block-offset)))
652 ((and (looking-at octave-block-end-regexp)
653 (octave-not-in-string-or-comment-p))
654 (setq icol (- icol (octave-block-end-offset))))
655 ((looking-at "\\s<\\s<\\s<\\S<")
656 (setq icol (list 0 icol)))
657 ((looking-at "\\s<\\S<")
658 (setq icol (list comment-column icol)))))
661 (defun octave-block-end-offset ()
663 (octave-backward-up-block 1)
664 (* octave-block-offset
665 (if (string-match (match-string 0) "switch") 2 1))))
667 (defun octave-comment-indent ()
668 (if (looking-at "\\s<\\s<\\s<")
670 (if (looking-at "\\s<\\s<")
671 (calculate-octave-indent)
672 (skip-syntax-backward " ")
673 (max (if (bolp) 0 (+ 1 (current-column)))
676 (defun octave-indent-for-comment ()
677 "Maybe insert and indent an Octave comment.
678 If there is no comment already on this line, create a code-level comment
679 (started by two comment characters) if the line is empty, or an in-line
680 comment (started by one comment character) otherwise.
681 Point is left after the start of the comment which is properly aligned."
684 (indent-according-to-mode))
686 (defun octave-indent-line (&optional arg)
687 "Indent current line as Octave code.
688 With optional ARG, use this as offset unless this line is a comment with
691 (or arg (setq arg 0))
692 (let ((icol (calculate-octave-indent))
693 (relpos (- (current-column) (current-indentation))))
695 (setq icol (car icol))
696 (setq icol (+ icol arg)))
698 (error "Unmatched end keyword")
699 (indent-line-to icol)
701 (move-to-column (+ icol relpos))))))
703 (defun octave-indent-new-comment-line ()
704 "Break Octave line at point, continuing comment if within one.
705 If within code, insert `octave-continuation-string' before breaking the
706 line. If within a string, signal an error.
707 The new line is properly indented."
709 (delete-horizontal-space)
711 ((octave-in-comment-p)
712 (indent-new-comment-line))
713 ((octave-in-string-p)
714 (error "Cannot split a code line inside a string"))
716 (insert (concat " " octave-continuation-string))
717 (octave-reindent-then-newline-and-indent))))
719 (defun octave-indent-defun ()
720 "Properly indents the Octave function which contains point."
724 (message "Indenting function...")
725 (indent-region (point) (mark) nil))
726 (message "Indenting function...done."))
730 (defun octave-next-code-line (&optional arg)
731 "Move ARG lines of Octave code forward (backward if ARG is negative).
732 Skips past all empty and comment lines. Default for ARG is 1.
734 On success, return 0. Otherwise, go as far as possible and return -1."
736 (or arg (setq arg 1))
739 (inc (if (> arg 0) 1 -1)))
740 (while (and (/= arg 0) (= n 0))
741 (setq n (forward-line inc))
743 (looking-at "\\s-*\\($\\|\\s<\\)"))
744 (setq n (forward-line inc)))
745 (setq arg (- arg inc)))
748 (defun octave-previous-code-line (&optional arg)
749 "Move ARG lines of Octave code backward (forward if ARG is negative).
750 Skips past all empty and comment lines. Default for ARG is 1.
752 On success, return 0. Otherwise, go as far as possible and return -1."
754 (or arg (setq arg 1))
755 (octave-next-code-line (- arg)))
757 (defun octave-beginning-of-line ()
758 "Move point to beginning of current Octave line.
759 If on an empty or comment line, go to the beginning of that line.
760 Otherwise, move backward to the beginning of the first Octave code line
761 which is not inside a continuation statement, i.e., which does not
762 follow a code line ending in `...' or `\\', or is inside an open
766 (if (not (looking-at "\\s-*\\($\\|\\s<\\)"))
767 (while (or (condition-case nil
773 (and (or (looking-at "\\s-*\\($\\|\\s<\\)")
775 (if (zerop (octave-previous-code-line))
776 (looking-at octave-continuation-regexp))))
777 (zerop (forward-line -1)))))))
779 (defun octave-end-of-line ()
780 "Move point to end of current Octave line.
781 If on an empty or comment line, go to the end of that line.
782 Otherwise, move forward to the end of the first Octave code line which
783 does not end in `...' or `\\' or is inside an open parenthesis list."
788 (looking-at "\\s-*\\($\\|\\s<\\)"))
790 (while (or (condition-case nil
798 (or (looking-at "\\s-*\\($\\|\\s<\\)")
799 (looking-at octave-continuation-regexp)))
800 (zerop (forward-line 1)))))
803 (defun octave-scan-blocks (from count depth)
804 "Scan from character number FROM by COUNT Octave begin-end blocks.
805 Returns the character number of the position thus found.
807 If DEPTH is nonzero, block depth begins counting from that value.
808 Only places where the depth in blocks becomes zero are candidates for
809 stopping; COUNT such places are counted.
811 If the beginning or end of the buffer is reached and the depth is wrong,
812 an error is signaled."
813 (let ((min-depth (if (> depth 0) 0 depth))
814 (inc (if (> count 0) 1 -1)))
818 (while (or (re-search-forward
819 octave-block-begin-or-end-regexp nil 'move inc)
821 (error "Unbalanced block")))
822 (if (octave-not-in-string-or-comment-p)
826 (setq depth (+ depth inc)))
828 (setq depth (- depth inc))))
829 (if (< depth min-depth)
830 (error "Containing expression ends prematurely"))
832 (throw 'foo nil))))))
833 (setq count (- count inc)))
836 (defun octave-forward-block (&optional arg)
837 "Move forward across one balanced Octave begin-end block.
838 With argument, do it that many times.
839 Negative arg -N means move backward across N blocks."
841 (or arg (setq arg 1))
842 (goto-char (or (octave-scan-blocks (point) arg 0) (buffer-end arg))))
844 (defun octave-backward-block (&optional arg)
845 "Move backward across one balanced Octave begin-end block.
846 With argument, do it that many times.
847 Negative arg -N means move forward across N blocks."
849 (or arg (setq arg 1))
850 (octave-forward-block (- arg)))
852 (defun octave-down-block (arg)
853 "Move forward down one begin-end block level of Octave code.
854 With argument, do this that many times.
855 A negative argument means move backward but still go down a level.
856 In Lisp programs, an argument is required."
858 (let ((inc (if (> arg 0) 1 -1)))
860 (goto-char (or (octave-scan-blocks (point) inc -1)
862 (setq arg (- arg inc)))))
864 (defun octave-backward-up-block (arg)
865 "Move backward out of one begin-end block level of Octave code.
866 With argument, do this that many times.
867 A negative argument means move forward but still to a less deep spot.
868 In Lisp programs, an argument is required."
870 (octave-up-block (- arg)))
872 (defun octave-up-block (arg)
873 "Move forward out of one begin-end block level of Octave code.
874 With argument, do this that many times.
875 A negative argument means move backward but still to a less deep spot.
876 In Lisp programs, an argument is required."
878 (let ((inc (if (> arg 0) 1 -1)))
880 (goto-char (or (octave-scan-blocks (point) inc 1)
882 (setq arg (- arg inc)))))
884 (defun octave-mark-block ()
885 "Put point at the beginning of this Octave block, mark at the end.
886 The block marked is the one that contains point or follows point."
889 (if (or (and (octave-in-block-p)
890 (skip-syntax-forward "w"))
893 (octave-down-block 1)
899 (octave-forward-block)
900 (exchange-point-and-mark))
902 (message "No block to mark found"))))
904 (defun octave-close-block ()
905 "Close the current Octave block on a separate line.
906 An error is signaled if no block to close is found."
912 (octave-backward-up-block 1)
913 (setq bb-keyword (buffer-substring-no-properties
914 (match-beginning 1) (match-end 1))))
917 (looking-at "^\\s-*$"))
918 (indent-according-to-mode)
919 (octave-reindent-then-newline-and-indent))
920 (insert (car (reverse
922 octave-block-match-alist))))
923 (octave-reindent-then-newline-and-indent)
925 (error (message "No block to close found")))))
927 (defun octave-blink-matching-block-open ()
928 "Blink the matching Octave begin block keyword.
929 If point is right after an Octave else or end type block keyword, move
930 cursor momentarily to the corresponding begin keyword.
931 Signal an error if the keywords are incompatible."
933 (let (bb-keyword bb-arg eb-keyword pos eol)
934 (if (and (octave-not-in-string-or-comment-p)
937 (skip-syntax-backward "w")
938 (looking-at octave-block-else-or-end-regexp)))
943 (buffer-substring-no-properties
944 (match-beginning 1) (match-end 1)))
945 (octave-backward-up-block 1))
948 (buffer-substring-no-properties
949 (match-beginning 2) (match-end 2)))
950 (octave-backward-block)))
951 (setq pos (match-end 0)
953 (buffer-substring-no-properties
954 (match-beginning 0) pos)
956 eol (octave-point 'eol)
961 (while (and (skip-syntax-forward "^<" eol)
963 (not (forward-char 1))))
964 (skip-syntax-backward " ")
965 (buffer-substring-no-properties pos (point)))))
966 (if (member eb-keyword
967 (cdr (assoc bb-keyword octave-block-match-alist)))
969 (message "Matches `%s %s'" bb-keyword bb-arg)
970 (if (pos-visible-in-window-p)
971 (sit-for blink-matching-delay)))
972 (error "Block keywords `%s' and `%s' do not match"
973 bb-keyword eb-keyword))))))
975 (defun octave-beginning-of-defun (&optional arg)
976 "Move backward to the beginning of an Octave function.
977 With positive ARG, do it that many times. Negative argument -N means
978 move forward to Nth following beginning of a function.
979 Returns t unless search stops at the beginning or end of the buffer."
981 (let* ((arg (or arg 1))
982 (inc (if (> arg 0) 1 -1))
985 (not (and (> arg 0) (looking-at "\\<function\\>")))
986 (skip-syntax-forward "w"))
987 (while (and (/= arg 0)
989 (re-search-backward "\\<function\\>" nil 'move inc)))
990 (if (octave-not-in-string-or-comment-p)
991 (setq arg (- arg inc))))
994 (and (< inc 0) (goto-char (match-beginning 0)))
997 (defun octave-end-of-defun (&optional arg)
998 "Move forward to the end of an Octave function.
999 With positive ARG, do it that many times. Negative argument -N means
1000 move back to Nth preceding end of a function.
1002 An end of a function occurs right after the end keyword matching the
1003 `function' keyword that starts the function."
1005 (or arg (setq arg 1))
1006 (and (< arg 0) (skip-syntax-backward "w"))
1007 (and (> arg 0) (skip-syntax-forward "w"))
1008 (if (octave-in-defun-p)
1009 (setq arg (- arg 1)))
1010 (if (= arg 0) (setq arg -1))
1011 (if (octave-beginning-of-defun (- arg))
1012 (octave-forward-block)))
1014 (defun octave-mark-defun ()
1015 "Put point at the beginning of this Octave function, mark at its end.
1016 The function marked is the one containing point or following point."
1018 (let ((pos (point)))
1019 (if (or (octave-in-defun-p)
1020 (and (octave-beginning-of-defun -1)
1021 (octave-in-defun-p)))
1023 (skip-syntax-forward "w")
1024 (octave-beginning-of-defun)
1026 (octave-end-of-defun)
1027 (exchange-point-and-mark))
1029 (message "No function to mark found"))))
1033 (defun octave-auto-fill ()
1034 "Perform auto-fill in Octave mode."
1035 (if (> (current-column) (current-fill-column))
1036 (if (octave-in-comment-p)
1038 (if (> (current-column) (current-fill-column))
1039 (let ((fill-column (- (current-fill-column)
1040 (length octave-continuation-string))))
1045 (insert (concat " " octave-continuation-string)))
1046 (indent-according-to-mode))))))
1048 (defun octave-fill-paragraph (&optional arg)
1049 "Fill paragraph of Octave code, handling Octave comments."
1052 (let ((end (progn (forward-paragraph) (point)))
1054 (forward-paragraph -1)
1055 (skip-chars-forward " \t\n")
1058 (cfc (current-fill-column))
1059 (ind (calculate-octave-indent))
1063 (narrow-to-region beg end)
1064 (if (listp ind) (setq ind (nth 1 ind)))
1067 (octave-indent-line ind)
1073 (looking-at "^\\s-*\\($\\|\\s<+\\)"))))
1075 (move-to-column cfc)
1076 ;; First check whether we need to combine non-empty comment lines
1077 (if (and (< (current-column) cfc)
1078 (octave-in-comment-p)
1079 (not (save-excursion
1081 (looking-at "^\\s-*\\s<+\\s-*$"))))
1082 ;; This is a nonempty comment line which does not extend
1083 ;; past the fill column. If it is followed by an nonempty
1084 ;; comment line with the same comment prefix, try to
1085 ;; combine them, and repeat this until either we reach the
1086 ;; fill-column or there is nothing more to combine.
1088 ;; Get the comment prefix
1091 (while (and (re-search-forward "\\s<+")
1092 (not (octave-in-comment-p))))
1093 (setq comment-prefix (match-string 0)))
1094 ;; And keep combining ...
1095 (while (and (< (current-column) cfc)
1107 (re-search-forward comment-prefix)
1108 (delete-region (match-beginning 0) (match-end 0))
1110 (move-to-column cfc))))
1111 (skip-chars-forward "^ \t\n")
1112 (delete-horizontal-space)
1113 (if (or (< (current-column) cfc)
1114 (and (= (current-column) cfc) (eolp)))
1116 (if (not (eolp)) (insert " "))
1117 (octave-auto-fill))))
1122 (defun octave-initialize-completions ()
1123 "Create an alist for Octave completions."
1124 (if octave-completion-alist
1126 (setq octave-completion-alist
1127 (mapcar '(lambda (var) (cons var var))
1128 (append octave-reserved-words
1129 octave-text-functions
1130 octave-variables)))))
1132 (defun octave-complete-symbol ()
1133 "Perform completion on Octave symbol preceding point.
1134 Compare that symbol against Octave's reserved words and builtin
1136 ;; This code taken from lisp-complete-symbol
1138 (let* ((end (point))
1139 (beg (save-excursion (backward-sexp 1) (point)))
1140 (string (buffer-substring-no-properties beg end))
1141 (completion (try-completion string octave-completion-alist)))
1142 (cond ((eq completion t)) ; ???
1144 (message "Can't find completion for \"%s\"" string)
1146 ((not (string= string completion))
1147 (delete-region beg end)
1148 (insert completion))
1150 (let ((list (all-completions string octave-completion-alist))
1151 (conf (current-window-configuration)))
1152 ;; Taken from comint.el
1153 (message "Making completion list...")
1154 (with-output-to-temp-buffer "*Completions*"
1155 (display-completion-list list))
1156 (message "Hit space to flush")
1159 (set-buffer (get-buffer "*Completions*"))
1160 (setq key (read-key-sequence nil)
1162 (and (consp first) (consp (event-start first))
1163 (eq (window-buffer (posn-window (event-start
1165 (get-buffer "*Completions*"))
1166 (eq (key-binding key) 'mouse-choose-completion)))
1168 (mouse-choose-completion first)
1169 (set-window-configuration conf))
1171 (set-window-configuration conf)
1172 (setq unread-command-events
1173 (listify-key-sequence key))))))))))
1176 ;;; Electric characters && friends
1177 (defun octave-reindent-then-newline-and-indent ()
1178 "Reindent current Octave line, insert newline, and indent the new line.
1179 If Abbrev mode is on, expand abbrevs first."
1181 (if abbrev-mode (expand-abbrev))
1182 (if octave-blink-matching-block
1183 (octave-blink-matching-block-open))
1185 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
1186 (indent-according-to-mode))
1188 (indent-according-to-mode))
1190 (defun octave-electric-semi ()
1191 "Insert a semicolon in Octave mode.
1192 Always reindent the line. Insert a newline if `octave-auto-newline' is
1195 (if (not (octave-not-in-string-or-comment-p))
1197 (if abbrev-mode (expand-abbrev))
1198 (if octave-blink-matching-block
1199 (octave-blink-matching-block-open))
1200 (indent-according-to-mode)
1202 (if octave-auto-newline
1203 (newline-and-indent))))
1205 (defun octave-electric-space ()
1206 "Insert a space in Octave mode.
1207 Maybe expand abbrevs and blink matching block open keywords."
1209 (setq last-command-char ? )
1210 (if (not (octave-not-in-string-or-comment-p))
1212 (indent-according-to-mode)
1213 (self-insert-command 1))
1214 (if abbrev-mode (expand-abbrev))
1215 (if octave-blink-matching-block
1216 (octave-blink-matching-block-open))
1218 (skip-syntax-backward " ")
1220 (indent-according-to-mode))
1221 (self-insert-command 1)))
1223 (defun octave-abbrev-start ()
1224 "Start entering an Octave abbreviation.
1225 If Abbrev mode is turned on, typing ` (grave accent) followed by ? or
1226 \\[help-command] lists all Octave abbrevs. Any other key combination is
1228 Note that all Octave mode abbrevs start with a grave accent."
1230 (if (not abbrev-mode)
1231 (self-insert-command 1)
1233 (insert last-command-char)
1234 (if (or (eq (setq c (read-event)) ??)
1236 (let ((abbrev-table-name-list '(octave-mode-abbrev-table)))
1238 (setq unread-command-events (list c))))))
1240 (defun octave-insert-defun (name args vals)
1241 "Insert an Octave function skeleton.
1242 Prompt for the function's name, arguments and return values (to be
1243 entered without parens)."
1246 (read-from-minibuffer "Function name: "
1247 (substring (buffer-name) 0 -2))
1248 (read-from-minibuffer "Arguments: ")
1249 (read-from-minibuffer "Return values: ")))
1250 (let ((string (format "%s %s (%s)"
1252 ((string-equal vals "")
1254 ((string-match "[ ,]" vals)
1255 (concat " [" vals "] ="))
1257 (concat " " vals " =")))
1260 (prefix octave-block-comment-start))
1261 (if (not (bobp)) (newline))
1262 (insert "function" string)
1263 (indent-according-to-mode)
1265 (insert prefix "usage: " string)
1266 (reindent-then-newline-and-indent)
1268 (reindent-then-newline-and-indent)
1270 (indent-according-to-mode)
1273 (insert "endfunction")
1274 (indent-according-to-mode))))
1278 (defun octave-add-octave-menu ()
1279 "Adds the `Octave' menu to the menu bar in Octave mode."
1281 (easy-menu-define octave-mode-menu-map octave-mode-map
1282 "Menu keymap for Octave mode." octave-mode-menu)
1283 (easy-menu-add octave-mode-menu-map octave-mode-map))
1286 ;;; Communication with the inferior Octave process
1287 (defun octave-kill-process ()
1288 "Kill inferior Octave process and its buffer."
1290 (if inferior-octave-process
1292 (process-send-string inferior-octave-process "quit;\n")
1293 (accept-process-output inferior-octave-process)))
1294 (if inferior-octave-buffer
1295 (kill-buffer inferior-octave-buffer)))
1297 (defun octave-show-process-buffer ()
1298 "Make sure that `inferior-octave-buffer' is displayed."
1300 (if (get-buffer inferior-octave-buffer)
1301 (display-buffer inferior-octave-buffer)
1302 (message "No buffer named %s" inferior-octave-buffer)))
1304 (defun octave-hide-process-buffer ()
1305 "Delete all windows that display `inferior-octave-buffer'."
1307 (if (get-buffer inferior-octave-buffer)
1308 (delete-windows-on inferior-octave-buffer)
1309 (message "No buffer named %s" inferior-octave-buffer)))
1311 (defun octave-send-region (beg end)
1312 "Send current region to the inferior Octave process."
1315 (let ((proc inferior-octave-process)
1316 (string (buffer-substring-no-properties beg end))
1319 (set-buffer inferior-octave-buffer)
1320 (setq inferior-octave-output-list nil)
1321 (while (not (string-equal string ""))
1322 (if (string-match "\n" string)
1323 (setq line (substring string 0 (match-beginning 0))
1324 string (substring string (match-end 0)))
1325 (setq line string string ""))
1326 (setq inferior-octave-receive-in-progress t)
1327 (inferior-octave-send-list-and-digest (list (concat line "\n")))
1328 (while inferior-octave-receive-in-progress
1329 (accept-process-output proc))
1330 (insert-before-markers
1331 (mapconcat 'identity
1333 (if octave-send-echo-input (list line) (list ""))
1334 (mapcar 'inferior-octave-strip-ctrl-g
1335 inferior-octave-output-list)
1336 (list inferior-octave-output-string))
1338 (if octave-send-show-buffer
1339 (display-buffer inferior-octave-buffer)))
1341 (defun octave-send-block ()
1342 "Send current Octave block to the inferior Octave process."
1346 (octave-send-region (point) (mark))))
1348 (defun octave-send-defun ()
1349 "Send current Octave function to the inferior Octave process."
1353 (octave-send-region (point) (mark))))
1355 (defun octave-send-line (&optional arg)
1356 "Send current Octave code line to the inferior Octave process.
1357 With positive prefix ARG, send that many lines.
1358 If `octave-send-line-auto-forward' is non-nil, go to the next unsent
1361 (or arg (setq arg 1))
1366 (octave-next-code-line (- arg 1))
1369 (if octave-send-line-auto-forward
1370 (octave-next-code-line 1))
1371 (octave-send-region beg end))))
1373 (defun octave-eval-print-last-sexp ()
1374 "Evaluate Octave sexp before point and print value into current buffer."
1377 (let ((standard-output (current-buffer))
1378 (print-escape-newlines nil)
1384 (inferior-octave-send-list-and-digest
1385 (list (concat (buffer-substring-no-properties (point) opoint)
1387 (mapconcat 'identity inferior-octave-output-list "\n")))
1391 (defun octave-submit-bug-report ()
1392 "Submit a bug report on the Emacs Octave package via mail."
1396 (y-or-n-p "Do you want to submit a bug report? ")
1397 (reporter-submit-bug-report
1398 octave-maintainer-address
1399 (concat "Emacs version " emacs-version)
1401 'octave-auto-newline
1402 'octave-blink-matching-block
1403 'octave-block-offset
1404 'octave-comment-char
1405 'octave-continuation-offset
1406 'octave-continuation-string
1408 'octave-mode-startup-message
1409 'octave-send-echo-input
1410 'octave-send-line-auto-forward
1411 'octave-send-show-buffer))))
1415 (provide 'octave-mod)
1417 ;;; octave-mod.el ends here