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