1 ;;; octave-mod.el --- editing Octave source files under Emacs
3 ;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
7 ;; Author: John Eaton <jwe@octave.org>
8 ;; Maintainer: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
11 ;; This file is part of GNU Emacs.
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 of the License, or
16 ;; (at your option) any later version.
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This package provides Emacs support for Octave.
29 ;; It defines Octave mode, a major mode for editing
32 ;; The file octave-inf.el contains code for interacting with an inferior
33 ;; Octave process using comint.
35 ;; See the documentation of `octave-mode' and
36 ;; `run-octave' for further information on usage and customization.
42 "Major mode for editing Octave source files."
43 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
46 (defvar inferior-octave-output-list nil
)
47 (defvar inferior-octave-output-string nil
)
48 (defvar inferior-octave-receive-in-progress nil
)
50 (declare-function inferior-octave-send-list-and-digest
"octave-inf" (list))
52 (defconst octave-maintainer-address
53 "Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>, bug-gnu-emacs@gnu.org"
54 "Current maintainer of the Emacs Octave package.")
56 (define-abbrev-table 'octave-abbrev-table
57 (mapcar (lambda (e) (append e
'(nil 0 t
)))
58 '(("`a" "all_va_args")
65 ("`et" "end_try_catch")
66 ("`eu" "end_unwind_protect")
68 ("`efu" "endfunction")
84 ("`up" "unwind_protect")
85 ("`upc" "unwind_protect_cleanup")
87 "Abbrev table for Octave's reserved words.
88 Used in `octave-mode' and inferior-octave-mode buffers.
89 All Octave abbrevs start with a grave accent (`)."
90 :regexp
"\\(?:[^`]\\|^\\)\\(\\(?:\\<\\|`\\)\\w+\\)\\W*")
92 (defvar octave-comment-char ?
#
93 "Character to start an Octave comment.")
94 (defvar octave-comment-start
95 (string octave-comment-char ?\s
)
96 "String to insert to start a new Octave in-line comment.")
97 (defvar octave-comment-start-skip
"\\s<+\\s-*"
98 "Regexp to match the start of an Octave comment up to its body.")
100 (defvar octave-begin-keywords
101 '("do" "for" "function" "if" "switch" "try" "unwind_protect" "while"))
102 (defvar octave-else-keywords
103 '("case" "catch" "else" "elseif" "otherwise" "unwind_protect_cleanup"))
104 (defvar octave-end-keywords
105 '("endfor" "endfunction" "endif" "endswitch" "end_try_catch"
106 "end_unwind_protect" "endwhile" "until" "end"))
108 (defvar octave-reserved-words
109 (append octave-begin-keywords
112 '("break" "continue" "end" "global" "persistent" "return"))
113 "Reserved words in Octave.")
115 (defvar octave-text-functions
116 '("casesen" "cd" "chdir" "clear" "diary" "dir" "document" "echo"
117 "edit_history" "format" "help" "history" "hold"
118 "load" "ls" "more" "run_history" "save" "type"
119 "which" "who" "whos")
120 "Text functions in Octave.")
122 (defvar octave-variables
123 '("DEFAULT_EXEC_PATH" "DEFAULT_LOADPATH"
124 "EDITOR" "EXEC_PATH" "F_DUPFD" "F_GETFD" "F_GETFL" "F_SETFD"
125 "F_SETFL" "I" "IMAGE_PATH" "Inf" "J"
126 "NaN" "OCTAVE_VERSION" "O_APPEND" "O_CREAT" "O_EXCL"
127 "O_NONBLOCK" "O_RDONLY" "O_RDWR" "O_TRUNC" "O_WRONLY" "PAGER" "PS1"
128 "PS2" "PS4" "PWD" "SEEK_CUR" "SEEK_END" "SEEK_SET" "__F_DUPFD__"
129 "__F_GETFD__" "__F_GETFL__" "__F_SETFD__" "__F_SETFL__" "__I__"
130 "__Inf__" "__J__" "__NaN__" "__OCTAVE_VERSION__" "__O_APPEND__"
131 "__O_CREAT__" "__O_EXCL__" "__O_NONBLOCK__" "__O_RDONLY__"
132 "__O_RDWR__" "__O_TRUNC__" "__O_WRONLY__" "__PWD__" "__SEEK_CUR__"
133 "__SEEK_END__" "__SEEK_SET__" "__argv__" "__e__" "__eps__"
134 "__i__" "__inf__" "__j__" "__nan__" "__pi__"
135 "__program_invocation_name__" "__program_name__" "__realmax__"
136 "__realmin__" "__stderr__" "__stdin__" "__stdout__" "ans" "argv"
137 "beep_on_error" "completion_append_char"
138 "crash_dumps_octave_core" "default_save_format"
139 "e" "echo_executing_commands" "eps"
140 "error_text" "gnuplot_binary" "history_file"
141 "history_size" "ignore_function_time_stamp"
142 "inf" "nan" "nargin" "output_max_field_width" "output_precision"
143 "page_output_immediately" "page_screen_output" "pi"
144 "print_answer_id_name" "print_empty_dimensions"
145 "program_invocation_name" "program_name"
146 "realmax" "realmin" "return_last_computed_value" "save_precision"
147 "saving_history" "sighup_dumps_octave_core" "sigterm_dumps_octave_core"
148 "silent_functions" "split_long_rows" "stderr" "stdin" "stdout"
149 "string_fill_char" "struct_levels_to_print"
150 "suppress_verbose_help_message")
151 "Builtin variables in Octave.")
153 (defvar octave-function-header-regexp
154 (concat "^\\s-*\\<\\(function\\)\\>"
155 "\\([^=;\n]*=[ \t]*\\|[ \t]*\\)\\(\\w+\\)\\>")
156 "Regexp to match an Octave function header.
157 The string `function' and its name are given by the first and third
158 parenthetical grouping.")
160 (defvar octave-font-lock-keywords
162 ;; Fontify all builtin keywords.
163 (cons (concat "\\<\\("
164 (mapconcat 'identity octave-reserved-words
"\\|")
165 (mapconcat 'identity octave-text-functions
"\\|")
167 'font-lock-keyword-face
)
168 ;; Fontify all builtin operators.
169 (cons "\\(&\\||\\|<=\\|>=\\|==\\|<\\|>\\|!=\\|!\\)"
170 (if (boundp 'font-lock-builtin-face
)
171 'font-lock-builtin-face
172 'font-lock-preprocessor-face
))
173 ;; Fontify all builtin variables.
174 (cons (concat "\\<" (regexp-opt octave-variables
) "\\>")
175 'font-lock-variable-name-face
)
176 ;; Fontify all function declarations.
177 (list octave-function-header-regexp
178 '(1 font-lock-keyword-face
)
179 '(3 font-lock-function-name-face nil t
)))
180 "Additional Octave expressions to highlight.")
182 (defvar octave-font-lock-syntactic-keywords
183 ;; Try to distinguish the string-quotes from the transpose-quotes.
184 '(("[[({,; ]\\('\\)" (1 "\"'"))
185 (octave-font-lock-close-quotes)))
187 (defun octave-font-lock-close-quotes (limit)
188 "Fix the syntax-table of the closing quotes of single-quote strings."
189 ;; Freely inspired from perl-font-lock-special-syntactic-constructs.
190 (let ((state (syntax-ppss)))
191 (while (< (point) limit
)
193 ((eq (nth 3 state
) ?
\')
196 (when (and (or (looking-at "\\('\\)")
197 (re-search-forward "[^\\]\\(?:\\\\\\\\\\)*\\('\\)"
200 (put-text-property (match-beginning 1) (match-end 1)
201 'syntax-table
(string-to-syntax "\"'"))))))
203 (setq state
(parse-partial-sexp (point) limit nil nil state
206 (defcustom inferior-octave-buffer
"*Inferior Octave*"
207 "Name of buffer for running an inferior Octave process."
209 :group
'octave-inferior
)
211 (defvar inferior-octave-process nil
)
213 (defvar octave-mode-map
214 (let ((map (make-sparse-keymap)))
215 (define-key map
"`" 'octave-abbrev-start
)
216 (define-key map
";" 'octave-electric-semi
)
217 (define-key map
" " 'octave-electric-space
)
218 (define-key map
"\n" 'octave-reindent-then-newline-and-indent
)
219 (define-key map
"\e\n" 'octave-indent-new-comment-line
)
220 (define-key map
"\M-\C-q" 'octave-indent-defun
)
221 (define-key map
"\C-c\C-b" 'octave-submit-bug-report
)
222 (define-key map
"\C-c\C-p" 'octave-previous-code-line
)
223 (define-key map
"\C-c\C-n" 'octave-next-code-line
)
224 (define-key map
"\C-c\C-a" 'octave-beginning-of-line
)
225 (define-key map
"\C-c\C-e" 'octave-end-of-line
)
226 (define-key map
"\C-c\M-\C-n" 'octave-forward-block
)
227 (define-key map
"\C-c\M-\C-p" 'octave-backward-block
)
228 (define-key map
"\C-c\M-\C-u" 'octave-backward-up-block
)
229 (define-key map
"\C-c\M-\C-d" 'octave-down-block
)
230 (define-key map
"\C-c\M-\C-h" 'octave-mark-block
)
231 (define-key map
"\C-c]" 'octave-close-block
)
232 (define-key map
"\C-c/" 'octave-close-block
)
233 (define-key map
"\C-c\C-f" 'octave-insert-defun
)
234 (define-key map
"\C-c\C-h" 'octave-help
)
235 (define-key map
"\C-c\C-il" 'octave-send-line
)
236 (define-key map
"\C-c\C-ib" 'octave-send-block
)
237 (define-key map
"\C-c\C-if" 'octave-send-defun
)
238 (define-key map
"\C-c\C-ir" 'octave-send-region
)
239 (define-key map
"\C-c\C-is" 'octave-show-process-buffer
)
240 (define-key map
"\C-c\C-ih" 'octave-hide-process-buffer
)
241 (define-key map
"\C-c\C-ik" 'octave-kill-process
)
242 (define-key map
"\C-c\C-i\C-l" 'octave-send-line
)
243 (define-key map
"\C-c\C-i\C-b" 'octave-send-block
)
244 (define-key map
"\C-c\C-i\C-f" 'octave-send-defun
)
245 (define-key map
"\C-c\C-i\C-r" 'octave-send-region
)
246 (define-key map
"\C-c\C-i\C-s" 'octave-show-process-buffer
)
247 (define-key map
"\C-c\C-i\C-h" 'octave-hide-process-buffer
)
248 (define-key map
"\C-c\C-i\C-k" 'octave-kill-process
)
250 "Keymap used in Octave mode.")
254 (easy-menu-define octave-mode-menu octave-mode-map
255 "Menu for Octave mode."
258 ["Previous Code Line" octave-previous-code-line t
]
259 ["Next Code Line" octave-next-code-line t
]
260 ["Begin of Continuation" octave-beginning-of-line t
]
261 ["End of Continuation" octave-end-of-line t
]
262 ["Split Line at Point" octave-indent-new-comment-line t
])
264 ["Next Block" octave-forward-block t
]
265 ["Previous Block" octave-backward-block t
]
266 ["Down Block" octave-down-block t
]
267 ["Up Block" octave-backward-up-block t
]
268 ["Mark Block" octave-mark-block t
]
269 ["Close Block" octave-close-block t
])
271 ["Indent Function" octave-indent-defun
t]
272 ["Insert Function" octave-insert-defun
t])
275 ["Send Current Line" octave-send-line t
]
276 ["Send Current Block" octave-send-block t
]
277 ["Send Current Function" octave-send-defun
t]
278 ["Send Region" octave-send-region t
]
279 ["Show Process Buffer" octave-show-process-buffer t
]
280 ["Hide Process Buffer" octave-hide-process-buffer t
]
281 ["Kill Process" octave-kill-process t
])
283 ["Indent Line" indent-according-to-mode t
]
284 ["Complete Symbol" completion-at-point t
]
286 ;; FIXME: Make them toggle-buttons.
287 ["Toggle Abbrev Mode" abbrev-mode t
]
288 ["Toggle Auto-Fill Mode" auto-fill-mode t
]
290 ["Submit Bug Report" octave-submit-bug-report t
]
292 ["Describe Octave Mode" describe-mode t
]
293 ["Lookup Octave Index" info-lookup-symbol t
]))
295 (defvar octave-mode-syntax-table
296 (let ((table (make-syntax-table)))
297 (modify-syntax-entry ?
\r " " table
)
298 (modify-syntax-entry ?
+ "." table
)
299 (modify-syntax-entry ?-
"." table
)
300 (modify-syntax-entry ?
= "." table
)
301 (modify-syntax-entry ?
* "." table
)
302 (modify-syntax-entry ?
/ "." table
)
303 (modify-syntax-entry ?
> "." table
)
304 (modify-syntax-entry ?
< "." table
)
305 (modify-syntax-entry ?
& "." table
)
306 (modify-syntax-entry ?|
"." table
)
307 (modify-syntax-entry ?
! "." table
)
308 (modify-syntax-entry ?
\\ "\\" table
)
309 (modify-syntax-entry ?
\' "." table
)
310 ;; Was "w" for abbrevs, but now that it's not necessary any more,
311 (modify-syntax-entry ?\
` "." table
)
312 (modify-syntax-entry ?
\" "\"" table
)
313 (modify-syntax-entry ?.
"w" table
)
314 (modify-syntax-entry ?_
"w" table
)
315 ;; The "b" flag only applies to the second letter of the comstart
316 ;; and the first letter of the comend, i.e. the "4b" below is ineffective.
317 ;; If we try to put `b' on the single-line comments, we get a similar
318 ;; problem where the % and # chars appear as first chars of the 2-char
319 ;; comend, so the multi-line ender is also turned into style-b.
320 ;; So we need the new "c" comment style.
321 (modify-syntax-entry ?\%
"< 13" table
)
322 (modify-syntax-entry ?\
# "< 13" table
)
323 (modify-syntax-entry ?\
{ "(} 2c" table
)
324 (modify-syntax-entry ?\
} "){ 4c" table
)
325 (modify-syntax-entry ?
\n ">" table
)
327 "Syntax table in use in `octave-mode' buffers.")
329 (defcustom octave-auto-indent nil
330 "Non-nil means indent line after a semicolon or space in Octave mode."
334 (defcustom octave-auto-newline nil
335 "Non-nil means automatically newline after a semicolon in Octave mode."
339 (defcustom octave-blink-matching-block t
340 "Control the blinking of matching Octave block keywords.
341 Non-nil means show matching begin of block when inserting a space,
342 newline or semicolon after an else or end keyword."
346 (defcustom octave-block-offset
2
347 "Extra indentation applied to statements in Octave block structures."
351 (defvar octave-block-begin-regexp
353 (mapconcat 'identity octave-begin-keywords
"\\|")
355 (defvar octave-block-else-regexp
357 (mapconcat 'identity octave-else-keywords
"\\|")
359 (defvar octave-block-end-regexp
361 (mapconcat 'identity octave-end-keywords
"\\|")
363 (defvar octave-block-begin-or-end-regexp
364 (concat octave-block-begin-regexp
"\\|" octave-block-end-regexp
))
365 (defvar octave-block-else-or-end-regexp
366 (concat octave-block-else-regexp
"\\|" octave-block-end-regexp
))
367 (defvar octave-block-match-alist
369 ("for" .
("end" "endfor"))
370 ("function" .
("end" "endfunction"))
371 ("if" .
("else" "elseif" "end" "endif"))
372 ("switch" .
("case" "otherwise" "end" "endswitch"))
373 ("try" .
("catch" "end" "end_try_catch"))
374 ("unwind_protect" .
("unwind_protect_cleanup" "end" "end_unwind_protect"))
375 ("while" .
("end" "endwhile")))
376 "Alist with Octave's matching block keywords.
377 Has Octave's begin keywords as keys and a list of the matching else or
378 end keywords as associated values.")
380 (defvar octave-block-comment-start
381 (concat (make-string 2 octave-comment-char
) " ")
382 "String to insert to start a new Octave comment on an empty line.")
384 (defcustom octave-continuation-offset
4
385 "Extra indentation applied to Octave continuation lines."
388 (defvar octave-continuation-regexp
389 "[^#%\n]*\\(\\\\\\|\\.\\.\\.\\)\\s-*\\(\\s<.*\\)?$")
390 (defcustom octave-continuation-string
"\\"
391 "Character string used for Octave continuation lines. Normally \\."
395 (defvar octave-completion-alist nil
396 "Alist of Octave symbols for completion in Octave mode.
397 Each element looks like (VAR . VAR), where the car and cdr are the same
398 symbol (an Octave command or variable name).
399 Currently, only builtin variables can be completed.")
401 (defvar octave-mode-imenu-generic-expression
404 (list nil octave-function-header-regexp
3))
405 "Imenu expression for Octave mode. See `imenu-generic-expression'.")
407 (defcustom octave-mode-hook nil
408 "Hook to be run when Octave mode is started."
412 (defcustom octave-send-show-buffer t
413 "Non-nil means display `inferior-octave-buffer' after sending to it."
416 (defcustom octave-send-line-auto-forward t
417 "Control auto-forward after sending to the inferior Octave process.
418 Non-nil means always go to the next Octave code line after sending."
421 (defcustom octave-send-echo-input t
422 "Non-nil means echo input sent to the inferior Octave process."
428 (define-derived-mode octave-mode prog-mode
"Octave"
429 "Major mode for editing Octave code.
431 This mode makes it easier to write Octave code by helping with
432 indentation, doing some of the typing for you (with Abbrev mode) and by
433 showing keywords, comments, strings, etc. in different faces (with
434 Font Lock mode on terminals that support it).
436 Octave itself is a high-level language, primarily intended for numerical
437 computations. It provides a convenient command line interface for
438 solving linear and nonlinear problems numerically. Function definitions
439 can also be stored in files, and it can be used in a batch mode (which
440 is why you need this mode!).
442 The latest released version of Octave is always available via anonymous
443 ftp from ftp.octave.org in the directory `/pub/octave'. Complete
444 source and binaries for several popular systems are available.
446 Type \\[list-abbrevs] to display the built-in abbrevs for Octave keywords.
453 Variables you can use to customize Octave mode
454 ==============================================
457 Non-nil means indent current line after a semicolon or space.
460 `octave-auto-newline'
461 Non-nil means auto-insert a newline and indent after a semicolon.
464 `octave-blink-matching-block'
465 Non-nil means show matching begin of block when inserting a space,
466 newline or semicolon after an else or end keyword. Default is t.
468 `octave-block-offset'
469 Extra indentation applied to statements in block structures.
472 `octave-continuation-offset'
473 Extra indentation applied to Octave continuation lines.
476 `octave-continuation-string'
477 String used for Octave continuation lines.
478 Default is a backslash.
480 `octave-send-echo-input'
481 Non-nil means always display `inferior-octave-buffer' after sending a
482 command to the inferior Octave process.
484 `octave-send-line-auto-forward'
485 Non-nil means always go to the next unsent line of Octave code after
486 sending a line to the inferior Octave process.
488 `octave-send-echo-input'
489 Non-nil means echo input sent to the inferior Octave process.
491 Turning on Octave mode runs the hook `octave-mode-hook'.
493 To begin using this mode for all `.m' files that you edit, add the
494 following lines to your `.emacs' file:
496 (add-to-list 'auto-mode-alist '(\"\\\\.m\\\\'\" . octave-mode))
498 To automatically turn on the abbrev and auto-fill features,
499 add the following lines to your `.emacs' file as well:
501 (add-hook 'octave-mode-hook
506 To submit a problem report, enter \\[octave-submit-bug-report] from \
507 an Octave mode buffer.
508 This automatically sets up a mail buffer with version information
509 already added. You just need to add a description of the problem,
510 including a reproducible test case and send the message."
511 (setq local-abbrev-table octave-abbrev-table
)
513 (set (make-local-variable 'indent-line-function
) 'octave-indent-line
)
515 (set (make-local-variable 'comment-start
) octave-comment-start
)
516 (set (make-local-variable 'comment-end
) "")
517 ;; Don't set it here: it's not really a property of the language,
518 ;; just a personal preference of the author.
519 ;; (set (make-local-variable 'comment-column) 32)
520 (set (make-local-variable 'comment-start-skip
) "\\s<+\\s-*")
521 (set (make-local-variable 'comment-add
) 1)
523 (set (make-local-variable 'parse-sexp-ignore-comments
) t
)
524 (set (make-local-variable 'paragraph-start
)
525 (concat "\\s-*$\\|" page-delimiter
))
526 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
527 (set (make-local-variable 'paragraph-ignore-fill-prefix
) t
)
528 (set (make-local-variable 'fill-paragraph-function
) 'octave-fill-paragraph
)
529 ;; FIXME: Why disable it?
530 ;; (set (make-local-variable 'adaptive-fill-regexp) nil)
531 ;; Again, this is not a property of the language, don't set it here.
532 ;; (set (make-local-variable 'fill-column) 72)
533 (set (make-local-variable 'normal-auto-fill-function
) 'octave-auto-fill
)
535 (set (make-local-variable 'font-lock-defaults
)
536 '(octave-font-lock-keywords nil nil nil nil
537 (font-lock-syntactic-keywords . octave-font-lock-syntactic-keywords
)
538 (parse-sexp-lookup-properties . t
)))
540 (set (make-local-variable 'imenu-generic-expression
)
541 octave-mode-imenu-generic-expression
)
542 (set (make-local-variable 'imenu-case-fold-search
) nil
)
544 (add-hook 'completion-at-point-functions
545 'octave-completion-at-point-function nil t
)
546 (set (make-local-variable 'beginning-of-defun-function
)
547 'octave-beginning-of-defun
)
549 (easy-menu-add octave-mode-menu
)
550 (octave-initialize-completions)
551 (run-mode-hooks 'octave-mode-hook
))
553 (defvar info-lookup-mode
)
555 (defun octave-help ()
556 "Get help on Octave symbols from the Octave info files.
557 Look up symbol in the function, operator and variable indices of the info files."
558 (let ((info-lookup-mode 'octave-mode
))
559 (call-interactively 'info-lookup-symbol
)))
561 ;;; Miscellaneous useful functions
563 (defsubst octave-in-comment-p
()
564 "Return t if point is inside an Octave comment."
566 ;; FIXME: use syntax-ppss?
567 (nth 4 (parse-partial-sexp (line-beginning-position) (point)))))
569 (defsubst octave-in-string-p
()
570 "Return t if point is inside an Octave string."
572 ;; FIXME: use syntax-ppss?
573 (nth 3 (parse-partial-sexp (line-beginning-position) (point)))))
575 (defsubst octave-not-in-string-or-comment-p
()
576 "Return t if point is not inside an Octave string or comment."
577 ;; FIXME: Use syntax-ppss?
578 (let ((pps (parse-partial-sexp (line-beginning-position) (point))))
579 (not (or (nth 3 pps
) (nth 4 pps
)))))
581 (defun octave-in-block-p ()
582 "Return t if point is inside an Octave block.
583 The block is taken to start at the first letter of the begin keyword and
584 to end after the end keyword."
589 (skip-syntax-forward "w")
591 (octave-forward-block)
596 (defun octave-looking-at-kw (regexp)
597 "Like `looking-at', but sets `case-fold-search' nil."
598 (let ((case-fold-search nil
))
599 (looking-at regexp
)))
601 (defun octave-re-search-forward-kw (regexp count
)
602 "Like `re-search-forward', but sets `case-fold-search' nil, and moves point."
603 (let ((case-fold-search nil
))
604 (re-search-forward regexp nil
'move count
)))
606 (defun octave-re-search-backward-kw (regexp count
)
607 "Like `re-search-backward', but sets `case-fold-search' nil, and moves point."
608 (let ((case-fold-search nil
))
609 (re-search-backward regexp nil
'move count
)))
611 (defun octave-maybe-insert-continuation-string ()
612 (if (or (octave-in-comment-p)
615 (looking-at octave-continuation-regexp
)))
617 (delete-horizontal-space)
618 (insert (concat " " octave-continuation-string
))))
622 (defun octave-indent-calculate ()
623 "Return appropriate indentation for current line as Octave code.
624 Returns an integer (the column to indent to) unless the line is a
625 comment line with fixed goal golumn. In that case, returns a list whose
626 car is the column to indent to, and whose cdr is the current indentation
628 (let ((is-continuation-line
630 (if (zerop (octave-previous-code-line))
631 (looking-at octave-continuation-regexp
))))
635 ;; If we can move backward out one level of parentheses, take 1
636 ;; plus the indentation of that parenthesis. Otherwise, go back
637 ;; to the beginning of the previous code line, and compute the
638 ;; offset this line gives.
639 (if (condition-case nil
644 (setq icol
(+ 1 (current-column)))
645 (if (zerop (octave-previous-code-line))
647 (octave-beginning-of-line)
648 (back-to-indentation)
649 (setq icol
(current-column))
651 (eol (line-end-position)))
652 (while (< (point) eol
)
653 (if (octave-not-in-string-or-comment-p)
655 ((octave-looking-at-kw "\\<switch\\>")
656 (setq icol
(+ icol
(* 2 octave-block-offset
))))
657 ((octave-looking-at-kw octave-block-begin-regexp
)
658 (setq icol
(+ icol octave-block-offset
)))
659 ((octave-looking-at-kw octave-block-else-regexp
)
661 (setq icol
(+ icol octave-block-offset
))))
662 ((octave-looking-at-kw octave-block-end-regexp
)
663 (if (and (not (= bot
(point)))
664 ;; special case for `end' keyword,
665 ;; applied to all keywords
666 (not (octave-end-as-array-index-p)))
668 (octave-block-end-offset)))))))
670 (if is-continuation-line
671 (setq icol
(+ icol octave-continuation-offset
)))))))
673 (back-to-indentation)
675 ((and (octave-looking-at-kw octave-block-else-regexp
)
676 (octave-not-in-string-or-comment-p))
677 (setq icol
(- icol octave-block-offset
)))
678 ((and (octave-looking-at-kw octave-block-end-regexp
)
679 (octave-not-in-string-or-comment-p))
680 (setq icol
(- icol
(octave-block-end-offset))))
681 ((or (looking-at "\\s<\\s<\\s<\\S<")
682 (octave-before-magic-comment-p))
683 (setq icol
(list 0 icol
)))
684 ((looking-at "\\s<\\S<")
685 (setq icol
(list comment-column icol
)))))
688 ;; FIXME: this should probably also make sure we are actually looking
689 ;; at the "end" keyword.
690 (defun octave-end-as-array-index-p ()
693 ;; Check if point is between parens
694 (progn (up-list 1) t
)
697 (defun octave-block-end-offset ()
699 (octave-backward-up-block 1)
700 (* octave-block-offset
701 (if (string-match (match-string 0) "switch") 2 1))))
703 (defun octave-before-magic-comment-p ()
706 (and (bobp) (looking-at "\\s-*#!"))))
708 (defun octave-indent-line (&optional arg
)
709 "Indent current line as Octave code.
710 With optional ARG, use this as offset unless this line is a comment with
713 (or arg
(setq arg
0))
714 (let ((icol (octave-indent-calculate))
715 (relpos (- (current-column) (current-indentation))))
717 (setq icol
(car icol
))
718 (setq icol
(+ icol arg
)))
720 (error "Unmatched end keyword")
721 (indent-line-to icol
)
723 (move-to-column (+ icol relpos
))))))
725 (defun octave-indent-new-comment-line ()
726 "Break Octave line at point, continuing comment if within one.
727 If within code, insert `octave-continuation-string' before breaking the
728 line. If within a string, signal an error.
729 The new line is properly indented."
731 (delete-horizontal-space)
733 ((octave-in-comment-p)
734 (indent-new-comment-line))
735 ((octave-in-string-p)
736 (error "Cannot split a code line inside a string"))
738 (insert (concat " " octave-continuation-string
))
739 (octave-reindent-then-newline-and-indent))))
741 (defun octave-indent-defun ()
742 "Properly indent the Octave function which contains point."
746 (message "Indenting function...")
747 (indent-region (point) (mark) nil
))
748 (message "Indenting function...done."))
752 (defun octave-next-code-line (&optional arg
)
753 "Move ARG lines of Octave code forward (backward if ARG is negative).
754 Skips past all empty and comment lines. Default for ARG is 1.
756 On success, return 0. Otherwise, go as far as possible and return -1."
758 (or arg
(setq arg
1))
761 (inc (if (> arg
0) 1 -
1)))
762 (while (and (/= arg
0) (= n
0))
763 (setq n
(forward-line inc
))
765 (looking-at "\\s-*\\($\\|\\s<\\)"))
766 (setq n
(forward-line inc
)))
767 (setq arg
(- arg inc
)))
770 (defun octave-previous-code-line (&optional arg
)
771 "Move ARG lines of Octave code backward (forward if ARG is negative).
772 Skips past all empty and comment lines. Default for ARG is 1.
774 On success, return 0. Otherwise, go as far as possible and return -1."
776 (or arg
(setq arg
1))
777 (octave-next-code-line (- arg
)))
779 (defun octave-beginning-of-line ()
780 "Move point to beginning of current Octave line.
781 If on an empty or comment line, go to the beginning of that line.
782 Otherwise, move backward to the beginning of the first Octave code line
783 which is not inside a continuation statement, i.e., which does not
784 follow a code line ending in `...' or `\\', or is inside an open
788 (if (not (looking-at "\\s-*\\($\\|\\s<\\)"))
789 (while (or (condition-case nil
795 (and (or (looking-at "\\s-*\\($\\|\\s<\\)")
797 (if (zerop (octave-previous-code-line))
798 (looking-at octave-continuation-regexp
))))
799 (zerop (forward-line -
1)))))))
801 (defun octave-end-of-line ()
802 "Move point to end of current Octave line.
803 If on an empty or comment line, go to the end of that line.
804 Otherwise, move forward to the end of the first Octave code line which
805 does not end in `...' or `\\' or is inside an open parenthesis list."
810 (looking-at "\\s-*\\($\\|\\s<\\)"))
812 (while (or (condition-case nil
820 (or (looking-at "\\s-*\\($\\|\\s<\\)")
821 (looking-at octave-continuation-regexp
)))
822 (zerop (forward-line 1)))))
825 (defun octave-scan-blocks (count depth
)
826 "Scan from point by COUNT Octave begin-end blocks.
827 Returns the character number of the position thus found.
829 If DEPTH is nonzero, block depth begins counting from that value.
830 Only places where the depth in blocks becomes zero are candidates for
831 stopping; COUNT such places are counted.
833 If the beginning or end of the buffer is reached and the depth is wrong,
834 an error is signaled."
835 (let ((min-depth (if (> depth
0) 0 depth
))
836 (inc (if (> count
0) 1 -
1)))
840 (while (or (octave-re-search-forward-kw
841 octave-block-begin-or-end-regexp inc
)
843 (error "Unbalanced block")))
844 (if (octave-not-in-string-or-comment-p)
848 (setq depth
(+ depth inc
)))
850 (setq depth
(- depth inc
))))
851 (if (< depth min-depth
)
852 (error "Containing expression ends prematurely"))
854 (throw 'foo nil
))))))
855 (setq count
(- count inc
)))
858 (defun octave-forward-block (&optional arg
)
859 "Move forward across one balanced Octave begin-end block.
860 With argument, do it that many times.
861 Negative arg -N means move backward across N blocks."
863 (or arg
(setq arg
1))
864 (goto-char (or (octave-scan-blocks arg
0) (buffer-end arg
))))
866 (defun octave-backward-block (&optional arg
)
867 "Move backward across one balanced Octave begin-end block.
868 With argument, do it that many times.
869 Negative arg -N means move forward across N blocks."
871 (or arg
(setq arg
1))
872 (octave-forward-block (- arg
)))
874 (defun octave-down-block (arg)
875 "Move forward down one begin-end block level of Octave code.
876 With argument, do this that many times.
877 A negative argument means move backward but still go down a level.
878 In Lisp programs, an argument is required."
880 (let ((inc (if (> arg
0) 1 -
1)))
882 (goto-char (or (octave-scan-blocks inc -
1)
884 (setq arg
(- arg inc
)))))
886 (defun octave-backward-up-block (arg)
887 "Move backward out of one begin-end block level of Octave code.
888 With argument, do this that many times.
889 A negative argument means move forward but still to a less deep spot.
890 In Lisp programs, an argument is required."
892 (octave-up-block (- arg
)))
894 (defun octave-up-block (arg)
895 "Move forward out of one begin-end block level of Octave code.
896 With argument, do this that many times.
897 A negative argument means move backward but still to a less deep spot.
898 In Lisp programs, an argument is required."
900 (let ((inc (if (> arg
0) 1 -
1)))
902 (goto-char (or (octave-scan-blocks inc
1)
904 (setq arg
(- arg inc
)))))
906 (defun octave-mark-block ()
907 "Put point at the beginning of this Octave block, mark at the end.
908 The block marked is the one that contains point or follows point."
911 (if (or (and (octave-in-block-p)
912 (skip-syntax-forward "w"))
915 (octave-down-block 1)
921 (octave-forward-block)
922 (exchange-point-and-mark))
924 (message "No block to mark found"))))
926 (defun octave-close-block ()
927 "Close the current Octave block on a separate line.
928 An error is signaled if no block to close is found."
934 (octave-backward-up-block 1)
935 (setq bb-keyword
(buffer-substring-no-properties
936 (match-beginning 1) (match-end 1))))
939 (looking-at "^\\s-*$"))
940 (indent-according-to-mode)
941 (octave-reindent-then-newline-and-indent))
942 (insert (car (reverse
944 octave-block-match-alist
))))
945 (octave-reindent-then-newline-and-indent)
947 (error (message "No block to close found")))))
949 (defun octave-blink-matching-block-open ()
950 "Blink the matching Octave begin block keyword.
951 If point is right after an Octave else or end type block keyword, move
952 cursor momentarily to the corresponding begin keyword.
953 Signal an error if the keywords are incompatible."
955 (let (bb-keyword bb-arg eb-keyword pos eol
)
956 (if (and (octave-not-in-string-or-comment-p)
959 (skip-syntax-backward "w")
960 (octave-looking-at-kw octave-block-else-or-end-regexp
)))
965 (buffer-substring-no-properties
966 (match-beginning 1) (match-end 1)))
967 (octave-backward-up-block 1))
970 (buffer-substring-no-properties
971 (match-beginning 2) (match-end 2)))
972 (octave-backward-block)))
973 (setq pos
(match-end 0)
975 (buffer-substring-no-properties
976 (match-beginning 0) pos
)
978 eol
(line-end-position)
983 (while (and (skip-syntax-forward "^<" eol
)
985 (not (forward-char 1))))
986 (skip-syntax-backward " ")
987 (buffer-substring-no-properties pos
(point)))))
988 (if (member eb-keyword
989 (cdr (assoc bb-keyword octave-block-match-alist
)))
991 (message "Matches `%s %s'" bb-keyword bb-arg
)
992 (if (pos-visible-in-window-p)
993 (sit-for blink-matching-delay
)))
994 (error "Block keywords `%s' and `%s' do not match"
995 bb-keyword eb-keyword
))))))
997 (defun octave-beginning-of-defun (&optional arg
)
998 "Move backward to the beginning of an Octave function.
999 With positive ARG, do it that many times. Negative argument -N means
1000 move forward to Nth following beginning of a function.
1001 Returns t unless search stops at the beginning or end of the buffer."
1002 (let* ((arg (or arg
1))
1003 (inc (if (> arg
0) 1 -
1))
1005 (case-fold-search nil
))
1007 (not (and (> arg
0) (looking-at "\\<function\\>")))
1008 (skip-syntax-forward "w"))
1009 (while (and (/= arg
0)
1011 (re-search-backward "\\<function\\>" inc
)))
1012 (if (octave-not-in-string-or-comment-p)
1013 (setq arg
(- arg inc
))))
1016 (and (< inc
0) (goto-char (match-beginning 0)))
1021 (defun octave-auto-fill ()
1022 "Perform auto-fill in Octave mode.
1023 Returns nil if no feasible place to break the line could be found, and t
1026 (if (or (null (setq fc
(current-fill-column)))
1029 (and auto-fill-inhibit-regexp
1030 (octave-looking-at-kw auto-fill-inhibit-regexp
))))
1031 nil
; Can't do anything
1032 (if (and (not (octave-in-comment-p))
1033 (> (current-column) fc
))
1034 (setq fc
(- fc
(+ (length octave-continuation-string
) 1))))
1035 (while (and (not give-up
) (> (current-column) fc
))
1036 (let* ((opoint (point))
1039 (move-to-column (+ fc
1))
1040 (skip-chars-backward "^ \t\n")
1041 ;; If we're at the beginning of the line, break after
1044 (re-search-forward "[ \t]" opoint t
))
1045 ;; If we're in a comment line, don't break after the
1048 (skip-syntax-backward " <")
1050 (re-search-forward "[ \t]" (line-end-position)
1052 ;; If we're not in a comment line and just ahead the
1053 ;; continuation string, don't break here.
1054 (if (and (not (octave-in-comment-p))
1058 octave-continuation-string
)
1061 (skip-chars-backward " \t")
1065 (not (or (bolp) (eolp))))
1066 (let ((prev-column (current-column)))
1068 (skip-chars-backward " \t")
1071 (octave-maybe-insert-continuation-string)
1072 (indent-new-comment-line t
))
1075 (octave-maybe-insert-continuation-string)
1076 (indent-new-comment-line t
)))
1077 (if (>= (current-column) prev-column
)
1082 (defun octave-fill-paragraph (&optional arg
)
1083 "Fill paragraph of Octave code, handling Octave comments."
1084 ;; FIXME: now that the default fill-paragraph takes care of similar issues,
1085 ;; this seems obsolete. --Stef
1088 (let ((end (progn (forward-paragraph) (point)))
1090 (forward-paragraph -
1)
1091 (skip-chars-forward " \t\n")
1094 (cfc (current-fill-column))
1095 (ind (octave-indent-calculate))
1099 (narrow-to-region beg end
)
1100 (if (listp ind
) (setq ind
(nth 1 ind
)))
1103 (octave-indent-line ind
)
1109 (looking-at "^\\s-*\\($\\|\\s<+\\)"))))
1111 (move-to-column cfc
)
1112 ;; First check whether we need to combine non-empty comment lines
1113 (if (and (< (current-column) cfc
)
1114 (octave-in-comment-p)
1115 (not (save-excursion
1117 (looking-at "^\\s-*\\s<+\\s-*$"))))
1118 ;; This is a nonempty comment line which does not extend
1119 ;; past the fill column. If it is followed by a nonempty
1120 ;; comment line with the same comment prefix, try to
1121 ;; combine them, and repeat this until either we reach the
1122 ;; fill-column or there is nothing more to combine.
1124 ;; Get the comment prefix
1127 (while (and (re-search-forward "\\s<+")
1128 (not (octave-in-comment-p))))
1129 (setq comment-prefix
(match-string 0)))
1130 ;; And keep combining ...
1131 (while (and (< (current-column) cfc
)
1143 (re-search-forward comment-prefix
)
1144 (delete-region (match-beginning 0) (match-end 0))
1146 (move-to-column cfc
))))
1147 ;; We might also try to combine continued code lines> Perhaps
1148 ;; some other time ...
1149 (skip-chars-forward "^ \t\n")
1150 (delete-horizontal-space)
1151 (if (or (< (current-column) cfc
)
1152 (and (= (current-column) cfc
) (eolp)))
1154 (if (not (eolp)) (insert " "))
1155 (or (octave-auto-fill)
1156 (forward-line 1)))))
1161 (defun octave-initialize-completions ()
1162 "Create an alist for Octave completions."
1163 (if octave-completion-alist
1165 (setq octave-completion-alist
1166 (append octave-reserved-words
1167 octave-text-functions
1168 octave-variables
))))
1170 (defun octave-completion-at-point-function ()
1171 "Find the text to complete and the corresponding table."
1172 (let* ((beg (save-excursion (backward-sexp 1) (point)))
1175 ;; Extend region past point, if applicable.
1176 (save-excursion (goto-char beg
) (forward-sexp 1)
1177 (setq end
(max end
(point)))))
1178 (list beg end octave-completion-alist
)))
1180 (defun octave-complete-symbol ()
1181 "Perform completion on Octave symbol preceding point.
1182 Compare that symbol against Octave's reserved words and builtin
1185 (apply 'completion-in-region
(octave-completion-at-point-function)))
1187 ;;; Electric characters && friends
1188 (defun octave-reindent-then-newline-and-indent ()
1189 "Reindent current Octave line, insert newline, and indent the new line.
1190 If Abbrev mode is on, expand abbrevs first."
1191 ;; FIXME: None of this is Octave-specific.
1193 (if abbrev-mode
(expand-abbrev))
1194 (if octave-blink-matching-block
1195 (octave-blink-matching-block-open))
1196 (reindent-then-newline-and-indent))
1198 (defun octave-electric-semi ()
1199 "Insert a semicolon in Octave mode.
1200 Maybe expand abbrevs and blink matching block open keywords.
1201 Reindent the line if `octave-auto-indent' is non-nil.
1202 Insert a newline if `octave-auto-newline' is non-nil."
1204 (if (not (octave-not-in-string-or-comment-p))
1206 (if abbrev-mode
(expand-abbrev))
1207 (if octave-blink-matching-block
1208 (octave-blink-matching-block-open))
1209 (if octave-auto-indent
1210 (indent-according-to-mode))
1212 (if octave-auto-newline
1213 (newline-and-indent))))
1215 (defun octave-electric-space ()
1216 "Insert a space in Octave mode.
1217 Maybe expand abbrevs and blink matching block open keywords.
1218 Reindent the line if `octave-auto-indent' is non-nil."
1220 (setq last-command-event ?
)
1221 (if (and octave-auto-indent
1222 (not (octave-not-in-string-or-comment-p)))
1224 (indent-according-to-mode)
1225 (self-insert-command 1))
1226 (if abbrev-mode
(expand-abbrev))
1227 (if octave-blink-matching-block
1228 (octave-blink-matching-block-open))
1229 (if (and octave-auto-indent
1231 (skip-syntax-backward " ")
1233 (indent-according-to-mode))
1234 (self-insert-command 1)))
1236 (defun octave-abbrev-start ()
1237 "Start entering an Octave abbreviation.
1238 If Abbrev mode is turned on, typing ` (grave accent) followed by ? or
1239 \\[help-command] lists all Octave abbrevs. Any other key combination is
1241 Note that all Octave mode abbrevs start with a grave accent."
1243 (if (not abbrev-mode
)
1244 (self-insert-command 1)
1246 (insert last-command-event
)
1247 (if (if (featurep 'xemacs
)
1248 (or (eq (event-to-character (setq c
(next-event))) ??
)
1249 (eq (event-to-character c
) help-char
))
1250 (or (eq (setq c
(read-event)) ??
)
1252 (let ((abbrev-table-name-list '(octave-abbrev-table)))
1254 (setq unread-command-events
(list c
))))))
1256 (define-skeleton octave-insert-defun
1257 "Insert an Octave function skeleton.
1258 Prompt for the function's name, arguments and return values (to be
1259 entered without parens)."
1260 (let* ((defname (substring (buffer-name) 0 -
2))
1261 (name (read-string (format "Function name (default %s): " defname
)
1263 (args (read-string "Arguments: "))
1264 (vals (read-string "Return values: ")))
1267 ((string-equal vals
"") vals
)
1268 ((string-match "[ ,]" vals
) (concat "[" vals
"] = "))
1269 (t (concat vals
" = ")))
1272 \n "function " > str
\n \n
1273 octave-block-comment-start
"usage: " str
\n
1274 octave-block-comment-start
\n octave-block-comment-start
1278 ;;; Communication with the inferior Octave process
1279 (defun octave-kill-process ()
1280 "Kill inferior Octave process and its buffer."
1282 (if inferior-octave-process
1284 (process-send-string inferior-octave-process
"quit;\n")
1285 (accept-process-output inferior-octave-process
)))
1286 (if inferior-octave-buffer
1287 (kill-buffer inferior-octave-buffer
)))
1289 (defun octave-show-process-buffer ()
1290 "Make sure that `inferior-octave-buffer' is displayed."
1292 (if (get-buffer inferior-octave-buffer
)
1293 (display-buffer inferior-octave-buffer
)
1294 (message "No buffer named %s" inferior-octave-buffer
)))
1296 (defun octave-hide-process-buffer ()
1297 "Delete all windows that display `inferior-octave-buffer'."
1299 (if (get-buffer inferior-octave-buffer
)
1300 (delete-windows-on inferior-octave-buffer
)
1301 (message "No buffer named %s" inferior-octave-buffer
)))
1303 (defun octave-send-region (beg end
)
1304 "Send current region to the inferior Octave process."
1307 (let ((proc inferior-octave-process
)
1308 (string (buffer-substring-no-properties beg end
))
1310 (with-current-buffer inferior-octave-buffer
1311 (setq inferior-octave-output-list nil
)
1312 (while (not (string-equal string
""))
1313 (if (string-match "\n" string
)
1314 (setq line
(substring string
0 (match-beginning 0))
1315 string
(substring string
(match-end 0)))
1316 (setq line string string
""))
1317 (setq inferior-octave-receive-in-progress t
)
1318 (inferior-octave-send-list-and-digest (list (concat line
"\n")))
1319 (while inferior-octave-receive-in-progress
1320 (accept-process-output proc
))
1321 (insert-before-markers
1322 (mapconcat 'identity
1324 (if octave-send-echo-input
(list line
) (list ""))
1325 (mapcar 'inferior-octave-strip-ctrl-g
1326 inferior-octave-output-list
)
1327 (list inferior-octave-output-string
))
1329 (if octave-send-show-buffer
1330 (display-buffer inferior-octave-buffer
)))
1332 (defun octave-send-block ()
1333 "Send current Octave block to the inferior Octave process."
1337 (octave-send-region (point) (mark))))
1339 (defun octave-send-defun ()
1340 "Send current Octave function to the inferior Octave process."
1344 (octave-send-region (point) (mark))))
1346 (defun octave-send-line (&optional arg
)
1347 "Send current Octave code line to the inferior Octave process.
1348 With positive prefix ARG, send that many lines.
1349 If `octave-send-line-auto-forward' is non-nil, go to the next unsent
1352 (or arg
(setq arg
1))
1357 (octave-next-code-line (- arg
1))
1360 (if octave-send-line-auto-forward
1361 (octave-next-code-line 1))
1362 (octave-send-region beg end
))))
1364 (defun octave-eval-print-last-sexp ()
1365 "Evaluate Octave sexp before point and print value into current buffer."
1368 (let ((standard-output (current-buffer))
1369 (print-escape-newlines nil
)
1375 (inferior-octave-send-list-and-digest
1376 (list (concat (buffer-substring-no-properties (point) opoint
)
1378 (mapconcat 'identity inferior-octave-output-list
"\n")))
1382 (defun octave-submit-bug-report ()
1383 "Submit a bug report on the Emacs Octave package via mail."
1387 (y-or-n-p "Do you want to submit a bug report? ")
1388 (reporter-submit-bug-report
1389 octave-maintainer-address
1390 (concat "Emacs version " emacs-version
)
1393 'octave-auto-newline
1394 'octave-blink-matching-block
1395 'octave-block-offset
1396 'octave-comment-char
1397 'octave-continuation-offset
1398 'octave-continuation-string
1399 'octave-send-echo-input
1400 'octave-send-line-auto-forward
1401 'octave-send-show-buffer
))))
1405 (provide 'octave-mod
)
1407 ;; arch-tag: 05f1ce09-be87-4c00-803e-4919ffa26c23
1408 ;;; octave-mod.el ends here