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