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