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