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