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 |
|
299 "*Non-nil means automatically indent line after a semicolon or space.") |
|
300 |
2617
|
301 (defvar octave-auto-newline nil |
|
302 "*Non-nil means automatically newline after a semicolon in Octave mode.") |
|
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 |
|
586 ;;; Comments |
|
587 (defun octave-comment-region (beg end &optional arg) |
|
588 "Comment or uncomment each line in the region as Octave code. |
|
589 See `comment-region'." |
|
590 (interactive "r\nP") |
|
591 (let ((comment-start (char-to-string octave-comment-char))) |
|
592 (comment-region beg end arg))) |
|
593 |
|
594 (defun octave-uncomment-region (beg end &optional arg) |
|
595 "Uncomment each line in the region as Octave code." |
|
596 (interactive "r\nP") |
|
597 (or arg (setq arg 1)) |
|
598 (octave-comment-region beg end (- arg))) |
|
599 |
|
600 |
|
601 ;;; Indentation |
|
602 (defun calculate-octave-indent () |
|
603 "Return appropriate indentation for current line as Octave code. |
|
604 Returns an integer (the column to indent to) unless the line is a |
|
605 comment line with fixed goal golumn. In that case, returns a list whose |
|
606 car is the column to indent to, and whose cdr is the current indentation |
|
607 level." |
|
608 (let ((is-continuation-line |
|
609 (save-excursion |
|
610 (if (zerop (octave-previous-code-line)) |
|
611 (looking-at octave-continuation-regexp)))) |
|
612 (icol 0)) |
|
613 (save-excursion |
|
614 (beginning-of-line) |
|
615 ;; If we can move backward out one level of parentheses, take 1 |
|
616 ;; plus the indentation of that parenthesis. Otherwise, go back |
|
617 ;; to the beginning of the previous code line, and compute the |
|
618 ;; offset this line gives. |
|
619 (if (condition-case nil |
|
620 (progn |
|
621 (up-list -1) |
|
622 t) |
|
623 (error nil)) |
|
624 (setq icol (+ 1 (current-column))) |
|
625 (if (zerop (octave-previous-code-line)) |
|
626 (progn |
|
627 (octave-beginning-of-line) |
|
628 (back-to-indentation) |
|
629 (setq icol (current-column)) |
|
630 (let ((bot (point)) |
|
631 (eol (octave-point 'eol))) |
|
632 (while (< (point) eol) |
|
633 (if (octave-not-in-string-or-comment-p) |
|
634 (cond |
2788
|
635 ((looking-at "\\<switch\\>") |
|
636 (setq icol (+ icol (* 2 octave-block-offset)))) |
2617
|
637 ((looking-at octave-block-begin-regexp) |
|
638 (setq icol (+ icol octave-block-offset))) |
|
639 ((looking-at octave-block-else-regexp) |
|
640 (if (= bot (point)) |
|
641 (setq icol (+ icol octave-block-offset)))) |
|
642 ((looking-at octave-block-end-regexp) |
|
643 (if (not (= bot (point))) |
2788
|
644 (setq icol (- icol |
|
645 (octave-block-end-offset))))))) |
2617
|
646 (forward-char))) |
|
647 (if is-continuation-line |
|
648 (setq icol (+ icol octave-continuation-offset))))))) |
|
649 (save-excursion |
|
650 (back-to-indentation) |
|
651 (cond |
2788
|
652 ((and (looking-at octave-block-else-regexp) |
2617
|
653 (octave-not-in-string-or-comment-p)) |
|
654 (setq icol (- icol octave-block-offset))) |
2788
|
655 ((and (looking-at octave-block-end-regexp) |
|
656 (octave-not-in-string-or-comment-p)) |
|
657 (setq icol (- icol (octave-block-end-offset)))) |
3081
|
658 ((or (looking-at "\\s<\\s<\\s<\\S<") |
|
659 (octave-before-magic-comment-p)) |
2617
|
660 (setq icol (list 0 icol))) |
|
661 ((looking-at "\\s<\\S<") |
|
662 (setq icol (list comment-column icol))))) |
|
663 icol)) |
|
664 |
2788
|
665 (defun octave-block-end-offset () |
|
666 (save-excursion |
|
667 (octave-backward-up-block 1) |
|
668 (* octave-block-offset |
|
669 (if (string-match (match-string 0) "switch") 2 1)))) |
|
670 |
3081
|
671 (defun octave-before-magic-comment-p () |
|
672 (save-excursion |
|
673 (beginning-of-line) |
|
674 (and (bobp) (looking-at "\\s-*#!")))) |
|
675 |
2617
|
676 (defun octave-comment-indent () |
3081
|
677 (if (or (looking-at "\\s<\\s<\\s<") |
|
678 (octave-before-magic-comment-p)) |
2617
|
679 0 |
|
680 (if (looking-at "\\s<\\s<") |
|
681 (calculate-octave-indent) |
|
682 (skip-syntax-backward " ") |
2788
|
683 (max (if (bolp) 0 (+ 1 (current-column))) |
2617
|
684 comment-column)))) |
|
685 |
|
686 (defun octave-indent-for-comment () |
|
687 "Maybe insert and indent an Octave comment. |
|
688 If there is no comment already on this line, create a code-level comment |
|
689 (started by two comment characters) if the line is empty, or an in-line |
|
690 comment (started by one comment character) otherwise. |
|
691 Point is left after the start of the comment which is properly aligned." |
|
692 (interactive) |
|
693 (indent-for-comment) |
|
694 (indent-according-to-mode)) |
|
695 |
|
696 (defun octave-indent-line (&optional arg) |
|
697 "Indent current line as Octave code. |
|
698 With optional ARG, use this as offset unless this line is a comment with |
|
699 fixed goal column." |
|
700 (interactive) |
|
701 (or arg (setq arg 0)) |
|
702 (let ((icol (calculate-octave-indent)) |
|
703 (relpos (- (current-column) (current-indentation)))) |
|
704 (if (listp icol) |
|
705 (setq icol (car icol)) |
|
706 (setq icol (+ icol arg))) |
|
707 (if (< icol 0) |
|
708 (error "Unmatched end keyword") |
|
709 (indent-line-to icol) |
|
710 (if (> relpos 0) |
|
711 (move-to-column (+ icol relpos)))))) |
|
712 |
|
713 (defun octave-indent-new-comment-line () |
|
714 "Break Octave line at point, continuing comment if within one. |
|
715 If within code, insert `octave-continuation-string' before breaking the |
|
716 line. If within a string, signal an error. |
|
717 The new line is properly indented." |
|
718 (interactive) |
|
719 (delete-horizontal-space) |
|
720 (cond |
|
721 ((octave-in-comment-p) |
|
722 (indent-new-comment-line)) |
|
723 ((octave-in-string-p) |
|
724 (error "Cannot split a code line inside a string")) |
|
725 (t |
|
726 (insert (concat " " octave-continuation-string)) |
|
727 (octave-reindent-then-newline-and-indent)))) |
|
728 |
|
729 (defun octave-indent-defun () |
|
730 "Properly indents the Octave function which contains point." |
|
731 (interactive) |
|
732 (save-excursion |
|
733 (octave-mark-defun) |
|
734 (message "Indenting function...") |
|
735 (indent-region (point) (mark) nil)) |
|
736 (message "Indenting function...done.")) |
|
737 |
|
738 |
|
739 ;;; Motion |
|
740 (defun octave-next-code-line (&optional arg) |
|
741 "Move ARG lines of Octave code forward (backward if ARG is negative). |
|
742 Skips past all empty and comment lines. Default for ARG is 1. |
|
743 |
|
744 On success, return 0. Otherwise, go as far as possible and return -1." |
|
745 (interactive "p") |
|
746 (or arg (setq arg 1)) |
|
747 (beginning-of-line) |
|
748 (let ((n 0) |
|
749 (inc (if (> arg 0) 1 -1))) |
|
750 (while (and (/= arg 0) (= n 0)) |
|
751 (setq n (forward-line inc)) |
|
752 (while (and (= n 0) |
|
753 (looking-at "\\s-*\\($\\|\\s<\\)")) |
|
754 (setq n (forward-line inc))) |
|
755 (setq arg (- arg inc))) |
|
756 n)) |
|
757 |
|
758 (defun octave-previous-code-line (&optional arg) |
|
759 "Move ARG lines of Octave code backward (forward if ARG is negative). |
|
760 Skips past all empty and comment lines. Default for ARG is 1. |
|
761 |
|
762 On success, return 0. Otherwise, go as far as possible and return -1." |
|
763 (interactive "p") |
|
764 (or arg (setq arg 1)) |
|
765 (octave-next-code-line (- arg))) |
|
766 |
|
767 (defun octave-beginning-of-line () |
|
768 "Move point to beginning of current Octave line. |
|
769 If on an empty or comment line, go to the beginning of that line. |
|
770 Otherwise, move backward to the beginning of the first Octave code line |
|
771 which is not inside a continuation statement, i.e., which does not |
|
772 follow a code line ending in `...' or `\\', or is inside an open |
|
773 parenthesis list." |
|
774 (interactive) |
|
775 (beginning-of-line) |
|
776 (if (not (looking-at "\\s-*\\($\\|\\s<\\)")) |
|
777 (while (or (condition-case nil |
|
778 (progn |
|
779 (up-list -1) |
|
780 (beginning-of-line) |
|
781 t) |
|
782 (error nil)) |
|
783 (and (or (looking-at "\\s-*\\($\\|\\s<\\)") |
|
784 (save-excursion |
|
785 (if (zerop (octave-previous-code-line)) |
|
786 (looking-at octave-continuation-regexp)))) |
|
787 (zerop (forward-line -1))))))) |
|
788 |
|
789 (defun octave-end-of-line () |
|
790 "Move point to end of current Octave line. |
|
791 If on an empty or comment line, go to the end of that line. |
|
792 Otherwise, move forward to the end of the first Octave code line which |
|
793 does not end in `...' or `\\' or is inside an open parenthesis list." |
|
794 (interactive) |
|
795 (end-of-line) |
|
796 (if (save-excursion |
|
797 (beginning-of-line) |
|
798 (looking-at "\\s-*\\($\\|\\s<\\)")) |
|
799 () |
|
800 (while (or (condition-case nil |
|
801 (progn |
|
802 (up-list 1) |
|
803 (end-of-line) |
|
804 t) |
|
805 (error nil)) |
|
806 (and (save-excursion |
|
807 (beginning-of-line) |
|
808 (or (looking-at "\\s-*\\($\\|\\s<\\)") |
|
809 (looking-at octave-continuation-regexp))) |
|
810 (zerop (forward-line 1))))) |
|
811 (end-of-line))) |
|
812 |
|
813 (defun octave-scan-blocks (from count depth) |
|
814 "Scan from character number FROM by COUNT Octave begin-end blocks. |
|
815 Returns the character number of the position thus found. |
|
816 |
|
817 If DEPTH is nonzero, block depth begins counting from that value. |
|
818 Only places where the depth in blocks becomes zero are candidates for |
|
819 stopping; COUNT such places are counted. |
|
820 |
|
821 If the beginning or end of the buffer is reached and the depth is wrong, |
|
822 an error is signaled." |
|
823 (let ((min-depth (if (> depth 0) 0 depth)) |
|
824 (inc (if (> count 0) 1 -1))) |
|
825 (save-excursion |
|
826 (while (/= count 0) |
|
827 (catch 'foo |
|
828 (while (or (re-search-forward |
|
829 octave-block-begin-or-end-regexp nil 'move inc) |
|
830 (if (/= depth 0) |
|
831 (error "Unbalanced block"))) |
|
832 (if (octave-not-in-string-or-comment-p) |
|
833 (progn |
|
834 (cond |
|
835 ((match-end 1) |
|
836 (setq depth (+ depth inc))) |
|
837 ((match-end 2) |
|
838 (setq depth (- depth inc)))) |
|
839 (if (< depth min-depth) |
|
840 (error "Containing expression ends prematurely")) |
|
841 (if (= depth 0) |
|
842 (throw 'foo nil)))))) |
|
843 (setq count (- count inc))) |
|
844 (point)))) |
|
845 |
|
846 (defun octave-forward-block (&optional arg) |
|
847 "Move forward across one balanced Octave begin-end block. |
|
848 With argument, do it that many times. |
|
849 Negative arg -N means move backward across N blocks." |
|
850 (interactive "p") |
|
851 (or arg (setq arg 1)) |
|
852 (goto-char (or (octave-scan-blocks (point) arg 0) (buffer-end arg)))) |
|
853 |
|
854 (defun octave-backward-block (&optional arg) |
|
855 "Move backward across one balanced Octave begin-end block. |
|
856 With argument, do it that many times. |
|
857 Negative arg -N means move forward across N blocks." |
|
858 (interactive "p") |
|
859 (or arg (setq arg 1)) |
|
860 (octave-forward-block (- arg))) |
|
861 |
|
862 (defun octave-down-block (arg) |
|
863 "Move forward down one begin-end block level of Octave code. |
|
864 With argument, do this that many times. |
|
865 A negative argument means move backward but still go down a level. |
|
866 In Lisp programs, an argument is required." |
|
867 (interactive "p") |
|
868 (let ((inc (if (> arg 0) 1 -1))) |
|
869 (while (/= arg 0) |
|
870 (goto-char (or (octave-scan-blocks (point) inc -1) |
|
871 (buffer-end arg))) |
|
872 (setq arg (- arg inc))))) |
|
873 |
|
874 (defun octave-backward-up-block (arg) |
|
875 "Move backward out of one begin-end block level of Octave code. |
|
876 With argument, do this that many times. |
|
877 A negative argument means move forward but still to a less deep spot. |
|
878 In Lisp programs, an argument is required." |
|
879 (interactive "p") |
|
880 (octave-up-block (- arg))) |
|
881 |
|
882 (defun octave-up-block (arg) |
|
883 "Move forward out of one begin-end block level of Octave code. |
|
884 With argument, do this that many times. |
|
885 A negative argument means move backward but still to a less deep spot. |
|
886 In Lisp programs, an argument is required." |
|
887 (interactive "p") |
|
888 (let ((inc (if (> arg 0) 1 -1))) |
|
889 (while (/= arg 0) |
|
890 (goto-char (or (octave-scan-blocks (point) inc 1) |
|
891 (buffer-end arg))) |
|
892 (setq arg (- arg inc))))) |
|
893 |
|
894 (defun octave-mark-block () |
|
895 "Put point at the beginning of this Octave block, mark at the end. |
|
896 The block marked is the one that contains point or follows point." |
|
897 (interactive) |
|
898 (let ((pos (point))) |
|
899 (if (or (and (octave-in-block-p) |
|
900 (skip-syntax-forward "w")) |
|
901 (condition-case nil |
|
902 (progn |
|
903 (octave-down-block 1) |
|
904 (octave-in-block-p)) |
|
905 (error nil))) |
|
906 (progn |
|
907 (octave-up-block -1) |
|
908 (push-mark (point)) |
|
909 (octave-forward-block) |
|
910 (exchange-point-and-mark)) |
|
911 (goto-char pos) |
|
912 (message "No block to mark found")))) |
|
913 |
|
914 (defun octave-close-block () |
|
915 "Close the current Octave block on a separate line. |
|
916 An error is signaled if no block to close is found." |
|
917 (interactive) |
|
918 (let (bb-keyword) |
|
919 (condition-case nil |
|
920 (progn |
|
921 (save-excursion |
|
922 (octave-backward-up-block 1) |
|
923 (setq bb-keyword (buffer-substring-no-properties |
|
924 (match-beginning 1) (match-end 1)))) |
|
925 (if (save-excursion |
|
926 (beginning-of-line) |
|
927 (looking-at "^\\s-*$")) |
|
928 (indent-according-to-mode) |
|
929 (octave-reindent-then-newline-and-indent)) |
|
930 (insert (car (reverse |
|
931 (assoc bb-keyword |
|
932 octave-block-match-alist)))) |
|
933 (octave-reindent-then-newline-and-indent) |
|
934 t) |
|
935 (error (message "No block to close found"))))) |
|
936 |
|
937 (defun octave-blink-matching-block-open () |
|
938 "Blink the matching Octave begin block keyword. |
|
939 If point is right after an Octave else or end type block keyword, move |
|
940 cursor momentarily to the corresponding begin keyword. |
|
941 Signal an error if the keywords are incompatible." |
|
942 (interactive) |
|
943 (let (bb-keyword bb-arg eb-keyword pos eol) |
|
944 (if (and (octave-not-in-string-or-comment-p) |
|
945 (looking-at "\\>") |
|
946 (save-excursion |
|
947 (skip-syntax-backward "w") |
|
948 (looking-at octave-block-else-or-end-regexp))) |
|
949 (save-excursion |
|
950 (cond |
|
951 ((match-end 1) |
|
952 (setq eb-keyword |
|
953 (buffer-substring-no-properties |
|
954 (match-beginning 1) (match-end 1))) |
|
955 (octave-backward-up-block 1)) |
|
956 ((match-end 2) |
|
957 (setq eb-keyword |
|
958 (buffer-substring-no-properties |
|
959 (match-beginning 2) (match-end 2))) |
|
960 (octave-backward-block))) |
|
961 (setq pos (match-end 0) |
|
962 bb-keyword |
|
963 (buffer-substring-no-properties |
|
964 (match-beginning 0) pos) |
|
965 pos (+ pos 1) |
|
966 eol (octave-point 'eol) |
|
967 bb-arg |
|
968 (save-excursion |
|
969 (save-restriction |
|
970 (goto-char pos) |
|
971 (while (and (skip-syntax-forward "^<" eol) |
|
972 (octave-in-string-p) |
|
973 (not (forward-char 1)))) |
|
974 (skip-syntax-backward " ") |
|
975 (buffer-substring-no-properties pos (point))))) |
|
976 (if (member eb-keyword |
|
977 (cdr (assoc bb-keyword octave-block-match-alist))) |
|
978 (progn |
|
979 (message "Matches `%s %s'" bb-keyword bb-arg) |
|
980 (if (pos-visible-in-window-p) |
|
981 (sit-for blink-matching-delay))) |
|
982 (error "Block keywords `%s' and `%s' do not match" |
|
983 bb-keyword eb-keyword)))))) |
|
984 |
|
985 (defun octave-beginning-of-defun (&optional arg) |
|
986 "Move backward to the beginning of an Octave function. |
|
987 With positive ARG, do it that many times. Negative argument -N means |
|
988 move forward to Nth following beginning of a function. |
|
989 Returns t unless search stops at the beginning or end of the buffer." |
|
990 (interactive "p") |
|
991 (let* ((arg (or arg 1)) |
|
992 (inc (if (> arg 0) 1 -1)) |
|
993 (found)) |
|
994 (and (not (eobp)) |
|
995 (not (and (> arg 0) (looking-at "\\<function\\>"))) |
|
996 (skip-syntax-forward "w")) |
|
997 (while (and (/= arg 0) |
|
998 (setq found |
|
999 (re-search-backward "\\<function\\>" nil 'move inc))) |
|
1000 (if (octave-not-in-string-or-comment-p) |
|
1001 (setq arg (- arg inc)))) |
|
1002 (if found |
|
1003 (progn |
|
1004 (and (< inc 0) (goto-char (match-beginning 0))) |
|
1005 t)))) |
|
1006 |
|
1007 (defun octave-end-of-defun (&optional arg) |
|
1008 "Move forward to the end of an Octave function. |
|
1009 With positive ARG, do it that many times. Negative argument -N means |
|
1010 move back to Nth preceding end of a function. |
|
1011 |
|
1012 An end of a function occurs right after the end keyword matching the |
|
1013 `function' keyword that starts the function." |
|
1014 (interactive "p") |
|
1015 (or arg (setq arg 1)) |
|
1016 (and (< arg 0) (skip-syntax-backward "w")) |
|
1017 (and (> arg 0) (skip-syntax-forward "w")) |
|
1018 (if (octave-in-defun-p) |
|
1019 (setq arg (- arg 1))) |
|
1020 (if (= arg 0) (setq arg -1)) |
|
1021 (if (octave-beginning-of-defun (- arg)) |
|
1022 (octave-forward-block))) |
|
1023 |
|
1024 (defun octave-mark-defun () |
|
1025 "Put point at the beginning of this Octave function, mark at its end. |
|
1026 The function marked is the one containing point or following point." |
|
1027 (interactive) |
|
1028 (let ((pos (point))) |
|
1029 (if (or (octave-in-defun-p) |
|
1030 (and (octave-beginning-of-defun -1) |
|
1031 (octave-in-defun-p))) |
|
1032 (progn |
|
1033 (skip-syntax-forward "w") |
|
1034 (octave-beginning-of-defun) |
|
1035 (push-mark (point)) |
|
1036 (octave-end-of-defun) |
|
1037 (exchange-point-and-mark)) |
|
1038 (goto-char pos) |
|
1039 (message "No function to mark found")))) |
|
1040 |
|
1041 |
|
1042 ;;; Filling |
|
1043 (defun octave-auto-fill () |
|
1044 "Perform auto-fill in Octave mode." |
|
1045 (if (> (current-column) (current-fill-column)) |
|
1046 (if (octave-in-comment-p) |
|
1047 (do-auto-fill) |
|
1048 (if (> (current-column) (current-fill-column)) |
|
1049 (let ((fill-column (- (current-fill-column) |
|
1050 (length octave-continuation-string)))) |
|
1051 (do-auto-fill) |
|
1052 (save-excursion |
|
1053 (forward-line -1) |
|
1054 (end-of-line) |
|
1055 (insert (concat " " octave-continuation-string))) |
|
1056 (indent-according-to-mode)))))) |
|
1057 |
|
1058 (defun octave-fill-paragraph (&optional arg) |
|
1059 "Fill paragraph of Octave code, handling Octave comments." |
|
1060 (interactive "P") |
|
1061 (save-excursion |
|
1062 (let ((end (progn (forward-paragraph) (point))) |
|
1063 (beg (progn |
|
1064 (forward-paragraph -1) |
|
1065 (skip-chars-forward " \t\n") |
|
1066 (beginning-of-line) |
|
1067 (point))) |
|
1068 (cfc (current-fill-column)) |
|
1069 (ind (calculate-octave-indent)) |
|
1070 comment-prefix) |
|
1071 (save-restriction |
|
1072 (goto-char beg) |
|
1073 (narrow-to-region beg end) |
|
1074 (if (listp ind) (setq ind (nth 1 ind))) |
|
1075 (while (not (eobp)) |
|
1076 (condition-case nil |
|
1077 (octave-indent-line ind) |
|
1078 (error nil)) |
|
1079 (if (and (> ind 0) |
|
1080 (not |
|
1081 (save-excursion |
|
1082 (beginning-of-line) |
|
1083 (looking-at "^\\s-*\\($\\|\\s<+\\)")))) |
|
1084 (setq ind 0)) |
|
1085 (move-to-column cfc) |
|
1086 ;; First check whether we need to combine non-empty comment lines |
|
1087 (if (and (< (current-column) cfc) |
|
1088 (octave-in-comment-p) |
|
1089 (not (save-excursion |
|
1090 (beginning-of-line) |
|
1091 (looking-at "^\\s-*\\s<+\\s-*$")))) |
|
1092 ;; This is a nonempty comment line which does not extend |
|
1093 ;; past the fill column. If it is followed by an nonempty |
|
1094 ;; comment line with the same comment prefix, try to |
|
1095 ;; combine them, and repeat this until either we reach the |
|
1096 ;; fill-column or there is nothing more to combine. |
|
1097 (progn |
|
1098 ;; Get the comment prefix |
|
1099 (save-excursion |
|
1100 (beginning-of-line) |
|
1101 (while (and (re-search-forward "\\s<+") |
|
1102 (not (octave-in-comment-p)))) |
|
1103 (setq comment-prefix (match-string 0))) |
|
1104 ;; And keep combining ... |
|
1105 (while (and (< (current-column) cfc) |
|
1106 (save-excursion |
|
1107 (forward-line 1) |
|
1108 (and (looking-at |
|
1109 (concat "^\\s-*" |
|
1110 comment-prefix |
|
1111 "\\S<")) |
|
1112 (not (looking-at |
|
1113 (concat "^\\s-*" |
|
1114 comment-prefix |
|
1115 "\\s-*$")))))) |
|
1116 (delete-char 1) |
|
1117 (re-search-forward comment-prefix) |
|
1118 (delete-region (match-beginning 0) (match-end 0)) |
|
1119 (fixup-whitespace) |
|
1120 (move-to-column cfc)))) |
|
1121 (skip-chars-forward "^ \t\n") |
|
1122 (delete-horizontal-space) |
|
1123 (if (or (< (current-column) cfc) |
|
1124 (and (= (current-column) cfc) (eolp))) |
|
1125 (forward-line 1) |
|
1126 (if (not (eolp)) (insert " ")) |
|
1127 (octave-auto-fill)))) |
|
1128 t))) |
|
1129 |
|
1130 |
|
1131 ;;; Completions |
|
1132 (defun octave-initialize-completions () |
|
1133 "Create an alist for Octave completions." |
|
1134 (if octave-completion-alist |
|
1135 () |
|
1136 (setq octave-completion-alist |
|
1137 (mapcar '(lambda (var) (cons var var)) |
|
1138 (append octave-reserved-words |
|
1139 octave-text-functions |
|
1140 octave-variables))))) |
|
1141 |
|
1142 (defun octave-complete-symbol () |
|
1143 "Perform completion on Octave symbol preceding point. |
|
1144 Compare that symbol against Octave's reserved words and builtin |
|
1145 variables." |
|
1146 ;; This code taken from lisp-complete-symbol |
|
1147 (interactive) |
|
1148 (let* ((end (point)) |
|
1149 (beg (save-excursion (backward-sexp 1) (point))) |
|
1150 (string (buffer-substring-no-properties beg end)) |
|
1151 (completion (try-completion string octave-completion-alist))) |
|
1152 (cond ((eq completion t)) ; ??? |
|
1153 ((null completion) |
|
1154 (message "Can't find completion for \"%s\"" string) |
|
1155 (ding)) |
|
1156 ((not (string= string completion)) |
|
1157 (delete-region beg end) |
|
1158 (insert completion)) |
|
1159 (t |
|
1160 (let ((list (all-completions string octave-completion-alist)) |
|
1161 (conf (current-window-configuration))) |
|
1162 ;; Taken from comint.el |
|
1163 (message "Making completion list...") |
|
1164 (with-output-to-temp-buffer "*Completions*" |
|
1165 (display-completion-list list)) |
|
1166 (message "Hit space to flush") |
|
1167 (let (key first) |
|
1168 (if (save-excursion |
|
1169 (set-buffer (get-buffer "*Completions*")) |
|
1170 (setq key (read-key-sequence nil) |
|
1171 first (aref key 0)) |
|
1172 (and (consp first) (consp (event-start first)) |
|
1173 (eq (window-buffer (posn-window (event-start |
|
1174 first))) |
|
1175 (get-buffer "*Completions*")) |
|
1176 (eq (key-binding key) 'mouse-choose-completion))) |
|
1177 (progn |
|
1178 (mouse-choose-completion first) |
|
1179 (set-window-configuration conf)) |
|
1180 (if (eq first ?\ ) |
|
1181 (set-window-configuration conf) |
|
1182 (setq unread-command-events |
|
1183 (listify-key-sequence key)))))))))) |
|
1184 |
|
1185 |
|
1186 ;;; Electric characters && friends |
|
1187 (defun octave-reindent-then-newline-and-indent () |
|
1188 "Reindent current Octave line, insert newline, and indent the new line. |
|
1189 If Abbrev mode is on, expand abbrevs first." |
|
1190 (interactive) |
|
1191 (if abbrev-mode (expand-abbrev)) |
|
1192 (if octave-blink-matching-block |
|
1193 (octave-blink-matching-block-open)) |
|
1194 (save-excursion |
|
1195 (delete-region (point) (progn (skip-chars-backward " \t") (point))) |
|
1196 (indent-according-to-mode)) |
|
1197 (insert "\n") |
|
1198 (indent-according-to-mode)) |
|
1199 |
|
1200 (defun octave-electric-semi () |
|
1201 "Insert a semicolon in Octave mode. |
|
1202 Always reindent the line. Insert a newline if `octave-auto-newline' is |
|
1203 non-nil." |
|
1204 (interactive) |
|
1205 (if (not (octave-not-in-string-or-comment-p)) |
|
1206 (insert ";") |
|
1207 (if abbrev-mode (expand-abbrev)) |
|
1208 (if octave-blink-matching-block |
|
1209 (octave-blink-matching-block-open)) |
3069
|
1210 (if octave-auto-indent |
|
1211 (indent-according-to-mode)) |
2617
|
1212 (insert ";") |
|
1213 (if octave-auto-newline |
|
1214 (newline-and-indent)))) |
|
1215 |
|
1216 (defun octave-electric-space () |
|
1217 "Insert a space in Octave mode. |
|
1218 Maybe expand abbrevs and blink matching block open keywords." |
|
1219 (interactive) |
|
1220 (setq last-command-char ? ) |
|
1221 (if (not (octave-not-in-string-or-comment-p)) |
|
1222 (progn |
|
1223 (indent-according-to-mode) |
|
1224 (self-insert-command 1)) |
|
1225 (if abbrev-mode (expand-abbrev)) |
|
1226 (if octave-blink-matching-block |
|
1227 (octave-blink-matching-block-open)) |
3069
|
1228 (if (and octave-auto-indent |
|
1229 (save-excursion |
|
1230 (skip-syntax-backward " ") |
|
1231 (not (bolp)))) |
2617
|
1232 (indent-according-to-mode)) |
|
1233 (self-insert-command 1))) |
|
1234 |
|
1235 (defun octave-abbrev-start () |
|
1236 "Start entering an Octave abbreviation. |
|
1237 If Abbrev mode is turned on, typing ` (grave accent) followed by ? or |
|
1238 \\[help-command] lists all Octave abbrevs. Any other key combination is |
|
1239 executed normally. |
|
1240 Note that all Octave mode abbrevs start with a grave accent." |
|
1241 (interactive) |
|
1242 (if (not abbrev-mode) |
|
1243 (self-insert-command 1) |
|
1244 (let (c) |
|
1245 (insert last-command-char) |
|
1246 (if (or (eq (setq c (read-event)) ??) |
|
1247 (eq c help-char)) |
|
1248 (let ((abbrev-table-name-list '(octave-mode-abbrev-table))) |
|
1249 (list-abbrevs)) |
|
1250 (setq unread-command-events (list c)))))) |
|
1251 |
|
1252 (defun octave-insert-defun (name args vals) |
|
1253 "Insert an Octave function skeleton. |
|
1254 Prompt for the function's name, arguments and return values (to be |
|
1255 entered without parens)." |
|
1256 (interactive |
|
1257 (list |
|
1258 (read-from-minibuffer "Function name: " |
|
1259 (substring (buffer-name) 0 -2)) |
|
1260 (read-from-minibuffer "Arguments: ") |
|
1261 (read-from-minibuffer "Return values: "))) |
|
1262 (let ((string (format "%s %s (%s)" |
|
1263 (cond |
|
1264 ((string-equal vals "") |
|
1265 vals) |
|
1266 ((string-match "[ ,]" vals) |
|
1267 (concat " [" vals "] =")) |
|
1268 (t |
|
1269 (concat " " vals " ="))) |
|
1270 name |
|
1271 args)) |
|
1272 (prefix octave-block-comment-start)) |
|
1273 (if (not (bobp)) (newline)) |
|
1274 (insert "function" string) |
|
1275 (indent-according-to-mode) |
|
1276 (newline 2) |
|
1277 (insert prefix "usage: " string) |
|
1278 (reindent-then-newline-and-indent) |
|
1279 (insert prefix) |
|
1280 (reindent-then-newline-and-indent) |
|
1281 (insert prefix) |
|
1282 (indent-according-to-mode) |
|
1283 (save-excursion |
|
1284 (newline 2) |
|
1285 (insert "endfunction") |
|
1286 (indent-according-to-mode)))) |
|
1287 |
|
1288 |
|
1289 ;;; Menu |
|
1290 (defun octave-add-octave-menu () |
|
1291 "Adds the `Octave' menu to the menu bar in Octave mode." |
|
1292 (require 'easymenu) |
|
1293 (easy-menu-define octave-mode-menu-map octave-mode-map |
|
1294 "Menu keymap for Octave mode." octave-mode-menu) |
|
1295 (easy-menu-add octave-mode-menu-map octave-mode-map)) |
|
1296 |
|
1297 |
|
1298 ;;; Communication with the inferior Octave process |
|
1299 (defun octave-kill-process () |
|
1300 "Kill inferior Octave process and its buffer." |
|
1301 (interactive) |
|
1302 (if inferior-octave-process |
|
1303 (progn |
|
1304 (process-send-string inferior-octave-process "quit;\n") |
|
1305 (accept-process-output inferior-octave-process))) |
|
1306 (if inferior-octave-buffer |
|
1307 (kill-buffer inferior-octave-buffer))) |
|
1308 |
|
1309 (defun octave-show-process-buffer () |
|
1310 "Make sure that `inferior-octave-buffer' is displayed." |
|
1311 (interactive) |
|
1312 (if (get-buffer inferior-octave-buffer) |
|
1313 (display-buffer inferior-octave-buffer) |
|
1314 (message "No buffer named %s" inferior-octave-buffer))) |
|
1315 |
|
1316 (defun octave-hide-process-buffer () |
|
1317 "Delete all windows that display `inferior-octave-buffer'." |
|
1318 (interactive) |
|
1319 (if (get-buffer inferior-octave-buffer) |
|
1320 (delete-windows-on inferior-octave-buffer) |
|
1321 (message "No buffer named %s" inferior-octave-buffer))) |
|
1322 |
|
1323 (defun octave-send-region (beg end) |
|
1324 "Send current region to the inferior Octave process." |
|
1325 (interactive "r") |
|
1326 (inferior-octave t) |
|
1327 (let ((proc inferior-octave-process) |
|
1328 (string (buffer-substring-no-properties beg end)) |
|
1329 line) |
|
1330 (save-excursion |
|
1331 (set-buffer inferior-octave-buffer) |
|
1332 (setq inferior-octave-output-list nil) |
|
1333 (while (not (string-equal string "")) |
|
1334 (if (string-match "\n" string) |
|
1335 (setq line (substring string 0 (match-beginning 0)) |
|
1336 string (substring string (match-end 0))) |
|
1337 (setq line string string "")) |
|
1338 (setq inferior-octave-receive-in-progress t) |
|
1339 (inferior-octave-send-list-and-digest (list (concat line "\n"))) |
|
1340 (while inferior-octave-receive-in-progress |
|
1341 (accept-process-output proc)) |
|
1342 (insert-before-markers |
|
1343 (mapconcat 'identity |
|
1344 (append |
|
1345 (if octave-send-echo-input (list line) (list "")) |
|
1346 (mapcar 'inferior-octave-strip-ctrl-g |
|
1347 inferior-octave-output-list) |
|
1348 (list inferior-octave-output-string)) |
|
1349 "\n"))))) |
|
1350 (if octave-send-show-buffer |
|
1351 (display-buffer inferior-octave-buffer))) |
|
1352 |
|
1353 (defun octave-send-block () |
|
1354 "Send current Octave block to the inferior Octave process." |
|
1355 (interactive) |
|
1356 (save-excursion |
|
1357 (octave-mark-block) |
|
1358 (octave-send-region (point) (mark)))) |
|
1359 |
|
1360 (defun octave-send-defun () |
|
1361 "Send current Octave function to the inferior Octave process." |
|
1362 (interactive) |
|
1363 (save-excursion |
|
1364 (octave-mark-defun) |
|
1365 (octave-send-region (point) (mark)))) |
|
1366 |
|
1367 (defun octave-send-line (&optional arg) |
|
1368 "Send current Octave code line to the inferior Octave process. |
|
1369 With positive prefix ARG, send that many lines. |
|
1370 If `octave-send-line-auto-forward' is non-nil, go to the next unsent |
|
1371 code line." |
|
1372 (interactive "P") |
|
1373 (or arg (setq arg 1)) |
|
1374 (if (> arg 0) |
|
1375 (let (beg end) |
|
1376 (beginning-of-line) |
|
1377 (setq beg (point)) |
|
1378 (octave-next-code-line (- arg 1)) |
|
1379 (end-of-line) |
|
1380 (setq end (point)) |
|
1381 (if octave-send-line-auto-forward |
|
1382 (octave-next-code-line 1)) |
|
1383 (octave-send-region beg end)))) |
|
1384 |
|
1385 (defun octave-eval-print-last-sexp () |
|
1386 "Evaluate Octave sexp before point and print value into current buffer." |
|
1387 (interactive) |
|
1388 (inferior-octave t) |
|
1389 (let ((standard-output (current-buffer)) |
|
1390 (print-escape-newlines nil) |
|
1391 (opoint (point))) |
|
1392 (terpri) |
|
1393 (prin1 |
|
1394 (save-excursion |
|
1395 (forward-sexp -1) |
|
1396 (inferior-octave-send-list-and-digest |
|
1397 (list (concat (buffer-substring-no-properties (point) opoint) |
|
1398 "\n"))) |
|
1399 (mapconcat 'identity inferior-octave-output-list "\n"))) |
|
1400 (terpri))) |
|
1401 |
|
1402 ;;; Bug reporting |
|
1403 (defun octave-submit-bug-report () |
|
1404 "Submit a bug report on the Emacs Octave package via mail." |
|
1405 (interactive) |
|
1406 (require 'reporter) |
|
1407 (and |
|
1408 (y-or-n-p "Do you want to submit a bug report? ") |
|
1409 (reporter-submit-bug-report |
|
1410 octave-maintainer-address |
|
1411 (concat "Emacs version " emacs-version) |
|
1412 (list |
3069
|
1413 'octave-auto-indent |
2617
|
1414 'octave-auto-newline |
|
1415 'octave-blink-matching-block |
|
1416 'octave-block-offset |
|
1417 'octave-comment-char |
|
1418 'octave-continuation-offset |
|
1419 'octave-continuation-string |
|
1420 'octave-help-files |
|
1421 'octave-mode-startup-message |
|
1422 'octave-send-echo-input |
|
1423 'octave-send-line-auto-forward |
|
1424 'octave-send-show-buffer)))) |
|
1425 |
|
1426 ;;; provide ourself |
|
1427 |
|
1428 (provide 'octave-mod) |
|
1429 |
|
1430 ;;; octave-mod.el ends here |