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>
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. 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.
40 (unless (fboundp 'setq-local)
41 (defmacro setq-local (var val)
42 "Set variable VAR to value VAL in current buffer."
43 (list 'set (list 'make-local-variable (list 'quote var)) val))))
46 "Editing Octave code."
47 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
50 (define-obsolete-function-alias 'octave-submit-bug-report
51 'report-emacs-bug "24.4")
53 (define-abbrev-table 'octave-abbrev-table nil
54 "Abbrev table for Octave's reserved words.
55 Used in `octave-mode' and `inferior-octave-mode' buffers.")
57 (defvar octave-comment-char ?#
58 "Character to start an Octave comment.")
60 (defvar octave-comment-start
61 (string octave-comment-char ?\s)
62 "String to insert to start a new Octave in-line comment.")
64 (defvar octave-comment-start-skip "\\s<+\\s-*"
65 "Regexp to match the start of an Octave comment up to its body.")
67 (defvar octave-begin-keywords
68 '("do" "for" "function" "if" "switch" "try" "unwind_protect" "while"))
70 (defvar octave-else-keywords
71 '("case" "catch" "else" "elseif" "otherwise" "unwind_protect_cleanup"))
73 (defvar octave-end-keywords
74 '("endfor" "endfunction" "endif" "endswitch" "end_try_catch"
75 "end_unwind_protect" "endwhile" "until" "end"))
77 (defvar octave-reserved-words
78 (append octave-begin-keywords
81 '("break" "continue" "end" "global" "persistent" "return"))
82 "Reserved words in Octave.")
84 (defvar octave-text-functions
85 '("casesen" "cd" "chdir" "clear" "diary" "dir" "document" "echo"
86 "edit_history" "format" "help" "history" "hold"
87 "load" "ls" "more" "run_history" "save" "type"
89 "Text functions in Octave.")
91 (defvar octave-function-header-regexp
92 (concat "^\\s-*\\_<\\(function\\)\\_>"
93 "\\([^=;\n]*=[ \t]*\\|[ \t]*\\)\\(\\(?:\\w\\|\\s_\\)+\\)\\_>")
94 "Regexp to match an Octave function header.
95 The string `function' and its name are given by the first and third
96 parenthetical grouping.")
99 (defvar octave-font-lock-keywords
101 ;; Fontify all builtin keywords.
102 (cons (concat "\\_<\\("
103 (regexp-opt (append octave-reserved-words
104 octave-text-functions))
106 'font-lock-keyword-face)
107 ;; Note: 'end' also serves as the last index in an indexing expression.
108 ;; Ref: http://www.mathworks.com/help/matlab/ref/end.html
110 (while (re-search-forward "\\_<end\\_>" limit 'move)
111 (let ((beg (match-beginning 0))
113 (unless (octave-in-string-or-comment-p)
118 (when (memq (char-after) '(?\( ?\[ ?\{))
119 (put-text-property beg end 'face nil)))
122 ;; Fontify all builtin operators.
123 (cons "\\(&\\||\\|<=\\|>=\\|==\\|<\\|>\\|!=\\|!\\)"
124 (if (boundp 'font-lock-builtin-face)
125 'font-lock-builtin-face
126 'font-lock-preprocessor-face))
127 ;; Fontify all function declarations.
128 (list octave-function-header-regexp
129 '(1 font-lock-keyword-face)
130 '(3 font-lock-function-name-face nil t)))
131 "Additional Octave expressions to highlight.")
133 (defun octave-syntax-propertize-function (start end)
135 (octave-syntax-propertize-sqs end)
136 (funcall (syntax-propertize-rules
137 ;; Try to distinguish the string-quotes from the transpose-quotes.
138 ("\\(?:^\\|[[({,; ]\\)\\('\\)"
139 (1 (prog1 "\"'" (octave-syntax-propertize-sqs end)))))
142 (defun octave-syntax-propertize-sqs (end)
143 "Propertize the content/end of single-quote strings."
144 (when (eq (nth 3 (syntax-ppss)) ?\')
146 (when (re-search-forward
147 "\\(?:\\=\\|[^']\\)\\(?:''\\)*\\('\\)\\($\\|[^']\\)" end 'move)
148 (goto-char (match-beginning 2))
149 (when (eq (char-before (match-beginning 1)) ?\\)
150 ;; Backslash cannot escape a single quote.
151 (put-text-property (1- (match-beginning 1)) (match-beginning 1)
152 'syntax-table (string-to-syntax ".")))
153 (put-text-property (match-beginning 1) (match-end 1)
154 'syntax-table (string-to-syntax "\"'")))))
157 (defvar octave-mode-map
158 (let ((map (make-sparse-keymap)))
159 (define-key map "\M-." 'octave-find-definition)
160 (define-key map "\e\n" 'octave-indent-new-comment-line)
161 (define-key map "\M-\C-q" 'octave-indent-defun)
162 (define-key map "\C-c\C-p" 'octave-previous-code-line)
163 (define-key map "\C-c\C-n" 'octave-next-code-line)
164 (define-key map "\C-c\C-a" 'octave-beginning-of-line)
165 (define-key map "\C-c\C-e" 'octave-end-of-line)
166 (define-key map [remap down-list] 'smie-down-list)
167 (define-key map "\C-c\M-\C-h" 'octave-mark-block)
168 (define-key map "\C-c]" 'smie-close-block)
169 (define-key map "\C-c/" 'smie-close-block)
170 (define-key map "\C-c;" 'octave-update-function-file-comment)
171 (define-key map "\C-hd" 'octave-help)
172 (define-key map "\C-c\C-f" 'octave-insert-defun)
173 (define-key map "\C-c\C-il" 'octave-send-line)
174 (define-key map "\C-c\C-ib" 'octave-send-block)
175 (define-key map "\C-c\C-if" 'octave-send-defun)
176 (define-key map "\C-c\C-ir" 'octave-send-region)
177 (define-key map "\C-c\C-is" 'octave-show-process-buffer)
178 (define-key map "\C-c\C-iq" 'octave-hide-process-buffer)
179 (define-key map "\C-c\C-ik" 'octave-kill-process)
180 (define-key map "\C-c\C-i\C-l" 'octave-send-line)
181 (define-key map "\C-c\C-i\C-b" 'octave-send-block)
182 (define-key map "\C-c\C-i\C-f" 'octave-send-defun)
183 (define-key map "\C-c\C-i\C-r" 'octave-send-region)
184 (define-key map "\C-c\C-i\C-s" 'octave-show-process-buffer)
185 (define-key map "\C-c\C-i\C-q" 'octave-hide-process-buffer)
186 (define-key map "\C-c\C-i\C-k" 'octave-kill-process)
188 "Keymap used in Octave mode.")
192 (easy-menu-define octave-mode-menu octave-mode-map
193 "Menu for Octave mode."
196 ["Previous Code Line" octave-previous-code-line t]
197 ["Next Code Line" octave-next-code-line t]
198 ["Begin of Continuation" octave-beginning-of-line t]
199 ["End of Continuation" octave-end-of-line t]
200 ["Split Line at Point" octave-indent-new-comment-line t])
202 ["Mark Block" octave-mark-block t]
203 ["Close Block" smie-close-block t])
205 ["Indent Function" octave-indent-defun t]
206 ["Insert Function" octave-insert-defun t]
207 ["Update function file comment" octave-update-function-file-comment t])
210 ["Send Current Line" octave-send-line t]
211 ["Send Current Block" octave-send-block t]
212 ["Send Current Function" octave-send-defun t]
213 ["Send Region" octave-send-region t]
214 ["Show Process Buffer" octave-show-process-buffer t]
215 ["Hide Process Buffer" octave-hide-process-buffer t]
216 ["Kill Process" octave-kill-process t])
218 ["Indent Line" indent-according-to-mode t]
219 ["Complete Symbol" completion-at-point t]
220 ["Toggle Auto-Fill Mode" auto-fill-mode
221 :style toggle :selected auto-fill-function]
223 ["Describe Octave Mode" describe-mode t]
224 ["Lookup Octave Index" info-lookup-symbol t]
225 ["Customize Octave" (customize-group 'octave) t]
227 ["Submit Bug Report" report-emacs-bug t]))
229 (defvar octave-mode-syntax-table
230 (let ((table (make-syntax-table)))
231 (modify-syntax-entry ?\r " " table)
232 (modify-syntax-entry ?+ "." table)
233 (modify-syntax-entry ?- "." table)
234 (modify-syntax-entry ?= "." table)
235 (modify-syntax-entry ?* "." table)
236 (modify-syntax-entry ?/ "." table)
237 (modify-syntax-entry ?> "." table)
238 (modify-syntax-entry ?< "." table)
239 (modify-syntax-entry ?& "." table)
240 (modify-syntax-entry ?| "." table)
241 (modify-syntax-entry ?! "." table)
242 (modify-syntax-entry ?\\ "." table)
243 (modify-syntax-entry ?\' "." table)
244 ;; Was "w" for abbrevs, but now that it's not necessary any more,
245 (modify-syntax-entry ?\` "." table)
246 (modify-syntax-entry ?\" "\"" table)
247 (modify-syntax-entry ?. "_" table)
248 (modify-syntax-entry ?_ "_" table)
249 ;; The "b" flag only applies to the second letter of the comstart
250 ;; and the first letter of the comend, i.e. the "4b" below is ineffective.
251 ;; If we try to put `b' on the single-line comments, we get a similar
252 ;; problem where the % and # chars appear as first chars of the 2-char
253 ;; comend, so the multi-line ender is also turned into style-b.
254 ;; So we need the new "c" comment style.
255 (modify-syntax-entry ?\% "< 13" table)
256 (modify-syntax-entry ?\# "< 13" table)
257 (modify-syntax-entry ?\{ "(} 2c" table)
258 (modify-syntax-entry ?\} "){ 4c" table)
259 (modify-syntax-entry ?\n ">" table)
261 "Syntax table in use in `octave-mode' buffers.")
263 (defcustom octave-font-lock-texinfo-comment t
264 "Control whether to highlight the texinfo comment block."
269 (defcustom octave-blink-matching-block t
270 "Control the blinking of matching Octave block keywords.
271 Non-nil means show matching begin of block when inserting a space,
272 newline or semicolon after an else or end keyword."
276 (defcustom octave-block-offset 2
277 "Extra indentation applied to statements in Octave block structures."
281 (defvar octave-block-comment-start
282 (concat (make-string 2 octave-comment-char) " ")
283 "String to insert to start a new Octave comment on an empty line.")
285 (defcustom octave-continuation-offset 4
286 "Extra indentation applied to Octave continuation lines."
291 (defconst octave-continuation-marker-regexp "\\\\\\|\\.\\.\\."))
293 (defvar octave-continuation-regexp
294 (concat "[^#%\n]*\\(" octave-continuation-marker-regexp
295 "\\)\\s-*\\(\\s<.*\\)?$"))
297 ;; Char \ is considered a bad decision for continuing a line.
298 (defconst octave-continuation-string "..."
299 "Character string used for Octave continuation lines.")
301 (defvar octave-mode-imenu-generic-expression
304 (list nil octave-function-header-regexp 3))
305 "Imenu expression for Octave mode. See `imenu-generic-expression'.")
307 (defcustom octave-mode-hook nil
308 "Hook to be run when Octave mode is started."
312 (defcustom octave-send-show-buffer t
313 "Non-nil means display `inferior-octave-buffer' after sending to it."
317 (defcustom octave-send-line-auto-forward t
318 "Control auto-forward after sending to the inferior Octave process.
319 Non-nil means always go to the next Octave code line after sending."
323 (defcustom octave-send-echo-input t
324 "Non-nil means echo input sent to the inferior Octave process."
333 (defconst octave-operator-table
334 '((assoc ";" "\n") (assoc ",") ; The doc claims they have equal precedence!?
335 (right "=" "+=" "-=" "*=" "/=")
336 (assoc "&&") (assoc "||") ; The doc claims they have equal precedence!?
337 (assoc "&") (assoc "|") ; The doc claims they have equal precedence!?
338 (nonassoc "<" "<=" "==" ">=" ">" "!=" "~=")
339 (nonassoc ":") ;No idea what this is.
341 (assoc "*" "/" "\\" ".\\" ".*" "./")
343 (nonassoc "++" "--" "!" "~") ;And unary "+" and "-".
344 (right "^" "**" ".^" ".**")
345 ;; It's not really an operator, but for indentation purposes it
346 ;; could be convenient to treat it as one.
349 (defconst octave-smie-bnf-table
351 ;; We can't distinguish the first element in a sequence with
352 ;; precedence grammars, so we can't distinguish the condition
353 ;; if the `if' from the subsequent body, for example.
354 ;; This has to be done later in the indentation rules.
356 ;; We need to mention at least one of the operators in this part
357 ;; of the grammar: if the BNF and the operator table have
358 ;; no overlap, SMIE can't know how they relate.
360 ("try" exp "catch" exp "end_try_catch")
361 ("try" exp "catch" exp "end")
362 ("unwind_protect" exp
363 "unwind_protect_cleanup" exp "end_unwind_protect")
364 ("unwind_protect" exp "unwind_protect_cleanup" exp "end")
367 ("do" exp "until" atom)
368 ("while" exp "endwhile")
371 ("if" exp "else" exp "endif")
372 ("if" exp "elseif" exp "else" exp "endif")
373 ("if" exp "elseif" exp "elseif" exp "else" exp "endif")
374 ("if" exp "elseif" exp "elseif" exp "else" exp "end")
375 ("switch" exp "case" exp "endswitch")
376 ("switch" exp "case" exp "otherwise" exp "endswitch")
377 ("switch" exp "case" exp "case" exp "otherwise" exp "endswitch")
378 ("switch" exp "case" exp "case" exp "otherwise" exp "end")
379 ("function" exp "endfunction")
380 ("function" exp "end"))
381 ;; (fundesc (atom "=" atom))
384 (defconst octave-smie-grammar
387 (smie-bnf->prec2 octave-smie-bnf-table
390 (smie-precs->prec2 octave-operator-table))))
392 ;; Tokenizing needs to be refined so that ";;" is treated as two
393 ;; tokens and also so as to recognize the \n separator (and
394 ;; corresponding continuation lines).
396 (defconst octave-operator-regexp
397 (regexp-opt (apply 'append (mapcar 'cdr octave-operator-table))))
399 (defun octave-smie-backward-token ()
401 (forward-comment (- (point)))
403 ((and (not (eq (char-before) ?\;)) ;Coalesce ";" and "\n".
404 (> pos (line-end-position))
405 (if (looking-back octave-continuation-marker-regexp (- (point) 3))
407 (goto-char (match-beginning 0))
408 (forward-comment (- (point)))
411 ;; Ignore it if it's within parentheses.
412 (let ((ppss (syntax-ppss)))
413 (not (and (nth 1 ppss)
414 (eq ?\( (char-after (nth 1 ppss)))))))
415 (skip-chars-forward " \t")
416 ;; Why bother distinguishing \n and ;?
418 ((and (looking-back octave-operator-regexp (- (point) 3) 'greedy)
419 ;; Don't mistake a string quote for a transpose.
420 (not (looking-back "\\s\"" (1- (point)))))
421 (goto-char (match-beginning 0))
422 (match-string-no-properties 0))
424 (smie-default-backward-token)))))
426 (defun octave-smie-forward-token ()
427 (skip-chars-forward " \t")
428 (when (looking-at (eval-when-compile
429 (concat "\\(" octave-continuation-marker-regexp
430 "\\)[ \t]*\\($\\|[%#]\\)")))
431 (goto-char (match-end 1))
434 ((and (looking-at "$\\|[%#]")
435 ;; Ignore it if it's within parentheses or if the newline does not end
436 ;; some preceding text.
437 (prog1 (and (not (smie-rule-bolp))
438 (let ((ppss (syntax-ppss)))
439 (not (and (nth 1 ppss)
440 (eq ?\( (char-after (nth 1 ppss)))))))
441 (forward-comment (point-max))))
442 ;; Why bother distinguishing \n and ;?
444 ((looking-at ";[ \t]*\\($\\|[%#]\\)")
445 ;; Combine the ; with the subsequent \n.
446 (goto-char (match-beginning 1))
449 ((and (looking-at octave-operator-regexp)
450 ;; Don't mistake a string quote for a transpose.
451 (not (looking-at "\\s\"")))
452 (goto-char (match-end 0))
453 (match-string-no-properties 0))
455 (smie-default-forward-token))))
457 (defun octave-smie-rules (kind token)
458 (pcase (cons kind token)
459 ;; We could set smie-indent-basic instead, but that would have two
461 ;; - changes to octave-block-offset wouldn't take effect immediately.
462 ;; - edebug wouldn't show the use of this variable.
463 (`(:elem . basic) octave-block-offset)
464 ;; Since "case" is in the same BNF rules as switch..end, SMIE by default
465 ;; aligns it with "switch".
466 (`(:before . "case") (if (not (smie-rule-sibling-p)) octave-block-offset))
468 (if (smie-rule-parent-p "function" "if" "while" "else" "elseif" "for"
469 "otherwise" "case" "try" "catch" "unwind_protect"
470 "unwind_protect_cleanup")
471 (smie-rule-parent octave-block-offset)
472 ;; For (invalid) code between switch and case.
473 ;; (if (smie-parent-p "switch") 4)
476 (defvar electric-layout-rules)
479 (define-derived-mode octave-mode prog-mode "Octave"
480 "Major mode for editing Octave code.
482 Octave is a high-level language, primarily intended for numerical
483 computations. It provides a convenient command line interface
484 for solving linear and nonlinear problems numerically. Function
485 definitions can also be stored in files and used in batch mode."
486 :abbrev-table octave-abbrev-table
488 (smie-setup octave-smie-grammar #'octave-smie-rules
489 :forward-token #'octave-smie-forward-token
490 :backward-token #'octave-smie-backward-token)
491 (setq-local smie-indent-basic 'octave-block-offset)
493 (setq-local smie-blink-matching-triggers
494 (cons ?\; smie-blink-matching-triggers))
495 (unless octave-blink-matching-block
496 (remove-hook 'post-self-insert-hook #'smie-blink-matching-open 'local))
498 (setq-local electric-indent-chars
499 (cons ?\; electric-indent-chars))
500 ;; IIUC matlab-mode takes the opposite approach: it makes RET insert
501 ;; a ";" at those places where it's correct (i.e. outside of parens).
502 (setq-local electric-layout-rules '((?\; . after)))
504 (setq-local comment-start octave-comment-start)
505 (setq-local comment-end "")
506 ;; Don't set it here: it's not really a property of the language,
507 ;; just a personal preference of the author.
508 ;; (setq-local comment-column 32)
509 (setq-local comment-start-skip "\\s<+\\s-*")
510 (setq-local comment-add 1)
512 (setq-local parse-sexp-ignore-comments t)
513 (setq-local paragraph-start (concat "\\s-*$\\|" page-delimiter))
514 (setq-local paragraph-separate paragraph-start)
515 (setq-local paragraph-ignore-fill-prefix t)
516 (setq-local fill-paragraph-function 'octave-fill-paragraph)
517 ;; FIXME: Why disable it?
518 ;; (setq-local adaptive-fill-regexp nil)
519 ;; Again, this is not a property of the language, don't set it here.
520 ;; (setq fill-column 72)
521 (setq-local normal-auto-fill-function 'octave-auto-fill)
523 (setq font-lock-defaults '(octave-font-lock-keywords))
525 (setq-local syntax-propertize-function #'octave-syntax-propertize-function)
527 (setq imenu-generic-expression octave-mode-imenu-generic-expression)
528 (setq imenu-case-fold-search nil)
530 (add-hook 'completion-at-point-functions
531 'octave-completion-at-point-function nil t)
532 (add-hook 'before-save-hook 'octave-sync-function-file-names nil t)
533 (setq-local beginning-of-defun-function 'octave-beginning-of-defun)
534 (and octave-font-lock-texinfo-comment (octave-font-lock-texinfo-comment))
536 (easy-menu-add octave-mode-menu))
539 (defcustom inferior-octave-program "octave"
540 "Program invoked by `inferior-octave'."
544 (defcustom inferior-octave-buffer "*Inferior Octave*"
545 "Name of buffer for running an inferior Octave process."
549 (defcustom inferior-octave-prompt
550 "\\(^octave\\(\\|.bin\\|.exe\\)\\(-[.0-9]+\\)?\\(:[0-9]+\\)?\\|^debug\\|^\\)>+ "
551 "Regexp to match prompts for the inferior Octave process."
555 (defcustom inferior-octave-prompt-read-only comint-prompt-read-only
556 "If non-nil, the Octave prompt is read only.
557 See `comint-prompt-read-only' for details."
562 (defcustom inferior-octave-startup-file
563 (convert-standard-filename
564 (concat "~/.emacs-" (file-name-nondirectory inferior-octave-program)))
565 "Name of the inferior Octave startup file.
566 The contents of this file are sent to the inferior Octave process on
568 :type '(choice (const :tag "None" nil) file)
572 (defcustom inferior-octave-startup-args nil
573 "List of command line arguments for the inferior Octave process.
574 For example, for suppressing the startup message and using `traditional'
575 mode, set this to (\"-q\" \"--traditional\")."
576 :type '(repeat string)
579 (defcustom inferior-octave-mode-hook nil
580 "Hook to be run when Inferior Octave mode is started."
584 (defvar inferior-octave-process nil)
586 (defvar inferior-octave-mode-map
587 (let ((map (make-sparse-keymap)))
588 (set-keymap-parent map comint-mode-map)
589 (define-key map "\M-." 'octave-find-definition)
590 (define-key map "\t" 'comint-dynamic-complete)
591 (define-key map "\C-hd" 'octave-help)
592 (define-key map "\M-?" 'comint-dynamic-list-filename-completions)
593 (define-key map "\C-c\C-l" 'inferior-octave-dynamic-list-input-ring)
594 (define-key map [menu-bar inout list-history]
595 '("List Input History" . inferior-octave-dynamic-list-input-ring))
597 "Keymap used in Inferior Octave mode.")
599 (defvar inferior-octave-mode-syntax-table
600 (let ((table (make-syntax-table octave-mode-syntax-table)))
602 "Syntax table in use in inferior-octave-mode buffers.")
604 (defvar inferior-octave-font-lock-keywords
606 (cons inferior-octave-prompt 'font-lock-type-face))
607 ;; Could certainly do more font locking in inferior Octave ...
608 "Additional expressions to highlight in Inferior Octave mode.")
610 (defvar inferior-octave-output-list nil)
611 (defvar inferior-octave-output-string nil)
612 (defvar inferior-octave-receive-in-progress nil)
614 (define-obsolete-variable-alias 'inferior-octave-startup-hook
615 'inferior-octave-mode-hook "24.4")
617 (defvar inferior-octave-dynamic-complete-functions
618 '(inferior-octave-completion-at-point comint-filename-completion)
619 "List of functions called to perform completion for inferior Octave.
620 This variable is used to initialize `comint-dynamic-complete-functions'
621 in the Inferior Octave buffer.")
623 (defvar info-lookup-mode)
625 (define-derived-mode inferior-octave-mode comint-mode "Inferior Octave"
626 "Major mode for interacting with an inferior Octave process."
627 :abbrev-table octave-abbrev-table
628 (setq comint-prompt-regexp inferior-octave-prompt)
630 (setq-local comment-start octave-comment-start)
631 (setq-local comment-end "")
632 (setq comment-column 32)
633 (setq-local comment-start-skip octave-comment-start-skip)
635 (setq font-lock-defaults '(inferior-octave-font-lock-keywords nil nil))
637 (setq info-lookup-mode 'octave-mode)
639 (setq comint-input-ring-file-name
640 (or (getenv "OCTAVE_HISTFILE") "~/.octave_hist")
641 comint-input-ring-size (or (getenv "OCTAVE_HISTSIZE") 1024))
642 (setq-local comint-dynamic-complete-functions
643 inferior-octave-dynamic-complete-functions)
644 (setq-local comint-prompt-read-only inferior-octave-prompt-read-only)
645 (add-hook 'comint-input-filter-functions
646 'inferior-octave-directory-tracker nil t)
647 (comint-read-input-ring t))
650 (defun inferior-octave (&optional arg)
651 "Run an inferior Octave process, I/O via `inferior-octave-buffer'.
652 This buffer is put in Inferior Octave mode. See `inferior-octave-mode'.
654 Unless ARG is non-nil, switches to this buffer.
656 The elements of the list `inferior-octave-startup-args' are sent as
657 command line arguments to the inferior Octave process on startup.
659 Additional commands to be executed on startup can be provided either in
660 the file specified by `inferior-octave-startup-file' or by the default
661 startup file, `~/.emacs-octave'."
663 (let ((buffer (get-buffer-create inferior-octave-buffer)))
664 (unless (comint-check-proc buffer)
665 (with-current-buffer buffer
666 (inferior-octave-startup)
667 (inferior-octave-mode)))
669 (pop-to-buffer buffer))
673 (defalias 'run-octave 'inferior-octave)
675 (defun inferior-octave-startup ()
676 "Start an inferior Octave process."
677 (let ((proc (comint-exec-1
678 (substring inferior-octave-buffer 1 -1)
679 inferior-octave-buffer
680 inferior-octave-program
681 (append (list "-i" "--no-line-editing")
682 inferior-octave-startup-args))))
683 (set-process-filter proc 'inferior-octave-output-digest)
684 (setq inferior-octave-process proc
685 inferior-octave-output-list nil
686 inferior-octave-output-string nil
687 inferior-octave-receive-in-progress t)
689 ;; This may look complicated ... However, we need to make sure that
690 ;; we additional startup code only AFTER Octave is ready (otherwise,
691 ;; output may be mixed up). Hence, we need to digest the Octave
692 ;; output to see when it issues a prompt.
693 (while inferior-octave-receive-in-progress
694 (accept-process-output inferior-octave-process))
695 (goto-char (point-max))
696 (set-marker (process-mark proc) (point))
697 (insert-before-markers
699 (if (not (bobp)) "
\f\n")
700 (if inferior-octave-output-list
702 'identity inferior-octave-output-list "\n")
705 ;; An empty secondary prompt, as e.g. obtained by '--braindead',
707 (inferior-octave-send-list-and-digest (list "PS2\n"))
708 (when (string-match "\\(PS2\\|ans\\) = *$"
709 (car inferior-octave-output-list))
710 (inferior-octave-send-list-and-digest (list "PS2 (\"> \");\n")))
712 ;; O.K., now we are ready for the Inferior Octave startup commands.
713 (inferior-octave-send-list-and-digest
715 (unless (equal inferior-octave-output-string ">> ")
716 "PS1 (\"\\\\s> \");\n")
717 (when (and inferior-octave-startup-file
718 (file-exists-p inferior-octave-startup-file))
719 (format "source (\"%s\");\n" inferior-octave-startup-file))))
720 ;; XXX: the first prompt is incorrectly highlighted
721 (insert-before-markers
723 (if inferior-octave-output-list
725 'identity inferior-octave-output-list "\n")
727 inferior-octave-output-string))
729 ;; And finally, everything is back to normal.
730 (set-process-filter proc 'inferior-octave-output-filter)
731 ;; Just in case, to be sure a cd in the startup file
732 ;; won't have detrimental effects.
733 (inferior-octave-resync-dirs)))
735 (defun inferior-octave-completion-table ()
736 (completion-table-dynamic
738 (inferior-octave-send-list-and-digest
739 (list (concat "completion_matches (\"" command "\");\n")))
740 (sort (delete-dups inferior-octave-output-list)
743 (defun inferior-octave-completion-at-point ()
744 "Return the data to complete the Octave symbol at point."
748 (skip-syntax-backward "w_" (comint-line-beginning-position))
751 (list start end (inferior-octave-completion-table)))))
753 (define-obsolete-function-alias 'inferior-octave-complete
754 'completion-at-point "24.1")
756 (defun inferior-octave-dynamic-list-input-ring ()
757 "List the buffer's input history in a help buffer."
758 ;; We cannot use `comint-dynamic-list-input-ring', because it replaces
759 ;; "completion" by "history reference" ...
761 (if (or (not (ring-p comint-input-ring))
762 (ring-empty-p comint-input-ring))
763 (message "No history")
765 (history-buffer " *Input History*")
766 (index (1- (ring-length comint-input-ring)))
767 (conf (current-window-configuration)))
768 ;; We have to build up a list ourselves from the ring vector.
770 (setq history (cons (ring-ref comint-input-ring index) history)
772 ;; Change "completion" to "history reference"
773 ;; to make the display accurate.
774 (with-output-to-temp-buffer history-buffer
775 (display-completion-list history)
776 (set-buffer history-buffer))
777 (message "Hit space to flush")
778 (let ((ch (read-event)))
780 (set-window-configuration conf)
781 (setq unread-command-events (list ch)))))))
783 (defun inferior-octave-strip-ctrl-g (string)
784 "Strip leading `^G' character.
785 If STRING starts with a `^G', ring the bell and strip it."
786 (if (string-match "^\a" string)
789 (setq string (substring string 1))))
792 (defun inferior-octave-output-filter (proc string)
793 "Standard output filter for the inferior Octave process.
794 Ring Emacs bell if process output starts with an ASCII bell, and pass
795 the rest to `comint-output-filter'."
796 (comint-output-filter proc (inferior-octave-strip-ctrl-g string)))
798 (defun inferior-octave-output-digest (_proc string)
799 "Special output filter for the inferior Octave process.
800 Save all output between newlines into `inferior-octave-output-list', and
801 the rest to `inferior-octave-output-string'."
802 (setq string (concat inferior-octave-output-string string))
803 (while (string-match "\n" string)
804 (setq inferior-octave-output-list
805 (append inferior-octave-output-list
806 (list (substring string 0 (match-beginning 0))))
807 string (substring string (match-end 0))))
808 (if (string-match inferior-octave-prompt string)
809 (setq inferior-octave-receive-in-progress nil))
810 (setq inferior-octave-output-string string))
812 (defun inferior-octave-send-list-and-digest (list)
813 "Send LIST to the inferior Octave process and digest the output.
814 The elements of LIST have to be strings and are sent one by one. All
815 output is passed to the filter `inferior-octave-output-digest'."
816 (let* ((proc inferior-octave-process)
817 (filter (process-filter proc))
819 (set-process-filter proc 'inferior-octave-output-digest)
820 (setq inferior-octave-output-list nil)
822 (while (setq string (car list))
823 (setq inferior-octave-output-string nil
824 inferior-octave-receive-in-progress t)
825 (comint-send-string proc string)
826 (while inferior-octave-receive-in-progress
827 (accept-process-output proc))
828 (setq list (cdr list)))
829 (set-process-filter proc filter))))
831 (defun inferior-octave-directory-tracker (string)
832 "Tracks `cd' commands issued to the inferior Octave process.
833 Use \\[inferior-octave-resync-dirs] to resync if Emacs gets confused."
835 ((string-match "^[ \t]*cd[ \t;]*$" string)
837 ((string-match "^[ \t]*cd[ \t]+\\([^ \t\n;]*\\)[ \t\n;]*" string)
838 (cd (substring string (match-beginning 1) (match-end 1))))))
840 (defun inferior-octave-resync-dirs ()
841 "Resync the buffer's idea of the current directory.
842 This command queries the inferior Octave process about its current
843 directory and makes this the current buffer's default directory."
845 (inferior-octave-send-list-and-digest '("disp (pwd ())\n"))
846 (cd (car inferior-octave-output-list)))
849 ;;; Miscellaneous useful functions
851 (defun octave-in-comment-p ()
852 "Return non-nil if point is inside an Octave comment."
853 (nth 4 (syntax-ppss)))
855 (defun octave-in-string-p ()
856 "Return non-nil if point is inside an Octave string."
857 (nth 3 (syntax-ppss)))
859 (defun octave-in-string-or-comment-p ()
860 "Return non-nil if point is inside an Octave string or comment."
861 (nth 8 (syntax-ppss)))
863 (defun octave-looking-at-kw (regexp)
864 "Like `looking-at', but sets `case-fold-search' nil."
865 (let ((case-fold-search nil))
866 (looking-at regexp)))
868 (defun octave-maybe-insert-continuation-string ()
869 (if (or (octave-in-comment-p)
872 (looking-at octave-continuation-regexp)))
874 (delete-horizontal-space)
875 (insert (concat " " octave-continuation-string))))
877 (defun octave-completing-read ()
878 (let ((def (or (thing-at-point 'symbol)
880 (skip-syntax-backward "-(")
881 (thing-at-point 'symbol)))))
883 (format (if def "Function (default %s): "
885 (inferior-octave-completion-table)
886 nil nil nil nil def)))
888 (defun octave-goto-function-definition ()
889 "Go to the first function definition."
890 (when (save-excursion
891 (goto-char (point-min))
892 (re-search-forward octave-function-header-regexp nil t))
893 (goto-char (match-beginning 3))
896 (defun octave-function-file-p ()
897 "Return non-nil if the first token is \"function\".
898 The value is (START END NAME-START NAME-END) of the function."
900 (goto-char (point-min))
901 (when (equal (funcall smie-forward-token-function) "function")
903 (let* ((start (point))
904 (end (progn (forward-sexp 1) (point)))
907 (re-search-forward octave-function-header-regexp
909 (list (match-beginning 3) (match-end 3)))))
910 (cons start (cons end name))))))
912 ;; Like forward-comment but stop at non-comment blank
913 (defun octave-skip-comment-forward (limit)
914 (let ((ppss (syntax-ppss)))
916 (goto-char (nth 8 ppss))
917 (goto-char (or (comment-search-forward limit t) (point)))))
918 (while (and (< (point) limit) (looking-at-p "\\s<"))
919 (forward-comment 1)))
921 ;;; First non-copyright comment block
922 (defun octave-function-file-comment ()
923 "Beginning and end positions of the function file comment."
925 (goto-char (point-min))
926 ;; Copyright block: octave/libinterp/parse-tree/lex.ll around line 1634
927 (while (save-excursion
928 (when (comment-search-forward (point-max) t)
929 (when (eq (char-after) ?\{) ; case of block comment
931 (skip-syntax-forward "-")
932 (let ((case-fold-search t))
933 (looking-at-p "\\(?:copyright\\|author\\)\\_>"))))
934 (octave-skip-comment-forward (point-max)))
935 (let ((beg (comment-search-forward (point-max) t)))
938 (octave-skip-comment-forward (point-max))
939 (list beg (point))))))
941 (defun octave-sync-function-file-names ()
942 "Ensure function name agree with function file name.
943 See Info node `(octave)Function Files'."
945 (when buffer-file-name
946 (pcase-let ((`(,start ,_end ,name-start ,name-end)
947 (octave-function-file-p)))
948 (when (and start name-start)
949 (let* ((func (buffer-substring name-start name-end))
950 (file (file-name-sans-extension
951 (file-name-nondirectory buffer-file-name)))
952 (help-form (format "\
953 a: Use function name `%s'
954 b: Use file name `%s'
955 q: Don't fix\n" func file))
956 (c (unless (equal file func)
957 (save-window-excursion
960 "Which name to use? (a/b/q) " '(?a ?b ?q))))))
962 (`?a (let ((newname (expand-file-name
963 (concat func (file-name-extension
964 buffer-file-name t)))))
965 (when (or (not (file-exists-p newname))
967 (format "Target file %s exists; proceed? " newname)))
968 (when (file-exists-p buffer-file-name)
969 (rename-file buffer-file-name newname t))
970 (set-visited-file-name newname))))
972 (goto-char name-start)
973 (delete-region name-start name-end)
974 (insert file)))))))))
976 (defun octave-update-function-file-comment (beg end)
977 "Query replace function names in function file comment."
980 (barf-if-buffer-read-only)
982 (list (region-beginning) (region-end))
983 (or (octave-function-file-comment)
984 (error "No function file comment found")))))
986 (let* ((bounds (or (octave-function-file-p)
987 (error "Not in a function file buffer")))
988 (func (if (cddr bounds)
989 (apply #'buffer-substring (cddr bounds))
990 (error "Function name not found")))
993 (when (re-search-forward
994 "[=}]\\s-*\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>"
995 (min (line-end-position 4) end)
998 (old-func (read-string (format (if old-func
999 "Name to replace (default %s): "
1000 "Name to replace: ")
1003 (if (and func old-func (not (equal func old-func)))
1004 (perform-replace old-func func 'query
1005 nil 'delimited nil nil beg end)
1006 (message "Function names match")))))
1008 ;; Adapted from texinfo-font-lock-keywords
1009 (defvar octave-texinfo-font-lock-keywords
1010 `(("@\\([a-zA-Z]+\\|[^ \t\n]\\)" 1 font-lock-keyword-face prepend) ;commands
1011 ("^\\*\\([^\n:]*\\)" 1 font-lock-function-name-face prepend) ;menu items
1012 ("@\\(emph\\|i\\|sc\\){\\([^}]+\\)" 2 'italic prepend)
1013 ("@\\(strong\\|b\\){\\([^}]+\\)" 2 'bold prepend)
1014 ("@\\(kbd\\|key\\|url\\|uref\\){\\([^}]+\\)"
1015 2 font-lock-string-face prepend)
1016 ("@\\(file\\|email\\){\\([^}]+\\)" 2 font-lock-string-face prepend)
1017 ("@\\(samp\\|code\\|var\\|math\\|env\\|command\\|option\\){\\([^}]+\\)"
1018 2 font-lock-variable-name-face prepend)
1019 ("@\\(cite\\|x?ref\\|pxref\\|dfn\\|inforef\\){\\([^}]+\\)"
1020 2 font-lock-constant-face prepend)
1021 ("@\\(anchor\\){\\([^}]+\\)" 2 font-lock-type-face prepend)
1022 ("@\\(dmn\\|acronym\\|value\\){\\([^}]+\\)"
1023 2 font-lock-builtin-face prepend)
1024 ("@\\(end\\|itemx?\\) +\\(.+\\)" 2 font-lock-keyword-face prepend))
1025 "Additional keywords to highlight in texinfo comment block.")
1027 (defface octave-function-comment-block
1028 '((t (:inherit font-lock-doc-face)))
1029 "Face used to highlight function comment block."
1032 (defun octave-font-lock-texinfo-comment ()
1033 (font-lock-add-keywords
1036 (while (and (search-forward "-*- texinfo -*-" limit t)
1037 (octave-in-comment-p))
1038 (let ((beg (nth 8 (syntax-ppss)))
1040 (octave-skip-comment-forward (point-max))
1042 (put-text-property beg end 'font-lock-multiline t)
1043 (font-lock-prepend-text-property
1044 beg end 'face 'octave-function-comment-block)
1045 (dolist (kw octave-texinfo-font-lock-keywords)
1047 (while (re-search-forward (car kw) end 'move)
1048 (font-lock-apply-highlight (cdr kw))))))
1055 (defun octave-indent-new-comment-line ()
1056 "Break Octave line at point, continuing comment if within one.
1057 If within code, insert `octave-continuation-string' before breaking the
1058 line. If within a string, signal an error.
1059 The new line is properly indented."
1061 (delete-horizontal-space)
1063 ((octave-in-comment-p)
1064 (indent-new-comment-line))
1065 ((octave-in-string-p)
1066 (error "Cannot split a code line inside a string"))
1068 (insert (concat " " octave-continuation-string))
1069 (reindent-then-newline-and-indent))))
1071 (defun octave-indent-defun ()
1072 "Properly indent the Octave function which contains point."
1076 (message "Indenting function...")
1077 (indent-region (point) (mark) nil))
1078 (message "Indenting function...done."))
1082 (defun octave-next-code-line (&optional arg)
1083 "Move ARG lines of Octave code forward (backward if ARG is negative).
1084 Skips past all empty and comment lines. Default for ARG is 1.
1086 On success, return 0. Otherwise, go as far as possible and return -1."
1088 (or arg (setq arg 1))
1091 (inc (if (> arg 0) 1 -1)))
1092 (while (and (/= arg 0) (= n 0))
1093 (setq n (forward-line inc))
1095 (looking-at "\\s-*\\($\\|\\s<\\)"))
1096 (setq n (forward-line inc)))
1097 (setq arg (- arg inc)))
1100 (defun octave-previous-code-line (&optional arg)
1101 "Move ARG lines of Octave code backward (forward if ARG is negative).
1102 Skips past all empty and comment lines. Default for ARG is 1.
1104 On success, return 0. Otherwise, go as far as possible and return -1."
1106 (or arg (setq arg 1))
1107 (octave-next-code-line (- arg)))
1109 (defun octave-beginning-of-line ()
1110 "Move point to beginning of current Octave line.
1111 If on an empty or comment line, go to the beginning of that line.
1112 Otherwise, move backward to the beginning of the first Octave code line
1113 which is not inside a continuation statement, i.e., which does not
1114 follow a code line ending in `...' or `\\', or is inside an open
1118 (if (not (looking-at "\\s-*\\($\\|\\s<\\)"))
1119 (while (or (condition-case nil
1125 (and (or (looking-at "\\s-*\\($\\|\\s<\\)")
1127 (if (zerop (octave-previous-code-line))
1128 (looking-at octave-continuation-regexp))))
1129 (zerop (forward-line -1)))))))
1131 (defun octave-end-of-line ()
1132 "Move point to end of current Octave line.
1133 If on an empty or comment line, go to the end of that line.
1134 Otherwise, move forward to the end of the first Octave code line which
1135 does not end in `...' or `\\' or is inside an open parenthesis list."
1140 (looking-at "\\s-*\\($\\|\\s<\\)"))
1142 (while (or (condition-case nil
1148 (and (save-excursion
1150 (or (looking-at "\\s-*\\($\\|\\s<\\)")
1151 (looking-at octave-continuation-regexp)))
1152 (zerop (forward-line 1)))))
1155 (defun octave-mark-block ()
1156 "Put point at the beginning of this Octave block, mark at the end.
1157 The block marked is the one that contains point or follows point."
1159 (if (and (looking-at "\\sw\\|\\s_")
1160 (looking-back "\\sw\\|\\s_" (1- (point))))
1161 (skip-syntax-forward "w_"))
1162 (unless (or (looking-at "\\s(")
1164 (let* ((token (funcall smie-forward-token-function))
1165 (level (assoc token smie-grammar)))
1166 (and level (not (numberp (cadr level)))))))
1167 (backward-up-list 1))
1170 (defun octave-beginning-of-defun (&optional arg)
1171 "Move backward to the beginning of an Octave function.
1172 With positive ARG, do it that many times. Negative argument -N means
1173 move forward to Nth following beginning of a function.
1174 Returns t unless search stops at the beginning or end of the buffer."
1175 (let* ((arg (or arg 1))
1176 (inc (if (> arg 0) 1 -1))
1178 (case-fold-search nil))
1180 (not (and (> arg 0) (looking-at "\\_<function\\_>")))
1181 (skip-syntax-forward "w"))
1182 (while (and (/= arg 0)
1184 (re-search-backward "\\_<function\\_>" inc)))
1185 (unless (octave-in-string-or-comment-p)
1186 (setq arg (- arg inc))))
1189 (and (< inc 0) (goto-char (match-beginning 0)))
1194 (defun octave-auto-fill ()
1195 "Perform auto-fill in Octave mode.
1196 Returns nil if no feasible place to break the line could be found, and t
1199 (if (or (null (setq fc (current-fill-column)))
1202 (and auto-fill-inhibit-regexp
1203 (octave-looking-at-kw auto-fill-inhibit-regexp))))
1204 nil ; Can't do anything
1205 (if (and (not (octave-in-comment-p))
1206 (> (current-column) fc))
1207 (setq fc (- fc (+ (length octave-continuation-string) 1))))
1208 (while (and (not give-up) (> (current-column) fc))
1209 (let* ((opoint (point))
1212 (move-to-column (+ fc 1))
1213 (skip-chars-backward "^ \t\n")
1214 ;; If we're at the beginning of the line, break after
1217 (re-search-forward "[ \t]" opoint t))
1218 ;; If we're in a comment line, don't break after the
1221 (skip-syntax-backward " <")
1223 (re-search-forward "[ \t]" (line-end-position)
1225 ;; If we're not in a comment line and just ahead the
1226 ;; continuation string, don't break here.
1227 (if (and (not (octave-in-comment-p))
1231 octave-continuation-string)
1234 (skip-chars-backward " \t")
1238 (not (or (bolp) (eolp))))
1239 (let ((prev-column (current-column)))
1241 (skip-chars-backward " \t")
1244 (octave-maybe-insert-continuation-string)
1245 (indent-new-comment-line t))
1248 (octave-maybe-insert-continuation-string)
1249 (indent-new-comment-line t)))
1250 (if (>= (current-column) prev-column)
1255 (defun octave-fill-paragraph (&optional _arg)
1256 "Fill paragraph of Octave code, handling Octave comments."
1257 ;; FIXME: difference with generic fill-paragraph:
1258 ;; - code lines are only split, never joined.
1259 ;; - \n that end comments are never removed.
1260 ;; - insert continuation marker when splitting code lines.
1263 (let ((end (progn (forward-paragraph) (copy-marker (point) t)))
1265 (forward-paragraph -1)
1266 (skip-chars-forward " \t\n")
1269 (cfc (current-fill-column))
1272 (while (< (point) end)
1274 (indent-according-to-mode)
1276 (move-to-column cfc)
1277 ;; First check whether we need to combine non-empty comment lines
1278 (if (and (< (current-column) cfc)
1279 (octave-in-comment-p)
1280 (not (save-excursion
1282 (looking-at "^\\s-*\\s<+\\s-*$"))))
1283 ;; This is a nonempty comment line which does not extend
1284 ;; past the fill column. If it is followed by a nonempty
1285 ;; comment line with the same comment prefix, try to
1286 ;; combine them, and repeat this until either we reach the
1287 ;; fill-column or there is nothing more to combine.
1289 ;; Get the comment prefix
1292 (while (and (re-search-forward "\\s<+")
1293 (not (octave-in-comment-p))))
1294 (setq comment-prefix (match-string 0)))
1295 ;; And keep combining ...
1296 (while (and (< (current-column) cfc)
1308 (re-search-forward comment-prefix)
1309 (delete-region (match-beginning 0) (match-end 0))
1311 (move-to-column cfc))))
1312 ;; We might also try to combine continued code lines> Perhaps
1313 ;; some other time ...
1314 (skip-chars-forward "^ \t\n")
1315 (delete-horizontal-space)
1316 (if (or (< (current-column) cfc)
1317 (and (= (current-column) cfc) (eolp)))
1319 (if (not (eolp)) (insert " "))
1320 (or (octave-auto-fill)
1327 (defun octave-completion-at-point-function ()
1328 "Find the text to complete and the corresponding table."
1329 (let* ((beg (save-excursion (skip-syntax-backward "w_") (point)))
1332 ;; Extend region past point, if applicable.
1333 (save-excursion (skip-syntax-forward "w_")
1334 (setq end (point))))
1335 (list beg end (or (and inferior-octave-process
1336 (process-live-p inferior-octave-process)
1337 (inferior-octave-completion-table))
1338 (append octave-reserved-words
1339 octave-text-functions)))))
1341 (define-obsolete-function-alias 'octave-complete-symbol
1342 'completion-at-point "24.1")
1344 ;;; Electric characters && friends
1345 (define-skeleton octave-insert-defun
1346 "Insert an Octave function skeleton.
1347 Prompt for the function's name, arguments and return values (to be
1348 entered without parens)."
1349 (let* ((defname (file-name-sans-extension (buffer-name)))
1350 (name (read-string (format "Function name (default %s): " defname)
1352 (args (read-string "Arguments: "))
1353 (vals (read-string "Return values: ")))
1356 ((string-equal vals "") vals)
1357 ((string-match "[ ,]" vals) (concat "[" vals "] = "))
1358 (t (concat vals " = ")))
1361 \n octave-block-comment-start "usage: " str \n
1362 octave-block-comment-start '(delete-horizontal-space) \n
1363 octave-block-comment-start '(delete-horizontal-space) \n
1364 "function " > str \n
1368 ;;; Communication with the inferior Octave process
1369 (defun octave-kill-process ()
1370 "Kill inferior Octave process and its buffer."
1372 (if inferior-octave-process
1374 (process-send-string inferior-octave-process "quit;\n")
1375 (accept-process-output inferior-octave-process)))
1376 (if inferior-octave-buffer
1377 (kill-buffer inferior-octave-buffer)))
1379 (defun octave-show-process-buffer ()
1380 "Make sure that `inferior-octave-buffer' is displayed."
1382 (if (get-buffer inferior-octave-buffer)
1383 (display-buffer inferior-octave-buffer)
1384 (message "No buffer named %s" inferior-octave-buffer)))
1386 (defun octave-hide-process-buffer ()
1387 "Delete all windows that display `inferior-octave-buffer'."
1389 (if (get-buffer inferior-octave-buffer)
1390 (delete-windows-on inferior-octave-buffer)
1391 (message "No buffer named %s" inferior-octave-buffer)))
1393 (defun octave-send-region (beg end)
1394 "Send current region to the inferior Octave process."
1397 (let ((proc inferior-octave-process)
1398 (string (buffer-substring-no-properties beg end))
1400 (with-current-buffer inferior-octave-buffer
1401 (setq inferior-octave-output-list nil)
1402 (while (not (string-equal string ""))
1403 (if (string-match "\n" string)
1404 (setq line (substring string 0 (match-beginning 0))
1405 string (substring string (match-end 0)))
1406 (setq line string string ""))
1407 (setq inferior-octave-receive-in-progress t)
1408 (inferior-octave-send-list-and-digest (list (concat line "\n")))
1409 (while inferior-octave-receive-in-progress
1410 (accept-process-output proc))
1411 (insert-before-markers
1412 (mapconcat 'identity
1414 (if octave-send-echo-input (list line) (list ""))
1415 (mapcar 'inferior-octave-strip-ctrl-g
1416 inferior-octave-output-list)
1417 (list inferior-octave-output-string))
1419 (if octave-send-show-buffer
1420 (display-buffer inferior-octave-buffer)))
1422 (defun octave-send-block ()
1423 "Send current Octave block to the inferior Octave process."
1427 (octave-send-region (point) (mark))))
1429 (defun octave-send-defun ()
1430 "Send current Octave function to the inferior Octave process."
1434 (octave-send-region (point) (mark))))
1436 (defun octave-send-line (&optional arg)
1437 "Send current Octave code line to the inferior Octave process.
1438 With positive prefix ARG, send that many lines.
1439 If `octave-send-line-auto-forward' is non-nil, go to the next unsent
1442 (or arg (setq arg 1))
1447 (octave-next-code-line (- arg 1))
1450 (if octave-send-line-auto-forward
1451 (octave-next-code-line 1))
1452 (octave-send-region beg end))))
1454 (defun octave-eval-print-last-sexp ()
1455 "Evaluate Octave sexp before point and print value into current buffer."
1458 (let ((standard-output (current-buffer))
1459 (print-escape-newlines nil)
1465 (inferior-octave-send-list-and-digest
1466 (list (concat (buffer-substring-no-properties (point) opoint)
1468 (mapconcat 'identity inferior-octave-output-list "\n")))
1473 (defcustom octave-help-buffer "*Octave Help*"
1474 "Buffer name for `octave-help'."
1479 (define-button-type 'octave-help-file
1481 'action #'help-button-action
1482 'help-function (lambda (fn)
1484 (octave-goto-function-definition)))
1486 (define-button-type 'octave-help-function
1490 (buffer-substring (button-start b) (button-end b)))))
1492 (defvar help-xref-following)
1494 (defun octave-help (fn)
1495 "Display the documentation of FN."
1496 (interactive (list (octave-completing-read)))
1497 (inferior-octave-send-list-and-digest
1498 (list (format "help \"%s\"\n" fn)))
1499 (let ((lines inferior-octave-output-list))
1500 (when (string-match "error: \\(.*\\)$" (car lines))
1501 (error "%s" (match-string 1 (car lines))))
1502 (with-help-window octave-help-buffer
1503 (princ (mapconcat 'identity lines "\n"))
1504 (with-current-buffer octave-help-buffer
1505 ;; Bound to t so that `help-buffer' returns current buffer for
1506 ;; `help-setup-xref'.
1507 (let ((help-xref-following t))
1508 (help-setup-xref (list 'octave-help fn)
1509 (called-interactively-p 'interactive)))
1510 ;; Note: can be turned off by suppress_verbose_help_message.
1512 ;; Remove boring trailing text: Additional help for built-in functions
1513 ;; and operators ...
1514 (goto-char (point-max))
1515 (when (search-backward "\n\n\n" nil t)
1516 (goto-char (match-beginning 0))
1517 (delete-region (point) (point-max)))
1518 ;; File name highlight
1519 (goto-char (point-min))
1520 (when (re-search-forward "from the file \\(.*\\)$"
1523 (let ((file (match-string 1)))
1524 (replace-match "" nil nil nil 1)
1526 (help-insert-xref-button (file-name-nondirectory file)
1527 'octave-help-file file)
1529 ;; Make 'See also' clickable
1530 (with-syntax-table octave-mode-syntax-table
1531 (when (re-search-forward "^\\s-*See also:" nil t)
1532 (while (re-search-forward "\\_<\\(?:\\sw\\|\\s_\\)+\\_>" nil t)
1533 (make-text-button (match-beginning 0)
1535 :type 'octave-help-function))))))))
1537 (defvar find-tag-marker-ring)
1539 (defun octave-find-definition (fn)
1540 "Find the definition of FN."
1541 (interactive (list (octave-completing-read)))
1542 (inferior-octave-send-list-and-digest
1543 ;; help NAME is more verbose
1545 if iskeyword(\"%s\") disp(\"`%s' is a keyword\") else which(\"%s\") endif\n"
1547 (let* ((line (car inferior-octave-output-list))
1548 (file (when (and line (string-match "from the file \\(.*\\)$" line))
1549 (match-string 1 line))))
1551 (user-error "%s" (or line (format "`%s' not found" fn)))
1553 (ring-insert find-tag-marker-ring (point-marker))
1555 (octave-goto-function-definition))))
1559 ;;; octave.el ends here