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