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