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