Commit | Line | Data |
---|---|---|
4d2e94d1 | 1 | ;;; octave.el --- editing octave source files under emacs -*- lexical-binding: t; -*- |
f2727dfb | 2 | |
ab422c4d | 3 | ;; Copyright (C) 1997, 2001-2013 Free Software Foundation, Inc. |
f2727dfb | 4 | |
f7fba1a8 | 5 | ;; Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
daf3dc79 | 6 | ;; John Eaton <jwe@octave.org> |
71d4c2a5 | 7 | ;; Maintainer: FSF |
f2727dfb RS |
8 | ;; Keywords: languages |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
b1fc2b50 | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
f2727dfb | 13 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
f2727dfb RS |
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 | |
b1fc2b50 | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
f2727dfb RS |
24 | |
25 | ;;; Commentary: | |
26 | ||
7e138a62 | 27 | ;; This package provides Emacs support for Octave. It defines a major |
02502a5f LL |
28 | ;; mode for editing Octave code and contains code for interacting with |
29 | ;; an inferior Octave process using comint. | |
d1e49742 | 30 | |
be64c05d LL |
31 | ;; See the documentation of `octave-mode' and `run-octave' for further |
32 | ;; information on usage and customization. | |
f2727dfb | 33 | |
d1e49742 | 34 | ;;; Code: |
be64c05d | 35 | (require 'comint) |
f2727dfb | 36 | |
2640d52e LL |
37 | ;;; For emacs < 24.3. |
38 | (require 'newcomment) | |
d74a1581 LL |
39 | (eval-and-compile |
40 | (unless (fboundp 'user-error) | |
c7a8fcac LL |
41 | (defalias 'user-error 'error)) |
42 | (unless (fboundp 'delete-consecutive-dups) | |
43 | (defalias 'delete-consecutive-dups 'delete-dups))) | |
2640d52e LL |
44 | (eval-when-compile |
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)))) | |
49 | ||
28d16ed3 | 50 | (defgroup octave nil |
be64c05d | 51 | "Editing Octave code." |
8ec3bce0 | 52 | :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces) |
28d16ed3 AS |
53 | :group 'languages) |
54 | ||
070ccca4 LL |
55 | (define-obsolete-function-alias 'octave-submit-bug-report |
56 | 'report-emacs-bug "24.4") | |
e7017ef9 | 57 | |
9b92c13b | 58 | (define-abbrev-table 'octave-abbrev-table nil |
e7017ef9 | 59 | "Abbrev table for Octave's reserved words. |
9b92c13b | 60 | Used in `octave-mode' and `inferior-octave-mode' buffers.") |
e7017ef9 RS |
61 | |
62 | (defvar octave-comment-char ?# | |
63 | "Character to start an Octave comment.") | |
9b92c13b | 64 | |
42caeb89 LL |
65 | (defvar octave-comment-start (char-to-string octave-comment-char) |
66 | "Octave-specific `comment-start' (which see).") | |
9b92c13b | 67 | |
42caeb89 LL |
68 | (defvar octave-comment-start-skip "\\(^\\|\\S<\\)\\(?:%!\\|\\s<+\\)\\s-*" |
69 | "Octave-specific `comment-start-skip' (which see).") | |
e7017ef9 RS |
70 | |
71 | (defvar octave-begin-keywords | |
6980b0ca LL |
72 | '("classdef" "do" "enumeration" "events" "for" "function" "if" "methods" |
73 | "parfor" "properties" "switch" "try" "unwind_protect" "while")) | |
9b92c13b | 74 | |
e7017ef9 | 75 | (defvar octave-else-keywords |
6e6d2764 | 76 | '("case" "catch" "else" "elseif" "otherwise" "unwind_protect_cleanup")) |
9b92c13b | 77 | |
e7017ef9 | 78 | (defvar octave-end-keywords |
6980b0ca LL |
79 | '("endclassdef" "endenumeration" "endevents" "endfor" "endfunction" "endif" |
80 | "endmethods" "endparfor" "endproperties" "endswitch" "end_try_catch" | |
b2ad70b6 | 81 | "end_unwind_protect" "endwhile" "until" "end")) |
e7017ef9 RS |
82 | |
83 | (defvar octave-reserved-words | |
6e6d2764 KH |
84 | (append octave-begin-keywords |
85 | octave-else-keywords | |
86 | octave-end-keywords | |
6980b0ca | 87 | '("break" "continue" "global" "persistent" "return")) |
e7017ef9 RS |
88 | "Reserved words in Octave.") |
89 | ||
e7017ef9 | 90 | (defvar octave-function-header-regexp |
e92f3bd3 | 91 | (concat "^\\s-*\\_<\\(function\\)\\_>" |
ce8209d4 | 92 | "\\([^=;(\n]*=[ \t]*\\|[ \t]*\\)\\(\\(?:\\w\\|\\s_\\)+\\)\\_>") |
e7017ef9 RS |
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.") | |
96 | ||
be64c05d | 97 | \f |
e22bbd48 | 98 | (defvar octave-mode-map |
f2727dfb | 99 | (let ((map (make-sparse-keymap))) |
f5a9432f LL |
100 | (define-key map "\M-." 'octave-find-definition) |
101 | (define-key map "\M-\C-j" 'octave-indent-new-comment-line) | |
f2727dfb RS |
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) | |
a1506d29 | 105 | (define-key map "\C-c\C-e" 'octave-end-of-line) |
ec5d3ff7 | 106 | (define-key map [remap down-list] 'smie-down-list) |
f2727dfb | 107 | (define-key map "\C-c\M-\C-h" 'octave-mark-block) |
e17b68ed SM |
108 | (define-key map "\C-c]" 'smie-close-block) |
109 | (define-key map "\C-c/" 'smie-close-block) | |
b7260dd4 | 110 | (define-key map "\C-c;" 'octave-update-function-file-comment) |
d4d0f9b3 | 111 | (define-key map "\C-hd" 'octave-help) |
5af5ed08 | 112 | (define-key map "\C-ha" 'octave-lookfor) |
06e752b4 | 113 | (define-key map "\C-c\C-l" 'octave-source-file) |
34a463f1 | 114 | (define-key map "\C-c\C-f" 'octave-insert-defun) |
34a463f1 RS |
115 | (define-key map "\C-c\C-il" 'octave-send-line) |
116 | (define-key map "\C-c\C-ib" 'octave-send-block) | |
117 | (define-key map "\C-c\C-if" 'octave-send-defun) | |
a1506d29 | 118 | (define-key map "\C-c\C-ir" 'octave-send-region) |
7e138a62 | 119 | (define-key map "\C-c\C-ia" 'octave-send-buffer) |
34a463f1 | 120 | (define-key map "\C-c\C-is" 'octave-show-process-buffer) |
ad64b83d | 121 | (define-key map "\C-c\C-iq" 'octave-hide-process-buffer) |
34a463f1 RS |
122 | (define-key map "\C-c\C-ik" 'octave-kill-process) |
123 | (define-key map "\C-c\C-i\C-l" 'octave-send-line) | |
124 | (define-key map "\C-c\C-i\C-b" 'octave-send-block) | |
125 | (define-key map "\C-c\C-i\C-f" 'octave-send-defun) | |
a1506d29 | 126 | (define-key map "\C-c\C-i\C-r" 'octave-send-region) |
7e138a62 | 127 | (define-key map "\C-c\C-i\C-a" 'octave-send-buffer) |
34a463f1 | 128 | (define-key map "\C-c\C-i\C-s" 'octave-show-process-buffer) |
7a1beb6c | 129 | (define-key map "\C-c\C-i\C-q" 'octave-hide-process-buffer) |
34a463f1 | 130 | (define-key map "\C-c\C-i\C-k" 'octave-kill-process) |
e22bbd48 SM |
131 | map) |
132 | "Keymap used in Octave mode.") | |
133 | ||
f2727dfb | 134 | |
ff80a446 SM |
135 | |
136 | (easy-menu-define octave-mode-menu octave-mode-map | |
137 | "Menu for Octave mode." | |
e22bbd48 | 138 | '("Octave" |
ee44b62a LL |
139 | ["Split Line at Point" octave-indent-new-comment-line t] |
140 | ["Previous Code Line" octave-previous-code-line t] | |
141 | ["Next Code Line" octave-next-code-line t] | |
142 | ["Begin of Line" octave-beginning-of-line t] | |
143 | ["End of Line" octave-end-of-line t] | |
144 | ["Mark Block" octave-mark-block t] | |
145 | ["Close Block" smie-close-block t] | |
146 | "---" | |
147 | ["Start Octave Process" run-octave t] | |
3ac7cff4 | 148 | ["Documentation Lookup" info-lookup-symbol t] |
ee44b62a | 149 | ["Help on Function" octave-help t] |
5af5ed08 | 150 | ["Search help" octave-lookfor t] |
ee44b62a LL |
151 | ["Find Function Definition" octave-find-definition t] |
152 | ["Insert Function" octave-insert-defun t] | |
153 | ["Update Function File Comment" octave-update-function-file-comment t] | |
154 | "---" | |
155 | ["Function Syntax Hints" (call-interactively | |
156 | (if (fboundp 'eldoc-post-insert-mode) | |
157 | 'eldoc-post-insert-mode | |
158 | 'eldoc-mode)) | |
159 | :style toggle :selected (or eldoc-post-insert-mode eldoc-mode) | |
160 | :help "Display function signatures after typing `SPC' or `('"] | |
976cb066 LL |
161 | ["Delimiter Matching" show-paren-mode |
162 | :style toggle :selected show-paren-mode | |
ee44b62a | 163 | :help "Highlight matched pairs such as `if ... end'" |
976cb066 | 164 | :visible (fboundp 'smie--matching-block-data)] |
ee44b62a LL |
165 | ["Auto Fill" auto-fill-mode |
166 | :style toggle :selected auto-fill-function | |
167 | :help "Automatic line breaking"] | |
168 | ["Electric Layout" electric-layout-mode | |
169 | :style toggle :selected electric-layout-mode | |
170 | :help "Automatically insert newlines around some chars"] | |
171 | "---" | |
67885e8c | 172 | ("Debug" |
ee44b62a LL |
173 | ["Send Current Line" octave-send-line t] |
174 | ["Send Current Block" octave-send-block t] | |
175 | ["Send Current Function" octave-send-defun t] | |
176 | ["Send Region" octave-send-region t] | |
7e138a62 | 177 | ["Send Buffer" octave-send-buffer t] |
06e752b4 | 178 | ["Source Current File" octave-source-file t] |
ee44b62a LL |
179 | ["Show Process Buffer" octave-show-process-buffer t] |
180 | ["Hide Process Buffer" octave-hide-process-buffer t] | |
181 | ["Kill Process" octave-kill-process t]) | |
182 | "---" | |
183 | ["Customize Octave" (customize-group 'octave) t] | |
184 | ["Submit Bug Report" report-emacs-bug t])) | |
f2727dfb | 185 | |
cb3d3ec1 | 186 | (defvar octave-mode-syntax-table |
f2727dfb RS |
187 | (let ((table (make-syntax-table))) |
188 | (modify-syntax-entry ?\r " " 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 | (modify-syntax-entry ?! "." table) | |
27135018 | 199 | (modify-syntax-entry ?\\ "." table) |
f2727dfb | 200 | (modify-syntax-entry ?\' "." table) |
7210c33f | 201 | (modify-syntax-entry ?\` "." table) |
c75c93c7 | 202 | (modify-syntax-entry ?. "." table) |
f2727dfb | 203 | (modify-syntax-entry ?\" "\"" table) |
e92f3bd3 | 204 | (modify-syntax-entry ?_ "_" table) |
c5683ceb | 205 | ;; The "b" flag only applies to the second letter of the comstart |
7e82caa7 SM |
206 | ;; and the first letter of the comend, i.e. the "4b" below is ineffective. |
207 | ;; If we try to put `b' on the single-line comments, we get a similar | |
208 | ;; problem where the % and # chars appear as first chars of the 2-char | |
209 | ;; comend, so the multi-line ender is also turned into style-b. | |
c5683ceb | 210 | ;; So we need the new "c" comment style. |
c82d5b11 SM |
211 | (modify-syntax-entry ?\% "< 13" table) |
212 | (modify-syntax-entry ?\# "< 13" table) | |
c5683ceb SM |
213 | (modify-syntax-entry ?\{ "(} 2c" table) |
214 | (modify-syntax-entry ?\} "){ 4c" table) | |
f2727dfb | 215 | (modify-syntax-entry ?\n ">" table) |
cb3d3ec1 SM |
216 | table) |
217 | "Syntax table in use in `octave-mode' buffers.") | |
f2727dfb | 218 | |
5b78d7fc LL |
219 | (defcustom octave-font-lock-texinfo-comment t |
220 | "Control whether to highlight the texinfo comment block." | |
221 | :type 'boolean | |
222 | :group 'octave | |
223 | :version "24.4") | |
224 | ||
28d16ed3 | 225 | (defcustom octave-blink-matching-block t |
e22bbd48 | 226 | "Control the blinking of matching Octave block keywords. |
f2727dfb | 227 | Non-nil means show matching begin of block when inserting a space, |
28d16ed3 AS |
228 | newline or semicolon after an else or end keyword." |
229 | :type 'boolean | |
230 | :group 'octave) | |
ff80a446 | 231 | |
28d16ed3 | 232 | (defcustom octave-block-offset 2 |
e22bbd48 | 233 | "Extra indentation applied to statements in Octave block structures." |
28d16ed3 AS |
234 | :type 'integer |
235 | :group 'octave) | |
f2727dfb | 236 | |
f2727dfb RS |
237 | (defvar octave-block-comment-start |
238 | (concat (make-string 2 octave-comment-char) " ") | |
239 | "String to insert to start a new Octave comment on an empty line.") | |
240 | ||
28d16ed3 | 241 | (defcustom octave-continuation-offset 4 |
e22bbd48 | 242 | "Extra indentation applied to Octave continuation lines." |
28d16ed3 AS |
243 | :type 'integer |
244 | :group 'octave) | |
38d8527b | 245 | |
e17b68ed SM |
246 | (eval-and-compile |
247 | (defconst octave-continuation-marker-regexp "\\\\\\|\\.\\.\\.")) | |
38d8527b | 248 | |
f2727dfb | 249 | (defvar octave-continuation-regexp |
e17b68ed SM |
250 | (concat "[^#%\n]*\\(" octave-continuation-marker-regexp |
251 | "\\)\\s-*\\(\\s<.*\\)?$")) | |
38d8527b LL |
252 | |
253 | ;; Char \ is considered a bad decision for continuing a line. | |
254 | (defconst octave-continuation-string "..." | |
255 | "Character string used for Octave continuation lines.") | |
f2727dfb | 256 | |
f2727dfb RS |
257 | (defvar octave-mode-imenu-generic-expression |
258 | (list | |
259 | ;; Functions | |
260 | (list nil octave-function-header-regexp 3)) | |
261 | "Imenu expression for Octave mode. See `imenu-generic-expression'.") | |
262 | ||
28d16ed3 | 263 | (defcustom octave-mode-hook nil |
e22bbd48 | 264 | "Hook to be run when Octave mode is started." |
28d16ed3 AS |
265 | :type 'hook |
266 | :group 'octave) | |
267 | ||
268 | (defcustom octave-send-show-buffer t | |
e22bbd48 | 269 | "Non-nil means display `inferior-octave-buffer' after sending to it." |
28d16ed3 AS |
270 | :type 'boolean |
271 | :group 'octave) | |
9b92c13b | 272 | |
28d16ed3 | 273 | (defcustom octave-send-line-auto-forward t |
e22bbd48 | 274 | "Control auto-forward after sending to the inferior Octave process. |
28d16ed3 AS |
275 | Non-nil means always go to the next Octave code line after sending." |
276 | :type 'boolean | |
277 | :group 'octave) | |
9b92c13b | 278 | |
28d16ed3 | 279 | (defcustom octave-send-echo-input t |
e22bbd48 | 280 | "Non-nil means echo input sent to the inferior Octave process." |
28d16ed3 AS |
281 | :type 'boolean |
282 | :group 'octave) | |
f2727dfb RS |
283 | |
284 | \f | |
e17b68ed SM |
285 | ;;; SMIE indentation |
286 | ||
287 | (require 'smie) | |
288 | ||
203a5572 | 289 | ;; Use '__operators__' in Octave REPL to get a full list. |
e17b68ed SM |
290 | (defconst octave-operator-table |
291 | '((assoc ";" "\n") (assoc ",") ; The doc claims they have equal precedence!? | |
292 | (right "=" "+=" "-=" "*=" "/=") | |
293 | (assoc "&&") (assoc "||") ; The doc claims they have equal precedence!? | |
294 | (assoc "&") (assoc "|") ; The doc claims they have equal precedence!? | |
295 | (nonassoc "<" "<=" "==" ">=" ">" "!=" "~=") | |
296 | (nonassoc ":") ;No idea what this is. | |
297 | (assoc "+" "-") | |
298 | (assoc "*" "/" "\\" ".\\" ".*" "./") | |
299 | (nonassoc "'" ".'") | |
300 | (nonassoc "++" "--" "!" "~") ;And unary "+" and "-". | |
301 | (right "^" "**" ".^" ".**") | |
302 | ;; It's not really an operator, but for indentation purposes it | |
303 | ;; could be convenient to treat it as one. | |
304 | (assoc "..."))) | |
305 | ||
89acf735 SM |
306 | (defconst octave-smie-bnf-table |
307 | '((atom) | |
308 | ;; We can't distinguish the first element in a sequence with | |
309 | ;; precedence grammars, so we can't distinguish the condition | |
310 | ;; if the `if' from the subsequent body, for example. | |
311 | ;; This has to be done later in the indentation rules. | |
312 | (exp (exp "\n" exp) | |
313 | ;; We need to mention at least one of the operators in this part | |
314 | ;; of the grammar: if the BNF and the operator table have | |
315 | ;; no overlap, SMIE can't know how they relate. | |
316 | (exp ";" exp) | |
317 | ("try" exp "catch" exp "end_try_catch") | |
318 | ("try" exp "catch" exp "end") | |
319 | ("unwind_protect" exp | |
320 | "unwind_protect_cleanup" exp "end_unwind_protect") | |
321 | ("unwind_protect" exp "unwind_protect_cleanup" exp "end") | |
322 | ("for" exp "endfor") | |
323 | ("for" exp "end") | |
6980b0ca LL |
324 | ("parfor" exp "endparfor") |
325 | ("parfor" exp "end") | |
89acf735 SM |
326 | ("do" exp "until" atom) |
327 | ("while" exp "endwhile") | |
328 | ("while" exp "end") | |
329 | ("if" exp "endif") | |
330 | ("if" exp "else" exp "endif") | |
331 | ("if" exp "elseif" exp "else" exp "endif") | |
332 | ("if" exp "elseif" exp "elseif" exp "else" exp "endif") | |
333 | ("if" exp "elseif" exp "elseif" exp "else" exp "end") | |
334 | ("switch" exp "case" exp "endswitch") | |
335 | ("switch" exp "case" exp "otherwise" exp "endswitch") | |
336 | ("switch" exp "case" exp "case" exp "otherwise" exp "endswitch") | |
337 | ("switch" exp "case" exp "case" exp "otherwise" exp "end") | |
338 | ("function" exp "endfunction") | |
6980b0ca LL |
339 | ("function" exp "end") |
340 | ("enumeration" exp "endenumeration") | |
341 | ("enumeration" exp "end") | |
342 | ("events" exp "endevents") | |
343 | ("events" exp "end") | |
344 | ("methods" exp "endmethods") | |
345 | ("methods" exp "end") | |
346 | ("properties" exp "endproperties") | |
347 | ("properties" exp "end") | |
348 | ("classdef" exp "endclassdef") | |
349 | ("classdef" exp "end")) | |
89acf735 SM |
350 | ;; (fundesc (atom "=" atom)) |
351 | )) | |
352 | ||
674728d4 SM |
353 | (defconst octave-smie-grammar |
354 | (smie-prec2->grammar | |
e17b68ed | 355 | (smie-merge-prec2s |
674728d4 SM |
356 | (smie-bnf->prec2 octave-smie-bnf-table |
357 | '((assoc "\n" ";"))) | |
e17b68ed | 358 | |
674728d4 | 359 | (smie-precs->prec2 octave-operator-table)))) |
e17b68ed SM |
360 | |
361 | ;; Tokenizing needs to be refined so that ";;" is treated as two | |
362 | ;; tokens and also so as to recognize the \n separator (and | |
363 | ;; corresponding continuation lines). | |
364 | ||
365 | (defconst octave-operator-regexp | |
7a7567d2 LL |
366 | (regexp-opt (remove "\n" (apply 'append |
367 | (mapcar 'cdr octave-operator-table))))) | |
e17b68ed SM |
368 | |
369 | (defun octave-smie-backward-token () | |
370 | (let ((pos (point))) | |
371 | (forward-comment (- (point))) | |
372 | (cond | |
373 | ((and (not (eq (char-before) ?\;)) ;Coalesce ";" and "\n". | |
374 | (> pos (line-end-position)) | |
375 | (if (looking-back octave-continuation-marker-regexp (- (point) 3)) | |
376 | (progn | |
377 | (goto-char (match-beginning 0)) | |
378 | (forward-comment (- (point))) | |
379 | nil) | |
380 | t) | |
381 | ;; Ignore it if it's within parentheses. | |
382 | (let ((ppss (syntax-ppss))) | |
383 | (not (and (nth 1 ppss) | |
384 | (eq ?\( (char-after (nth 1 ppss))))))) | |
385 | (skip-chars-forward " \t") | |
386 | ;; Why bother distinguishing \n and ;? | |
387 | ";") ;;"\n" | |
388 | ((and (looking-back octave-operator-regexp (- (point) 3) 'greedy) | |
389 | ;; Don't mistake a string quote for a transpose. | |
390 | (not (looking-back "\\s\"" (1- (point))))) | |
391 | (goto-char (match-beginning 0)) | |
392 | (match-string-no-properties 0)) | |
393 | (t | |
394 | (smie-default-backward-token))))) | |
395 | ||
396 | (defun octave-smie-forward-token () | |
397 | (skip-chars-forward " \t") | |
398 | (when (looking-at (eval-when-compile | |
399 | (concat "\\(" octave-continuation-marker-regexp | |
400 | "\\)[ \t]*\\($\\|[%#]\\)"))) | |
401 | (goto-char (match-end 1)) | |
402 | (forward-comment 1)) | |
403 | (cond | |
ceb57e59 SM |
404 | ((and (looking-at "[%#\n]") |
405 | (not (or (save-excursion (skip-chars-backward " \t") | |
406 | ;; Only add implicit ; when needed. | |
150194c3 | 407 | (or (bolp) (eq (char-before) ?\;))) |
ceb57e59 SM |
408 | ;; Ignore it if it's within parentheses. |
409 | (let ((ppss (syntax-ppss))) | |
410 | (and (nth 1 ppss) | |
411 | (eq ?\( (char-after (nth 1 ppss)))))))) | |
412 | (if (eolp) (forward-char 1) (forward-comment 1)) | |
e17b68ed SM |
413 | ;; Why bother distinguishing \n and ;? |
414 | ";") ;;"\n" | |
ceb57e59 | 415 | ((progn (forward-comment (point-max)) nil)) |
e17b68ed SM |
416 | ((looking-at ";[ \t]*\\($\\|[%#]\\)") |
417 | ;; Combine the ; with the subsequent \n. | |
418 | (goto-char (match-beginning 1)) | |
419 | (forward-comment 1) | |
420 | ";") | |
421 | ((and (looking-at octave-operator-regexp) | |
422 | ;; Don't mistake a string quote for a transpose. | |
423 | (not (looking-at "\\s\""))) | |
424 | (goto-char (match-end 0)) | |
425 | (match-string-no-properties 0)) | |
426 | (t | |
427 | (smie-default-forward-token)))) | |
428 | ||
c4d17d50 SM |
429 | (defun octave-smie-rules (kind token) |
430 | (pcase (cons kind token) | |
674728d4 SM |
431 | ;; We could set smie-indent-basic instead, but that would have two |
432 | ;; disadvantages: | |
433 | ;; - changes to octave-block-offset wouldn't take effect immediately. | |
434 | ;; - edebug wouldn't show the use of this variable. | |
c4d17d50 | 435 | (`(:elem . basic) octave-block-offset) |
674728d4 SM |
436 | ;; Since "case" is in the same BNF rules as switch..end, SMIE by default |
437 | ;; aligns it with "switch". | |
438 | (`(:before . "case") (if (not (smie-rule-sibling-p)) octave-block-offset)) | |
c4d17d50 | 439 | (`(:after . ";") |
6980b0ca LL |
440 | (if (smie-rule-parent-p "classdef" "events" "enumeration" "function" "if" |
441 | "while" "else" "elseif" "for" "parfor" | |
442 | "properties" "methods" "otherwise" "case" | |
443 | "try" "catch" "unwind_protect" | |
674728d4 SM |
444 | "unwind_protect_cleanup") |
445 | (smie-rule-parent octave-block-offset) | |
c4d17d50 SM |
446 | ;; For (invalid) code between switch and case. |
447 | ;; (if (smie-parent-p "switch") 4) | |
650cff3d | 448 | nil)))) |
e17b68ed | 449 | |
9dbdb67e LL |
450 | (defun octave-indent-comment () |
451 | "A function for `smie-indent-functions' (which see)." | |
452 | (save-excursion | |
453 | (back-to-indentation) | |
2aeb3a1d LL |
454 | (cond |
455 | ((octave-in-string-or-comment-p) nil) | |
f236dd84 | 456 | ((looking-at-p "\\(\\s<\\)\\1\\{2,\\}") |
2aeb3a1d | 457 | 0) |
b0e069c2 LL |
458 | ;; Exclude %{, %} and %!. |
459 | ((and (looking-at-p "\\s<\\(?:[^{}!]\\|$\\)") | |
f236dd84 | 460 | (not (looking-at-p "\\(\\s<\\)\\1"))) |
2aeb3a1d | 461 | (comment-choose-indent))))) |
9dbdb67e | 462 | |
203a5572 LL |
463 | \f |
464 | (defvar octave-font-lock-keywords | |
465 | (list | |
466 | ;; Fontify all builtin keywords. | |
467 | (cons (concat "\\_<\\(" | |
468 | (regexp-opt octave-reserved-words) | |
469 | "\\)\\_>") | |
470 | 'font-lock-keyword-face) | |
471 | ;; Note: 'end' also serves as the last index in an indexing expression. | |
472 | ;; Ref: http://www.mathworks.com/help/matlab/ref/end.html | |
473 | (list (lambda (limit) | |
474 | (while (re-search-forward "\\_<end\\_>" limit 'move) | |
475 | (let ((beg (match-beginning 0)) | |
476 | (end (match-end 0))) | |
477 | (unless (octave-in-string-or-comment-p) | |
4d25fd7e | 478 | (condition-case nil |
203a5572 LL |
479 | (progn |
480 | (goto-char beg) | |
481 | (backward-up-list) | |
482 | (when (memq (char-after) '(?\( ?\[ ?\{)) | |
4d25fd7e LL |
483 | (put-text-property beg end 'face nil)) |
484 | (goto-char end)) | |
485 | (error (goto-char end)))))) | |
203a5572 LL |
486 | nil)) |
487 | ;; Fontify all operators. | |
488 | (cons octave-operator-regexp 'font-lock-builtin-face) | |
489 | ;; Fontify all function declarations. | |
490 | (list octave-function-header-regexp | |
491 | '(1 font-lock-keyword-face) | |
492 | '(3 font-lock-function-name-face nil t))) | |
493 | "Additional Octave expressions to highlight.") | |
494 | ||
495 | (defun octave-syntax-propertize-function (start end) | |
496 | (goto-char start) | |
497 | (octave-syntax-propertize-sqs end) | |
498 | (funcall (syntax-propertize-rules | |
499 | ("\\\\" (0 (when (eq (nth 3 (save-excursion | |
500 | (syntax-ppss (match-beginning 0)))) | |
501 | ?\") | |
502 | (string-to-syntax "\\")))) | |
503 | ;; Try to distinguish the string-quotes from the transpose-quotes. | |
504 | ("\\(?:^\\|[[({,; ]\\)\\('\\)" | |
505 | (1 (prog1 "\"'" (octave-syntax-propertize-sqs end))))) | |
506 | (point) end)) | |
507 | ||
508 | (defun octave-syntax-propertize-sqs (end) | |
509 | "Propertize the content/end of single-quote strings." | |
510 | (when (eq (nth 3 (syntax-ppss)) ?\') | |
511 | ;; A '..' string. | |
512 | (when (re-search-forward | |
513 | "\\(?:\\=\\|[^']\\)\\(?:''\\)*\\('\\)\\($\\|[^']\\)" end 'move) | |
514 | (goto-char (match-beginning 2)) | |
515 | (when (eq (char-before (match-beginning 1)) ?\\) | |
516 | ;; Backslash cannot escape a single quote. | |
517 | (put-text-property (1- (match-beginning 1)) (match-beginning 1) | |
518 | 'syntax-table (string-to-syntax "."))) | |
519 | (put-text-property (match-beginning 1) (match-end 1) | |
520 | 'syntax-table (string-to-syntax "\"'"))))) | |
521 | ||
03f70355 | 522 | (defvar electric-layout-rules) |
b073dc4b | 523 | |
f2727dfb | 524 | ;;;###autoload |
c82d5b11 | 525 | (define-derived-mode octave-mode prog-mode "Octave" |
f2727dfb RS |
526 | "Major mode for editing Octave code. |
527 | ||
b7260dd4 LL |
528 | Octave is a high-level language, primarily intended for numerical |
529 | computations. It provides a convenient command line interface | |
530 | for solving linear and nonlinear problems numerically. Function | |
531 | definitions can also be stored in files and used in batch mode." | |
9b92c13b | 532 | :abbrev-table octave-abbrev-table |
c82d5b11 | 533 | |
674728d4 | 534 | (smie-setup octave-smie-grammar #'octave-smie-rules |
c4d17d50 SM |
535 | :forward-token #'octave-smie-forward-token |
536 | :backward-token #'octave-smie-backward-token) | |
070ccca4 | 537 | (setq-local smie-indent-basic 'octave-block-offset) |
9dbdb67e | 538 | (add-hook 'smie-indent-functions #'octave-indent-comment nil t) |
c4d17d50 | 539 | |
070ccca4 LL |
540 | (setq-local smie-blink-matching-triggers |
541 | (cons ?\; smie-blink-matching-triggers)) | |
c4d17d50 SM |
542 | (unless octave-blink-matching-block |
543 | (remove-hook 'post-self-insert-hook #'smie-blink-matching-open 'local)) | |
89acf735 | 544 | |
070ccca4 LL |
545 | (setq-local electric-indent-chars |
546 | (cons ?\; electric-indent-chars)) | |
03f70355 SM |
547 | ;; IIUC matlab-mode takes the opposite approach: it makes RET insert |
548 | ;; a ";" at those places where it's correct (i.e. outside of parens). | |
070ccca4 | 549 | (setq-local electric-layout-rules '((?\; . after))) |
c82d5b11 | 550 | |
38637cca | 551 | (setq-local comment-use-syntax t) |
070ccca4 LL |
552 | (setq-local comment-start octave-comment-start) |
553 | (setq-local comment-end "") | |
b0e069c2 | 554 | (setq-local comment-start-skip octave-comment-start-skip) |
070ccca4 LL |
555 | (setq-local comment-add 1) |
556 | ||
557 | (setq-local parse-sexp-ignore-comments t) | |
558 | (setq-local paragraph-start (concat "\\s-*$\\|" page-delimiter)) | |
559 | (setq-local paragraph-separate paragraph-start) | |
560 | (setq-local paragraph-ignore-fill-prefix t) | |
561 | (setq-local fill-paragraph-function 'octave-fill-paragraph) | |
9cc3e83f | 562 | |
ee44b62a LL |
563 | (setq-local fill-nobreak-predicate |
564 | (lambda () (eq (octave-in-string-p) ?'))) | |
62efb35e LL |
565 | (with-no-warnings |
566 | (if (fboundp 'add-function) ; new in 24.4 | |
567 | (add-function :around (local 'comment-line-break-function) | |
568 | #'octave--indent-new-comment-line) | |
569 | (setq-local comment-line-break-function | |
570 | (apply-partially #'octave--indent-new-comment-line | |
571 | #'comment-indent-new-line)))) | |
c82d5b11 | 572 | |
070ccca4 | 573 | (setq font-lock-defaults '(octave-font-lock-keywords)) |
cf38dd42 | 574 | |
070ccca4 | 575 | (setq-local syntax-propertize-function #'octave-syntax-propertize-function) |
c82d5b11 | 576 | |
02502a5f LL |
577 | (setq-local imenu-generic-expression octave-mode-imenu-generic-expression) |
578 | (setq-local imenu-case-fold-search nil) | |
c82d5b11 | 579 | |
fda74125 LL |
580 | (setq-local add-log-current-defun-function #'octave-add-log-current-defun) |
581 | ||
203a5572 | 582 | (add-hook 'completion-at-point-functions 'octave-completion-at-point nil t) |
e55d3b04 | 583 | (add-hook 'before-save-hook 'octave-sync-function-file-names nil t) |
070ccca4 | 584 | (setq-local beginning-of-defun-function 'octave-beginning-of-defun) |
5b78d7fc | 585 | (and octave-font-lock-texinfo-comment (octave-font-lock-texinfo-comment)) |
c8730c3a | 586 | (setq-local eldoc-documentation-function 'octave-eldoc-function) |
f2727dfb | 587 | |
584ea277 | 588 | (easy-menu-add octave-mode-menu)) |
be64c05d LL |
589 | |
590 | \f | |
591 | (defcustom inferior-octave-program "octave" | |
592 | "Program invoked by `inferior-octave'." | |
593 | :type 'string | |
594 | :group 'octave) | |
595 | ||
596 | (defcustom inferior-octave-buffer "*Inferior Octave*" | |
597 | "Name of buffer for running an inferior Octave process." | |
598 | :type 'string | |
599 | :group 'octave) | |
600 | ||
601 | (defcustom inferior-octave-prompt | |
8c09f64b | 602 | "\\(^octave\\(\\|.bin\\|.exe\\|-gui\\)\\(-[.0-9]+\\)?\\(:[0-9]+\\)?\\|^debug\\|^\\)>+ " |
be64c05d LL |
603 | "Regexp to match prompts for the inferior Octave process." |
604 | :type 'regexp | |
605 | :group 'octave) | |
606 | ||
b12d33d7 | 607 | (defcustom inferior-octave-prompt-read-only comint-prompt-read-only |
5147fc17 | 608 | "If non-nil, the Octave prompt is read only. |
b12d33d7 | 609 | See `comint-prompt-read-only' for details." |
5147fc17 RS |
610 | :type 'boolean |
611 | :group 'octave | |
612 | :version "24.4") | |
613 | ||
0d634d3a | 614 | (defcustom inferior-octave-startup-file |
481a8e0f RS |
615 | (let ((n (file-name-nondirectory inferior-octave-program))) |
616 | (locate-user-emacs-file (format "init_%s.m" n) (format ".emacs-%s" n))) | |
be64c05d LL |
617 | "Name of the inferior Octave startup file. |
618 | The contents of this file are sent to the inferior Octave process on | |
619 | startup." | |
0d634d3a LL |
620 | :type '(choice (const :tag "None" nil) file) |
621 | :group 'octave | |
622 | :version "24.4") | |
be64c05d | 623 | |
daabf15a | 624 | (defcustom inferior-octave-startup-args '("-i" "--no-line-editing") |
be64c05d LL |
625 | "List of command line arguments for the inferior Octave process. |
626 | For example, for suppressing the startup message and using `traditional' | |
daabf15a | 627 | mode, include \"-q\" and \"--traditional\"." |
be64c05d | 628 | :type '(repeat string) |
daabf15a LL |
629 | :group 'octave |
630 | :version "24.4") | |
be64c05d LL |
631 | |
632 | (defcustom inferior-octave-mode-hook nil | |
633 | "Hook to be run when Inferior Octave mode is started." | |
634 | :type 'hook | |
635 | :group 'octave) | |
636 | ||
1dab32a3 LL |
637 | (defcustom inferior-octave-error-regexp-alist |
638 | '(("error:\\s-*\\(.*?\\) at line \\([0-9]+\\), column \\([0-9]+\\)" | |
639 | 1 2 3 2 1) | |
640 | ("warning:\\s-*\\([^:\n]+\\):.*at line \\([0-9]+\\), column \\([0-9]+\\)" | |
641 | 1 2 3 1 1)) | |
642 | "Value for `compilation-error-regexp-alist' in inferior octave." | |
643 | :type '(repeat (choice (symbol :tag "Predefined symbol") | |
644 | (sexp :tag "Error specification"))) | |
645 | :group 'octave) | |
646 | ||
647 | (defvar inferior-octave-compilation-font-lock-keywords | |
648 | '(("\\_<PASS\\_>" . compilation-info-face) | |
649 | ("\\_<FAIL\\_>" . compilation-error-face) | |
650 | ("\\_<\\(warning\\):" 1 compilation-warning-face) | |
651 | ("\\_<\\(error\\):" 1 compilation-error-face) | |
652 | ("^\\s-*!!!!!.*\\|^.*failed$" . compilation-error-face)) | |
653 | "Value for `compilation-mode-font-lock-keywords' in inferior octave.") | |
654 | ||
be64c05d LL |
655 | (defvar inferior-octave-process nil) |
656 | ||
657 | (defvar inferior-octave-mode-map | |
658 | (let ((map (make-sparse-keymap))) | |
659 | (set-keymap-parent map comint-mode-map) | |
d4d0f9b3 | 660 | (define-key map "\M-." 'octave-find-definition) |
c129b51f | 661 | (define-key map "\t" 'completion-at-point) |
d4d0f9b3 | 662 | (define-key map "\C-hd" 'octave-help) |
5af5ed08 | 663 | (define-key map "\C-ha" 'octave-lookfor) |
c129b51f | 664 | ;; Same as in `shell-mode'. |
be64c05d LL |
665 | (define-key map "\M-?" 'comint-dynamic-list-filename-completions) |
666 | (define-key map "\C-c\C-l" 'inferior-octave-dynamic-list-input-ring) | |
667 | (define-key map [menu-bar inout list-history] | |
668 | '("List Input History" . inferior-octave-dynamic-list-input-ring)) | |
669 | map) | |
670 | "Keymap used in Inferior Octave mode.") | |
671 | ||
672 | (defvar inferior-octave-mode-syntax-table | |
673 | (let ((table (make-syntax-table octave-mode-syntax-table))) | |
674 | table) | |
7e138a62 | 675 | "Syntax table in use in `inferior-octave-mode' buffers.") |
be64c05d LL |
676 | |
677 | (defvar inferior-octave-font-lock-keywords | |
678 | (list | |
679 | (cons inferior-octave-prompt 'font-lock-type-face)) | |
680 | ;; Could certainly do more font locking in inferior Octave ... | |
681 | "Additional expressions to highlight in Inferior Octave mode.") | |
682 | ||
be64c05d LL |
683 | (defvar inferior-octave-output-list nil) |
684 | (defvar inferior-octave-output-string nil) | |
685 | (defvar inferior-octave-receive-in-progress nil) | |
686 | ||
2ec12cb0 LL |
687 | (define-obsolete-variable-alias 'inferior-octave-startup-hook |
688 | 'inferior-octave-mode-hook "24.4") | |
be64c05d | 689 | |
be64c05d LL |
690 | (defvar inferior-octave-dynamic-complete-functions |
691 | '(inferior-octave-completion-at-point comint-filename-completion) | |
692 | "List of functions called to perform completion for inferior Octave. | |
693 | This variable is used to initialize `comint-dynamic-complete-functions' | |
694 | in the Inferior Octave buffer.") | |
695 | ||
696 | (defvar info-lookup-mode) | |
1dab32a3 LL |
697 | (defvar compilation-error-regexp-alist) |
698 | (defvar compilation-mode-font-lock-keywords) | |
be64c05d | 699 | |
939fb29c LL |
700 | (declare-function compilation-forget-errors "compile" ()) |
701 | ||
724bc265 | 702 | (defun inferior-octave-process-live-p () |
0518b057 | 703 | (process-live-p inferior-octave-process)) |
724bc265 | 704 | |
be64c05d | 705 | (define-derived-mode inferior-octave-mode comint-mode "Inferior Octave" |
b7260dd4 | 706 | "Major mode for interacting with an inferior Octave process." |
9b92c13b LL |
707 | :abbrev-table octave-abbrev-table |
708 | (setq comint-prompt-regexp inferior-octave-prompt) | |
be64c05d | 709 | |
38637cca | 710 | (setq-local comment-use-syntax t) |
070ccca4 LL |
711 | (setq-local comment-start octave-comment-start) |
712 | (setq-local comment-end "") | |
713 | (setq comment-column 32) | |
714 | (setq-local comment-start-skip octave-comment-start-skip) | |
be64c05d | 715 | |
070ccca4 | 716 | (setq font-lock-defaults '(inferior-octave-font-lock-keywords nil nil)) |
be64c05d | 717 | |
02502a5f | 718 | (setq-local info-lookup-mode 'octave-mode) |
c8730c3a | 719 | (setq-local eldoc-documentation-function 'octave-eldoc-function) |
be64c05d LL |
720 | |
721 | (setq comint-input-ring-file-name | |
c75c93c7 LL |
722 | (or (getenv "OCTAVE_HISTFILE") "~/.octave_hist") |
723 | comint-input-ring-size (or (getenv "OCTAVE_HISTSIZE") 1024)) | |
939fb29c | 724 | (comint-read-input-ring t) |
070ccca4 LL |
725 | (setq-local comint-dynamic-complete-functions |
726 | inferior-octave-dynamic-complete-functions) | |
5147fc17 | 727 | (setq-local comint-prompt-read-only inferior-octave-prompt-read-only) |
be64c05d | 728 | (add-hook 'comint-input-filter-functions |
203a5572 | 729 | 'inferior-octave-directory-tracker nil t) |
c75c93c7 LL |
730 | ;; http://thread.gmane.org/gmane.comp.gnu.octave.general/48572 |
731 | (add-hook 'window-configuration-change-hook | |
732 | 'inferior-octave-track-window-width-change nil t) | |
1dab32a3 LL |
733 | (setq-local compilation-error-regexp-alist inferior-octave-error-regexp-alist) |
734 | (setq-local compilation-mode-font-lock-keywords | |
735 | inferior-octave-compilation-font-lock-keywords) | |
939fb29c LL |
736 | (compilation-shell-minor-mode 1) |
737 | (compilation-forget-errors)) | |
be64c05d LL |
738 | |
739 | ;;;###autoload | |
740 | (defun inferior-octave (&optional arg) | |
741 | "Run an inferior Octave process, I/O via `inferior-octave-buffer'. | |
742 | This buffer is put in Inferior Octave mode. See `inferior-octave-mode'. | |
743 | ||
744 | Unless ARG is non-nil, switches to this buffer. | |
745 | ||
746 | The elements of the list `inferior-octave-startup-args' are sent as | |
747 | command line arguments to the inferior Octave process on startup. | |
748 | ||
749 | Additional commands to be executed on startup can be provided either in | |
750 | the file specified by `inferior-octave-startup-file' or by the default | |
751 | startup file, `~/.emacs-octave'." | |
752 | (interactive "P") | |
0d634d3a | 753 | (let ((buffer (get-buffer-create inferior-octave-buffer))) |
ee44b62a LL |
754 | (unless arg |
755 | (pop-to-buffer buffer)) | |
0d634d3a | 756 | (unless (comint-check-proc buffer) |
be64c05d | 757 | (with-current-buffer buffer |
0d634d3a LL |
758 | (inferior-octave-startup) |
759 | (inferior-octave-mode))) | |
0d634d3a | 760 | buffer)) |
be64c05d LL |
761 | |
762 | ;;;###autoload | |
763 | (defalias 'run-octave 'inferior-octave) | |
764 | ||
765 | (defun inferior-octave-startup () | |
766 | "Start an inferior Octave process." | |
767 | (let ((proc (comint-exec-1 | |
0d634d3a LL |
768 | (substring inferior-octave-buffer 1 -1) |
769 | inferior-octave-buffer | |
770 | inferior-octave-program | |
daabf15a LL |
771 | (append |
772 | inferior-octave-startup-args | |
773 | ;; --no-gui is introduced in Octave > 3.7 | |
774 | (and (not (member "--no-gui" inferior-octave-startup-args)) | |
775 | (zerop (process-file inferior-octave-program | |
776 | nil nil nil "--no-gui" "--help")) | |
777 | '("--no-gui")))))) | |
be64c05d | 778 | (set-process-filter proc 'inferior-octave-output-digest) |
0d634d3a LL |
779 | (setq inferior-octave-process proc |
780 | inferior-octave-output-list nil | |
781 | inferior-octave-output-string nil | |
782 | inferior-octave-receive-in-progress t) | |
be64c05d LL |
783 | |
784 | ;; This may look complicated ... However, we need to make sure that | |
785 | ;; we additional startup code only AFTER Octave is ready (otherwise, | |
786 | ;; output may be mixed up). Hence, we need to digest the Octave | |
787 | ;; output to see when it issues a prompt. | |
788 | (while inferior-octave-receive-in-progress | |
965bb23a LL |
789 | (unless (inferior-octave-process-live-p) |
790 | ;; Spit out the error messages. | |
791 | (when inferior-octave-output-list | |
792 | (princ (concat (mapconcat 'identity inferior-octave-output-list "\n") | |
793 | "\n") | |
794 | (process-mark inferior-octave-process))) | |
795 | (error "Process `%s' died" inferior-octave-process)) | |
be64c05d LL |
796 | (accept-process-output inferior-octave-process)) |
797 | (goto-char (point-max)) | |
798 | (set-marker (process-mark proc) (point)) | |
799 | (insert-before-markers | |
800 | (concat | |
801 | (if (not (bobp)) "\f\n") | |
802 | (if inferior-octave-output-list | |
0d634d3a LL |
803 | (concat (mapconcat |
804 | 'identity inferior-octave-output-list "\n") | |
805 | "\n")))) | |
be64c05d | 806 | |
be64c05d LL |
807 | ;; An empty secondary prompt, as e.g. obtained by '--braindead', |
808 | ;; means trouble. | |
809 | (inferior-octave-send-list-and-digest (list "PS2\n")) | |
0d634d3a LL |
810 | (when (string-match "\\(PS2\\|ans\\) = *$" |
811 | (car inferior-octave-output-list)) | |
daabf15a | 812 | (inferior-octave-send-list-and-digest (list "PS2 ('> ');\n"))) |
0d634d3a | 813 | |
30ea8374 | 814 | (inferior-octave-send-list-and-digest |
daabf15a | 815 | (list "disp (getenv ('OCTAVE_SRCDIR'))\n")) |
5e80b74f LL |
816 | (process-put proc 'octave-srcdir |
817 | (unless (equal (car inferior-octave-output-list) "") | |
818 | (car inferior-octave-output-list))) | |
30ea8374 | 819 | |
0d634d3a LL |
820 | ;; O.K., now we are ready for the Inferior Octave startup commands. |
821 | (inferior-octave-send-list-and-digest | |
822 | (list "more off;\n" | |
823 | (unless (equal inferior-octave-output-string ">> ") | |
cbb3eb52 | 824 | "PS1 ('\\s> ');\n") |
0d634d3a LL |
825 | (when (and inferior-octave-startup-file |
826 | (file-exists-p inferior-octave-startup-file)) | |
daabf15a | 827 | (format "source ('%s');\n" inferior-octave-startup-file)))) |
85d090a9 LL |
828 | (when inferior-octave-output-list |
829 | (insert-before-markers | |
830 | (mapconcat 'identity inferior-octave-output-list "\n"))) | |
be64c05d LL |
831 | |
832 | ;; And finally, everything is back to normal. | |
c129b51f | 833 | (set-process-filter proc 'comint-output-filter) |
daabf15a LL |
834 | ;; Just in case, to be sure a cd in the startup file won't have |
835 | ;; detrimental effects. | |
836 | (with-demoted-errors (inferior-octave-resync-dirs)) | |
85d090a9 LL |
837 | ;; Generate a proper prompt, which is critical to |
838 | ;; `comint-history-isearch-backward-regexp'. Bug#14433. | |
455851dd | 839 | (comint-send-string proc "\n"))) |
be64c05d | 840 | |
f71c50d0 LL |
841 | (defvar inferior-octave-completion-table |
842 | ;; | |
30ea8374 | 843 | ;; Use cache to avoid repetitive computation of completions due to |
f71c50d0 | 844 | ;; bug#11906 - http://debbugs.gnu.org/11906 - which may cause |
4d2e94d1 | 845 | ;; noticeable delay. CACHE: (CMD . VALUE). |
f71c50d0 LL |
846 | (let ((cache)) |
847 | (completion-table-dynamic | |
848 | (lambda (command) | |
4d2e94d1 | 849 | (unless (equal (car cache) command) |
f71c50d0 | 850 | (inferior-octave-send-list-and-digest |
daabf15a | 851 | (list (format "completion_matches ('%s');\n" command))) |
4d2e94d1 | 852 | (setq cache (cons command |
c7a8fcac LL |
853 | (delete-consecutive-dups |
854 | (sort inferior-octave-output-list 'string-lessp))))) | |
4d2e94d1 | 855 | (cdr cache))))) |
584ea277 | 856 | |
be64c05d LL |
857 | (defun inferior-octave-completion-at-point () |
858 | "Return the data to complete the Octave symbol at point." | |
455851dd | 859 | ;; http://debbugs.gnu.org/14300 |
4d2e94d1 LL |
860 | (unless (string-match-p "/" (or (comint--match-partial-filename) "")) |
861 | (let ((beg (save-excursion | |
862 | (skip-syntax-backward "w_" (comint-line-beginning-position)) | |
863 | (point))) | |
864 | (end (point))) | |
865 | (when (and beg (> end beg)) | |
866 | (list beg end (completion-table-in-turn | |
f71c50d0 | 867 | inferior-octave-completion-table |
4d2e94d1 | 868 | 'comint-completion-file-name-table)))))) |
be64c05d LL |
869 | |
870 | (define-obsolete-function-alias 'inferior-octave-complete | |
871 | 'completion-at-point "24.1") | |
872 | ||
873 | (defun inferior-octave-dynamic-list-input-ring () | |
874 | "List the buffer's input history in a help buffer." | |
875 | ;; We cannot use `comint-dynamic-list-input-ring', because it replaces | |
876 | ;; "completion" by "history reference" ... | |
877 | (interactive) | |
878 | (if (or (not (ring-p comint-input-ring)) | |
879 | (ring-empty-p comint-input-ring)) | |
880 | (message "No history") | |
881 | (let ((history nil) | |
882 | (history-buffer " *Input History*") | |
883 | (index (1- (ring-length comint-input-ring))) | |
884 | (conf (current-window-configuration))) | |
885 | ;; We have to build up a list ourselves from the ring vector. | |
886 | (while (>= index 0) | |
887 | (setq history (cons (ring-ref comint-input-ring index) history) | |
888 | index (1- index))) | |
889 | ;; Change "completion" to "history reference" | |
890 | ;; to make the display accurate. | |
891 | (with-output-to-temp-buffer history-buffer | |
892 | (display-completion-list history) | |
893 | (set-buffer history-buffer)) | |
894 | (message "Hit space to flush") | |
895 | (let ((ch (read-event))) | |
896 | (if (eq ch ?\ ) | |
897 | (set-window-configuration conf) | |
898 | (setq unread-command-events (list ch))))))) | |
899 | ||
be64c05d LL |
900 | (defun inferior-octave-output-digest (_proc string) |
901 | "Special output filter for the inferior Octave process. | |
902 | Save all output between newlines into `inferior-octave-output-list', and | |
903 | the rest to `inferior-octave-output-string'." | |
904 | (setq string (concat inferior-octave-output-string string)) | |
905 | (while (string-match "\n" string) | |
906 | (setq inferior-octave-output-list | |
907 | (append inferior-octave-output-list | |
908 | (list (substring string 0 (match-beginning 0)))) | |
909 | string (substring string (match-end 0)))) | |
910 | (if (string-match inferior-octave-prompt string) | |
911 | (setq inferior-octave-receive-in-progress nil)) | |
912 | (setq inferior-octave-output-string string)) | |
913 | ||
30ea8374 | 914 | (defun inferior-octave-check-process () |
724bc265 | 915 | (or (inferior-octave-process-live-p) |
30ea8374 LL |
916 | (error (substitute-command-keys |
917 | "No inferior octave process running. Type \\[run-octave]")))) | |
918 | ||
be64c05d LL |
919 | (defun inferior-octave-send-list-and-digest (list) |
920 | "Send LIST to the inferior Octave process and digest the output. | |
921 | The elements of LIST have to be strings and are sent one by one. All | |
922 | output is passed to the filter `inferior-octave-output-digest'." | |
30ea8374 | 923 | (inferior-octave-check-process) |
be64c05d LL |
924 | (let* ((proc inferior-octave-process) |
925 | (filter (process-filter proc)) | |
926 | string) | |
927 | (set-process-filter proc 'inferior-octave-output-digest) | |
928 | (setq inferior-octave-output-list nil) | |
929 | (unwind-protect | |
930 | (while (setq string (car list)) | |
931 | (setq inferior-octave-output-string nil | |
932 | inferior-octave-receive-in-progress t) | |
933 | (comint-send-string proc string) | |
934 | (while inferior-octave-receive-in-progress | |
935 | (accept-process-output proc)) | |
936 | (setq list (cdr list))) | |
937 | (set-process-filter proc filter)))) | |
938 | ||
837fd9af LL |
939 | (defvar inferior-octave-directory-tracker-resync nil) |
940 | (make-variable-buffer-local 'inferior-octave-directory-tracker-resync) | |
941 | ||
be64c05d LL |
942 | (defun inferior-octave-directory-tracker (string) |
943 | "Tracks `cd' commands issued to the inferior Octave process. | |
944 | Use \\[inferior-octave-resync-dirs] to resync if Emacs gets confused." | |
837fd9af | 945 | (when inferior-octave-directory-tracker-resync |
daabf15a LL |
946 | (or (inferior-octave-resync-dirs 'noerror) |
947 | (setq inferior-octave-directory-tracker-resync nil))) | |
be64c05d LL |
948 | (cond |
949 | ((string-match "^[ \t]*cd[ \t;]*$" string) | |
950 | (cd "~")) | |
951 | ((string-match "^[ \t]*cd[ \t]+\\([^ \t\n;]*\\)[ \t\n;]*" string) | |
837fd9af LL |
952 | (condition-case err |
953 | (cd (match-string 1 string)) | |
954 | (error (setq inferior-octave-directory-tracker-resync t) | |
955 | (message "%s: `%s'" | |
956 | (error-message-string err) | |
957 | (match-string 1 string))))))) | |
be64c05d | 958 | |
daabf15a | 959 | (defun inferior-octave-resync-dirs (&optional noerror) |
be64c05d LL |
960 | "Resync the buffer's idea of the current directory. |
961 | This command queries the inferior Octave process about its current | |
962 | directory and makes this the current buffer's default directory." | |
963 | (interactive) | |
964 | (inferior-octave-send-list-and-digest '("disp (pwd ())\n")) | |
daabf15a LL |
965 | (condition-case err |
966 | (progn | |
967 | (cd (car inferior-octave-output-list)) | |
968 | t) | |
969 | (error (unless noerror (signal (car err) (cdr err)))))) | |
be64c05d | 970 | |
c75c93c7 LL |
971 | (defcustom inferior-octave-minimal-columns 80 |
972 | "The minimal column width for the inferior Octave process." | |
973 | :type 'integer | |
974 | :group 'octave | |
975 | :version "24.4") | |
976 | ||
977 | (defvar inferior-octave-last-column-width nil) | |
978 | ||
979 | (defun inferior-octave-track-window-width-change () | |
980 | ;; http://thread.gmane.org/gmane.comp.gnu.octave.general/48572 | |
981 | (let ((width (max inferior-octave-minimal-columns (window-width)))) | |
982 | (unless (eq inferior-octave-last-column-width width) | |
983 | (setq-local inferior-octave-last-column-width width) | |
724bc265 | 984 | (when (inferior-octave-process-live-p) |
c75c93c7 | 985 | (inferior-octave-send-list-and-digest |
daabf15a | 986 | (list (format "putenv ('COLUMNS', '%s');\n" width))))))) |
c75c93c7 | 987 | |
f2727dfb RS |
988 | \f |
989 | ;;; Miscellaneous useful functions | |
f2727dfb | 990 | |
070ccca4 LL |
991 | (defun octave-in-comment-p () |
992 | "Return non-nil if point is inside an Octave comment." | |
602ea69d | 993 | (nth 4 (syntax-ppss))) |
f2727dfb | 994 | |
070ccca4 LL |
995 | (defun octave-in-string-p () |
996 | "Return non-nil if point is inside an Octave string." | |
602ea69d | 997 | (nth 3 (syntax-ppss))) |
f2727dfb | 998 | |
070ccca4 LL |
999 | (defun octave-in-string-or-comment-p () |
1000 | "Return non-nil if point is inside an Octave string or comment." | |
1001 | (nth 8 (syntax-ppss))) | |
f2727dfb | 1002 | |
a584f30f | 1003 | (defun octave-looking-at-kw (regexp) |
050a4b35 | 1004 | "Like `looking-at', but sets `case-fold-search' nil." |
a584f30f GM |
1005 | (let ((case-fold-search nil)) |
1006 | (looking-at regexp))) | |
1007 | ||
d83ee578 KH |
1008 | (defun octave-maybe-insert-continuation-string () |
1009 | (if (or (octave-in-comment-p) | |
1010 | (save-excursion | |
1011 | (beginning-of-line) | |
1012 | (looking-at octave-continuation-regexp))) | |
1013 | nil | |
1014 | (delete-horizontal-space) | |
1015 | (insert (concat " " octave-continuation-string)))) | |
e55d3b04 | 1016 | |
d4d0f9b3 LL |
1017 | (defun octave-completing-read () |
1018 | (let ((def (or (thing-at-point 'symbol) | |
1019 | (save-excursion | |
1020 | (skip-syntax-backward "-(") | |
1021 | (thing-at-point 'symbol))))) | |
1022 | (completing-read | |
1023 | (format (if def "Function (default %s): " | |
1024 | "Function: ") def) | |
f71c50d0 | 1025 | inferior-octave-completion-table |
d4d0f9b3 LL |
1026 | nil nil nil nil def))) |
1027 | ||
e3772e98 LL |
1028 | (defun octave-goto-function-definition (fn) |
1029 | "Go to the function definition of FN in current buffer." | |
e3772e98 LL |
1030 | (let ((search |
1031 | (lambda (re sub) | |
fda74125 LL |
1032 | (let ((orig (point)) found) |
1033 | (goto-char (point-min)) | |
1034 | (while (and (not found) (re-search-forward re nil t)) | |
e3772e98 LL |
1035 | (when (and (equal (match-string sub) fn) |
1036 | (not (nth 8 (syntax-ppss)))) | |
fda74125 LL |
1037 | (setq found t))) |
1038 | (unless found (goto-char orig)) | |
1039 | found)))) | |
daabf15a | 1040 | (pcase (and buffer-file-name (file-name-extension buffer-file-name)) |
e3772e98 | 1041 | (`"cc" (funcall search |
9cc3e83f | 1042 | "\\_<DEFUN\\(?:_DLD\\)?\\s-*(\\s-*\\(\\(?:\\sw\\|\\s_\\)+\\)" 1)) |
e3772e98 | 1043 | (t (funcall search octave-function-header-regexp 3))))) |
d4d0f9b3 | 1044 | |
b7260dd4 LL |
1045 | (defun octave-function-file-p () |
1046 | "Return non-nil if the first token is \"function\". | |
1047 | The value is (START END NAME-START NAME-END) of the function." | |
1048 | (save-excursion | |
1049 | (goto-char (point-min)) | |
1050 | (when (equal (funcall smie-forward-token-function) "function") | |
1051 | (forward-word -1) | |
1052 | (let* ((start (point)) | |
1053 | (end (progn (forward-sexp 1) (point))) | |
1054 | (name (when (progn | |
1055 | (goto-char start) | |
1056 | (re-search-forward octave-function-header-regexp | |
1057 | end t)) | |
1058 | (list (match-beginning 3) (match-end 3))))) | |
1059 | (cons start (cons end name)))))) | |
1060 | ||
1061 | ;; Like forward-comment but stop at non-comment blank | |
1062 | (defun octave-skip-comment-forward (limit) | |
1063 | (let ((ppss (syntax-ppss))) | |
1064 | (if (nth 4 ppss) | |
1065 | (goto-char (nth 8 ppss)) | |
1066 | (goto-char (or (comment-search-forward limit t) (point))))) | |
1067 | (while (and (< (point) limit) (looking-at-p "\\s<")) | |
1068 | (forward-comment 1))) | |
1069 | ||
1070 | ;;; First non-copyright comment block | |
1071 | (defun octave-function-file-comment () | |
2ec12cb0 | 1072 | "Beginning and end positions of the function file comment." |
b7260dd4 LL |
1073 | (save-excursion |
1074 | (goto-char (point-min)) | |
38d8527b LL |
1075 | ;; Copyright block: octave/libinterp/parse-tree/lex.ll around line 1634 |
1076 | (while (save-excursion | |
1077 | (when (comment-search-forward (point-max) t) | |
1078 | (when (eq (char-after) ?\{) ; case of block comment | |
1079 | (forward-char 1)) | |
1080 | (skip-syntax-forward "-") | |
1081 | (let ((case-fold-search t)) | |
1082 | (looking-at-p "\\(?:copyright\\|author\\)\\_>")))) | |
1083 | (octave-skip-comment-forward (point-max))) | |
1084 | (let ((beg (comment-search-forward (point-max) t))) | |
1085 | (when beg | |
1086 | (goto-char beg) | |
1087 | (octave-skip-comment-forward (point-max)) | |
1088 | (list beg (point)))))) | |
b7260dd4 | 1089 | |
e55d3b04 LL |
1090 | (defun octave-sync-function-file-names () |
1091 | "Ensure function name agree with function file name. | |
1092 | See Info node `(octave)Function Files'." | |
1093 | (interactive) | |
b7260dd4 LL |
1094 | (when buffer-file-name |
1095 | (pcase-let ((`(,start ,_end ,name-start ,name-end) | |
1096 | (octave-function-file-p))) | |
1097 | (when (and start name-start) | |
2ec12cb0 LL |
1098 | (let* ((func (buffer-substring name-start name-end)) |
1099 | (file (file-name-sans-extension | |
1100 | (file-name-nondirectory buffer-file-name))) | |
1101 | (help-form (format "\ | |
1102 | a: Use function name `%s' | |
1103 | b: Use file name `%s' | |
1104 | q: Don't fix\n" func file)) | |
1105 | (c (unless (equal file func) | |
1106 | (save-window-excursion | |
1107 | (help-form-show) | |
1108 | (read-char-choice | |
1109 | "Which name to use? (a/b/q) " '(?a ?b ?q)))))) | |
1110 | (pcase c | |
1111 | (`?a (let ((newname (expand-file-name | |
1112 | (concat func (file-name-extension | |
1113 | buffer-file-name t))))) | |
1114 | (when (or (not (file-exists-p newname)) | |
1115 | (yes-or-no-p | |
1116 | (format "Target file %s exists; proceed? " newname))) | |
1117 | (when (file-exists-p buffer-file-name) | |
1118 | (rename-file buffer-file-name newname t)) | |
1119 | (set-visited-file-name newname)))) | |
1120 | (`?b (save-excursion | |
1121 | (goto-char name-start) | |
1122 | (delete-region name-start name-end) | |
1123 | (insert file))))))))) | |
b7260dd4 LL |
1124 | |
1125 | (defun octave-update-function-file-comment (beg end) | |
1126 | "Query replace function names in function file comment." | |
1127 | (interactive | |
1128 | (progn | |
1129 | (barf-if-buffer-read-only) | |
1130 | (if (use-region-p) | |
1131 | (list (region-beginning) (region-end)) | |
1132 | (or (octave-function-file-comment) | |
1133 | (error "No function file comment found"))))) | |
e55d3b04 | 1134 | (save-excursion |
b7260dd4 LL |
1135 | (let* ((bounds (or (octave-function-file-p) |
1136 | (error "Not in a function file buffer"))) | |
1137 | (func (if (cddr bounds) | |
1138 | (apply #'buffer-substring (cddr bounds)) | |
1139 | (error "Function name not found"))) | |
1140 | (old-func (progn | |
1141 | (goto-char beg) | |
38d8527b LL |
1142 | (when (re-search-forward |
1143 | "[=}]\\s-*\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>" | |
1144 | (min (line-end-position 4) end) | |
1145 | t) | |
b7260dd4 LL |
1146 | (match-string 1)))) |
1147 | (old-func (read-string (format (if old-func | |
1148 | "Name to replace (default %s): " | |
1149 | "Name to replace: ") | |
1150 | old-func) | |
1151 | nil nil old-func))) | |
1152 | (if (and func old-func (not (equal func old-func))) | |
1153 | (perform-replace old-func func 'query | |
1154 | nil 'delimited nil nil beg end) | |
1155 | (message "Function names match"))))) | |
e55d3b04 | 1156 | |
5b78d7fc LL |
1157 | (defface octave-function-comment-block |
1158 | '((t (:inherit font-lock-doc-face))) | |
1159 | "Face used to highlight function comment block." | |
1160 | :group 'octave) | |
1161 | ||
1e2c18df SM |
1162 | (eval-when-compile (require 'texinfo)) |
1163 | ||
5b78d7fc | 1164 | (defun octave-font-lock-texinfo-comment () |
1e2c18df SM |
1165 | (let ((kws |
1166 | (eval-when-compile | |
1167 | (delq nil (mapcar | |
1168 | (lambda (kw) | |
1169 | (if (numberp (nth 1 kw)) | |
1170 | `(,(nth 0 kw) ,(nth 1 kw) ,(nth 2 kw) prepend) | |
1171 | (message "Ignoring Texinfo highlight: %S" kw))) | |
1172 | texinfo-font-lock-keywords))))) | |
1173 | (font-lock-add-keywords | |
1174 | nil | |
1175 | `((,(lambda (limit) | |
6073d8f4 LL |
1176 | (while (and (< (point) limit) |
1177 | (search-forward "-*- texinfo -*-" limit t) | |
1e2c18df SM |
1178 | (octave-in-comment-p)) |
1179 | (let ((beg (nth 8 (syntax-ppss))) | |
1180 | (end (progn | |
1181 | (octave-skip-comment-forward (point-max)) | |
1182 | (point)))) | |
1183 | (put-text-property beg end 'font-lock-multiline t) | |
1184 | (font-lock-prepend-text-property | |
1185 | beg end 'face 'octave-function-comment-block) | |
1186 | (dolist (kw kws) | |
1187 | (goto-char beg) | |
1188 | (while (re-search-forward (car kw) end 'move) | |
1189 | (font-lock-apply-highlight (cdr kw)))))) | |
1190 | nil))) | |
1191 | 'append))) | |
5b78d7fc | 1192 | |
f2727dfb RS |
1193 | \f |
1194 | ;;; Indentation | |
f2727dfb | 1195 | |
9cc3e83f | 1196 | (defun octave-indent-new-comment-line (&optional soft) |
f2727dfb | 1197 | "Break Octave line at point, continuing comment if within one. |
9cc3e83f LL |
1198 | Insert `octave-continuation-string' before breaking the line |
1199 | unless inside a list. Signal an error if within a single-quoted | |
1200 | string." | |
f2727dfb | 1201 | (interactive) |
650cff3d SM |
1202 | (funcall comment-line-break-function soft)) |
1203 | ||
1204 | (defun octave--indent-new-comment-line (orig &rest args) | |
f2727dfb | 1205 | (cond |
9cc3e83f LL |
1206 | ((octave-in-comment-p) nil) |
1207 | ((eq (octave-in-string-p) ?') | |
1208 | (error "Cannot split a single-quoted string")) | |
1209 | ((eq (octave-in-string-p) ?\") | |
1210 | (insert octave-continuation-string)) | |
f2727dfb | 1211 | (t |
9cc3e83f LL |
1212 | (delete-horizontal-space) |
1213 | (unless (and (cadr (syntax-ppss)) | |
1214 | (eq (char-after (cadr (syntax-ppss))) ?\()) | |
1215 | (insert " " octave-continuation-string)))) | |
650cff3d | 1216 | (apply orig args) |
9cc3e83f | 1217 | (indent-according-to-mode)) |
f2727dfb | 1218 | |
2d4bf34b LL |
1219 | (define-obsolete-function-alias |
1220 | 'octave-indent-defun 'prog-indent-sexp "24.4") | |
f2727dfb RS |
1221 | |
1222 | \f | |
1223 | ;;; Motion | |
1224 | (defun octave-next-code-line (&optional arg) | |
1225 | "Move ARG lines of Octave code forward (backward if ARG is negative). | |
1226 | Skips past all empty and comment lines. Default for ARG is 1. | |
1227 | ||
1228 | On success, return 0. Otherwise, go as far as possible and return -1." | |
1229 | (interactive "p") | |
1230 | (or arg (setq arg 1)) | |
1231 | (beginning-of-line) | |
1232 | (let ((n 0) | |
1233 | (inc (if (> arg 0) 1 -1))) | |
1234 | (while (and (/= arg 0) (= n 0)) | |
1235 | (setq n (forward-line inc)) | |
1236 | (while (and (= n 0) | |
1237 | (looking-at "\\s-*\\($\\|\\s<\\)")) | |
1238 | (setq n (forward-line inc))) | |
1239 | (setq arg (- arg inc))) | |
1240 | n)) | |
a1506d29 | 1241 | |
f2727dfb RS |
1242 | (defun octave-previous-code-line (&optional arg) |
1243 | "Move ARG lines of Octave code backward (forward if ARG is negative). | |
1244 | Skips past all empty and comment lines. Default for ARG is 1. | |
1245 | ||
1246 | On success, return 0. Otherwise, go as far as possible and return -1." | |
1247 | (interactive "p") | |
1248 | (or arg (setq arg 1)) | |
1249 | (octave-next-code-line (- arg))) | |
1250 | ||
1251 | (defun octave-beginning-of-line () | |
1252 | "Move point to beginning of current Octave line. | |
1253 | If on an empty or comment line, go to the beginning of that line. | |
1254 | Otherwise, move backward to the beginning of the first Octave code line | |
1255 | which is not inside a continuation statement, i.e., which does not | |
083fe0d7 | 1256 | follow a code line ending with `...' or is inside an open |
f2727dfb RS |
1257 | parenthesis list." |
1258 | (interactive) | |
1259 | (beginning-of-line) | |
083fe0d7 LL |
1260 | (unless (looking-at "\\s-*\\($\\|\\s<\\)") |
1261 | (while (or (when (cadr (syntax-ppss)) | |
1262 | (goto-char (cadr (syntax-ppss))) | |
1263 | (beginning-of-line) | |
1264 | t) | |
1265 | (and (or (looking-at "\\s-*\\($\\|\\s<\\)") | |
1266 | (save-excursion | |
1267 | (if (zerop (octave-previous-code-line)) | |
1268 | (looking-at octave-continuation-regexp)))) | |
1269 | (zerop (forward-line -1))))))) | |
f2727dfb RS |
1270 | |
1271 | (defun octave-end-of-line () | |
1272 | "Move point to end of current Octave line. | |
1273 | If on an empty or comment line, go to the end of that line. | |
1274 | Otherwise, move forward to the end of the first Octave code line which | |
083fe0d7 | 1275 | does not end with `...' or is inside an open parenthesis list." |
f2727dfb RS |
1276 | (interactive) |
1277 | (end-of-line) | |
083fe0d7 LL |
1278 | (unless (save-excursion |
1279 | (beginning-of-line) | |
1280 | (looking-at "\\s-*\\($\\|\\s<\\)")) | |
1281 | (while (or (when (cadr (syntax-ppss)) | |
1282 | (condition-case nil | |
1283 | (progn | |
1284 | (up-list 1) | |
1285 | (end-of-line) | |
1286 | t) | |
1287 | (error nil))) | |
1288 | (and (save-excursion | |
1289 | (beginning-of-line) | |
1290 | (or (looking-at "\\s-*\\($\\|\\s<\\)") | |
1291 | (looking-at octave-continuation-regexp))) | |
1292 | (zerop (forward-line 1))))) | |
f2727dfb | 1293 | (end-of-line))) |
a1506d29 | 1294 | |
f2727dfb RS |
1295 | (defun octave-mark-block () |
1296 | "Put point at the beginning of this Octave block, mark at the end. | |
1297 | The block marked is the one that contains point or follows point." | |
1298 | (interactive) | |
ef54d315 SM |
1299 | (if (and (looking-at "\\sw\\|\\s_") |
1300 | (looking-back "\\sw\\|\\s_" (1- (point)))) | |
1301 | (skip-syntax-forward "w_")) | |
ec5d3ff7 SM |
1302 | (unless (or (looking-at "\\s(") |
1303 | (save-excursion | |
1304 | (let* ((token (funcall smie-forward-token-function)) | |
6d2a1e35 | 1305 | (level (assoc token smie-grammar))) |
ef54d315 | 1306 | (and level (not (numberp (cadr level))))))) |
ec5d3ff7 SM |
1307 | (backward-up-list 1)) |
1308 | (mark-sexp)) | |
f2727dfb | 1309 | |
f2727dfb | 1310 | (defun octave-beginning-of-defun (&optional arg) |
f71c50d0 LL |
1311 | "Octave-specific `beginning-of-defun-function' (which see)." |
1312 | (or arg (setq arg 1)) | |
1313 | ;; Move out of strings or comments. | |
1314 | (when (octave-in-string-or-comment-p) | |
1315 | (goto-char (octave-in-string-or-comment-p))) | |
1316 | (letrec ((orig (point)) | |
1317 | (toplevel (lambda (pos) | |
1318 | (condition-case nil | |
1319 | (progn | |
1320 | (backward-up-list 1) | |
1321 | (funcall toplevel (point))) | |
1322 | (scan-error pos))))) | |
1323 | (goto-char (funcall toplevel (point))) | |
1324 | (when (and (> arg 0) (/= orig (point))) | |
1325 | (setq arg (1- arg))) | |
1326 | (forward-sexp (- arg)) | |
9cc3e83f | 1327 | (and (< arg 0) (forward-sexp -1)) |
f71c50d0 | 1328 | (/= orig (point)))) |
f2727dfb | 1329 | |
e02f48d7 | 1330 | (defun octave-fill-paragraph (&optional _arg) |
ec5d3ff7 SM |
1331 | "Fill paragraph of Octave code, handling Octave comments." |
1332 | ;; FIXME: difference with generic fill-paragraph: | |
1333 | ;; - code lines are only split, never joined. | |
1334 | ;; - \n that end comments are never removed. | |
1335 | ;; - insert continuation marker when splitting code lines. | |
1336 | (interactive "P") | |
1337 | (save-excursion | |
1338 | (let ((end (progn (forward-paragraph) (copy-marker (point) t))) | |
1339 | (beg (progn | |
1340 | (forward-paragraph -1) | |
1341 | (skip-chars-forward " \t\n") | |
1342 | (beginning-of-line) | |
1343 | (point))) | |
1344 | (cfc (current-fill-column)) | |
1345 | comment-prefix) | |
1346 | (goto-char beg) | |
1347 | (while (< (point) end) | |
1348 | (condition-case nil | |
1349 | (indent-according-to-mode) | |
1350 | (error nil)) | |
1351 | (move-to-column cfc) | |
1352 | ;; First check whether we need to combine non-empty comment lines | |
1353 | (if (and (< (current-column) cfc) | |
1354 | (octave-in-comment-p) | |
1355 | (not (save-excursion | |
1356 | (beginning-of-line) | |
1357 | (looking-at "^\\s-*\\s<+\\s-*$")))) | |
1358 | ;; This is a nonempty comment line which does not extend | |
1359 | ;; past the fill column. If it is followed by a nonempty | |
1360 | ;; comment line with the same comment prefix, try to | |
1361 | ;; combine them, and repeat this until either we reach the | |
1362 | ;; fill-column or there is nothing more to combine. | |
1363 | (progn | |
1364 | ;; Get the comment prefix | |
1365 | (save-excursion | |
1366 | (beginning-of-line) | |
1367 | (while (and (re-search-forward "\\s<+") | |
1368 | (not (octave-in-comment-p)))) | |
1369 | (setq comment-prefix (match-string 0))) | |
1370 | ;; And keep combining ... | |
1371 | (while (and (< (current-column) cfc) | |
1372 | (save-excursion | |
1373 | (forward-line 1) | |
1374 | (and (looking-at | |
1375 | (concat "^\\s-*" | |
1376 | comment-prefix | |
1377 | "\\S<")) | |
1378 | (not (looking-at | |
1379 | (concat "^\\s-*" | |
1380 | comment-prefix | |
1381 | "\\s-*$")))))) | |
1382 | (delete-char 1) | |
1383 | (re-search-forward comment-prefix) | |
1384 | (delete-region (match-beginning 0) (match-end 0)) | |
1385 | (fixup-whitespace) | |
1386 | (move-to-column cfc)))) | |
1387 | ;; We might also try to combine continued code lines> Perhaps | |
1388 | ;; some other time ... | |
1389 | (skip-chars-forward "^ \t\n") | |
1390 | (delete-horizontal-space) | |
1391 | (if (or (< (current-column) cfc) | |
1392 | (and (= (current-column) cfc) (eolp))) | |
1393 | (forward-line 1) | |
1394 | (if (not (eolp)) (insert " ")) | |
ee44b62a | 1395 | (or (funcall normal-auto-fill-function) |
ec5d3ff7 SM |
1396 | (forward-line 1)))) |
1397 | t))) | |
f2727dfb | 1398 | |
203a5572 | 1399 | (defun octave-completion-at-point () |
c82d5b11 | 1400 | "Find the text to complete and the corresponding table." |
e92f3bd3 | 1401 | (let* ((beg (save-excursion (skip-syntax-backward "w_") (point))) |
ff80a446 SM |
1402 | (end (point))) |
1403 | (if (< beg (point)) | |
1404 | ;; Extend region past point, if applicable. | |
e92f3bd3 SM |
1405 | (save-excursion (skip-syntax-forward "w_") |
1406 | (setq end (point)))) | |
203a5572 | 1407 | (when (> end beg) |
724bc265 | 1408 | (list beg end (or (and (inferior-octave-process-live-p) |
f71c50d0 | 1409 | inferior-octave-completion-table) |
203a5572 | 1410 | octave-reserved-words))))) |
f2727dfb | 1411 | |
bcd70d97 SM |
1412 | (define-obsolete-function-alias 'octave-complete-symbol |
1413 | 'completion-at-point "24.1") | |
fda74125 LL |
1414 | |
1415 | (defun octave-add-log-current-defun () | |
1416 | "A function for `add-log-current-defun-function' (which see)." | |
1417 | (save-excursion | |
1418 | (end-of-line) | |
1419 | (and (beginning-of-defun) | |
1420 | (re-search-forward octave-function-header-regexp | |
1421 | (line-end-position) t) | |
1422 | (match-string 3)))) | |
1423 | ||
f2727dfb RS |
1424 | \f |
1425 | ;;; Electric characters && friends | |
c82d5b11 | 1426 | (define-skeleton octave-insert-defun |
f2727dfb RS |
1427 | "Insert an Octave function skeleton. |
1428 | Prompt for the function's name, arguments and return values (to be | |
1429 | entered without parens)." | |
070ccca4 | 1430 | (let* ((defname (file-name-sans-extension (buffer-name))) |
c82d5b11 SM |
1431 | (name (read-string (format "Function name (default %s): " defname) |
1432 | nil nil defname)) | |
1433 | (args (read-string "Arguments: ")) | |
1434 | (vals (read-string "Return values: "))) | |
1435 | (format "%s%s (%s)" | |
1436 | (cond | |
1437 | ((string-equal vals "") vals) | |
1438 | ((string-match "[ ,]" vals) (concat "[" vals "] = ")) | |
1439 | (t (concat vals " = "))) | |
1440 | name | |
1441 | args)) | |
b7260dd4 LL |
1442 | \n octave-block-comment-start "usage: " str \n |
1443 | octave-block-comment-start '(delete-horizontal-space) \n | |
1444 | octave-block-comment-start '(delete-horizontal-space) \n | |
1445 | "function " > str \n | |
1446 | _ \n | |
c82d5b11 | 1447 | "endfunction" > \n) |
fda74125 | 1448 | |
f2727dfb RS |
1449 | ;;; Communication with the inferior Octave process |
1450 | (defun octave-kill-process () | |
1451 | "Kill inferior Octave process and its buffer." | |
1452 | (interactive) | |
2cc18f93 LL |
1453 | (when (and (buffer-live-p (get-buffer inferior-octave-buffer)) |
1454 | (or (yes-or-no-p (format "Kill %S and its buffer? " | |
1455 | inferior-octave-process)) | |
1456 | (user-error "Aborted"))) | |
1457 | (when (inferior-octave-process-live-p) | |
1458 | (set-process-query-on-exit-flag inferior-octave-process nil) | |
1459 | (process-send-string inferior-octave-process "quit;\n") | |
1460 | (accept-process-output inferior-octave-process)) | |
724bc265 | 1461 | (kill-buffer inferior-octave-buffer))) |
f2727dfb RS |
1462 | |
1463 | (defun octave-show-process-buffer () | |
1464 | "Make sure that `inferior-octave-buffer' is displayed." | |
1465 | (interactive) | |
1466 | (if (get-buffer inferior-octave-buffer) | |
1467 | (display-buffer inferior-octave-buffer) | |
1468 | (message "No buffer named %s" inferior-octave-buffer))) | |
1469 | ||
1470 | (defun octave-hide-process-buffer () | |
1471 | "Delete all windows that display `inferior-octave-buffer'." | |
1472 | (interactive) | |
1473 | (if (get-buffer inferior-octave-buffer) | |
1474 | (delete-windows-on inferior-octave-buffer) | |
1475 | (message "No buffer named %s" inferior-octave-buffer))) | |
1476 | ||
06e752b4 RS |
1477 | (defun octave-source-file (file) |
1478 | "Execute FILE in the inferior Octave process. | |
1479 | This is done using Octave's source function. FILE defaults to | |
1480 | current buffer file unless called with a prefix arg \\[universal-argument]." | |
1481 | (interactive (list (or (and (not current-prefix-arg) buffer-file-name) | |
1482 | (read-file-name "File: " nil nil t)))) | |
1483 | (or (stringp file) | |
1484 | (signal 'wrong-type-argument (list 'stringp file))) | |
1485 | (inferior-octave t) | |
1486 | (with-current-buffer inferior-octave-buffer | |
1487 | (comint-send-string inferior-octave-process | |
1488 | (format "source '%s'\n" file)))) | |
1489 | ||
f2727dfb RS |
1490 | (defun octave-send-region (beg end) |
1491 | "Send current region to the inferior Octave process." | |
1492 | (interactive "r") | |
a1506d29 | 1493 | (inferior-octave t) |
f2727dfb | 1494 | (let ((proc inferior-octave-process) |
c129b51f LL |
1495 | (string (buffer-substring-no-properties beg end)) |
1496 | line) | |
5d8137ab | 1497 | (with-current-buffer inferior-octave-buffer |
6f6ab820 LL |
1498 | ;; http://lists.gnu.org/archive/html/emacs-devel/2013-10/msg00095.html |
1499 | (compilation-forget-errors) | |
f2727dfb RS |
1500 | (setq inferior-octave-output-list nil) |
1501 | (while (not (string-equal string "")) | |
c129b51f LL |
1502 | (if (string-match "\n" string) |
1503 | (setq line (substring string 0 (match-beginning 0)) | |
1504 | string (substring string (match-end 0))) | |
1505 | (setq line string string "")) | |
1506 | (setq inferior-octave-receive-in-progress t) | |
1507 | (inferior-octave-send-list-and-digest (list (concat line "\n"))) | |
1508 | (while inferior-octave-receive-in-progress | |
1509 | (accept-process-output proc)) | |
1510 | (insert-before-markers | |
1511 | (mapconcat 'identity | |
1512 | (append | |
1513 | (if octave-send-echo-input (list line) (list "")) | |
1514 | inferior-octave-output-list | |
1515 | (list inferior-octave-output-string)) | |
1516 | "\n"))))) | |
f2727dfb RS |
1517 | (if octave-send-show-buffer |
1518 | (display-buffer inferior-octave-buffer))) | |
1519 | ||
7e138a62 RS |
1520 | (defun octave-send-buffer () |
1521 | "Send current buffer to the inferior Octave process." | |
1522 | (interactive) | |
1523 | (octave-send-region (point-min) (point-max))) | |
1524 | ||
f2727dfb | 1525 | (defun octave-send-block () |
a1506d29 | 1526 | "Send current Octave block to the inferior Octave process." |
f2727dfb RS |
1527 | (interactive) |
1528 | (save-excursion | |
1529 | (octave-mark-block) | |
1530 | (octave-send-region (point) (mark)))) | |
1531 | ||
1532 | (defun octave-send-defun () | |
1533 | "Send current Octave function to the inferior Octave process." | |
1534 | (interactive) | |
1535 | (save-excursion | |
ff80a446 | 1536 | (mark-defun) |
f2727dfb RS |
1537 | (octave-send-region (point) (mark)))) |
1538 | ||
1539 | (defun octave-send-line (&optional arg) | |
1540 | "Send current Octave code line to the inferior Octave process. | |
1541 | With positive prefix ARG, send that many lines. | |
1542 | If `octave-send-line-auto-forward' is non-nil, go to the next unsent | |
1543 | code line." | |
1544 | (interactive "P") | |
1545 | (or arg (setq arg 1)) | |
1546 | (if (> arg 0) | |
1547 | (let (beg end) | |
1548 | (beginning-of-line) | |
1549 | (setq beg (point)) | |
1550 | (octave-next-code-line (- arg 1)) | |
1551 | (end-of-line) | |
1552 | (setq end (point)) | |
1553 | (if octave-send-line-auto-forward | |
1554 | (octave-next-code-line 1)) | |
1555 | (octave-send-region beg end)))) | |
1556 | ||
1557 | (defun octave-eval-print-last-sexp () | |
1558 | "Evaluate Octave sexp before point and print value into current buffer." | |
1559 | (interactive) | |
1560 | (inferior-octave t) | |
1561 | (let ((standard-output (current-buffer)) | |
1562 | (print-escape-newlines nil) | |
1563 | (opoint (point))) | |
1564 | (terpri) | |
a1506d29 | 1565 | (prin1 |
f2727dfb RS |
1566 | (save-excursion |
1567 | (forward-sexp -1) | |
1568 | (inferior-octave-send-list-and-digest | |
1569 | (list (concat (buffer-substring-no-properties (point) opoint) | |
1570 | "\n"))) | |
1571 | (mapconcat 'identity inferior-octave-output-list "\n"))) | |
1572 | (terpri))) | |
070ccca4 | 1573 | |
d4d0f9b3 LL |
1574 | \f |
1575 | ||
c8730c3a LL |
1576 | (defcustom octave-eldoc-message-style 'auto |
1577 | "Octave eldoc message style: auto, oneline, multiline." | |
1578 | :type '(choice (const :tag "Automatic" auto) | |
1579 | (const :tag "One Line" oneline) | |
1580 | (const :tag "Multi Line" multiline)) | |
1581 | :group 'octave | |
1582 | :version "24.4") | |
1583 | ||
1584 | ;; (FN SIGNATURE1 SIGNATURE2 ...) | |
1585 | (defvar octave-eldoc-cache nil) | |
1586 | ||
1587 | (defun octave-eldoc-function-signatures (fn) | |
1588 | (unless (equal fn (car octave-eldoc-cache)) | |
1589 | (inferior-octave-send-list-and-digest | |
daabf15a | 1590 | (list (format "print_usage ('%s');\n" fn))) |
c8730c3a LL |
1591 | (let (result) |
1592 | (dolist (line inferior-octave-output-list) | |
1593 | (when (string-match | |
1594 | "\\s-*\\(?:--[^:]+\\|usage\\):\\s-*\\(.*\\)$" | |
1595 | line) | |
1596 | (push (match-string 1 line) result))) | |
1597 | (setq octave-eldoc-cache | |
1598 | (cons (substring-no-properties fn) | |
1599 | (nreverse result))))) | |
1600 | (cdr octave-eldoc-cache)) | |
1601 | ||
1602 | (defun octave-eldoc-function () | |
1603 | "A function for `eldoc-documentation-function' (which see)." | |
724bc265 | 1604 | (when (inferior-octave-process-live-p) |
c8730c3a LL |
1605 | (let* ((ppss (syntax-ppss)) |
1606 | (paren-pos (cadr ppss)) | |
1607 | (fn (save-excursion | |
1608 | (if (and paren-pos | |
1609 | ;; PAREN-POS must be after the prompt | |
1610 | (>= paren-pos | |
1611 | (if (eq (get-buffer-process (current-buffer)) | |
1612 | inferior-octave-process) | |
1613 | (process-mark inferior-octave-process) | |
1614 | (point-min))) | |
1615 | (or (not (eq (get-buffer-process (current-buffer)) | |
1616 | inferior-octave-process)) | |
1617 | (< (process-mark inferior-octave-process) | |
1618 | paren-pos)) | |
1619 | (eq (char-after paren-pos) ?\()) | |
1620 | (goto-char paren-pos) | |
1621 | (setq paren-pos nil)) | |
1622 | (when (or (< (skip-syntax-backward "-") 0) paren-pos) | |
1623 | (thing-at-point 'symbol)))) | |
1624 | (sigs (and fn (octave-eldoc-function-signatures fn))) | |
1625 | (oneline (mapconcat 'identity sigs | |
1626 | (propertize " | " 'face 'warning))) | |
1627 | (multiline (mapconcat (lambda (s) (concat "-- " s)) sigs "\n"))) | |
1628 | ;; | |
1629 | ;; Return the value according to style. | |
1630 | (pcase octave-eldoc-message-style | |
1631 | (`auto (if (< (length oneline) (window-width (minibuffer-window))) | |
1632 | oneline | |
1633 | multiline)) | |
1634 | (`oneline oneline) | |
1635 | (`multiline multiline))))) | |
1636 | ||
d4d0f9b3 LL |
1637 | (defcustom octave-help-buffer "*Octave Help*" |
1638 | "Buffer name for `octave-help'." | |
1639 | :type 'string | |
1640 | :group 'octave | |
1641 | :version "24.4") | |
1642 | ||
30b626e9 GM |
1643 | ;; Used in a mode derived from help-mode. |
1644 | (declare-function help-button-action "help-mode" (button)) | |
1645 | ||
d4d0f9b3 LL |
1646 | (define-button-type 'octave-help-file |
1647 | 'follow-link t | |
1648 | 'action #'help-button-action | |
c129b51f | 1649 | 'help-function 'octave-find-definition) |
d4d0f9b3 LL |
1650 | |
1651 | (define-button-type 'octave-help-function | |
1652 | 'follow-link t | |
1653 | 'action (lambda (b) | |
1654 | (octave-help | |
1655 | (buffer-substring (button-start b) (button-end b))))) | |
1656 | ||
b4da2cbb LL |
1657 | (defvar octave-help-mode-map |
1658 | (let ((map (make-sparse-keymap))) | |
8c09f64b | 1659 | (define-key map "\M-." 'octave-find-definition) |
b4da2cbb | 1660 | (define-key map "\C-hd" 'octave-help) |
5af5ed08 | 1661 | (define-key map "\C-ha" 'octave-lookfor) |
b4da2cbb LL |
1662 | map)) |
1663 | ||
1664 | (define-derived-mode octave-help-mode help-mode "OctHelp" | |
1665 | "Major mode for displaying Octave documentation." | |
1666 | :abbrev-table nil | |
1667 | :syntax-table octave-mode-syntax-table | |
1668 | (eval-and-compile (require 'help-mode)) | |
8c09f64b LL |
1669 | ;; Don't highlight `EXAMPLE' as elisp symbols by using a regexp that |
1670 | ;; can never match. | |
1671 | (setq-local help-xref-symbol-regexp "x\\`")) | |
b4da2cbb | 1672 | |
d4d0f9b3 LL |
1673 | (defun octave-help (fn) |
1674 | "Display the documentation of FN." | |
1675 | (interactive (list (octave-completing-read))) | |
1676 | (inferior-octave-send-list-and-digest | |
daabf15a | 1677 | (list (format "help ('%s');\n" fn))) |
b4da2cbb LL |
1678 | (let ((lines inferior-octave-output-list) |
1679 | (inhibit-read-only t)) | |
d4d0f9b3 LL |
1680 | (when (string-match "error: \\(.*\\)$" (car lines)) |
1681 | (error "%s" (match-string 1 (car lines)))) | |
1682 | (with-help-window octave-help-buffer | |
1683 | (princ (mapconcat 'identity lines "\n")) | |
1684 | (with-current-buffer octave-help-buffer | |
1685 | ;; Bound to t so that `help-buffer' returns current buffer for | |
1686 | ;; `help-setup-xref'. | |
1687 | (let ((help-xref-following t)) | |
1688 | (help-setup-xref (list 'octave-help fn) | |
1689 | (called-interactively-p 'interactive))) | |
1690 | ;; Note: can be turned off by suppress_verbose_help_message. | |
1691 | ;; | |
1692 | ;; Remove boring trailing text: Additional help for built-in functions | |
1693 | ;; and operators ... | |
1694 | (goto-char (point-max)) | |
1695 | (when (search-backward "\n\n\n" nil t) | |
1696 | (goto-char (match-beginning 0)) | |
1697 | (delete-region (point) (point-max))) | |
1698 | ;; File name highlight | |
1699 | (goto-char (point-min)) | |
1700 | (when (re-search-forward "from the file \\(.*\\)$" | |
1701 | (line-end-position) | |
1702 | t) | |
9cc3e83f LL |
1703 | (let* ((file (match-string 1)) |
1704 | (dir (file-name-directory | |
1705 | (directory-file-name (file-name-directory file))))) | |
d4d0f9b3 LL |
1706 | (replace-match "" nil nil nil 1) |
1707 | (insert "`") | |
9cc3e83f LL |
1708 | ;; Include the parent directory which may be regarded as |
1709 | ;; the category for the FN. | |
1710 | (help-insert-xref-button (file-relative-name file dir) | |
c129b51f | 1711 | 'octave-help-file fn) |
d4d0f9b3 | 1712 | (insert "'"))) |
daabf15a | 1713 | ;; Make 'See also' clickable. |
d4d0f9b3 LL |
1714 | (with-syntax-table octave-mode-syntax-table |
1715 | (when (re-search-forward "^\\s-*See also:" nil t) | |
f236dd84 | 1716 | (let ((end (save-excursion (re-search-forward "^\\s-*$" nil t)))) |
daabf15a | 1717 | (while (re-search-forward |
ad528125 | 1718 | "\\s-*\\([^,\n]+?\\)\\s-*\\(?:[,]\\|[.]?$\\)" end t) |
daabf15a | 1719 | (make-text-button (match-beginning 1) (match-end 1) |
837fd9af | 1720 | :type 'octave-help-function))))) |
b4da2cbb | 1721 | (octave-help-mode))))) |
d4d0f9b3 | 1722 | |
5af5ed08 RS |
1723 | (defun octave-lookfor (str &optional all) |
1724 | "Search for the string STR in all function help strings. | |
1725 | If ALL is non-nil search the entire help string else only search the first | |
1726 | sentence." | |
1727 | (interactive "sSearch for: \nP") | |
1728 | (inferior-octave-send-list-and-digest | |
1729 | (list (format "lookfor (%s'%s');\n" | |
1730 | (if all "'-all', " "") | |
1731 | str))) | |
1732 | (let ((lines inferior-octave-output-list)) | |
6408a65d RS |
1733 | (when (and (stringp (car lines)) |
1734 | (string-match "error: \\(.*\\)$" (car lines))) | |
5af5ed08 RS |
1735 | (error "%s" (match-string 1 (car lines)))) |
1736 | (with-help-window octave-help-buffer | |
5af5ed08 | 1737 | (with-current-buffer octave-help-buffer |
6408a65d RS |
1738 | (if lines |
1739 | (insert (mapconcat 'identity lines "\n")) | |
1740 | (insert (format "Nothing found for \"%s\".\n" str))) | |
5af5ed08 RS |
1741 | ;; Bound to t so that `help-buffer' returns current buffer for |
1742 | ;; `help-setup-xref'. | |
1743 | (let ((help-xref-following t)) | |
1744 | (help-setup-xref (list 'octave-lookfor str all) | |
1745 | (called-interactively-p 'interactive))) | |
1746 | (goto-char (point-min)) | |
6408a65d RS |
1747 | (when lines |
1748 | (while (re-search-forward "^\\([^[:blank:]]+\\) " nil 'noerror) | |
1749 | (make-text-button (match-beginning 1) (match-end 1) | |
1750 | :type 'octave-help-function))) | |
1751 | (unless all | |
1752 | (goto-char (point-max)) | |
1753 | (insert "\nRetry with ") | |
1754 | (insert-text-button "'-all'" | |
1755 | 'follow-link t | |
3eb91f7b | 1756 | 'action #'(lambda (_b) |
6408a65d RS |
1757 | (octave-lookfor str '-all))) |
1758 | (insert ".\n")) | |
5af5ed08 RS |
1759 | (octave-help-mode))))) |
1760 | ||
30ea8374 | 1761 | (defcustom octave-source-directories nil |
5e80b74f LL |
1762 | "A list of directories for Octave sources. |
1763 | If the environment variable OCTAVE_SRCDIR is set, it is searched first." | |
30ea8374 | 1764 | :type '(repeat directory) |
c129b51f LL |
1765 | :group 'octave |
1766 | :version "24.4") | |
1767 | ||
30ea8374 | 1768 | (defun octave-source-directories () |
5e80b74f LL |
1769 | (let ((srcdir (or (and inferior-octave-process |
1770 | (process-get inferior-octave-process 'octave-srcdir)) | |
1771 | (getenv "OCTAVE_SRCDIR")))) | |
30ea8374 LL |
1772 | (if srcdir |
1773 | (cons srcdir octave-source-directories) | |
1774 | octave-source-directories))) | |
1775 | ||
1776 | (defvar octave-find-definition-filename-function | |
1777 | #'octave-find-definition-default-filename) | |
1778 | ||
1779 | (defun octave-find-definition-default-filename (name) | |
1780 | "Default value for `octave-find-definition-filename-function'." | |
1781 | (pcase (file-name-extension name) | |
1782 | (`"oct" | |
1783 | (octave-find-definition-default-filename | |
1784 | (concat "libinterp/dldfcn/" | |
1785 | (file-name-sans-extension (file-name-nondirectory name)) | |
1786 | ".cc"))) | |
1787 | (`"cc" | |
1788 | (let ((file (or (locate-file name (octave-source-directories)) | |
1789 | (locate-file (file-name-nondirectory name) | |
1790 | (octave-source-directories))))) | |
1791 | (or (and file (file-exists-p file)) | |
1792 | (error "File `%s' not found" name)) | |
1793 | file)) | |
1794 | (`"mex" | |
1795 | (if (yes-or-no-p (format "File `%s' may be binary; open? " | |
1796 | (file-name-nondirectory name))) | |
1797 | name | |
1798 | (user-error "Aborted"))) | |
1799 | (t name))) | |
1800 | ||
d4d0f9b3 LL |
1801 | (defvar find-tag-marker-ring) |
1802 | ||
1803 | (defun octave-find-definition (fn) | |
30ea8374 | 1804 | "Find the definition of FN. |
5e80b74f | 1805 | Functions implemented in C++ can be found if |
7e138a62 | 1806 | variable `octave-source-directories' is set correctly." |
d4d0f9b3 | 1807 | (interactive (list (octave-completing-read))) |
fda74125 LL |
1808 | (require 'etags) |
1809 | (let ((orig (point))) | |
daabf15a LL |
1810 | (if (and (derived-mode-p 'octave-mode) |
1811 | (octave-goto-function-definition fn)) | |
fda74125 LL |
1812 | (ring-insert find-tag-marker-ring (copy-marker orig)) |
1813 | (inferior-octave-send-list-and-digest | |
1814 | ;; help NAME is more verbose | |
1815 | (list (format "\ | |
daabf15a | 1816 | if iskeyword('%s') disp('`%s'' is a keyword') else which('%s') endif\n" |
fda74125 LL |
1817 | fn fn fn))) |
1818 | (let (line file) | |
1819 | ;; Skip garbage lines such as | |
1820 | ;; warning: fmincg.m: possible Matlab-style .... | |
1821 | (while (and (not file) (consp inferior-octave-output-list)) | |
1822 | (setq line (pop inferior-octave-output-list)) | |
1823 | (when (string-match "from the file \\(.*\\)$" line) | |
1824 | (setq file (match-string 1 line)))) | |
1825 | (if (not file) | |
1826 | (user-error "%s" (or line (format "`%s' not found" fn))) | |
1827 | (ring-insert find-tag-marker-ring (point-marker)) | |
1828 | (setq file (funcall octave-find-definition-filename-function file)) | |
1829 | (when file | |
1830 | (find-file file) | |
1831 | (octave-goto-function-definition fn))))))) | |
d4d0f9b3 | 1832 | |
be64c05d LL |
1833 | (provide 'octave) |
1834 | ;;; octave.el ends here |