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 'user-error
)
41 (defalias 'user-error
'error
))
42 (unless (fboundp 'delete-consecutive-dups
)
43 (defalias 'delete-consecutive-dups
'delete-dups
)))
45 (unless (fboundp 'setq-local
)
46 (defmacro setq-local
(var val
)
47 "Set variable VAR to value VAL in current buffer."
48 (list 'set
(list 'make-local-variable
(list 'quote var
)) val
))))
51 "Editing Octave code."
52 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
55 (define-obsolete-function-alias 'octave-submit-bug-report
56 'report-emacs-bug
"24.4")
58 (define-abbrev-table 'octave-abbrev-table nil
59 "Abbrev table for Octave's reserved words.
60 Used in `octave-mode' and `inferior-octave-mode' buffers.")
62 (defvar octave-comment-char ?
#
63 "Character to start an Octave comment.")
65 (defvar octave-comment-start
(char-to-string octave-comment-char
)
66 "Octave-specific `comment-start' (which see).")
68 (defvar octave-comment-start-skip
"\\(^\\|\\S<\\)\\(?:%!\\|\\s<+\\)\\s-*"
69 "Octave-specific `comment-start-skip' (which see).")
71 (defvar octave-begin-keywords
72 '("classdef" "do" "enumeration" "events" "for" "function" "if" "methods"
73 "parfor" "properties" "switch" "try" "unwind_protect" "while"))
75 (defvar octave-else-keywords
76 '("case" "catch" "else" "elseif" "otherwise" "unwind_protect_cleanup"))
78 (defvar octave-end-keywords
79 '("endclassdef" "endenumeration" "endevents" "endfor" "endfunction" "endif"
80 "endmethods" "endparfor" "endproperties" "endswitch" "end_try_catch"
81 "end_unwind_protect" "endwhile" "until" "end"))
83 (defvar octave-reserved-words
84 (append octave-begin-keywords
87 '("break" "continue" "global" "persistent" "return"))
88 "Reserved words in Octave.")
90 (defvar octave-function-header-regexp
91 (concat "^\\s-*\\_<\\(function\\)\\_>"
92 "\\([^=;(\n]*=[ \t]*\\|[ \t]*\\)\\(\\(?:\\w\\|\\s_\\)+\\)\\_>")
93 "Regexp to match an Octave function header.
94 The string `function' and its name are given by the first and third
95 parenthetical grouping.")
98 (defvar octave-mode-map
99 (let ((map (make-sparse-keymap)))
100 (define-key map
"\M-." 'octave-find-definition
)
101 (define-key map
"\M-\C-j" 'octave-indent-new-comment-line
)
102 (define-key map
"\C-c\C-p" 'octave-previous-code-line
)
103 (define-key map
"\C-c\C-n" 'octave-next-code-line
)
104 (define-key map
"\C-c\C-a" 'octave-beginning-of-line
)
105 (define-key map
"\C-c\C-e" 'octave-end-of-line
)
106 (define-key map
[remap down-list
] 'smie-down-list
)
107 (define-key map
"\C-c\M-\C-h" 'octave-mark-block
)
108 (define-key map
"\C-c]" 'smie-close-block
)
109 (define-key map
"\C-c/" 'smie-close-block
)
110 (define-key map
"\C-c;" 'octave-update-function-file-comment
)
111 (define-key map
"\C-hd" 'octave-help
)
112 (define-key map
"\C-c\C-f" 'octave-insert-defun
)
113 (define-key map
"\C-c\C-il" 'octave-send-line
)
114 (define-key map
"\C-c\C-ib" 'octave-send-block
)
115 (define-key map
"\C-c\C-if" 'octave-send-defun
)
116 (define-key map
"\C-c\C-ir" 'octave-send-region
)
117 (define-key map
"\C-c\C-is" 'octave-show-process-buffer
)
118 (define-key map
"\C-c\C-iq" 'octave-hide-process-buffer
)
119 (define-key map
"\C-c\C-ik" 'octave-kill-process
)
120 (define-key map
"\C-c\C-i\C-l" 'octave-send-line
)
121 (define-key map
"\C-c\C-i\C-b" 'octave-send-block
)
122 (define-key map
"\C-c\C-i\C-f" 'octave-send-defun
)
123 (define-key map
"\C-c\C-i\C-r" 'octave-send-region
)
124 (define-key map
"\C-c\C-i\C-s" 'octave-show-process-buffer
)
125 (define-key map
"\C-c\C-i\C-q" 'octave-hide-process-buffer
)
126 (define-key map
"\C-c\C-i\C-k" 'octave-kill-process
)
128 "Keymap used in Octave mode.")
132 (easy-menu-define octave-mode-menu octave-mode-map
133 "Menu for Octave mode."
135 ["Split Line at Point" octave-indent-new-comment-line t
]
136 ["Previous Code Line" octave-previous-code-line t
]
137 ["Next Code Line" octave-next-code-line t
]
138 ["Begin of Line" octave-beginning-of-line t
]
139 ["End of Line" octave-end-of-line t
]
140 ["Mark Block" octave-mark-block t
]
141 ["Close Block" smie-close-block t
]
143 ["Start Octave Process" run-octave t
]
144 ["Documentation Lookup" info-lookup-symbol t
]
145 ["Help on Function" octave-help t
]
146 ["Find Function Definition" octave-find-definition t
]
147 ["Insert Function" octave-insert-defun
t]
148 ["Update Function File Comment" octave-update-function-file-comment t
]
150 ["Function Syntax Hints" (call-interactively
151 (if (fboundp 'eldoc-post-insert-mode
)
152 'eldoc-post-insert-mode
154 :style toggle
:selected
(or eldoc-post-insert-mode eldoc-mode
)
155 :help
"Display function signatures after typing `SPC' or `('"]
156 ["Delimiter Matching" show-paren-mode
157 :style toggle
:selected show-paren-mode
158 :help
"Highlight matched pairs such as `if ... end'"
159 :visible
(fboundp 'smie--matching-block-data
)]
160 ["Auto Fill" auto-fill-mode
161 :style toggle
:selected auto-fill-function
162 :help
"Automatic line breaking"]
163 ["Electric Layout" electric-layout-mode
164 :style toggle
:selected electric-layout-mode
165 :help
"Automatically insert newlines around some chars"]
168 ["Send Current Line" octave-send-line t
]
169 ["Send Current Block" octave-send-block t
]
170 ["Send Current Function" octave-send-defun
t]
171 ["Send Region" octave-send-region t
]
172 ["Show Process Buffer" octave-show-process-buffer t
]
173 ["Hide Process Buffer" octave-hide-process-buffer t
]
174 ["Kill Process" octave-kill-process t
])
176 ["Customize Octave" (customize-group 'octave
) t
]
177 ["Submit Bug Report" report-emacs-bug t
]))
179 (defvar octave-mode-syntax-table
180 (let ((table (make-syntax-table)))
181 (modify-syntax-entry ?
\r " " table
)
182 (modify-syntax-entry ?
+ "." table
)
183 (modify-syntax-entry ?-
"." table
)
184 (modify-syntax-entry ?
= "." table
)
185 (modify-syntax-entry ?
* "." table
)
186 (modify-syntax-entry ?
/ "." table
)
187 (modify-syntax-entry ?
> "." table
)
188 (modify-syntax-entry ?
< "." table
)
189 (modify-syntax-entry ?
& "." table
)
190 (modify-syntax-entry ?|
"." table
)
191 (modify-syntax-entry ?
! "." table
)
192 (modify-syntax-entry ?
\\ "." table
)
193 (modify-syntax-entry ?
\' "." table
)
194 (modify-syntax-entry ?\
` "." table
)
195 (modify-syntax-entry ?.
"." table
)
196 (modify-syntax-entry ?
\" "\"" table
)
197 (modify-syntax-entry ?_
"_" table
)
198 ;; The "b" flag only applies to the second letter of the comstart
199 ;; and the first letter of the comend, i.e. the "4b" below is ineffective.
200 ;; If we try to put `b' on the single-line comments, we get a similar
201 ;; problem where the % and # chars appear as first chars of the 2-char
202 ;; comend, so the multi-line ender is also turned into style-b.
203 ;; So we need the new "c" comment style.
204 (modify-syntax-entry ?\%
"< 13" table
)
205 (modify-syntax-entry ?\
# "< 13" table
)
206 (modify-syntax-entry ?\
{ "(} 2c" table
)
207 (modify-syntax-entry ?\
} "){ 4c" table
)
208 (modify-syntax-entry ?
\n ">" table
)
210 "Syntax table in use in `octave-mode' buffers.")
212 (defcustom octave-font-lock-texinfo-comment t
213 "Control whether to highlight the texinfo comment block."
218 (defcustom octave-blink-matching-block t
219 "Control the blinking of matching Octave block keywords.
220 Non-nil means show matching begin of block when inserting a space,
221 newline or semicolon after an else or end keyword."
225 (defcustom octave-block-offset
2
226 "Extra indentation applied to statements in Octave block structures."
230 (defvar octave-block-comment-start
231 (concat (make-string 2 octave-comment-char
) " ")
232 "String to insert to start a new Octave comment on an empty line.")
234 (defcustom octave-continuation-offset
4
235 "Extra indentation applied to Octave continuation lines."
240 (defconst octave-continuation-marker-regexp
"\\\\\\|\\.\\.\\."))
242 (defvar octave-continuation-regexp
243 (concat "[^#%\n]*\\(" octave-continuation-marker-regexp
244 "\\)\\s-*\\(\\s<.*\\)?$"))
246 ;; Char \ is considered a bad decision for continuing a line.
247 (defconst octave-continuation-string
"..."
248 "Character string used for Octave continuation lines.")
250 (defvar octave-mode-imenu-generic-expression
253 (list nil octave-function-header-regexp
3))
254 "Imenu expression for Octave mode. See `imenu-generic-expression'.")
256 (defcustom octave-mode-hook nil
257 "Hook to be run when Octave mode is started."
261 (defcustom octave-send-show-buffer t
262 "Non-nil means display `inferior-octave-buffer' after sending to it."
266 (defcustom octave-send-line-auto-forward t
267 "Control auto-forward after sending to the inferior Octave process.
268 Non-nil means always go to the next Octave code line after sending."
272 (defcustom octave-send-echo-input t
273 "Non-nil means echo input sent to the inferior Octave process."
282 ;; Use '__operators__' in Octave REPL to get a full list.
283 (defconst octave-operator-table
284 '((assoc ";" "\n") (assoc ",") ; The doc claims they have equal precedence!?
285 (right "=" "+=" "-=" "*=" "/=")
286 (assoc "&&") (assoc "||") ; The doc claims they have equal precedence!?
287 (assoc "&") (assoc "|") ; The doc claims they have equal precedence!?
288 (nonassoc "<" "<=" "==" ">=" ">" "!=" "~=")
289 (nonassoc ":") ;No idea what this is.
291 (assoc "*" "/" "\\" ".\\" ".*" "./")
293 (nonassoc "++" "--" "!" "~") ;And unary "+" and "-".
294 (right "^" "**" ".^" ".**")
295 ;; It's not really an operator, but for indentation purposes it
296 ;; could be convenient to treat it as one.
299 (defconst octave-smie-bnf-table
301 ;; We can't distinguish the first element in a sequence with
302 ;; precedence grammars, so we can't distinguish the condition
303 ;; if the `if' from the subsequent body, for example.
304 ;; This has to be done later in the indentation rules.
306 ;; We need to mention at least one of the operators in this part
307 ;; of the grammar: if the BNF and the operator table have
308 ;; no overlap, SMIE can't know how they relate.
310 ("try" exp
"catch" exp
"end_try_catch")
311 ("try" exp
"catch" exp
"end")
312 ("unwind_protect" exp
313 "unwind_protect_cleanup" exp
"end_unwind_protect")
314 ("unwind_protect" exp
"unwind_protect_cleanup" exp
"end")
317 ("parfor" exp
"endparfor")
319 ("do" exp
"until" atom
)
320 ("while" exp
"endwhile")
323 ("if" exp
"else" exp
"endif")
324 ("if" exp
"elseif" exp
"else" exp
"endif")
325 ("if" exp
"elseif" exp
"elseif" exp
"else" exp
"endif")
326 ("if" exp
"elseif" exp
"elseif" exp
"else" exp
"end")
327 ("switch" exp
"case" exp
"endswitch")
328 ("switch" exp
"case" exp
"otherwise" exp
"endswitch")
329 ("switch" exp
"case" exp
"case" exp
"otherwise" exp
"endswitch")
330 ("switch" exp
"case" exp
"case" exp
"otherwise" exp
"end")
331 ("function" exp
"endfunction")
332 ("function" exp
"end")
333 ("enumeration" exp
"endenumeration")
334 ("enumeration" exp
"end")
335 ("events" exp
"endevents")
337 ("methods" exp
"endmethods")
338 ("methods" exp
"end")
339 ("properties" exp
"endproperties")
340 ("properties" exp
"end")
341 ("classdef" exp
"endclassdef")
342 ("classdef" exp
"end"))
343 ;; (fundesc (atom "=" atom))
346 (defconst octave-smie-grammar
349 (smie-bnf->prec2 octave-smie-bnf-table
352 (smie-precs->prec2 octave-operator-table
))))
354 ;; Tokenizing needs to be refined so that ";;" is treated as two
355 ;; tokens and also so as to recognize the \n separator (and
356 ;; corresponding continuation lines).
358 (defconst octave-operator-regexp
359 (regexp-opt (apply 'append
(mapcar 'cdr octave-operator-table
))))
361 (defun octave-smie-backward-token ()
363 (forward-comment (- (point)))
365 ((and (not (eq (char-before) ?\
;)) ;Coalesce ";" and "\n".
366 (> pos
(line-end-position))
367 (if (looking-back octave-continuation-marker-regexp
(- (point) 3))
369 (goto-char (match-beginning 0))
370 (forward-comment (- (point)))
373 ;; Ignore it if it's within parentheses.
374 (let ((ppss (syntax-ppss)))
375 (not (and (nth 1 ppss
)
376 (eq ?\
( (char-after (nth 1 ppss
)))))))
377 (skip-chars-forward " \t")
378 ;; Why bother distinguishing \n and ;?
380 ((and (looking-back octave-operator-regexp
(- (point) 3) 'greedy
)
381 ;; Don't mistake a string quote for a transpose.
382 (not (looking-back "\\s\"" (1- (point)))))
383 (goto-char (match-beginning 0))
384 (match-string-no-properties 0))
386 (smie-default-backward-token)))))
388 (defun octave-smie-forward-token ()
389 (skip-chars-forward " \t")
390 (when (looking-at (eval-when-compile
391 (concat "\\(" octave-continuation-marker-regexp
392 "\\)[ \t]*\\($\\|[%#]\\)")))
393 (goto-char (match-end 1))
396 ((and (looking-at "[%#\n]")
397 (not (or (save-excursion (skip-chars-backward " \t")
398 ;; Only add implicit ; when needed.
399 (or (bolp) (eq (char-before) ?\
;)))
400 ;; Ignore it if it's within parentheses.
401 (let ((ppss (syntax-ppss)))
403 (eq ?\
( (char-after (nth 1 ppss
))))))))
404 (if (eolp) (forward-char 1) (forward-comment 1))
405 ;; Why bother distinguishing \n and ;?
407 ((progn (forward-comment (point-max)) nil
))
408 ((looking-at ";[ \t]*\\($\\|[%#]\\)")
409 ;; Combine the ; with the subsequent \n.
410 (goto-char (match-beginning 1))
413 ((and (looking-at octave-operator-regexp
)
414 ;; Don't mistake a string quote for a transpose.
415 (not (looking-at "\\s\"")))
416 (goto-char (match-end 0))
417 (match-string-no-properties 0))
419 (smie-default-forward-token))))
421 (defun octave-smie-rules (kind token
)
422 (pcase (cons kind token
)
423 ;; We could set smie-indent-basic instead, but that would have two
425 ;; - changes to octave-block-offset wouldn't take effect immediately.
426 ;; - edebug wouldn't show the use of this variable.
427 (`(:elem . basic
) octave-block-offset
)
428 ;; Since "case" is in the same BNF rules as switch..end, SMIE by default
429 ;; aligns it with "switch".
430 (`(:before .
"case") (if (not (smie-rule-sibling-p)) octave-block-offset
))
432 (if (smie-rule-parent-p "classdef" "events" "enumeration" "function" "if"
433 "while" "else" "elseif" "for" "parfor"
434 "properties" "methods" "otherwise" "case"
435 "try" "catch" "unwind_protect"
436 "unwind_protect_cleanup")
437 (smie-rule-parent octave-block-offset
)
438 ;; For (invalid) code between switch and case.
439 ;; (if (smie-parent-p "switch") 4)
442 (defun octave-indent-comment ()
443 "A function for `smie-indent-functions' (which see)."
445 (back-to-indentation)
447 ((octave-in-string-or-comment-p) nil
)
448 ((looking-at-p "\\(\\s<\\)\\1\\{2,\\}")
450 ;; Exclude %{, %} and %!.
451 ((and (looking-at-p "\\s<\\(?:[^{}!]\\|$\\)")
452 (not (looking-at-p "\\(\\s<\\)\\1")))
453 (comment-choose-indent)))))
456 (defvar octave-font-lock-keywords
458 ;; Fontify all builtin keywords.
459 (cons (concat "\\_<\\("
460 (regexp-opt octave-reserved-words
)
462 'font-lock-keyword-face
)
463 ;; Note: 'end' also serves as the last index in an indexing expression.
464 ;; Ref: http://www.mathworks.com/help/matlab/ref/end.html
465 (list (lambda (limit)
466 (while (re-search-forward "\\_<end\\_>" limit
'move
)
467 (let ((beg (match-beginning 0))
469 (unless (octave-in-string-or-comment-p)
474 (when (memq (char-after) '(?\
( ?\
[ ?\
{))
475 (put-text-property beg end
'face nil
))
477 (error (goto-char end
))))))
479 ;; Fontify all operators.
480 (cons octave-operator-regexp
'font-lock-builtin-face
)
481 ;; Fontify all function declarations.
482 (list octave-function-header-regexp
483 '(1 font-lock-keyword-face
)
484 '(3 font-lock-function-name-face nil t
)))
485 "Additional Octave expressions to highlight.")
487 (defun octave-syntax-propertize-function (start end
)
489 (octave-syntax-propertize-sqs end
)
490 (funcall (syntax-propertize-rules
491 ("\\\\" (0 (when (eq (nth 3 (save-excursion
492 (syntax-ppss (match-beginning 0))))
494 (string-to-syntax "\\"))))
495 ;; Try to distinguish the string-quotes from the transpose-quotes.
496 ("\\(?:^\\|[[({,; ]\\)\\('\\)"
497 (1 (prog1 "\"'" (octave-syntax-propertize-sqs end
)))))
500 (defun octave-syntax-propertize-sqs (end)
501 "Propertize the content/end of single-quote strings."
502 (when (eq (nth 3 (syntax-ppss)) ?
\')
504 (when (re-search-forward
505 "\\(?:\\=\\|[^']\\)\\(?:''\\)*\\('\\)\\($\\|[^']\\)" end
'move
)
506 (goto-char (match-beginning 2))
507 (when (eq (char-before (match-beginning 1)) ?
\\)
508 ;; Backslash cannot escape a single quote.
509 (put-text-property (1- (match-beginning 1)) (match-beginning 1)
510 'syntax-table
(string-to-syntax ".")))
511 (put-text-property (match-beginning 1) (match-end 1)
512 'syntax-table
(string-to-syntax "\"'")))))
514 (defvar electric-layout-rules
)
517 (define-derived-mode octave-mode prog-mode
"Octave"
518 "Major mode for editing Octave code.
520 Octave is a high-level language, primarily intended for numerical
521 computations. It provides a convenient command line interface
522 for solving linear and nonlinear problems numerically. Function
523 definitions can also be stored in files and used in batch mode."
524 :abbrev-table octave-abbrev-table
526 (smie-setup octave-smie-grammar
#'octave-smie-rules
527 :forward-token
#'octave-smie-forward-token
528 :backward-token
#'octave-smie-backward-token
)
529 (setq-local smie-indent-basic
'octave-block-offset
)
530 (add-hook 'smie-indent-functions
#'octave-indent-comment nil t
)
532 (setq-local smie-blink-matching-triggers
533 (cons ?\
; smie-blink-matching-triggers))
534 (unless octave-blink-matching-block
535 (remove-hook 'post-self-insert-hook
#'smie-blink-matching-open
'local
))
537 (setq-local electric-indent-chars
538 (cons ?\
; electric-indent-chars))
539 ;; IIUC matlab-mode takes the opposite approach: it makes RET insert
540 ;; a ";" at those places where it's correct (i.e. outside of parens).
541 (setq-local electric-layout-rules
'((?\
; . after)))
543 (setq-local comment-use-global-state t
)
544 (setq-local comment-start octave-comment-start
)
545 (setq-local comment-end
"")
546 (setq-local comment-start-skip octave-comment-start-skip
)
547 (setq-local comment-add
1)
549 (setq-local parse-sexp-ignore-comments t
)
550 (setq-local paragraph-start
(concat "\\s-*$\\|" page-delimiter
))
551 (setq-local paragraph-separate paragraph-start
)
552 (setq-local paragraph-ignore-fill-prefix t
)
553 (setq-local fill-paragraph-function
'octave-fill-paragraph
)
555 (setq-local fill-nobreak-predicate
556 (lambda () (eq (octave-in-string-p) ?
')))
558 (if (fboundp 'add-function
) ; new in 24.4
559 (add-function :around
(local 'comment-line-break-function
)
560 #'octave--indent-new-comment-line
)
561 (setq-local comment-line-break-function
562 (apply-partially #'octave--indent-new-comment-line
563 #'comment-indent-new-line
))))
565 (setq font-lock-defaults
'(octave-font-lock-keywords))
567 (setq-local syntax-propertize-function
#'octave-syntax-propertize-function
)
569 (setq-local imenu-generic-expression octave-mode-imenu-generic-expression
)
570 (setq-local imenu-case-fold-search nil
)
572 (setq-local add-log-current-defun-function
#'octave-add-log-current-defun
)
574 (add-hook 'completion-at-point-functions
'octave-completion-at-point nil t
)
575 (add-hook 'before-save-hook
'octave-sync-function-file-names nil t
)
576 (setq-local beginning-of-defun-function
'octave-beginning-of-defun
)
577 (and octave-font-lock-texinfo-comment
(octave-font-lock-texinfo-comment))
578 (setq-local eldoc-documentation-function
'octave-eldoc-function
)
580 (easy-menu-add octave-mode-menu
))
583 (defcustom inferior-octave-program
"octave"
584 "Program invoked by `inferior-octave'."
588 (defcustom inferior-octave-buffer
"*Inferior Octave*"
589 "Name of buffer for running an inferior Octave process."
593 (defcustom inferior-octave-prompt
594 "\\(^octave\\(\\|.bin\\|.exe\\)\\(-[.0-9]+\\)?\\(:[0-9]+\\)?\\|^debug\\|^\\)>+ "
595 "Regexp to match prompts for the inferior Octave process."
599 (defcustom inferior-octave-prompt-read-only comint-prompt-read-only
600 "If non-nil, the Octave prompt is read only.
601 See `comint-prompt-read-only' for details."
606 (defcustom inferior-octave-startup-file
607 (convert-standard-filename
608 (concat "~/.emacs-" (file-name-nondirectory inferior-octave-program
)))
609 "Name of the inferior Octave startup file.
610 The contents of this file are sent to the inferior Octave process on
612 :type
'(choice (const :tag
"None" nil
) file
)
616 (defcustom inferior-octave-startup-args
'("-i" "--no-line-editing")
617 "List of command line arguments for the inferior Octave process.
618 For example, for suppressing the startup message and using `traditional'
619 mode, include \"-q\" and \"--traditional\"."
620 :type
'(repeat string
)
624 (defcustom inferior-octave-mode-hook nil
625 "Hook to be run when Inferior Octave mode is started."
629 (defvar inferior-octave-process nil
)
631 (defvar inferior-octave-mode-map
632 (let ((map (make-sparse-keymap)))
633 (set-keymap-parent map comint-mode-map
)
634 (define-key map
"\M-." 'octave-find-definition
)
635 (define-key map
"\t" 'completion-at-point
)
636 (define-key map
"\C-hd" 'octave-help
)
637 ;; Same as in `shell-mode'.
638 (define-key map
"\M-?" 'comint-dynamic-list-filename-completions
)
639 (define-key map
"\C-c\C-l" 'inferior-octave-dynamic-list-input-ring
)
640 (define-key map
[menu-bar inout list-history
]
641 '("List Input History" . inferior-octave-dynamic-list-input-ring
))
643 "Keymap used in Inferior Octave mode.")
645 (defvar inferior-octave-mode-syntax-table
646 (let ((table (make-syntax-table octave-mode-syntax-table
)))
648 "Syntax table in use in inferior-octave-mode buffers.")
650 (defvar inferior-octave-font-lock-keywords
652 (cons inferior-octave-prompt
'font-lock-type-face
))
653 ;; Could certainly do more font locking in inferior Octave ...
654 "Additional expressions to highlight in Inferior Octave mode.")
656 (defvar inferior-octave-output-list nil
)
657 (defvar inferior-octave-output-string nil
)
658 (defvar inferior-octave-receive-in-progress nil
)
660 (define-obsolete-variable-alias 'inferior-octave-startup-hook
661 'inferior-octave-mode-hook
"24.4")
663 (defvar inferior-octave-dynamic-complete-functions
664 '(inferior-octave-completion-at-point comint-filename-completion
)
665 "List of functions called to perform completion for inferior Octave.
666 This variable is used to initialize `comint-dynamic-complete-functions'
667 in the Inferior Octave buffer.")
669 (defvar info-lookup-mode
)
671 (define-derived-mode inferior-octave-mode comint-mode
"Inferior Octave"
672 "Major mode for interacting with an inferior Octave process."
673 :abbrev-table octave-abbrev-table
674 (setq comint-prompt-regexp inferior-octave-prompt
)
676 (setq-local comment-use-global-state t
)
677 (setq-local comment-start octave-comment-start
)
678 (setq-local comment-end
"")
679 (setq comment-column
32)
680 (setq-local comment-start-skip octave-comment-start-skip
)
682 (setq font-lock-defaults
'(inferior-octave-font-lock-keywords nil nil
))
684 (setq-local info-lookup-mode
'octave-mode
)
685 (setq-local eldoc-documentation-function
'octave-eldoc-function
)
687 (setq comint-input-ring-file-name
688 (or (getenv "OCTAVE_HISTFILE") "~/.octave_hist")
689 comint-input-ring-size
(or (getenv "OCTAVE_HISTSIZE") 1024))
690 (setq-local comint-dynamic-complete-functions
691 inferior-octave-dynamic-complete-functions
)
692 (setq-local comint-prompt-read-only inferior-octave-prompt-read-only
)
693 (add-hook 'comint-input-filter-functions
694 'inferior-octave-directory-tracker nil t
)
695 ;; http://thread.gmane.org/gmane.comp.gnu.octave.general/48572
696 (add-hook 'window-configuration-change-hook
697 'inferior-octave-track-window-width-change nil t
)
698 (comint-read-input-ring t
))
701 (defun inferior-octave (&optional arg
)
702 "Run an inferior Octave process, I/O via `inferior-octave-buffer'.
703 This buffer is put in Inferior Octave mode. See `inferior-octave-mode'.
705 Unless ARG is non-nil, switches to this buffer.
707 The elements of the list `inferior-octave-startup-args' are sent as
708 command line arguments to the inferior Octave process on startup.
710 Additional commands to be executed on startup can be provided either in
711 the file specified by `inferior-octave-startup-file' or by the default
712 startup file, `~/.emacs-octave'."
714 (let ((buffer (get-buffer-create inferior-octave-buffer
)))
716 (pop-to-buffer buffer
))
717 (unless (comint-check-proc buffer
)
718 (with-current-buffer buffer
719 (inferior-octave-startup)
720 (inferior-octave-mode)))
724 (defalias 'run-octave
'inferior-octave
)
726 (defun inferior-octave-startup ()
727 "Start an inferior Octave process."
728 (let ((proc (comint-exec-1
729 (substring inferior-octave-buffer
1 -
1)
730 inferior-octave-buffer
731 inferior-octave-program
733 inferior-octave-startup-args
734 ;; --no-gui is introduced in Octave > 3.7
735 (and (not (member "--no-gui" inferior-octave-startup-args
))
736 (zerop (process-file inferior-octave-program
737 nil nil nil
"--no-gui" "--help"))
739 (set-process-filter proc
'inferior-octave-output-digest
)
740 (setq inferior-octave-process proc
741 inferior-octave-output-list nil
742 inferior-octave-output-string nil
743 inferior-octave-receive-in-progress t
)
745 ;; This may look complicated ... However, we need to make sure that
746 ;; we additional startup code only AFTER Octave is ready (otherwise,
747 ;; output may be mixed up). Hence, we need to digest the Octave
748 ;; output to see when it issues a prompt.
749 (while inferior-octave-receive-in-progress
750 (or (process-live-p inferior-octave-process
)
751 (error "Process `%s' died" inferior-octave-process
))
752 (accept-process-output inferior-octave-process
))
753 (goto-char (point-max))
754 (set-marker (process-mark proc
) (point))
755 (insert-before-markers
757 (if (not (bobp)) "\f\n")
758 (if inferior-octave-output-list
760 'identity inferior-octave-output-list
"\n")
763 ;; An empty secondary prompt, as e.g. obtained by '--braindead',
765 (inferior-octave-send-list-and-digest (list "PS2\n"))
766 (when (string-match "\\(PS2\\|ans\\) = *$"
767 (car inferior-octave-output-list
))
768 (inferior-octave-send-list-and-digest (list "PS2 ('> ');\n")))
770 (inferior-octave-send-list-and-digest
771 (list "disp (getenv ('OCTAVE_SRCDIR'))\n"))
772 (process-put proc
'octave-srcdir
773 (unless (equal (car inferior-octave-output-list
) "")
774 (car inferior-octave-output-list
)))
776 ;; O.K., now we are ready for the Inferior Octave startup commands.
777 (inferior-octave-send-list-and-digest
779 (unless (equal inferior-octave-output-string
">> ")
781 (when (and inferior-octave-startup-file
782 (file-exists-p inferior-octave-startup-file
))
783 (format "source ('%s');\n" inferior-octave-startup-file
))))
784 (when inferior-octave-output-list
785 (insert-before-markers
786 (mapconcat 'identity inferior-octave-output-list
"\n")))
788 ;; And finally, everything is back to normal.
789 (set-process-filter proc
'comint-output-filter
)
790 ;; Just in case, to be sure a cd in the startup file won't have
791 ;; detrimental effects.
792 (with-demoted-errors (inferior-octave-resync-dirs))
793 ;; Generate a proper prompt, which is critical to
794 ;; `comint-history-isearch-backward-regexp'. Bug#14433.
795 (comint-send-string proc
"\n")))
797 (defvar inferior-octave-completion-table
799 ;; Use cache to avoid repetitive computation of completions due to
800 ;; bug#11906 - http://debbugs.gnu.org/11906 - which may cause
801 ;; noticeable delay. CACHE: (CMD TIME VALUE).
803 (completion-table-dynamic
805 (unless (and (equal (car cache
) command
)
806 (< (float-time) (+ 5 (cadr cache
))))
807 (inferior-octave-send-list-and-digest
808 (list (format "completion_matches ('%s');\n" command
)))
809 (setq cache
(list command
(float-time)
810 (delete-consecutive-dups
811 (sort inferior-octave-output-list
'string-lessp
)))))
812 (car (cddr cache
))))))
814 (defun inferior-octave-completion-at-point ()
815 "Return the data to complete the Octave symbol at point."
816 ;; http://debbugs.gnu.org/14300
817 (let* ((filecomp (string-match-p
818 "/" (or (comint--match-partial-filename) "")))
823 (skip-syntax-backward "w_" (comint-line-beginning-position))
825 (when (and start
(> end start
))
826 (list start end
(completion-table-in-turn
827 inferior-octave-completion-table
828 'comint-completion-file-name-table
)))))
830 (define-obsolete-function-alias 'inferior-octave-complete
831 'completion-at-point
"24.1")
833 (defun inferior-octave-dynamic-list-input-ring ()
834 "List the buffer's input history in a help buffer."
835 ;; We cannot use `comint-dynamic-list-input-ring', because it replaces
836 ;; "completion" by "history reference" ...
838 (if (or (not (ring-p comint-input-ring
))
839 (ring-empty-p comint-input-ring
))
840 (message "No history")
842 (history-buffer " *Input History*")
843 (index (1- (ring-length comint-input-ring
)))
844 (conf (current-window-configuration)))
845 ;; We have to build up a list ourselves from the ring vector.
847 (setq history
(cons (ring-ref comint-input-ring index
) history
)
849 ;; Change "completion" to "history reference"
850 ;; to make the display accurate.
851 (with-output-to-temp-buffer history-buffer
852 (display-completion-list history
)
853 (set-buffer history-buffer
))
854 (message "Hit space to flush")
855 (let ((ch (read-event)))
857 (set-window-configuration conf
)
858 (setq unread-command-events
(list ch
)))))))
860 (defun inferior-octave-output-digest (_proc string
)
861 "Special output filter for the inferior Octave process.
862 Save all output between newlines into `inferior-octave-output-list', and
863 the rest to `inferior-octave-output-string'."
864 (setq string
(concat inferior-octave-output-string string
))
865 (while (string-match "\n" string
)
866 (setq inferior-octave-output-list
867 (append inferior-octave-output-list
868 (list (substring string
0 (match-beginning 0))))
869 string
(substring string
(match-end 0))))
870 (if (string-match inferior-octave-prompt string
)
871 (setq inferior-octave-receive-in-progress nil
))
872 (setq inferior-octave-output-string string
))
874 (defun inferior-octave-check-process ()
875 (or (and inferior-octave-process
876 (process-live-p inferior-octave-process
))
877 (error (substitute-command-keys
878 "No inferior octave process running. Type \\[run-octave]"))))
880 (defun inferior-octave-send-list-and-digest (list)
881 "Send LIST to the inferior Octave process and digest the output.
882 The elements of LIST have to be strings and are sent one by one. All
883 output is passed to the filter `inferior-octave-output-digest'."
884 (inferior-octave-check-process)
885 (let* ((proc inferior-octave-process
)
886 (filter (process-filter proc
))
888 (set-process-filter proc
'inferior-octave-output-digest
)
889 (setq inferior-octave-output-list nil
)
891 (while (setq string
(car list
))
892 (setq inferior-octave-output-string nil
893 inferior-octave-receive-in-progress t
)
894 (comint-send-string proc string
)
895 (while inferior-octave-receive-in-progress
896 (accept-process-output proc
))
897 (setq list
(cdr list
)))
898 (set-process-filter proc filter
))))
900 (defvar inferior-octave-directory-tracker-resync nil
)
901 (make-variable-buffer-local 'inferior-octave-directory-tracker-resync
)
903 (defun inferior-octave-directory-tracker (string)
904 "Tracks `cd' commands issued to the inferior Octave process.
905 Use \\[inferior-octave-resync-dirs] to resync if Emacs gets confused."
906 (when inferior-octave-directory-tracker-resync
907 (or (inferior-octave-resync-dirs 'noerror
)
908 (setq inferior-octave-directory-tracker-resync nil
)))
910 ((string-match "^[ \t]*cd[ \t;]*$" string
)
912 ((string-match "^[ \t]*cd[ \t]+\\([^ \t\n;]*\\)[ \t\n;]*" string
)
914 (cd (match-string 1 string
))
915 (error (setq inferior-octave-directory-tracker-resync t
)
917 (error-message-string err
)
918 (match-string 1 string
)))))))
920 (defun inferior-octave-resync-dirs (&optional noerror
)
921 "Resync the buffer's idea of the current directory.
922 This command queries the inferior Octave process about its current
923 directory and makes this the current buffer's default directory."
925 (inferior-octave-send-list-and-digest '("disp (pwd ())\n"))
928 (cd (car inferior-octave-output-list
))
930 (error (unless noerror
(signal (car err
) (cdr err
))))))
932 (defcustom inferior-octave-minimal-columns
80
933 "The minimal column width for the inferior Octave process."
938 (defvar inferior-octave-last-column-width nil
)
940 (defun inferior-octave-track-window-width-change ()
941 ;; http://thread.gmane.org/gmane.comp.gnu.octave.general/48572
942 (let ((width (max inferior-octave-minimal-columns
(window-width))))
943 (unless (eq inferior-octave-last-column-width width
)
944 (setq-local inferior-octave-last-column-width width
)
945 (when (and inferior-octave-process
946 (process-live-p inferior-octave-process
))
947 (inferior-octave-send-list-and-digest
948 (list (format "putenv ('COLUMNS', '%s');\n" width
)))))))
951 ;;; Miscellaneous useful functions
953 (defun octave-in-comment-p ()
954 "Return non-nil if point is inside an Octave comment."
955 (nth 4 (syntax-ppss)))
957 (defun octave-in-string-p ()
958 "Return non-nil if point is inside an Octave string."
959 (nth 3 (syntax-ppss)))
961 (defun octave-in-string-or-comment-p ()
962 "Return non-nil if point is inside an Octave string or comment."
963 (nth 8 (syntax-ppss)))
965 (defun octave-looking-at-kw (regexp)
966 "Like `looking-at', but sets `case-fold-search' nil."
967 (let ((case-fold-search nil
))
968 (looking-at regexp
)))
970 (defun octave-maybe-insert-continuation-string ()
971 (if (or (octave-in-comment-p)
974 (looking-at octave-continuation-regexp
)))
976 (delete-horizontal-space)
977 (insert (concat " " octave-continuation-string
))))
979 (defun octave-completing-read ()
980 (let ((def (or (thing-at-point 'symbol
)
982 (skip-syntax-backward "-(")
983 (thing-at-point 'symbol
)))))
985 (format (if def
"Function (default %s): "
987 inferior-octave-completion-table
988 nil nil nil nil def
)))
990 (defun octave-goto-function-definition (fn)
991 "Go to the function definition of FN in current buffer."
994 (let ((orig (point)) found
)
995 (goto-char (point-min))
996 (while (and (not found
) (re-search-forward re nil t
))
997 (when (and (equal (match-string sub
) fn
)
998 (not (nth 8 (syntax-ppss))))
1000 (unless found
(goto-char orig
))
1002 (pcase (and buffer-file-name
(file-name-extension buffer-file-name
))
1003 (`"cc" (funcall search
1004 "\\_<DEFUN\\(?:_DLD\\)?\\s-*(\\s-*\\(\\(?:\\sw\\|\\s_\\)+\\)" 1))
1005 (t (funcall search octave-function-header-regexp
3)))))
1007 (defun octave-function-file-p ()
1008 "Return non-nil if the first token is \"function\".
1009 The value is (START END NAME-START NAME-END) of the function."
1011 (goto-char (point-min))
1012 (when (equal (funcall smie-forward-token-function
) "function")
1014 (let* ((start (point))
1015 (end (progn (forward-sexp 1) (point)))
1018 (re-search-forward octave-function-header-regexp
1020 (list (match-beginning 3) (match-end 3)))))
1021 (cons start
(cons end name
))))))
1023 ;; Like forward-comment but stop at non-comment blank
1024 (defun octave-skip-comment-forward (limit)
1025 (let ((ppss (syntax-ppss)))
1027 (goto-char (nth 8 ppss
))
1028 (goto-char (or (comment-search-forward limit t
) (point)))))
1029 (while (and (< (point) limit
) (looking-at-p "\\s<"))
1030 (forward-comment 1)))
1032 ;;; First non-copyright comment block
1033 (defun octave-function-file-comment ()
1034 "Beginning and end positions of the function file comment."
1036 (goto-char (point-min))
1037 ;; Copyright block: octave/libinterp/parse-tree/lex.ll around line 1634
1038 (while (save-excursion
1039 (when (comment-search-forward (point-max) t
)
1040 (when (eq (char-after) ?\
{) ; case of block comment
1042 (skip-syntax-forward "-")
1043 (let ((case-fold-search t
))
1044 (looking-at-p "\\(?:copyright\\|author\\)\\_>"))))
1045 (octave-skip-comment-forward (point-max)))
1046 (let ((beg (comment-search-forward (point-max) t
)))
1049 (octave-skip-comment-forward (point-max))
1050 (list beg
(point))))))
1052 (defun octave-sync-function-file-names ()
1053 "Ensure function name agree with function file name.
1054 See Info node `(octave)Function Files'."
1056 (when buffer-file-name
1057 (pcase-let ((`(,start
,_end
,name-start
,name-end
)
1058 (octave-function-file-p)))
1059 (when (and start name-start
)
1060 (let* ((func (buffer-substring name-start name-end
))
1061 (file (file-name-sans-extension
1062 (file-name-nondirectory buffer-file-name
)))
1063 (help-form (format "\
1064 a: Use function name `%s'
1065 b: Use file name `%s'
1066 q: Don't fix\n" func file
))
1067 (c (unless (equal file func
)
1068 (save-window-excursion
1071 "Which name to use? (a/b/q) " '(?a ?b ?q
))))))
1073 (`?a
(let ((newname (expand-file-name
1074 (concat func
(file-name-extension
1075 buffer-file-name t
)))))
1076 (when (or (not (file-exists-p newname
))
1078 (format "Target file %s exists; proceed? " newname
)))
1079 (when (file-exists-p buffer-file-name
)
1080 (rename-file buffer-file-name newname t
))
1081 (set-visited-file-name newname
))))
1082 (`?b
(save-excursion
1083 (goto-char name-start
)
1084 (delete-region name-start name-end
)
1085 (insert file
)))))))))
1087 (defun octave-update-function-file-comment (beg end
)
1088 "Query replace function names in function file comment."
1091 (barf-if-buffer-read-only)
1093 (list (region-beginning) (region-end))
1094 (or (octave-function-file-comment)
1095 (error "No function file comment found")))))
1097 (let* ((bounds (or (octave-function-file-p)
1098 (error "Not in a function file buffer")))
1099 (func (if (cddr bounds
)
1100 (apply #'buffer-substring
(cddr bounds
))
1101 (error "Function name not found")))
1104 (when (re-search-forward
1105 "[=}]\\s-*\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>"
1106 (min (line-end-position 4) end
)
1109 (old-func (read-string (format (if old-func
1110 "Name to replace (default %s): "
1111 "Name to replace: ")
1114 (if (and func old-func
(not (equal func old-func
)))
1115 (perform-replace old-func func
'query
1116 nil
'delimited nil nil beg end
)
1117 (message "Function names match")))))
1119 (defface octave-function-comment-block
1120 '((t (:inherit font-lock-doc-face
)))
1121 "Face used to highlight function comment block."
1124 (eval-when-compile (require 'texinfo
))
1126 (defun octave-font-lock-texinfo-comment ()
1131 (if (numberp (nth 1 kw
))
1132 `(,(nth 0 kw
) ,(nth 1 kw
) ,(nth 2 kw
) prepend
)
1133 (message "Ignoring Texinfo highlight: %S" kw
)))
1134 texinfo-font-lock-keywords
)))))
1135 (font-lock-add-keywords
1138 (while (and (< (point) limit
)
1139 (search-forward "-*- texinfo -*-" limit t
)
1140 (octave-in-comment-p))
1141 (let ((beg (nth 8 (syntax-ppss)))
1143 (octave-skip-comment-forward (point-max))
1145 (put-text-property beg end
'font-lock-multiline t
)
1146 (font-lock-prepend-text-property
1147 beg end
'face
'octave-function-comment-block
)
1150 (while (re-search-forward (car kw
) end
'move
)
1151 (font-lock-apply-highlight (cdr kw
))))))
1158 (defun octave-indent-new-comment-line (&optional soft
)
1159 "Break Octave line at point, continuing comment if within one.
1160 Insert `octave-continuation-string' before breaking the line
1161 unless inside a list. Signal an error if within a single-quoted
1164 (funcall comment-line-break-function soft
))
1166 (defun octave--indent-new-comment-line (orig &rest args
)
1168 ((octave-in-comment-p) nil
)
1169 ((eq (octave-in-string-p) ?
')
1170 (error "Cannot split a single-quoted string"))
1171 ((eq (octave-in-string-p) ?
\")
1172 (insert octave-continuation-string
))
1174 (delete-horizontal-space)
1175 (unless (and (cadr (syntax-ppss))
1176 (eq (char-after (cadr (syntax-ppss))) ?\
())
1177 (insert " " octave-continuation-string
))))
1179 (indent-according-to-mode))
1181 (define-obsolete-function-alias
1182 'octave-indent-defun
'prog-indent-sexp
"24.4")
1186 (defun octave-next-code-line (&optional arg
)
1187 "Move ARG lines of Octave code forward (backward if ARG is negative).
1188 Skips past all empty and comment lines. Default for ARG is 1.
1190 On success, return 0. Otherwise, go as far as possible and return -1."
1192 (or arg
(setq arg
1))
1195 (inc (if (> arg
0) 1 -
1)))
1196 (while (and (/= arg
0) (= n
0))
1197 (setq n
(forward-line inc
))
1199 (looking-at "\\s-*\\($\\|\\s<\\)"))
1200 (setq n
(forward-line inc
)))
1201 (setq arg
(- arg inc
)))
1204 (defun octave-previous-code-line (&optional arg
)
1205 "Move ARG lines of Octave code backward (forward if ARG is negative).
1206 Skips past all empty and comment lines. Default for ARG is 1.
1208 On success, return 0. Otherwise, go as far as possible and return -1."
1210 (or arg
(setq arg
1))
1211 (octave-next-code-line (- arg
)))
1213 (defun octave-beginning-of-line ()
1214 "Move point to beginning of current Octave line.
1215 If on an empty or comment line, go to the beginning of that line.
1216 Otherwise, move backward to the beginning of the first Octave code line
1217 which is not inside a continuation statement, i.e., which does not
1218 follow a code line ending with `...' or is inside an open
1222 (unless (looking-at "\\s-*\\($\\|\\s<\\)")
1223 (while (or (when (cadr (syntax-ppss))
1224 (goto-char (cadr (syntax-ppss)))
1227 (and (or (looking-at "\\s-*\\($\\|\\s<\\)")
1229 (if (zerop (octave-previous-code-line))
1230 (looking-at octave-continuation-regexp
))))
1231 (zerop (forward-line -
1)))))))
1233 (defun octave-end-of-line ()
1234 "Move point to end of current Octave line.
1235 If on an empty or comment line, go to the end of that line.
1236 Otherwise, move forward to the end of the first Octave code line which
1237 does not end with `...' or is inside an open parenthesis list."
1240 (unless (save-excursion
1242 (looking-at "\\s-*\\($\\|\\s<\\)"))
1243 (while (or (when (cadr (syntax-ppss))
1250 (and (save-excursion
1252 (or (looking-at "\\s-*\\($\\|\\s<\\)")
1253 (looking-at octave-continuation-regexp
)))
1254 (zerop (forward-line 1)))))
1257 (defun octave-mark-block ()
1258 "Put point at the beginning of this Octave block, mark at the end.
1259 The block marked is the one that contains point or follows point."
1261 (if (and (looking-at "\\sw\\|\\s_")
1262 (looking-back "\\sw\\|\\s_" (1- (point))))
1263 (skip-syntax-forward "w_"))
1264 (unless (or (looking-at "\\s(")
1266 (let* ((token (funcall smie-forward-token-function
))
1267 (level (assoc token smie-grammar
)))
1268 (and level
(not (numberp (cadr level
)))))))
1269 (backward-up-list 1))
1272 (defun octave-beginning-of-defun (&optional arg
)
1273 "Octave-specific `beginning-of-defun-function' (which see)."
1274 (or arg
(setq arg
1))
1275 ;; Move out of strings or comments.
1276 (when (octave-in-string-or-comment-p)
1277 (goto-char (octave-in-string-or-comment-p)))
1278 (letrec ((orig (point))
1279 (toplevel (lambda (pos)
1282 (backward-up-list 1)
1283 (funcall toplevel
(point)))
1284 (scan-error pos
)))))
1285 (goto-char (funcall toplevel
(point)))
1286 (when (and (> arg
0) (/= orig
(point)))
1287 (setq arg
(1- arg
)))
1288 (forward-sexp (- arg
))
1289 (and (< arg
0) (forward-sexp -
1))
1292 (defun octave-fill-paragraph (&optional _arg
)
1293 "Fill paragraph of Octave code, handling Octave comments."
1294 ;; FIXME: difference with generic fill-paragraph:
1295 ;; - code lines are only split, never joined.
1296 ;; - \n that end comments are never removed.
1297 ;; - insert continuation marker when splitting code lines.
1300 (let ((end (progn (forward-paragraph) (copy-marker (point) t
)))
1302 (forward-paragraph -
1)
1303 (skip-chars-forward " \t\n")
1306 (cfc (current-fill-column))
1309 (while (< (point) end
)
1311 (indent-according-to-mode)
1313 (move-to-column cfc
)
1314 ;; First check whether we need to combine non-empty comment lines
1315 (if (and (< (current-column) cfc
)
1316 (octave-in-comment-p)
1317 (not (save-excursion
1319 (looking-at "^\\s-*\\s<+\\s-*$"))))
1320 ;; This is a nonempty comment line which does not extend
1321 ;; past the fill column. If it is followed by a nonempty
1322 ;; comment line with the same comment prefix, try to
1323 ;; combine them, and repeat this until either we reach the
1324 ;; fill-column or there is nothing more to combine.
1326 ;; Get the comment prefix
1329 (while (and (re-search-forward "\\s<+")
1330 (not (octave-in-comment-p))))
1331 (setq comment-prefix
(match-string 0)))
1332 ;; And keep combining ...
1333 (while (and (< (current-column) cfc
)
1345 (re-search-forward comment-prefix
)
1346 (delete-region (match-beginning 0) (match-end 0))
1348 (move-to-column cfc
))))
1349 ;; We might also try to combine continued code lines> Perhaps
1350 ;; some other time ...
1351 (skip-chars-forward "^ \t\n")
1352 (delete-horizontal-space)
1353 (if (or (< (current-column) cfc
)
1354 (and (= (current-column) cfc
) (eolp)))
1356 (if (not (eolp)) (insert " "))
1357 (or (funcall normal-auto-fill-function
)
1361 (defun octave-completion-at-point ()
1362 "Find the text to complete and the corresponding table."
1363 (let* ((beg (save-excursion (skip-syntax-backward "w_") (point)))
1366 ;; Extend region past point, if applicable.
1367 (save-excursion (skip-syntax-forward "w_")
1368 (setq end
(point))))
1370 (list beg end
(or (and inferior-octave-process
1371 (process-live-p inferior-octave-process
)
1372 inferior-octave-completion-table
)
1373 octave-reserved-words
)))))
1375 (define-obsolete-function-alias 'octave-complete-symbol
1376 'completion-at-point
"24.1")
1378 (defun octave-add-log-current-defun ()
1379 "A function for `add-log-current-defun-function' (which see)."
1382 (and (beginning-of-defun)
1383 (re-search-forward octave-function-header-regexp
1384 (line-end-position) t
)
1388 ;;; Electric characters && friends
1389 (define-skeleton octave-insert-defun
1390 "Insert an Octave function skeleton.
1391 Prompt for the function's name, arguments and return values (to be
1392 entered without parens)."
1393 (let* ((defname (file-name-sans-extension (buffer-name)))
1394 (name (read-string (format "Function name (default %s): " defname
)
1396 (args (read-string "Arguments: "))
1397 (vals (read-string "Return values: ")))
1400 ((string-equal vals
"") vals
)
1401 ((string-match "[ ,]" vals
) (concat "[" vals
"] = "))
1402 (t (concat vals
" = ")))
1405 \n octave-block-comment-start
"usage: " str
\n
1406 octave-block-comment-start
'(delete-horizontal-space) \n
1407 octave-block-comment-start
'(delete-horizontal-space) \n
1408 "function " > str
\n
1412 ;;; Communication with the inferior Octave process
1413 (defun octave-kill-process ()
1414 "Kill inferior Octave process and its buffer."
1416 (if inferior-octave-process
1418 (process-send-string inferior-octave-process
"quit;\n")
1419 (accept-process-output inferior-octave-process
)))
1420 (if inferior-octave-buffer
1421 (kill-buffer inferior-octave-buffer
)))
1423 (defun octave-show-process-buffer ()
1424 "Make sure that `inferior-octave-buffer' is displayed."
1426 (if (get-buffer inferior-octave-buffer
)
1427 (display-buffer inferior-octave-buffer
)
1428 (message "No buffer named %s" inferior-octave-buffer
)))
1430 (defun octave-hide-process-buffer ()
1431 "Delete all windows that display `inferior-octave-buffer'."
1433 (if (get-buffer inferior-octave-buffer
)
1434 (delete-windows-on inferior-octave-buffer
)
1435 (message "No buffer named %s" inferior-octave-buffer
)))
1437 (defun octave-send-region (beg end
)
1438 "Send current region to the inferior Octave process."
1441 (let ((proc inferior-octave-process
)
1442 (string (buffer-substring-no-properties beg end
))
1444 (with-current-buffer inferior-octave-buffer
1445 (setq inferior-octave-output-list nil
)
1446 (while (not (string-equal string
""))
1447 (if (string-match "\n" string
)
1448 (setq line
(substring string
0 (match-beginning 0))
1449 string
(substring string
(match-end 0)))
1450 (setq line string string
""))
1451 (setq inferior-octave-receive-in-progress t
)
1452 (inferior-octave-send-list-and-digest (list (concat line
"\n")))
1453 (while inferior-octave-receive-in-progress
1454 (accept-process-output proc
))
1455 (insert-before-markers
1456 (mapconcat 'identity
1458 (if octave-send-echo-input
(list line
) (list ""))
1459 inferior-octave-output-list
1460 (list inferior-octave-output-string
))
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."
1470 (octave-send-region (point) (mark))))
1472 (defun octave-send-defun ()
1473 "Send current Octave function to the inferior Octave process."
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
1485 (or arg
(setq arg
1))
1490 (octave-next-code-line (- arg
1))
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."
1501 (let ((standard-output (current-buffer))
1502 (print-escape-newlines nil
)
1508 (inferior-octave-send-list-and-digest
1509 (list (concat (buffer-substring-no-properties (point) opoint
)
1511 (mapconcat 'identity inferior-octave-output-list
"\n")))
1516 (defcustom octave-eldoc-message-style
'auto
1517 "Octave eldoc message style: auto, oneline, multiline."
1518 :type
'(choice (const :tag
"Automatic" auto
)
1519 (const :tag
"One Line" oneline
)
1520 (const :tag
"Multi Line" multiline
))
1524 ;; (FN SIGNATURE1 SIGNATURE2 ...)
1525 (defvar octave-eldoc-cache nil
)
1527 (defun octave-eldoc-function-signatures (fn)
1528 (unless (equal fn
(car octave-eldoc-cache
))
1529 (inferior-octave-send-list-and-digest
1530 (list (format "print_usage ('%s');\n" fn
)))
1532 (dolist (line inferior-octave-output-list
)
1534 "\\s-*\\(?:--[^:]+\\|usage\\):\\s-*\\(.*\\)$"
1536 (push (match-string 1 line
) result
)))
1537 (setq octave-eldoc-cache
1538 (cons (substring-no-properties fn
)
1539 (nreverse result
)))))
1540 (cdr octave-eldoc-cache
))
1542 (defun octave-eldoc-function ()
1543 "A function for `eldoc-documentation-function' (which see)."
1544 (when (and inferior-octave-process
1545 (process-live-p inferior-octave-process
))
1546 (let* ((ppss (syntax-ppss))
1547 (paren-pos (cadr ppss
))
1550 ;; PAREN-POS must be after the prompt
1552 (if (eq (get-buffer-process (current-buffer))
1553 inferior-octave-process
)
1554 (process-mark inferior-octave-process
)
1556 (or (not (eq (get-buffer-process (current-buffer))
1557 inferior-octave-process
))
1558 (< (process-mark inferior-octave-process
)
1560 (eq (char-after paren-pos
) ?\
())
1561 (goto-char paren-pos
)
1562 (setq paren-pos nil
))
1563 (when (or (< (skip-syntax-backward "-") 0) paren-pos
)
1564 (thing-at-point 'symbol
))))
1565 (sigs (and fn
(octave-eldoc-function-signatures fn
)))
1566 (oneline (mapconcat 'identity sigs
1567 (propertize " | " 'face
'warning
)))
1568 (multiline (mapconcat (lambda (s) (concat "-- " s
)) sigs
"\n")))
1570 ;; Return the value according to style.
1571 (pcase octave-eldoc-message-style
1572 (`auto
(if (< (length oneline
) (window-width (minibuffer-window)))
1576 (`multiline multiline
)))))
1578 (defcustom octave-help-buffer
"*Octave Help*"
1579 "Buffer name for `octave-help'."
1584 (define-button-type 'octave-help-file
1586 'action
#'help-button-action
1587 'help-function
'octave-find-definition
)
1589 (define-button-type 'octave-help-function
1593 (buffer-substring (button-start b
) (button-end b
)))))
1595 (defvar octave-help-mode-map
1596 (let ((map (make-sparse-keymap)))
1597 (define-key map
"\M-." 'octave-find-definition
)
1598 (define-key map
"\C-hd" 'octave-help
)
1601 (define-derived-mode octave-help-mode help-mode
"OctHelp"
1602 "Major mode for displaying Octave documentation."
1604 :syntax-table octave-mode-syntax-table
1605 (eval-and-compile (require 'help-mode
))
1606 ;; Mostly stolen from `help-make-xrefs'.
1607 (let ((inhibit-read-only t
))
1608 (setq-local info-lookup-mode
'octave-mode
)
1609 ;; Delete extraneous newlines at the end of the docstring
1610 (goto-char (point-max))
1611 (while (and (not (bobp)) (bolp))
1614 (when (or help-xref-stack help-xref-forward-stack
)
1616 (when help-xref-stack
1617 (help-insert-xref-button help-back-label
'help-back
1619 (when help-xref-forward-stack
1620 (when help-xref-stack
1622 (help-insert-xref-button help-forward-label
'help-forward
1624 (when (or help-xref-stack help-xref-forward-stack
)
1627 (defun octave-help (fn)
1628 "Display the documentation of FN."
1629 (interactive (list (octave-completing-read)))
1630 (inferior-octave-send-list-and-digest
1631 (list (format "help ('%s');\n" fn
)))
1632 (let ((lines inferior-octave-output-list
)
1633 (inhibit-read-only t
))
1634 (when (string-match "error: \\(.*\\)$" (car lines
))
1635 (error "%s" (match-string 1 (car lines
))))
1636 (with-help-window octave-help-buffer
1637 (princ (mapconcat 'identity lines
"\n"))
1638 (with-current-buffer octave-help-buffer
1639 ;; Bound to t so that `help-buffer' returns current buffer for
1640 ;; `help-setup-xref'.
1641 (let ((help-xref-following t
))
1642 (help-setup-xref (list 'octave-help fn
)
1643 (called-interactively-p 'interactive
)))
1644 ;; Note: can be turned off by suppress_verbose_help_message.
1646 ;; Remove boring trailing text: Additional help for built-in functions
1647 ;; and operators ...
1648 (goto-char (point-max))
1649 (when (search-backward "\n\n\n" nil t
)
1650 (goto-char (match-beginning 0))
1651 (delete-region (point) (point-max)))
1652 ;; File name highlight
1653 (goto-char (point-min))
1654 (when (re-search-forward "from the file \\(.*\\)$"
1657 (let* ((file (match-string 1))
1658 (dir (file-name-directory
1659 (directory-file-name (file-name-directory file
)))))
1660 (replace-match "" nil nil nil
1)
1662 ;; Include the parent directory which may be regarded as
1663 ;; the category for the FN.
1664 (help-insert-xref-button (file-relative-name file dir
)
1665 'octave-help-file fn
)
1667 ;; Make 'See also' clickable.
1668 (with-syntax-table octave-mode-syntax-table
1669 (when (re-search-forward "^\\s-*See also:" nil t
)
1670 (let ((end (save-excursion (re-search-forward "^\\s-*$" nil t
))))
1671 (while (re-search-forward
1672 "\\s-*\\([^,\n]+?\\)\\s-*\\(?:[,]\\|[.]?$\\)" end t
)
1673 (make-text-button (match-beginning 1) (match-end 1)
1674 :type
'octave-help-function
)))))
1675 (octave-help-mode)))))
1677 (defcustom octave-source-directories nil
1678 "A list of directories for Octave sources.
1679 If the environment variable OCTAVE_SRCDIR is set, it is searched first."
1680 :type
'(repeat directory
)
1684 (defun octave-source-directories ()
1685 (let ((srcdir (or (and inferior-octave-process
1686 (process-get inferior-octave-process
'octave-srcdir
))
1687 (getenv "OCTAVE_SRCDIR"))))
1689 (cons srcdir octave-source-directories
)
1690 octave-source-directories
)))
1692 (defvar octave-find-definition-filename-function
1693 #'octave-find-definition-default-filename
)
1695 (defun octave-find-definition-default-filename (name)
1696 "Default value for `octave-find-definition-filename-function'."
1697 (pcase (file-name-extension name
)
1699 (octave-find-definition-default-filename
1700 (concat "libinterp/dldfcn/"
1701 (file-name-sans-extension (file-name-nondirectory name
))
1704 (let ((file (or (locate-file name
(octave-source-directories))
1705 (locate-file (file-name-nondirectory name
)
1706 (octave-source-directories)))))
1707 (or (and file
(file-exists-p file
))
1708 (error "File `%s' not found" name
))
1711 (if (yes-or-no-p (format "File `%s' may be binary; open? "
1712 (file-name-nondirectory name
)))
1714 (user-error "Aborted")))
1717 (defvar find-tag-marker-ring
)
1719 (defun octave-find-definition (fn)
1720 "Find the definition of FN.
1721 Functions implemented in C++ can be found if
1722 `octave-source-directories' is set correctly."
1723 (interactive (list (octave-completing-read)))
1725 (let ((orig (point)))
1726 (if (and (derived-mode-p 'octave-mode
)
1727 (octave-goto-function-definition fn
))
1728 (ring-insert find-tag-marker-ring
(copy-marker orig
))
1729 (inferior-octave-send-list-and-digest
1730 ;; help NAME is more verbose
1732 if iskeyword('%s') disp('`%s'' is a keyword') else which('%s') endif\n"
1735 ;; Skip garbage lines such as
1736 ;; warning: fmincg.m: possible Matlab-style ....
1737 (while (and (not file
) (consp inferior-octave-output-list
))
1738 (setq line
(pop inferior-octave-output-list
))
1739 (when (string-match "from the file \\(.*\\)$" line
)
1740 (setq file
(match-string 1 line
))))
1742 (user-error "%s" (or line
(format "`%s' not found" fn
)))
1743 (ring-insert find-tag-marker-ring
(point-marker))
1744 (setq file
(funcall octave-find-definition-filename-function file
))
1747 (octave-goto-function-definition fn
)))))))
1750 ;;; octave.el ends here