1 ;;; octave-mod.el --- editing Octave source files under Emacs
3 ;; Copyright (C) 1997, 2001-2013 Free Software Foundation, Inc.
5 ;; Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
6 ;; John Eaton <jwe@octave.org>
10 ;; This file is part of GNU Emacs.
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 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This package provides Emacs support for Octave.
28 ;; It defines Octave mode, a major mode for editing
31 ;; The file octave-inf.el contains code for interacting with an inferior
32 ;; Octave process using comint.
34 ;; See the documentation of `octave-mode' and
35 ;; `run-octave' for further information on usage and customization.
41 "Major mode for editing Octave source files."
42 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
45 (defvar inferior-octave-output-list nil
)
46 (defvar inferior-octave-output-string nil
)
47 (defvar inferior-octave-receive-in-progress nil
)
49 (declare-function inferior-octave-send-list-and-digest
"octave-inf" (list))
51 (defconst octave-maintainer-address
52 "Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>, bug-gnu-emacs@gnu.org"
53 "Current maintainer of the Emacs Octave package.")
55 (define-abbrev-table 'octave-abbrev-table
56 (mapcar (lambda (e) (append e
'(nil 0 t
)))
57 '(("`a" "all_va_args")
64 ("`et" "end_try_catch")
65 ("`eu" "end_unwind_protect")
67 ("`efu" "endfunction")
83 ("`up" "unwind_protect")
84 ("`upc" "unwind_protect_cleanup")
86 "Abbrev table for Octave's reserved words.
87 Used in `octave-mode' and inferior-octave-mode buffers.
88 All Octave abbrevs start with a grave accent (`)."
89 :regexp
"\\(?:[^`]\\|^\\)\\(\\(?:\\<\\|`\\)\\w+\\)\\W*")
91 (defvar octave-comment-char ?
#
92 "Character to start an Octave comment.")
93 (defvar octave-comment-start
94 (string octave-comment-char ?\s
)
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.")
99 (defvar octave-begin-keywords
100 '("do" "for" "function" "if" "switch" "try" "unwind_protect" "while"))
101 (defvar octave-else-keywords
102 '("case" "catch" "else" "elseif" "otherwise" "unwind_protect_cleanup"))
103 (defvar octave-end-keywords
104 '("endfor" "endfunction" "endif" "endswitch" "end_try_catch"
105 "end_unwind_protect" "endwhile" "until" "end"))
107 (defvar octave-reserved-words
108 (append octave-begin-keywords
111 '("break" "continue" "end" "global" "persistent" "return"))
112 "Reserved words in Octave.")
114 (defvar octave-text-functions
115 '("casesen" "cd" "chdir" "clear" "diary" "dir" "document" "echo"
116 "edit_history" "format" "help" "history" "hold"
117 "load" "ls" "more" "run_history" "save" "type"
118 "which" "who" "whos")
119 "Text functions in Octave.")
121 (defvar octave-variables
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"
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__"
133 "__i__" "__inf__" "__j__" "__nan__" "__pi__"
134 "__program_invocation_name__" "__program_name__" "__realmax__"
135 "__realmin__" "__stderr__" "__stdin__" "__stdout__" "ans" "argv"
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"
142 "page_output_immediately" "page_screen_output" "pi"
143 "print_answer_id_name" "print_empty_dimensions"
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"
147 "silent_functions" "split_long_rows" "stderr" "stdin" "stdout"
148 "string_fill_char" "struct_levels_to_print"
149 "suppress_verbose_help_message")
150 "Builtin variables in Octave.")
152 (defvar octave-function-header-regexp
153 (concat "^\\s-*\\_<\\(function\\)\\_>"
154 "\\([^=;\n]*=[ \t]*\\|[ \t]*\\)\\(\\(?:\\w\\|\\s_\\)+\\)\\_>")
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.")
159 (defvar octave-font-lock-keywords
161 ;; Fontify all builtin keywords.
162 (cons (concat "\\_<\\("
163 (regexp-opt (append octave-reserved-words
164 octave-text-functions
))
166 'font-lock-keyword-face
)
167 ;; Fontify all builtin operators.
168 (cons "\\(&\\||\\|<=\\|>=\\|==\\|<\\|>\\|!=\\|!\\)"
169 (if (boundp 'font-lock-builtin-face
)
170 'font-lock-builtin-face
171 'font-lock-preprocessor-face
))
172 ;; Fontify all builtin variables.
173 (cons (concat "\\_<" (regexp-opt octave-variables
) "\\_>")
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.")
181 (defun octave-syntax-propertize-function (start end
)
183 (octave-syntax-propertize-sqs end
)
184 (funcall (syntax-propertize-rules
185 ;; Try to distinguish the string-quotes from the transpose-quotes.
187 (1 (prog1 "\"'" (octave-syntax-propertize-sqs end
)))))
190 (defun octave-syntax-propertize-sqs (end)
191 "Propertize the content/end of single-quote strings."
192 (when (eq (nth 3 (syntax-ppss)) ?
\')
194 (when (re-search-forward
195 "\\(?:\\=\\|[^']\\)\\(?:''\\)*\\('\\)\\($\\|[^']\\)" end
'move
)
196 (goto-char (match-beginning 2))
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)
202 'syntax-table
(string-to-syntax "\"'")))))
204 (defcustom inferior-octave-buffer
"*Inferior Octave*"
205 "Name of buffer for running an inferior Octave process."
207 :group
'octave-inferior
)
209 (defvar inferior-octave-process nil
)
211 (defvar octave-mode-map
212 (let ((map (make-sparse-keymap)))
213 (define-key map
"`" 'octave-abbrev-start
)
214 (define-key map
"\e\n" 'octave-indent-new-comment-line
)
215 (define-key map
"\M-\C-q" 'octave-indent-defun
)
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
)
220 (define-key map
"\C-c\C-e" 'octave-end-of-line
)
221 (define-key map
[remap down-list
] 'smie-down-list
)
222 (define-key map
"\C-c\M-\C-h" 'octave-mark-block
)
223 (define-key map
"\C-c]" 'smie-close-block
)
224 (define-key map
"\C-c/" 'smie-close-block
)
225 (define-key map
"\C-c\C-f" 'octave-insert-defun
)
226 ;; FIXME: free C-h so it can do the describe-prefix-bindings.
227 (define-key map
"\C-c\C-h" 'info-lookup-symbol
)
228 (define-key map
"\C-c\C-il" 'octave-send-line
)
229 (define-key map
"\C-c\C-ib" 'octave-send-block
)
230 (define-key map
"\C-c\C-if" 'octave-send-defun
)
231 (define-key map
"\C-c\C-ir" 'octave-send-region
)
232 (define-key map
"\C-c\C-is" 'octave-show-process-buffer
)
233 (define-key map
"\C-c\C-ih" 'octave-hide-process-buffer
)
234 (define-key map
"\C-c\C-ik" 'octave-kill-process
)
235 (define-key map
"\C-c\C-i\C-l" 'octave-send-line
)
236 (define-key map
"\C-c\C-i\C-b" 'octave-send-block
)
237 (define-key map
"\C-c\C-i\C-f" 'octave-send-defun
)
238 (define-key map
"\C-c\C-i\C-r" 'octave-send-region
)
239 (define-key map
"\C-c\C-i\C-s" 'octave-show-process-buffer
)
240 ;; FIXME: free C-h so it can do the describe-prefix-bindings.
241 (define-key map
"\C-c\C-i\C-h" 'octave-hide-process-buffer
)
242 (define-key map
"\C-c\C-i\C-k" 'octave-kill-process
)
244 "Keymap used in Octave mode.")
248 (easy-menu-define octave-mode-menu octave-mode-map
249 "Menu for Octave mode."
252 ["Previous Code Line" octave-previous-code-line t
]
253 ["Next Code Line" octave-next-code-line t
]
254 ["Begin of Continuation" octave-beginning-of-line t
]
255 ["End of Continuation" octave-end-of-line t
]
256 ["Split Line at Point" octave-indent-new-comment-line t
])
258 ["Mark Block" octave-mark-block t
]
259 ["Close Block" smie-close-block t
])
261 ["Indent Function" octave-indent-defun
t]
262 ["Insert Function" octave-insert-defun
t])
265 ["Send Current Line" octave-send-line t
]
266 ["Send Current Block" octave-send-block t
]
267 ["Send Current Function" octave-send-defun
t]
268 ["Send Region" octave-send-region t
]
269 ["Show Process Buffer" octave-show-process-buffer t
]
270 ["Hide Process Buffer" octave-hide-process-buffer t
]
271 ["Kill Process" octave-kill-process t
])
273 ["Indent Line" indent-according-to-mode t
]
274 ["Complete Symbol" completion-at-point t
]
276 ["Toggle Abbrev Mode" abbrev-mode
277 :style toggle
:selected abbrev-mode
]
278 ["Toggle Auto-Fill Mode" auto-fill-mode
279 :style toggle
:selected auto-fill-function
]
281 ["Submit Bug Report" octave-submit-bug-report t
]
283 ["Describe Octave Mode" describe-mode t
]
284 ["Lookup Octave Index" info-lookup-symbol t
]))
286 (defvar octave-mode-syntax-table
287 (let ((table (make-syntax-table)))
288 (modify-syntax-entry ?
\r " " table
)
289 (modify-syntax-entry ?
+ "." table
)
290 (modify-syntax-entry ?-
"." table
)
291 (modify-syntax-entry ?
= "." table
)
292 (modify-syntax-entry ?
* "." table
)
293 (modify-syntax-entry ?
/ "." table
)
294 (modify-syntax-entry ?
> "." table
)
295 (modify-syntax-entry ?
< "." table
)
296 (modify-syntax-entry ?
& "." table
)
297 (modify-syntax-entry ?|
"." table
)
298 (modify-syntax-entry ?
! "." table
)
299 (modify-syntax-entry ?
\\ "\\" table
)
300 (modify-syntax-entry ?
\' "." table
)
301 ;; Was "w" for abbrevs, but now that it's not necessary any more,
302 (modify-syntax-entry ?\
` "." table
)
303 (modify-syntax-entry ?
\" "\"" table
)
304 (modify-syntax-entry ?.
"_" table
)
305 (modify-syntax-entry ?_
"_" table
)
306 ;; The "b" flag only applies to the second letter of the comstart
307 ;; and the first letter of the comend, i.e. the "4b" below is ineffective.
308 ;; If we try to put `b' on the single-line comments, we get a similar
309 ;; problem where the % and # chars appear as first chars of the 2-char
310 ;; comend, so the multi-line ender is also turned into style-b.
311 ;; So we need the new "c" comment style.
312 (modify-syntax-entry ?\%
"< 13" table
)
313 (modify-syntax-entry ?\
# "< 13" table
)
314 (modify-syntax-entry ?\
{ "(} 2c" table
)
315 (modify-syntax-entry ?\
} "){ 4c" table
)
316 (modify-syntax-entry ?
\n ">" table
)
318 "Syntax table in use in `octave-mode' buffers.")
320 (defcustom octave-blink-matching-block t
321 "Control the blinking of matching Octave block keywords.
322 Non-nil means show matching begin of block when inserting a space,
323 newline or semicolon after an else or end keyword."
327 (defcustom octave-block-offset
2
328 "Extra indentation applied to statements in Octave block structures."
332 (defvar octave-block-comment-start
333 (concat (make-string 2 octave-comment-char
) " ")
334 "String to insert to start a new Octave comment on an empty line.")
336 (defcustom octave-continuation-offset
4
337 "Extra indentation applied to Octave continuation lines."
341 (defconst octave-continuation-marker-regexp
"\\\\\\|\\.\\.\\."))
342 (defvar octave-continuation-regexp
343 (concat "[^#%\n]*\\(" octave-continuation-marker-regexp
344 "\\)\\s-*\\(\\s<.*\\)?$"))
345 (defcustom octave-continuation-string
"\\"
346 "Character string used for Octave continuation lines. Normally \\."
350 (defvar octave-completion-alist nil
351 "Alist of Octave symbols for completion in Octave mode.
352 Each element looks like (VAR . VAR), where the car and cdr are the same
353 symbol (an Octave command or variable name).
354 Currently, only builtin variables can be completed.")
356 (defvar octave-mode-imenu-generic-expression
359 (list nil octave-function-header-regexp
3))
360 "Imenu expression for Octave mode. See `imenu-generic-expression'.")
362 (defcustom octave-mode-hook nil
363 "Hook to be run when Octave mode is started."
367 (defcustom octave-send-show-buffer t
368 "Non-nil means display `inferior-octave-buffer' after sending to it."
371 (defcustom 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."
376 (defcustom octave-send-echo-input t
377 "Non-nil means echo input sent to the inferior Octave process."
386 (defconst octave-operator-table
387 '((assoc ";" "\n") (assoc ",") ; The doc claims they have equal precedence!?
388 (right "=" "+=" "-=" "*=" "/=")
389 (assoc "&&") (assoc "||") ; The doc claims they have equal precedence!?
390 (assoc "&") (assoc "|") ; The doc claims they have equal precedence!?
391 (nonassoc "<" "<=" "==" ">=" ">" "!=" "~=")
392 (nonassoc ":") ;No idea what this is.
394 (assoc "*" "/" "\\" ".\\" ".*" "./")
396 (nonassoc "++" "--" "!" "~") ;And unary "+" and "-".
397 (right "^" "**" ".^" ".**")
398 ;; It's not really an operator, but for indentation purposes it
399 ;; could be convenient to treat it as one.
402 (defconst octave-smie-bnf-table
404 ;; We can't distinguish the first element in a sequence with
405 ;; precedence grammars, so we can't distinguish the condition
406 ;; if the `if' from the subsequent body, for example.
407 ;; This has to be done later in the indentation rules.
409 ;; We need to mention at least one of the operators in this part
410 ;; of the grammar: if the BNF and the operator table have
411 ;; no overlap, SMIE can't know how they relate.
413 ("try" exp
"catch" exp
"end_try_catch")
414 ("try" exp
"catch" exp
"end")
415 ("unwind_protect" exp
416 "unwind_protect_cleanup" exp
"end_unwind_protect")
417 ("unwind_protect" exp
"unwind_protect_cleanup" exp
"end")
420 ("do" exp
"until" atom
)
421 ("while" exp
"endwhile")
424 ("if" exp
"else" exp
"endif")
425 ("if" exp
"elseif" exp
"else" exp
"endif")
426 ("if" exp
"elseif" exp
"elseif" exp
"else" exp
"endif")
427 ("if" exp
"elseif" exp
"elseif" exp
"else" exp
"end")
428 ("switch" exp
"case" exp
"endswitch")
429 ("switch" exp
"case" exp
"otherwise" exp
"endswitch")
430 ("switch" exp
"case" exp
"case" exp
"otherwise" exp
"endswitch")
431 ("switch" exp
"case" exp
"case" exp
"otherwise" exp
"end")
432 ("function" exp
"endfunction")
433 ("function" exp
"end"))
434 ;; (fundesc (atom "=" atom))
437 (defconst octave-smie-grammar
440 (smie-bnf->prec2 octave-smie-bnf-table
443 (smie-precs->prec2 octave-operator-table
))))
445 ;; Tokenizing needs to be refined so that ";;" is treated as two
446 ;; tokens and also so as to recognize the \n separator (and
447 ;; corresponding continuation lines).
449 (defconst octave-operator-regexp
450 (regexp-opt (apply 'append
(mapcar 'cdr octave-operator-table
))))
452 (defun octave-smie-backward-token ()
454 (forward-comment (- (point)))
456 ((and (not (eq (char-before) ?\
;)) ;Coalesce ";" and "\n".
457 (> pos
(line-end-position))
458 (if (looking-back octave-continuation-marker-regexp
(- (point) 3))
460 (goto-char (match-beginning 0))
461 (forward-comment (- (point)))
464 ;; Ignore it if it's within parentheses.
465 (let ((ppss (syntax-ppss)))
466 (not (and (nth 1 ppss
)
467 (eq ?\
( (char-after (nth 1 ppss
)))))))
468 (skip-chars-forward " \t")
469 ;; Why bother distinguishing \n and ;?
471 ((and (looking-back octave-operator-regexp
(- (point) 3) 'greedy
)
472 ;; Don't mistake a string quote for a transpose.
473 (not (looking-back "\\s\"" (1- (point)))))
474 (goto-char (match-beginning 0))
475 (match-string-no-properties 0))
477 (smie-default-backward-token)))))
479 (defun octave-smie-forward-token ()
480 (skip-chars-forward " \t")
481 (when (looking-at (eval-when-compile
482 (concat "\\(" octave-continuation-marker-regexp
483 "\\)[ \t]*\\($\\|[%#]\\)")))
484 (goto-char (match-end 1))
487 ((and (looking-at "$\\|[%#]")
488 ;; Ignore it if it's within parentheses.
489 (prog1 (let ((ppss (syntax-ppss)))
490 (not (and (nth 1 ppss
)
491 (eq ?\
( (char-after (nth 1 ppss
))))))
492 (forward-comment (point-max))))
493 ;; Why bother distinguishing \n and ;?
495 ((looking-at ";[ \t]*\\($\\|[%#]\\)")
496 ;; Combine the ; with the subsequent \n.
497 (goto-char (match-beginning 1))
500 ((and (looking-at octave-operator-regexp
)
501 ;; Don't mistake a string quote for a transpose.
502 (not (looking-at "\\s\"")))
503 (goto-char (match-end 0))
504 (match-string-no-properties 0))
506 (smie-default-forward-token))))
508 (defun octave-smie-rules (kind token
)
509 (pcase (cons kind token
)
510 ;; We could set smie-indent-basic instead, but that would have two
512 ;; - changes to octave-block-offset wouldn't take effect immediately.
513 ;; - edebug wouldn't show the use of this variable.
514 (`(:elem . basic
) octave-block-offset
)
515 ;; Since "case" is in the same BNF rules as switch..end, SMIE by default
516 ;; aligns it with "switch".
517 (`(:before .
"case") (if (not (smie-rule-sibling-p)) octave-block-offset
))
519 (if (smie-rule-parent-p "function" "if" "while" "else" "elseif" "for"
520 "otherwise" "case" "try" "catch" "unwind_protect"
521 "unwind_protect_cleanup")
522 (smie-rule-parent octave-block-offset
)
523 ;; For (invalid) code between switch and case.
524 ;; (if (smie-parent-p "switch") 4)
527 (defvar electric-layout-rules
)
530 (define-derived-mode octave-mode prog-mode
"Octave"
531 "Major mode for editing Octave code.
533 This mode makes it easier to write Octave code by helping with
534 indentation, doing some of the typing for you (with Abbrev mode) and by
535 showing keywords, comments, strings, etc. in different faces (with
536 Font Lock mode on terminals that support it).
538 Octave itself is a high-level language, primarily intended for numerical
539 computations. It provides a convenient command line interface for
540 solving linear and nonlinear problems numerically. Function definitions
541 can also be stored in files, and it can be used in a batch mode (which
542 is why you need this mode!).
544 The latest released version of Octave is always available via anonymous
545 ftp from ftp.octave.org in the directory `/pub/octave'. Complete
546 source and binaries for several popular systems are available.
548 Type \\[list-abbrevs] to display the built-in abbrevs for Octave keywords.
555 Variables you can use to customize Octave mode
556 ==============================================
558 `octave-blink-matching-block'
559 Non-nil means show matching begin of block when inserting a space,
560 newline or semicolon after an else or end keyword. Default is t.
562 `octave-block-offset'
563 Extra indentation applied to statements in block structures.
566 `octave-continuation-offset'
567 Extra indentation applied to Octave continuation lines.
570 `octave-continuation-string'
571 String used for Octave continuation lines.
572 Default is a backslash.
574 `octave-send-echo-input'
575 Non-nil means always display `inferior-octave-buffer' after sending a
576 command to the inferior Octave process.
578 `octave-send-line-auto-forward'
579 Non-nil means always go to the next unsent line of Octave code after
580 sending a line to the inferior Octave process.
582 `octave-send-echo-input'
583 Non-nil means echo input sent to the inferior Octave process.
585 Turning on Octave mode runs the hook `octave-mode-hook'.
587 To begin using this mode for all `.m' files that you edit, add the
588 following lines to your init file:
590 (add-to-list 'auto-mode-alist '(\"\\\\.m\\\\'\" . octave-mode))
592 To automatically turn on the abbrev and auto-fill features,
593 add the following lines to your init file as well:
595 (add-hook 'octave-mode-hook
600 To submit a problem report, enter \\[octave-submit-bug-report] from \
601 an Octave mode buffer.
602 This automatically sets up a mail buffer with version information
603 already added. You just need to add a description of the problem,
604 including a reproducible test case and send the message."
605 (setq local-abbrev-table octave-abbrev-table
)
607 (smie-setup octave-smie-grammar
#'octave-smie-rules
608 :forward-token
#'octave-smie-forward-token
609 :backward-token
#'octave-smie-backward-token
)
610 (set (make-local-variable 'smie-indent-basic
) 'octave-block-offset
)
612 (set (make-local-variable 'smie-blink-matching-triggers
)
613 (cons ?\
; smie-blink-matching-triggers))
614 (unless octave-blink-matching-block
615 (remove-hook 'post-self-insert-hook
#'smie-blink-matching-open
'local
))
617 (set (make-local-variable 'electric-indent-chars
)
618 (cons ?\
; electric-indent-chars))
619 ;; IIUC matlab-mode takes the opposite approach: it makes RET insert
620 ;; a ";" at those places where it's correct (i.e. outside of parens).
621 (set (make-local-variable 'electric-layout-rules
) '((?\
; . after)))
623 (set (make-local-variable 'comment-start
) octave-comment-start
)
624 (set (make-local-variable 'comment-end
) "")
625 ;; Don't set it here: it's not really a property of the language,
626 ;; just a personal preference of the author.
627 ;; (set (make-local-variable 'comment-column) 32)
628 (set (make-local-variable 'comment-start-skip
) "\\s<+\\s-*")
629 (set (make-local-variable 'comment-add
) 1)
631 (set (make-local-variable 'parse-sexp-ignore-comments
) t
)
632 (set (make-local-variable 'paragraph-start
)
633 (concat "\\s-*$\\|" page-delimiter
))
634 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
635 (set (make-local-variable 'paragraph-ignore-fill-prefix
) t
)
636 (set (make-local-variable 'fill-paragraph-function
) 'octave-fill-paragraph
)
637 ;; FIXME: Why disable it?
638 ;; (set (make-local-variable 'adaptive-fill-regexp) nil)
639 ;; Again, this is not a property of the language, don't set it here.
640 ;; (set (make-local-variable 'fill-column) 72)
641 (set (make-local-variable 'normal-auto-fill-function
) 'octave-auto-fill
)
643 (set (make-local-variable 'font-lock-defaults
)
644 '(octave-font-lock-keywords))
646 (set (make-local-variable 'syntax-propertize-function
)
647 #'octave-syntax-propertize-function
)
649 (set (make-local-variable 'imenu-generic-expression
)
650 octave-mode-imenu-generic-expression
)
651 (set (make-local-variable 'imenu-case-fold-search
) nil
)
653 (add-hook 'completion-at-point-functions
654 'octave-completion-at-point-function nil t
)
655 (set (make-local-variable 'beginning-of-defun-function
)
656 'octave-beginning-of-defun
)
658 (easy-menu-add octave-mode-menu
)
659 (octave-initialize-completions))
661 ;;; Miscellaneous useful functions
663 (defsubst octave-in-comment-p
()
664 "Return t if point is inside an Octave comment."
665 (nth 4 (syntax-ppss)))
667 (defsubst octave-in-string-p
()
668 "Return t if point is inside an Octave string."
669 (nth 3 (syntax-ppss)))
671 (defsubst octave-not-in-string-or-comment-p
()
672 "Return t if point is not inside an Octave string or comment."
673 (let ((pps (syntax-ppss)))
674 (not (or (nth 3 pps
) (nth 4 pps
)))))
677 (defun octave-looking-at-kw (regexp)
678 "Like `looking-at', but sets `case-fold-search' nil."
679 (let ((case-fold-search nil
))
680 (looking-at regexp
)))
682 (defun octave-maybe-insert-continuation-string ()
683 (if (or (octave-in-comment-p)
686 (looking-at octave-continuation-regexp
)))
688 (delete-horizontal-space)
689 (insert (concat " " octave-continuation-string
))))
693 (defun octave-indent-new-comment-line ()
694 "Break Octave line at point, continuing comment if within one.
695 If within code, insert `octave-continuation-string' before breaking the
696 line. If within a string, signal an error.
697 The new line is properly indented."
699 (delete-horizontal-space)
701 ((octave-in-comment-p)
702 (indent-new-comment-line))
703 ((octave-in-string-p)
704 (error "Cannot split a code line inside a string"))
706 (insert (concat " " octave-continuation-string
))
707 (reindent-then-newline-and-indent))))
709 (defun octave-indent-defun ()
710 "Properly indent the Octave function which contains point."
714 (message "Indenting function...")
715 (indent-region (point) (mark) nil
))
716 (message "Indenting function...done."))
720 (defun octave-next-code-line (&optional arg
)
721 "Move ARG lines of Octave code forward (backward if ARG is negative).
722 Skips past all empty and comment lines. Default for ARG is 1.
724 On success, return 0. Otherwise, go as far as possible and return -1."
726 (or arg
(setq arg
1))
729 (inc (if (> arg
0) 1 -
1)))
730 (while (and (/= arg
0) (= n
0))
731 (setq n
(forward-line inc
))
733 (looking-at "\\s-*\\($\\|\\s<\\)"))
734 (setq n
(forward-line inc
)))
735 (setq arg
(- arg inc
)))
738 (defun octave-previous-code-line (&optional arg
)
739 "Move ARG lines of Octave code backward (forward if ARG is negative).
740 Skips past all empty and comment lines. Default for ARG is 1.
742 On success, return 0. Otherwise, go as far as possible and return -1."
744 (or arg
(setq arg
1))
745 (octave-next-code-line (- arg
)))
747 (defun octave-beginning-of-line ()
748 "Move point to beginning of current Octave line.
749 If on an empty or comment line, go to the beginning of that line.
750 Otherwise, move backward to the beginning of the first Octave code line
751 which is not inside a continuation statement, i.e., which does not
752 follow a code line ending in `...' or `\\', or is inside an open
756 (if (not (looking-at "\\s-*\\($\\|\\s<\\)"))
757 (while (or (condition-case nil
763 (and (or (looking-at "\\s-*\\($\\|\\s<\\)")
765 (if (zerop (octave-previous-code-line))
766 (looking-at octave-continuation-regexp
))))
767 (zerop (forward-line -
1)))))))
769 (defun octave-end-of-line ()
770 "Move point to end of current Octave line.
771 If on an empty or comment line, go to the end of that line.
772 Otherwise, move forward to the end of the first Octave code line which
773 does not end in `...' or `\\' or is inside an open parenthesis list."
778 (looking-at "\\s-*\\($\\|\\s<\\)"))
780 (while (or (condition-case nil
788 (or (looking-at "\\s-*\\($\\|\\s<\\)")
789 (looking-at octave-continuation-regexp
)))
790 (zerop (forward-line 1)))))
793 (defun octave-mark-block ()
794 "Put point at the beginning of this Octave block, mark at the end.
795 The block marked is the one that contains point or follows point."
797 (if (and (looking-at "\\sw\\|\\s_")
798 (looking-back "\\sw\\|\\s_" (1- (point))))
799 (skip-syntax-forward "w_"))
800 (unless (or (looking-at "\\s(")
802 (let* ((token (funcall smie-forward-token-function
))
803 (level (assoc token smie-grammar
)))
804 (and level
(not (numberp (cadr level
)))))))
805 (backward-up-list 1))
808 (defun octave-beginning-of-defun (&optional arg
)
809 "Move backward to the beginning of an Octave function.
810 With positive ARG, do it that many times. Negative argument -N means
811 move forward to Nth following beginning of a function.
812 Returns t unless search stops at the beginning or end of the buffer."
813 (let* ((arg (or arg
1))
814 (inc (if (> arg
0) 1 -
1))
816 (case-fold-search nil
))
818 (not (and (> arg
0) (looking-at "\\_<function\\_>")))
819 (skip-syntax-forward "w"))
820 (while (and (/= arg
0)
822 (re-search-backward "\\_<function\\_>" inc
)))
823 (if (octave-not-in-string-or-comment-p)
824 (setq arg
(- arg inc
))))
827 (and (< inc
0) (goto-char (match-beginning 0)))
832 (defun octave-auto-fill ()
833 "Perform auto-fill in Octave mode.
834 Returns nil if no feasible place to break the line could be found, and t
837 (if (or (null (setq fc
(current-fill-column)))
840 (and auto-fill-inhibit-regexp
841 (octave-looking-at-kw auto-fill-inhibit-regexp
))))
842 nil
; Can't do anything
843 (if (and (not (octave-in-comment-p))
844 (> (current-column) fc
))
845 (setq fc
(- fc
(+ (length octave-continuation-string
) 1))))
846 (while (and (not give-up
) (> (current-column) fc
))
847 (let* ((opoint (point))
850 (move-to-column (+ fc
1))
851 (skip-chars-backward "^ \t\n")
852 ;; If we're at the beginning of the line, break after
855 (re-search-forward "[ \t]" opoint t
))
856 ;; If we're in a comment line, don't break after the
859 (skip-syntax-backward " <")
861 (re-search-forward "[ \t]" (line-end-position)
863 ;; If we're not in a comment line and just ahead the
864 ;; continuation string, don't break here.
865 (if (and (not (octave-in-comment-p))
869 octave-continuation-string
)
872 (skip-chars-backward " \t")
876 (not (or (bolp) (eolp))))
877 (let ((prev-column (current-column)))
879 (skip-chars-backward " \t")
882 (octave-maybe-insert-continuation-string)
883 (indent-new-comment-line t
))
886 (octave-maybe-insert-continuation-string)
887 (indent-new-comment-line t
)))
888 (if (>= (current-column) prev-column
)
893 (defun octave-fill-paragraph (&optional _arg
)
894 "Fill paragraph of Octave code, handling Octave comments."
895 ;; FIXME: difference with generic fill-paragraph:
896 ;; - code lines are only split, never joined.
897 ;; - \n that end comments are never removed.
898 ;; - insert continuation marker when splitting code lines.
901 (let ((end (progn (forward-paragraph) (copy-marker (point) t
)))
903 (forward-paragraph -
1)
904 (skip-chars-forward " \t\n")
907 (cfc (current-fill-column))
910 (while (< (point) end
)
912 (indent-according-to-mode)
915 ;; First check whether we need to combine non-empty comment lines
916 (if (and (< (current-column) cfc
)
917 (octave-in-comment-p)
920 (looking-at "^\\s-*\\s<+\\s-*$"))))
921 ;; This is a nonempty comment line which does not extend
922 ;; past the fill column. If it is followed by a nonempty
923 ;; comment line with the same comment prefix, try to
924 ;; combine them, and repeat this until either we reach the
925 ;; fill-column or there is nothing more to combine.
927 ;; Get the comment prefix
930 (while (and (re-search-forward "\\s<+")
931 (not (octave-in-comment-p))))
932 (setq comment-prefix
(match-string 0)))
933 ;; And keep combining ...
934 (while (and (< (current-column) cfc
)
946 (re-search-forward comment-prefix
)
947 (delete-region (match-beginning 0) (match-end 0))
949 (move-to-column cfc
))))
950 ;; We might also try to combine continued code lines> Perhaps
951 ;; some other time ...
952 (skip-chars-forward "^ \t\n")
953 (delete-horizontal-space)
954 (if (or (< (current-column) cfc
)
955 (and (= (current-column) cfc
) (eolp)))
957 (if (not (eolp)) (insert " "))
958 (or (octave-auto-fill)
964 (defun octave-initialize-completions ()
965 "Create an alist for Octave completions."
966 (if octave-completion-alist
968 (setq octave-completion-alist
969 (append octave-reserved-words
970 octave-text-functions
973 (defun octave-completion-at-point-function ()
974 "Find the text to complete and the corresponding table."
975 (let* ((beg (save-excursion (skip-syntax-backward "w_") (point)))
978 ;; Extend region past point, if applicable.
979 (save-excursion (skip-syntax-forward "w_")
981 (list beg end octave-completion-alist
)))
983 (define-obsolete-function-alias 'octave-complete-symbol
984 'completion-at-point
"24.1")
986 ;;; Electric characters && friends
988 (defun octave-abbrev-start ()
989 "Start entering an Octave abbreviation.
990 If Abbrev mode is turned on, typing ` (grave accent) followed by ? or
991 \\[help-command] lists all Octave abbrevs. Any other key combination is
993 Note that all Octave mode abbrevs start with a grave accent."
995 (self-insert-command 1)
997 (set-temporary-overlay-map
998 (let ((map (make-sparse-keymap)))
999 (define-key map
[??
] 'list-abbrevs
)
1000 (define-key map
(vector help-char
) 'list-abbrevs
)
1003 (define-skeleton octave-insert-defun
1004 "Insert an Octave function skeleton.
1005 Prompt for the function's name, arguments and return values (to be
1006 entered without parens)."
1007 (let* ((defname (substring (buffer-name) 0 -
2))
1008 (name (read-string (format "Function name (default %s): " defname
)
1010 (args (read-string "Arguments: "))
1011 (vals (read-string "Return values: ")))
1014 ((string-equal vals
"") vals
)
1015 ((string-match "[ ,]" vals
) (concat "[" vals
"] = "))
1016 (t (concat vals
" = ")))
1019 \n "function " > str
\n \n
1020 octave-block-comment-start
"usage: " str
\n
1021 octave-block-comment-start
\n octave-block-comment-start
1025 ;;; Communication with the inferior Octave process
1026 (defun octave-kill-process ()
1027 "Kill inferior Octave process and its buffer."
1029 (if inferior-octave-process
1031 (process-send-string inferior-octave-process
"quit;\n")
1032 (accept-process-output inferior-octave-process
)))
1033 (if inferior-octave-buffer
1034 (kill-buffer inferior-octave-buffer
)))
1036 (defun octave-show-process-buffer ()
1037 "Make sure that `inferior-octave-buffer' is displayed."
1039 (if (get-buffer inferior-octave-buffer
)
1040 (display-buffer inferior-octave-buffer
)
1041 (message "No buffer named %s" inferior-octave-buffer
)))
1043 (defun octave-hide-process-buffer ()
1044 "Delete all windows that display `inferior-octave-buffer'."
1046 (if (get-buffer inferior-octave-buffer
)
1047 (delete-windows-on inferior-octave-buffer
)
1048 (message "No buffer named %s" inferior-octave-buffer
)))
1050 (defun octave-send-region (beg end
)
1051 "Send current region to the inferior Octave process."
1054 (let ((proc inferior-octave-process
)
1055 (string (buffer-substring-no-properties beg end
))
1057 (with-current-buffer inferior-octave-buffer
1058 (setq inferior-octave-output-list nil
)
1059 (while (not (string-equal string
""))
1060 (if (string-match "\n" string
)
1061 (setq line
(substring string
0 (match-beginning 0))
1062 string
(substring string
(match-end 0)))
1063 (setq line string string
""))
1064 (setq inferior-octave-receive-in-progress t
)
1065 (inferior-octave-send-list-and-digest (list (concat line
"\n")))
1066 (while inferior-octave-receive-in-progress
1067 (accept-process-output proc
))
1068 (insert-before-markers
1069 (mapconcat 'identity
1071 (if octave-send-echo-input
(list line
) (list ""))
1072 (mapcar 'inferior-octave-strip-ctrl-g
1073 inferior-octave-output-list
)
1074 (list inferior-octave-output-string
))
1076 (if octave-send-show-buffer
1077 (display-buffer inferior-octave-buffer
)))
1079 (defun octave-send-block ()
1080 "Send current Octave block to the inferior Octave process."
1084 (octave-send-region (point) (mark))))
1086 (defun octave-send-defun ()
1087 "Send current Octave function to the inferior Octave process."
1091 (octave-send-region (point) (mark))))
1093 (defun octave-send-line (&optional arg
)
1094 "Send current Octave code line to the inferior Octave process.
1095 With positive prefix ARG, send that many lines.
1096 If `octave-send-line-auto-forward' is non-nil, go to the next unsent
1099 (or arg
(setq arg
1))
1104 (octave-next-code-line (- arg
1))
1107 (if octave-send-line-auto-forward
1108 (octave-next-code-line 1))
1109 (octave-send-region beg end
))))
1111 (defun octave-eval-print-last-sexp ()
1112 "Evaluate Octave sexp before point and print value into current buffer."
1115 (let ((standard-output (current-buffer))
1116 (print-escape-newlines nil
)
1122 (inferior-octave-send-list-and-digest
1123 (list (concat (buffer-substring-no-properties (point) opoint
)
1125 (mapconcat 'identity inferior-octave-output-list
"\n")))
1129 (defun octave-submit-bug-report ()
1130 "Submit a bug report on the Emacs Octave package via mail."
1134 (y-or-n-p "Do you want to submit a bug report? ")
1135 (reporter-submit-bug-report
1136 octave-maintainer-address
1137 (concat "Emacs version " emacs-version
)
1139 'octave-blink-matching-block
1140 'octave-block-offset
1141 'octave-comment-char
1142 'octave-continuation-offset
1143 'octave-continuation-string
1144 'octave-send-echo-input
1145 'octave-send-line-auto-forward
1146 'octave-send-show-buffer
))))
1150 (provide 'octave-mod
)
1152 ;;; octave-mod.el ends here