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