Commit | Line | Data |
---|---|---|
092af6d8 | 1 | ;;; octave-mod.el --- editing Octave source files under Emacs |
f2727dfb | 2 | |
ab422c4d | 3 | ;; Copyright (C) 1997, 2001-2013 Free Software Foundation, Inc. |
f2727dfb | 4 | |
f7fba1a8 | 5 | ;; Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
daf3dc79 | 6 | ;; John Eaton <jwe@octave.org> |
71d4c2a5 | 7 | ;; Maintainer: FSF |
f2727dfb RS |
8 | ;; Keywords: languages |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
b1fc2b50 | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
f2727dfb | 13 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
f2727dfb RS |
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 | |
b1fc2b50 | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
f2727dfb RS |
24 | |
25 | ;;; Commentary: | |
26 | ||
d1e49742 RS |
27 | ;; This package provides Emacs support for Octave. |
28 | ;; It defines Octave mode, a major mode for editing | |
29 | ;; Octave code. | |
30 | ||
d1e49742 RS |
31 | ;; The file octave-inf.el contains code for interacting with an inferior |
32 | ;; Octave process using comint. | |
f2727dfb | 33 | |
5d8137ab | 34 | ;; See the documentation of `octave-mode' and |
d1e49742 | 35 | ;; `run-octave' for further information on usage and customization. |
f2727dfb | 36 | |
d1e49742 | 37 | ;;; Code: |
2454554e | 38 | (require 'custom) |
f2727dfb | 39 | |
28d16ed3 AS |
40 | (defgroup octave nil |
41 | "Major mode for editing Octave source files." | |
8ec3bce0 | 42 | :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces) |
28d16ed3 AS |
43 | :group 'languages) |
44 | ||
e09735aa RS |
45 | (defvar inferior-octave-output-list nil) |
46 | (defvar inferior-octave-output-string nil) | |
47 | (defvar inferior-octave-receive-in-progress nil) | |
48 | ||
004a00f4 DN |
49 | (declare-function inferior-octave-send-list-and-digest "octave-inf" (list)) |
50 | ||
e7017ef9 | 51 | (defconst octave-maintainer-address |
f7fba1a8 | 52 | "Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>, bug-gnu-emacs@gnu.org" |
e7017ef9 RS |
53 | "Current maintainer of the Emacs Octave package.") |
54 | ||
7210c33f SM |
55 | (define-abbrev-table 'octave-abbrev-table |
56 | (mapcar (lambda (e) (append e '(nil 0 t))) | |
57 | '(("`a" "all_va_args") | |
58 | ("`b" "break") | |
59 | ("`cs" "case") | |
60 | ("`ca" "catch") | |
61 | ("`c" "continue") | |
62 | ("`el" "else") | |
63 | ("`eli" "elseif") | |
64 | ("`et" "end_try_catch") | |
65 | ("`eu" "end_unwind_protect") | |
66 | ("`ef" "endfor") | |
67 | ("`efu" "endfunction") | |
68 | ("`ei" "endif") | |
69 | ("`es" "endswitch") | |
70 | ("`ew" "endwhile") | |
71 | ("`f" "for") | |
72 | ("`fu" "function") | |
73 | ("`gl" "global") | |
74 | ("`gp" "gplot") | |
75 | ("`gs" "gsplot") | |
76 | ("`if" "if ()") | |
77 | ("`o" "otherwise") | |
78 | ("`rp" "replot") | |
79 | ("`r" "return") | |
80 | ("`s" "switch") | |
81 | ("`t" "try") | |
82 | ("`u" "until ()") | |
83 | ("`up" "unwind_protect") | |
84 | ("`upc" "unwind_protect_cleanup") | |
85 | ("`w" "while ()"))) | |
e7017ef9 | 86 | "Abbrev table for Octave's reserved words. |
99158127 | 87 | Used in `octave-mode' and inferior-octave-mode buffers. |
7210c33f SM |
88 | All Octave abbrevs start with a grave accent (`)." |
89 | :regexp "\\(?:[^`]\\|^\\)\\(\\(?:\\<\\|`\\)\\w+\\)\\W*") | |
e7017ef9 RS |
90 | |
91 | (defvar octave-comment-char ?# | |
92 | "Character to start an Octave comment.") | |
93 | (defvar octave-comment-start | |
c82d5b11 | 94 | (string octave-comment-char ?\s) |
e7017ef9 RS |
95 | "String to insert to start a new Octave in-line comment.") |
96 | (defvar octave-comment-start-skip "\\s<+\\s-*" | |
97 | "Regexp to match the start of an Octave comment up to its body.") | |
98 | ||
99 | (defvar octave-begin-keywords | |
490a8abe | 100 | '("do" "for" "function" "if" "switch" "try" "unwind_protect" "while")) |
e7017ef9 | 101 | (defvar octave-else-keywords |
6e6d2764 | 102 | '("case" "catch" "else" "elseif" "otherwise" "unwind_protect_cleanup")) |
e7017ef9 | 103 | (defvar octave-end-keywords |
490a8abe | 104 | '("endfor" "endfunction" "endif" "endswitch" "end_try_catch" |
b2ad70b6 | 105 | "end_unwind_protect" "endwhile" "until" "end")) |
e7017ef9 RS |
106 | |
107 | (defvar octave-reserved-words | |
6e6d2764 KH |
108 | (append octave-begin-keywords |
109 | octave-else-keywords | |
110 | octave-end-keywords | |
490a8abe | 111 | '("break" "continue" "end" "global" "persistent" "return")) |
e7017ef9 RS |
112 | "Reserved words in Octave.") |
113 | ||
114 | (defvar octave-text-functions | |
115 | '("casesen" "cd" "chdir" "clear" "diary" "dir" "document" "echo" | |
490a8abe GM |
116 | "edit_history" "format" "help" "history" "hold" |
117 | "load" "ls" "more" "run_history" "save" "type" | |
e7017ef9 | 118 | "which" "who" "whos") |
490a8abe | 119 | "Text functions in Octave.") |
e7017ef9 RS |
120 | |
121 | (defvar octave-variables | |
490a8abe GM |
122 | '("DEFAULT_EXEC_PATH" "DEFAULT_LOADPATH" |
123 | "EDITOR" "EXEC_PATH" "F_DUPFD" "F_GETFD" "F_GETFL" "F_SETFD" | |
124 | "F_SETFL" "I" "IMAGE_PATH" "Inf" "J" | |
125 | "NaN" "OCTAVE_VERSION" "O_APPEND" "O_CREAT" "O_EXCL" | |
e7017ef9 RS |
126 | "O_NONBLOCK" "O_RDONLY" "O_RDWR" "O_TRUNC" "O_WRONLY" "PAGER" "PS1" |
127 | "PS2" "PS4" "PWD" "SEEK_CUR" "SEEK_END" "SEEK_SET" "__F_DUPFD__" | |
128 | "__F_GETFD__" "__F_GETFL__" "__F_SETFD__" "__F_SETFL__" "__I__" | |
129 | "__Inf__" "__J__" "__NaN__" "__OCTAVE_VERSION__" "__O_APPEND__" | |
130 | "__O_CREAT__" "__O_EXCL__" "__O_NONBLOCK__" "__O_RDONLY__" | |
131 | "__O_RDWR__" "__O_TRUNC__" "__O_WRONLY__" "__PWD__" "__SEEK_CUR__" | |
132 | "__SEEK_END__" "__SEEK_SET__" "__argv__" "__e__" "__eps__" | |
490a8abe | 133 | "__i__" "__inf__" "__j__" "__nan__" "__pi__" |
e7017ef9 RS |
134 | "__program_invocation_name__" "__program_name__" "__realmax__" |
135 | "__realmin__" "__stderr__" "__stdin__" "__stdout__" "ans" "argv" | |
490a8abe GM |
136 | "beep_on_error" "completion_append_char" |
137 | "crash_dumps_octave_core" "default_save_format" | |
138 | "e" "echo_executing_commands" "eps" | |
139 | "error_text" "gnuplot_binary" "history_file" | |
140 | "history_size" "ignore_function_time_stamp" | |
141 | "inf" "nan" "nargin" "output_max_field_width" "output_precision" | |
e7017ef9 | 142 | "page_output_immediately" "page_screen_output" "pi" |
e7017ef9 | 143 | "print_answer_id_name" "print_empty_dimensions" |
490a8abe GM |
144 | "program_invocation_name" "program_name" |
145 | "realmax" "realmin" "return_last_computed_value" "save_precision" | |
146 | "saving_history" "sighup_dumps_octave_core" "sigterm_dumps_octave_core" | |
e7017ef9 RS |
147 | "silent_functions" "split_long_rows" "stderr" "stdin" "stdout" |
148 | "string_fill_char" "struct_levels_to_print" | |
490a8abe | 149 | "suppress_verbose_help_message") |
e7017ef9 RS |
150 | "Builtin variables in Octave.") |
151 | ||
152 | (defvar octave-function-header-regexp | |
e92f3bd3 SM |
153 | (concat "^\\s-*\\_<\\(function\\)\\_>" |
154 | "\\([^=;\n]*=[ \t]*\\|[ \t]*\\)\\(\\(?:\\w\\|\\s_\\)+\\)\\_>") | |
e7017ef9 RS |
155 | "Regexp to match an Octave function header. |
156 | The string `function' and its name are given by the first and third | |
157 | parenthetical grouping.") | |
158 | ||
159 | (defvar octave-font-lock-keywords | |
160 | (list | |
161 | ;; Fontify all builtin keywords. | |
e92f3bd3 | 162 | (cons (concat "\\_<\\(" |
e17b68ed SM |
163 | (regexp-opt (append octave-reserved-words |
164 | octave-text-functions)) | |
e92f3bd3 | 165 | "\\)\\_>") |
e7017ef9 RS |
166 | 'font-lock-keyword-face) |
167 | ;; Fontify all builtin operators. | |
168 | (cons "\\(&\\||\\|<=\\|>=\\|==\\|<\\|>\\|!=\\|!\\)" | |
cb41203e GM |
169 | (if (boundp 'font-lock-builtin-face) |
170 | 'font-lock-builtin-face | |
171 | 'font-lock-preprocessor-face)) | |
e7017ef9 | 172 | ;; Fontify all builtin variables. |
e92f3bd3 | 173 | (cons (concat "\\_<" (regexp-opt octave-variables) "\\_>") |
e7017ef9 RS |
174 | 'font-lock-variable-name-face) |
175 | ;; Fontify all function declarations. | |
176 | (list octave-function-header-regexp | |
177 | '(1 font-lock-keyword-face) | |
178 | '(3 font-lock-function-name-face nil t))) | |
179 | "Additional Octave expressions to highlight.") | |
180 | ||
cf38dd42 SM |
181 | (defun octave-syntax-propertize-function (start end) |
182 | (goto-char start) | |
183 | (octave-syntax-propertize-sqs end) | |
184 | (funcall (syntax-propertize-rules | |
602ea69d | 185 | ;; Try to distinguish the string-quotes from the transpose-quotes. |
cf38dd42 SM |
186 | ("[[({,; ]\\('\\)" |
187 | (1 (prog1 "\"'" (octave-syntax-propertize-sqs end))))) | |
188 | (point) end)) | |
189 | ||
190 | (defun octave-syntax-propertize-sqs (end) | |
191 | "Propertize the content/end of single-quote strings." | |
192 | (when (eq (nth 3 (syntax-ppss)) ?\') | |
602ea69d | 193 | ;; A '..' string. |
cf38dd42 SM |
194 | (when (re-search-forward |
195 | "\\(?:\\=\\|[^']\\)\\(?:''\\)*\\('\\)\\($\\|[^']\\)" end 'move) | |
196 | (goto-char (match-beginning 2)) | |
602ea69d SM |
197 | (when (eq (char-before (match-beginning 1)) ?\\) |
198 | ;; Backslash cannot escape a single quote. | |
199 | (put-text-property (1- (match-beginning 1)) (match-beginning 1) | |
200 | 'syntax-table (string-to-syntax "."))) | |
201 | (put-text-property (match-beginning 1) (match-end 1) | |
cf38dd42 | 202 | 'syntax-table (string-to-syntax "\"'"))))) |
ff80a446 | 203 | |
28d16ed3 | 204 | (defcustom inferior-octave-buffer "*Inferior Octave*" |
e22bbd48 | 205 | "Name of buffer for running an inferior Octave process." |
28d16ed3 AS |
206 | :type 'string |
207 | :group 'octave-inferior) | |
e7017ef9 RS |
208 | |
209 | (defvar inferior-octave-process nil) | |
210 | \f | |
e22bbd48 | 211 | (defvar octave-mode-map |
f2727dfb RS |
212 | (let ((map (make-sparse-keymap))) |
213 | (define-key map "`" 'octave-abbrev-start) | |
a1506d29 | 214 | (define-key map "\e\n" 'octave-indent-new-comment-line) |
a1506d29 | 215 | (define-key map "\M-\C-q" 'octave-indent-defun) |
f2727dfb RS |
216 | (define-key map "\C-c\C-b" 'octave-submit-bug-report) |
217 | (define-key map "\C-c\C-p" 'octave-previous-code-line) | |
218 | (define-key map "\C-c\C-n" 'octave-next-code-line) | |
219 | (define-key map "\C-c\C-a" 'octave-beginning-of-line) | |
a1506d29 | 220 | (define-key map "\C-c\C-e" 'octave-end-of-line) |
ec5d3ff7 | 221 | (define-key map [remap down-list] 'smie-down-list) |
f2727dfb | 222 | (define-key map "\C-c\M-\C-h" 'octave-mark-block) |
e17b68ed SM |
223 | (define-key map "\C-c]" 'smie-close-block) |
224 | (define-key map "\C-c/" 'smie-close-block) | |
34a463f1 | 225 | (define-key map "\C-c\C-f" 'octave-insert-defun) |
34a463f1 RS |
226 | (define-key map "\C-c\C-il" 'octave-send-line) |
227 | (define-key map "\C-c\C-ib" 'octave-send-block) | |
228 | (define-key map "\C-c\C-if" 'octave-send-defun) | |
a1506d29 | 229 | (define-key map "\C-c\C-ir" 'octave-send-region) |
34a463f1 RS |
230 | (define-key map "\C-c\C-is" 'octave-show-process-buffer) |
231 | (define-key map "\C-c\C-ih" 'octave-hide-process-buffer) | |
232 | (define-key map "\C-c\C-ik" 'octave-kill-process) | |
233 | (define-key map "\C-c\C-i\C-l" 'octave-send-line) | |
234 | (define-key map "\C-c\C-i\C-b" 'octave-send-block) | |
235 | (define-key map "\C-c\C-i\C-f" 'octave-send-defun) | |
a1506d29 | 236 | (define-key map "\C-c\C-i\C-r" 'octave-send-region) |
34a463f1 | 237 | (define-key map "\C-c\C-i\C-s" 'octave-show-process-buffer) |
7a1beb6c | 238 | (define-key map "\C-c\C-i\C-q" 'octave-hide-process-buffer) |
34a463f1 | 239 | (define-key map "\C-c\C-i\C-k" 'octave-kill-process) |
e22bbd48 SM |
240 | map) |
241 | "Keymap used in Octave mode.") | |
242 | ||
f2727dfb | 243 | |
ff80a446 SM |
244 | |
245 | (easy-menu-define octave-mode-menu octave-mode-map | |
246 | "Menu for Octave mode." | |
e22bbd48 | 247 | '("Octave" |
67885e8c | 248 | ("Lines" |
e22bbd48 SM |
249 | ["Previous Code Line" octave-previous-code-line t] |
250 | ["Next Code Line" octave-next-code-line t] | |
251 | ["Begin of Continuation" octave-beginning-of-line t] | |
252 | ["End of Continuation" octave-end-of-line t] | |
253 | ["Split Line at Point" octave-indent-new-comment-line t]) | |
67885e8c | 254 | ("Blocks" |
e22bbd48 | 255 | ["Mark Block" octave-mark-block t] |
e17b68ed | 256 | ["Close Block" smie-close-block t]) |
67885e8c | 257 | ("Functions" |
e22bbd48 SM |
258 | ["Indent Function" octave-indent-defun t] |
259 | ["Insert Function" octave-insert-defun t]) | |
260 | "-" | |
67885e8c | 261 | ("Debug" |
e22bbd48 SM |
262 | ["Send Current Line" octave-send-line t] |
263 | ["Send Current Block" octave-send-block t] | |
264 | ["Send Current Function" octave-send-defun t] | |
265 | ["Send Region" octave-send-region t] | |
266 | ["Show Process Buffer" octave-show-process-buffer t] | |
267 | ["Hide Process Buffer" octave-hide-process-buffer t] | |
268 | ["Kill Process" octave-kill-process t]) | |
269 | "-" | |
270 | ["Indent Line" indent-according-to-mode t] | |
c82d5b11 | 271 | ["Complete Symbol" completion-at-point t] |
e22bbd48 | 272 | "-" |
b5ba2d6f SM |
273 | ["Toggle Abbrev Mode" abbrev-mode |
274 | :style toggle :selected abbrev-mode] | |
275 | ["Toggle Auto-Fill Mode" auto-fill-mode | |
276 | :style toggle :selected auto-fill-function] | |
e22bbd48 SM |
277 | "-" |
278 | ["Submit Bug Report" octave-submit-bug-report t] | |
279 | "-" | |
c82d5b11 | 280 | ["Describe Octave Mode" describe-mode t] |
ff80a446 | 281 | ["Lookup Octave Index" info-lookup-symbol t])) |
f2727dfb | 282 | |
cb3d3ec1 | 283 | (defvar octave-mode-syntax-table |
f2727dfb RS |
284 | (let ((table (make-syntax-table))) |
285 | (modify-syntax-entry ?\r " " table) | |
286 | (modify-syntax-entry ?+ "." table) | |
287 | (modify-syntax-entry ?- "." table) | |
288 | (modify-syntax-entry ?= "." table) | |
289 | (modify-syntax-entry ?* "." table) | |
290 | (modify-syntax-entry ?/ "." table) | |
291 | (modify-syntax-entry ?> "." table) | |
292 | (modify-syntax-entry ?< "." table) | |
293 | (modify-syntax-entry ?& "." table) | |
294 | (modify-syntax-entry ?| "." table) | |
295 | (modify-syntax-entry ?! "." table) | |
296 | (modify-syntax-entry ?\\ "\\" table) | |
297 | (modify-syntax-entry ?\' "." table) | |
7210c33f SM |
298 | ;; Was "w" for abbrevs, but now that it's not necessary any more, |
299 | (modify-syntax-entry ?\` "." table) | |
f2727dfb | 300 | (modify-syntax-entry ?\" "\"" table) |
e92f3bd3 SM |
301 | (modify-syntax-entry ?. "_" table) |
302 | (modify-syntax-entry ?_ "_" table) | |
c5683ceb | 303 | ;; The "b" flag only applies to the second letter of the comstart |
7e82caa7 SM |
304 | ;; and the first letter of the comend, i.e. the "4b" below is ineffective. |
305 | ;; If we try to put `b' on the single-line comments, we get a similar | |
306 | ;; problem where the % and # chars appear as first chars of the 2-char | |
307 | ;; comend, so the multi-line ender is also turned into style-b. | |
c5683ceb | 308 | ;; So we need the new "c" comment style. |
c82d5b11 SM |
309 | (modify-syntax-entry ?\% "< 13" table) |
310 | (modify-syntax-entry ?\# "< 13" table) | |
c5683ceb SM |
311 | (modify-syntax-entry ?\{ "(} 2c" table) |
312 | (modify-syntax-entry ?\} "){ 4c" table) | |
f2727dfb | 313 | (modify-syntax-entry ?\n ">" table) |
cb3d3ec1 SM |
314 | table) |
315 | "Syntax table in use in `octave-mode' buffers.") | |
f2727dfb | 316 | |
28d16ed3 | 317 | (defcustom octave-blink-matching-block t |
e22bbd48 | 318 | "Control the blinking of matching Octave block keywords. |
f2727dfb | 319 | Non-nil means show matching begin of block when inserting a space, |
28d16ed3 AS |
320 | newline or semicolon after an else or end keyword." |
321 | :type 'boolean | |
322 | :group 'octave) | |
ff80a446 | 323 | |
28d16ed3 | 324 | (defcustom octave-block-offset 2 |
e22bbd48 | 325 | "Extra indentation applied to statements in Octave block structures." |
28d16ed3 AS |
326 | :type 'integer |
327 | :group 'octave) | |
f2727dfb | 328 | |
f2727dfb RS |
329 | (defvar octave-block-comment-start |
330 | (concat (make-string 2 octave-comment-char) " ") | |
331 | "String to insert to start a new Octave comment on an empty line.") | |
332 | ||
28d16ed3 | 333 | (defcustom octave-continuation-offset 4 |
e22bbd48 | 334 | "Extra indentation applied to Octave continuation lines." |
28d16ed3 AS |
335 | :type 'integer |
336 | :group 'octave) | |
e17b68ed SM |
337 | (eval-and-compile |
338 | (defconst octave-continuation-marker-regexp "\\\\\\|\\.\\.\\.")) | |
f2727dfb | 339 | (defvar octave-continuation-regexp |
e17b68ed SM |
340 | (concat "[^#%\n]*\\(" octave-continuation-marker-regexp |
341 | "\\)\\s-*\\(\\s<.*\\)?$")) | |
28d16ed3 | 342 | (defcustom octave-continuation-string "\\" |
e22bbd48 | 343 | "Character string used for Octave continuation lines. Normally \\." |
28d16ed3 AS |
344 | :type 'string |
345 | :group 'octave) | |
f2727dfb RS |
346 | |
347 | (defvar octave-completion-alist nil | |
348 | "Alist of Octave symbols for completion in Octave mode. | |
349 | Each element looks like (VAR . VAR), where the car and cdr are the same | |
350 | symbol (an Octave command or variable name). | |
a1506d29 | 351 | Currently, only builtin variables can be completed.") |
f2727dfb RS |
352 | |
353 | (defvar octave-mode-imenu-generic-expression | |
354 | (list | |
355 | ;; Functions | |
356 | (list nil octave-function-header-regexp 3)) | |
357 | "Imenu expression for Octave mode. See `imenu-generic-expression'.") | |
358 | ||
28d16ed3 | 359 | (defcustom octave-mode-hook nil |
e22bbd48 | 360 | "Hook to be run when Octave mode is started." |
28d16ed3 AS |
361 | :type 'hook |
362 | :group 'octave) | |
363 | ||
364 | (defcustom octave-send-show-buffer t | |
e22bbd48 | 365 | "Non-nil means display `inferior-octave-buffer' after sending to it." |
28d16ed3 AS |
366 | :type 'boolean |
367 | :group 'octave) | |
368 | (defcustom octave-send-line-auto-forward t | |
e22bbd48 | 369 | "Control auto-forward after sending to the inferior Octave process. |
28d16ed3 AS |
370 | Non-nil means always go to the next Octave code line after sending." |
371 | :type 'boolean | |
372 | :group 'octave) | |
373 | (defcustom octave-send-echo-input t | |
e22bbd48 | 374 | "Non-nil means echo input sent to the inferior Octave process." |
28d16ed3 AS |
375 | :type 'boolean |
376 | :group 'octave) | |
f2727dfb RS |
377 | |
378 | \f | |
e17b68ed SM |
379 | ;;; SMIE indentation |
380 | ||
381 | (require 'smie) | |
382 | ||
383 | (defconst octave-operator-table | |
384 | '((assoc ";" "\n") (assoc ",") ; The doc claims they have equal precedence!? | |
385 | (right "=" "+=" "-=" "*=" "/=") | |
386 | (assoc "&&") (assoc "||") ; The doc claims they have equal precedence!? | |
387 | (assoc "&") (assoc "|") ; The doc claims they have equal precedence!? | |
388 | (nonassoc "<" "<=" "==" ">=" ">" "!=" "~=") | |
389 | (nonassoc ":") ;No idea what this is. | |
390 | (assoc "+" "-") | |
391 | (assoc "*" "/" "\\" ".\\" ".*" "./") | |
392 | (nonassoc "'" ".'") | |
393 | (nonassoc "++" "--" "!" "~") ;And unary "+" and "-". | |
394 | (right "^" "**" ".^" ".**") | |
395 | ;; It's not really an operator, but for indentation purposes it | |
396 | ;; could be convenient to treat it as one. | |
397 | (assoc "..."))) | |
398 | ||
89acf735 SM |
399 | (defconst octave-smie-bnf-table |
400 | '((atom) | |
401 | ;; We can't distinguish the first element in a sequence with | |
402 | ;; precedence grammars, so we can't distinguish the condition | |
403 | ;; if the `if' from the subsequent body, for example. | |
404 | ;; This has to be done later in the indentation rules. | |
405 | (exp (exp "\n" exp) | |
406 | ;; We need to mention at least one of the operators in this part | |
407 | ;; of the grammar: if the BNF and the operator table have | |
408 | ;; no overlap, SMIE can't know how they relate. | |
409 | (exp ";" exp) | |
410 | ("try" exp "catch" exp "end_try_catch") | |
411 | ("try" exp "catch" exp "end") | |
412 | ("unwind_protect" exp | |
413 | "unwind_protect_cleanup" exp "end_unwind_protect") | |
414 | ("unwind_protect" exp "unwind_protect_cleanup" exp "end") | |
415 | ("for" exp "endfor") | |
416 | ("for" exp "end") | |
417 | ("do" exp "until" atom) | |
418 | ("while" exp "endwhile") | |
419 | ("while" exp "end") | |
420 | ("if" exp "endif") | |
421 | ("if" exp "else" exp "endif") | |
422 | ("if" exp "elseif" exp "else" exp "endif") | |
423 | ("if" exp "elseif" exp "elseif" exp "else" exp "endif") | |
424 | ("if" exp "elseif" exp "elseif" exp "else" exp "end") | |
425 | ("switch" exp "case" exp "endswitch") | |
426 | ("switch" exp "case" exp "otherwise" exp "endswitch") | |
427 | ("switch" exp "case" exp "case" exp "otherwise" exp "endswitch") | |
428 | ("switch" exp "case" exp "case" exp "otherwise" exp "end") | |
429 | ("function" exp "endfunction") | |
430 | ("function" exp "end")) | |
431 | ;; (fundesc (atom "=" atom)) | |
432 | )) | |
433 | ||
674728d4 SM |
434 | (defconst octave-smie-grammar |
435 | (smie-prec2->grammar | |
e17b68ed | 436 | (smie-merge-prec2s |
674728d4 SM |
437 | (smie-bnf->prec2 octave-smie-bnf-table |
438 | '((assoc "\n" ";"))) | |
e17b68ed | 439 | |
674728d4 | 440 | (smie-precs->prec2 octave-operator-table)))) |
e17b68ed SM |
441 | |
442 | ;; Tokenizing needs to be refined so that ";;" is treated as two | |
443 | ;; tokens and also so as to recognize the \n separator (and | |
444 | ;; corresponding continuation lines). | |
445 | ||
446 | (defconst octave-operator-regexp | |
447 | (regexp-opt (apply 'append (mapcar 'cdr octave-operator-table)))) | |
448 | ||
449 | (defun octave-smie-backward-token () | |
450 | (let ((pos (point))) | |
451 | (forward-comment (- (point))) | |
452 | (cond | |
453 | ((and (not (eq (char-before) ?\;)) ;Coalesce ";" and "\n". | |
454 | (> pos (line-end-position)) | |
455 | (if (looking-back octave-continuation-marker-regexp (- (point) 3)) | |
456 | (progn | |
457 | (goto-char (match-beginning 0)) | |
458 | (forward-comment (- (point))) | |
459 | nil) | |
460 | t) | |
461 | ;; Ignore it if it's within parentheses. | |
462 | (let ((ppss (syntax-ppss))) | |
463 | (not (and (nth 1 ppss) | |
464 | (eq ?\( (char-after (nth 1 ppss))))))) | |
465 | (skip-chars-forward " \t") | |
466 | ;; Why bother distinguishing \n and ;? | |
467 | ";") ;;"\n" | |
468 | ((and (looking-back octave-operator-regexp (- (point) 3) 'greedy) | |
469 | ;; Don't mistake a string quote for a transpose. | |
470 | (not (looking-back "\\s\"" (1- (point))))) | |
471 | (goto-char (match-beginning 0)) | |
472 | (match-string-no-properties 0)) | |
473 | (t | |
474 | (smie-default-backward-token))))) | |
475 | ||
476 | (defun octave-smie-forward-token () | |
477 | (skip-chars-forward " \t") | |
478 | (when (looking-at (eval-when-compile | |
479 | (concat "\\(" octave-continuation-marker-regexp | |
480 | "\\)[ \t]*\\($\\|[%#]\\)"))) | |
481 | (goto-char (match-end 1)) | |
482 | (forward-comment 1)) | |
483 | (cond | |
484 | ((and (looking-at "$\\|[%#]") | |
485 | ;; Ignore it if it's within parentheses. | |
486 | (prog1 (let ((ppss (syntax-ppss))) | |
487 | (not (and (nth 1 ppss) | |
488 | (eq ?\( (char-after (nth 1 ppss)))))) | |
489 | (forward-comment (point-max)))) | |
490 | ;; Why bother distinguishing \n and ;? | |
491 | ";") ;;"\n" | |
492 | ((looking-at ";[ \t]*\\($\\|[%#]\\)") | |
493 | ;; Combine the ; with the subsequent \n. | |
494 | (goto-char (match-beginning 1)) | |
495 | (forward-comment 1) | |
496 | ";") | |
497 | ((and (looking-at octave-operator-regexp) | |
498 | ;; Don't mistake a string quote for a transpose. | |
499 | (not (looking-at "\\s\""))) | |
500 | (goto-char (match-end 0)) | |
501 | (match-string-no-properties 0)) | |
502 | (t | |
503 | (smie-default-forward-token)))) | |
504 | ||
c4d17d50 SM |
505 | (defun octave-smie-rules (kind token) |
506 | (pcase (cons kind token) | |
674728d4 SM |
507 | ;; We could set smie-indent-basic instead, but that would have two |
508 | ;; disadvantages: | |
509 | ;; - changes to octave-block-offset wouldn't take effect immediately. | |
510 | ;; - edebug wouldn't show the use of this variable. | |
c4d17d50 | 511 | (`(:elem . basic) octave-block-offset) |
674728d4 SM |
512 | ;; Since "case" is in the same BNF rules as switch..end, SMIE by default |
513 | ;; aligns it with "switch". | |
514 | (`(:before . "case") (if (not (smie-rule-sibling-p)) octave-block-offset)) | |
c4d17d50 | 515 | (`(:after . ";") |
674728d4 SM |
516 | (if (smie-rule-parent-p "function" "if" "while" "else" "elseif" "for" |
517 | "otherwise" "case" "try" "catch" "unwind_protect" | |
518 | "unwind_protect_cleanup") | |
519 | (smie-rule-parent octave-block-offset) | |
c4d17d50 SM |
520 | ;; For (invalid) code between switch and case. |
521 | ;; (if (smie-parent-p "switch") 4) | |
522 | 0)))) | |
e17b68ed | 523 | |
03f70355 | 524 | (defvar electric-layout-rules) |
b073dc4b | 525 | |
f2727dfb | 526 | ;;;###autoload |
c82d5b11 | 527 | (define-derived-mode octave-mode prog-mode "Octave" |
f2727dfb RS |
528 | "Major mode for editing Octave code. |
529 | ||
530 | This mode makes it easier to write Octave code by helping with | |
531 | indentation, doing some of the typing for you (with Abbrev mode) and by | |
0472835f | 532 | showing keywords, comments, strings, etc. in different faces (with |
f2727dfb RS |
533 | Font Lock mode on terminals that support it). |
534 | ||
535 | Octave itself is a high-level language, primarily intended for numerical | |
536 | computations. It provides a convenient command line interface for | |
537 | solving linear and nonlinear problems numerically. Function definitions | |
538 | can also be stored in files, and it can be used in a batch mode (which | |
539 | is why you need this mode!). | |
540 | ||
541 | The latest released version of Octave is always available via anonymous | |
490a8abe | 542 | ftp from ftp.octave.org in the directory `/pub/octave'. Complete |
f2727dfb RS |
543 | source and binaries for several popular systems are available. |
544 | ||
545 | Type \\[list-abbrevs] to display the built-in abbrevs for Octave keywords. | |
546 | ||
547 | Keybindings | |
548 | =========== | |
549 | ||
550 | \\{octave-mode-map} | |
551 | ||
552 | Variables you can use to customize Octave mode | |
553 | ============================================== | |
554 | ||
e22bbd48 | 555 | `octave-blink-matching-block' |
f2727dfb RS |
556 | Non-nil means show matching begin of block when inserting a space, |
557 | newline or semicolon after an else or end keyword. Default is t. | |
558 | ||
e22bbd48 | 559 | `octave-block-offset' |
f2727dfb RS |
560 | Extra indentation applied to statements in block structures. |
561 | Default is 2. | |
562 | ||
e22bbd48 | 563 | `octave-continuation-offset' |
f2727dfb RS |
564 | Extra indentation applied to Octave continuation lines. |
565 | Default is 4. | |
566 | ||
e22bbd48 | 567 | `octave-continuation-string' |
f2727dfb RS |
568 | String used for Octave continuation lines. |
569 | Default is a backslash. | |
570 | ||
e22bbd48 | 571 | `octave-send-echo-input' |
f2727dfb RS |
572 | Non-nil means always display `inferior-octave-buffer' after sending a |
573 | command to the inferior Octave process. | |
574 | ||
e22bbd48 | 575 | `octave-send-line-auto-forward' |
f2727dfb RS |
576 | Non-nil means always go to the next unsent line of Octave code after |
577 | sending a line to the inferior Octave process. | |
578 | ||
e22bbd48 | 579 | `octave-send-echo-input' |
f2727dfb RS |
580 | Non-nil means echo input sent to the inferior Octave process. |
581 | ||
582 | Turning on Octave mode runs the hook `octave-mode-hook'. | |
583 | ||
584 | To begin using this mode for all `.m' files that you edit, add the | |
865fe16f | 585 | following lines to your init file: |
f2727dfb | 586 | |
e22bbd48 | 587 | (add-to-list 'auto-mode-alist '(\"\\\\.m\\\\'\" . octave-mode)) |
f2727dfb | 588 | |
490a8abe | 589 | To automatically turn on the abbrev and auto-fill features, |
865fe16f | 590 | add the following lines to your init file as well: |
f2727dfb RS |
591 | |
592 | (add-hook 'octave-mode-hook | |
593 | (lambda () | |
594 | (abbrev-mode 1) | |
e22bbd48 | 595 | (auto-fill-mode 1))) |
f2727dfb RS |
596 | |
597 | To submit a problem report, enter \\[octave-submit-bug-report] from \ | |
598 | an Octave mode buffer. | |
599 | This automatically sets up a mail buffer with version information | |
600 | already added. You just need to add a description of the problem, | |
601 | including a reproducible test case and send the message." | |
f2727dfb | 602 | (setq local-abbrev-table octave-abbrev-table) |
c82d5b11 | 603 | |
674728d4 | 604 | (smie-setup octave-smie-grammar #'octave-smie-rules |
c4d17d50 SM |
605 | :forward-token #'octave-smie-forward-token |
606 | :backward-token #'octave-smie-backward-token) | |
e17b68ed | 607 | (set (make-local-variable 'smie-indent-basic) 'octave-block-offset) |
c4d17d50 | 608 | |
89acf735 | 609 | (set (make-local-variable 'smie-blink-matching-triggers) |
c4d17d50 SM |
610 | (cons ?\; smie-blink-matching-triggers)) |
611 | (unless octave-blink-matching-block | |
612 | (remove-hook 'post-self-insert-hook #'smie-blink-matching-open 'local)) | |
89acf735 | 613 | |
2122161f SM |
614 | (set (make-local-variable 'electric-indent-chars) |
615 | (cons ?\; electric-indent-chars)) | |
03f70355 SM |
616 | ;; IIUC matlab-mode takes the opposite approach: it makes RET insert |
617 | ;; a ";" at those places where it's correct (i.e. outside of parens). | |
618 | (set (make-local-variable 'electric-layout-rules) '((?\; . after))) | |
c82d5b11 SM |
619 | |
620 | (set (make-local-variable 'comment-start) octave-comment-start) | |
621 | (set (make-local-variable 'comment-end) "") | |
622 | ;; Don't set it here: it's not really a property of the language, | |
623 | ;; just a personal preference of the author. | |
624 | ;; (set (make-local-variable 'comment-column) 32) | |
625 | (set (make-local-variable 'comment-start-skip) "\\s<+\\s-*") | |
dc94fc85 | 626 | (set (make-local-variable 'comment-add) 1) |
f2727dfb | 627 | |
c82d5b11 SM |
628 | (set (make-local-variable 'parse-sexp-ignore-comments) t) |
629 | (set (make-local-variable 'paragraph-start) | |
630 | (concat "\\s-*$\\|" page-delimiter)) | |
631 | (set (make-local-variable 'paragraph-separate) paragraph-start) | |
632 | (set (make-local-variable 'paragraph-ignore-fill-prefix) t) | |
633 | (set (make-local-variable 'fill-paragraph-function) 'octave-fill-paragraph) | |
634 | ;; FIXME: Why disable it? | |
635 | ;; (set (make-local-variable 'adaptive-fill-regexp) nil) | |
636 | ;; Again, this is not a property of the language, don't set it here. | |
637 | ;; (set (make-local-variable 'fill-column) 72) | |
638 | (set (make-local-variable 'normal-auto-fill-function) 'octave-auto-fill) | |
639 | ||
640 | (set (make-local-variable 'font-lock-defaults) | |
cf38dd42 SM |
641 | '(octave-font-lock-keywords)) |
642 | ||
643 | (set (make-local-variable 'syntax-propertize-function) | |
644 | #'octave-syntax-propertize-function) | |
c82d5b11 SM |
645 | |
646 | (set (make-local-variable 'imenu-generic-expression) | |
647 | octave-mode-imenu-generic-expression) | |
648 | (set (make-local-variable 'imenu-case-fold-search) nil) | |
649 | ||
650 | (add-hook 'completion-at-point-functions | |
651 | 'octave-completion-at-point-function nil t) | |
ff80a446 SM |
652 | (set (make-local-variable 'beginning-of-defun-function) |
653 | 'octave-beginning-of-defun) | |
f2727dfb | 654 | |
ff80a446 | 655 | (easy-menu-add octave-mode-menu) |
175069ef | 656 | (octave-initialize-completions)) |
f2727dfb RS |
657 | \f |
658 | ;;; Miscellaneous useful functions | |
f2727dfb | 659 | |
f2727dfb | 660 | (defsubst octave-in-comment-p () |
e7f767c2 | 661 | "Return t if point is inside an Octave comment." |
602ea69d | 662 | (nth 4 (syntax-ppss))) |
f2727dfb RS |
663 | |
664 | (defsubst octave-in-string-p () | |
e7f767c2 | 665 | "Return t if point is inside an Octave string." |
602ea69d | 666 | (nth 3 (syntax-ppss))) |
f2727dfb RS |
667 | |
668 | (defsubst octave-not-in-string-or-comment-p () | |
e7f767c2 | 669 | "Return t if point is not inside an Octave string or comment." |
602ea69d | 670 | (let ((pps (syntax-ppss))) |
f2727dfb RS |
671 | (not (or (nth 3 pps) (nth 4 pps))))) |
672 | ||
f2727dfb | 673 | |
a584f30f | 674 | (defun octave-looking-at-kw (regexp) |
050a4b35 | 675 | "Like `looking-at', but sets `case-fold-search' nil." |
a584f30f GM |
676 | (let ((case-fold-search nil)) |
677 | (looking-at regexp))) | |
678 | ||
d83ee578 KH |
679 | (defun octave-maybe-insert-continuation-string () |
680 | (if (or (octave-in-comment-p) | |
681 | (save-excursion | |
682 | (beginning-of-line) | |
683 | (looking-at octave-continuation-regexp))) | |
684 | nil | |
685 | (delete-horizontal-space) | |
686 | (insert (concat " " octave-continuation-string)))) | |
f2727dfb RS |
687 | \f |
688 | ;;; Indentation | |
f2727dfb RS |
689 | |
690 | (defun octave-indent-new-comment-line () | |
691 | "Break Octave line at point, continuing comment if within one. | |
692 | If within code, insert `octave-continuation-string' before breaking the | |
a1506d29 JB |
693 | line. If within a string, signal an error. |
694 | The new line is properly indented." | |
f2727dfb RS |
695 | (interactive) |
696 | (delete-horizontal-space) | |
697 | (cond | |
698 | ((octave-in-comment-p) | |
699 | (indent-new-comment-line)) | |
700 | ((octave-in-string-p) | |
701 | (error "Cannot split a code line inside a string")) | |
702 | (t | |
703 | (insert (concat " " octave-continuation-string)) | |
03f70355 | 704 | (reindent-then-newline-and-indent)))) |
f2727dfb RS |
705 | |
706 | (defun octave-indent-defun () | |
e22bbd48 | 707 | "Properly indent the Octave function which contains point." |
f2727dfb RS |
708 | (interactive) |
709 | (save-excursion | |
ff80a446 | 710 | (mark-defun) |
f2727dfb RS |
711 | (message "Indenting function...") |
712 | (indent-region (point) (mark) nil)) | |
713 | (message "Indenting function...done.")) | |
714 | ||
715 | \f | |
716 | ;;; Motion | |
717 | (defun octave-next-code-line (&optional arg) | |
718 | "Move ARG lines of Octave code forward (backward if ARG is negative). | |
719 | Skips past all empty and comment lines. Default for ARG is 1. | |
720 | ||
721 | On success, return 0. Otherwise, go as far as possible and return -1." | |
722 | (interactive "p") | |
723 | (or arg (setq arg 1)) | |
724 | (beginning-of-line) | |
725 | (let ((n 0) | |
726 | (inc (if (> arg 0) 1 -1))) | |
727 | (while (and (/= arg 0) (= n 0)) | |
728 | (setq n (forward-line inc)) | |
729 | (while (and (= n 0) | |
730 | (looking-at "\\s-*\\($\\|\\s<\\)")) | |
731 | (setq n (forward-line inc))) | |
732 | (setq arg (- arg inc))) | |
733 | n)) | |
a1506d29 | 734 | |
f2727dfb RS |
735 | (defun octave-previous-code-line (&optional arg) |
736 | "Move ARG lines of Octave code backward (forward if ARG is negative). | |
737 | Skips past all empty and comment lines. Default for ARG is 1. | |
738 | ||
739 | On success, return 0. Otherwise, go as far as possible and return -1." | |
740 | (interactive "p") | |
741 | (or arg (setq arg 1)) | |
742 | (octave-next-code-line (- arg))) | |
743 | ||
744 | (defun octave-beginning-of-line () | |
745 | "Move point to beginning of current Octave line. | |
746 | If on an empty or comment line, go to the beginning of that line. | |
747 | Otherwise, move backward to the beginning of the first Octave code line | |
748 | which is not inside a continuation statement, i.e., which does not | |
749 | follow a code line ending in `...' or `\\', or is inside an open | |
750 | parenthesis list." | |
751 | (interactive) | |
752 | (beginning-of-line) | |
753 | (if (not (looking-at "\\s-*\\($\\|\\s<\\)")) | |
754 | (while (or (condition-case nil | |
755 | (progn | |
756 | (up-list -1) | |
757 | (beginning-of-line) | |
758 | t) | |
759 | (error nil)) | |
760 | (and (or (looking-at "\\s-*\\($\\|\\s<\\)") | |
761 | (save-excursion | |
762 | (if (zerop (octave-previous-code-line)) | |
763 | (looking-at octave-continuation-regexp)))) | |
764 | (zerop (forward-line -1))))))) | |
765 | ||
766 | (defun octave-end-of-line () | |
767 | "Move point to end of current Octave line. | |
768 | If on an empty or comment line, go to the end of that line. | |
769 | Otherwise, move forward to the end of the first Octave code line which | |
770 | does not end in `...' or `\\' or is inside an open parenthesis list." | |
771 | (interactive) | |
772 | (end-of-line) | |
773 | (if (save-excursion | |
774 | (beginning-of-line) | |
775 | (looking-at "\\s-*\\($\\|\\s<\\)")) | |
776 | () | |
777 | (while (or (condition-case nil | |
778 | (progn | |
779 | (up-list 1) | |
780 | (end-of-line) | |
781 | t) | |
782 | (error nil)) | |
783 | (and (save-excursion | |
784 | (beginning-of-line) | |
785 | (or (looking-at "\\s-*\\($\\|\\s<\\)") | |
786 | (looking-at octave-continuation-regexp))) | |
787 | (zerop (forward-line 1))))) | |
788 | (end-of-line))) | |
a1506d29 | 789 | |
f2727dfb RS |
790 | (defun octave-mark-block () |
791 | "Put point at the beginning of this Octave block, mark at the end. | |
792 | The block marked is the one that contains point or follows point." | |
793 | (interactive) | |
ef54d315 SM |
794 | (if (and (looking-at "\\sw\\|\\s_") |
795 | (looking-back "\\sw\\|\\s_" (1- (point)))) | |
796 | (skip-syntax-forward "w_")) | |
ec5d3ff7 SM |
797 | (unless (or (looking-at "\\s(") |
798 | (save-excursion | |
799 | (let* ((token (funcall smie-forward-token-function)) | |
6d2a1e35 | 800 | (level (assoc token smie-grammar))) |
ef54d315 | 801 | (and level (not (numberp (cadr level))))))) |
ec5d3ff7 SM |
802 | (backward-up-list 1)) |
803 | (mark-sexp)) | |
f2727dfb | 804 | |
f2727dfb RS |
805 | (defun octave-beginning-of-defun (&optional arg) |
806 | "Move backward to the beginning of an Octave function. | |
807 | With positive ARG, do it that many times. Negative argument -N means | |
808 | move forward to Nth following beginning of a function. | |
809 | Returns t unless search stops at the beginning or end of the buffer." | |
f2727dfb RS |
810 | (let* ((arg (or arg 1)) |
811 | (inc (if (> arg 0) 1 -1)) | |
ff80a446 SM |
812 | (found nil) |
813 | (case-fold-search nil)) | |
f2727dfb | 814 | (and (not (eobp)) |
e92f3bd3 | 815 | (not (and (> arg 0) (looking-at "\\_<function\\_>"))) |
f2727dfb RS |
816 | (skip-syntax-forward "w")) |
817 | (while (and (/= arg 0) | |
818 | (setq found | |
e92f3bd3 | 819 | (re-search-backward "\\_<function\\_>" inc))) |
f2727dfb RS |
820 | (if (octave-not-in-string-or-comment-p) |
821 | (setq arg (- arg inc)))) | |
822 | (if found | |
823 | (progn | |
824 | (and (< inc 0) (goto-char (match-beginning 0))) | |
825 | t)))) | |
826 | ||
f2727dfb RS |
827 | \f |
828 | ;;; Filling | |
829 | (defun octave-auto-fill () | |
d83ee578 KH |
830 | "Perform auto-fill in Octave mode. |
831 | Returns nil if no feasible place to break the line could be found, and t | |
832 | otherwise." | |
833 | (let (fc give-up) | |
834 | (if (or (null (setq fc (current-fill-column))) | |
835 | (save-excursion | |
a1506d29 | 836 | (beginning-of-line) |
d83ee578 | 837 | (and auto-fill-inhibit-regexp |
a584f30f | 838 | (octave-looking-at-kw auto-fill-inhibit-regexp)))) |
d83ee578 KH |
839 | nil ; Can't do anything |
840 | (if (and (not (octave-in-comment-p)) | |
841 | (> (current-column) fc)) | |
842 | (setq fc (- fc (+ (length octave-continuation-string) 1)))) | |
843 | (while (and (not give-up) (> (current-column) fc)) | |
844 | (let* ((opoint (point)) | |
845 | (fpoint | |
846 | (save-excursion | |
847 | (move-to-column (+ fc 1)) | |
848 | (skip-chars-backward "^ \t\n") | |
849 | ;; If we're at the beginning of the line, break after | |
850 | ;; the first word | |
851 | (if (bolp) | |
852 | (re-search-forward "[ \t]" opoint t)) | |
853 | ;; If we're in a comment line, don't break after the | |
854 | ;; comment chars | |
855 | (if (save-excursion | |
856 | (skip-syntax-backward " <") | |
857 | (bolp)) | |
cb3d3ec1 | 858 | (re-search-forward "[ \t]" (line-end-position) |
d83ee578 KH |
859 | 'move)) |
860 | ;; If we're not in a comment line and just ahead the | |
861 | ;; continuation string, don't break here. | |
862 | (if (and (not (octave-in-comment-p)) | |
863 | (looking-at | |
864 | (concat "\\s-*" | |
865 | (regexp-quote | |
866 | octave-continuation-string) | |
867 | "\\s-*$"))) | |
868 | (end-of-line)) | |
869 | (skip-chars-backward " \t") | |
870 | (point)))) | |
871 | (if (save-excursion | |
872 | (goto-char fpoint) | |
873 | (not (or (bolp) (eolp)))) | |
874 | (let ((prev-column (current-column))) | |
875 | (if (save-excursion | |
876 | (skip-chars-backward " \t") | |
877 | (= (point) fpoint)) | |
878 | (progn | |
879 | (octave-maybe-insert-continuation-string) | |
880 | (indent-new-comment-line t)) | |
881 | (save-excursion | |
882 | (goto-char fpoint) | |
883 | (octave-maybe-insert-continuation-string) | |
884 | (indent-new-comment-line t))) | |
885 | (if (>= (current-column) prev-column) | |
886 | (setq give-up t))) | |
887 | (setq give-up t)))) | |
888 | (not give-up)))) | |
f2727dfb | 889 | |
e02f48d7 | 890 | (defun octave-fill-paragraph (&optional _arg) |
ec5d3ff7 SM |
891 | "Fill paragraph of Octave code, handling Octave comments." |
892 | ;; FIXME: difference with generic fill-paragraph: | |
893 | ;; - code lines are only split, never joined. | |
894 | ;; - \n that end comments are never removed. | |
895 | ;; - insert continuation marker when splitting code lines. | |
896 | (interactive "P") | |
897 | (save-excursion | |
898 | (let ((end (progn (forward-paragraph) (copy-marker (point) t))) | |
899 | (beg (progn | |
900 | (forward-paragraph -1) | |
901 | (skip-chars-forward " \t\n") | |
902 | (beginning-of-line) | |
903 | (point))) | |
904 | (cfc (current-fill-column)) | |
905 | comment-prefix) | |
906 | (goto-char beg) | |
907 | (while (< (point) end) | |
908 | (condition-case nil | |
909 | (indent-according-to-mode) | |
910 | (error nil)) | |
911 | (move-to-column cfc) | |
912 | ;; First check whether we need to combine non-empty comment lines | |
913 | (if (and (< (current-column) cfc) | |
914 | (octave-in-comment-p) | |
915 | (not (save-excursion | |
916 | (beginning-of-line) | |
917 | (looking-at "^\\s-*\\s<+\\s-*$")))) | |
918 | ;; This is a nonempty comment line which does not extend | |
919 | ;; past the fill column. If it is followed by a nonempty | |
920 | ;; comment line with the same comment prefix, try to | |
921 | ;; combine them, and repeat this until either we reach the | |
922 | ;; fill-column or there is nothing more to combine. | |
923 | (progn | |
924 | ;; Get the comment prefix | |
925 | (save-excursion | |
926 | (beginning-of-line) | |
927 | (while (and (re-search-forward "\\s<+") | |
928 | (not (octave-in-comment-p)))) | |
929 | (setq comment-prefix (match-string 0))) | |
930 | ;; And keep combining ... | |
931 | (while (and (< (current-column) cfc) | |
932 | (save-excursion | |
933 | (forward-line 1) | |
934 | (and (looking-at | |
935 | (concat "^\\s-*" | |
936 | comment-prefix | |
937 | "\\S<")) | |
938 | (not (looking-at | |
939 | (concat "^\\s-*" | |
940 | comment-prefix | |
941 | "\\s-*$")))))) | |
942 | (delete-char 1) | |
943 | (re-search-forward comment-prefix) | |
944 | (delete-region (match-beginning 0) (match-end 0)) | |
945 | (fixup-whitespace) | |
946 | (move-to-column cfc)))) | |
947 | ;; We might also try to combine continued code lines> Perhaps | |
948 | ;; some other time ... | |
949 | (skip-chars-forward "^ \t\n") | |
950 | (delete-horizontal-space) | |
951 | (if (or (< (current-column) cfc) | |
952 | (and (= (current-column) cfc) (eolp))) | |
953 | (forward-line 1) | |
954 | (if (not (eolp)) (insert " ")) | |
955 | (or (octave-auto-fill) | |
956 | (forward-line 1)))) | |
957 | t))) | |
f2727dfb RS |
958 | |
959 | \f | |
960 | ;;; Completions | |
961 | (defun octave-initialize-completions () | |
962 | "Create an alist for Octave completions." | |
963 | (if octave-completion-alist | |
964 | () | |
965 | (setq octave-completion-alist | |
c82d5b11 SM |
966 | (append octave-reserved-words |
967 | octave-text-functions | |
968 | octave-variables)))) | |
969 | ||
970 | (defun octave-completion-at-point-function () | |
971 | "Find the text to complete and the corresponding table." | |
e92f3bd3 | 972 | (let* ((beg (save-excursion (skip-syntax-backward "w_") (point))) |
ff80a446 SM |
973 | (end (point))) |
974 | (if (< beg (point)) | |
975 | ;; Extend region past point, if applicable. | |
e92f3bd3 SM |
976 | (save-excursion (skip-syntax-forward "w_") |
977 | (setq end (point)))) | |
c82d5b11 | 978 | (list beg end octave-completion-alist))) |
f2727dfb | 979 | |
bcd70d97 SM |
980 | (define-obsolete-function-alias 'octave-complete-symbol |
981 | 'completion-at-point "24.1") | |
f2727dfb RS |
982 | \f |
983 | ;;; Electric characters && friends | |
f2727dfb RS |
984 | |
985 | (defun octave-abbrev-start () | |
986 | "Start entering an Octave abbreviation. | |
987 | If Abbrev mode is turned on, typing ` (grave accent) followed by ? or | |
988 | \\[help-command] lists all Octave abbrevs. Any other key combination is | |
989 | executed normally. | |
990 | Note that all Octave mode abbrevs start with a grave accent." | |
991 | (interactive) | |
df96ab1e SM |
992 | (self-insert-command 1) |
993 | (when abbrev-mode | |
994 | (set-temporary-overlay-map | |
995 | (let ((map (make-sparse-keymap))) | |
996 | (define-key map [??] 'list-abbrevs) | |
997 | (define-key map (vector help-char) 'list-abbrevs) | |
998 | map)))) | |
f2727dfb | 999 | |
c82d5b11 | 1000 | (define-skeleton octave-insert-defun |
f2727dfb RS |
1001 | "Insert an Octave function skeleton. |
1002 | Prompt for the function's name, arguments and return values (to be | |
1003 | entered without parens)." | |
c82d5b11 SM |
1004 | (let* ((defname (substring (buffer-name) 0 -2)) |
1005 | (name (read-string (format "Function name (default %s): " defname) | |
1006 | nil nil defname)) | |
1007 | (args (read-string "Arguments: ")) | |
1008 | (vals (read-string "Return values: "))) | |
1009 | (format "%s%s (%s)" | |
1010 | (cond | |
1011 | ((string-equal vals "") vals) | |
1012 | ((string-match "[ ,]" vals) (concat "[" vals "] = ")) | |
1013 | (t (concat vals " = "))) | |
1014 | name | |
1015 | args)) | |
1016 | \n "function " > str \n \n | |
1017 | octave-block-comment-start "usage: " str \n | |
1018 | octave-block-comment-start \n octave-block-comment-start | |
1019 | \n _ \n | |
1020 | "endfunction" > \n) | |
f2727dfb | 1021 | \f |
f2727dfb RS |
1022 | ;;; Communication with the inferior Octave process |
1023 | (defun octave-kill-process () | |
1024 | "Kill inferior Octave process and its buffer." | |
1025 | (interactive) | |
1026 | (if inferior-octave-process | |
1027 | (progn | |
1028 | (process-send-string inferior-octave-process "quit;\n") | |
1029 | (accept-process-output inferior-octave-process))) | |
1030 | (if inferior-octave-buffer | |
1031 | (kill-buffer inferior-octave-buffer))) | |
1032 | ||
1033 | (defun octave-show-process-buffer () | |
1034 | "Make sure that `inferior-octave-buffer' is displayed." | |
1035 | (interactive) | |
1036 | (if (get-buffer inferior-octave-buffer) | |
1037 | (display-buffer inferior-octave-buffer) | |
1038 | (message "No buffer named %s" inferior-octave-buffer))) | |
1039 | ||
1040 | (defun octave-hide-process-buffer () | |
1041 | "Delete all windows that display `inferior-octave-buffer'." | |
1042 | (interactive) | |
1043 | (if (get-buffer inferior-octave-buffer) | |
1044 | (delete-windows-on inferior-octave-buffer) | |
1045 | (message "No buffer named %s" inferior-octave-buffer))) | |
1046 | ||
1047 | (defun octave-send-region (beg end) | |
1048 | "Send current region to the inferior Octave process." | |
1049 | (interactive "r") | |
a1506d29 | 1050 | (inferior-octave t) |
f2727dfb RS |
1051 | (let ((proc inferior-octave-process) |
1052 | (string (buffer-substring-no-properties beg end)) | |
1053 | line) | |
5d8137ab | 1054 | (with-current-buffer inferior-octave-buffer |
f2727dfb RS |
1055 | (setq inferior-octave-output-list nil) |
1056 | (while (not (string-equal string "")) | |
1057 | (if (string-match "\n" string) | |
1058 | (setq line (substring string 0 (match-beginning 0)) | |
1059 | string (substring string (match-end 0))) | |
1060 | (setq line string string "")) | |
1061 | (setq inferior-octave-receive-in-progress t) | |
1062 | (inferior-octave-send-list-and-digest (list (concat line "\n"))) | |
1063 | (while inferior-octave-receive-in-progress | |
1064 | (accept-process-output proc)) | |
1065 | (insert-before-markers | |
1066 | (mapconcat 'identity | |
1067 | (append | |
1068 | (if octave-send-echo-input (list line) (list "")) | |
1069 | (mapcar 'inferior-octave-strip-ctrl-g | |
1070 | inferior-octave-output-list) | |
1071 | (list inferior-octave-output-string)) | |
1072 | "\n"))))) | |
1073 | (if octave-send-show-buffer | |
1074 | (display-buffer inferior-octave-buffer))) | |
1075 | ||
1076 | (defun octave-send-block () | |
a1506d29 | 1077 | "Send current Octave block to the inferior Octave process." |
f2727dfb RS |
1078 | (interactive) |
1079 | (save-excursion | |
1080 | (octave-mark-block) | |
1081 | (octave-send-region (point) (mark)))) | |
1082 | ||
1083 | (defun octave-send-defun () | |
1084 | "Send current Octave function to the inferior Octave process." | |
1085 | (interactive) | |
1086 | (save-excursion | |
ff80a446 | 1087 | (mark-defun) |
f2727dfb RS |
1088 | (octave-send-region (point) (mark)))) |
1089 | ||
1090 | (defun octave-send-line (&optional arg) | |
1091 | "Send current Octave code line to the inferior Octave process. | |
1092 | With positive prefix ARG, send that many lines. | |
1093 | If `octave-send-line-auto-forward' is non-nil, go to the next unsent | |
1094 | code line." | |
1095 | (interactive "P") | |
1096 | (or arg (setq arg 1)) | |
1097 | (if (> arg 0) | |
1098 | (let (beg end) | |
1099 | (beginning-of-line) | |
1100 | (setq beg (point)) | |
1101 | (octave-next-code-line (- arg 1)) | |
1102 | (end-of-line) | |
1103 | (setq end (point)) | |
1104 | (if octave-send-line-auto-forward | |
1105 | (octave-next-code-line 1)) | |
1106 | (octave-send-region beg end)))) | |
1107 | ||
1108 | (defun octave-eval-print-last-sexp () | |
1109 | "Evaluate Octave sexp before point and print value into current buffer." | |
1110 | (interactive) | |
1111 | (inferior-octave t) | |
1112 | (let ((standard-output (current-buffer)) | |
1113 | (print-escape-newlines nil) | |
1114 | (opoint (point))) | |
1115 | (terpri) | |
a1506d29 | 1116 | (prin1 |
f2727dfb RS |
1117 | (save-excursion |
1118 | (forward-sexp -1) | |
1119 | (inferior-octave-send-list-and-digest | |
1120 | (list (concat (buffer-substring-no-properties (point) opoint) | |
1121 | "\n"))) | |
1122 | (mapconcat 'identity inferior-octave-output-list "\n"))) | |
1123 | (terpri))) | |
d1e49742 | 1124 | \f |
d1e49742 RS |
1125 | ;;; Bug reporting |
1126 | (defun octave-submit-bug-report () | |
1127 | "Submit a bug report on the Emacs Octave package via mail." | |
1128 | (interactive) | |
1129 | (require 'reporter) | |
1130 | (and | |
1131 | (y-or-n-p "Do you want to submit a bug report? ") | |
1132 | (reporter-submit-bug-report | |
1133 | octave-maintainer-address | |
1134 | (concat "Emacs version " emacs-version) | |
1135 | (list | |
d1e49742 RS |
1136 | 'octave-blink-matching-block |
1137 | 'octave-block-offset | |
1138 | 'octave-comment-char | |
1139 | 'octave-continuation-offset | |
1140 | 'octave-continuation-string | |
d1e49742 RS |
1141 | 'octave-send-echo-input |
1142 | 'octave-send-line-auto-forward | |
1143 | 'octave-send-show-buffer)))) | |
1144 | ||
5d8137ab | 1145 | ;; provide ourself |
d1e49742 | 1146 | |
e7017ef9 | 1147 | (provide 'octave-mod) |
d1e49742 RS |
1148 | |
1149 | ;;; octave-mod.el ends here |