1 ;;; octave-mod.el --- editing Octave source files under Emacs
3 ;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005
4 ;; Free Software Foundation, Inc.
6 ;; Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
7 ;; Author: John Eaton <jwe@bevo.che.wisc.edu>
8 ;; Maintainer: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; This package provides Emacs support for Octave.
31 ;; It defines Octave mode, a major mode for editing
34 ;; The file octave-hlp.el provides `octave-help', a facility for looking up
35 ;; documentation on a symbol in the Octave info files.
37 ;; The file octave-inf.el contains code for interacting with an inferior
38 ;; Octave process using comint.
40 ;; See the documentation of `octave-mode', `octave-help' and
41 ;; `run-octave' for further information on usage and customization.
47 "Major mode for editing Octave source files."
50 (defvar inferior-octave-output-list nil)
51 (defvar inferior-octave-output-string nil)
52 (defvar inferior-octave-receive-in-progress nil)
54 (defconst octave-maintainer-address
55 "Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>, bug-gnu-emacs@gnu.org"
56 "Current maintainer of the Emacs Octave package.")
58 (defvar octave-abbrev-table nil
59 "Abbrev table for Octave's reserved words.
60 Used in `octave-mode' and inferior-octave-mode buffers.
61 All Octave abbrevs start with a grave accent (`).")
62 (unless octave-abbrev-table
63 (let ((ac abbrevs-changed))
64 (define-abbrev-table 'octave-abbrev-table ())
65 (define-abbrev octave-abbrev-table "`a" "all_va_args" nil 0 t)
66 (define-abbrev octave-abbrev-table "`b" "break" nil 0 t)
67 (define-abbrev octave-abbrev-table "`cs" "case" nil 0 t)
68 (define-abbrev octave-abbrev-table "`ca" "catch" nil 0 t)
69 (define-abbrev octave-abbrev-table "`c" "continue" nil 0 t)
70 (define-abbrev octave-abbrev-table "`el" "else" nil 0 t)
71 (define-abbrev octave-abbrev-table "`eli" "elseif" nil 0 t)
72 (define-abbrev octave-abbrev-table "`et" "end_try_catch" nil 0 t)
73 (define-abbrev octave-abbrev-table "`eu" "end_unwind_protect" nil 0 t)
74 (define-abbrev octave-abbrev-table "`ef" "endfor" nil 0 t)
75 (define-abbrev octave-abbrev-table "`efu" "endfunction" nil 0 t)
76 (define-abbrev octave-abbrev-table "`ei" "endif" nil 0 t)
77 (define-abbrev octave-abbrev-table "`es" "endswitch" nil 0 t)
78 (define-abbrev octave-abbrev-table "`ew" "endwhile" nil 0 t)
79 (define-abbrev octave-abbrev-table "`f" "for" nil 0 t)
80 (define-abbrev octave-abbrev-table "`fu" "function" nil 0 t)
81 (define-abbrev octave-abbrev-table "`gl" "global" nil 0 t)
82 (define-abbrev octave-abbrev-table "`gp" "gplot" nil 0 t)
83 (define-abbrev octave-abbrev-table "`gs" "gsplot" nil 0 t)
84 (define-abbrev octave-abbrev-table "`if" "if ()" nil 0 t)
85 (define-abbrev octave-abbrev-table "`o" "otherwise" nil 0 t)
86 (define-abbrev octave-abbrev-table "`rp" "replot" nil 0 t)
87 (define-abbrev octave-abbrev-table "`r" "return" nil 0 t)
88 (define-abbrev octave-abbrev-table "`s" "switch" nil 0 t)
89 (define-abbrev octave-abbrev-table "`t" "try" nil 0 t)
90 (define-abbrev octave-abbrev-table "`up" "unwind_protect" nil 0 t)
91 (define-abbrev octave-abbrev-table "`upc" "unwind_protect_cleanup" nil 0 t)
92 (define-abbrev octave-abbrev-table "`w" "while ()" nil 0 t)
93 (setq abbrevs-changed ac)))
95 (defvar octave-comment-char ?#
96 "Character to start an Octave comment.")
97 (defvar octave-comment-start
98 (string octave-comment-char ?\ )
99 "String to insert to start a new Octave in-line comment.")
100 (defvar octave-comment-start-skip "\\s<+\\s-*"
101 "Regexp to match the start of an Octave comment up to its body.")
103 (defvar octave-begin-keywords
104 '("for" "function" "if" "switch" "try" "unwind_protect" "while"))
105 (defvar octave-else-keywords
106 '("case" "catch" "else" "elseif" "otherwise" "unwind_protect_cleanup"))
107 (defvar octave-end-keywords
108 '("end" "endfor" "endfunction" "endif" "endswitch" "end_try_catch"
109 "end_unwind_protect" "endwhile"))
111 (defvar octave-reserved-words
112 (append octave-begin-keywords
115 '("all_va_args" "break" "continue" "global" "gplot" "gsplot"
117 "Reserved words in Octave.")
119 (defvar octave-text-functions
120 '("casesen" "cd" "chdir" "clear" "diary" "dir" "document" "echo"
121 "edit_history" "format" "gset" "gshow" "help" "history" "hold"
122 "load" "ls" "more" "run_history" "save" "set" "show" "type"
123 "which" "who" "whos")
124 "Text functions in Octave (these names are also reserved).")
126 (defvar octave-variables
127 '("EDITOR" "EXEC_PATH" "F_DUPFD" "F_GETFD" "F_GETFL" "F_SETFD"
128 "F_SETFL" "I" "IMAGEPATH" "INFO_FILE" "INFO_PROGRAM" "Inf" "J"
129 "LOADPATH" "NaN" "OCTAVE_VERSION" "O_APPEND" "O_CREAT" "O_EXCL"
130 "O_NONBLOCK" "O_RDONLY" "O_RDWR" "O_TRUNC" "O_WRONLY" "PAGER" "PS1"
131 "PS2" "PS4" "PWD" "SEEK_CUR" "SEEK_END" "SEEK_SET" "__F_DUPFD__"
132 "__F_GETFD__" "__F_GETFL__" "__F_SETFD__" "__F_SETFL__" "__I__"
133 "__Inf__" "__J__" "__NaN__" "__OCTAVE_VERSION__" "__O_APPEND__"
134 "__O_CREAT__" "__O_EXCL__" "__O_NONBLOCK__" "__O_RDONLY__"
135 "__O_RDWR__" "__O_TRUNC__" "__O_WRONLY__" "__PWD__" "__SEEK_CUR__"
136 "__SEEK_END__" "__SEEK_SET__" "__argv__" "__e__" "__eps__"
137 "__error_text__" "__i__" "__inf__" "__j__" "__nan__" "__pi__"
138 "__program_invocation_name__" "__program_name__" "__realmax__"
139 "__realmin__" "__stderr__" "__stdin__" "__stdout__" "ans" "argv"
140 "automatic_replot" "beep_on_error" "completion_append_char"
141 "default_return_value" "default_save_format"
142 "define_all_return_values" "do_fortran_indexing" "e"
143 "echo_executing_commands" "empty_list_elements_ok" "eps"
144 "error_text" "gnuplot_binary" "gnuplot_has_multiplot" "history_file"
145 "history_size" "ignore_function_time_stamp" "implicit_str_to_num_ok"
146 "inf" "nan" "nargin" "ok_to_lose_imaginary_part"
147 "output_max_field_width" "output_precision"
148 "page_output_immediately" "page_screen_output" "pi"
149 "prefer_column_vectors" "prefer_zero_one_indexing"
150 "print_answer_id_name" "print_empty_dimensions"
151 "program_invocation_name" "program_name" "propagate_empty_matrices"
152 "realmax" "realmin" "resize_on_range_error"
153 "return_last_computed_value" "save_precision" "saving_history"
154 "silent_functions" "split_long_rows" "stderr" "stdin" "stdout"
155 "string_fill_char" "struct_levels_to_print"
156 "suppress_verbose_help_message" "treat_neg_dim_as_zero"
157 "warn_assign_as_truth_value" "warn_comma_in_global_decl"
158 "warn_divide_by_zero" "warn_function_name_clash"
159 "warn_missing_semicolon" "whitespace_in_literal_matrix")
160 "Builtin variables in Octave.")
162 (defvar octave-function-header-regexp
163 (concat "^\\s-*\\<\\(function\\)\\>"
164 "\\([^=;\n]*=[ \t]*\\|[ \t]*\\)\\(\\w+\\)\\>")
165 "Regexp to match an Octave function header.
166 The string `function' and its name are given by the first and third
167 parenthetical grouping.")
169 (defvar octave-font-lock-keywords
171 ;; Fontify all builtin keywords.
172 (cons (concat "\\<\\("
173 (mapconcat 'identity octave-reserved-words "\\|")
174 (mapconcat 'identity octave-text-functions "\\|")
176 'font-lock-keyword-face)
177 ;; Fontify all builtin operators.
178 (cons "\\(&\\||\\|<=\\|>=\\|==\\|<\\|>\\|!=\\|!\\)"
179 (if (boundp 'font-lock-builtin-face)
180 'font-lock-builtin-face
181 'font-lock-preprocessor-face))
182 ;; Fontify all builtin variables.
183 (cons (concat "\\<\\("
184 (mapconcat 'identity octave-variables "\\|")
186 'font-lock-variable-name-face)
187 ;; Fontify all function declarations.
188 (list octave-function-header-regexp
189 '(1 font-lock-keyword-face)
190 '(3 font-lock-function-name-face nil t)))
191 "Additional Octave expressions to highlight.")
193 (defcustom inferior-octave-buffer "*Inferior Octave*"
194 "*Name of buffer for running an inferior Octave process."
196 :group 'octave-inferior)
198 (defvar inferior-octave-process nil)
200 (defvar octave-mode-map nil
201 "Keymap used in Octave mode.")
204 (let ((map (make-sparse-keymap)))
205 (define-key map "`" 'octave-abbrev-start)
206 (define-key map ";" 'octave-electric-semi)
207 (define-key map " " 'octave-electric-space)
208 (define-key map "\n" 'octave-reindent-then-newline-and-indent)
209 (define-key map "\t" 'indent-according-to-mode)
210 (define-key map "\e;" 'octave-indent-for-comment)
211 (define-key map "\e\n" 'octave-indent-new-comment-line)
212 (define-key map "\e\t" 'octave-complete-symbol)
213 (define-key map "\M-\C-a" 'octave-beginning-of-defun)
214 (define-key map "\M-\C-e" 'octave-end-of-defun)
215 (define-key map "\M-\C-h" 'octave-mark-defun)
216 (define-key map "\M-\C-q" 'octave-indent-defun)
217 (define-key map "\C-c;" 'octave-comment-region)
218 (define-key map "\C-c:" 'octave-uncomment-region)
219 (define-key map "\C-c\C-b" 'octave-submit-bug-report)
220 (define-key map "\C-c\C-p" 'octave-previous-code-line)
221 (define-key map "\C-c\C-n" 'octave-next-code-line)
222 (define-key map "\C-c\C-a" 'octave-beginning-of-line)
223 (define-key map "\C-c\C-e" 'octave-end-of-line)
224 (define-key map "\C-c\M-\C-n" 'octave-forward-block)
225 (define-key map "\C-c\M-\C-p" 'octave-backward-block)
226 (define-key map "\C-c\M-\C-u" 'octave-backward-up-block)
227 (define-key map "\C-c\M-\C-d" 'octave-down-block)
228 (define-key map "\C-c\M-\C-h" 'octave-mark-block)
229 (define-key map "\C-c]" 'octave-close-block)
230 (define-key map "\C-c\C-f" 'octave-insert-defun)
231 (define-key map "\C-c\C-h" 'octave-help)
232 (define-key map "\C-c\C-il" 'octave-send-line)
233 (define-key map "\C-c\C-ib" 'octave-send-block)
234 (define-key map "\C-c\C-if" 'octave-send-defun)
235 (define-key map "\C-c\C-ir" 'octave-send-region)
236 (define-key map "\C-c\C-is" 'octave-show-process-buffer)
237 (define-key map "\C-c\C-ih" 'octave-hide-process-buffer)
238 (define-key map "\C-c\C-ik" 'octave-kill-process)
239 (define-key map "\C-c\C-i\C-l" 'octave-send-line)
240 (define-key map "\C-c\C-i\C-b" 'octave-send-block)
241 (define-key map "\C-c\C-i\C-f" 'octave-send-defun)
242 (define-key map "\C-c\C-i\C-r" 'octave-send-region)
243 (define-key map "\C-c\C-i\C-s" 'octave-show-process-buffer)
244 (define-key map "\C-c\C-i\C-h" 'octave-hide-process-buffer)
245 (define-key map "\C-c\C-i\C-k" 'octave-kill-process)
246 (setq octave-mode-map map)))
248 (defvar octave-mode-menu
251 ["Previous Code Line" octave-previous-code-line t]
252 ["Next Code Line" octave-next-code-line t]
253 ["Begin of Continuation" octave-beginning-of-line t]
254 ["End of Continuation" octave-end-of-line t]
255 ["Split Line at Point" octave-indent-new-comment-line t])
257 ["Next Block" octave-forward-block t]
258 ["Previous Block" octave-backward-block t]
259 ["Down Block" octave-down-block t]
260 ["Up Block" octave-backward-up-block t]
261 ["Mark Block" octave-mark-block t]
262 ["Close Block" octave-close-block t])
264 ["Begin of Function" octave-beginning-of-defun t]
265 ["End of Function" octave-end-of-defun t]
266 ["Mark Function" octave-mark-defun t]
267 ["Indent Function" octave-indent-defun t]
268 ["Insert Function" octave-insert-defun t])
271 ["Send Current Line" octave-send-line t]
272 ["Send Current Block" octave-send-block t]
273 ["Send Current Function" octave-send-defun t]
274 ["Send Region" octave-send-region t]
275 ["Show Process Buffer" octave-show-process-buffer t]
276 ["Hide Process Buffer" octave-hide-process-buffer t]
277 ["Kill Process" octave-kill-process t])
279 ["Indent Line" indent-according-to-mode t]
280 ["Complete Symbol" octave-complete-symbol t]
282 ["Toggle Abbrev Mode" abbrev-mode t]
283 ["Toggle Auto-Fill Mode" auto-fill-mode t]
285 ["Submit Bug Report" octave-submit-bug-report t]
287 ["Describe Octave Mode" octave-describe-major-mode t]
288 ["Lookup Octave Index" octave-help t])
289 "Menu for Octave mode.")
291 (defvar octave-mode-syntax-table
292 (let ((table (make-syntax-table)))
293 (modify-syntax-entry ?\r " " table)
294 (modify-syntax-entry ?+ "." table)
295 (modify-syntax-entry ?- "." table)
296 (modify-syntax-entry ?= "." table)
297 (modify-syntax-entry ?* "." table)
298 (modify-syntax-entry ?/ "." table)
299 (modify-syntax-entry ?> "." table)
300 (modify-syntax-entry ?< "." table)
301 (modify-syntax-entry ?& "." table)
302 (modify-syntax-entry ?| "." table)
303 (modify-syntax-entry ?! "." table)
304 (modify-syntax-entry ?\\ "\\" table)
305 (modify-syntax-entry ?\' "." table)
306 (modify-syntax-entry ?\` "w" table)
307 (modify-syntax-entry ?\" "\"" table)
308 (modify-syntax-entry ?. "w" table)
309 (modify-syntax-entry ?_ "w" table)
310 (modify-syntax-entry ?\% "<" table)
311 (modify-syntax-entry ?\# "<" table)
312 (modify-syntax-entry ?\n ">" table)
314 "Syntax table in use in `octave-mode' buffers.")
316 (defcustom octave-auto-indent nil
317 "*Non-nil means indent line after a semicolon or space in Octave mode."
321 (defcustom octave-auto-newline nil
322 "*Non-nil means automatically newline after a semicolon in Octave mode."
326 (defcustom octave-blink-matching-block t
327 "*Control the blinking of matching Octave block keywords.
328 Non-nil means show matching begin of block when inserting a space,
329 newline or semicolon after an else or end keyword."
332 (defcustom octave-block-offset 2
333 "*Extra indentation applied to statements in Octave block structures."
337 (defvar octave-block-begin-regexp
339 (mapconcat 'identity octave-begin-keywords "\\|")
341 (defvar octave-block-else-regexp
343 (mapconcat 'identity octave-else-keywords "\\|")
345 (defvar octave-block-end-regexp
347 (mapconcat 'identity octave-end-keywords "\\|")
349 (defvar octave-block-begin-or-end-regexp
350 (concat octave-block-begin-regexp "\\|" octave-block-end-regexp))
351 (defvar octave-block-else-or-end-regexp
352 (concat octave-block-else-regexp "\\|" octave-block-end-regexp))
353 (defvar octave-block-match-alist
354 '(("for" . ("end" "endfor"))
355 ("function" . ("end" "endfunction"))
356 ("if" . ("else" "elseif" "end" "endif"))
357 ("switch" . ("case" "otherwise" "end" "endswitch"))
358 ("try" . ("catch" "end" "end_try_catch"))
359 ("unwind_protect" . ("unwind_protect_cleanup" "end"
360 "end_unwind_protect"))
361 ("while" . ("end" "endwhile")))
362 "Alist with Octave's matching block keywords.
363 Has Octave's begin keywords as keys and a list of the matching else or
364 end keywords as associated values.")
366 (defvar octave-block-comment-start
367 (concat (make-string 2 octave-comment-char) " ")
368 "String to insert to start a new Octave comment on an empty line.")
370 (defcustom octave-continuation-offset 4
371 "*Extra indentation applied to Octave continuation lines."
374 (defvar octave-continuation-regexp
375 "[^#%\n]*\\(\\\\\\|\\.\\.\\.\\)\\s-*\\(\\s<.*\\)?$")
376 (defcustom octave-continuation-string "\\"
377 "*Character string used for Octave continuation lines. Normally \\."
381 (defvar octave-completion-alist nil
382 "Alist of Octave symbols for completion in Octave mode.
383 Each element looks like (VAR . VAR), where the car and cdr are the same
384 symbol (an Octave command or variable name).
385 Currently, only builtin variables can be completed.")
387 (defvar octave-mode-imenu-generic-expression
390 (list nil octave-function-header-regexp 3))
391 "Imenu expression for Octave mode. See `imenu-generic-expression'.")
393 (defcustom octave-mode-startup-message t
394 "*nil means do not display the Octave mode startup message."
398 (defcustom octave-mode-hook nil
399 "*Hook to be run when Octave mode is started."
403 (defcustom octave-send-show-buffer t
404 "*Non-nil means display `inferior-octave-buffer' after sending to it."
407 (defcustom octave-send-line-auto-forward t
408 "*Control auto-forward after sending to the inferior Octave process.
409 Non-nil means always go to the next Octave code line after sending."
412 (defcustom octave-send-echo-input t
413 "*Non-nil means echo input sent to the inferior Octave process."
419 (defun octave-mode ()
420 "Major mode for editing Octave code.
422 This mode makes it easier to write Octave code by helping with
423 indentation, doing some of the typing for you (with Abbrev mode) and by
424 showing keywords, comments, strings, etc. in different faces (with
425 Font Lock mode on terminals that support it).
427 Octave itself is a high-level language, primarily intended for numerical
428 computations. It provides a convenient command line interface for
429 solving linear and nonlinear problems numerically. Function definitions
430 can also be stored in files, and it can be used in a batch mode (which
431 is why you need this mode!).
433 The latest released version of Octave is always available via anonymous
434 ftp from bevo.che.wisc.edu in the directory `/pub/octave'. Complete
435 source and binaries for several popular systems are available.
437 Type \\[list-abbrevs] to display the built-in abbrevs for Octave keywords.
444 Variables you can use to customize Octave mode
445 ==============================================
448 Non-nil means indent current line after a semicolon or space.
452 Non-nil means auto-insert a newline and indent after a semicolon.
455 octave-blink-matching-block
456 Non-nil means show matching begin of block when inserting a space,
457 newline or semicolon after an else or end keyword. Default is t.
460 Extra indentation applied to statements in block structures.
463 octave-continuation-offset
464 Extra indentation applied to Octave continuation lines.
467 octave-continuation-string
468 String used for Octave continuation lines.
469 Default is a backslash.
471 octave-mode-startup-message
472 nil means do not display the Octave mode startup message.
475 octave-send-echo-input
476 Non-nil means always display `inferior-octave-buffer' after sending a
477 command to the inferior Octave process.
479 octave-send-line-auto-forward
480 Non-nil means always go to the next unsent line of Octave code after
481 sending a line to the inferior Octave process.
483 octave-send-echo-input
484 Non-nil means echo input sent to the inferior Octave process.
486 Turning on Octave mode runs the hook `octave-mode-hook'.
488 To begin using this mode for all `.m' files that you edit, add the
489 following lines to your `.emacs' file:
491 (autoload 'octave-mode \"octave-mod\" nil t)
492 (setq auto-mode-alist
493 (cons '(\"\\\\.m$\" . octave-mode) auto-mode-alist))
495 To automatically turn on the abbrev, auto-fill and font-lock features,
496 add the following lines to your `.emacs' file as well:
498 (add-hook 'octave-mode-hook
502 (if (eq window-system 'x)
503 (font-lock-mode 1))))
505 To submit a problem report, enter \\[octave-submit-bug-report] from \
506 an Octave mode buffer.
507 This automatically sets up a mail buffer with version information
508 already added. You just need to add a description of the problem,
509 including a reproducible test case and send the message."
511 (kill-all-local-variables)
513 (use-local-map octave-mode-map)
514 (setq major-mode 'octave-mode)
515 (setq mode-name "Octave")
516 (setq local-abbrev-table octave-abbrev-table)
517 (set-syntax-table octave-mode-syntax-table)
519 (make-local-variable 'indent-line-function)
520 (setq indent-line-function 'octave-indent-line)
522 (make-local-variable 'comment-start)
523 (setq comment-start octave-comment-start)
524 (make-local-variable 'comment-end)
525 (setq comment-end "")
526 (make-local-variable 'comment-column)
527 (setq comment-column 32)
528 (make-local-variable 'comment-start-skip)
529 (setq comment-start-skip "\\s<+\\s-*")
530 (make-local-variable 'comment-indent-function)
531 (setq comment-indent-function 'octave-comment-indent)
533 (make-local-variable 'parse-sexp-ignore-comments)
534 (setq parse-sexp-ignore-comments t)
535 (make-local-variable 'paragraph-start)
536 (setq paragraph-start (concat "\\s-*$\\|" page-delimiter))
537 (make-local-variable 'paragraph-separate)
538 (setq paragraph-separate paragraph-start)
539 (make-local-variable 'paragraph-ignore-fill-prefix)
540 (setq paragraph-ignore-fill-prefix t)
541 (make-local-variable 'fill-paragraph-function)
542 (setq fill-paragraph-function 'octave-fill-paragraph)
543 (make-local-variable 'adaptive-fill-regexp)
544 (setq adaptive-fill-regexp nil)
545 (make-local-variable 'fill-column)
546 (setq fill-column 72)
547 (make-local-variable 'normal-auto-fill-function)
548 (setq normal-auto-fill-function 'octave-auto-fill)
550 (make-local-variable 'font-lock-defaults)
551 (setq font-lock-defaults '(octave-font-lock-keywords nil nil))
553 (make-local-variable 'imenu-generic-expression)
554 (setq imenu-generic-expression octave-mode-imenu-generic-expression
555 imenu-case-fold-search nil)
557 (octave-add-octave-menu)
558 (octave-initialize-completions)
559 (run-mode-hooks 'octave-mode-hook))
561 ;;; Miscellaneous useful functions
562 (defun octave-describe-major-mode ()
563 "Describe the current major mode."
565 (describe-function major-mode))
567 (defsubst octave-in-comment-p ()
568 "Returns t if point is inside an Octave comment, nil otherwise."
571 (nth 4 (parse-partial-sexp (line-beginning-position) (point)))))
573 (defsubst octave-in-string-p ()
574 "Returns t if point is inside an Octave string, nil otherwise."
577 (nth 3 (parse-partial-sexp (line-beginning-position) (point)))))
579 (defsubst octave-not-in-string-or-comment-p ()
580 "Returns t iff point is not inside an Octave string or comment."
581 (let ((pps (parse-partial-sexp (line-beginning-position) (point))))
582 (not (or (nth 3 pps) (nth 4 pps)))))
584 (defun octave-in-block-p ()
585 "Returns t if point is inside an Octave block, nil otherwise.
586 The block is taken to start at the first letter of the begin keyword and
587 to end after the end keyword."
592 (skip-syntax-forward "w")
594 (octave-forward-block)
599 (defun octave-in-defun-p ()
600 "Returns t iff point is inside an Octave function declaration.
601 The function is taken to start at the `f' of `function' and to end after
605 (or (and (looking-at "\\<function\\>")
606 (octave-not-in-string-or-comment-p))
607 (and (octave-beginning-of-defun)
610 (octave-forward-block)
615 (defun octave-maybe-insert-continuation-string ()
616 (if (or (octave-in-comment-p)
619 (looking-at octave-continuation-regexp)))
621 (delete-horizontal-space)
622 (insert (concat " " octave-continuation-string))))
624 (defvar octave-xemacs-p
625 (string-match "XEmacs\\|Lucid" emacs-version))
628 (defun octave-comment-region (beg end &optional arg)
629 "Comment or uncomment each line in the region as Octave code.
630 See `comment-region'."
632 (let ((comment-start (char-to-string octave-comment-char)))
633 (comment-region beg end arg)))
635 (defun octave-uncomment-region (beg end &optional arg)
636 "Uncomment each line in the region as Octave code."
638 (or arg (setq arg 1))
639 (octave-comment-region beg end (- arg)))
643 (defun calculate-octave-indent ()
644 "Return appropriate indentation for current line as Octave code.
645 Returns an integer (the column to indent to) unless the line is a
646 comment line with fixed goal golumn. In that case, returns a list whose
647 car is the column to indent to, and whose cdr is the current indentation
649 (let ((is-continuation-line
651 (if (zerop (octave-previous-code-line))
652 (looking-at octave-continuation-regexp))))
656 ;; If we can move backward out one level of parentheses, take 1
657 ;; plus the indentation of that parenthesis. Otherwise, go back
658 ;; to the beginning of the previous code line, and compute the
659 ;; offset this line gives.
660 (if (condition-case nil
665 (setq icol (+ 1 (current-column)))
666 (if (zerop (octave-previous-code-line))
668 (octave-beginning-of-line)
669 (back-to-indentation)
670 (setq icol (current-column))
672 (eol (line-end-position)))
673 (while (< (point) eol)
674 (if (octave-not-in-string-or-comment-p)
676 ((looking-at "\\<switch\\>")
677 (setq icol (+ icol (* 2 octave-block-offset))))
678 ((looking-at octave-block-begin-regexp)
679 (setq icol (+ icol octave-block-offset)))
680 ((looking-at octave-block-else-regexp)
682 (setq icol (+ icol octave-block-offset))))
683 ((looking-at octave-block-end-regexp)
684 (if (not (= bot (point)))
686 (octave-block-end-offset)))))))
688 (if is-continuation-line
689 (setq icol (+ icol octave-continuation-offset)))))))
691 (back-to-indentation)
693 ((and (looking-at octave-block-else-regexp)
694 (octave-not-in-string-or-comment-p))
695 (setq icol (- icol octave-block-offset)))
696 ((and (looking-at octave-block-end-regexp)
697 (octave-not-in-string-or-comment-p))
698 (setq icol (- icol (octave-block-end-offset))))
699 ((or (looking-at "\\s<\\s<\\s<\\S<")
700 (octave-before-magic-comment-p))
701 (setq icol (list 0 icol)))
702 ((looking-at "\\s<\\S<")
703 (setq icol (list comment-column icol)))))
706 (defun octave-block-end-offset ()
708 (octave-backward-up-block 1)
709 (* octave-block-offset
710 (if (string-match (match-string 0) "switch") 2 1))))
712 (defun octave-before-magic-comment-p ()
715 (and (bobp) (looking-at "\\s-*#!"))))
717 (defun octave-comment-indent ()
718 (if (or (looking-at "\\s<\\s<\\s<")
719 (octave-before-magic-comment-p))
721 (if (looking-at "\\s<\\s<")
722 (calculate-octave-indent)
723 (skip-syntax-backward " ")
724 (max (if (bolp) 0 (+ 1 (current-column)))
727 (defun octave-indent-for-comment ()
728 "Maybe insert and indent an Octave comment.
729 If there is no comment already on this line, create a code-level comment
730 \(started by two comment characters) if the line is empty, or an in-line
731 comment (started by one comment character) otherwise.
732 Point is left after the start of the comment which is properly aligned."
735 (indent-according-to-mode))
737 (defun octave-indent-line (&optional arg)
738 "Indent current line as Octave code.
739 With optional ARG, use this as offset unless this line is a comment with
742 (or arg (setq arg 0))
743 (let ((icol (calculate-octave-indent))
744 (relpos (- (current-column) (current-indentation))))
746 (setq icol (car icol))
747 (setq icol (+ icol arg)))
749 (error "Unmatched end keyword")
750 (indent-line-to icol)
752 (move-to-column (+ icol relpos))))))
754 (defun octave-indent-new-comment-line ()
755 "Break Octave line at point, continuing comment if within one.
756 If within code, insert `octave-continuation-string' before breaking the
757 line. If within a string, signal an error.
758 The new line is properly indented."
760 (delete-horizontal-space)
762 ((octave-in-comment-p)
763 (indent-new-comment-line))
764 ((octave-in-string-p)
765 (error "Cannot split a code line inside a string"))
767 (insert (concat " " octave-continuation-string))
768 (octave-reindent-then-newline-and-indent))))
770 (defun octave-indent-defun ()
771 "Properly indents the Octave function which contains point."
775 (message "Indenting function...")
776 (indent-region (point) (mark) nil))
777 (message "Indenting function...done."))
781 (defun octave-next-code-line (&optional arg)
782 "Move ARG lines of Octave code forward (backward if ARG is negative).
783 Skips past all empty and comment lines. Default for ARG is 1.
785 On success, return 0. Otherwise, go as far as possible and return -1."
787 (or arg (setq arg 1))
790 (inc (if (> arg 0) 1 -1)))
791 (while (and (/= arg 0) (= n 0))
792 (setq n (forward-line inc))
794 (looking-at "\\s-*\\($\\|\\s<\\)"))
795 (setq n (forward-line inc)))
796 (setq arg (- arg inc)))
799 (defun octave-previous-code-line (&optional arg)
800 "Move ARG lines of Octave code backward (forward if ARG is negative).
801 Skips past all empty and comment lines. Default for ARG is 1.
803 On success, return 0. Otherwise, go as far as possible and return -1."
805 (or arg (setq arg 1))
806 (octave-next-code-line (- arg)))
808 (defun octave-beginning-of-line ()
809 "Move point to beginning of current Octave line.
810 If on an empty or comment line, go to the beginning of that line.
811 Otherwise, move backward to the beginning of the first Octave code line
812 which is not inside a continuation statement, i.e., which does not
813 follow a code line ending in `...' or `\\', or is inside an open
817 (if (not (looking-at "\\s-*\\($\\|\\s<\\)"))
818 (while (or (condition-case nil
824 (and (or (looking-at "\\s-*\\($\\|\\s<\\)")
826 (if (zerop (octave-previous-code-line))
827 (looking-at octave-continuation-regexp))))
828 (zerop (forward-line -1)))))))
830 (defun octave-end-of-line ()
831 "Move point to end of current Octave line.
832 If on an empty or comment line, go to the end of that line.
833 Otherwise, move forward to the end of the first Octave code line which
834 does not end in `...' or `\\' or is inside an open parenthesis list."
839 (looking-at "\\s-*\\($\\|\\s<\\)"))
841 (while (or (condition-case nil
849 (or (looking-at "\\s-*\\($\\|\\s<\\)")
850 (looking-at octave-continuation-regexp)))
851 (zerop (forward-line 1)))))
854 (defun octave-scan-blocks (from count depth)
855 "Scan from character number FROM by COUNT Octave begin-end blocks.
856 Returns the character number of the position thus found.
858 If DEPTH is nonzero, block depth begins counting from that value.
859 Only places where the depth in blocks becomes zero are candidates for
860 stopping; COUNT such places are counted.
862 If the beginning or end of the buffer is reached and the depth is wrong,
863 an error is signaled."
864 (let ((min-depth (if (> depth 0) 0 depth))
865 (inc (if (> count 0) 1 -1)))
869 (while (or (re-search-forward
870 octave-block-begin-or-end-regexp nil 'move inc)
872 (error "Unbalanced block")))
873 (if (octave-not-in-string-or-comment-p)
877 (setq depth (+ depth inc)))
879 (setq depth (- depth inc))))
880 (if (< depth min-depth)
881 (error "Containing expression ends prematurely"))
883 (throw 'foo nil))))))
884 (setq count (- count inc)))
887 (defun octave-forward-block (&optional arg)
888 "Move forward across one balanced Octave begin-end block.
889 With argument, do it that many times.
890 Negative arg -N means move backward across N blocks."
892 (or arg (setq arg 1))
893 (goto-char (or (octave-scan-blocks (point) arg 0) (buffer-end arg))))
895 (defun octave-backward-block (&optional arg)
896 "Move backward across one balanced Octave begin-end block.
897 With argument, do it that many times.
898 Negative arg -N means move forward across N blocks."
900 (or arg (setq arg 1))
901 (octave-forward-block (- arg)))
903 (defun octave-down-block (arg)
904 "Move forward down one begin-end block level of Octave code.
905 With argument, do this that many times.
906 A negative argument means move backward but still go down a level.
907 In Lisp programs, an argument is required."
909 (let ((inc (if (> arg 0) 1 -1)))
911 (goto-char (or (octave-scan-blocks (point) inc -1)
913 (setq arg (- arg inc)))))
915 (defun octave-backward-up-block (arg)
916 "Move backward out of one begin-end block level of Octave code.
917 With argument, do this that many times.
918 A negative argument means move forward but still to a less deep spot.
919 In Lisp programs, an argument is required."
921 (octave-up-block (- arg)))
923 (defun octave-up-block (arg)
924 "Move forward out of one begin-end block level of Octave code.
925 With argument, do this that many times.
926 A negative argument means move backward but still to a less deep spot.
927 In Lisp programs, an argument is required."
929 (let ((inc (if (> arg 0) 1 -1)))
931 (goto-char (or (octave-scan-blocks (point) inc 1)
933 (setq arg (- arg inc)))))
935 (defun octave-mark-block ()
936 "Put point at the beginning of this Octave block, mark at the end.
937 The block marked is the one that contains point or follows point."
940 (if (or (and (octave-in-block-p)
941 (skip-syntax-forward "w"))
944 (octave-down-block 1)
950 (octave-forward-block)
951 (exchange-point-and-mark))
953 (message "No block to mark found"))))
955 (defun octave-close-block ()
956 "Close the current Octave block on a separate line.
957 An error is signaled if no block to close is found."
963 (octave-backward-up-block 1)
964 (setq bb-keyword (buffer-substring-no-properties
965 (match-beginning 1) (match-end 1))))
968 (looking-at "^\\s-*$"))
969 (indent-according-to-mode)
970 (octave-reindent-then-newline-and-indent))
971 (insert (car (reverse
973 octave-block-match-alist))))
974 (octave-reindent-then-newline-and-indent)
976 (error (message "No block to close found")))))
978 (defun octave-blink-matching-block-open ()
979 "Blink the matching Octave begin block keyword.
980 If point is right after an Octave else or end type block keyword, move
981 cursor momentarily to the corresponding begin keyword.
982 Signal an error if the keywords are incompatible."
984 (let (bb-keyword bb-arg eb-keyword pos eol)
985 (if (and (octave-not-in-string-or-comment-p)
988 (skip-syntax-backward "w")
989 (looking-at octave-block-else-or-end-regexp)))
994 (buffer-substring-no-properties
995 (match-beginning 1) (match-end 1)))
996 (octave-backward-up-block 1))
999 (buffer-substring-no-properties
1000 (match-beginning 2) (match-end 2)))
1001 (octave-backward-block)))
1002 (setq pos (match-end 0)
1004 (buffer-substring-no-properties
1005 (match-beginning 0) pos)
1007 eol (line-end-position)
1012 (while (and (skip-syntax-forward "^<" eol)
1013 (octave-in-string-p)
1014 (not (forward-char 1))))
1015 (skip-syntax-backward " ")
1016 (buffer-substring-no-properties pos (point)))))
1017 (if (member eb-keyword
1018 (cdr (assoc bb-keyword octave-block-match-alist)))
1020 (message "Matches `%s %s'" bb-keyword bb-arg)
1021 (if (pos-visible-in-window-p)
1022 (sit-for blink-matching-delay)))
1023 (error "Block keywords `%s' and `%s' do not match"
1024 bb-keyword eb-keyword))))))
1026 (defun octave-beginning-of-defun (&optional arg)
1027 "Move backward to the beginning of an Octave function.
1028 With positive ARG, do it that many times. Negative argument -N means
1029 move forward to Nth following beginning of a function.
1030 Returns t unless search stops at the beginning or end of the buffer."
1032 (let* ((arg (or arg 1))
1033 (inc (if (> arg 0) 1 -1))
1036 (not (and (> arg 0) (looking-at "\\<function\\>")))
1037 (skip-syntax-forward "w"))
1038 (while (and (/= arg 0)
1040 (re-search-backward "\\<function\\>" nil 'move inc)))
1041 (if (octave-not-in-string-or-comment-p)
1042 (setq arg (- arg inc))))
1045 (and (< inc 0) (goto-char (match-beginning 0)))
1048 (defun octave-end-of-defun (&optional arg)
1049 "Move forward to the end of an Octave function.
1050 With positive ARG, do it that many times. Negative argument -N means
1051 move back to Nth preceding end of a function.
1053 An end of a function occurs right after the end keyword matching the
1054 `function' keyword that starts the function."
1056 (or arg (setq arg 1))
1057 (and (< arg 0) (skip-syntax-backward "w"))
1058 (and (> arg 0) (skip-syntax-forward "w"))
1059 (if (octave-in-defun-p)
1060 (setq arg (- arg 1)))
1061 (if (= arg 0) (setq arg -1))
1062 (if (octave-beginning-of-defun (- arg))
1063 (octave-forward-block)))
1065 (defun octave-mark-defun ()
1066 "Put point at the beginning of this Octave function, mark at its end.
1067 The function marked is the one containing point or following point."
1069 (let ((pos (point)))
1070 (if (or (octave-in-defun-p)
1071 (and (octave-beginning-of-defun -1)
1072 (octave-in-defun-p)))
1074 (skip-syntax-forward "w")
1075 (octave-beginning-of-defun)
1077 (octave-end-of-defun)
1078 (exchange-point-and-mark))
1080 (message "No function to mark found"))))
1084 (defun octave-auto-fill ()
1085 "Perform auto-fill in Octave mode.
1086 Returns nil if no feasible place to break the line could be found, and t
1089 (if (or (null (setq fc (current-fill-column)))
1092 (and auto-fill-inhibit-regexp
1093 (looking-at auto-fill-inhibit-regexp))))
1094 nil ; Can't do anything
1095 (if (and (not (octave-in-comment-p))
1096 (> (current-column) fc))
1097 (setq fc (- fc (+ (length octave-continuation-string) 1))))
1098 (while (and (not give-up) (> (current-column) fc))
1099 (let* ((opoint (point))
1102 (move-to-column (+ fc 1))
1103 (skip-chars-backward "^ \t\n")
1104 ;; If we're at the beginning of the line, break after
1107 (re-search-forward "[ \t]" opoint t))
1108 ;; If we're in a comment line, don't break after the
1111 (skip-syntax-backward " <")
1113 (re-search-forward "[ \t]" (line-end-position)
1115 ;; If we're not in a comment line and just ahead the
1116 ;; continuation string, don't break here.
1117 (if (and (not (octave-in-comment-p))
1121 octave-continuation-string)
1124 (skip-chars-backward " \t")
1128 (not (or (bolp) (eolp))))
1129 (let ((prev-column (current-column)))
1131 (skip-chars-backward " \t")
1134 (octave-maybe-insert-continuation-string)
1135 (indent-new-comment-line t))
1138 (octave-maybe-insert-continuation-string)
1139 (indent-new-comment-line t)))
1140 (if (>= (current-column) prev-column)
1145 (defun octave-fill-paragraph (&optional arg)
1146 "Fill paragraph of Octave code, handling Octave comments."
1149 (let ((end (progn (forward-paragraph) (point)))
1151 (forward-paragraph -1)
1152 (skip-chars-forward " \t\n")
1155 (cfc (current-fill-column))
1156 (ind (calculate-octave-indent))
1160 (narrow-to-region beg end)
1161 (if (listp ind) (setq ind (nth 1 ind)))
1164 (octave-indent-line ind)
1170 (looking-at "^\\s-*\\($\\|\\s<+\\)"))))
1172 (move-to-column cfc)
1173 ;; First check whether we need to combine non-empty comment lines
1174 (if (and (< (current-column) cfc)
1175 (octave-in-comment-p)
1176 (not (save-excursion
1178 (looking-at "^\\s-*\\s<+\\s-*$"))))
1179 ;; This is a nonempty comment line which does not extend
1180 ;; past the fill column. If it is followed by a nonempty
1181 ;; comment line with the same comment prefix, try to
1182 ;; combine them, and repeat this until either we reach the
1183 ;; fill-column or there is nothing more to combine.
1185 ;; Get the comment prefix
1188 (while (and (re-search-forward "\\s<+")
1189 (not (octave-in-comment-p))))
1190 (setq comment-prefix (match-string 0)))
1191 ;; And keep combining ...
1192 (while (and (< (current-column) cfc)
1204 (re-search-forward comment-prefix)
1205 (delete-region (match-beginning 0) (match-end 0))
1207 (move-to-column cfc))))
1208 ;; We might also try to combine continued code lines> Perhaps
1209 ;; some other time ...
1210 (skip-chars-forward "^ \t\n")
1211 (delete-horizontal-space)
1212 (if (or (< (current-column) cfc)
1213 (and (= (current-column) cfc) (eolp)))
1215 (if (not (eolp)) (insert " "))
1216 (or (octave-auto-fill)
1217 (forward-line 1)))))
1222 (defun octave-initialize-completions ()
1223 "Create an alist for Octave completions."
1224 (if octave-completion-alist
1226 (setq octave-completion-alist
1227 (mapcar '(lambda (var) (cons var var))
1228 (append octave-reserved-words
1229 octave-text-functions
1230 octave-variables)))))
1232 (defun octave-complete-symbol ()
1233 "Perform completion on Octave symbol preceding point.
1234 Compare that symbol against Octave's reserved words and builtin
1236 ;; This code taken from lisp-complete-symbol
1238 (let* ((end (point))
1239 (beg (save-excursion (backward-sexp 1) (point)))
1240 (string (buffer-substring-no-properties beg end))
1241 (completion (try-completion string octave-completion-alist)))
1242 (cond ((eq completion t)) ; ???
1244 (message "Can't find completion for \"%s\"" string)
1246 ((not (string= string completion))
1247 (delete-region beg end)
1248 (insert completion))
1250 (let ((list (all-completions string octave-completion-alist))
1251 (conf (current-window-configuration)))
1252 ;; Taken from comint.el
1253 (message "Making completion list...")
1254 (with-output-to-temp-buffer "*Completions*"
1255 (display-completion-list list))
1256 (message "Hit space to flush")
1259 (set-buffer (get-buffer "*Completions*"))
1260 (setq key (read-key-sequence nil)
1262 (and (consp first) (consp (event-start first))
1263 (eq (window-buffer (posn-window (event-start
1265 (get-buffer "*Completions*"))
1266 (eq (key-binding key) 'mouse-choose-completion)))
1268 (mouse-choose-completion first)
1269 (set-window-configuration conf))
1271 (set-window-configuration conf)
1272 (setq unread-command-events
1273 (listify-key-sequence key))))))))))
1276 ;;; Electric characters && friends
1277 (defun octave-reindent-then-newline-and-indent ()
1278 "Reindent current Octave line, insert newline, and indent the new line.
1279 If Abbrev mode is on, expand abbrevs first."
1281 (if abbrev-mode (expand-abbrev))
1282 (if octave-blink-matching-block
1283 (octave-blink-matching-block-open))
1285 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
1286 (indent-according-to-mode))
1288 (indent-according-to-mode))
1290 (defun octave-electric-semi ()
1291 "Insert a semicolon in Octave mode.
1292 Maybe expand abbrevs and blink matching block open keywords.
1293 Reindent the line of `octave-auto-indent' is non-nil.
1294 Insert a newline if `octave-auto-newline' is non-nil."
1296 (if (not (octave-not-in-string-or-comment-p))
1298 (if abbrev-mode (expand-abbrev))
1299 (if octave-blink-matching-block
1300 (octave-blink-matching-block-open))
1301 (if octave-auto-indent
1302 (indent-according-to-mode))
1304 (if octave-auto-newline
1305 (newline-and-indent))))
1307 (defun octave-electric-space ()
1308 "Insert a space in Octave mode.
1309 Maybe expand abbrevs and blink matching block open keywords.
1310 Reindent the line of `octave-auto-indent' is non-nil."
1312 (setq last-command-char ? )
1313 (if (not (octave-not-in-string-or-comment-p))
1315 (indent-according-to-mode)
1316 (self-insert-command 1))
1317 (if abbrev-mode (expand-abbrev))
1318 (if octave-blink-matching-block
1319 (octave-blink-matching-block-open))
1320 (if (and octave-auto-indent
1322 (skip-syntax-backward " ")
1324 (indent-according-to-mode))
1325 (self-insert-command 1)))
1327 (defun octave-abbrev-start ()
1328 "Start entering an Octave abbreviation.
1329 If Abbrev mode is turned on, typing ` (grave accent) followed by ? or
1330 \\[help-command] lists all Octave abbrevs. Any other key combination is
1332 Note that all Octave mode abbrevs start with a grave accent."
1334 (if (not abbrev-mode)
1335 (self-insert-command 1)
1337 (insert last-command-char)
1338 (if (if octave-xemacs-p
1339 (or (eq (event-to-character (setq c (next-event))) ??)
1340 (eq (event-to-character c) help-char))
1341 (or (eq (setq c (read-event)) ??)
1343 (let ((abbrev-table-name-list '(octave-abbrev-table)))
1345 (setq unread-command-events (list c))))))
1347 (defun octave-insert-defun (name args vals)
1348 "Insert an Octave function skeleton.
1349 Prompt for the function's name, arguments and return values (to be
1350 entered without parens)."
1353 (read-from-minibuffer "Function name: "
1354 (substring (buffer-name) 0 -2))
1355 (read-from-minibuffer "Arguments: ")
1356 (read-from-minibuffer "Return values: ")))
1357 (let ((string (format "%s %s (%s)"
1359 ((string-equal vals "")
1361 ((string-match "[ ,]" vals)
1362 (concat " [" vals "] ="))
1364 (concat " " vals " =")))
1367 (prefix octave-block-comment-start))
1368 (if (not (bobp)) (newline))
1369 (insert "function" string)
1370 (indent-according-to-mode)
1372 (insert prefix "usage: " string)
1373 (reindent-then-newline-and-indent)
1375 (reindent-then-newline-and-indent)
1377 (indent-according-to-mode)
1380 (insert "endfunction")
1381 (indent-according-to-mode))))
1385 (defun octave-add-octave-menu ()
1386 "Adds the `Octave' menu to the menu bar in Octave mode."
1388 (easy-menu-define octave-mode-menu-map octave-mode-map
1389 "Menu keymap for Octave mode." octave-mode-menu)
1390 (easy-menu-add octave-mode-menu-map octave-mode-map))
1393 ;;; Communication with the inferior Octave process
1394 (defun octave-kill-process ()
1395 "Kill inferior Octave process and its buffer."
1397 (if inferior-octave-process
1399 (process-send-string inferior-octave-process "quit;\n")
1400 (accept-process-output inferior-octave-process)))
1401 (if inferior-octave-buffer
1402 (kill-buffer inferior-octave-buffer)))
1404 (defun octave-show-process-buffer ()
1405 "Make sure that `inferior-octave-buffer' is displayed."
1407 (if (get-buffer inferior-octave-buffer)
1408 (display-buffer inferior-octave-buffer)
1409 (message "No buffer named %s" inferior-octave-buffer)))
1411 (defun octave-hide-process-buffer ()
1412 "Delete all windows that display `inferior-octave-buffer'."
1414 (if (get-buffer inferior-octave-buffer)
1415 (delete-windows-on inferior-octave-buffer)
1416 (message "No buffer named %s" inferior-octave-buffer)))
1418 (defun octave-send-region (beg end)
1419 "Send current region to the inferior Octave process."
1422 (let ((proc inferior-octave-process)
1423 (string (buffer-substring-no-properties beg end))
1426 (set-buffer inferior-octave-buffer)
1427 (setq inferior-octave-output-list nil)
1428 (while (not (string-equal string ""))
1429 (if (string-match "\n" string)
1430 (setq line (substring string 0 (match-beginning 0))
1431 string (substring string (match-end 0)))
1432 (setq line string string ""))
1433 (setq inferior-octave-receive-in-progress t)
1434 (inferior-octave-send-list-and-digest (list (concat line "\n")))
1435 (while inferior-octave-receive-in-progress
1436 (accept-process-output proc))
1437 (insert-before-markers
1438 (mapconcat 'identity
1440 (if octave-send-echo-input (list line) (list ""))
1441 (mapcar 'inferior-octave-strip-ctrl-g
1442 inferior-octave-output-list)
1443 (list inferior-octave-output-string))
1445 (if octave-send-show-buffer
1446 (display-buffer inferior-octave-buffer)))
1448 (defun octave-send-block ()
1449 "Send current Octave block to the inferior Octave process."
1453 (octave-send-region (point) (mark))))
1455 (defun octave-send-defun ()
1456 "Send current Octave function to the inferior Octave process."
1460 (octave-send-region (point) (mark))))
1462 (defun octave-send-line (&optional arg)
1463 "Send current Octave code line to the inferior Octave process.
1464 With positive prefix ARG, send that many lines.
1465 If `octave-send-line-auto-forward' is non-nil, go to the next unsent
1468 (or arg (setq arg 1))
1473 (octave-next-code-line (- arg 1))
1476 (if octave-send-line-auto-forward
1477 (octave-next-code-line 1))
1478 (octave-send-region beg end))))
1480 (defun octave-eval-print-last-sexp ()
1481 "Evaluate Octave sexp before point and print value into current buffer."
1484 (let ((standard-output (current-buffer))
1485 (print-escape-newlines nil)
1491 (inferior-octave-send-list-and-digest
1492 (list (concat (buffer-substring-no-properties (point) opoint)
1494 (mapconcat 'identity inferior-octave-output-list "\n")))
1498 (defun octave-submit-bug-report ()
1499 "Submit a bug report on the Emacs Octave package via mail."
1503 (y-or-n-p "Do you want to submit a bug report? ")
1504 (reporter-submit-bug-report
1505 octave-maintainer-address
1506 (concat "Emacs version " emacs-version)
1509 'octave-auto-newline
1510 'octave-blink-matching-block
1511 'octave-block-offset
1512 'octave-comment-char
1513 'octave-continuation-offset
1514 'octave-continuation-string
1516 'octave-mode-startup-message
1517 'octave-send-echo-input
1518 'octave-send-line-auto-forward
1519 'octave-send-show-buffer))))
1523 (provide 'octave-mod)
1525 ;;; arch-tag: 05f1ce09-be87-4c00-803e-4919ffa26c23
1526 ;;; octave-mod.el ends here