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