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