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 (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.")
48 (defvar octave-abbrev-table nil
49 "Abbrev table for Octave's reserved words.
50 Used in octave-mode and inferior-octave-mode buffers.
51 All Octave abbrevs start with a grave accent (`).")
52 (if octave-abbrev-table
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)
58 (define-abbrev octave-abbrev-table "`ca" "catch" nil)
59 (define-abbrev octave-abbrev-table "`c" "continue" nil)
60 (define-abbrev octave-abbrev-table "`el" "else" nil)
61 (define-abbrev octave-abbrev-table "`eli" "elseif" nil)
62 (define-abbrev octave-abbrev-table "`et" "end_try_catch" nil)
63 (define-abbrev octave-abbrev-table "`eu" "end_unwind_protect" nil)
64 (define-abbrev octave-abbrev-table "`ef" "endfor" nil)
65 (define-abbrev octave-abbrev-table "`efu" "endfunction" nil)
66 (define-abbrev octave-abbrev-table "`ei" "endif" nil)
67 (define-abbrev octave-abbrev-table "`ew" "endwhile" nil)
68 (define-abbrev octave-abbrev-table "`f" "for" nil)
69 (define-abbrev octave-abbrev-table "`fu" "function" nil)
70 (define-abbrev octave-abbrev-table "`gl" "global" nil)
71 (define-abbrev octave-abbrev-table "`gp" "gplot" nil)
72 (define-abbrev octave-abbrev-table "`gs" "gsplot" nil)
73 (define-abbrev octave-abbrev-table "`if" "if ()" nil)
74 (define-abbrev octave-abbrev-table "`rp" "replot" nil)
75 (define-abbrev octave-abbrev-table "`r" "return" nil)
76 (define-abbrev octave-abbrev-table "`t" "try" nil)
77 (define-abbrev octave-abbrev-table "`up" "unwind_protect" nil)
78 (define-abbrev octave-abbrev-table "`upc" "unwind_protect_cleanup" nil)
79 (define-abbrev octave-abbrev-table "`w" "while ()" nil)
80 (setq abbrevs-changed ac)))
82 (defvar octave-comment-char ?#
83 "Character to start an Octave comment.")
84 (defvar octave-comment-start
85 (concat (make-string 1 octave-comment-char) " ")
86 "String to insert to start a new Octave in-line comment.")
87 (defvar octave-comment-start-skip "\\s<+\\s-*"
88 "Regexp to match the start of an Octave comment up to its body.")
90 (defvar octave-begin-keywords
91 '("for" "function" "if" "try" "unwind_protect" "while"))
92 (defvar octave-else-keywords
93 '("catch" "else" "elseif" "unwind_protect_cleanup"))
94 (defvar octave-end-keywords
95 '("end" "endfor" "endfunction" "endif" "end_try_catch"
96 "end_unwind_protect" "endwhile"))
98 (defvar octave-reserved-words
99 (append octave-begin-keywords octave-else-keywords octave-end-keywords
100 '("all_va_args" "break" "continue" "global" "gplot" "gsplot"
102 "Reserved words in Octave.")
104 (defvar octave-text-functions
105 '("casesen" "cd" "chdir" "clear" "diary" "dir" "document" "echo"
106 "edit_history" "format" "gset" "gshow" "help" "history" "hold"
107 "load" "ls" "more" "run_history" "save" "set" "show" "type"
108 "which" "who" "whos")
109 "Text functions in Octave (these names are also reserved).")
111 (defvar octave-variables
112 '("EDITOR" "EXEC_PATH" "F_DUPFD" "F_GETFD" "F_GETFL" "F_SETFD"
113 "F_SETFL" "I" "IMAGEPATH" "INFO_FILE" "INFO_PROGRAM" "Inf" "J"
114 "LOADPATH" "NaN" "OCTAVE_VERSION" "O_APPEND" "O_CREAT" "O_EXCL"
115 "O_NONBLOCK" "O_RDONLY" "O_RDWR" "O_TRUNC" "O_WRONLY" "PAGER" "PS1"
116 "PS2" "PS4" "PWD" "SEEK_CUR" "SEEK_END" "SEEK_SET" "__F_DUPFD__"
117 "__F_GETFD__" "__F_GETFL__" "__F_SETFD__" "__F_SETFL__" "__I__"
118 "__Inf__" "__J__" "__NaN__" "__OCTAVE_VERSION__" "__O_APPEND__"
119 "__O_CREAT__" "__O_EXCL__" "__O_NONBLOCK__" "__O_RDONLY__"
120 "__O_RDWR__" "__O_TRUNC__" "__O_WRONLY__" "__PWD__" "__SEEK_CUR__"
121 "__SEEK_END__" "__SEEK_SET__" "__argv__" "__e__" "__eps__"
122 "__error_text__" "__i__" "__inf__" "__j__" "__nan__" "__pi__"
123 "__program_invocation_name__" "__program_name__" "__realmax__"
124 "__realmin__" "__stderr__" "__stdin__" "__stdout__" "ans" "argv"
125 "automatic_replot" "beep_on_error" "completion_append_char"
126 "default_return_value" "default_save_format"
127 "define_all_return_values" "do_fortran_indexing" "e"
128 "echo_executing_commands" "empty_list_elements_ok" "eps"
129 "error_text" "gnuplot_binary" "gnuplot_has_multiplot" "history_file"
130 "history_size" "ignore_function_time_stamp" "implicit_str_to_num_ok"
131 "inf" "nan" "nargin" "ok_to_lose_imaginary_part"
132 "output_max_field_width" "output_precision"
133 "page_output_immediately" "page_screen_output" "pi"
134 "prefer_column_vectors" "prefer_zero_one_indexing"
135 "print_answer_id_name" "print_empty_dimensions"
136 "program_invocation_name" "program_name" "propagate_empty_matrices"
137 "realmax" "realmin" "resize_on_range_error"
138 "return_last_computed_value" "save_precision" "saving_history"
139 "silent_functions" "split_long_rows" "stderr" "stdin" "stdout"
140 "string_fill_char" "struct_levels_to_print"
141 "suppress_verbose_help_message" "treat_neg_dim_as_zero"
142 "warn_assign_as_truth_value" "warn_comma_in_global_decl"
143 "warn_divide_by_zero" "warn_function_name_clash"
144 "warn_missing_semicolon" "whitespace_in_literal_matrix")
145 "Builtin variables in Octave.")
147 (defvar octave-function-header-regexp
148 (concat "^\\s-*\\<\\(function\\)\\>"
149 "\\([^=;\n]*=[ \t]*\\|[ \t]*\\)\\(\\w+\\)\\>")
150 "Regexp to match an Octave function header.
151 The string `function' and its name are given by the first and third
152 parenthetical grouping.")
154 (defvar octave-font-lock-keywords
156 ;; Fontify all builtin keywords.
157 (cons (concat "\\<\\("
158 (mapconcat 'identity octave-reserved-words "\\|")
159 (mapconcat 'identity octave-text-functions "\\|")
161 'font-lock-keyword-face)
162 ;; Fontify all builtin operators.
163 (cons "\\(&\\||\\|<=\\|>=\\|==\\|<\\|>\\|!=\\|!\\)"
164 'font-lock-reference-face)
165 ;; Fontify all builtin variables.
166 (cons (concat "\\<\\("
167 (mapconcat 'identity octave-variables "\\|")
169 'font-lock-variable-name-face)
170 ;; Fontify all function declarations.
171 (list octave-function-header-regexp
172 '(1 font-lock-keyword-face)
173 '(3 font-lock-function-name-face nil t)))
174 "Additional Octave expressions to highlight.")
176 (defvar inferior-octave-buffer "*Inferior Octave*"
177 "*Name of buffer for running an inferior Octave process.")
179 (defvar inferior-octave-process nil)
181 (defvar octave-mode-map nil
182 "Keymap used in Octave mode.")
185 (let ((map (make-sparse-keymap)))
186 (define-key map "`" 'octave-abbrev-start)
187 (define-key map ";" 'octave-electric-semi)
188 (define-key map " " 'octave-electric-space)
189 (define-key map "\n" 'octave-reindent-then-newline-and-indent)
190 (define-key map "\t" 'indent-according-to-mode)
191 (define-key map "\e;" 'octave-indent-for-comment)
192 (define-key map "\e\n" 'octave-indent-new-comment-line)
193 (define-key map "\e\t" 'octave-complete-symbol)
194 (define-key map "\M-\C-a" 'octave-beginning-of-defun)
195 (define-key map "\M-\C-e" 'octave-end-of-defun)
196 (define-key map "\M-\C-h" 'octave-mark-defun)
197 (define-key map "\M-\C-q" 'octave-indent-defun)
198 (define-key map "\C-c;" 'octave-comment-region)
199 (define-key map "\C-c:" 'octave-uncomment-region)
200 (define-key map "\C-c\C-b" 'octave-submit-bug-report)
201 (define-key map "\C-c\C-p" 'octave-previous-code-line)
202 (define-key map "\C-c\C-n" 'octave-next-code-line)
203 (define-key map "\C-c\C-a" 'octave-beginning-of-line)
204 (define-key map "\C-c\C-e" 'octave-end-of-line)
205 (define-key map "\C-c\M-\C-n" 'octave-forward-block)
206 (define-key map "\C-c\M-\C-p" 'octave-backward-block)
207 (define-key map "\C-c\M-\C-u" 'octave-backward-up-block)
208 (define-key map "\C-c\M-\C-d" 'octave-down-block)
209 (define-key map "\C-c\M-\C-h" 'octave-mark-block)
210 (define-key map "\C-c]" 'octave-close-block)
211 (define-key map "\C-cf" 'octave-insert-defun)
212 (define-key map "\C-c\C-h" 'octave-help)
213 (define-key map "\C-cil" 'octave-send-line)
214 (define-key map "\C-cib" 'octave-send-block)
215 (define-key map "\C-cif" 'octave-send-defun)
216 (define-key map "\C-cir" 'octave-send-region)
217 (define-key map "\C-cis" 'octave-show-process-buffer)
218 (define-key map "\C-cih" 'octave-hide-process-buffer)
219 (define-key map "\C-cik" 'octave-kill-process)
220 (setq octave-mode-map map)))
222 (defvar octave-mode-menu
225 ["Previous Code Line" octave-previous-code-line t]
226 ["Next Code Line" octave-next-code-line t]
227 ["Begin of Continuation" octave-beginning-of-line t]
228 ["End of Continuation" octave-end-of-line t]
229 ["Split Line at Point" octave-indent-new-comment-line t])
231 ["Next Block" octave-forward-block t]
232 ["Previous Block" octave-backward-block t]
233 ["Down Block" octave-down-block t]
234 ["Up Block" octave-backward-up-block t]
235 ["Mark Block" octave-mark-block t]
236 ["Close Block" octave-close-block t])
238 ["Begin of Function" octave-beginning-of-defun t]
239 ["End of Function" octave-end-of-defun t]
240 ["Mark Function" octave-mark-defun t]
241 ["Indent Function" octave-indent-defun t]
242 ["Insert Function" octave-insert-defun t])
245 ["Send Current Line" octave-send-line t]
246 ["Send Current Block" octave-send-block t]
247 ["Send Current Function" octave-send-defun t]
248 ["Send Region" octave-send-region t]
249 ["Show Process Buffer" octave-show-process-buffer t]
250 ["Hide Process Buffer" octave-hide-process-buffer t]
251 ["Kill Process" octave-kill-process t])
253 ["Indent Line" indent-according-to-mode t]
254 ["Complete Symbol" octave-complete-symbol t]
256 ["Toggle Abbrev Mode" abbrev-mode t]
257 ["Toggle Auto-Fill Mode" auto-fill-mode t]
259 ["Submit Bug Report" octave-submit-bug-report t]
261 ["Describe Octave Mode" octave-describe-major-mode t]
262 ["Lookup Octave Index" octave-help t])
263 "Menu for Octave mode.")
265 (defvar octave-mode-syntax-table nil
266 "Syntax table in use in octave-mode buffers.")
267 (if octave-mode-syntax-table
269 (let ((table (make-syntax-table)))
270 (modify-syntax-entry ?\r " " table)
271 (modify-syntax-entry ?+ "." table)
272 (modify-syntax-entry ?- "." table)
273 (modify-syntax-entry ?= "." table)
274 (modify-syntax-entry ?* "." table)
275 (modify-syntax-entry ?/ "." table)
276 (modify-syntax-entry ?> "." 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 ?\` "w" table)
284 (modify-syntax-entry ?\" "\"" table)
285 (modify-syntax-entry ?. "w" table)
286 (modify-syntax-entry ?_ "w" table)
287 (modify-syntax-entry ?\% "." table)
288 (modify-syntax-entry ?\# "<" table)
289 (modify-syntax-entry ?\n ">" table)
290 (setq octave-mode-syntax-table table)))
292 (defvar octave-auto-newline nil
293 "*Non-nil means automatically newline after a semicolon in Octave mode.")
295 (defvar octave-blink-matching-block t
296 "*Control the blinking of matching Octave block keywords.
297 Non-nil means show matching begin of block when inserting a space,
298 newline or semicolon after an else or end keyword.")
299 (defvar octave-block-offset 2
300 "*Extra indentation applied to statements in Octave block structures.")
302 (defvar octave-block-begin-regexp
304 (mapconcat 'identity octave-begin-keywords "\\|")
306 (defvar octave-block-else-regexp
308 (mapconcat 'identity octave-else-keywords "\\|")
310 (defvar octave-block-end-regexp
312 (mapconcat 'identity octave-end-keywords "\\|")
314 (defvar octave-block-begin-or-end-regexp
315 (concat octave-block-begin-regexp "\\|" octave-block-end-regexp))
316 (defvar octave-block-else-or-end-regexp
317 (concat octave-block-else-regexp "\\|" octave-block-end-regexp))
318 (defvar octave-block-match-alist
319 '(("for" . ("end" "endfor"))
320 ("function" . ("end" "endfunction"))
321 ("if" . ("else" "elseif" "end" "endif"))
322 ("try" . ("catch" "end" "end_try_catch"))
323 ("unwind_protect" . ("unwind_protect_cleanup" "end"
324 "end_unwind_protect"))
325 ("while" . ("end" "endwhile")))
326 "Alist with Octave's matching block keywords.
327 Has Octave's begin keywords as keys and a list of the matching else or
328 end keywords as associated values.")
330 (defvar octave-block-comment-start
331 (concat (make-string 2 octave-comment-char) " ")
332 "String to insert to start a new Octave comment on an empty line.")
334 (defvar octave-continuation-offset 4
335 "*Extra indentation applied to Octave continuation lines.")
336 (defvar octave-continuation-regexp
337 "[^#%\n]*\\(\\\\\\|\\.\\.\\.\\)\\s-*\\(\\s<.*\\)?$")
338 (defvar octave-continuation-string "\\"
339 "*Character string used for Octave continuation lines. Normally \\.")
341 (defvar octave-completion-alist nil
342 "Alist of Octave symbols for completion in Octave mode.
343 Each element looks like (VAR . VAR), where the car and cdr are the same
344 symbol (an Octave command or variable name).
345 Currently, only builtin variables can be completed.")
347 (defvar octave-mode-imenu-generic-expression
350 (list nil octave-function-header-regexp 3))
351 "Imenu expression for Octave mode. See `imenu-generic-expression'.")
353 (defvar octave-mode-startup-message t
354 "*Nil means do not display the Octave mode startup message.")
356 (defvar octave-mode-hook nil
357 "*Hook to be run when Octave mode is started.")
359 (defvar octave-send-show-buffer t
360 "*Non-nil means display `inferior-octave-buffer' after sending to it.")
361 (defvar octave-send-line-auto-forward t
362 "*Control auto-forward after sending to the inferior Octave process.
363 Non-nil means always go to the next Octave code line after sending.")
364 (defvar octave-send-echo-input t
365 "*Non-nil means echo input sent to the inferior Octave process.")
369 (defun octave-mode ()
370 "Major mode for editing Octave code.
372 This mode makes it easier to write Octave code by helping with
373 indentation, doing some of the typing for you (with Abbrev mode) and by
374 showing keywords, comments, strings, etc. in different faces (with
375 Font Lock mode on terminals that support it).
377 Octave itself is a high-level language, primarily intended for numerical
378 computations. It provides a convenient command line interface for
379 solving linear and nonlinear problems numerically. Function definitions
380 can also be stored in files, and it can be used in a batch mode (which
381 is why you need this mode!).
383 The latest released version of Octave is always available via anonymous
384 ftp from bevo.che.wisc.edu in the directory `/pub/octave'. Complete
385 source and binaries for several popular systems are available.
387 Type \\[list-abbrevs] to display the built-in abbrevs for Octave keywords.
394 Variables you can use to customize Octave mode
395 ==============================================
398 Non-nil means auto-insert a newline and indent after a semicolon.
401 octave-blink-matching-block
402 Non-nil means show matching begin of block when inserting a space,
403 newline or semicolon after an else or end keyword. Default is t.
406 Extra indentation applied to statements in block structures.
409 octave-continuation-offset
410 Extra indentation applied to Octave continuation lines.
413 octave-continuation-string
414 String used for Octave continuation lines.
415 Default is a backslash.
417 octave-mode-startup-message
418 Nil means do not display the Octave mode startup message.
421 octave-send-echo-input
422 Non-nil means always display `inferior-octave-buffer' after sending a
423 command to the inferior Octave process.
425 octave-send-line-auto-forward
426 Non-nil means always go to the next unsent line of Octave code after
427 sending a line to the inferior Octave process.
429 octave-send-echo-input
430 Non-nil means echo input sent to the inferior Octave process.
432 Turning on Octave mode runs the hook `octave-mode-hook'.
434 To begin using this mode for all `.m' files that you edit, add the
435 following lines to your `.emacs' file:
437 (autoload 'octave-mode \"octave-mod\" nil t)
438 (setq auto-mode-alist
439 (cons '(\"\\\\.m$\" . octave-mode) auto-mode-alist))
441 To automatically turn on the abbrev, auto-fill and font-lock features,
442 add the following lines to your `.emacs' file as well:
444 (add-hook 'octave-mode-hook
448 (if (eq window-system 'x)
449 (font-lock-mode 1))))
451 To submit a problem report, enter \\[octave-submit-bug-report] from \
452 an Octave mode buffer.
453 This automatically sets up a mail buffer with version information
454 already added. You just need to add a description of the problem,
455 including a reproducible test case and send the message."
457 (kill-all-local-variables)
459 (use-local-map octave-mode-map)
460 (setq major-mode 'octave-mode)
461 (setq mode-name "Octave")
462 (setq local-abbrev-table octave-abbrev-table)
463 (set-syntax-table octave-mode-syntax-table)
465 (make-local-variable 'indent-line-function)
466 (setq indent-line-function 'octave-indent-line)
468 (make-local-variable 'comment-start)
469 (setq comment-start octave-comment-start)
470 (make-local-variable 'comment-end)
471 (setq comment-end "")
472 (make-local-variable 'comment-column)
473 (setq comment-column 32)
474 (make-local-variable 'comment-start-skip)
475 (setq comment-start-skip "\\s<+\\s-*")
476 (make-local-variable 'comment-indent-function)
477 (setq comment-indent-function 'octave-comment-indent)
479 (make-local-variable 'parse-sexp-ignore-comments)
480 (setq parse-sexp-ignore-comments t)
481 (make-local-variable 'paragraph-start)
482 (setq paragraph-start (concat "\\s-*$\\|" page-delimiter))
483 (make-local-variable 'paragraph-separate)
484 (setq paragraph-separate paragraph-start)
485 (make-local-variable 'paragraph-ignore-fill-prefix)
486 (setq paragraph-ignore-fill-prefix t)
487 (make-local-variable 'fill-paragraph-function)
488 (setq fill-paragraph-function 'octave-fill-paragraph)
489 (make-local-variable 'adaptive-fill-regexp)
490 (setq adaptive-fill-regexp nil)
491 (make-local-variable 'fill-column)
492 (setq fill-column 72)
493 (make-local-variable 'normal-auto-fill-function)
494 (setq normal-auto-fill-function 'octave-auto-fill)
496 (make-local-variable 'font-lock-defaults)
497 (setq font-lock-defaults '(octave-font-lock-keywords nil nil))
499 (make-local-variable 'imenu-generic-expression)
500 (setq imenu-generic-expression octave-mode-imenu-generic-expression)
502 (octave-add-octave-menu)
503 (octave-initialize-completions)
504 (run-hooks 'octave-mode-hook))
506 ;;; Miscellaneous useful functions
507 (defun octave-describe-major-mode ()
508 "Describe the current major mode."
510 (describe-function major-mode))
512 (defun octave-point (position)
513 "Returns the value of point at certain positions."
516 ((eq position 'bol) (beginning-of-line))
517 ((eq position 'eol) (end-of-line))
518 ((eq position 'boi) (back-to-indentation))
519 ((eq position 'bonl) (forward-line 1))
520 ((eq position 'bopl) (forward-line -1))
521 (t (error "unknown buffer position requested: %s" position)))
524 (defsubst octave-in-comment-p ()
525 "Returns t if point is inside an Octave comment, nil otherwise."
528 (nth 4 (parse-partial-sexp (octave-point 'bol) (point)))))
530 (defsubst octave-in-string-p ()
531 "Returns t if point is inside an Octave string, nil otherwise."
534 (nth 3 (parse-partial-sexp (octave-point 'bol) (point)))))
536 (defsubst octave-not-in-string-or-comment-p ()
537 "Returns t iff point is not inside an Octave string or comment."
538 (let ((pps (parse-partial-sexp (octave-point 'bol) (point))))
539 (not (or (nth 3 pps) (nth 4 pps)))))
541 (defun octave-in-block-p ()
542 "Returns t if point is inside an Octave block, nil otherwise.
543 The block is taken to start at the first letter of the begin keyword and
544 to end after the end keyword."
549 (skip-syntax-forward "w")
551 (octave-forward-block)
556 (defun octave-in-defun-p ()
557 "Returns t iff point is inside an Octave function declaration.
558 The function is taken to start at the `f' of `function' and to end after
562 (or (and (looking-at "\\<function\\>")
563 (octave-not-in-string-or-comment-p))
564 (and (octave-beginning-of-defun)
567 (octave-forward-block)
573 (defun octave-comment-region (beg end &optional arg)
574 "Comment or uncomment each line in the region as Octave code.
575 See `comment-region'."
577 (let ((comment-start (char-to-string octave-comment-char)))
578 (comment-region beg end arg)))
580 (defun octave-uncomment-region (beg end &optional arg)
581 "Uncomment each line in the region as Octave code."
583 (or arg (setq arg 1))
584 (octave-comment-region beg end (- arg)))
588 (defun calculate-octave-indent ()
589 "Return appropriate indentation for current line as Octave code.
590 Returns an integer (the column to indent to) unless the line is a
591 comment line with fixed goal golumn. In that case, returns a list whose
592 car is the column to indent to, and whose cdr is the current indentation
594 (let ((is-continuation-line
596 (if (zerop (octave-previous-code-line))
597 (looking-at octave-continuation-regexp))))
601 ;; If we can move backward out one level of parentheses, take 1
602 ;; plus the indentation of that parenthesis. Otherwise, go back
603 ;; to the beginning of the previous code line, and compute the
604 ;; offset this line gives.
605 (if (condition-case nil
610 (setq icol (+ 1 (current-column)))
611 (if (zerop (octave-previous-code-line))
613 (octave-beginning-of-line)
614 (back-to-indentation)
615 (setq icol (current-column))
617 (eol (octave-point 'eol)))
618 (while (< (point) eol)
619 (if (octave-not-in-string-or-comment-p)
621 ((looking-at octave-block-begin-regexp)
622 (setq icol (+ icol octave-block-offset)))
623 ((looking-at octave-block-else-regexp)
625 (setq icol (+ icol octave-block-offset))))
626 ((looking-at octave-block-end-regexp)
627 (if (not (= bot (point)))
628 (setq icol (- icol octave-block-offset))))))
630 (if is-continuation-line
631 (setq icol (+ icol octave-continuation-offset)))))))
633 (back-to-indentation)
635 ((and (or (looking-at octave-block-else-regexp)
636 (looking-at octave-block-end-regexp))
637 (octave-not-in-string-or-comment-p))
638 (setq icol (- icol octave-block-offset)))
639 ((looking-at "\\s<\\s<\\s<\\S<")
640 (setq icol (list 0 icol)))
641 ((looking-at "\\s<\\S<")
642 (setq icol (list comment-column icol)))))
645 (defun octave-comment-indent ()
646 (if (looking-at "\\s<\\s<\\s<")
648 (if (looking-at "\\s<\\s<")
649 (calculate-octave-indent)
650 (skip-syntax-backward " ")
651 (max (if (bolp) 0 (+ (current-column)))
654 (defun octave-indent-for-comment ()
655 "Maybe insert and indent an Octave comment.
656 If there is no comment already on this line, create a code-level comment
657 (started by two comment characters) if the line is empty, or an in-line
658 comment (started by one comment character) otherwise.
659 Point is left after the start of the comment which is properly aligned."
662 (indent-according-to-mode))
664 (defun octave-indent-line (&optional arg)
665 "Indent current line as Octave code.
666 With optional ARG, use this as offset unless this line is a comment with
669 (or arg (setq arg 0))
670 (let ((icol (calculate-octave-indent))
671 (relpos (- (current-column) (current-indentation))))
673 (setq icol (car icol))
674 (setq icol (+ icol arg)))
676 (error "Unmatched end keyword")
677 (indent-line-to icol)
679 (move-to-column (+ icol relpos))))))
681 (defun octave-indent-new-comment-line ()
682 "Break Octave line at point, continuing comment if within one.
683 If within code, insert `octave-continuation-string' before breaking the
684 line. If within a string, signal an error.
685 The new line is properly indented."
687 (delete-horizontal-space)
689 ((octave-in-comment-p)
690 (indent-new-comment-line))
691 ((octave-in-string-p)
692 (error "Cannot split a code line inside a string"))
694 (insert (concat " " octave-continuation-string))
695 (octave-reindent-then-newline-and-indent))))
697 (defun octave-indent-defun ()
698 "Properly indents the Octave function which contains point."
702 (message "Indenting function...")
703 (indent-region (point) (mark) nil))
704 (message "Indenting function...done."))
708 (defun octave-next-code-line (&optional arg)
709 "Move ARG lines of Octave code forward (backward if ARG is negative).
710 Skips past all empty and comment lines. Default for ARG is 1.
712 On success, return 0. Otherwise, go as far as possible and return -1."
714 (or arg (setq arg 1))
717 (inc (if (> arg 0) 1 -1)))
718 (while (and (/= arg 0) (= n 0))
719 (setq n (forward-line inc))
721 (looking-at "\\s-*\\($\\|\\s<\\)"))
722 (setq n (forward-line inc)))
723 (setq arg (- arg inc)))
726 (defun octave-previous-code-line (&optional arg)
727 "Move ARG lines of Octave code backward (forward if ARG is negative).
728 Skips past all empty and comment lines. Default for ARG is 1.
730 On success, return 0. Otherwise, go as far as possible and return -1."
732 (or arg (setq arg 1))
733 (octave-next-code-line (- arg)))
735 (defun octave-beginning-of-line ()
736 "Move point to beginning of current Octave line.
737 If on an empty or comment line, go to the beginning of that line.
738 Otherwise, move backward to the beginning of the first Octave code line
739 which is not inside a continuation statement, i.e., which does not
740 follow a code line ending in `...' or `\\', or is inside an open
744 (if (not (looking-at "\\s-*\\($\\|\\s<\\)"))
745 (while (or (condition-case nil
751 (and (or (looking-at "\\s-*\\($\\|\\s<\\)")
753 (if (zerop (octave-previous-code-line))
754 (looking-at octave-continuation-regexp))))
755 (zerop (forward-line -1)))))))
757 (defun octave-end-of-line ()
758 "Move point to end of current Octave line.
759 If on an empty or comment line, go to the end of that line.
760 Otherwise, move forward to the end of the first Octave code line which
761 does not end in `...' or `\\' or is inside an open parenthesis list."
766 (looking-at "\\s-*\\($\\|\\s<\\)"))
768 (while (or (condition-case nil
776 (or (looking-at "\\s-*\\($\\|\\s<\\)")
777 (looking-at octave-continuation-regexp)))
778 (zerop (forward-line 1)))))
781 (defun octave-scan-blocks (from count depth)
782 "Scan from character number FROM by COUNT Octave begin-end blocks.
783 Returns the character number of the position thus found.
785 If DEPTH is nonzero, block depth begins counting from that value.
786 Only places where the depth in blocks becomes zero are candidates for
787 stopping; COUNT such places are counted.
789 If the beginning or end of the buffer is reached and the depth is wrong,
790 an error is signaled."
791 (let ((min-depth (if (> depth 0) 0 depth))
792 (inc (if (> count 0) 1 -1)))
796 (while (or (re-search-forward
797 octave-block-begin-or-end-regexp nil 'move inc)
799 (error "Unbalanced block")))
800 (if (octave-not-in-string-or-comment-p)
804 (setq depth (+ depth inc)))
806 (setq depth (- depth inc))))
807 (if (< depth min-depth)
808 (error "Containing expression ends prematurely"))
810 (throw 'foo nil))))))
811 (setq count (- count inc)))
814 (defun octave-forward-block (&optional arg)
815 "Move forward across one balanced Octave begin-end block.
816 With argument, do it that many times.
817 Negative arg -N means move backward across N blocks."
819 (or arg (setq arg 1))
820 (goto-char (or (octave-scan-blocks (point) arg 0) (buffer-end arg))))
822 (defun octave-backward-block (&optional arg)
823 "Move backward across one balanced Octave begin-end block.
824 With argument, do it that many times.
825 Negative arg -N means move forward across N blocks."
827 (or arg (setq arg 1))
828 (octave-forward-block (- arg)))
830 (defun octave-down-block (arg)
831 "Move forward down one begin-end block level of Octave code.
832 With argument, do this that many times.
833 A negative argument means move backward but still go down a level.
834 In Lisp programs, an argument is required."
836 (let ((inc (if (> arg 0) 1 -1)))
838 (goto-char (or (octave-scan-blocks (point) inc -1)
840 (setq arg (- arg inc)))))
842 (defun octave-backward-up-block (arg)
843 "Move backward out of one begin-end block level of Octave code.
844 With argument, do this that many times.
845 A negative argument means move forward but still to a less deep spot.
846 In Lisp programs, an argument is required."
848 (octave-up-block (- arg)))
850 (defun octave-up-block (arg)
851 "Move forward out of one begin-end block level of Octave code.
852 With argument, do this that many times.
853 A negative argument means move backward but still to a less deep spot.
854 In Lisp programs, an argument is required."
856 (let ((inc (if (> arg 0) 1 -1)))
858 (goto-char (or (octave-scan-blocks (point) inc 1)
860 (setq arg (- arg inc)))))
862 (defun octave-mark-block ()
863 "Put point at the beginning of this Octave block, mark at the end.
864 The block marked is the one that contains point or follows point."
867 (if (or (and (octave-in-block-p)
868 (skip-syntax-forward "w"))
871 (octave-down-block 1)
877 (octave-forward-block)
878 (exchange-point-and-mark))
880 (message "No block to mark found"))))
882 (defun octave-close-block ()
883 "Close the current Octave block on a separate line.
884 An error is signaled if no block to close is found."
890 (octave-backward-up-block 1)
891 (setq bb-keyword (buffer-substring-no-properties
892 (match-beginning 1) (match-end 1))))
895 (looking-at "^\\s-*$"))
896 (indent-according-to-mode)
897 (octave-reindent-then-newline-and-indent))
898 (insert (car (reverse
900 octave-block-match-alist))))
901 (octave-reindent-then-newline-and-indent)
903 (error (message "No block to close found")))))
905 (defun octave-blink-matching-block-open ()
906 "Blink the matching Octave begin block keyword.
907 If point is right after an Octave else or end type block keyword, move
908 cursor momentarily to the corresponding begin keyword.
909 Signal an error if the keywords are incompatible."
911 (let (bb-keyword bb-arg eb-keyword pos eol)
912 (if (and (octave-not-in-string-or-comment-p)
915 (skip-syntax-backward "w")
916 (looking-at octave-block-else-or-end-regexp)))
921 (buffer-substring-no-properties
922 (match-beginning 1) (match-end 1)))
923 (octave-backward-up-block 1))
926 (buffer-substring-no-properties
927 (match-beginning 2) (match-end 2)))
928 (octave-backward-block)))
929 (setq pos (match-end 0)
931 (buffer-substring-no-properties
932 (match-beginning 0) pos)
934 eol (octave-point 'eol)
939 (while (and (skip-syntax-forward "^<" eol)
941 (not (forward-char 1))))
942 (skip-syntax-backward " ")
943 (buffer-substring-no-properties pos (point)))))
944 (if (member eb-keyword
945 (cdr (assoc bb-keyword octave-block-match-alist)))
947 (message "Matches `%s %s'" bb-keyword bb-arg)
948 (if (pos-visible-in-window-p)
949 (sit-for blink-matching-delay)))
950 (error "Block keywords `%s' and `%s' do not match"
951 bb-keyword eb-keyword))))))
953 (defun octave-beginning-of-defun (&optional arg)
954 "Move backward to the beginning of an Octave function.
955 With positive ARG, do it that many times. Negative argument -N means
956 move forward to Nth following beginning of a function.
957 Returns t unless search stops at the beginning or end of the buffer."
959 (let* ((arg (or arg 1))
960 (inc (if (> arg 0) 1 -1))
963 (not (and (> arg 0) (looking-at "\\<function\\>")))
964 (skip-syntax-forward "w"))
965 (while (and (/= arg 0)
967 (re-search-backward "\\<function\\>" nil 'move inc)))
968 (if (octave-not-in-string-or-comment-p)
969 (setq arg (- arg inc))))
972 (and (< inc 0) (goto-char (match-beginning 0)))
975 (defun octave-end-of-defun (&optional arg)
976 "Move forward to the end of an Octave function.
977 With positive ARG, do it that many times. Negative argument -N means
978 move back to Nth preceding end of a function.
980 An end of a function occurs right after the end keyword matching the
981 `function' keyword that starts the function."
983 (or arg (setq arg 1))
984 (and (< arg 0) (skip-syntax-backward "w"))
985 (and (> arg 0) (skip-syntax-forward "w"))
986 (if (octave-in-defun-p)
987 (setq arg (- arg 1)))
988 (if (= arg 0) (setq arg -1))
989 (if (octave-beginning-of-defun (- arg))
990 (octave-forward-block)))
992 (defun octave-mark-defun ()
993 "Put point at the beginning of this Octave function, mark at its end.
994 The function marked is the one containing point or following point."
997 (if (or (octave-in-defun-p)
998 (and (octave-beginning-of-defun -1)
999 (octave-in-defun-p)))
1001 (skip-syntax-forward "w")
1002 (octave-beginning-of-defun)
1004 (octave-end-of-defun)
1005 (exchange-point-and-mark))
1007 (message "No function to mark found"))))
1011 (defun octave-auto-fill ()
1012 "Perform auto-fill in Octave mode."
1013 (if (> (current-column) (current-fill-column))
1014 (if (octave-in-comment-p)
1016 (if (> (current-column) (current-fill-column))
1017 (let ((fill-column (- (current-fill-column)
1018 (length octave-continuation-string))))
1023 (insert (concat " " octave-continuation-string)))
1024 (indent-according-to-mode))))))
1026 (defun octave-fill-paragraph (&optional arg)
1027 "Fill paragraph of Octave code, handling Octave comments."
1030 (let ((end (progn (forward-paragraph) (point)))
1032 (forward-paragraph -1)
1033 (skip-chars-forward " \t\n")
1036 (cfc (current-fill-column))
1037 (ind (calculate-octave-indent))
1041 (narrow-to-region beg end)
1042 (if (listp ind) (setq ind (nth 1 ind)))
1045 (octave-indent-line ind)
1051 (looking-at "^\\s-*\\($\\|\\s<+\\)"))))
1053 (move-to-column cfc)
1054 ;; First check whether we need to combine non-empty comment lines
1055 (if (and (< (current-column) cfc)
1056 (octave-in-comment-p)
1057 (not (save-excursion
1059 (looking-at "^\\s-*\\s<+\\s-*$"))))
1060 ;; This is a nonempty comment line which does not extend
1061 ;; past the fill column. If it is followed by an nonempty
1062 ;; comment line with the same comment prefix, try to
1063 ;; combine them, and repeat this until either we reach the
1064 ;; fill-column or there is nothing more to combine.
1066 ;; Get the comment prefix
1069 (while (and (re-search-forward "\\s<+")
1070 (not (octave-in-comment-p))))
1071 (setq comment-prefix (match-string 0)))
1072 ;; And keep combining ...
1073 (while (and (< (current-column) cfc)
1085 (re-search-forward comment-prefix)
1086 (delete-region (match-beginning 0) (match-end 0))
1088 (move-to-column cfc))))
1089 (skip-chars-forward "^ \t\n")
1090 (delete-horizontal-space)
1091 (if (or (< (current-column) cfc)
1092 (and (= (current-column) cfc) (eolp)))
1094 (if (not (eolp)) (insert " "))
1095 (octave-auto-fill))))
1100 (defun octave-initialize-completions ()
1101 "Create an alist for Octave completions."
1102 (if octave-completion-alist
1104 (setq octave-completion-alist
1105 (mapcar '(lambda (var) (cons var var))
1106 (append octave-reserved-words
1107 octave-text-functions
1108 octave-variables)))))
1110 (defun octave-complete-symbol ()
1111 "Perform completion on Octave symbol preceding point.
1112 Compare that symbol against Octave's reserved words and builtin
1114 ;; This code taken from lisp-complete-symbol
1116 (let* ((end (point))
1117 (beg (save-excursion (backward-sexp 1) (point)))
1118 (string (buffer-substring-no-properties beg end))
1119 (completion (try-completion string octave-completion-alist)))
1120 (cond ((eq completion t)) ; ???
1122 (message "Can't find completion for \"%s\"" string)
1124 ((not (string= string completion))
1125 (delete-region beg end)
1126 (insert completion))
1128 (let ((list (all-completions string octave-completion-alist))
1129 (conf (current-window-configuration)))
1130 ;; Taken from comint.el
1131 (message "Making completion list...")
1132 (with-output-to-temp-buffer "*Completions*"
1133 (display-completion-list list))
1134 (message "Hit space to flush")
1137 (set-buffer (get-buffer "*Completions*"))
1138 (setq key (read-key-sequence nil)
1140 (and (consp first) (consp (event-start first))
1141 (eq (window-buffer (posn-window (event-start
1143 (get-buffer "*Completions*"))
1144 (eq (key-binding key) 'mouse-choose-completion)))
1146 (mouse-choose-completion first)
1147 (set-window-configuration conf))
1149 (set-window-configuration conf)
1150 (setq unread-command-events
1151 (listify-key-sequence key))))))))))
1154 ;;; Electric characters && friends
1155 (defun octave-reindent-then-newline-and-indent ()
1156 "Reindent current Octave line, insert newline, and indent the new line.
1157 If Abbrev mode is on, expand abbrevs first."
1159 (if abbrev-mode (expand-abbrev))
1160 (if octave-blink-matching-block
1161 (octave-blink-matching-block-open))
1163 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
1164 (indent-according-to-mode))
1166 (indent-according-to-mode))
1168 (defun octave-electric-semi ()
1169 "Insert a semicolon in Octave mode.
1170 Always reindent the line. Insert a newline if `octave-auto-newline' is
1173 (if (not (octave-not-in-string-or-comment-p))
1175 (if abbrev-mode (expand-abbrev))
1176 (if octave-blink-matching-block
1177 (octave-blink-matching-block-open))
1178 (indent-according-to-mode)
1180 (if octave-auto-newline
1181 (newline-and-indent))))
1183 (defun octave-electric-space ()
1184 "Insert a space in Octave mode.
1185 Maybe expand abbrevs and blink matching block open keywords."
1187 (setq last-command-char ? )
1188 (if (not (octave-not-in-string-or-comment-p))
1190 (indent-according-to-mode)
1191 (self-insert-command 1))
1192 (if abbrev-mode (expand-abbrev))
1193 (if octave-blink-matching-block
1194 (octave-blink-matching-block-open))
1196 (skip-syntax-backward " ")
1198 (indent-according-to-mode))
1199 (self-insert-command 1)))
1201 (defun octave-abbrev-start ()
1202 "Start entering an Octave abbreviation.
1203 If Abbrev mode is turned on, typing ` (grave accent) followed by ? or
1204 \\[help-command] lists all Octave abbrevs. Any other key combination is
1206 Note that all Octave mode abbrevs start with a grave accent."
1208 (if (not abbrev-mode)
1209 (self-insert-command 1)
1211 (insert last-command-char)
1212 (if (or (eq (setq c (read-event)) ??)
1214 (let ((abbrev-table-name-list '(octave-mode-abbrev-table)))
1216 (setq unread-command-events (list c))))))
1218 (defun octave-insert-defun (name args vals)
1219 "Insert an Octave function skeleton.
1220 Prompt for the function's name, arguments and return values (to be
1221 entered without parens)."
1224 (read-from-minibuffer "Function name: "
1225 (substring (buffer-name) 0 -2))
1226 (read-from-minibuffer "Arguments: ")
1227 (read-from-minibuffer "Return values: ")))
1228 (let ((string (format "%s %s (%s)"
1230 ((string-equal vals "")
1232 ((string-match "[ ,]" vals)
1233 (concat " [" vals "] ="))
1235 (concat " " vals " =")))
1238 (prefix octave-block-comment-start))
1239 (if (not (bobp)) (newline))
1240 (insert "function" string)
1241 (indent-according-to-mode)
1243 (insert prefix "usage: " string)
1244 (reindent-then-newline-and-indent)
1246 (reindent-then-newline-and-indent)
1248 (indent-according-to-mode)
1251 (insert "endfunction")
1252 (indent-according-to-mode))))
1256 (defun octave-add-octave-menu ()
1257 "Adds the `Octave' menu to the menu bar in Octave mode."
1259 (easy-menu-define octave-mode-menu-map octave-mode-map
1260 "Menu keymap for Octave mode." octave-mode-menu)
1261 (easy-menu-add octave-mode-menu-map octave-mode-map))
1264 ;;; Communication with the inferior Octave process
1265 (defun octave-kill-process ()
1266 "Kill inferior Octave process and its buffer."
1268 (if inferior-octave-process
1270 (process-send-string inferior-octave-process "quit;\n")
1271 (accept-process-output inferior-octave-process)))
1272 (if inferior-octave-buffer
1273 (kill-buffer inferior-octave-buffer)))
1275 (defun octave-show-process-buffer ()
1276 "Make sure that `inferior-octave-buffer' is displayed."
1278 (if (get-buffer inferior-octave-buffer)
1279 (display-buffer inferior-octave-buffer)
1280 (message "No buffer named %s" inferior-octave-buffer)))
1282 (defun octave-hide-process-buffer ()
1283 "Delete all windows that display `inferior-octave-buffer'."
1285 (if (get-buffer inferior-octave-buffer)
1286 (delete-windows-on inferior-octave-buffer)
1287 (message "No buffer named %s" inferior-octave-buffer)))
1289 (defun octave-send-region (beg end)
1290 "Send current region to the inferior Octave process."
1293 (let ((proc inferior-octave-process)
1294 (string (buffer-substring-no-properties beg end))
1297 (set-buffer inferior-octave-buffer)
1298 (setq inferior-octave-output-list nil)
1299 (while (not (string-equal string ""))
1300 (if (string-match "\n" string)
1301 (setq line (substring string 0 (match-beginning 0))
1302 string (substring string (match-end 0)))
1303 (setq line string string ""))
1304 (setq inferior-octave-receive-in-progress t)
1305 (inferior-octave-send-list-and-digest (list (concat line "\n")))
1306 (while inferior-octave-receive-in-progress
1307 (accept-process-output proc))
1308 (insert-before-markers
1309 (mapconcat 'identity
1311 (if octave-send-echo-input (list line) (list ""))
1312 (mapcar 'inferior-octave-strip-ctrl-g
1313 inferior-octave-output-list)
1314 (list inferior-octave-output-string))
1316 (if octave-send-show-buffer
1317 (display-buffer inferior-octave-buffer)))
1319 (defun octave-send-block ()
1320 "Send current Octave block to the inferior Octave process."
1324 (octave-send-region (point) (mark))))
1326 (defun octave-send-defun ()
1327 "Send current Octave function to the inferior Octave process."
1331 (octave-send-region (point) (mark))))
1333 (defun octave-send-line (&optional arg)
1334 "Send current Octave code line to the inferior Octave process.
1335 With positive prefix ARG, send that many lines.
1336 If `octave-send-line-auto-forward' is non-nil, go to the next unsent
1339 (or arg (setq arg 1))
1344 (octave-next-code-line (- arg 1))
1347 (if octave-send-line-auto-forward
1348 (octave-next-code-line 1))
1349 (octave-send-region beg end))))
1351 (defun octave-eval-print-last-sexp ()
1352 "Evaluate Octave sexp before point and print value into current buffer."
1355 (let ((standard-output (current-buffer))
1356 (print-escape-newlines nil)
1362 (inferior-octave-send-list-and-digest
1363 (list (concat (buffer-substring-no-properties (point) opoint)
1365 (mapconcat 'identity inferior-octave-output-list "\n")))
1369 (defun octave-submit-bug-report ()
1370 "Submit a bug report on the Emacs Octave package via mail."
1374 (y-or-n-p "Do you want to submit a bug report? ")
1375 (reporter-submit-bug-report
1376 octave-maintainer-address
1377 (concat "Emacs version " emacs-version)
1379 'octave-auto-newline
1380 'octave-blink-matching-block
1381 'octave-block-offset
1382 'octave-comment-char
1383 'octave-continuation-offset
1384 'octave-continuation-string
1386 'octave-mode-startup-message
1387 'octave-send-echo-input
1388 'octave-send-line-auto-forward
1389 'octave-send-show-buffer))))
1393 (provide 'octave-mod)
1395 ;;; octave-mod.el ends here