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