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