* progmodes/octave.el (inferior-octave-prompt-read-only): New user
[emacs.git] / lisp / progmodes / octave.el
blob32d265d2e80f8f0b483092f0389c1bc0e5fbec42
1 ;;; octave.el --- editing octave source files under emacs -*- lexical-binding: t; -*-
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>
7 ;; Maintainer: FSF
8 ;; Keywords: languages
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/>.
25 ;;; Commentary:
27 ;; This package provides emacs support for octave. It defines a major
28 ;; mode for editing octave code and contains code for interacting with
29 ;; an inferior octave process using comint.
31 ;; See the documentation of `octave-mode' and `run-octave' for further
32 ;; information on usage and customization.
34 ;;; Code:
35 (require 'comint)
37 (defgroup octave nil
38 "Editing Octave code."
39 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
40 :group 'languages)
42 (define-obsolete-function-alias 'octave-submit-bug-report
43 'report-emacs-bug "24.4")
45 (define-abbrev-table 'octave-abbrev-table
46 (mapcar (lambda (e) (append e '(nil 0 t)))
47 '(("`a" "all_va_args")
48 ("`b" "break")
49 ("`cs" "case")
50 ("`ca" "catch")
51 ("`c" "continue")
52 ("`el" "else")
53 ("`eli" "elseif")
54 ("`et" "end_try_catch")
55 ("`eu" "end_unwind_protect")
56 ("`ef" "endfor")
57 ("`efu" "endfunction")
58 ("`ei" "endif")
59 ("`es" "endswitch")
60 ("`ew" "endwhile")
61 ("`f" "for")
62 ("`fu" "function")
63 ("`gl" "global")
64 ("`gp" "gplot")
65 ("`gs" "gsplot")
66 ("`if" "if ()")
67 ("`o" "otherwise")
68 ("`rp" "replot")
69 ("`r" "return")
70 ("`s" "switch")
71 ("`t" "try")
72 ("`u" "until ()")
73 ("`up" "unwind_protect")
74 ("`upc" "unwind_protect_cleanup")
75 ("`w" "while ()")))
76 "Abbrev table for Octave's reserved words.
77 Used in `octave-mode' and `inferior-octave-mode' buffers.
78 All Octave abbrevs start with a grave accent (`)."
79 :regexp "\\(?:[^`]\\|^\\)\\(\\(?:\\<\\|`\\)\\w+\\)\\W*")
81 (defvar octave-comment-char ?#
82 "Character to start an Octave comment.")
83 (defvar octave-comment-start
84 (string octave-comment-char ?\s)
85 "String to insert to start a new Octave in-line comment.")
86 (defvar octave-comment-start-skip "\\s<+\\s-*"
87 "Regexp to match the start of an Octave comment up to its body.")
89 (defvar octave-begin-keywords
90 '("do" "for" "function" "if" "switch" "try" "unwind_protect" "while"))
91 (defvar octave-else-keywords
92 '("case" "catch" "else" "elseif" "otherwise" "unwind_protect_cleanup"))
93 (defvar octave-end-keywords
94 '("endfor" "endfunction" "endif" "endswitch" "end_try_catch"
95 "end_unwind_protect" "endwhile" "until" "end"))
97 (defvar octave-reserved-words
98 (append octave-begin-keywords
99 octave-else-keywords
100 octave-end-keywords
101 '("break" "continue" "end" "global" "persistent" "return"))
102 "Reserved words in Octave.")
104 (defvar octave-text-functions
105 '("casesen" "cd" "chdir" "clear" "diary" "dir" "document" "echo"
106 "edit_history" "format" "help" "history" "hold"
107 "load" "ls" "more" "run_history" "save" "type"
108 "which" "who" "whos")
109 "Text functions in Octave.")
111 (defvar octave-function-header-regexp
112 (concat "^\\s-*\\_<\\(function\\)\\_>"
113 "\\([^=;\n]*=[ \t]*\\|[ \t]*\\)\\(\\(?:\\w\\|\\s_\\)+\\)\\_>")
114 "Regexp to match an Octave function header.
115 The string `function' and its name are given by the first and third
116 parenthetical grouping.")
119 (defvar octave-font-lock-keywords
120 (list
121 ;; Fontify all builtin keywords.
122 (cons (concat "\\_<\\("
123 (regexp-opt (append octave-reserved-words
124 octave-text-functions))
125 "\\)\\_>")
126 'font-lock-keyword-face)
127 ;; Note: 'end' also serves as the last index in an indexing expression.
128 ;; Ref: http://www.mathworks.com/help/matlab/ref/end.html
129 '("\\_<end\\_>" (0 (save-excursion
130 (condition-case nil
131 (progn
132 (goto-char (match-beginning 0))
133 (backward-up-list)
134 (unless (eq (char-after) ?\()
135 font-lock-keyword-face))
136 (error font-lock-keyword-face)))
138 ;; Fontify all builtin operators.
139 (cons "\\(&\\||\\|<=\\|>=\\|==\\|<\\|>\\|!=\\|!\\)"
140 (if (boundp 'font-lock-builtin-face)
141 'font-lock-builtin-face
142 'font-lock-preprocessor-face))
143 ;; Fontify all function declarations.
144 (list octave-function-header-regexp
145 '(1 font-lock-keyword-face)
146 '(3 font-lock-function-name-face nil t)))
147 "Additional Octave expressions to highlight.")
149 (defun octave-syntax-propertize-function (start end)
150 (goto-char start)
151 (octave-syntax-propertize-sqs end)
152 (funcall (syntax-propertize-rules
153 ;; Try to distinguish the string-quotes from the transpose-quotes.
154 ("[[({,; ]\\('\\)"
155 (1 (prog1 "\"'" (octave-syntax-propertize-sqs end)))))
156 (point) end))
158 (defun octave-syntax-propertize-sqs (end)
159 "Propertize the content/end of single-quote strings."
160 (when (eq (nth 3 (syntax-ppss)) ?\')
161 ;; A '..' string.
162 (when (re-search-forward
163 "\\(?:\\=\\|[^']\\)\\(?:''\\)*\\('\\)\\($\\|[^']\\)" end 'move)
164 (goto-char (match-beginning 2))
165 (when (eq (char-before (match-beginning 1)) ?\\)
166 ;; Backslash cannot escape a single quote.
167 (put-text-property (1- (match-beginning 1)) (match-beginning 1)
168 'syntax-table (string-to-syntax ".")))
169 (put-text-property (match-beginning 1) (match-end 1)
170 'syntax-table (string-to-syntax "\"'")))))
173 (defvar octave-mode-map
174 (let ((map (make-sparse-keymap)))
175 (define-key map "`" 'octave-abbrev-start)
176 (define-key map "\e\n" 'octave-indent-new-comment-line)
177 (define-key map "\M-\C-q" 'octave-indent-defun)
178 (define-key map "\C-c\C-p" 'octave-previous-code-line)
179 (define-key map "\C-c\C-n" 'octave-next-code-line)
180 (define-key map "\C-c\C-a" 'octave-beginning-of-line)
181 (define-key map "\C-c\C-e" 'octave-end-of-line)
182 (define-key map [remap down-list] 'smie-down-list)
183 (define-key map "\C-c\M-\C-h" 'octave-mark-block)
184 (define-key map "\C-c]" 'smie-close-block)
185 (define-key map "\C-c/" 'smie-close-block)
186 (define-key map "\C-c;" 'octave-update-function-file-comment)
187 (define-key map "\C-c\C-f" 'octave-insert-defun)
188 (define-key map "\C-c\C-il" 'octave-send-line)
189 (define-key map "\C-c\C-ib" 'octave-send-block)
190 (define-key map "\C-c\C-if" 'octave-send-defun)
191 (define-key map "\C-c\C-ir" 'octave-send-region)
192 (define-key map "\C-c\C-is" 'octave-show-process-buffer)
193 (define-key map "\C-c\C-iq" 'octave-hide-process-buffer)
194 (define-key map "\C-c\C-ik" 'octave-kill-process)
195 (define-key map "\C-c\C-i\C-l" 'octave-send-line)
196 (define-key map "\C-c\C-i\C-b" 'octave-send-block)
197 (define-key map "\C-c\C-i\C-f" 'octave-send-defun)
198 (define-key map "\C-c\C-i\C-r" 'octave-send-region)
199 (define-key map "\C-c\C-i\C-s" 'octave-show-process-buffer)
200 (define-key map "\C-c\C-i\C-q" 'octave-hide-process-buffer)
201 (define-key map "\C-c\C-i\C-k" 'octave-kill-process)
202 map)
203 "Keymap used in Octave mode.")
207 (easy-menu-define octave-mode-menu octave-mode-map
208 "Menu for Octave mode."
209 '("Octave"
210 ("Lines"
211 ["Previous Code Line" octave-previous-code-line t]
212 ["Next Code Line" octave-next-code-line t]
213 ["Begin of Continuation" octave-beginning-of-line t]
214 ["End of Continuation" octave-end-of-line t]
215 ["Split Line at Point" octave-indent-new-comment-line t])
216 ("Blocks"
217 ["Mark Block" octave-mark-block t]
218 ["Close Block" smie-close-block t])
219 ("Functions"
220 ["Indent Function" octave-indent-defun t]
221 ["Insert Function" octave-insert-defun t]
222 ["Update function file comment" octave-update-function-file-comment t])
224 ("Debug"
225 ["Send Current Line" octave-send-line t]
226 ["Send Current Block" octave-send-block t]
227 ["Send Current Function" octave-send-defun t]
228 ["Send Region" octave-send-region t]
229 ["Show Process Buffer" octave-show-process-buffer t]
230 ["Hide Process Buffer" octave-hide-process-buffer t]
231 ["Kill Process" octave-kill-process t])
233 ["Indent Line" indent-according-to-mode t]
234 ["Complete Symbol" completion-at-point t]
236 ["Toggle Abbrev Mode" abbrev-mode
237 :style toggle :selected abbrev-mode]
238 ["Toggle Auto-Fill Mode" auto-fill-mode
239 :style toggle :selected auto-fill-function]
241 ["Describe Octave Mode" describe-mode t]
242 ["Lookup Octave Index" info-lookup-symbol t]
243 ["Customize Octave" (customize-group 'octave) t]
245 ["Submit Bug Report" report-emacs-bug t]))
247 (defvar octave-mode-syntax-table
248 (let ((table (make-syntax-table)))
249 (modify-syntax-entry ?\r " " table)
250 (modify-syntax-entry ?+ "." table)
251 (modify-syntax-entry ?- "." table)
252 (modify-syntax-entry ?= "." table)
253 (modify-syntax-entry ?* "." table)
254 (modify-syntax-entry ?/ "." table)
255 (modify-syntax-entry ?> "." table)
256 (modify-syntax-entry ?< "." table)
257 (modify-syntax-entry ?& "." table)
258 (modify-syntax-entry ?| "." table)
259 (modify-syntax-entry ?! "." table)
260 (modify-syntax-entry ?\\ "\\" table)
261 (modify-syntax-entry ?\' "." table)
262 ;; Was "w" for abbrevs, but now that it's not necessary any more,
263 (modify-syntax-entry ?\` "." table)
264 (modify-syntax-entry ?\" "\"" table)
265 (modify-syntax-entry ?. "_" table)
266 (modify-syntax-entry ?_ "_" table)
267 ;; The "b" flag only applies to the second letter of the comstart
268 ;; and the first letter of the comend, i.e. the "4b" below is ineffective.
269 ;; If we try to put `b' on the single-line comments, we get a similar
270 ;; problem where the % and # chars appear as first chars of the 2-char
271 ;; comend, so the multi-line ender is also turned into style-b.
272 ;; So we need the new "c" comment style.
273 (modify-syntax-entry ?\% "< 13" table)
274 (modify-syntax-entry ?\# "< 13" table)
275 (modify-syntax-entry ?\{ "(} 2c" table)
276 (modify-syntax-entry ?\} "){ 4c" table)
277 (modify-syntax-entry ?\n ">" table)
278 table)
279 "Syntax table in use in `octave-mode' buffers.")
281 (defcustom octave-font-lock-texinfo-comment t
282 "Control whether to highlight the texinfo comment block."
283 :type 'boolean
284 :group 'octave
285 :version "24.4")
287 (defcustom octave-blink-matching-block t
288 "Control the blinking of matching Octave block keywords.
289 Non-nil means show matching begin of block when inserting a space,
290 newline or semicolon after an else or end keyword."
291 :type 'boolean
292 :group 'octave)
294 (defcustom octave-block-offset 2
295 "Extra indentation applied to statements in Octave block structures."
296 :type 'integer
297 :group 'octave)
299 (defvar octave-block-comment-start
300 (concat (make-string 2 octave-comment-char) " ")
301 "String to insert to start a new Octave comment on an empty line.")
303 (defcustom octave-continuation-offset 4
304 "Extra indentation applied to Octave continuation lines."
305 :type 'integer
306 :group 'octave)
308 (eval-and-compile
309 (defconst octave-continuation-marker-regexp "\\\\\\|\\.\\.\\."))
311 (defvar octave-continuation-regexp
312 (concat "[^#%\n]*\\(" octave-continuation-marker-regexp
313 "\\)\\s-*\\(\\s<.*\\)?$"))
315 ;; Char \ is considered a bad decision for continuing a line.
316 (defconst octave-continuation-string "..."
317 "Character string used for Octave continuation lines.")
319 (defvar octave-mode-imenu-generic-expression
320 (list
321 ;; Functions
322 (list nil octave-function-header-regexp 3))
323 "Imenu expression for Octave mode. See `imenu-generic-expression'.")
325 (defcustom octave-mode-hook nil
326 "Hook to be run when Octave mode is started."
327 :type 'hook
328 :group 'octave)
330 (defcustom octave-send-show-buffer t
331 "Non-nil means display `inferior-octave-buffer' after sending to it."
332 :type 'boolean
333 :group 'octave)
334 (defcustom octave-send-line-auto-forward t
335 "Control auto-forward after sending to the inferior Octave process.
336 Non-nil means always go to the next Octave code line after sending."
337 :type 'boolean
338 :group 'octave)
339 (defcustom octave-send-echo-input t
340 "Non-nil means echo input sent to the inferior Octave process."
341 :type 'boolean
342 :group 'octave)
345 ;;; SMIE indentation
347 (require 'smie)
349 (defconst octave-operator-table
350 '((assoc ";" "\n") (assoc ",") ; The doc claims they have equal precedence!?
351 (right "=" "+=" "-=" "*=" "/=")
352 (assoc "&&") (assoc "||") ; The doc claims they have equal precedence!?
353 (assoc "&") (assoc "|") ; The doc claims they have equal precedence!?
354 (nonassoc "<" "<=" "==" ">=" ">" "!=" "~=")
355 (nonassoc ":") ;No idea what this is.
356 (assoc "+" "-")
357 (assoc "*" "/" "\\" ".\\" ".*" "./")
358 (nonassoc "'" ".'")
359 (nonassoc "++" "--" "!" "~") ;And unary "+" and "-".
360 (right "^" "**" ".^" ".**")
361 ;; It's not really an operator, but for indentation purposes it
362 ;; could be convenient to treat it as one.
363 (assoc "...")))
365 (defconst octave-smie-bnf-table
366 '((atom)
367 ;; We can't distinguish the first element in a sequence with
368 ;; precedence grammars, so we can't distinguish the condition
369 ;; if the `if' from the subsequent body, for example.
370 ;; This has to be done later in the indentation rules.
371 (exp (exp "\n" exp)
372 ;; We need to mention at least one of the operators in this part
373 ;; of the grammar: if the BNF and the operator table have
374 ;; no overlap, SMIE can't know how they relate.
375 (exp ";" exp)
376 ("try" exp "catch" exp "end_try_catch")
377 ("try" exp "catch" exp "end")
378 ("unwind_protect" exp
379 "unwind_protect_cleanup" exp "end_unwind_protect")
380 ("unwind_protect" exp "unwind_protect_cleanup" exp "end")
381 ("for" exp "endfor")
382 ("for" exp "end")
383 ("do" exp "until" atom)
384 ("while" exp "endwhile")
385 ("while" exp "end")
386 ("if" exp "endif")
387 ("if" exp "else" exp "endif")
388 ("if" exp "elseif" exp "else" exp "endif")
389 ("if" exp "elseif" exp "elseif" exp "else" exp "endif")
390 ("if" exp "elseif" exp "elseif" exp "else" exp "end")
391 ("switch" exp "case" exp "endswitch")
392 ("switch" exp "case" exp "otherwise" exp "endswitch")
393 ("switch" exp "case" exp "case" exp "otherwise" exp "endswitch")
394 ("switch" exp "case" exp "case" exp "otherwise" exp "end")
395 ("function" exp "endfunction")
396 ("function" exp "end"))
397 ;; (fundesc (atom "=" atom))
400 (defconst octave-smie-grammar
401 (smie-prec2->grammar
402 (smie-merge-prec2s
403 (smie-bnf->prec2 octave-smie-bnf-table
404 '((assoc "\n" ";")))
406 (smie-precs->prec2 octave-operator-table))))
408 ;; Tokenizing needs to be refined so that ";;" is treated as two
409 ;; tokens and also so as to recognize the \n separator (and
410 ;; corresponding continuation lines).
412 (defconst octave-operator-regexp
413 (regexp-opt (apply 'append (mapcar 'cdr octave-operator-table))))
415 (defun octave-smie-backward-token ()
416 (let ((pos (point)))
417 (forward-comment (- (point)))
418 (cond
419 ((and (not (eq (char-before) ?\;)) ;Coalesce ";" and "\n".
420 (> pos (line-end-position))
421 (if (looking-back octave-continuation-marker-regexp (- (point) 3))
422 (progn
423 (goto-char (match-beginning 0))
424 (forward-comment (- (point)))
425 nil)
427 ;; Ignore it if it's within parentheses.
428 (let ((ppss (syntax-ppss)))
429 (not (and (nth 1 ppss)
430 (eq ?\( (char-after (nth 1 ppss)))))))
431 (skip-chars-forward " \t")
432 ;; Why bother distinguishing \n and ;?
433 ";") ;;"\n"
434 ((and (looking-back octave-operator-regexp (- (point) 3) 'greedy)
435 ;; Don't mistake a string quote for a transpose.
436 (not (looking-back "\\s\"" (1- (point)))))
437 (goto-char (match-beginning 0))
438 (match-string-no-properties 0))
440 (smie-default-backward-token)))))
442 (defun octave-smie-forward-token ()
443 (skip-chars-forward " \t")
444 (when (looking-at (eval-when-compile
445 (concat "\\(" octave-continuation-marker-regexp
446 "\\)[ \t]*\\($\\|[%#]\\)")))
447 (goto-char (match-end 1))
448 (forward-comment 1))
449 (cond
450 ((and (looking-at "$\\|[%#]")
451 ;; Ignore it if it's within parentheses or if the newline does not end
452 ;; some preceding text.
453 (prog1 (and (not (smie-rule-bolp))
454 (let ((ppss (syntax-ppss)))
455 (not (and (nth 1 ppss)
456 (eq ?\( (char-after (nth 1 ppss)))))))
457 (forward-comment (point-max))))
458 ;; Why bother distinguishing \n and ;?
459 ";") ;;"\n"
460 ((looking-at ";[ \t]*\\($\\|[%#]\\)")
461 ;; Combine the ; with the subsequent \n.
462 (goto-char (match-beginning 1))
463 (forward-comment 1)
464 ";")
465 ((and (looking-at octave-operator-regexp)
466 ;; Don't mistake a string quote for a transpose.
467 (not (looking-at "\\s\"")))
468 (goto-char (match-end 0))
469 (match-string-no-properties 0))
471 (smie-default-forward-token))))
473 (defun octave-smie-rules (kind token)
474 (pcase (cons kind token)
475 ;; We could set smie-indent-basic instead, but that would have two
476 ;; disadvantages:
477 ;; - changes to octave-block-offset wouldn't take effect immediately.
478 ;; - edebug wouldn't show the use of this variable.
479 (`(:elem . basic) octave-block-offset)
480 ;; Since "case" is in the same BNF rules as switch..end, SMIE by default
481 ;; aligns it with "switch".
482 (`(:before . "case") (if (not (smie-rule-sibling-p)) octave-block-offset))
483 (`(:after . ";")
484 (if (smie-rule-parent-p "function" "if" "while" "else" "elseif" "for"
485 "otherwise" "case" "try" "catch" "unwind_protect"
486 "unwind_protect_cleanup")
487 (smie-rule-parent octave-block-offset)
488 ;; For (invalid) code between switch and case.
489 ;; (if (smie-parent-p "switch") 4)
490 0))))
492 (defvar electric-layout-rules)
494 ;;;###autoload
495 (define-derived-mode octave-mode prog-mode "Octave"
496 "Major mode for editing Octave code.
498 Octave is a high-level language, primarily intended for numerical
499 computations. It provides a convenient command line interface
500 for solving linear and nonlinear problems numerically. Function
501 definitions can also be stored in files and used in batch mode."
502 (setq local-abbrev-table octave-abbrev-table)
504 (smie-setup octave-smie-grammar #'octave-smie-rules
505 :forward-token #'octave-smie-forward-token
506 :backward-token #'octave-smie-backward-token)
507 (setq-local smie-indent-basic 'octave-block-offset)
509 (setq-local smie-blink-matching-triggers
510 (cons ?\; smie-blink-matching-triggers))
511 (unless octave-blink-matching-block
512 (remove-hook 'post-self-insert-hook #'smie-blink-matching-open 'local))
514 (setq-local electric-indent-chars
515 (cons ?\; electric-indent-chars))
516 ;; IIUC matlab-mode takes the opposite approach: it makes RET insert
517 ;; a ";" at those places where it's correct (i.e. outside of parens).
518 (setq-local electric-layout-rules '((?\; . after)))
520 (setq-local comment-start octave-comment-start)
521 (setq-local comment-end "")
522 ;; Don't set it here: it's not really a property of the language,
523 ;; just a personal preference of the author.
524 ;; (setq-local comment-column 32)
525 (setq-local comment-start-skip "\\s<+\\s-*")
526 (setq-local comment-add 1)
528 (setq-local parse-sexp-ignore-comments t)
529 (setq-local paragraph-start (concat "\\s-*$\\|" page-delimiter))
530 (setq-local paragraph-separate paragraph-start)
531 (setq-local paragraph-ignore-fill-prefix t)
532 (setq-local fill-paragraph-function 'octave-fill-paragraph)
533 ;; FIXME: Why disable it?
534 ;; (setq-local adaptive-fill-regexp nil)
535 ;; Again, this is not a property of the language, don't set it here.
536 ;; (setq fill-column 72)
537 (setq-local normal-auto-fill-function 'octave-auto-fill)
539 (setq font-lock-defaults '(octave-font-lock-keywords))
541 (setq-local syntax-propertize-function #'octave-syntax-propertize-function)
543 (setq imenu-generic-expression octave-mode-imenu-generic-expression)
544 (setq imenu-case-fold-search nil)
546 (add-hook 'completion-at-point-functions
547 'octave-completion-at-point-function nil t)
548 (add-hook 'before-save-hook 'octave-sync-function-file-names nil t)
549 (setq-local beginning-of-defun-function 'octave-beginning-of-defun)
550 (and octave-font-lock-texinfo-comment (octave-font-lock-texinfo-comment))
552 (easy-menu-add octave-mode-menu))
555 (defcustom inferior-octave-program "octave"
556 "Program invoked by `inferior-octave'."
557 :type 'string
558 :group 'octave)
560 (defcustom inferior-octave-buffer "*Inferior Octave*"
561 "Name of buffer for running an inferior Octave process."
562 :type 'string
563 :group 'octave)
565 (defcustom inferior-octave-prompt
566 "\\(^octave\\(\\|.bin\\|.exe\\)\\(-[.0-9]+\\)?\\(:[0-9]+\\)?\\|^debug\\|^\\)>+ "
567 "Regexp to match prompts for the inferior Octave process."
568 :type 'regexp
569 :group 'octave)
571 (defcustom inferior-octave-prompt-read-only t
572 "If non-nil, the Octave prompt is read only.
573 The read only region includes the newline before the prompt.
574 Setting this variable does not affect existing Octave runs.
575 This feature utilizes the `comint-prompt-read-only' variable."
576 :type 'boolean
577 :group 'octave
578 :version "24.4")
580 (defcustom inferior-octave-startup-file nil
581 "Name of the inferior Octave startup file.
582 The contents of this file are sent to the inferior Octave process on
583 startup."
584 :type '(choice (const :tag "None" nil)
585 file)
586 :group 'octave)
588 (defcustom inferior-octave-startup-args nil
589 "List of command line arguments for the inferior Octave process.
590 For example, for suppressing the startup message and using `traditional'
591 mode, set this to (\"-q\" \"--traditional\")."
592 :type '(repeat string)
593 :group 'octave)
595 (defcustom inferior-octave-mode-hook nil
596 "Hook to be run when Inferior Octave mode is started."
597 :type 'hook
598 :group 'octave)
600 (defvar inferior-octave-process nil)
602 (defvar inferior-octave-mode-map
603 (let ((map (make-sparse-keymap)))
604 (set-keymap-parent map comint-mode-map)
605 (define-key map "\t" 'comint-dynamic-complete)
606 (define-key map "\M-?" 'comint-dynamic-list-filename-completions)
607 (define-key map "\C-c\C-l" 'inferior-octave-dynamic-list-input-ring)
608 (define-key map [menu-bar inout list-history]
609 '("List Input History" . inferior-octave-dynamic-list-input-ring))
610 map)
611 "Keymap used in Inferior Octave mode.")
613 (defvar inferior-octave-mode-syntax-table
614 (let ((table (make-syntax-table octave-mode-syntax-table)))
615 table)
616 "Syntax table in use in inferior-octave-mode buffers.")
618 (defvar inferior-octave-font-lock-keywords
619 (list
620 (cons inferior-octave-prompt 'font-lock-type-face))
621 ;; Could certainly do more font locking in inferior Octave ...
622 "Additional expressions to highlight in Inferior Octave mode.")
625 ;;; Compatibility functions
626 (if (not (fboundp 'comint-line-beginning-position))
627 ;; comint-line-beginning-position is defined in Emacs 21
628 (defun comint-line-beginning-position ()
629 "Returns the buffer position of the beginning of the line, after any prompt.
630 The prompt is assumed to be any text at the beginning of the line matching
631 the regular expression `comint-prompt-regexp', a buffer local variable."
632 (save-excursion (comint-bol nil) (point))))
635 (defvar inferior-octave-output-list nil)
636 (defvar inferior-octave-output-string nil)
637 (defvar inferior-octave-receive-in-progress nil)
639 (define-obsolete-variable-alias 'inferior-octave-startup-hook
640 'inferior-octave-mode-hook "24.4")
642 (defvar inferior-octave-has-built-in-variables nil
643 "Non-nil means that Octave has built-in variables.")
645 (defvar inferior-octave-dynamic-complete-functions
646 '(inferior-octave-completion-at-point comint-filename-completion)
647 "List of functions called to perform completion for inferior Octave.
648 This variable is used to initialize `comint-dynamic-complete-functions'
649 in the Inferior Octave buffer.")
651 (defvar info-lookup-mode)
653 (define-derived-mode inferior-octave-mode comint-mode "Inferior Octave"
654 "Major mode for interacting with an inferior Octave process."
655 (setq comint-prompt-regexp inferior-octave-prompt
656 mode-line-process '(":%s")
657 local-abbrev-table octave-abbrev-table)
659 (setq-local comment-start octave-comment-start)
660 (setq-local comment-end "")
661 (setq comment-column 32)
662 (setq-local comment-start-skip octave-comment-start-skip)
664 (setq font-lock-defaults '(inferior-octave-font-lock-keywords nil nil))
666 (setq info-lookup-mode 'octave-mode)
668 (setq comint-input-ring-file-name
669 (or (getenv "OCTAVE_HISTFILE") "~/.octave_hist")
670 comint-input-ring-size (or (getenv "OCTAVE_HISTSIZE") 1024))
671 (setq-local comint-dynamic-complete-functions
672 inferior-octave-dynamic-complete-functions)
673 (setq-local comint-prompt-read-only inferior-octave-prompt-read-only)
674 (add-hook 'comint-input-filter-functions
675 'inferior-octave-directory-tracker nil t)
676 (comint-read-input-ring t))
678 ;;;###autoload
679 (defun inferior-octave (&optional arg)
680 "Run an inferior Octave process, I/O via `inferior-octave-buffer'.
681 This buffer is put in Inferior Octave mode. See `inferior-octave-mode'.
683 Unless ARG is non-nil, switches to this buffer.
685 The elements of the list `inferior-octave-startup-args' are sent as
686 command line arguments to the inferior Octave process on startup.
688 Additional commands to be executed on startup can be provided either in
689 the file specified by `inferior-octave-startup-file' or by the default
690 startup file, `~/.emacs-octave'."
691 (interactive "P")
692 (let ((buffer inferior-octave-buffer))
693 (get-buffer-create buffer)
694 (if (comint-check-proc buffer)
696 (with-current-buffer buffer
697 (comint-mode)
698 (inferior-octave-startup)
699 (inferior-octave-mode)))
700 (if (not arg)
701 (pop-to-buffer buffer))))
703 ;;;###autoload
704 (defalias 'run-octave 'inferior-octave)
706 (defun inferior-octave-startup ()
707 "Start an inferior Octave process."
708 (let ((proc (comint-exec-1
709 (substring inferior-octave-buffer 1 -1)
710 inferior-octave-buffer
711 inferior-octave-program
712 (append (list "-i" "--no-line-editing")
713 inferior-octave-startup-args))))
714 (set-process-filter proc 'inferior-octave-output-digest)
715 (setq comint-ptyp process-connection-type
716 inferior-octave-process proc
717 inferior-octave-output-list nil
718 inferior-octave-output-string nil
719 inferior-octave-receive-in-progress t)
721 ;; This may look complicated ... However, we need to make sure that
722 ;; we additional startup code only AFTER Octave is ready (otherwise,
723 ;; output may be mixed up). Hence, we need to digest the Octave
724 ;; output to see when it issues a prompt.
725 (while inferior-octave-receive-in-progress
726 (accept-process-output inferior-octave-process))
727 (goto-char (point-max))
728 (set-marker (process-mark proc) (point))
729 (insert-before-markers
730 (concat
731 (if (not (bobp)) "\f\n")
732 (if inferior-octave-output-list
733 (concat (mapconcat
734 'identity inferior-octave-output-list "\n")
735 "\n"))))
737 ;; Find out whether Octave has built-in variables.
738 (inferior-octave-send-list-and-digest
739 (list "exist \"LOADPATH\"\n"))
740 (setq inferior-octave-has-built-in-variables
741 (string-match "101$" (car inferior-octave-output-list)))
743 ;; An empty secondary prompt, as e.g. obtained by '--braindead',
744 ;; means trouble.
745 (inferior-octave-send-list-and-digest (list "PS2\n"))
746 (if (string-match "\\(PS2\\|ans\\) = *$" (car inferior-octave-output-list))
747 (inferior-octave-send-list-and-digest
748 (list (if inferior-octave-has-built-in-variables
749 "PS2 = \"> \"\n"
750 "PS2 (\"> \");\n"))))
752 ;; O.k., now we are ready for the Inferior Octave startup commands.
753 (let* (commands
754 (program (file-name-nondirectory inferior-octave-program))
755 (file (or inferior-octave-startup-file
756 (concat "~/.emacs-" program))))
757 (setq commands
758 (list "more off;\n"
759 (if (not (string-equal
760 inferior-octave-output-string ">> "))
761 (if inferior-octave-has-built-in-variables
762 "PS1=\"\\\\s> \";\n"
763 "PS1 (\"\\\\s> \");\n"))
764 (if (file-exists-p file)
765 (format "source (\"%s\");\n" file))))
766 (inferior-octave-send-list-and-digest commands))
767 (insert-before-markers
768 (concat
769 (if inferior-octave-output-list
770 (concat (mapconcat
771 'identity inferior-octave-output-list "\n")
772 "\n"))
773 inferior-octave-output-string))
775 ;; And finally, everything is back to normal.
776 (set-process-filter proc 'inferior-octave-output-filter)
777 ;; Just in case, to be sure a cd in the startup file
778 ;; won't have detrimental effects.
779 (inferior-octave-resync-dirs)))
781 (defun inferior-octave-completion-table ()
782 (completion-table-dynamic
783 (lambda (command)
784 (inferior-octave-send-list-and-digest
785 (list (concat "completion_matches (\"" command "\");\n")))
786 (sort (delete-dups inferior-octave-output-list)
787 'string-lessp))))
789 (defun inferior-octave-completion-at-point ()
790 "Return the data to complete the Octave symbol at point."
791 (let* ((end (point))
792 (start
793 (save-excursion
794 (skip-syntax-backward "w_" (comint-line-beginning-position))
795 (point))))
796 (when (> end start)
797 (list start end (inferior-octave-completion-table)))))
799 (define-obsolete-function-alias 'inferior-octave-complete
800 'completion-at-point "24.1")
802 (defun inferior-octave-dynamic-list-input-ring ()
803 "List the buffer's input history in a help buffer."
804 ;; We cannot use `comint-dynamic-list-input-ring', because it replaces
805 ;; "completion" by "history reference" ...
806 (interactive)
807 (if (or (not (ring-p comint-input-ring))
808 (ring-empty-p comint-input-ring))
809 (message "No history")
810 (let ((history nil)
811 (history-buffer " *Input History*")
812 (index (1- (ring-length comint-input-ring)))
813 (conf (current-window-configuration)))
814 ;; We have to build up a list ourselves from the ring vector.
815 (while (>= index 0)
816 (setq history (cons (ring-ref comint-input-ring index) history)
817 index (1- index)))
818 ;; Change "completion" to "history reference"
819 ;; to make the display accurate.
820 (with-output-to-temp-buffer history-buffer
821 (display-completion-list history)
822 (set-buffer history-buffer))
823 (message "Hit space to flush")
824 (let ((ch (read-event)))
825 (if (eq ch ?\ )
826 (set-window-configuration conf)
827 (setq unread-command-events (list ch)))))))
829 (defun inferior-octave-strip-ctrl-g (string)
830 "Strip leading `^G' character.
831 If STRING starts with a `^G', ring the bell and strip it."
832 (if (string-match "^\a" string)
833 (progn
834 (ding)
835 (setq string (substring string 1))))
836 string)
838 (defun inferior-octave-output-filter (proc string)
839 "Standard output filter for the inferior Octave process.
840 Ring Emacs bell if process output starts with an ASCII bell, and pass
841 the rest to `comint-output-filter'."
842 (comint-output-filter proc (inferior-octave-strip-ctrl-g string)))
844 (defun inferior-octave-output-digest (_proc string)
845 "Special output filter for the inferior Octave process.
846 Save all output between newlines into `inferior-octave-output-list', and
847 the rest to `inferior-octave-output-string'."
848 (setq string (concat inferior-octave-output-string string))
849 (while (string-match "\n" string)
850 (setq inferior-octave-output-list
851 (append inferior-octave-output-list
852 (list (substring string 0 (match-beginning 0))))
853 string (substring string (match-end 0))))
854 (if (string-match inferior-octave-prompt string)
855 (setq inferior-octave-receive-in-progress nil))
856 (setq inferior-octave-output-string string))
858 (defun inferior-octave-send-list-and-digest (list)
859 "Send LIST to the inferior Octave process and digest the output.
860 The elements of LIST have to be strings and are sent one by one. All
861 output is passed to the filter `inferior-octave-output-digest'."
862 (let* ((proc inferior-octave-process)
863 (filter (process-filter proc))
864 string)
865 (set-process-filter proc 'inferior-octave-output-digest)
866 (setq inferior-octave-output-list nil)
867 (unwind-protect
868 (while (setq string (car list))
869 (setq inferior-octave-output-string nil
870 inferior-octave-receive-in-progress t)
871 (comint-send-string proc string)
872 (while inferior-octave-receive-in-progress
873 (accept-process-output proc))
874 (setq list (cdr list)))
875 (set-process-filter proc filter))))
877 (defun inferior-octave-directory-tracker (string)
878 "Tracks `cd' commands issued to the inferior Octave process.
879 Use \\[inferior-octave-resync-dirs] to resync if Emacs gets confused."
880 (cond
881 ((string-match "^[ \t]*cd[ \t;]*$" string)
882 (cd "~"))
883 ((string-match "^[ \t]*cd[ \t]+\\([^ \t\n;]*\\)[ \t\n;]*" string)
884 (cd (substring string (match-beginning 1) (match-end 1))))))
886 (defun inferior-octave-resync-dirs ()
887 "Resync the buffer's idea of the current directory.
888 This command queries the inferior Octave process about its current
889 directory and makes this the current buffer's default directory."
890 (interactive)
891 (inferior-octave-send-list-and-digest '("disp (pwd ())\n"))
892 (cd (car inferior-octave-output-list)))
895 ;;; Miscellaneous useful functions
897 (defun octave-in-comment-p ()
898 "Return non-nil if point is inside an Octave comment."
899 (nth 4 (syntax-ppss)))
901 (defun octave-in-string-p ()
902 "Return non-nil if point is inside an Octave string."
903 (nth 3 (syntax-ppss)))
905 (defun octave-in-string-or-comment-p ()
906 "Return non-nil if point is inside an Octave string or comment."
907 (nth 8 (syntax-ppss)))
909 (defun octave-looking-at-kw (regexp)
910 "Like `looking-at', but sets `case-fold-search' nil."
911 (let ((case-fold-search nil))
912 (looking-at regexp)))
914 (defun octave-maybe-insert-continuation-string ()
915 (if (or (octave-in-comment-p)
916 (save-excursion
917 (beginning-of-line)
918 (looking-at octave-continuation-regexp)))
920 (delete-horizontal-space)
921 (insert (concat " " octave-continuation-string))))
923 (defun octave-function-file-p ()
924 "Return non-nil if the first token is \"function\".
925 The value is (START END NAME-START NAME-END) of the function."
926 (save-excursion
927 (goto-char (point-min))
928 (when (equal (funcall smie-forward-token-function) "function")
929 (forward-word -1)
930 (let* ((start (point))
931 (end (progn (forward-sexp 1) (point)))
932 (name (when (progn
933 (goto-char start)
934 (re-search-forward octave-function-header-regexp
935 end t))
936 (list (match-beginning 3) (match-end 3)))))
937 (cons start (cons end name))))))
939 ;; Like forward-comment but stop at non-comment blank
940 (defun octave-skip-comment-forward (limit)
941 (let ((ppss (syntax-ppss)))
942 (if (nth 4 ppss)
943 (goto-char (nth 8 ppss))
944 (goto-char (or (comment-search-forward limit t) (point)))))
945 (while (and (< (point) limit) (looking-at-p "\\s<"))
946 (forward-comment 1)))
948 ;;; First non-copyright comment block
949 (defun octave-function-file-comment ()
950 "Beginning and end positions of the function file comment."
951 (save-excursion
952 (goto-char (point-min))
953 ;; Copyright block: octave/libinterp/parse-tree/lex.ll around line 1634
954 (while (save-excursion
955 (when (comment-search-forward (point-max) t)
956 (when (eq (char-after) ?\{) ; case of block comment
957 (forward-char 1))
958 (skip-syntax-forward "-")
959 (let ((case-fold-search t))
960 (looking-at-p "\\(?:copyright\\|author\\)\\_>"))))
961 (octave-skip-comment-forward (point-max)))
962 (let ((beg (comment-search-forward (point-max) t)))
963 (when beg
964 (goto-char beg)
965 (octave-skip-comment-forward (point-max))
966 (list beg (point))))))
968 (defun octave-sync-function-file-names ()
969 "Ensure function name agree with function file name.
970 See Info node `(octave)Function Files'."
971 (interactive)
972 (when buffer-file-name
973 (pcase-let ((`(,start ,_end ,name-start ,name-end)
974 (octave-function-file-p)))
975 (when (and start name-start)
976 (let* ((func (buffer-substring name-start name-end))
977 (file (file-name-sans-extension
978 (file-name-nondirectory buffer-file-name)))
979 (help-form (format "\
980 a: Use function name `%s'
981 b: Use file name `%s'
982 q: Don't fix\n" func file))
983 (c (unless (equal file func)
984 (save-window-excursion
985 (help-form-show)
986 (read-char-choice
987 "Which name to use? (a/b/q) " '(?a ?b ?q))))))
988 (pcase c
989 (`?a (let ((newname (expand-file-name
990 (concat func (file-name-extension
991 buffer-file-name t)))))
992 (when (or (not (file-exists-p newname))
993 (yes-or-no-p
994 (format "Target file %s exists; proceed? " newname)))
995 (when (file-exists-p buffer-file-name)
996 (rename-file buffer-file-name newname t))
997 (set-visited-file-name newname))))
998 (`?b (save-excursion
999 (goto-char name-start)
1000 (delete-region name-start name-end)
1001 (insert file)))))))))
1003 (defun octave-update-function-file-comment (beg end)
1004 "Query replace function names in function file comment."
1005 (interactive
1006 (progn
1007 (barf-if-buffer-read-only)
1008 (if (use-region-p)
1009 (list (region-beginning) (region-end))
1010 (or (octave-function-file-comment)
1011 (error "No function file comment found")))))
1012 (save-excursion
1013 (let* ((bounds (or (octave-function-file-p)
1014 (error "Not in a function file buffer")))
1015 (func (if (cddr bounds)
1016 (apply #'buffer-substring (cddr bounds))
1017 (error "Function name not found")))
1018 (old-func (progn
1019 (goto-char beg)
1020 (when (re-search-forward
1021 "[=}]\\s-*\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>"
1022 (min (line-end-position 4) end)
1024 (match-string 1))))
1025 (old-func (read-string (format (if old-func
1026 "Name to replace (default %s): "
1027 "Name to replace: ")
1028 old-func)
1029 nil nil old-func)))
1030 (if (and func old-func (not (equal func old-func)))
1031 (perform-replace old-func func 'query
1032 nil 'delimited nil nil beg end)
1033 (message "Function names match")))))
1035 ;; Adapted from texinfo-font-lock-keywords
1036 (defvar octave-texinfo-font-lock-keywords
1037 `(("@\\([a-zA-Z]+\\|[^ \t\n]\\)" 1 font-lock-keyword-face prepend) ;commands
1038 ("^\\*\\([^\n:]*\\)" 1 font-lock-function-name-face prepend) ;menu items
1039 ("@\\(emph\\|i\\|sc\\){\\([^}]+\\)" 2 'italic prepend)
1040 ("@\\(strong\\|b\\){\\([^}]+\\)" 2 'bold prepend)
1041 ("@\\(kbd\\|key\\|url\\|uref\\){\\([^}]+\\)"
1042 2 font-lock-string-face prepend)
1043 ("@\\(file\\|email\\){\\([^}]+\\)" 2 font-lock-string-face prepend)
1044 ("@\\(samp\\|code\\|var\\|math\\|env\\|command\\|option\\){\\([^}]+\\)"
1045 2 font-lock-variable-name-face prepend)
1046 ("@\\(cite\\|x?ref\\|pxref\\|dfn\\|inforef\\){\\([^}]+\\)"
1047 2 font-lock-constant-face prepend)
1048 ("@\\(anchor\\){\\([^}]+\\)" 2 font-lock-type-face prepend)
1049 ("@\\(dmn\\|acronym\\|value\\){\\([^}]+\\)"
1050 2 font-lock-builtin-face prepend)
1051 ("@\\(end\\|itemx?\\) +\\(.+\\)" 2 font-lock-keyword-face prepend))
1052 "Additional keywords to highlight in texinfo comment block.")
1054 (defface octave-function-comment-block
1055 '((t (:inherit font-lock-doc-face)))
1056 "Face used to highlight function comment block."
1057 :group 'octave)
1059 (defun octave-font-lock-texinfo-comment ()
1060 (font-lock-add-keywords
1062 '(((lambda (limit)
1063 (while (and (search-forward "-*- texinfo -*-" limit t)
1064 (octave-in-comment-p))
1065 (let ((beg (nth 8 (syntax-ppss)))
1066 (end (progn
1067 (octave-skip-comment-forward (point-max))
1068 (point))))
1069 (put-text-property beg end 'font-lock-multiline t)
1070 (font-lock-prepend-text-property
1071 beg end 'face 'octave-function-comment-block)
1072 (dolist (kw octave-texinfo-font-lock-keywords)
1073 (goto-char beg)
1074 (while (re-search-forward (car kw) end 'move)
1075 (font-lock-apply-highlight (cdr kw))))))
1076 nil)))
1077 'append))
1080 ;;; Indentation
1082 (defun octave-indent-new-comment-line ()
1083 "Break Octave line at point, continuing comment if within one.
1084 If within code, insert `octave-continuation-string' before breaking the
1085 line. If within a string, signal an error.
1086 The new line is properly indented."
1087 (interactive)
1088 (delete-horizontal-space)
1089 (cond
1090 ((octave-in-comment-p)
1091 (indent-new-comment-line))
1092 ((octave-in-string-p)
1093 (error "Cannot split a code line inside a string"))
1095 (insert (concat " " octave-continuation-string))
1096 (reindent-then-newline-and-indent))))
1098 (defun octave-indent-defun ()
1099 "Properly indent the Octave function which contains point."
1100 (interactive)
1101 (save-excursion
1102 (mark-defun)
1103 (message "Indenting function...")
1104 (indent-region (point) (mark) nil))
1105 (message "Indenting function...done."))
1108 ;;; Motion
1109 (defun octave-next-code-line (&optional arg)
1110 "Move ARG lines of Octave code forward (backward if ARG is negative).
1111 Skips past all empty and comment lines. Default for ARG is 1.
1113 On success, return 0. Otherwise, go as far as possible and return -1."
1114 (interactive "p")
1115 (or arg (setq arg 1))
1116 (beginning-of-line)
1117 (let ((n 0)
1118 (inc (if (> arg 0) 1 -1)))
1119 (while (and (/= arg 0) (= n 0))
1120 (setq n (forward-line inc))
1121 (while (and (= n 0)
1122 (looking-at "\\s-*\\($\\|\\s<\\)"))
1123 (setq n (forward-line inc)))
1124 (setq arg (- arg inc)))
1127 (defun octave-previous-code-line (&optional arg)
1128 "Move ARG lines of Octave code backward (forward if ARG is negative).
1129 Skips past all empty and comment lines. Default for ARG is 1.
1131 On success, return 0. Otherwise, go as far as possible and return -1."
1132 (interactive "p")
1133 (or arg (setq arg 1))
1134 (octave-next-code-line (- arg)))
1136 (defun octave-beginning-of-line ()
1137 "Move point to beginning of current Octave line.
1138 If on an empty or comment line, go to the beginning of that line.
1139 Otherwise, move backward to the beginning of the first Octave code line
1140 which is not inside a continuation statement, i.e., which does not
1141 follow a code line ending in `...' or `\\', or is inside an open
1142 parenthesis list."
1143 (interactive)
1144 (beginning-of-line)
1145 (if (not (looking-at "\\s-*\\($\\|\\s<\\)"))
1146 (while (or (condition-case nil
1147 (progn
1148 (up-list -1)
1149 (beginning-of-line)
1151 (error nil))
1152 (and (or (looking-at "\\s-*\\($\\|\\s<\\)")
1153 (save-excursion
1154 (if (zerop (octave-previous-code-line))
1155 (looking-at octave-continuation-regexp))))
1156 (zerop (forward-line -1)))))))
1158 (defun octave-end-of-line ()
1159 "Move point to end of current Octave line.
1160 If on an empty or comment line, go to the end of that line.
1161 Otherwise, move forward to the end of the first Octave code line which
1162 does not end in `...' or `\\' or is inside an open parenthesis list."
1163 (interactive)
1164 (end-of-line)
1165 (if (save-excursion
1166 (beginning-of-line)
1167 (looking-at "\\s-*\\($\\|\\s<\\)"))
1169 (while (or (condition-case nil
1170 (progn
1171 (up-list 1)
1172 (end-of-line)
1174 (error nil))
1175 (and (save-excursion
1176 (beginning-of-line)
1177 (or (looking-at "\\s-*\\($\\|\\s<\\)")
1178 (looking-at octave-continuation-regexp)))
1179 (zerop (forward-line 1)))))
1180 (end-of-line)))
1182 (defun octave-mark-block ()
1183 "Put point at the beginning of this Octave block, mark at the end.
1184 The block marked is the one that contains point or follows point."
1185 (interactive)
1186 (if (and (looking-at "\\sw\\|\\s_")
1187 (looking-back "\\sw\\|\\s_" (1- (point))))
1188 (skip-syntax-forward "w_"))
1189 (unless (or (looking-at "\\s(")
1190 (save-excursion
1191 (let* ((token (funcall smie-forward-token-function))
1192 (level (assoc token smie-grammar)))
1193 (and level (not (numberp (cadr level)))))))
1194 (backward-up-list 1))
1195 (mark-sexp))
1197 (defun octave-beginning-of-defun (&optional arg)
1198 "Move backward to the beginning of an Octave function.
1199 With positive ARG, do it that many times. Negative argument -N means
1200 move forward to Nth following beginning of a function.
1201 Returns t unless search stops at the beginning or end of the buffer."
1202 (let* ((arg (or arg 1))
1203 (inc (if (> arg 0) 1 -1))
1204 (found nil)
1205 (case-fold-search nil))
1206 (and (not (eobp))
1207 (not (and (> arg 0) (looking-at "\\_<function\\_>")))
1208 (skip-syntax-forward "w"))
1209 (while (and (/= arg 0)
1210 (setq found
1211 (re-search-backward "\\_<function\\_>" inc)))
1212 (unless (octave-in-string-or-comment-p)
1213 (setq arg (- arg inc))))
1214 (if found
1215 (progn
1216 (and (< inc 0) (goto-char (match-beginning 0)))
1217 t))))
1220 ;;; Filling
1221 (defun octave-auto-fill ()
1222 "Perform auto-fill in Octave mode.
1223 Returns nil if no feasible place to break the line could be found, and t
1224 otherwise."
1225 (let (fc give-up)
1226 (if (or (null (setq fc (current-fill-column)))
1227 (save-excursion
1228 (beginning-of-line)
1229 (and auto-fill-inhibit-regexp
1230 (octave-looking-at-kw auto-fill-inhibit-regexp))))
1231 nil ; Can't do anything
1232 (if (and (not (octave-in-comment-p))
1233 (> (current-column) fc))
1234 (setq fc (- fc (+ (length octave-continuation-string) 1))))
1235 (while (and (not give-up) (> (current-column) fc))
1236 (let* ((opoint (point))
1237 (fpoint
1238 (save-excursion
1239 (move-to-column (+ fc 1))
1240 (skip-chars-backward "^ \t\n")
1241 ;; If we're at the beginning of the line, break after
1242 ;; the first word
1243 (if (bolp)
1244 (re-search-forward "[ \t]" opoint t))
1245 ;; If we're in a comment line, don't break after the
1246 ;; comment chars
1247 (if (save-excursion
1248 (skip-syntax-backward " <")
1249 (bolp))
1250 (re-search-forward "[ \t]" (line-end-position)
1251 'move))
1252 ;; If we're not in a comment line and just ahead the
1253 ;; continuation string, don't break here.
1254 (if (and (not (octave-in-comment-p))
1255 (looking-at
1256 (concat "\\s-*"
1257 (regexp-quote
1258 octave-continuation-string)
1259 "\\s-*$")))
1260 (end-of-line))
1261 (skip-chars-backward " \t")
1262 (point))))
1263 (if (save-excursion
1264 (goto-char fpoint)
1265 (not (or (bolp) (eolp))))
1266 (let ((prev-column (current-column)))
1267 (if (save-excursion
1268 (skip-chars-backward " \t")
1269 (= (point) fpoint))
1270 (progn
1271 (octave-maybe-insert-continuation-string)
1272 (indent-new-comment-line t))
1273 (save-excursion
1274 (goto-char fpoint)
1275 (octave-maybe-insert-continuation-string)
1276 (indent-new-comment-line t)))
1277 (if (>= (current-column) prev-column)
1278 (setq give-up t)))
1279 (setq give-up t))))
1280 (not give-up))))
1282 (defun octave-fill-paragraph (&optional _arg)
1283 "Fill paragraph of Octave code, handling Octave comments."
1284 ;; FIXME: difference with generic fill-paragraph:
1285 ;; - code lines are only split, never joined.
1286 ;; - \n that end comments are never removed.
1287 ;; - insert continuation marker when splitting code lines.
1288 (interactive "P")
1289 (save-excursion
1290 (let ((end (progn (forward-paragraph) (copy-marker (point) t)))
1291 (beg (progn
1292 (forward-paragraph -1)
1293 (skip-chars-forward " \t\n")
1294 (beginning-of-line)
1295 (point)))
1296 (cfc (current-fill-column))
1297 comment-prefix)
1298 (goto-char beg)
1299 (while (< (point) end)
1300 (condition-case nil
1301 (indent-according-to-mode)
1302 (error nil))
1303 (move-to-column cfc)
1304 ;; First check whether we need to combine non-empty comment lines
1305 (if (and (< (current-column) cfc)
1306 (octave-in-comment-p)
1307 (not (save-excursion
1308 (beginning-of-line)
1309 (looking-at "^\\s-*\\s<+\\s-*$"))))
1310 ;; This is a nonempty comment line which does not extend
1311 ;; past the fill column. If it is followed by a nonempty
1312 ;; comment line with the same comment prefix, try to
1313 ;; combine them, and repeat this until either we reach the
1314 ;; fill-column or there is nothing more to combine.
1315 (progn
1316 ;; Get the comment prefix
1317 (save-excursion
1318 (beginning-of-line)
1319 (while (and (re-search-forward "\\s<+")
1320 (not (octave-in-comment-p))))
1321 (setq comment-prefix (match-string 0)))
1322 ;; And keep combining ...
1323 (while (and (< (current-column) cfc)
1324 (save-excursion
1325 (forward-line 1)
1326 (and (looking-at
1327 (concat "^\\s-*"
1328 comment-prefix
1329 "\\S<"))
1330 (not (looking-at
1331 (concat "^\\s-*"
1332 comment-prefix
1333 "\\s-*$"))))))
1334 (delete-char 1)
1335 (re-search-forward comment-prefix)
1336 (delete-region (match-beginning 0) (match-end 0))
1337 (fixup-whitespace)
1338 (move-to-column cfc))))
1339 ;; We might also try to combine continued code lines> Perhaps
1340 ;; some other time ...
1341 (skip-chars-forward "^ \t\n")
1342 (delete-horizontal-space)
1343 (if (or (< (current-column) cfc)
1344 (and (= (current-column) cfc) (eolp)))
1345 (forward-line 1)
1346 (if (not (eolp)) (insert " "))
1347 (or (octave-auto-fill)
1348 (forward-line 1))))
1349 t)))
1352 ;;; Completions
1354 (defun octave-completion-at-point-function ()
1355 "Find the text to complete and the corresponding table."
1356 (let* ((beg (save-excursion (skip-syntax-backward "w_") (point)))
1357 (end (point)))
1358 (if (< beg (point))
1359 ;; Extend region past point, if applicable.
1360 (save-excursion (skip-syntax-forward "w_")
1361 (setq end (point))))
1362 (list beg end (or (and inferior-octave-process
1363 (process-live-p inferior-octave-process)
1364 (inferior-octave-completion-table))
1365 (append octave-reserved-words
1366 octave-text-functions)))))
1368 (define-obsolete-function-alias 'octave-complete-symbol
1369 'completion-at-point "24.1")
1371 ;;; Electric characters && friends
1373 (defun octave-abbrev-start ()
1374 "Start entering an Octave abbreviation.
1375 If Abbrev mode is turned on, typing ` (grave accent) followed by ? or
1376 \\[help-command] lists all Octave abbrevs. Any other key combination is
1377 executed normally.
1378 Note that all Octave mode abbrevs start with a grave accent."
1379 (interactive)
1380 (self-insert-command 1)
1381 (when abbrev-mode
1382 (set-temporary-overlay-map
1383 (let ((map (make-sparse-keymap)))
1384 (define-key map [??] 'list-abbrevs)
1385 (define-key map (vector help-char) 'list-abbrevs)
1386 map))))
1388 (define-skeleton octave-insert-defun
1389 "Insert an Octave function skeleton.
1390 Prompt for the function's name, arguments and return values (to be
1391 entered without parens)."
1392 (let* ((defname (file-name-sans-extension (buffer-name)))
1393 (name (read-string (format "Function name (default %s): " defname)
1394 nil nil defname))
1395 (args (read-string "Arguments: "))
1396 (vals (read-string "Return values: ")))
1397 (format "%s%s (%s)"
1398 (cond
1399 ((string-equal vals "") vals)
1400 ((string-match "[ ,]" vals) (concat "[" vals "] = "))
1401 (t (concat vals " = ")))
1402 name
1403 args))
1404 \n octave-block-comment-start "usage: " str \n
1405 octave-block-comment-start '(delete-horizontal-space) \n
1406 octave-block-comment-start '(delete-horizontal-space) \n
1407 "function " > str \n
1408 _ \n
1409 "endfunction" > \n)
1411 ;;; Communication with the inferior Octave process
1412 (defun octave-kill-process ()
1413 "Kill inferior Octave process and its buffer."
1414 (interactive)
1415 (if inferior-octave-process
1416 (progn
1417 (process-send-string inferior-octave-process "quit;\n")
1418 (accept-process-output inferior-octave-process)))
1419 (if inferior-octave-buffer
1420 (kill-buffer inferior-octave-buffer)))
1422 (defun octave-show-process-buffer ()
1423 "Make sure that `inferior-octave-buffer' is displayed."
1424 (interactive)
1425 (if (get-buffer inferior-octave-buffer)
1426 (display-buffer inferior-octave-buffer)
1427 (message "No buffer named %s" inferior-octave-buffer)))
1429 (defun octave-hide-process-buffer ()
1430 "Delete all windows that display `inferior-octave-buffer'."
1431 (interactive)
1432 (if (get-buffer inferior-octave-buffer)
1433 (delete-windows-on inferior-octave-buffer)
1434 (message "No buffer named %s" inferior-octave-buffer)))
1436 (defun octave-send-region (beg end)
1437 "Send current region to the inferior Octave process."
1438 (interactive "r")
1439 (inferior-octave t)
1440 (let ((proc inferior-octave-process)
1441 (string (buffer-substring-no-properties beg end))
1442 line)
1443 (with-current-buffer inferior-octave-buffer
1444 (setq inferior-octave-output-list nil)
1445 (while (not (string-equal string ""))
1446 (if (string-match "\n" string)
1447 (setq line (substring string 0 (match-beginning 0))
1448 string (substring string (match-end 0)))
1449 (setq line string string ""))
1450 (setq inferior-octave-receive-in-progress t)
1451 (inferior-octave-send-list-and-digest (list (concat line "\n")))
1452 (while inferior-octave-receive-in-progress
1453 (accept-process-output proc))
1454 (insert-before-markers
1455 (mapconcat 'identity
1456 (append
1457 (if octave-send-echo-input (list line) (list ""))
1458 (mapcar 'inferior-octave-strip-ctrl-g
1459 inferior-octave-output-list)
1460 (list inferior-octave-output-string))
1461 "\n")))))
1462 (if octave-send-show-buffer
1463 (display-buffer inferior-octave-buffer)))
1465 (defun octave-send-block ()
1466 "Send current Octave block to the inferior Octave process."
1467 (interactive)
1468 (save-excursion
1469 (octave-mark-block)
1470 (octave-send-region (point) (mark))))
1472 (defun octave-send-defun ()
1473 "Send current Octave function to the inferior Octave process."
1474 (interactive)
1475 (save-excursion
1476 (mark-defun)
1477 (octave-send-region (point) (mark))))
1479 (defun octave-send-line (&optional arg)
1480 "Send current Octave code line to the inferior Octave process.
1481 With positive prefix ARG, send that many lines.
1482 If `octave-send-line-auto-forward' is non-nil, go to the next unsent
1483 code line."
1484 (interactive "P")
1485 (or arg (setq arg 1))
1486 (if (> arg 0)
1487 (let (beg end)
1488 (beginning-of-line)
1489 (setq beg (point))
1490 (octave-next-code-line (- arg 1))
1491 (end-of-line)
1492 (setq end (point))
1493 (if octave-send-line-auto-forward
1494 (octave-next-code-line 1))
1495 (octave-send-region beg end))))
1497 (defun octave-eval-print-last-sexp ()
1498 "Evaluate Octave sexp before point and print value into current buffer."
1499 (interactive)
1500 (inferior-octave t)
1501 (let ((standard-output (current-buffer))
1502 (print-escape-newlines nil)
1503 (opoint (point)))
1504 (terpri)
1505 (prin1
1506 (save-excursion
1507 (forward-sexp -1)
1508 (inferior-octave-send-list-and-digest
1509 (list (concat (buffer-substring-no-properties (point) opoint)
1510 "\n")))
1511 (mapconcat 'identity inferior-octave-output-list "\n")))
1512 (terpri)))
1515 (provide 'octave)
1516 ;;; octave.el ends here