Misc cleanups and simplifications.
[bpt/emacs.git] / lisp / progmodes / octave-mod.el
1 ;;; octave-mod.el --- editing Octave source files under Emacs
2
3 ;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
7 ;; Author: John Eaton <jwe@octave.org>
8 ;; Maintainer: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
9 ;; Keywords: languages
10
11 ;; This file is part of GNU Emacs.
12
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 3 of the License, or
16 ;; (at your option) any later version.
17
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.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; This package provides Emacs support for Octave.
29 ;; It defines Octave mode, a major mode for editing
30 ;; Octave code.
31
32 ;; The file octave-inf.el contains code for interacting with an inferior
33 ;; Octave process using comint.
34
35 ;; See the documentation of `octave-mode' and
36 ;; `run-octave' for further information on usage and customization.
37
38 ;;; Code:
39 (require 'custom)
40
41 (defgroup octave nil
42 "Major mode for editing Octave source files."
43 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
44 :group 'languages)
45
46 (defvar inferior-octave-output-list nil)
47 (defvar inferior-octave-output-string nil)
48 (defvar inferior-octave-receive-in-progress nil)
49
50 (declare-function inferior-octave-send-list-and-digest "octave-inf" (list))
51
52 (defconst octave-maintainer-address
53 "Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>, bug-gnu-emacs@gnu.org"
54 "Current maintainer of the Emacs Octave package.")
55
56 (define-abbrev-table 'octave-abbrev-table
57 (mapcar (lambda (e) (append e '(nil 0 t)))
58 '(("`a" "all_va_args")
59 ("`b" "break")
60 ("`cs" "case")
61 ("`ca" "catch")
62 ("`c" "continue")
63 ("`el" "else")
64 ("`eli" "elseif")
65 ("`et" "end_try_catch")
66 ("`eu" "end_unwind_protect")
67 ("`ef" "endfor")
68 ("`efu" "endfunction")
69 ("`ei" "endif")
70 ("`es" "endswitch")
71 ("`ew" "endwhile")
72 ("`f" "for")
73 ("`fu" "function")
74 ("`gl" "global")
75 ("`gp" "gplot")
76 ("`gs" "gsplot")
77 ("`if" "if ()")
78 ("`o" "otherwise")
79 ("`rp" "replot")
80 ("`r" "return")
81 ("`s" "switch")
82 ("`t" "try")
83 ("`u" "until ()")
84 ("`up" "unwind_protect")
85 ("`upc" "unwind_protect_cleanup")
86 ("`w" "while ()")))
87 "Abbrev table for Octave's reserved words.
88 Used in `octave-mode' and inferior-octave-mode buffers.
89 All Octave abbrevs start with a grave accent (`)."
90 :regexp "\\(?:[^`]\\|^\\)\\(\\(?:\\<\\|`\\)\\w+\\)\\W*")
91
92 (defvar octave-comment-char ?#
93 "Character to start an Octave comment.")
94 (defvar octave-comment-start
95 (string octave-comment-char ?\s)
96 "String to insert to start a new Octave in-line comment.")
97 (defvar octave-comment-start-skip "\\s<+\\s-*"
98 "Regexp to match the start of an Octave comment up to its body.")
99
100 (defvar octave-begin-keywords
101 '("do" "for" "function" "if" "switch" "try" "unwind_protect" "while"))
102 (defvar octave-else-keywords
103 '("case" "catch" "else" "elseif" "otherwise" "unwind_protect_cleanup"))
104 (defvar octave-end-keywords
105 '("endfor" "endfunction" "endif" "endswitch" "end_try_catch"
106 "end_unwind_protect" "endwhile" "until" "end"))
107
108 (defvar octave-reserved-words
109 (append octave-begin-keywords
110 octave-else-keywords
111 octave-end-keywords
112 '("break" "continue" "end" "global" "persistent" "return"))
113 "Reserved words in Octave.")
114
115 (defvar octave-text-functions
116 '("casesen" "cd" "chdir" "clear" "diary" "dir" "document" "echo"
117 "edit_history" "format" "help" "history" "hold"
118 "load" "ls" "more" "run_history" "save" "type"
119 "which" "who" "whos")
120 "Text functions in Octave.")
121
122 (defvar octave-variables
123 '("DEFAULT_EXEC_PATH" "DEFAULT_LOADPATH"
124 "EDITOR" "EXEC_PATH" "F_DUPFD" "F_GETFD" "F_GETFL" "F_SETFD"
125 "F_SETFL" "I" "IMAGE_PATH" "Inf" "J"
126 "NaN" "OCTAVE_VERSION" "O_APPEND" "O_CREAT" "O_EXCL"
127 "O_NONBLOCK" "O_RDONLY" "O_RDWR" "O_TRUNC" "O_WRONLY" "PAGER" "PS1"
128 "PS2" "PS4" "PWD" "SEEK_CUR" "SEEK_END" "SEEK_SET" "__F_DUPFD__"
129 "__F_GETFD__" "__F_GETFL__" "__F_SETFD__" "__F_SETFL__" "__I__"
130 "__Inf__" "__J__" "__NaN__" "__OCTAVE_VERSION__" "__O_APPEND__"
131 "__O_CREAT__" "__O_EXCL__" "__O_NONBLOCK__" "__O_RDONLY__"
132 "__O_RDWR__" "__O_TRUNC__" "__O_WRONLY__" "__PWD__" "__SEEK_CUR__"
133 "__SEEK_END__" "__SEEK_SET__" "__argv__" "__e__" "__eps__"
134 "__i__" "__inf__" "__j__" "__nan__" "__pi__"
135 "__program_invocation_name__" "__program_name__" "__realmax__"
136 "__realmin__" "__stderr__" "__stdin__" "__stdout__" "ans" "argv"
137 "beep_on_error" "completion_append_char"
138 "crash_dumps_octave_core" "default_save_format"
139 "e" "echo_executing_commands" "eps"
140 "error_text" "gnuplot_binary" "history_file"
141 "history_size" "ignore_function_time_stamp"
142 "inf" "nan" "nargin" "output_max_field_width" "output_precision"
143 "page_output_immediately" "page_screen_output" "pi"
144 "print_answer_id_name" "print_empty_dimensions"
145 "program_invocation_name" "program_name"
146 "realmax" "realmin" "return_last_computed_value" "save_precision"
147 "saving_history" "sighup_dumps_octave_core" "sigterm_dumps_octave_core"
148 "silent_functions" "split_long_rows" "stderr" "stdin" "stdout"
149 "string_fill_char" "struct_levels_to_print"
150 "suppress_verbose_help_message")
151 "Builtin variables in Octave.")
152
153 (defvar octave-function-header-regexp
154 (concat "^\\s-*\\<\\(function\\)\\>"
155 "\\([^=;\n]*=[ \t]*\\|[ \t]*\\)\\(\\w+\\)\\>")
156 "Regexp to match an Octave function header.
157 The string `function' and its name are given by the first and third
158 parenthetical grouping.")
159
160 (defvar octave-font-lock-keywords
161 (list
162 ;; Fontify all builtin keywords.
163 (cons (concat "\\<\\("
164 (regexp-opt (append octave-reserved-words
165 octave-text-functions))
166 "\\)\\>")
167 'font-lock-keyword-face)
168 ;; Fontify all builtin operators.
169 (cons "\\(&\\||\\|<=\\|>=\\|==\\|<\\|>\\|!=\\|!\\)"
170 (if (boundp 'font-lock-builtin-face)
171 'font-lock-builtin-face
172 'font-lock-preprocessor-face))
173 ;; Fontify all builtin variables.
174 (cons (concat "\\<" (regexp-opt octave-variables) "\\>")
175 'font-lock-variable-name-face)
176 ;; Fontify all function declarations.
177 (list octave-function-header-regexp
178 '(1 font-lock-keyword-face)
179 '(3 font-lock-function-name-face nil t)))
180 "Additional Octave expressions to highlight.")
181
182 (defvar octave-font-lock-syntactic-keywords
183 ;; Try to distinguish the string-quotes from the transpose-quotes.
184 '(("[[({,; ]\\('\\)" (1 "\"'"))
185 (octave-font-lock-close-quotes)))
186
187 (defun octave-font-lock-close-quotes (limit)
188 "Fix the syntax-table of the closing quotes of single-quote strings."
189 ;; Freely inspired from perl-font-lock-special-syntactic-constructs.
190 (let ((state (syntax-ppss)))
191 (while (< (point) limit)
192 (cond
193 ((eq (nth 3 state) ?\')
194 ;; A '..' string.
195 (save-excursion
196 (when (re-search-forward "\\(?:\\=\\|[^']\\)\\(?:''\\)*\\('\\)[^']"
197 nil t)
198 (goto-char (1- (point)))
199 ;; Remove any syntax-table property we may have applied to
200 ;; some of the (doubled) single quotes within the string.
201 ;; Since these are the only chars on which we place properties,
202 ;; we take a shortcut and just remove all properties.
203 (remove-text-properties (1+ (nth 8 state)) (match-beginning 1)
204 '(syntax-table nil))
205 (when (eq (char-before (match-beginning 1)) ?\\)
206 ;; Backslash cannot escape a single quote.
207 (put-text-property (1- (match-beginning 1)) (match-beginning 1)
208 'syntax-table (string-to-syntax ".")))
209 (put-text-property (match-beginning 1) (match-end 1)
210 'syntax-table (string-to-syntax "\"'"))))))
211
212 (setq state (parse-partial-sexp (point) limit nil nil state
213 'syntax-table)))))
214
215 (defcustom inferior-octave-buffer "*Inferior Octave*"
216 "Name of buffer for running an inferior Octave process."
217 :type 'string
218 :group 'octave-inferior)
219
220 (defvar inferior-octave-process nil)
221 \f
222 (defvar octave-mode-map
223 (let ((map (make-sparse-keymap)))
224 (define-key map "`" 'octave-abbrev-start)
225 (define-key map ";" 'octave-electric-semi)
226 (define-key map " " 'octave-electric-space)
227 (define-key map "\n" 'octave-reindent-then-newline-and-indent)
228 (define-key map "\e\n" 'octave-indent-new-comment-line)
229 (define-key map "\M-\C-q" 'octave-indent-defun)
230 (define-key map "\C-c\C-b" 'octave-submit-bug-report)
231 (define-key map "\C-c\C-p" 'octave-previous-code-line)
232 (define-key map "\C-c\C-n" 'octave-next-code-line)
233 (define-key map "\C-c\C-a" 'octave-beginning-of-line)
234 (define-key map "\C-c\C-e" 'octave-end-of-line)
235 (define-key map [remap down-list] 'smie-down-list)
236 (define-key map "\C-c\M-\C-h" 'octave-mark-block)
237 (define-key map "\C-c]" 'smie-close-block)
238 (define-key map "\C-c/" 'smie-close-block)
239 (define-key map "\C-c\C-f" 'octave-insert-defun)
240 (define-key map "\C-c\C-h" 'octave-help)
241 (define-key map "\C-c\C-il" 'octave-send-line)
242 (define-key map "\C-c\C-ib" 'octave-send-block)
243 (define-key map "\C-c\C-if" 'octave-send-defun)
244 (define-key map "\C-c\C-ir" 'octave-send-region)
245 (define-key map "\C-c\C-is" 'octave-show-process-buffer)
246 (define-key map "\C-c\C-ih" 'octave-hide-process-buffer)
247 (define-key map "\C-c\C-ik" 'octave-kill-process)
248 (define-key map "\C-c\C-i\C-l" 'octave-send-line)
249 (define-key map "\C-c\C-i\C-b" 'octave-send-block)
250 (define-key map "\C-c\C-i\C-f" 'octave-send-defun)
251 (define-key map "\C-c\C-i\C-r" 'octave-send-region)
252 (define-key map "\C-c\C-i\C-s" 'octave-show-process-buffer)
253 (define-key map "\C-c\C-i\C-h" 'octave-hide-process-buffer)
254 (define-key map "\C-c\C-i\C-k" 'octave-kill-process)
255 map)
256 "Keymap used in Octave mode.")
257
258
259
260 (easy-menu-define octave-mode-menu octave-mode-map
261 "Menu for Octave mode."
262 '("Octave"
263 ("Lines"
264 ["Previous Code Line" octave-previous-code-line t]
265 ["Next Code Line" octave-next-code-line t]
266 ["Begin of Continuation" octave-beginning-of-line t]
267 ["End of Continuation" octave-end-of-line t]
268 ["Split Line at Point" octave-indent-new-comment-line t])
269 ("Blocks"
270 ["Mark Block" octave-mark-block t]
271 ["Close Block" smie-close-block t])
272 ("Functions"
273 ["Indent Function" octave-indent-defun t]
274 ["Insert Function" octave-insert-defun t])
275 "-"
276 ("Debug"
277 ["Send Current Line" octave-send-line t]
278 ["Send Current Block" octave-send-block t]
279 ["Send Current Function" octave-send-defun t]
280 ["Send Region" octave-send-region t]
281 ["Show Process Buffer" octave-show-process-buffer t]
282 ["Hide Process Buffer" octave-hide-process-buffer t]
283 ["Kill Process" octave-kill-process t])
284 "-"
285 ["Indent Line" indent-according-to-mode t]
286 ["Complete Symbol" completion-at-point t]
287 "-"
288 ["Toggle Abbrev Mode" abbrev-mode
289 :style toggle :selected abbrev-mode]
290 ["Toggle Auto-Fill Mode" auto-fill-mode
291 :style toggle :selected auto-fill-function]
292 "-"
293 ["Submit Bug Report" octave-submit-bug-report t]
294 "-"
295 ["Describe Octave Mode" describe-mode t]
296 ["Lookup Octave Index" info-lookup-symbol t]))
297
298 (defvar octave-mode-syntax-table
299 (let ((table (make-syntax-table)))
300 (modify-syntax-entry ?\r " " 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 ?> "." table)
307 (modify-syntax-entry ?< "." table)
308 (modify-syntax-entry ?& "." table)
309 (modify-syntax-entry ?| "." table)
310 (modify-syntax-entry ?! "." table)
311 (modify-syntax-entry ?\\ "\\" table)
312 (modify-syntax-entry ?\' "." table)
313 ;; Was "w" for abbrevs, but now that it's not necessary any more,
314 (modify-syntax-entry ?\` "." table)
315 (modify-syntax-entry ?\" "\"" table)
316 (modify-syntax-entry ?. "w" table)
317 (modify-syntax-entry ?_ "w" table)
318 ;; The "b" flag only applies to the second letter of the comstart
319 ;; and the first letter of the comend, i.e. the "4b" below is ineffective.
320 ;; If we try to put `b' on the single-line comments, we get a similar
321 ;; problem where the % and # chars appear as first chars of the 2-char
322 ;; comend, so the multi-line ender is also turned into style-b.
323 ;; So we need the new "c" comment style.
324 (modify-syntax-entry ?\% "< 13" table)
325 (modify-syntax-entry ?\# "< 13" table)
326 (modify-syntax-entry ?\{ "(} 2c" table)
327 (modify-syntax-entry ?\} "){ 4c" table)
328 (modify-syntax-entry ?\n ">" table)
329 table)
330 "Syntax table in use in `octave-mode' buffers.")
331
332 (defcustom octave-auto-indent nil
333 "Non-nil means indent line after a semicolon or space in Octave mode."
334 :type 'boolean
335 :group 'octave)
336
337 (defcustom octave-auto-newline nil
338 "Non-nil means automatically newline after a semicolon in Octave mode."
339 :type 'boolean
340 :group 'octave)
341
342 (defcustom octave-blink-matching-block t
343 "Control the blinking of matching Octave block keywords.
344 Non-nil means show matching begin of block when inserting a space,
345 newline or semicolon after an else or end keyword."
346 :type 'boolean
347 :group 'octave)
348
349 (defcustom octave-block-offset 2
350 "Extra indentation applied to statements in Octave block structures."
351 :type 'integer
352 :group 'octave)
353
354 (defvar octave-block-comment-start
355 (concat (make-string 2 octave-comment-char) " ")
356 "String to insert to start a new Octave comment on an empty line.")
357
358 (defcustom octave-continuation-offset 4
359 "Extra indentation applied to Octave continuation lines."
360 :type 'integer
361 :group 'octave)
362 (eval-and-compile
363 (defconst octave-continuation-marker-regexp "\\\\\\|\\.\\.\\."))
364 (defvar octave-continuation-regexp
365 (concat "[^#%\n]*\\(" octave-continuation-marker-regexp
366 "\\)\\s-*\\(\\s<.*\\)?$"))
367 (defcustom octave-continuation-string "\\"
368 "Character string used for Octave continuation lines. Normally \\."
369 :type 'string
370 :group 'octave)
371
372 (defvar octave-completion-alist nil
373 "Alist of Octave symbols for completion in Octave mode.
374 Each element looks like (VAR . VAR), where the car and cdr are the same
375 symbol (an Octave command or variable name).
376 Currently, only builtin variables can be completed.")
377
378 (defvar octave-mode-imenu-generic-expression
379 (list
380 ;; Functions
381 (list nil octave-function-header-regexp 3))
382 "Imenu expression for Octave mode. See `imenu-generic-expression'.")
383
384 (defcustom octave-mode-hook nil
385 "Hook to be run when Octave mode is started."
386 :type 'hook
387 :group 'octave)
388
389 (defcustom octave-send-show-buffer t
390 "Non-nil means display `inferior-octave-buffer' after sending to it."
391 :type 'boolean
392 :group 'octave)
393 (defcustom octave-send-line-auto-forward t
394 "Control auto-forward after sending to the inferior Octave process.
395 Non-nil means always go to the next Octave code line after sending."
396 :type 'boolean
397 :group 'octave)
398 (defcustom octave-send-echo-input t
399 "Non-nil means echo input sent to the inferior Octave process."
400 :type 'boolean
401 :group 'octave)
402
403 \f
404 ;;; SMIE indentation
405
406 (require 'smie)
407
408 (defconst octave-operator-table
409 '((assoc ";" "\n") (assoc ",") ; The doc claims they have equal precedence!?
410 (right "=" "+=" "-=" "*=" "/=")
411 (assoc "&&") (assoc "||") ; The doc claims they have equal precedence!?
412 (assoc "&") (assoc "|") ; The doc claims they have equal precedence!?
413 (nonassoc "<" "<=" "==" ">=" ">" "!=" "~=")
414 (nonassoc ":") ;No idea what this is.
415 (assoc "+" "-")
416 (assoc "*" "/" "\\" ".\\" ".*" "./")
417 (nonassoc "'" ".'")
418 (nonassoc "++" "--" "!" "~") ;And unary "+" and "-".
419 (right "^" "**" ".^" ".**")
420 ;; It's not really an operator, but for indentation purposes it
421 ;; could be convenient to treat it as one.
422 (assoc "...")))
423
424 (defconst octave-smie-bnf-table
425 '((atom)
426 ;; We can't distinguish the first element in a sequence with
427 ;; precedence grammars, so we can't distinguish the condition
428 ;; if the `if' from the subsequent body, for example.
429 ;; This has to be done later in the indentation rules.
430 (exp (exp "\n" exp)
431 ;; We need to mention at least one of the operators in this part
432 ;; of the grammar: if the BNF and the operator table have
433 ;; no overlap, SMIE can't know how they relate.
434 (exp ";" exp)
435 ("try" exp "catch" exp "end_try_catch")
436 ("try" exp "catch" exp "end")
437 ("unwind_protect" exp
438 "unwind_protect_cleanup" exp "end_unwind_protect")
439 ("unwind_protect" exp "unwind_protect_cleanup" exp "end")
440 ("for" exp "endfor")
441 ("for" exp "end")
442 ("do" exp "until" atom)
443 ("while" exp "endwhile")
444 ("while" exp "end")
445 ("if" exp "endif")
446 ("if" exp "else" exp "endif")
447 ("if" exp "elseif" exp "else" exp "endif")
448 ("if" exp "elseif" exp "elseif" exp "else" exp "endif")
449 ("if" exp "elseif" exp "elseif" exp "else" exp "end")
450 ("switch" exp "case" exp "endswitch")
451 ("switch" exp "case" exp "otherwise" exp "endswitch")
452 ("switch" exp "case" exp "case" exp "otherwise" exp "endswitch")
453 ("switch" exp "case" exp "case" exp "otherwise" exp "end")
454 ("function" exp "endfunction")
455 ("function" exp "end"))
456 ;; (fundesc (atom "=" atom))
457 ))
458
459 (defconst octave-smie-closer-alist
460 (smie-bnf-closer-alist octave-smie-bnf-table))
461
462 (defconst octave-smie-op-levels
463 (smie-prec2-levels
464 (smie-merge-prec2s
465 (smie-bnf-precedence-table
466 octave-smie-bnf-table
467 '((assoc "\n" ";")))
468
469 (smie-precs-precedence-table
470 (append octave-operator-table
471 '((nonassoc " -dummy- "))) ;Bogus anchor at the end.
472 ))))
473
474 ;; Tokenizing needs to be refined so that ";;" is treated as two
475 ;; tokens and also so as to recognize the \n separator (and
476 ;; corresponding continuation lines).
477
478 (defconst octave-operator-regexp
479 (regexp-opt (apply 'append (mapcar 'cdr octave-operator-table))))
480
481 (defun octave-smie-backward-token ()
482 (let ((pos (point)))
483 (forward-comment (- (point)))
484 (cond
485 ((and (not (eq (char-before) ?\;)) ;Coalesce ";" and "\n".
486 (> pos (line-end-position))
487 (if (looking-back octave-continuation-marker-regexp (- (point) 3))
488 (progn
489 (goto-char (match-beginning 0))
490 (forward-comment (- (point)))
491 nil)
492 t)
493 ;; Ignore it if it's within parentheses.
494 (let ((ppss (syntax-ppss)))
495 (not (and (nth 1 ppss)
496 (eq ?\( (char-after (nth 1 ppss)))))))
497 (skip-chars-forward " \t")
498 ;; Why bother distinguishing \n and ;?
499 ";") ;;"\n"
500 ((and (looking-back octave-operator-regexp (- (point) 3) 'greedy)
501 ;; Don't mistake a string quote for a transpose.
502 (not (looking-back "\\s\"" (1- (point)))))
503 (goto-char (match-beginning 0))
504 (match-string-no-properties 0))
505 (t
506 (smie-default-backward-token)))))
507
508 (defun octave-smie-forward-token ()
509 (skip-chars-forward " \t")
510 (when (looking-at (eval-when-compile
511 (concat "\\(" octave-continuation-marker-regexp
512 "\\)[ \t]*\\($\\|[%#]\\)")))
513 (goto-char (match-end 1))
514 (forward-comment 1))
515 (cond
516 ((and (looking-at "$\\|[%#]")
517 ;; Ignore it if it's within parentheses.
518 (prog1 (let ((ppss (syntax-ppss)))
519 (not (and (nth 1 ppss)
520 (eq ?\( (char-after (nth 1 ppss))))))
521 (forward-comment (point-max))))
522 ;; Why bother distinguishing \n and ;?
523 ";") ;;"\n"
524 ((looking-at ";[ \t]*\\($\\|[%#]\\)")
525 ;; Combine the ; with the subsequent \n.
526 (goto-char (match-beginning 1))
527 (forward-comment 1)
528 ";")
529 ((and (looking-at octave-operator-regexp)
530 ;; Don't mistake a string quote for a transpose.
531 (not (looking-at "\\s\"")))
532 (goto-char (match-end 0))
533 (match-string-no-properties 0))
534 (t
535 (smie-default-forward-token))))
536
537 (defconst octave-smie-indent-rules
538 '((";"
539 (:parent ("function" "if" "while" "else" "elseif" "for" "otherwise"
540 "case" "try" "catch" "unwind_protect" "unwind_protect_cleanup")
541 ;; FIXME: don't hardcode 2.
542 (+ parent octave-block-offset))
543 ;; (:parent "switch" 4) ;For (invalid) code between switch and case.
544 0)
545 ((:before . "case") octave-block-offset)))
546
547 (defvar electric-indent-chars)
548
549 ;;;###autoload
550 (define-derived-mode octave-mode prog-mode "Octave"
551 "Major mode for editing Octave code.
552
553 This mode makes it easier to write Octave code by helping with
554 indentation, doing some of the typing for you (with Abbrev mode) and by
555 showing keywords, comments, strings, etc. in different faces (with
556 Font Lock mode on terminals that support it).
557
558 Octave itself is a high-level language, primarily intended for numerical
559 computations. It provides a convenient command line interface for
560 solving linear and nonlinear problems numerically. Function definitions
561 can also be stored in files, and it can be used in a batch mode (which
562 is why you need this mode!).
563
564 The latest released version of Octave is always available via anonymous
565 ftp from ftp.octave.org in the directory `/pub/octave'. Complete
566 source and binaries for several popular systems are available.
567
568 Type \\[list-abbrevs] to display the built-in abbrevs for Octave keywords.
569
570 Keybindings
571 ===========
572
573 \\{octave-mode-map}
574
575 Variables you can use to customize Octave mode
576 ==============================================
577
578 `octave-auto-indent'
579 Non-nil means indent current line after a semicolon or space.
580 Default is nil.
581
582 `octave-auto-newline'
583 Non-nil means auto-insert a newline and indent after a semicolon.
584 Default is nil.
585
586 `octave-blink-matching-block'
587 Non-nil means show matching begin of block when inserting a space,
588 newline or semicolon after an else or end keyword. Default is t.
589
590 `octave-block-offset'
591 Extra indentation applied to statements in block structures.
592 Default is 2.
593
594 `octave-continuation-offset'
595 Extra indentation applied to Octave continuation lines.
596 Default is 4.
597
598 `octave-continuation-string'
599 String used for Octave continuation lines.
600 Default is a backslash.
601
602 `octave-send-echo-input'
603 Non-nil means always display `inferior-octave-buffer' after sending a
604 command to the inferior Octave process.
605
606 `octave-send-line-auto-forward'
607 Non-nil means always go to the next unsent line of Octave code after
608 sending a line to the inferior Octave process.
609
610 `octave-send-echo-input'
611 Non-nil means echo input sent to the inferior Octave process.
612
613 Turning on Octave mode runs the hook `octave-mode-hook'.
614
615 To begin using this mode for all `.m' files that you edit, add the
616 following lines to your `.emacs' file:
617
618 (add-to-list 'auto-mode-alist '(\"\\\\.m\\\\'\" . octave-mode))
619
620 To automatically turn on the abbrev and auto-fill features,
621 add the following lines to your `.emacs' file as well:
622
623 (add-hook 'octave-mode-hook
624 (lambda ()
625 (abbrev-mode 1)
626 (auto-fill-mode 1)))
627
628 To submit a problem report, enter \\[octave-submit-bug-report] from \
629 an Octave mode buffer.
630 This automatically sets up a mail buffer with version information
631 already added. You just need to add a description of the problem,
632 including a reproducible test case and send the message."
633 (setq local-abbrev-table octave-abbrev-table)
634
635 (smie-setup octave-smie-op-levels octave-smie-indent-rules)
636 (set (make-local-variable 'smie-indent-basic) 'octave-block-offset)
637 (set (make-local-variable 'smie-backward-token-function)
638 'octave-smie-backward-token)
639 (set (make-local-variable 'smie-forward-token-function)
640 'octave-smie-forward-token)
641 (set (make-local-variable 'forward-sexp-function)
642 'smie-forward-sexp-command)
643 (set (make-local-variable 'smie-closer-alist) octave-smie-closer-alist)
644 ;; Only needed for interactive calls to blink-matching-open.
645 (set (make-local-variable 'blink-matching-check-function)
646 #'smie-blink-matching-check)
647
648 (when octave-blink-matching-block
649 (add-hook 'post-self-insert-hook #'smie-blink-matching-open 'append 'local)
650 (set (make-local-variable 'smie-blink-matching-triggers)
651 (append smie-blink-matching-triggers '(\;)
652 ;; Rather than wait for SPC or ; to blink, try to blink as
653 ;; soon as we type the last char of a block ender.
654 ;; But strip ?d from this list so that we don't blink twice
655 ;; when the user writes "endif" (once at "end" and another
656 ;; time at "endif").
657 (delq ?d (delete-dups
658 (mapcar (lambda (kw)
659 (aref (cdr kw) (1- (length (cdr kw)))))
660 smie-closer-alist))))))
661
662 ;; FIXME: maybe we should use (cons ?\; electric-indent-chars)
663 ;; since only ; is really octave-specific.
664 (set (make-local-variable 'electric-indent-chars) '(?\; ?\s ?\n))
665
666 (set (make-local-variable 'comment-start) octave-comment-start)
667 (set (make-local-variable 'comment-end) "")
668 ;; Don't set it here: it's not really a property of the language,
669 ;; just a personal preference of the author.
670 ;; (set (make-local-variable 'comment-column) 32)
671 (set (make-local-variable 'comment-start-skip) "\\s<+\\s-*")
672 (set (make-local-variable 'comment-add) 1)
673
674 (set (make-local-variable 'parse-sexp-ignore-comments) t)
675 (set (make-local-variable 'paragraph-start)
676 (concat "\\s-*$\\|" page-delimiter))
677 (set (make-local-variable 'paragraph-separate) paragraph-start)
678 (set (make-local-variable 'paragraph-ignore-fill-prefix) t)
679 (set (make-local-variable 'fill-paragraph-function) 'octave-fill-paragraph)
680 ;; FIXME: Why disable it?
681 ;; (set (make-local-variable 'adaptive-fill-regexp) nil)
682 ;; Again, this is not a property of the language, don't set it here.
683 ;; (set (make-local-variable 'fill-column) 72)
684 (set (make-local-variable 'normal-auto-fill-function) 'octave-auto-fill)
685
686 (set (make-local-variable 'font-lock-defaults)
687 '(octave-font-lock-keywords nil nil nil nil
688 (font-lock-syntactic-keywords . octave-font-lock-syntactic-keywords)
689 (parse-sexp-lookup-properties . t)))
690
691 (set (make-local-variable 'imenu-generic-expression)
692 octave-mode-imenu-generic-expression)
693 (set (make-local-variable 'imenu-case-fold-search) nil)
694
695 (add-hook 'completion-at-point-functions
696 'octave-completion-at-point-function nil t)
697 (set (make-local-variable 'beginning-of-defun-function)
698 'octave-beginning-of-defun)
699
700 (easy-menu-add octave-mode-menu)
701 (octave-initialize-completions)
702 (run-mode-hooks 'octave-mode-hook))
703
704 (defvar info-lookup-mode)
705
706 (defun octave-help ()
707 "Get help on Octave symbols from the Octave info files.
708 Look up symbol in the function, operator and variable indices of the info files."
709 (let ((info-lookup-mode 'octave-mode))
710 (call-interactively 'info-lookup-symbol)))
711 \f
712 ;;; Miscellaneous useful functions
713
714 (defsubst octave-in-comment-p ()
715 "Return t if point is inside an Octave comment."
716 (save-excursion
717 ;; FIXME: use syntax-ppss?
718 (nth 4 (parse-partial-sexp (line-beginning-position) (point)))))
719
720 (defsubst octave-in-string-p ()
721 "Return t if point is inside an Octave string."
722 (save-excursion
723 ;; FIXME: use syntax-ppss?
724 (nth 3 (parse-partial-sexp (line-beginning-position) (point)))))
725
726 (defsubst octave-not-in-string-or-comment-p ()
727 "Return t if point is not inside an Octave string or comment."
728 ;; FIXME: Use syntax-ppss?
729 (let ((pps (parse-partial-sexp (line-beginning-position) (point))))
730 (not (or (nth 3 pps) (nth 4 pps)))))
731
732
733 (defun octave-looking-at-kw (regexp)
734 "Like `looking-at', but sets `case-fold-search' nil."
735 (let ((case-fold-search nil))
736 (looking-at regexp)))
737
738 (defun octave-maybe-insert-continuation-string ()
739 (if (or (octave-in-comment-p)
740 (save-excursion
741 (beginning-of-line)
742 (looking-at octave-continuation-regexp)))
743 nil
744 (delete-horizontal-space)
745 (insert (concat " " octave-continuation-string))))
746
747 \f
748 ;;; Indentation
749
750 (defun octave-indent-new-comment-line ()
751 "Break Octave line at point, continuing comment if within one.
752 If within code, insert `octave-continuation-string' before breaking the
753 line. If within a string, signal an error.
754 The new line is properly indented."
755 (interactive)
756 (delete-horizontal-space)
757 (cond
758 ((octave-in-comment-p)
759 (indent-new-comment-line))
760 ((octave-in-string-p)
761 (error "Cannot split a code line inside a string"))
762 (t
763 (insert (concat " " octave-continuation-string))
764 (octave-reindent-then-newline-and-indent))))
765
766 (defun octave-indent-defun ()
767 "Properly indent the Octave function which contains point."
768 (interactive)
769 (save-excursion
770 (mark-defun)
771 (message "Indenting function...")
772 (indent-region (point) (mark) nil))
773 (message "Indenting function...done."))
774
775 \f
776 ;;; Motion
777 (defun octave-next-code-line (&optional arg)
778 "Move ARG lines of Octave code forward (backward if ARG is negative).
779 Skips past all empty and comment lines. Default for ARG is 1.
780
781 On success, return 0. Otherwise, go as far as possible and return -1."
782 (interactive "p")
783 (or arg (setq arg 1))
784 (beginning-of-line)
785 (let ((n 0)
786 (inc (if (> arg 0) 1 -1)))
787 (while (and (/= arg 0) (= n 0))
788 (setq n (forward-line inc))
789 (while (and (= n 0)
790 (looking-at "\\s-*\\($\\|\\s<\\)"))
791 (setq n (forward-line inc)))
792 (setq arg (- arg inc)))
793 n))
794
795 (defun octave-previous-code-line (&optional arg)
796 "Move ARG lines of Octave code backward (forward if ARG is negative).
797 Skips past all empty and comment lines. Default for ARG is 1.
798
799 On success, return 0. Otherwise, go as far as possible and return -1."
800 (interactive "p")
801 (or arg (setq arg 1))
802 (octave-next-code-line (- arg)))
803
804 (defun octave-beginning-of-line ()
805 "Move point to beginning of current Octave line.
806 If on an empty or comment line, go to the beginning of that line.
807 Otherwise, move backward to the beginning of the first Octave code line
808 which is not inside a continuation statement, i.e., which does not
809 follow a code line ending in `...' or `\\', or is inside an open
810 parenthesis list."
811 (interactive)
812 (beginning-of-line)
813 (if (not (looking-at "\\s-*\\($\\|\\s<\\)"))
814 (while (or (condition-case nil
815 (progn
816 (up-list -1)
817 (beginning-of-line)
818 t)
819 (error nil))
820 (and (or (looking-at "\\s-*\\($\\|\\s<\\)")
821 (save-excursion
822 (if (zerop (octave-previous-code-line))
823 (looking-at octave-continuation-regexp))))
824 (zerop (forward-line -1)))))))
825
826 (defun octave-end-of-line ()
827 "Move point to end of current Octave line.
828 If on an empty or comment line, go to the end of that line.
829 Otherwise, move forward to the end of the first Octave code line which
830 does not end in `...' or `\\' or is inside an open parenthesis list."
831 (interactive)
832 (end-of-line)
833 (if (save-excursion
834 (beginning-of-line)
835 (looking-at "\\s-*\\($\\|\\s<\\)"))
836 ()
837 (while (or (condition-case nil
838 (progn
839 (up-list 1)
840 (end-of-line)
841 t)
842 (error nil))
843 (and (save-excursion
844 (beginning-of-line)
845 (or (looking-at "\\s-*\\($\\|\\s<\\)")
846 (looking-at octave-continuation-regexp)))
847 (zerop (forward-line 1)))))
848 (end-of-line)))
849
850 (defun octave-mark-block ()
851 "Put point at the beginning of this Octave block, mark at the end.
852 The block marked is the one that contains point or follows point."
853 (interactive)
854 (unless (or (looking-at "\\s(")
855 (save-excursion
856 (let* ((token (funcall smie-forward-token-function))
857 (level (assoc token smie-op-levels)))
858 (and level (null (cadr level))))))
859 (backward-up-list 1))
860 (mark-sexp))
861
862 (defun octave-beginning-of-defun (&optional arg)
863 "Move backward to the beginning of an Octave function.
864 With positive ARG, do it that many times. Negative argument -N means
865 move forward to Nth following beginning of a function.
866 Returns t unless search stops at the beginning or end of the buffer."
867 (let* ((arg (or arg 1))
868 (inc (if (> arg 0) 1 -1))
869 (found nil)
870 (case-fold-search nil))
871 (and (not (eobp))
872 (not (and (> arg 0) (looking-at "\\<function\\>")))
873 (skip-syntax-forward "w"))
874 (while (and (/= arg 0)
875 (setq found
876 (re-search-backward "\\<function\\>" inc)))
877 (if (octave-not-in-string-or-comment-p)
878 (setq arg (- arg inc))))
879 (if found
880 (progn
881 (and (< inc 0) (goto-char (match-beginning 0)))
882 t))))
883
884 \f
885 ;;; Filling
886 (defun octave-auto-fill ()
887 "Perform auto-fill in Octave mode.
888 Returns nil if no feasible place to break the line could be found, and t
889 otherwise."
890 (let (fc give-up)
891 (if (or (null (setq fc (current-fill-column)))
892 (save-excursion
893 (beginning-of-line)
894 (and auto-fill-inhibit-regexp
895 (octave-looking-at-kw auto-fill-inhibit-regexp))))
896 nil ; Can't do anything
897 (if (and (not (octave-in-comment-p))
898 (> (current-column) fc))
899 (setq fc (- fc (+ (length octave-continuation-string) 1))))
900 (while (and (not give-up) (> (current-column) fc))
901 (let* ((opoint (point))
902 (fpoint
903 (save-excursion
904 (move-to-column (+ fc 1))
905 (skip-chars-backward "^ \t\n")
906 ;; If we're at the beginning of the line, break after
907 ;; the first word
908 (if (bolp)
909 (re-search-forward "[ \t]" opoint t))
910 ;; If we're in a comment line, don't break after the
911 ;; comment chars
912 (if (save-excursion
913 (skip-syntax-backward " <")
914 (bolp))
915 (re-search-forward "[ \t]" (line-end-position)
916 'move))
917 ;; If we're not in a comment line and just ahead the
918 ;; continuation string, don't break here.
919 (if (and (not (octave-in-comment-p))
920 (looking-at
921 (concat "\\s-*"
922 (regexp-quote
923 octave-continuation-string)
924 "\\s-*$")))
925 (end-of-line))
926 (skip-chars-backward " \t")
927 (point))))
928 (if (save-excursion
929 (goto-char fpoint)
930 (not (or (bolp) (eolp))))
931 (let ((prev-column (current-column)))
932 (if (save-excursion
933 (skip-chars-backward " \t")
934 (= (point) fpoint))
935 (progn
936 (octave-maybe-insert-continuation-string)
937 (indent-new-comment-line t))
938 (save-excursion
939 (goto-char fpoint)
940 (octave-maybe-insert-continuation-string)
941 (indent-new-comment-line t)))
942 (if (>= (current-column) prev-column)
943 (setq give-up t)))
944 (setq give-up t))))
945 (not give-up))))
946
947 (defun octave-fill-paragraph (&optional arg)
948 "Fill paragraph of Octave code, handling Octave comments."
949 ;; FIXME: difference with generic fill-paragraph:
950 ;; - code lines are only split, never joined.
951 ;; - \n that end comments are never removed.
952 ;; - insert continuation marker when splitting code lines.
953 (interactive "P")
954 (save-excursion
955 (let ((end (progn (forward-paragraph) (copy-marker (point) t)))
956 (beg (progn
957 (forward-paragraph -1)
958 (skip-chars-forward " \t\n")
959 (beginning-of-line)
960 (point)))
961 (cfc (current-fill-column))
962 comment-prefix)
963 (goto-char beg)
964 (while (< (point) end)
965 (condition-case nil
966 (indent-according-to-mode)
967 (error nil))
968 (move-to-column cfc)
969 ;; First check whether we need to combine non-empty comment lines
970 (if (and (< (current-column) cfc)
971 (octave-in-comment-p)
972 (not (save-excursion
973 (beginning-of-line)
974 (looking-at "^\\s-*\\s<+\\s-*$"))))
975 ;; This is a nonempty comment line which does not extend
976 ;; past the fill column. If it is followed by a nonempty
977 ;; comment line with the same comment prefix, try to
978 ;; combine them, and repeat this until either we reach the
979 ;; fill-column or there is nothing more to combine.
980 (progn
981 ;; Get the comment prefix
982 (save-excursion
983 (beginning-of-line)
984 (while (and (re-search-forward "\\s<+")
985 (not (octave-in-comment-p))))
986 (setq comment-prefix (match-string 0)))
987 ;; And keep combining ...
988 (while (and (< (current-column) cfc)
989 (save-excursion
990 (forward-line 1)
991 (and (looking-at
992 (concat "^\\s-*"
993 comment-prefix
994 "\\S<"))
995 (not (looking-at
996 (concat "^\\s-*"
997 comment-prefix
998 "\\s-*$"))))))
999 (delete-char 1)
1000 (re-search-forward comment-prefix)
1001 (delete-region (match-beginning 0) (match-end 0))
1002 (fixup-whitespace)
1003 (move-to-column cfc))))
1004 ;; We might also try to combine continued code lines> Perhaps
1005 ;; some other time ...
1006 (skip-chars-forward "^ \t\n")
1007 (delete-horizontal-space)
1008 (if (or (< (current-column) cfc)
1009 (and (= (current-column) cfc) (eolp)))
1010 (forward-line 1)
1011 (if (not (eolp)) (insert " "))
1012 (or (octave-auto-fill)
1013 (forward-line 1))))
1014 t)))
1015
1016 \f
1017 ;;; Completions
1018 (defun octave-initialize-completions ()
1019 "Create an alist for Octave completions."
1020 (if octave-completion-alist
1021 ()
1022 (setq octave-completion-alist
1023 (append octave-reserved-words
1024 octave-text-functions
1025 octave-variables))))
1026
1027 (defun octave-completion-at-point-function ()
1028 "Find the text to complete and the corresponding table."
1029 (let* ((beg (save-excursion (backward-sexp 1) (point)))
1030 (end (point)))
1031 (if (< beg (point))
1032 ;; Extend region past point, if applicable.
1033 (save-excursion (goto-char beg) (forward-sexp 1)
1034 (setq end (max end (point)))))
1035 (list beg end octave-completion-alist)))
1036
1037 (defun octave-complete-symbol ()
1038 "Perform completion on Octave symbol preceding point.
1039 Compare that symbol against Octave's reserved words and builtin
1040 variables."
1041 (interactive)
1042 (apply 'completion-in-region (octave-completion-at-point-function)))
1043 \f
1044 ;;; Electric characters && friends
1045 (defun octave-reindent-then-newline-and-indent ()
1046 "Reindent current Octave line, insert newline, and indent the new line.
1047 If Abbrev mode is on, expand abbrevs first."
1048 ;; FIXME: None of this is Octave-specific.
1049 (interactive)
1050 (reindent-then-newline-and-indent))
1051
1052 (defun octave-electric-semi ()
1053 "Insert a semicolon in Octave mode.
1054 Maybe expand abbrevs and blink matching block open keywords.
1055 Reindent the line if `octave-auto-indent' is non-nil.
1056 Insert a newline if `octave-auto-newline' is non-nil."
1057 (interactive)
1058 (setq last-command-event ?\;)
1059 (if (not (octave-not-in-string-or-comment-p))
1060 (self-insert-command 1)
1061 (if octave-auto-indent
1062 (indent-according-to-mode))
1063 (self-insert-command 1)
1064 (if octave-auto-newline
1065 (newline-and-indent))))
1066
1067 (defun octave-electric-space ()
1068 "Insert a space in Octave mode.
1069 Maybe expand abbrevs and blink matching block open keywords.
1070 Reindent the line if `octave-auto-indent' is non-nil."
1071 (interactive)
1072 (setq last-command-event ? )
1073 (if (and octave-auto-indent
1074 (not (octave-not-in-string-or-comment-p)))
1075 (progn
1076 (indent-according-to-mode)
1077 (self-insert-command 1))
1078 (if (and octave-auto-indent
1079 (save-excursion
1080 (skip-syntax-backward " ")
1081 (not (bolp))))
1082 (indent-according-to-mode))
1083 (self-insert-command 1)))
1084
1085 (defun octave-abbrev-start ()
1086 "Start entering an Octave abbreviation.
1087 If Abbrev mode is turned on, typing ` (grave accent) followed by ? or
1088 \\[help-command] lists all Octave abbrevs. Any other key combination is
1089 executed normally.
1090 Note that all Octave mode abbrevs start with a grave accent."
1091 (interactive)
1092 (if (not abbrev-mode)
1093 (self-insert-command 1)
1094 (let (c)
1095 (insert last-command-event)
1096 (if (if (featurep 'xemacs)
1097 (or (eq (event-to-character (setq c (next-event))) ??)
1098 (eq (event-to-character c) help-char))
1099 (or (eq (setq c (read-event)) ??)
1100 (eq c help-char)))
1101 (let ((abbrev-table-name-list '(octave-abbrev-table)))
1102 (list-abbrevs))
1103 (setq unread-command-events (list c))))))
1104
1105 (define-skeleton octave-insert-defun
1106 "Insert an Octave function skeleton.
1107 Prompt for the function's name, arguments and return values (to be
1108 entered without parens)."
1109 (let* ((defname (substring (buffer-name) 0 -2))
1110 (name (read-string (format "Function name (default %s): " defname)
1111 nil nil defname))
1112 (args (read-string "Arguments: "))
1113 (vals (read-string "Return values: ")))
1114 (format "%s%s (%s)"
1115 (cond
1116 ((string-equal vals "") vals)
1117 ((string-match "[ ,]" vals) (concat "[" vals "] = "))
1118 (t (concat vals " = ")))
1119 name
1120 args))
1121 \n "function " > str \n \n
1122 octave-block-comment-start "usage: " str \n
1123 octave-block-comment-start \n octave-block-comment-start
1124 \n _ \n
1125 "endfunction" > \n)
1126 \f
1127 ;;; Communication with the inferior Octave process
1128 (defun octave-kill-process ()
1129 "Kill inferior Octave process and its buffer."
1130 (interactive)
1131 (if inferior-octave-process
1132 (progn
1133 (process-send-string inferior-octave-process "quit;\n")
1134 (accept-process-output inferior-octave-process)))
1135 (if inferior-octave-buffer
1136 (kill-buffer inferior-octave-buffer)))
1137
1138 (defun octave-show-process-buffer ()
1139 "Make sure that `inferior-octave-buffer' is displayed."
1140 (interactive)
1141 (if (get-buffer inferior-octave-buffer)
1142 (display-buffer inferior-octave-buffer)
1143 (message "No buffer named %s" inferior-octave-buffer)))
1144
1145 (defun octave-hide-process-buffer ()
1146 "Delete all windows that display `inferior-octave-buffer'."
1147 (interactive)
1148 (if (get-buffer inferior-octave-buffer)
1149 (delete-windows-on inferior-octave-buffer)
1150 (message "No buffer named %s" inferior-octave-buffer)))
1151
1152 (defun octave-send-region (beg end)
1153 "Send current region to the inferior Octave process."
1154 (interactive "r")
1155 (inferior-octave t)
1156 (let ((proc inferior-octave-process)
1157 (string (buffer-substring-no-properties beg end))
1158 line)
1159 (with-current-buffer inferior-octave-buffer
1160 (setq inferior-octave-output-list nil)
1161 (while (not (string-equal string ""))
1162 (if (string-match "\n" string)
1163 (setq line (substring string 0 (match-beginning 0))
1164 string (substring string (match-end 0)))
1165 (setq line string string ""))
1166 (setq inferior-octave-receive-in-progress t)
1167 (inferior-octave-send-list-and-digest (list (concat line "\n")))
1168 (while inferior-octave-receive-in-progress
1169 (accept-process-output proc))
1170 (insert-before-markers
1171 (mapconcat 'identity
1172 (append
1173 (if octave-send-echo-input (list line) (list ""))
1174 (mapcar 'inferior-octave-strip-ctrl-g
1175 inferior-octave-output-list)
1176 (list inferior-octave-output-string))
1177 "\n")))))
1178 (if octave-send-show-buffer
1179 (display-buffer inferior-octave-buffer)))
1180
1181 (defun octave-send-block ()
1182 "Send current Octave block to the inferior Octave process."
1183 (interactive)
1184 (save-excursion
1185 (octave-mark-block)
1186 (octave-send-region (point) (mark))))
1187
1188 (defun octave-send-defun ()
1189 "Send current Octave function to the inferior Octave process."
1190 (interactive)
1191 (save-excursion
1192 (mark-defun)
1193 (octave-send-region (point) (mark))))
1194
1195 (defun octave-send-line (&optional arg)
1196 "Send current Octave code line to the inferior Octave process.
1197 With positive prefix ARG, send that many lines.
1198 If `octave-send-line-auto-forward' is non-nil, go to the next unsent
1199 code line."
1200 (interactive "P")
1201 (or arg (setq arg 1))
1202 (if (> arg 0)
1203 (let (beg end)
1204 (beginning-of-line)
1205 (setq beg (point))
1206 (octave-next-code-line (- arg 1))
1207 (end-of-line)
1208 (setq end (point))
1209 (if octave-send-line-auto-forward
1210 (octave-next-code-line 1))
1211 (octave-send-region beg end))))
1212
1213 (defun octave-eval-print-last-sexp ()
1214 "Evaluate Octave sexp before point and print value into current buffer."
1215 (interactive)
1216 (inferior-octave t)
1217 (let ((standard-output (current-buffer))
1218 (print-escape-newlines nil)
1219 (opoint (point)))
1220 (terpri)
1221 (prin1
1222 (save-excursion
1223 (forward-sexp -1)
1224 (inferior-octave-send-list-and-digest
1225 (list (concat (buffer-substring-no-properties (point) opoint)
1226 "\n")))
1227 (mapconcat 'identity inferior-octave-output-list "\n")))
1228 (terpri)))
1229 \f
1230 ;;; Bug reporting
1231 (defun octave-submit-bug-report ()
1232 "Submit a bug report on the Emacs Octave package via mail."
1233 (interactive)
1234 (require 'reporter)
1235 (and
1236 (y-or-n-p "Do you want to submit a bug report? ")
1237 (reporter-submit-bug-report
1238 octave-maintainer-address
1239 (concat "Emacs version " emacs-version)
1240 (list
1241 'octave-auto-indent
1242 'octave-auto-newline
1243 'octave-blink-matching-block
1244 'octave-block-offset
1245 'octave-comment-char
1246 'octave-continuation-offset
1247 'octave-continuation-string
1248 'octave-send-echo-input
1249 'octave-send-line-auto-forward
1250 'octave-send-show-buffer))))
1251
1252 ;; provide ourself
1253
1254 (provide 'octave-mod)
1255
1256 ;; arch-tag: 05f1ce09-be87-4c00-803e-4919ffa26c23
1257 ;;; octave-mod.el ends here