]> code.delx.au - gnu-emacs/blob - lisp/progmodes/octave-mod.el
(grep-process-setup): New function, sets up the
[gnu-emacs] / lisp / progmodes / octave-mod.el
1 ;; octave-mod.el --- editing Octave source files under Emacs
2
3 ;;; Copyright (C) 1997 Free Software Foundation, Inc.
4
5 ;; Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
6 ;; Author: John Eaton <jwe@bevo.che.wisc.edu>
7 ;; Maintainer: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
8 ;; Keywords: languages
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; This package provides Emacs support for Octave.
30 ;; It defines Octave mode, a major mode for editing
31 ;; Octave code.
32
33 ;; The file octave-hlp.el provides `octave-help', a facility for looking up
34 ;; documentation on a symbol in the Octave info files.
35
36 ;; The file octave-inf.el contains code for interacting with an inferior
37 ;; Octave process using comint.
38
39 ;; See the documentation of `octave-mode', `octave-help' and
40 ;; `run-octave' for further information on usage and customization.
41
42 ;;; Code:
43
44 (defconst octave-maintainer-address
45 "Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>, bug-gnu-emacs@prep.ai.mit.edu"
46 "Current maintainer of the Emacs Octave package.")
47
48 (defvar octave-abbrev-table nil
49 "Abbrev table for Octave's reserved words.
50 Used in octave-mode and inferior-octave-mode buffers.
51 All Octave abbrevs start with a grave accent (`).")
52 (if octave-abbrev-table
53 ()
54 (let ((ac abbrevs-changed))
55 (define-abbrev-table 'octave-abbrev-table ())
56 (define-abbrev octave-abbrev-table "`a" "all_va_args" nil)
57 (define-abbrev octave-abbrev-table "`b" "break" nil)
58 (define-abbrev octave-abbrev-table "`ca" "catch" nil)
59 (define-abbrev octave-abbrev-table "`c" "continue" nil)
60 (define-abbrev octave-abbrev-table "`el" "else" nil)
61 (define-abbrev octave-abbrev-table "`eli" "elseif" nil)
62 (define-abbrev octave-abbrev-table "`et" "end_try_catch" nil)
63 (define-abbrev octave-abbrev-table "`eu" "end_unwind_protect" nil)
64 (define-abbrev octave-abbrev-table "`ef" "endfor" nil)
65 (define-abbrev octave-abbrev-table "`efu" "endfunction" nil)
66 (define-abbrev octave-abbrev-table "`ei" "endif" nil)
67 (define-abbrev octave-abbrev-table "`ew" "endwhile" nil)
68 (define-abbrev octave-abbrev-table "`f" "for" nil)
69 (define-abbrev octave-abbrev-table "`fu" "function" nil)
70 (define-abbrev octave-abbrev-table "`gl" "global" nil)
71 (define-abbrev octave-abbrev-table "`gp" "gplot" nil)
72 (define-abbrev octave-abbrev-table "`gs" "gsplot" nil)
73 (define-abbrev octave-abbrev-table "`if" "if ()" nil)
74 (define-abbrev octave-abbrev-table "`rp" "replot" nil)
75 (define-abbrev octave-abbrev-table "`r" "return" nil)
76 (define-abbrev octave-abbrev-table "`t" "try" nil)
77 (define-abbrev octave-abbrev-table "`up" "unwind_protect" nil)
78 (define-abbrev octave-abbrev-table "`upc" "unwind_protect_cleanup" nil)
79 (define-abbrev octave-abbrev-table "`w" "while ()" nil)
80 (setq abbrevs-changed ac)))
81
82 (defvar octave-comment-char ?#
83 "Character to start an Octave comment.")
84 (defvar octave-comment-start
85 (concat (make-string 1 octave-comment-char) " ")
86 "String to insert to start a new Octave in-line comment.")
87 (defvar octave-comment-start-skip "\\s<+\\s-*"
88 "Regexp to match the start of an Octave comment up to its body.")
89
90 (defvar octave-begin-keywords
91 '("for" "function" "if" "try" "unwind_protect" "while"))
92 (defvar octave-else-keywords
93 '("catch" "else" "elseif" "unwind_protect_cleanup"))
94 (defvar octave-end-keywords
95 '("end" "endfor" "endfunction" "endif" "end_try_catch"
96 "end_unwind_protect" "endwhile"))
97
98 (defvar octave-reserved-words
99 (append octave-begin-keywords octave-else-keywords octave-end-keywords
100 '("all_va_args" "break" "continue" "global" "gplot" "gsplot"
101 "replot" "return"))
102 "Reserved words in Octave.")
103
104 (defvar octave-text-functions
105 '("casesen" "cd" "chdir" "clear" "diary" "dir" "document" "echo"
106 "edit_history" "format" "gset" "gshow" "help" "history" "hold"
107 "load" "ls" "more" "run_history" "save" "set" "show" "type"
108 "which" "who" "whos")
109 "Text functions in Octave (these names are also reserved).")
110
111 (defvar octave-variables
112 '("EDITOR" "EXEC_PATH" "F_DUPFD" "F_GETFD" "F_GETFL" "F_SETFD"
113 "F_SETFL" "I" "IMAGEPATH" "INFO_FILE" "INFO_PROGRAM" "Inf" "J"
114 "LOADPATH" "NaN" "OCTAVE_VERSION" "O_APPEND" "O_CREAT" "O_EXCL"
115 "O_NONBLOCK" "O_RDONLY" "O_RDWR" "O_TRUNC" "O_WRONLY" "PAGER" "PS1"
116 "PS2" "PS4" "PWD" "SEEK_CUR" "SEEK_END" "SEEK_SET" "__F_DUPFD__"
117 "__F_GETFD__" "__F_GETFL__" "__F_SETFD__" "__F_SETFL__" "__I__"
118 "__Inf__" "__J__" "__NaN__" "__OCTAVE_VERSION__" "__O_APPEND__"
119 "__O_CREAT__" "__O_EXCL__" "__O_NONBLOCK__" "__O_RDONLY__"
120 "__O_RDWR__" "__O_TRUNC__" "__O_WRONLY__" "__PWD__" "__SEEK_CUR__"
121 "__SEEK_END__" "__SEEK_SET__" "__argv__" "__e__" "__eps__"
122 "__error_text__" "__i__" "__inf__" "__j__" "__nan__" "__pi__"
123 "__program_invocation_name__" "__program_name__" "__realmax__"
124 "__realmin__" "__stderr__" "__stdin__" "__stdout__" "ans" "argv"
125 "automatic_replot" "beep_on_error" "completion_append_char"
126 "default_return_value" "default_save_format"
127 "define_all_return_values" "do_fortran_indexing" "e"
128 "echo_executing_commands" "empty_list_elements_ok" "eps"
129 "error_text" "gnuplot_binary" "gnuplot_has_multiplot" "history_file"
130 "history_size" "ignore_function_time_stamp" "implicit_str_to_num_ok"
131 "inf" "nan" "nargin" "ok_to_lose_imaginary_part"
132 "output_max_field_width" "output_precision"
133 "page_output_immediately" "page_screen_output" "pi"
134 "prefer_column_vectors" "prefer_zero_one_indexing"
135 "print_answer_id_name" "print_empty_dimensions"
136 "program_invocation_name" "program_name" "propagate_empty_matrices"
137 "realmax" "realmin" "resize_on_range_error"
138 "return_last_computed_value" "save_precision" "saving_history"
139 "silent_functions" "split_long_rows" "stderr" "stdin" "stdout"
140 "string_fill_char" "struct_levels_to_print"
141 "suppress_verbose_help_message" "treat_neg_dim_as_zero"
142 "warn_assign_as_truth_value" "warn_comma_in_global_decl"
143 "warn_divide_by_zero" "warn_function_name_clash"
144 "warn_missing_semicolon" "whitespace_in_literal_matrix")
145 "Builtin variables in Octave.")
146
147 (defvar octave-function-header-regexp
148 (concat "^\\s-*\\<\\(function\\)\\>"
149 "\\([^=;\n]*=[ \t]*\\|[ \t]*\\)\\(\\w+\\)\\>")
150 "Regexp to match an Octave function header.
151 The string `function' and its name are given by the first and third
152 parenthetical grouping.")
153
154 (defvar octave-font-lock-keywords
155 (list
156 ;; Fontify all builtin keywords.
157 (cons (concat "\\<\\("
158 (mapconcat 'identity octave-reserved-words "\\|")
159 (mapconcat 'identity octave-text-functions "\\|")
160 "\\)\\>")
161 'font-lock-keyword-face)
162 ;; Fontify all builtin operators.
163 (cons "\\(&\\||\\|<=\\|>=\\|==\\|<\\|>\\|!=\\|!\\)"
164 'font-lock-reference-face)
165 ;; Fontify all builtin variables.
166 (cons (concat "\\<\\("
167 (mapconcat 'identity octave-variables "\\|")
168 "\\)\\>")
169 'font-lock-variable-name-face)
170 ;; Fontify all function declarations.
171 (list octave-function-header-regexp
172 '(1 font-lock-keyword-face)
173 '(3 font-lock-function-name-face nil t)))
174 "Additional Octave expressions to highlight.")
175
176 (defvar inferior-octave-buffer "*Inferior Octave*"
177 "*Name of buffer for running an inferior Octave process.")
178
179 (defvar inferior-octave-process nil)
180 \f
181 (defvar octave-mode-map nil
182 "Keymap used in Octave mode.")
183 (if octave-mode-map
184 ()
185 (let ((map (make-sparse-keymap)))
186 (define-key map "`" 'octave-abbrev-start)
187 (define-key map ";" 'octave-electric-semi)
188 (define-key map " " 'octave-electric-space)
189 (define-key map "\n" 'octave-reindent-then-newline-and-indent)
190 (define-key map "\t" 'indent-according-to-mode)
191 (define-key map "\e;" 'octave-indent-for-comment)
192 (define-key map "\e\n" 'octave-indent-new-comment-line)
193 (define-key map "\e\t" 'octave-complete-symbol)
194 (define-key map "\M-\C-a" 'octave-beginning-of-defun)
195 (define-key map "\M-\C-e" 'octave-end-of-defun)
196 (define-key map "\M-\C-h" 'octave-mark-defun)
197 (define-key map "\M-\C-q" 'octave-indent-defun)
198 (define-key map "\C-c;" 'octave-comment-region)
199 (define-key map "\C-c:" 'octave-uncomment-region)
200 (define-key map "\C-c\C-b" 'octave-submit-bug-report)
201 (define-key map "\C-c\C-p" 'octave-previous-code-line)
202 (define-key map "\C-c\C-n" 'octave-next-code-line)
203 (define-key map "\C-c\C-a" 'octave-beginning-of-line)
204 (define-key map "\C-c\C-e" 'octave-end-of-line)
205 (define-key map "\C-c\M-\C-n" 'octave-forward-block)
206 (define-key map "\C-c\M-\C-p" 'octave-backward-block)
207 (define-key map "\C-c\M-\C-u" 'octave-backward-up-block)
208 (define-key map "\C-c\M-\C-d" 'octave-down-block)
209 (define-key map "\C-c\M-\C-h" 'octave-mark-block)
210 (define-key map "\C-c]" 'octave-close-block)
211 (define-key map "\C-cf" 'octave-insert-defun)
212 (define-key map "\C-c\C-h" 'octave-help)
213 (define-key map "\C-cil" 'octave-send-line)
214 (define-key map "\C-cib" 'octave-send-block)
215 (define-key map "\C-cif" 'octave-send-defun)
216 (define-key map "\C-cir" 'octave-send-region)
217 (define-key map "\C-cis" 'octave-show-process-buffer)
218 (define-key map "\C-cih" 'octave-hide-process-buffer)
219 (define-key map "\C-cik" 'octave-kill-process)
220 (setq octave-mode-map map)))
221
222 (defvar octave-mode-menu
223 (list "Octave"
224 (list "Lines"
225 ["Previous Code Line" octave-previous-code-line t]
226 ["Next Code Line" octave-next-code-line t]
227 ["Begin of Continuation" octave-beginning-of-line t]
228 ["End of Continuation" octave-end-of-line t]
229 ["Split Line at Point" octave-indent-new-comment-line t])
230 (list "Blocks"
231 ["Next Block" octave-forward-block t]
232 ["Previous Block" octave-backward-block t]
233 ["Down Block" octave-down-block t]
234 ["Up Block" octave-backward-up-block t]
235 ["Mark Block" octave-mark-block t]
236 ["Close Block" octave-close-block t])
237 (list "Functions"
238 ["Begin of Function" octave-beginning-of-defun t]
239 ["End of Function" octave-end-of-defun t]
240 ["Mark Function" octave-mark-defun t]
241 ["Indent Function" octave-indent-defun t]
242 ["Insert Function" octave-insert-defun t])
243 "-"
244 (list "Debug"
245 ["Send Current Line" octave-send-line t]
246 ["Send Current Block" octave-send-block t]
247 ["Send Current Function" octave-send-defun t]
248 ["Send Region" octave-send-region t]
249 ["Show Process Buffer" octave-show-process-buffer t]
250 ["Hide Process Buffer" octave-hide-process-buffer t]
251 ["Kill Process" octave-kill-process t])
252 "-"
253 ["Indent Line" indent-according-to-mode t]
254 ["Complete Symbol" octave-complete-symbol t]
255 "-"
256 ["Toggle Abbrev Mode" abbrev-mode t]
257 ["Toggle Auto-Fill Mode" auto-fill-mode t]
258 "-"
259 ["Submit Bug Report" octave-submit-bug-report t]
260 "-"
261 ["Describe Octave Mode" octave-describe-major-mode t]
262 ["Lookup Octave Index" octave-help t])
263 "Menu for Octave mode.")
264
265 (defvar octave-mode-syntax-table nil
266 "Syntax table in use in octave-mode buffers.")
267 (if octave-mode-syntax-table
268 ()
269 (let ((table (make-syntax-table)))
270 (modify-syntax-entry ?\r " " table)
271 (modify-syntax-entry ?+ "." table)
272 (modify-syntax-entry ?- "." table)
273 (modify-syntax-entry ?= "." table)
274 (modify-syntax-entry ?* "." table)
275 (modify-syntax-entry ?/ "." table)
276 (modify-syntax-entry ?> "." table)
277 (modify-syntax-entry ?< "." table)
278 (modify-syntax-entry ?& "." table)
279 (modify-syntax-entry ?| "." table)
280 (modify-syntax-entry ?! "." table)
281 (modify-syntax-entry ?\\ "\\" table)
282 (modify-syntax-entry ?\' "." table)
283 (modify-syntax-entry ?\` "w" table)
284 (modify-syntax-entry ?\" "\"" table)
285 (modify-syntax-entry ?. "w" table)
286 (modify-syntax-entry ?_ "w" table)
287 (modify-syntax-entry ?\% "." table)
288 (modify-syntax-entry ?\# "<" table)
289 (modify-syntax-entry ?\n ">" table)
290 (setq octave-mode-syntax-table table)))
291
292 (defvar octave-auto-newline nil
293 "*Non-nil means automatically newline after a semicolon in Octave mode.")
294
295 (defvar octave-blink-matching-block t
296 "*Control the blinking of matching Octave block keywords.
297 Non-nil means show matching begin of block when inserting a space,
298 newline or semicolon after an else or end keyword.")
299 (defvar octave-block-offset 2
300 "*Extra indentation applied to statements in Octave block structures.")
301
302 (defvar octave-block-begin-regexp
303 (concat "\\<\\("
304 (mapconcat 'identity octave-begin-keywords "\\|")
305 "\\)\\>"))
306 (defvar octave-block-else-regexp
307 (concat "\\<\\("
308 (mapconcat 'identity octave-else-keywords "\\|")
309 "\\)\\>"))
310 (defvar octave-block-end-regexp
311 (concat "\\<\\("
312 (mapconcat 'identity octave-end-keywords "\\|")
313 "\\)\\>"))
314 (defvar octave-block-begin-or-end-regexp
315 (concat octave-block-begin-regexp "\\|" octave-block-end-regexp))
316 (defvar octave-block-else-or-end-regexp
317 (concat octave-block-else-regexp "\\|" octave-block-end-regexp))
318 (defvar octave-block-match-alist
319 '(("for" . ("end" "endfor"))
320 ("function" . ("end" "endfunction"))
321 ("if" . ("else" "elseif" "end" "endif"))
322 ("try" . ("catch" "end" "end_try_catch"))
323 ("unwind_protect" . ("unwind_protect_cleanup" "end"
324 "end_unwind_protect"))
325 ("while" . ("end" "endwhile")))
326 "Alist with Octave's matching block keywords.
327 Has Octave's begin keywords as keys and a list of the matching else or
328 end keywords as associated values.")
329
330 (defvar octave-block-comment-start
331 (concat (make-string 2 octave-comment-char) " ")
332 "String to insert to start a new Octave comment on an empty line.")
333
334 (defvar octave-continuation-offset 4
335 "*Extra indentation applied to Octave continuation lines.")
336 (defvar octave-continuation-regexp
337 "[^#%\n]*\\(\\\\\\|\\.\\.\\.\\)\\s-*\\(\\s<.*\\)?$")
338 (defvar octave-continuation-string "\\"
339 "*Character string used for Octave continuation lines. Normally \\.")
340
341 (defvar octave-completion-alist nil
342 "Alist of Octave symbols for completion in Octave mode.
343 Each element looks like (VAR . VAR), where the car and cdr are the same
344 symbol (an Octave command or variable name).
345 Currently, only builtin variables can be completed.")
346
347 (defvar octave-mode-imenu-generic-expression
348 (list
349 ;; Functions
350 (list nil octave-function-header-regexp 3))
351 "Imenu expression for Octave mode. See `imenu-generic-expression'.")
352
353 (defvar octave-mode-startup-message t
354 "*Nil means do not display the Octave mode startup message.")
355
356 (defvar octave-mode-hook nil
357 "*Hook to be run when Octave mode is started.")
358
359 (defvar octave-send-show-buffer t
360 "*Non-nil means display `inferior-octave-buffer' after sending to it.")
361 (defvar octave-send-line-auto-forward t
362 "*Control auto-forward after sending to the inferior Octave process.
363 Non-nil means always go to the next Octave code line after sending.")
364 (defvar octave-send-echo-input t
365 "*Non-nil means echo input sent to the inferior Octave process.")
366
367 \f
368 ;;;###autoload
369 (defun octave-mode ()
370 "Major mode for editing Octave code.
371
372 This mode makes it easier to write Octave code by helping with
373 indentation, doing some of the typing for you (with Abbrev mode) and by
374 showing keywords, comments, strings, etc. in different faces (with
375 Font Lock mode on terminals that support it).
376
377 Octave itself is a high-level language, primarily intended for numerical
378 computations. It provides a convenient command line interface for
379 solving linear and nonlinear problems numerically. Function definitions
380 can also be stored in files, and it can be used in a batch mode (which
381 is why you need this mode!).
382
383 The latest released version of Octave is always available via anonymous
384 ftp from bevo.che.wisc.edu in the directory `/pub/octave'. Complete
385 source and binaries for several popular systems are available.
386
387 Type \\[list-abbrevs] to display the built-in abbrevs for Octave keywords.
388
389 Keybindings
390 ===========
391
392 \\{octave-mode-map}
393
394 Variables you can use to customize Octave mode
395 ==============================================
396
397 octave-auto-newline
398 Non-nil means auto-insert a newline and indent after a semicolon.
399 Default is nil.
400
401 octave-blink-matching-block
402 Non-nil means show matching begin of block when inserting a space,
403 newline or semicolon after an else or end keyword. Default is t.
404
405 octave-block-offset
406 Extra indentation applied to statements in block structures.
407 Default is 2.
408
409 octave-continuation-offset
410 Extra indentation applied to Octave continuation lines.
411 Default is 4.
412
413 octave-continuation-string
414 String used for Octave continuation lines.
415 Default is a backslash.
416
417 octave-mode-startup-message
418 Nil means do not display the Octave mode startup message.
419 Default is t.
420
421 octave-send-echo-input
422 Non-nil means always display `inferior-octave-buffer' after sending a
423 command to the inferior Octave process.
424
425 octave-send-line-auto-forward
426 Non-nil means always go to the next unsent line of Octave code after
427 sending a line to the inferior Octave process.
428
429 octave-send-echo-input
430 Non-nil means echo input sent to the inferior Octave process.
431
432 Turning on Octave mode runs the hook `octave-mode-hook'.
433
434 To begin using this mode for all `.m' files that you edit, add the
435 following lines to your `.emacs' file:
436
437 (autoload 'octave-mode \"octave-mod\" nil t)
438 (setq auto-mode-alist
439 (cons '(\"\\\\.m$\" . octave-mode) auto-mode-alist))
440
441 To automatically turn on the abbrev, auto-fill and font-lock features,
442 add the following lines to your `.emacs' file as well:
443
444 (add-hook 'octave-mode-hook
445 (lambda ()
446 (abbrev-mode 1)
447 (auto-fill-mode 1)
448 (if (eq window-system 'x)
449 (font-lock-mode 1))))
450
451 To submit a problem report, enter \\[octave-submit-bug-report] from \
452 an Octave mode buffer.
453 This automatically sets up a mail buffer with version information
454 already added. You just need to add a description of the problem,
455 including a reproducible test case and send the message."
456 (interactive)
457 (kill-all-local-variables)
458
459 (use-local-map octave-mode-map)
460 (setq major-mode 'octave-mode)
461 (setq mode-name "Octave")
462 (setq local-abbrev-table octave-abbrev-table)
463 (set-syntax-table octave-mode-syntax-table)
464
465 (make-local-variable 'indent-line-function)
466 (setq indent-line-function 'octave-indent-line)
467
468 (make-local-variable 'comment-start)
469 (setq comment-start octave-comment-start)
470 (make-local-variable 'comment-end)
471 (setq comment-end "")
472 (make-local-variable 'comment-column)
473 (setq comment-column 32)
474 (make-local-variable 'comment-start-skip)
475 (setq comment-start-skip "\\s<+\\s-*")
476 (make-local-variable 'comment-indent-function)
477 (setq comment-indent-function 'octave-comment-indent)
478
479 (make-local-variable 'parse-sexp-ignore-comments)
480 (setq parse-sexp-ignore-comments t)
481 (make-local-variable 'paragraph-start)
482 (setq paragraph-start (concat "\\s-*$\\|" page-delimiter))
483 (make-local-variable 'paragraph-separate)
484 (setq paragraph-separate paragraph-start)
485 (make-local-variable 'paragraph-ignore-fill-prefix)
486 (setq paragraph-ignore-fill-prefix t)
487 (make-local-variable 'fill-paragraph-function)
488 (setq fill-paragraph-function 'octave-fill-paragraph)
489 (make-local-variable 'adaptive-fill-regexp)
490 (setq adaptive-fill-regexp nil)
491 (make-local-variable 'fill-column)
492 (setq fill-column 72)
493 (make-local-variable 'normal-auto-fill-function)
494 (setq normal-auto-fill-function 'octave-auto-fill)
495
496 (make-local-variable 'font-lock-defaults)
497 (setq font-lock-defaults '(octave-font-lock-keywords nil nil))
498
499 (make-local-variable 'imenu-generic-expression)
500 (setq imenu-generic-expression octave-mode-imenu-generic-expression)
501
502 (octave-add-octave-menu)
503 (octave-initialize-completions)
504 (run-hooks 'octave-mode-hook))
505 \f
506 ;;; Miscellaneous useful functions
507 (defun octave-describe-major-mode ()
508 "Describe the current major mode."
509 (interactive)
510 (describe-function major-mode))
511
512 (defun octave-point (position)
513 "Returns the value of point at certain positions."
514 (save-excursion
515 (cond
516 ((eq position 'bol) (beginning-of-line))
517 ((eq position 'eol) (end-of-line))
518 ((eq position 'boi) (back-to-indentation))
519 ((eq position 'bonl) (forward-line 1))
520 ((eq position 'bopl) (forward-line -1))
521 (t (error "unknown buffer position requested: %s" position)))
522 (point)))
523
524 (defsubst octave-in-comment-p ()
525 "Returns t if point is inside an Octave comment, nil otherwise."
526 (interactive)
527 (save-excursion
528 (nth 4 (parse-partial-sexp (octave-point 'bol) (point)))))
529
530 (defsubst octave-in-string-p ()
531 "Returns t if point is inside an Octave string, nil otherwise."
532 (interactive)
533 (save-excursion
534 (nth 3 (parse-partial-sexp (octave-point 'bol) (point)))))
535
536 (defsubst octave-not-in-string-or-comment-p ()
537 "Returns t iff point is not inside an Octave string or comment."
538 (let ((pps (parse-partial-sexp (octave-point 'bol) (point))))
539 (not (or (nth 3 pps) (nth 4 pps)))))
540
541 (defun octave-in-block-p ()
542 "Returns t if point is inside an Octave block, nil otherwise.
543 The block is taken to start at the first letter of the begin keyword and
544 to end after the end keyword."
545 (let ((pos (point)))
546 (save-excursion
547 (condition-case nil
548 (progn
549 (skip-syntax-forward "w")
550 (octave-up-block -1)
551 (octave-forward-block)
552 t)
553 (error nil))
554 (< pos (point)))))
555
556 (defun octave-in-defun-p ()
557 "Returns t iff point is inside an Octave function declaration.
558 The function is taken to start at the `f' of `function' and to end after
559 the end keyword."
560 (let ((pos (point)))
561 (save-excursion
562 (or (and (looking-at "\\<function\\>")
563 (octave-not-in-string-or-comment-p))
564 (and (octave-beginning-of-defun)
565 (condition-case nil
566 (progn
567 (octave-forward-block)
568 t)
569 (error nil))
570 (< pos (point)))))))
571
572 ;;; Comments
573 (defun octave-comment-region (beg end &optional arg)
574 "Comment or uncomment each line in the region as Octave code.
575 See `comment-region'."
576 (interactive "r\nP")
577 (let ((comment-start (char-to-string octave-comment-char)))
578 (comment-region beg end arg)))
579
580 (defun octave-uncomment-region (beg end &optional arg)
581 "Uncomment each line in the region as Octave code."
582 (interactive "r\nP")
583 (or arg (setq arg 1))
584 (octave-comment-region beg end (- arg)))
585
586 \f
587 ;;; Indentation
588 (defun calculate-octave-indent ()
589 "Return appropriate indentation for current line as Octave code.
590 Returns an integer (the column to indent to) unless the line is a
591 comment line with fixed goal golumn. In that case, returns a list whose
592 car is the column to indent to, and whose cdr is the current indentation
593 level."
594 (let ((is-continuation-line
595 (save-excursion
596 (if (zerop (octave-previous-code-line))
597 (looking-at octave-continuation-regexp))))
598 (icol 0))
599 (save-excursion
600 (beginning-of-line)
601 ;; If we can move backward out one level of parentheses, take 1
602 ;; plus the indentation of that parenthesis. Otherwise, go back
603 ;; to the beginning of the previous code line, and compute the
604 ;; offset this line gives.
605 (if (condition-case nil
606 (progn
607 (up-list -1)
608 t)
609 (error nil))
610 (setq icol (+ 1 (current-column)))
611 (if (zerop (octave-previous-code-line))
612 (progn
613 (octave-beginning-of-line)
614 (back-to-indentation)
615 (setq icol (current-column))
616 (let ((bot (point))
617 (eol (octave-point 'eol)))
618 (while (< (point) eol)
619 (if (octave-not-in-string-or-comment-p)
620 (cond
621 ((looking-at octave-block-begin-regexp)
622 (setq icol (+ icol octave-block-offset)))
623 ((looking-at octave-block-else-regexp)
624 (if (= bot (point))
625 (setq icol (+ icol octave-block-offset))))
626 ((looking-at octave-block-end-regexp)
627 (if (not (= bot (point)))
628 (setq icol (- icol octave-block-offset))))))
629 (forward-char)))
630 (if is-continuation-line
631 (setq icol (+ icol octave-continuation-offset)))))))
632 (save-excursion
633 (back-to-indentation)
634 (cond
635 ((and (or (looking-at octave-block-else-regexp)
636 (looking-at octave-block-end-regexp))
637 (octave-not-in-string-or-comment-p))
638 (setq icol (- icol octave-block-offset)))
639 ((looking-at "\\s<\\s<\\s<\\S<")
640 (setq icol (list 0 icol)))
641 ((looking-at "\\s<\\S<")
642 (setq icol (list comment-column icol)))))
643 icol))
644
645 (defun octave-comment-indent ()
646 (if (looking-at "\\s<\\s<\\s<")
647 0
648 (if (looking-at "\\s<\\s<")
649 (calculate-octave-indent)
650 (skip-syntax-backward " ")
651 (max (if (bolp) 0 (+ (current-column)))
652 comment-column))))
653
654 (defun octave-indent-for-comment ()
655 "Maybe insert and indent an Octave comment.
656 If there is no comment already on this line, create a code-level comment
657 (started by two comment characters) if the line is empty, or an in-line
658 comment (started by one comment character) otherwise.
659 Point is left after the start of the comment which is properly aligned."
660 (interactive)
661 (indent-for-comment)
662 (indent-according-to-mode))
663
664 (defun octave-indent-line (&optional arg)
665 "Indent current line as Octave code.
666 With optional ARG, use this as offset unless this line is a comment with
667 fixed goal column."
668 (interactive)
669 (or arg (setq arg 0))
670 (let ((icol (calculate-octave-indent))
671 (relpos (- (current-column) (current-indentation))))
672 (if (listp icol)
673 (setq icol (car icol))
674 (setq icol (+ icol arg)))
675 (if (< icol 0)
676 (error "Unmatched end keyword")
677 (indent-line-to icol)
678 (if (> relpos 0)
679 (move-to-column (+ icol relpos))))))
680
681 (defun octave-indent-new-comment-line ()
682 "Break Octave line at point, continuing comment if within one.
683 If within code, insert `octave-continuation-string' before breaking the
684 line. If within a string, signal an error.
685 The new line is properly indented."
686 (interactive)
687 (delete-horizontal-space)
688 (cond
689 ((octave-in-comment-p)
690 (indent-new-comment-line))
691 ((octave-in-string-p)
692 (error "Cannot split a code line inside a string"))
693 (t
694 (insert (concat " " octave-continuation-string))
695 (octave-reindent-then-newline-and-indent))))
696
697 (defun octave-indent-defun ()
698 "Properly indents the Octave function which contains point."
699 (interactive)
700 (save-excursion
701 (octave-mark-defun)
702 (message "Indenting function...")
703 (indent-region (point) (mark) nil))
704 (message "Indenting function...done."))
705
706 \f
707 ;;; Motion
708 (defun octave-next-code-line (&optional arg)
709 "Move ARG lines of Octave code forward (backward if ARG is negative).
710 Skips past all empty and comment lines. Default for ARG is 1.
711
712 On success, return 0. Otherwise, go as far as possible and return -1."
713 (interactive "p")
714 (or arg (setq arg 1))
715 (beginning-of-line)
716 (let ((n 0)
717 (inc (if (> arg 0) 1 -1)))
718 (while (and (/= arg 0) (= n 0))
719 (setq n (forward-line inc))
720 (while (and (= n 0)
721 (looking-at "\\s-*\\($\\|\\s<\\)"))
722 (setq n (forward-line inc)))
723 (setq arg (- arg inc)))
724 n))
725
726 (defun octave-previous-code-line (&optional arg)
727 "Move ARG lines of Octave code backward (forward if ARG is negative).
728 Skips past all empty and comment lines. Default for ARG is 1.
729
730 On success, return 0. Otherwise, go as far as possible and return -1."
731 (interactive "p")
732 (or arg (setq arg 1))
733 (octave-next-code-line (- arg)))
734
735 (defun octave-beginning-of-line ()
736 "Move point to beginning of current Octave line.
737 If on an empty or comment line, go to the beginning of that line.
738 Otherwise, move backward to the beginning of the first Octave code line
739 which is not inside a continuation statement, i.e., which does not
740 follow a code line ending in `...' or `\\', or is inside an open
741 parenthesis list."
742 (interactive)
743 (beginning-of-line)
744 (if (not (looking-at "\\s-*\\($\\|\\s<\\)"))
745 (while (or (condition-case nil
746 (progn
747 (up-list -1)
748 (beginning-of-line)
749 t)
750 (error nil))
751 (and (or (looking-at "\\s-*\\($\\|\\s<\\)")
752 (save-excursion
753 (if (zerop (octave-previous-code-line))
754 (looking-at octave-continuation-regexp))))
755 (zerop (forward-line -1)))))))
756
757 (defun octave-end-of-line ()
758 "Move point to end of current Octave line.
759 If on an empty or comment line, go to the end of that line.
760 Otherwise, move forward to the end of the first Octave code line which
761 does not end in `...' or `\\' or is inside an open parenthesis list."
762 (interactive)
763 (end-of-line)
764 (if (save-excursion
765 (beginning-of-line)
766 (looking-at "\\s-*\\($\\|\\s<\\)"))
767 ()
768 (while (or (condition-case nil
769 (progn
770 (up-list 1)
771 (end-of-line)
772 t)
773 (error nil))
774 (and (save-excursion
775 (beginning-of-line)
776 (or (looking-at "\\s-*\\($\\|\\s<\\)")
777 (looking-at octave-continuation-regexp)))
778 (zerop (forward-line 1)))))
779 (end-of-line)))
780
781 (defun octave-scan-blocks (from count depth)
782 "Scan from character number FROM by COUNT Octave begin-end blocks.
783 Returns the character number of the position thus found.
784
785 If DEPTH is nonzero, block depth begins counting from that value.
786 Only places where the depth in blocks becomes zero are candidates for
787 stopping; COUNT such places are counted.
788
789 If the beginning or end of the buffer is reached and the depth is wrong,
790 an error is signaled."
791 (let ((min-depth (if (> depth 0) 0 depth))
792 (inc (if (> count 0) 1 -1)))
793 (save-excursion
794 (while (/= count 0)
795 (catch 'foo
796 (while (or (re-search-forward
797 octave-block-begin-or-end-regexp nil 'move inc)
798 (if (/= depth 0)
799 (error "Unbalanced block")))
800 (if (octave-not-in-string-or-comment-p)
801 (progn
802 (cond
803 ((match-end 1)
804 (setq depth (+ depth inc)))
805 ((match-end 2)
806 (setq depth (- depth inc))))
807 (if (< depth min-depth)
808 (error "Containing expression ends prematurely"))
809 (if (= depth 0)
810 (throw 'foo nil))))))
811 (setq count (- count inc)))
812 (point))))
813
814 (defun octave-forward-block (&optional arg)
815 "Move forward across one balanced Octave begin-end block.
816 With argument, do it that many times.
817 Negative arg -N means move backward across N blocks."
818 (interactive "p")
819 (or arg (setq arg 1))
820 (goto-char (or (octave-scan-blocks (point) arg 0) (buffer-end arg))))
821
822 (defun octave-backward-block (&optional arg)
823 "Move backward across one balanced Octave begin-end block.
824 With argument, do it that many times.
825 Negative arg -N means move forward across N blocks."
826 (interactive "p")
827 (or arg (setq arg 1))
828 (octave-forward-block (- arg)))
829
830 (defun octave-down-block (arg)
831 "Move forward down one begin-end block level of Octave code.
832 With argument, do this that many times.
833 A negative argument means move backward but still go down a level.
834 In Lisp programs, an argument is required."
835 (interactive "p")
836 (let ((inc (if (> arg 0) 1 -1)))
837 (while (/= arg 0)
838 (goto-char (or (octave-scan-blocks (point) inc -1)
839 (buffer-end arg)))
840 (setq arg (- arg inc)))))
841
842 (defun octave-backward-up-block (arg)
843 "Move backward out of one begin-end block level of Octave code.
844 With argument, do this that many times.
845 A negative argument means move forward but still to a less deep spot.
846 In Lisp programs, an argument is required."
847 (interactive "p")
848 (octave-up-block (- arg)))
849
850 (defun octave-up-block (arg)
851 "Move forward out of one begin-end block level of Octave code.
852 With argument, do this that many times.
853 A negative argument means move backward but still to a less deep spot.
854 In Lisp programs, an argument is required."
855 (interactive "p")
856 (let ((inc (if (> arg 0) 1 -1)))
857 (while (/= arg 0)
858 (goto-char (or (octave-scan-blocks (point) inc 1)
859 (buffer-end arg)))
860 (setq arg (- arg inc)))))
861
862 (defun octave-mark-block ()
863 "Put point at the beginning of this Octave block, mark at the end.
864 The block marked is the one that contains point or follows point."
865 (interactive)
866 (let ((pos (point)))
867 (if (or (and (octave-in-block-p)
868 (skip-syntax-forward "w"))
869 (condition-case nil
870 (progn
871 (octave-down-block 1)
872 (octave-in-block-p))
873 (error nil)))
874 (progn
875 (octave-up-block -1)
876 (push-mark (point))
877 (octave-forward-block)
878 (exchange-point-and-mark))
879 (goto-char pos)
880 (message "No block to mark found"))))
881
882 (defun octave-close-block ()
883 "Close the current Octave block on a separate line.
884 An error is signaled if no block to close is found."
885 (interactive)
886 (let (bb-keyword)
887 (condition-case nil
888 (progn
889 (save-excursion
890 (octave-backward-up-block 1)
891 (setq bb-keyword (buffer-substring-no-properties
892 (match-beginning 1) (match-end 1))))
893 (if (save-excursion
894 (beginning-of-line)
895 (looking-at "^\\s-*$"))
896 (indent-according-to-mode)
897 (octave-reindent-then-newline-and-indent))
898 (insert (car (reverse
899 (assoc bb-keyword
900 octave-block-match-alist))))
901 (octave-reindent-then-newline-and-indent)
902 t)
903 (error (message "No block to close found")))))
904
905 (defun octave-blink-matching-block-open ()
906 "Blink the matching Octave begin block keyword.
907 If point is right after an Octave else or end type block keyword, move
908 cursor momentarily to the corresponding begin keyword.
909 Signal an error if the keywords are incompatible."
910 (interactive)
911 (let (bb-keyword bb-arg eb-keyword pos eol)
912 (if (and (octave-not-in-string-or-comment-p)
913 (looking-at "\\>")
914 (save-excursion
915 (skip-syntax-backward "w")
916 (looking-at octave-block-else-or-end-regexp)))
917 (save-excursion
918 (cond
919 ((match-end 1)
920 (setq eb-keyword
921 (buffer-substring-no-properties
922 (match-beginning 1) (match-end 1)))
923 (octave-backward-up-block 1))
924 ((match-end 2)
925 (setq eb-keyword
926 (buffer-substring-no-properties
927 (match-beginning 2) (match-end 2)))
928 (octave-backward-block)))
929 (setq pos (match-end 0)
930 bb-keyword
931 (buffer-substring-no-properties
932 (match-beginning 0) pos)
933 pos (+ pos 1)
934 eol (octave-point 'eol)
935 bb-arg
936 (save-excursion
937 (save-restriction
938 (goto-char pos)
939 (while (and (skip-syntax-forward "^<" eol)
940 (octave-in-string-p)
941 (not (forward-char 1))))
942 (skip-syntax-backward " ")
943 (buffer-substring-no-properties pos (point)))))
944 (if (member eb-keyword
945 (cdr (assoc bb-keyword octave-block-match-alist)))
946 (progn
947 (message "Matches `%s %s'" bb-keyword bb-arg)
948 (if (pos-visible-in-window-p)
949 (sit-for blink-matching-delay)))
950 (error "Block keywords `%s' and `%s' do not match"
951 bb-keyword eb-keyword))))))
952
953 (defun octave-beginning-of-defun (&optional arg)
954 "Move backward to the beginning of an Octave function.
955 With positive ARG, do it that many times. Negative argument -N means
956 move forward to Nth following beginning of a function.
957 Returns t unless search stops at the beginning or end of the buffer."
958 (interactive "p")
959 (let* ((arg (or arg 1))
960 (inc (if (> arg 0) 1 -1))
961 (found))
962 (and (not (eobp))
963 (not (and (> arg 0) (looking-at "\\<function\\>")))
964 (skip-syntax-forward "w"))
965 (while (and (/= arg 0)
966 (setq found
967 (re-search-backward "\\<function\\>" nil 'move inc)))
968 (if (octave-not-in-string-or-comment-p)
969 (setq arg (- arg inc))))
970 (if found
971 (progn
972 (and (< inc 0) (goto-char (match-beginning 0)))
973 t))))
974
975 (defun octave-end-of-defun (&optional arg)
976 "Move forward to the end of an Octave function.
977 With positive ARG, do it that many times. Negative argument -N means
978 move back to Nth preceding end of a function.
979
980 An end of a function occurs right after the end keyword matching the
981 `function' keyword that starts the function."
982 (interactive "p")
983 (or arg (setq arg 1))
984 (and (< arg 0) (skip-syntax-backward "w"))
985 (and (> arg 0) (skip-syntax-forward "w"))
986 (if (octave-in-defun-p)
987 (setq arg (- arg 1)))
988 (if (= arg 0) (setq arg -1))
989 (if (octave-beginning-of-defun (- arg))
990 (octave-forward-block)))
991
992 (defun octave-mark-defun ()
993 "Put point at the beginning of this Octave function, mark at its end.
994 The function marked is the one containing point or following point."
995 (interactive)
996 (let ((pos (point)))
997 (if (or (octave-in-defun-p)
998 (and (octave-beginning-of-defun -1)
999 (octave-in-defun-p)))
1000 (progn
1001 (skip-syntax-forward "w")
1002 (octave-beginning-of-defun)
1003 (push-mark (point))
1004 (octave-end-of-defun)
1005 (exchange-point-and-mark))
1006 (goto-char pos)
1007 (message "No function to mark found"))))
1008
1009 \f
1010 ;;; Filling
1011 (defun octave-auto-fill ()
1012 "Perform auto-fill in Octave mode."
1013 (if (> (current-column) (current-fill-column))
1014 (if (octave-in-comment-p)
1015 (do-auto-fill)
1016 (if (> (current-column) (current-fill-column))
1017 (let ((fill-column (- (current-fill-column)
1018 (length octave-continuation-string))))
1019 (do-auto-fill)
1020 (save-excursion
1021 (forward-line -1)
1022 (end-of-line)
1023 (insert (concat " " octave-continuation-string)))
1024 (indent-according-to-mode))))))
1025
1026 (defun octave-fill-paragraph (&optional arg)
1027 "Fill paragraph of Octave code, handling Octave comments."
1028 (interactive "P")
1029 (save-excursion
1030 (let ((end (progn (forward-paragraph) (point)))
1031 (beg (progn
1032 (forward-paragraph -1)
1033 (skip-chars-forward " \t\n")
1034 (beginning-of-line)
1035 (point)))
1036 (cfc (current-fill-column))
1037 (ind (calculate-octave-indent))
1038 comment-prefix)
1039 (save-restriction
1040 (goto-char beg)
1041 (narrow-to-region beg end)
1042 (if (listp ind) (setq ind (nth 1 ind)))
1043 (while (not (eobp))
1044 (condition-case nil
1045 (octave-indent-line ind)
1046 (error nil))
1047 (if (and (> ind 0)
1048 (not
1049 (save-excursion
1050 (beginning-of-line)
1051 (looking-at "^\\s-*\\($\\|\\s<+\\)"))))
1052 (setq ind 0))
1053 (move-to-column cfc)
1054 ;; First check whether we need to combine non-empty comment lines
1055 (if (and (< (current-column) cfc)
1056 (octave-in-comment-p)
1057 (not (save-excursion
1058 (beginning-of-line)
1059 (looking-at "^\\s-*\\s<+\\s-*$"))))
1060 ;; This is a nonempty comment line which does not extend
1061 ;; past the fill column. If it is followed by an nonempty
1062 ;; comment line with the same comment prefix, try to
1063 ;; combine them, and repeat this until either we reach the
1064 ;; fill-column or there is nothing more to combine.
1065 (progn
1066 ;; Get the comment prefix
1067 (save-excursion
1068 (beginning-of-line)
1069 (while (and (re-search-forward "\\s<+")
1070 (not (octave-in-comment-p))))
1071 (setq comment-prefix (match-string 0)))
1072 ;; And keep combining ...
1073 (while (and (< (current-column) cfc)
1074 (save-excursion
1075 (forward-line 1)
1076 (and (looking-at
1077 (concat "^\\s-*"
1078 comment-prefix
1079 "\\S<"))
1080 (not (looking-at
1081 (concat "^\\s-*"
1082 comment-prefix
1083 "\\s-*$"))))))
1084 (delete-char 1)
1085 (re-search-forward comment-prefix)
1086 (delete-region (match-beginning 0) (match-end 0))
1087 (fixup-whitespace)
1088 (move-to-column cfc))))
1089 (skip-chars-forward "^ \t\n")
1090 (delete-horizontal-space)
1091 (if (or (< (current-column) cfc)
1092 (and (= (current-column) cfc) (eolp)))
1093 (forward-line 1)
1094 (if (not (eolp)) (insert " "))
1095 (octave-auto-fill))))
1096 t)))
1097
1098 \f
1099 ;;; Completions
1100 (defun octave-initialize-completions ()
1101 "Create an alist for Octave completions."
1102 (if octave-completion-alist
1103 ()
1104 (setq octave-completion-alist
1105 (mapcar '(lambda (var) (cons var var))
1106 (append octave-reserved-words
1107 octave-text-functions
1108 octave-variables)))))
1109
1110 (defun octave-complete-symbol ()
1111 "Perform completion on Octave symbol preceding point.
1112 Compare that symbol against Octave's reserved words and builtin
1113 variables."
1114 ;; This code taken from lisp-complete-symbol
1115 (interactive)
1116 (let* ((end (point))
1117 (beg (save-excursion (backward-sexp 1) (point)))
1118 (string (buffer-substring-no-properties beg end))
1119 (completion (try-completion string octave-completion-alist)))
1120 (cond ((eq completion t)) ; ???
1121 ((null completion)
1122 (message "Can't find completion for \"%s\"" string)
1123 (ding))
1124 ((not (string= string completion))
1125 (delete-region beg end)
1126 (insert completion))
1127 (t
1128 (let ((list (all-completions string octave-completion-alist))
1129 (conf (current-window-configuration)))
1130 ;; Taken from comint.el
1131 (message "Making completion list...")
1132 (with-output-to-temp-buffer "*Completions*"
1133 (display-completion-list list))
1134 (message "Hit space to flush")
1135 (let (key first)
1136 (if (save-excursion
1137 (set-buffer (get-buffer "*Completions*"))
1138 (setq key (read-key-sequence nil)
1139 first (aref key 0))
1140 (and (consp first) (consp (event-start first))
1141 (eq (window-buffer (posn-window (event-start
1142 first)))
1143 (get-buffer "*Completions*"))
1144 (eq (key-binding key) 'mouse-choose-completion)))
1145 (progn
1146 (mouse-choose-completion first)
1147 (set-window-configuration conf))
1148 (if (eq first ?\ )
1149 (set-window-configuration conf)
1150 (setq unread-command-events
1151 (listify-key-sequence key))))))))))
1152
1153 \f
1154 ;;; Electric characters && friends
1155 (defun octave-reindent-then-newline-and-indent ()
1156 "Reindent current Octave line, insert newline, and indent the new line.
1157 If Abbrev mode is on, expand abbrevs first."
1158 (interactive)
1159 (if abbrev-mode (expand-abbrev))
1160 (if octave-blink-matching-block
1161 (octave-blink-matching-block-open))
1162 (save-excursion
1163 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
1164 (indent-according-to-mode))
1165 (insert "\n")
1166 (indent-according-to-mode))
1167
1168 (defun octave-electric-semi ()
1169 "Insert a semicolon in Octave mode.
1170 Always reindent the line. Insert a newline if `octave-auto-newline' is
1171 non-nil."
1172 (interactive)
1173 (if (not (octave-not-in-string-or-comment-p))
1174 (insert ";")
1175 (if abbrev-mode (expand-abbrev))
1176 (if octave-blink-matching-block
1177 (octave-blink-matching-block-open))
1178 (indent-according-to-mode)
1179 (insert ";")
1180 (if octave-auto-newline
1181 (newline-and-indent))))
1182
1183 (defun octave-electric-space ()
1184 "Insert a space in Octave mode.
1185 Maybe expand abbrevs and blink matching block open keywords."
1186 (interactive)
1187 (setq last-command-char ? )
1188 (if (not (octave-not-in-string-or-comment-p))
1189 (progn
1190 (indent-according-to-mode)
1191 (self-insert-command 1))
1192 (if abbrev-mode (expand-abbrev))
1193 (if octave-blink-matching-block
1194 (octave-blink-matching-block-open))
1195 (if (save-excursion
1196 (skip-syntax-backward " ")
1197 (not (bolp)))
1198 (indent-according-to-mode))
1199 (self-insert-command 1)))
1200
1201 (defun octave-abbrev-start ()
1202 "Start entering an Octave abbreviation.
1203 If Abbrev mode is turned on, typing ` (grave accent) followed by ? or
1204 \\[help-command] lists all Octave abbrevs. Any other key combination is
1205 executed normally.
1206 Note that all Octave mode abbrevs start with a grave accent."
1207 (interactive)
1208 (if (not abbrev-mode)
1209 (self-insert-command 1)
1210 (let (c)
1211 (insert last-command-char)
1212 (if (or (eq (setq c (read-event)) ??)
1213 (eq c help-char))
1214 (let ((abbrev-table-name-list '(octave-mode-abbrev-table)))
1215 (list-abbrevs))
1216 (setq unread-command-events (list c))))))
1217
1218 (defun octave-insert-defun (name args vals)
1219 "Insert an Octave function skeleton.
1220 Prompt for the function's name, arguments and return values (to be
1221 entered without parens)."
1222 (interactive
1223 (list
1224 (read-from-minibuffer "Function name: "
1225 (substring (buffer-name) 0 -2))
1226 (read-from-minibuffer "Arguments: ")
1227 (read-from-minibuffer "Return values: ")))
1228 (let ((string (format "%s %s (%s)"
1229 (cond
1230 ((string-equal vals "")
1231 vals)
1232 ((string-match "[ ,]" vals)
1233 (concat " [" vals "] ="))
1234 (t
1235 (concat " " vals " =")))
1236 name
1237 args))
1238 (prefix octave-block-comment-start))
1239 (if (not (bobp)) (newline))
1240 (insert "function" string)
1241 (indent-according-to-mode)
1242 (newline 2)
1243 (insert prefix "usage: " string)
1244 (reindent-then-newline-and-indent)
1245 (insert prefix)
1246 (reindent-then-newline-and-indent)
1247 (insert prefix)
1248 (indent-according-to-mode)
1249 (save-excursion
1250 (newline 2)
1251 (insert "endfunction")
1252 (indent-according-to-mode))))
1253
1254 \f
1255 ;;; Menu
1256 (defun octave-add-octave-menu ()
1257 "Adds the `Octave' menu to the menu bar in Octave mode."
1258 (require 'easymenu)
1259 (easy-menu-define octave-mode-menu-map octave-mode-map
1260 "Menu keymap for Octave mode." octave-mode-menu)
1261 (easy-menu-add octave-mode-menu-map octave-mode-map))
1262
1263 \f
1264 ;;; Communication with the inferior Octave process
1265 (defun octave-kill-process ()
1266 "Kill inferior Octave process and its buffer."
1267 (interactive)
1268 (if inferior-octave-process
1269 (progn
1270 (process-send-string inferior-octave-process "quit;\n")
1271 (accept-process-output inferior-octave-process)))
1272 (if inferior-octave-buffer
1273 (kill-buffer inferior-octave-buffer)))
1274
1275 (defun octave-show-process-buffer ()
1276 "Make sure that `inferior-octave-buffer' is displayed."
1277 (interactive)
1278 (if (get-buffer inferior-octave-buffer)
1279 (display-buffer inferior-octave-buffer)
1280 (message "No buffer named %s" inferior-octave-buffer)))
1281
1282 (defun octave-hide-process-buffer ()
1283 "Delete all windows that display `inferior-octave-buffer'."
1284 (interactive)
1285 (if (get-buffer inferior-octave-buffer)
1286 (delete-windows-on inferior-octave-buffer)
1287 (message "No buffer named %s" inferior-octave-buffer)))
1288
1289 (defun octave-send-region (beg end)
1290 "Send current region to the inferior Octave process."
1291 (interactive "r")
1292 (inferior-octave t)
1293 (let ((proc inferior-octave-process)
1294 (string (buffer-substring-no-properties beg end))
1295 line)
1296 (save-excursion
1297 (set-buffer inferior-octave-buffer)
1298 (setq inferior-octave-output-list nil)
1299 (while (not (string-equal string ""))
1300 (if (string-match "\n" string)
1301 (setq line (substring string 0 (match-beginning 0))
1302 string (substring string (match-end 0)))
1303 (setq line string string ""))
1304 (setq inferior-octave-receive-in-progress t)
1305 (inferior-octave-send-list-and-digest (list (concat line "\n")))
1306 (while inferior-octave-receive-in-progress
1307 (accept-process-output proc))
1308 (insert-before-markers
1309 (mapconcat 'identity
1310 (append
1311 (if octave-send-echo-input (list line) (list ""))
1312 (mapcar 'inferior-octave-strip-ctrl-g
1313 inferior-octave-output-list)
1314 (list inferior-octave-output-string))
1315 "\n")))))
1316 (if octave-send-show-buffer
1317 (display-buffer inferior-octave-buffer)))
1318
1319 (defun octave-send-block ()
1320 "Send current Octave block to the inferior Octave process."
1321 (interactive)
1322 (save-excursion
1323 (octave-mark-block)
1324 (octave-send-region (point) (mark))))
1325
1326 (defun octave-send-defun ()
1327 "Send current Octave function to the inferior Octave process."
1328 (interactive)
1329 (save-excursion
1330 (octave-mark-defun)
1331 (octave-send-region (point) (mark))))
1332
1333 (defun octave-send-line (&optional arg)
1334 "Send current Octave code line to the inferior Octave process.
1335 With positive prefix ARG, send that many lines.
1336 If `octave-send-line-auto-forward' is non-nil, go to the next unsent
1337 code line."
1338 (interactive "P")
1339 (or arg (setq arg 1))
1340 (if (> arg 0)
1341 (let (beg end)
1342 (beginning-of-line)
1343 (setq beg (point))
1344 (octave-next-code-line (- arg 1))
1345 (end-of-line)
1346 (setq end (point))
1347 (if octave-send-line-auto-forward
1348 (octave-next-code-line 1))
1349 (octave-send-region beg end))))
1350
1351 (defun octave-eval-print-last-sexp ()
1352 "Evaluate Octave sexp before point and print value into current buffer."
1353 (interactive)
1354 (inferior-octave t)
1355 (let ((standard-output (current-buffer))
1356 (print-escape-newlines nil)
1357 (opoint (point)))
1358 (terpri)
1359 (prin1
1360 (save-excursion
1361 (forward-sexp -1)
1362 (inferior-octave-send-list-and-digest
1363 (list (concat (buffer-substring-no-properties (point) opoint)
1364 "\n")))
1365 (mapconcat 'identity inferior-octave-output-list "\n")))
1366 (terpri)))
1367 \f
1368 ;;; Bug reporting
1369 (defun octave-submit-bug-report ()
1370 "Submit a bug report on the Emacs Octave package via mail."
1371 (interactive)
1372 (require 'reporter)
1373 (and
1374 (y-or-n-p "Do you want to submit a bug report? ")
1375 (reporter-submit-bug-report
1376 octave-maintainer-address
1377 (concat "Emacs version " emacs-version)
1378 (list
1379 'octave-auto-newline
1380 'octave-blink-matching-block
1381 'octave-block-offset
1382 'octave-comment-char
1383 'octave-continuation-offset
1384 'octave-continuation-string
1385 'octave-help-files
1386 'octave-mode-startup-message
1387 'octave-send-echo-input
1388 'octave-send-line-auto-forward
1389 'octave-send-show-buffer))))
1390
1391 ;;; provide ourself
1392
1393 (provide 'octave-mod)
1394
1395 ;;; octave-mod.el ends here