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