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