octave-mod.el 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. ;;; octave-mod.el --- editing Octave source files under Emacs
  2. ;; Copyright (C) 1997, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
  4. ;; John Eaton <jwe@octave.org>
  5. ;; Maintainer: FSF
  6. ;; Keywords: languages
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This package provides Emacs support for Octave.
  20. ;; It defines Octave mode, a major mode for editing
  21. ;; Octave code.
  22. ;; The file octave-inf.el contains code for interacting with an inferior
  23. ;; Octave process using comint.
  24. ;; See the documentation of `octave-mode' and
  25. ;; `run-octave' for further information on usage and customization.
  26. ;;; Code:
  27. (require 'custom)
  28. (defgroup octave nil
  29. "Major mode for editing Octave source files."
  30. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  31. :group 'languages)
  32. (defvar inferior-octave-output-list nil)
  33. (defvar inferior-octave-output-string nil)
  34. (defvar inferior-octave-receive-in-progress nil)
  35. (declare-function inferior-octave-send-list-and-digest "octave-inf" (list))
  36. (defconst octave-maintainer-address
  37. "Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>, bug-gnu-emacs@gnu.org"
  38. "Current maintainer of the Emacs Octave package.")
  39. (define-abbrev-table 'octave-abbrev-table
  40. (mapcar (lambda (e) (append e '(nil 0 t)))
  41. '(("`a" "all_va_args")
  42. ("`b" "break")
  43. ("`cs" "case")
  44. ("`ca" "catch")
  45. ("`c" "continue")
  46. ("`el" "else")
  47. ("`eli" "elseif")
  48. ("`et" "end_try_catch")
  49. ("`eu" "end_unwind_protect")
  50. ("`ef" "endfor")
  51. ("`efu" "endfunction")
  52. ("`ei" "endif")
  53. ("`es" "endswitch")
  54. ("`ew" "endwhile")
  55. ("`f" "for")
  56. ("`fu" "function")
  57. ("`gl" "global")
  58. ("`gp" "gplot")
  59. ("`gs" "gsplot")
  60. ("`if" "if ()")
  61. ("`o" "otherwise")
  62. ("`rp" "replot")
  63. ("`r" "return")
  64. ("`s" "switch")
  65. ("`t" "try")
  66. ("`u" "until ()")
  67. ("`up" "unwind_protect")
  68. ("`upc" "unwind_protect_cleanup")
  69. ("`w" "while ()")))
  70. "Abbrev table for Octave's reserved words.
  71. Used in `octave-mode' and inferior-octave-mode buffers.
  72. All Octave abbrevs start with a grave accent (`)."
  73. :regexp "\\(?:[^`]\\|^\\)\\(\\(?:\\<\\|`\\)\\w+\\)\\W*")
  74. (defvar octave-comment-char ?#
  75. "Character to start an Octave comment.")
  76. (defvar octave-comment-start
  77. (string octave-comment-char ?\s)
  78. "String to insert to start a new Octave in-line comment.")
  79. (defvar octave-comment-start-skip "\\s<+\\s-*"
  80. "Regexp to match the start of an Octave comment up to its body.")
  81. (defvar octave-begin-keywords
  82. '("do" "for" "function" "if" "switch" "try" "unwind_protect" "while"))
  83. (defvar octave-else-keywords
  84. '("case" "catch" "else" "elseif" "otherwise" "unwind_protect_cleanup"))
  85. (defvar octave-end-keywords
  86. '("endfor" "endfunction" "endif" "endswitch" "end_try_catch"
  87. "end_unwind_protect" "endwhile" "until" "end"))
  88. (defvar octave-reserved-words
  89. (append octave-begin-keywords
  90. octave-else-keywords
  91. octave-end-keywords
  92. '("break" "continue" "end" "global" "persistent" "return"))
  93. "Reserved words in Octave.")
  94. (defvar octave-text-functions
  95. '("casesen" "cd" "chdir" "clear" "diary" "dir" "document" "echo"
  96. "edit_history" "format" "help" "history" "hold"
  97. "load" "ls" "more" "run_history" "save" "type"
  98. "which" "who" "whos")
  99. "Text functions in Octave.")
  100. (defvar octave-variables
  101. '("DEFAULT_EXEC_PATH" "DEFAULT_LOADPATH"
  102. "EDITOR" "EXEC_PATH" "F_DUPFD" "F_GETFD" "F_GETFL" "F_SETFD"
  103. "F_SETFL" "I" "IMAGE_PATH" "Inf" "J"
  104. "NaN" "OCTAVE_VERSION" "O_APPEND" "O_CREAT" "O_EXCL"
  105. "O_NONBLOCK" "O_RDONLY" "O_RDWR" "O_TRUNC" "O_WRONLY" "PAGER" "PS1"
  106. "PS2" "PS4" "PWD" "SEEK_CUR" "SEEK_END" "SEEK_SET" "__F_DUPFD__"
  107. "__F_GETFD__" "__F_GETFL__" "__F_SETFD__" "__F_SETFL__" "__I__"
  108. "__Inf__" "__J__" "__NaN__" "__OCTAVE_VERSION__" "__O_APPEND__"
  109. "__O_CREAT__" "__O_EXCL__" "__O_NONBLOCK__" "__O_RDONLY__"
  110. "__O_RDWR__" "__O_TRUNC__" "__O_WRONLY__" "__PWD__" "__SEEK_CUR__"
  111. "__SEEK_END__" "__SEEK_SET__" "__argv__" "__e__" "__eps__"
  112. "__i__" "__inf__" "__j__" "__nan__" "__pi__"
  113. "__program_invocation_name__" "__program_name__" "__realmax__"
  114. "__realmin__" "__stderr__" "__stdin__" "__stdout__" "ans" "argv"
  115. "beep_on_error" "completion_append_char"
  116. "crash_dumps_octave_core" "default_save_format"
  117. "e" "echo_executing_commands" "eps"
  118. "error_text" "gnuplot_binary" "history_file"
  119. "history_size" "ignore_function_time_stamp"
  120. "inf" "nan" "nargin" "output_max_field_width" "output_precision"
  121. "page_output_immediately" "page_screen_output" "pi"
  122. "print_answer_id_name" "print_empty_dimensions"
  123. "program_invocation_name" "program_name"
  124. "realmax" "realmin" "return_last_computed_value" "save_precision"
  125. "saving_history" "sighup_dumps_octave_core" "sigterm_dumps_octave_core"
  126. "silent_functions" "split_long_rows" "stderr" "stdin" "stdout"
  127. "string_fill_char" "struct_levels_to_print"
  128. "suppress_verbose_help_message")
  129. "Builtin variables in Octave.")
  130. (defvar octave-function-header-regexp
  131. (concat "^\\s-*\\_<\\(function\\)\\_>"
  132. "\\([^=;\n]*=[ \t]*\\|[ \t]*\\)\\(\\(?:\\w\\|\\s_\\)+\\)\\_>")
  133. "Regexp to match an Octave function header.
  134. The string `function' and its name are given by the first and third
  135. parenthetical grouping.")
  136. (defvar octave-font-lock-keywords
  137. (list
  138. ;; Fontify all builtin keywords.
  139. (cons (concat "\\_<\\("
  140. (regexp-opt (append octave-reserved-words
  141. octave-text-functions))
  142. "\\)\\_>")
  143. 'font-lock-keyword-face)
  144. ;; Fontify all builtin operators.
  145. (cons "\\(&\\||\\|<=\\|>=\\|==\\|<\\|>\\|!=\\|!\\)"
  146. (if (boundp 'font-lock-builtin-face)
  147. 'font-lock-builtin-face
  148. 'font-lock-preprocessor-face))
  149. ;; Fontify all builtin variables.
  150. (cons (concat "\\_<" (regexp-opt octave-variables) "\\_>")
  151. 'font-lock-variable-name-face)
  152. ;; Fontify all function declarations.
  153. (list octave-function-header-regexp
  154. '(1 font-lock-keyword-face)
  155. '(3 font-lock-function-name-face nil t)))
  156. "Additional Octave expressions to highlight.")
  157. (defun octave-syntax-propertize-function (start end)
  158. (goto-char start)
  159. (octave-syntax-propertize-sqs end)
  160. (funcall (syntax-propertize-rules
  161. ;; Try to distinguish the string-quotes from the transpose-quotes.
  162. ("[[({,; ]\\('\\)"
  163. (1 (prog1 "\"'" (octave-syntax-propertize-sqs end)))))
  164. (point) end))
  165. (defun octave-syntax-propertize-sqs (end)
  166. "Propertize the content/end of single-quote strings."
  167. (when (eq (nth 3 (syntax-ppss)) ?\')
  168. ;; A '..' string.
  169. (when (re-search-forward
  170. "\\(?:\\=\\|[^']\\)\\(?:''\\)*\\('\\)\\($\\|[^']\\)" end 'move)
  171. (goto-char (match-beginning 2))
  172. (when (eq (char-before (match-beginning 1)) ?\\)
  173. ;; Backslash cannot escape a single quote.
  174. (put-text-property (1- (match-beginning 1)) (match-beginning 1)
  175. 'syntax-table (string-to-syntax ".")))
  176. (put-text-property (match-beginning 1) (match-end 1)
  177. 'syntax-table (string-to-syntax "\"'")))))
  178. (defcustom inferior-octave-buffer "*Inferior Octave*"
  179. "Name of buffer for running an inferior Octave process."
  180. :type 'string
  181. :group 'octave-inferior)
  182. (defvar inferior-octave-process nil)
  183. (defvar octave-mode-map
  184. (let ((map (make-sparse-keymap)))
  185. (define-key map "`" 'octave-abbrev-start)
  186. (define-key map "\e\n" 'octave-indent-new-comment-line)
  187. (define-key map "\M-\C-q" 'octave-indent-defun)
  188. (define-key map "\C-c\C-b" 'octave-submit-bug-report)
  189. (define-key map "\C-c\C-p" 'octave-previous-code-line)
  190. (define-key map "\C-c\C-n" 'octave-next-code-line)
  191. (define-key map "\C-c\C-a" 'octave-beginning-of-line)
  192. (define-key map "\C-c\C-e" 'octave-end-of-line)
  193. (define-key map [remap down-list] 'smie-down-list)
  194. (define-key map "\C-c\M-\C-h" 'octave-mark-block)
  195. (define-key map "\C-c]" 'smie-close-block)
  196. (define-key map "\C-c/" 'smie-close-block)
  197. (define-key map "\C-c\C-f" 'octave-insert-defun)
  198. ;; FIXME: free C-h so it can do the describe-prefix-bindings.
  199. (define-key map "\C-c\C-h" 'info-lookup-symbol)
  200. (define-key map "\C-c\C-il" 'octave-send-line)
  201. (define-key map "\C-c\C-ib" 'octave-send-block)
  202. (define-key map "\C-c\C-if" 'octave-send-defun)
  203. (define-key map "\C-c\C-ir" 'octave-send-region)
  204. (define-key map "\C-c\C-is" 'octave-show-process-buffer)
  205. (define-key map "\C-c\C-ih" 'octave-hide-process-buffer)
  206. (define-key map "\C-c\C-ik" 'octave-kill-process)
  207. (define-key map "\C-c\C-i\C-l" 'octave-send-line)
  208. (define-key map "\C-c\C-i\C-b" 'octave-send-block)
  209. (define-key map "\C-c\C-i\C-f" 'octave-send-defun)
  210. (define-key map "\C-c\C-i\C-r" 'octave-send-region)
  211. (define-key map "\C-c\C-i\C-s" 'octave-show-process-buffer)
  212. ;; FIXME: free C-h so it can do the describe-prefix-bindings.
  213. (define-key map "\C-c\C-i\C-h" 'octave-hide-process-buffer)
  214. (define-key map "\C-c\C-i\C-k" 'octave-kill-process)
  215. map)
  216. "Keymap used in Octave mode.")
  217. (easy-menu-define octave-mode-menu octave-mode-map
  218. "Menu for Octave mode."
  219. '("Octave"
  220. ("Lines"
  221. ["Previous Code Line" octave-previous-code-line t]
  222. ["Next Code Line" octave-next-code-line t]
  223. ["Begin of Continuation" octave-beginning-of-line t]
  224. ["End of Continuation" octave-end-of-line t]
  225. ["Split Line at Point" octave-indent-new-comment-line t])
  226. ("Blocks"
  227. ["Mark Block" octave-mark-block t]
  228. ["Close Block" smie-close-block t])
  229. ("Functions"
  230. ["Indent Function" octave-indent-defun t]
  231. ["Insert Function" octave-insert-defun t])
  232. "-"
  233. ("Debug"
  234. ["Send Current Line" octave-send-line t]
  235. ["Send Current Block" octave-send-block t]
  236. ["Send Current Function" octave-send-defun t]
  237. ["Send Region" octave-send-region t]
  238. ["Show Process Buffer" octave-show-process-buffer t]
  239. ["Hide Process Buffer" octave-hide-process-buffer t]
  240. ["Kill Process" octave-kill-process t])
  241. "-"
  242. ["Indent Line" indent-according-to-mode t]
  243. ["Complete Symbol" completion-at-point t]
  244. "-"
  245. ["Toggle Abbrev Mode" abbrev-mode
  246. :style toggle :selected abbrev-mode]
  247. ["Toggle Auto-Fill Mode" auto-fill-mode
  248. :style toggle :selected auto-fill-function]
  249. "-"
  250. ["Submit Bug Report" octave-submit-bug-report t]
  251. "-"
  252. ["Describe Octave Mode" describe-mode t]
  253. ["Lookup Octave Index" info-lookup-symbol t]))
  254. (defvar octave-mode-syntax-table
  255. (let ((table (make-syntax-table)))
  256. (modify-syntax-entry ?\r " " table)
  257. (modify-syntax-entry ?+ "." table)
  258. (modify-syntax-entry ?- "." table)
  259. (modify-syntax-entry ?= "." table)
  260. (modify-syntax-entry ?* "." table)
  261. (modify-syntax-entry ?/ "." table)
  262. (modify-syntax-entry ?> "." table)
  263. (modify-syntax-entry ?< "." table)
  264. (modify-syntax-entry ?& "." table)
  265. (modify-syntax-entry ?| "." table)
  266. (modify-syntax-entry ?! "." table)
  267. (modify-syntax-entry ?\\ "\\" table)
  268. (modify-syntax-entry ?\' "." table)
  269. ;; Was "w" for abbrevs, but now that it's not necessary any more,
  270. (modify-syntax-entry ?\` "." table)
  271. (modify-syntax-entry ?\" "\"" table)
  272. (modify-syntax-entry ?. "_" table)
  273. (modify-syntax-entry ?_ "_" table)
  274. ;; The "b" flag only applies to the second letter of the comstart
  275. ;; and the first letter of the comend, i.e. the "4b" below is ineffective.
  276. ;; If we try to put `b' on the single-line comments, we get a similar
  277. ;; problem where the % and # chars appear as first chars of the 2-char
  278. ;; comend, so the multi-line ender is also turned into style-b.
  279. ;; So we need the new "c" comment style.
  280. (modify-syntax-entry ?\% "< 13" table)
  281. (modify-syntax-entry ?\# "< 13" table)
  282. (modify-syntax-entry ?\{ "(} 2c" table)
  283. (modify-syntax-entry ?\} "){ 4c" table)
  284. (modify-syntax-entry ?\n ">" table)
  285. table)
  286. "Syntax table in use in `octave-mode' buffers.")
  287. (defcustom octave-blink-matching-block t
  288. "Control the blinking of matching Octave block keywords.
  289. Non-nil means show matching begin of block when inserting a space,
  290. newline or semicolon after an else or end keyword."
  291. :type 'boolean
  292. :group 'octave)
  293. (defcustom octave-block-offset 2
  294. "Extra indentation applied to statements in Octave block structures."
  295. :type 'integer
  296. :group 'octave)
  297. (defvar octave-block-comment-start
  298. (concat (make-string 2 octave-comment-char) " ")
  299. "String to insert to start a new Octave comment on an empty line.")
  300. (defcustom octave-continuation-offset 4
  301. "Extra indentation applied to Octave continuation lines."
  302. :type 'integer
  303. :group 'octave)
  304. (eval-and-compile
  305. (defconst octave-continuation-marker-regexp "\\\\\\|\\.\\.\\."))
  306. (defvar octave-continuation-regexp
  307. (concat "[^#%\n]*\\(" octave-continuation-marker-regexp
  308. "\\)\\s-*\\(\\s<.*\\)?$"))
  309. (defcustom octave-continuation-string "\\"
  310. "Character string used for Octave continuation lines. Normally \\."
  311. :type 'string
  312. :group 'octave)
  313. (defvar octave-completion-alist nil
  314. "Alist of Octave symbols for completion in Octave mode.
  315. Each element looks like (VAR . VAR), where the car and cdr are the same
  316. symbol (an Octave command or variable name).
  317. Currently, only builtin variables can be completed.")
  318. (defvar octave-mode-imenu-generic-expression
  319. (list
  320. ;; Functions
  321. (list nil octave-function-header-regexp 3))
  322. "Imenu expression for Octave mode. See `imenu-generic-expression'.")
  323. (defcustom octave-mode-hook nil
  324. "Hook to be run when Octave mode is started."
  325. :type 'hook
  326. :group 'octave)
  327. (defcustom octave-send-show-buffer t
  328. "Non-nil means display `inferior-octave-buffer' after sending to it."
  329. :type 'boolean
  330. :group 'octave)
  331. (defcustom octave-send-line-auto-forward t
  332. "Control auto-forward after sending to the inferior Octave process.
  333. Non-nil means always go to the next Octave code line after sending."
  334. :type 'boolean
  335. :group 'octave)
  336. (defcustom octave-send-echo-input t
  337. "Non-nil means echo input sent to the inferior Octave process."
  338. :type 'boolean
  339. :group 'octave)
  340. ;;; SMIE indentation
  341. (require 'smie)
  342. (defconst octave-operator-table
  343. '((assoc ";" "\n") (assoc ",") ; The doc claims they have equal precedence!?
  344. (right "=" "+=" "-=" "*=" "/=")
  345. (assoc "&&") (assoc "||") ; The doc claims they have equal precedence!?
  346. (assoc "&") (assoc "|") ; The doc claims they have equal precedence!?
  347. (nonassoc "<" "<=" "==" ">=" ">" "!=" "~=")
  348. (nonassoc ":") ;No idea what this is.
  349. (assoc "+" "-")
  350. (assoc "*" "/" "\\" ".\\" ".*" "./")
  351. (nonassoc "'" ".'")
  352. (nonassoc "++" "--" "!" "~") ;And unary "+" and "-".
  353. (right "^" "**" ".^" ".**")
  354. ;; It's not really an operator, but for indentation purposes it
  355. ;; could be convenient to treat it as one.
  356. (assoc "...")))
  357. (defconst octave-smie-bnf-table
  358. '((atom)
  359. ;; We can't distinguish the first element in a sequence with
  360. ;; precedence grammars, so we can't distinguish the condition
  361. ;; if the `if' from the subsequent body, for example.
  362. ;; This has to be done later in the indentation rules.
  363. (exp (exp "\n" exp)
  364. ;; We need to mention at least one of the operators in this part
  365. ;; of the grammar: if the BNF and the operator table have
  366. ;; no overlap, SMIE can't know how they relate.
  367. (exp ";" exp)
  368. ("try" exp "catch" exp "end_try_catch")
  369. ("try" exp "catch" exp "end")
  370. ("unwind_protect" exp
  371. "unwind_protect_cleanup" exp "end_unwind_protect")
  372. ("unwind_protect" exp "unwind_protect_cleanup" exp "end")
  373. ("for" exp "endfor")
  374. ("for" exp "end")
  375. ("do" exp "until" atom)
  376. ("while" exp "endwhile")
  377. ("while" exp "end")
  378. ("if" exp "endif")
  379. ("if" exp "else" exp "endif")
  380. ("if" exp "elseif" exp "else" exp "endif")
  381. ("if" exp "elseif" exp "elseif" exp "else" exp "endif")
  382. ("if" exp "elseif" exp "elseif" exp "else" exp "end")
  383. ("switch" exp "case" exp "endswitch")
  384. ("switch" exp "case" exp "otherwise" exp "endswitch")
  385. ("switch" exp "case" exp "case" exp "otherwise" exp "endswitch")
  386. ("switch" exp "case" exp "case" exp "otherwise" exp "end")
  387. ("function" exp "endfunction")
  388. ("function" exp "end"))
  389. ;; (fundesc (atom "=" atom))
  390. ))
  391. (defconst octave-smie-grammar
  392. (smie-prec2->grammar
  393. (smie-merge-prec2s
  394. (smie-bnf->prec2 octave-smie-bnf-table
  395. '((assoc "\n" ";")))
  396. (smie-precs->prec2 octave-operator-table))))
  397. ;; Tokenizing needs to be refined so that ";;" is treated as two
  398. ;; tokens and also so as to recognize the \n separator (and
  399. ;; corresponding continuation lines).
  400. (defconst octave-operator-regexp
  401. (regexp-opt (apply 'append (mapcar 'cdr octave-operator-table))))
  402. (defun octave-smie-backward-token ()
  403. (let ((pos (point)))
  404. (forward-comment (- (point)))
  405. (cond
  406. ((and (not (eq (char-before) ?\;)) ;Coalesce ";" and "\n".
  407. (> pos (line-end-position))
  408. (if (looking-back octave-continuation-marker-regexp (- (point) 3))
  409. (progn
  410. (goto-char (match-beginning 0))
  411. (forward-comment (- (point)))
  412. nil)
  413. t)
  414. ;; Ignore it if it's within parentheses.
  415. (let ((ppss (syntax-ppss)))
  416. (not (and (nth 1 ppss)
  417. (eq ?\( (char-after (nth 1 ppss)))))))
  418. (skip-chars-forward " \t")
  419. ;; Why bother distinguishing \n and ;?
  420. ";") ;;"\n"
  421. ((and (looking-back octave-operator-regexp (- (point) 3) 'greedy)
  422. ;; Don't mistake a string quote for a transpose.
  423. (not (looking-back "\\s\"" (1- (point)))))
  424. (goto-char (match-beginning 0))
  425. (match-string-no-properties 0))
  426. (t
  427. (smie-default-backward-token)))))
  428. (defun octave-smie-forward-token ()
  429. (skip-chars-forward " \t")
  430. (when (looking-at (eval-when-compile
  431. (concat "\\(" octave-continuation-marker-regexp
  432. "\\)[ \t]*\\($\\|[%#]\\)")))
  433. (goto-char (match-end 1))
  434. (forward-comment 1))
  435. (cond
  436. ((and (looking-at "$\\|[%#]")
  437. ;; Ignore it if it's within parentheses.
  438. (prog1 (let ((ppss (syntax-ppss)))
  439. (not (and (nth 1 ppss)
  440. (eq ?\( (char-after (nth 1 ppss))))))
  441. (forward-comment (point-max))))
  442. ;; Why bother distinguishing \n and ;?
  443. ";") ;;"\n"
  444. ((looking-at ";[ \t]*\\($\\|[%#]\\)")
  445. ;; Combine the ; with the subsequent \n.
  446. (goto-char (match-beginning 1))
  447. (forward-comment 1)
  448. ";")
  449. ((and (looking-at octave-operator-regexp)
  450. ;; Don't mistake a string quote for a transpose.
  451. (not (looking-at "\\s\"")))
  452. (goto-char (match-end 0))
  453. (match-string-no-properties 0))
  454. (t
  455. (smie-default-forward-token))))
  456. (defun octave-smie-rules (kind token)
  457. (pcase (cons kind token)
  458. ;; We could set smie-indent-basic instead, but that would have two
  459. ;; disadvantages:
  460. ;; - changes to octave-block-offset wouldn't take effect immediately.
  461. ;; - edebug wouldn't show the use of this variable.
  462. (`(:elem . basic) octave-block-offset)
  463. ;; Since "case" is in the same BNF rules as switch..end, SMIE by default
  464. ;; aligns it with "switch".
  465. (`(:before . "case") (if (not (smie-rule-sibling-p)) octave-block-offset))
  466. (`(:after . ";")
  467. (if (smie-rule-parent-p "function" "if" "while" "else" "elseif" "for"
  468. "otherwise" "case" "try" "catch" "unwind_protect"
  469. "unwind_protect_cleanup")
  470. (smie-rule-parent octave-block-offset)
  471. ;; For (invalid) code between switch and case.
  472. ;; (if (smie-parent-p "switch") 4)
  473. 0))))
  474. (defvar electric-layout-rules)
  475. ;;;###autoload
  476. (define-derived-mode octave-mode prog-mode "Octave"
  477. "Major mode for editing Octave code.
  478. This mode makes it easier to write Octave code by helping with
  479. indentation, doing some of the typing for you (with Abbrev mode) and by
  480. showing keywords, comments, strings, etc. in different faces (with
  481. Font Lock mode on terminals that support it).
  482. Octave itself is a high-level language, primarily intended for numerical
  483. computations. It provides a convenient command line interface for
  484. solving linear and nonlinear problems numerically. Function definitions
  485. can also be stored in files, and it can be used in a batch mode (which
  486. is why you need this mode!).
  487. The latest released version of Octave is always available via anonymous
  488. ftp from ftp.octave.org in the directory `/pub/octave'. Complete
  489. source and binaries for several popular systems are available.
  490. Type \\[list-abbrevs] to display the built-in abbrevs for Octave keywords.
  491. Keybindings
  492. ===========
  493. \\{octave-mode-map}
  494. Variables you can use to customize Octave mode
  495. ==============================================
  496. `octave-blink-matching-block'
  497. Non-nil means show matching begin of block when inserting a space,
  498. newline or semicolon after an else or end keyword. Default is t.
  499. `octave-block-offset'
  500. Extra indentation applied to statements in block structures.
  501. Default is 2.
  502. `octave-continuation-offset'
  503. Extra indentation applied to Octave continuation lines.
  504. Default is 4.
  505. `octave-continuation-string'
  506. String used for Octave continuation lines.
  507. Default is a backslash.
  508. `octave-send-echo-input'
  509. Non-nil means always display `inferior-octave-buffer' after sending a
  510. command to the inferior Octave process.
  511. `octave-send-line-auto-forward'
  512. Non-nil means always go to the next unsent line of Octave code after
  513. sending a line to the inferior Octave process.
  514. `octave-send-echo-input'
  515. Non-nil means echo input sent to the inferior Octave process.
  516. Turning on Octave mode runs the hook `octave-mode-hook'.
  517. To begin using this mode for all `.m' files that you edit, add the
  518. following lines to your `.emacs' file:
  519. (add-to-list 'auto-mode-alist '(\"\\\\.m\\\\'\" . octave-mode))
  520. To automatically turn on the abbrev and auto-fill features,
  521. add the following lines to your `.emacs' file as well:
  522. (add-hook 'octave-mode-hook
  523. (lambda ()
  524. (abbrev-mode 1)
  525. (auto-fill-mode 1)))
  526. To submit a problem report, enter \\[octave-submit-bug-report] from \
  527. an Octave mode buffer.
  528. This automatically sets up a mail buffer with version information
  529. already added. You just need to add a description of the problem,
  530. including a reproducible test case and send the message."
  531. (setq local-abbrev-table octave-abbrev-table)
  532. (smie-setup octave-smie-grammar #'octave-smie-rules
  533. :forward-token #'octave-smie-forward-token
  534. :backward-token #'octave-smie-backward-token)
  535. (set (make-local-variable 'smie-indent-basic) 'octave-block-offset)
  536. (set (make-local-variable 'smie-blink-matching-triggers)
  537. (cons ?\; smie-blink-matching-triggers))
  538. (unless octave-blink-matching-block
  539. (remove-hook 'post-self-insert-hook #'smie-blink-matching-open 'local))
  540. (set (make-local-variable 'electric-indent-chars)
  541. (cons ?\; electric-indent-chars))
  542. ;; IIUC matlab-mode takes the opposite approach: it makes RET insert
  543. ;; a ";" at those places where it's correct (i.e. outside of parens).
  544. (set (make-local-variable 'electric-layout-rules) '((?\; . after)))
  545. (set (make-local-variable 'comment-start) octave-comment-start)
  546. (set (make-local-variable 'comment-end) "")
  547. ;; Don't set it here: it's not really a property of the language,
  548. ;; just a personal preference of the author.
  549. ;; (set (make-local-variable 'comment-column) 32)
  550. (set (make-local-variable 'comment-start-skip) "\\s<+\\s-*")
  551. (set (make-local-variable 'comment-add) 1)
  552. (set (make-local-variable 'parse-sexp-ignore-comments) t)
  553. (set (make-local-variable 'paragraph-start)
  554. (concat "\\s-*$\\|" page-delimiter))
  555. (set (make-local-variable 'paragraph-separate) paragraph-start)
  556. (set (make-local-variable 'paragraph-ignore-fill-prefix) t)
  557. (set (make-local-variable 'fill-paragraph-function) 'octave-fill-paragraph)
  558. ;; FIXME: Why disable it?
  559. ;; (set (make-local-variable 'adaptive-fill-regexp) nil)
  560. ;; Again, this is not a property of the language, don't set it here.
  561. ;; (set (make-local-variable 'fill-column) 72)
  562. (set (make-local-variable 'normal-auto-fill-function) 'octave-auto-fill)
  563. (set (make-local-variable 'font-lock-defaults)
  564. '(octave-font-lock-keywords))
  565. (set (make-local-variable 'syntax-propertize-function)
  566. #'octave-syntax-propertize-function)
  567. (set (make-local-variable 'imenu-generic-expression)
  568. octave-mode-imenu-generic-expression)
  569. (set (make-local-variable 'imenu-case-fold-search) nil)
  570. (add-hook 'completion-at-point-functions
  571. 'octave-completion-at-point-function nil t)
  572. (set (make-local-variable 'beginning-of-defun-function)
  573. 'octave-beginning-of-defun)
  574. (easy-menu-add octave-mode-menu)
  575. (octave-initialize-completions))
  576. ;;; Miscellaneous useful functions
  577. (defsubst octave-in-comment-p ()
  578. "Return t if point is inside an Octave comment."
  579. (nth 4 (syntax-ppss)))
  580. (defsubst octave-in-string-p ()
  581. "Return t if point is inside an Octave string."
  582. (nth 3 (syntax-ppss)))
  583. (defsubst octave-not-in-string-or-comment-p ()
  584. "Return t if point is not inside an Octave string or comment."
  585. (let ((pps (syntax-ppss)))
  586. (not (or (nth 3 pps) (nth 4 pps)))))
  587. (defun octave-looking-at-kw (regexp)
  588. "Like `looking-at', but sets `case-fold-search' nil."
  589. (let ((case-fold-search nil))
  590. (looking-at regexp)))
  591. (defun octave-maybe-insert-continuation-string ()
  592. (if (or (octave-in-comment-p)
  593. (save-excursion
  594. (beginning-of-line)
  595. (looking-at octave-continuation-regexp)))
  596. nil
  597. (delete-horizontal-space)
  598. (insert (concat " " octave-continuation-string))))
  599. ;;; Indentation
  600. (defun octave-indent-new-comment-line ()
  601. "Break Octave line at point, continuing comment if within one.
  602. If within code, insert `octave-continuation-string' before breaking the
  603. line. If within a string, signal an error.
  604. The new line is properly indented."
  605. (interactive)
  606. (delete-horizontal-space)
  607. (cond
  608. ((octave-in-comment-p)
  609. (indent-new-comment-line))
  610. ((octave-in-string-p)
  611. (error "Cannot split a code line inside a string"))
  612. (t
  613. (insert (concat " " octave-continuation-string))
  614. (reindent-then-newline-and-indent))))
  615. (defun octave-indent-defun ()
  616. "Properly indent the Octave function which contains point."
  617. (interactive)
  618. (save-excursion
  619. (mark-defun)
  620. (message "Indenting function...")
  621. (indent-region (point) (mark) nil))
  622. (message "Indenting function...done."))
  623. ;;; Motion
  624. (defun octave-next-code-line (&optional arg)
  625. "Move ARG lines of Octave code forward (backward if ARG is negative).
  626. Skips past all empty and comment lines. Default for ARG is 1.
  627. On success, return 0. Otherwise, go as far as possible and return -1."
  628. (interactive "p")
  629. (or arg (setq arg 1))
  630. (beginning-of-line)
  631. (let ((n 0)
  632. (inc (if (> arg 0) 1 -1)))
  633. (while (and (/= arg 0) (= n 0))
  634. (setq n (forward-line inc))
  635. (while (and (= n 0)
  636. (looking-at "\\s-*\\($\\|\\s<\\)"))
  637. (setq n (forward-line inc)))
  638. (setq arg (- arg inc)))
  639. n))
  640. (defun octave-previous-code-line (&optional arg)
  641. "Move ARG lines of Octave code backward (forward if ARG is negative).
  642. Skips past all empty and comment lines. Default for ARG is 1.
  643. On success, return 0. Otherwise, go as far as possible and return -1."
  644. (interactive "p")
  645. (or arg (setq arg 1))
  646. (octave-next-code-line (- arg)))
  647. (defun octave-beginning-of-line ()
  648. "Move point to beginning of current Octave line.
  649. If on an empty or comment line, go to the beginning of that line.
  650. Otherwise, move backward to the beginning of the first Octave code line
  651. which is not inside a continuation statement, i.e., which does not
  652. follow a code line ending in `...' or `\\', or is inside an open
  653. parenthesis list."
  654. (interactive)
  655. (beginning-of-line)
  656. (if (not (looking-at "\\s-*\\($\\|\\s<\\)"))
  657. (while (or (condition-case nil
  658. (progn
  659. (up-list -1)
  660. (beginning-of-line)
  661. t)
  662. (error nil))
  663. (and (or (looking-at "\\s-*\\($\\|\\s<\\)")
  664. (save-excursion
  665. (if (zerop (octave-previous-code-line))
  666. (looking-at octave-continuation-regexp))))
  667. (zerop (forward-line -1)))))))
  668. (defun octave-end-of-line ()
  669. "Move point to end of current Octave line.
  670. If on an empty or comment line, go to the end of that line.
  671. Otherwise, move forward to the end of the first Octave code line which
  672. does not end in `...' or `\\' or is inside an open parenthesis list."
  673. (interactive)
  674. (end-of-line)
  675. (if (save-excursion
  676. (beginning-of-line)
  677. (looking-at "\\s-*\\($\\|\\s<\\)"))
  678. ()
  679. (while (or (condition-case nil
  680. (progn
  681. (up-list 1)
  682. (end-of-line)
  683. t)
  684. (error nil))
  685. (and (save-excursion
  686. (beginning-of-line)
  687. (or (looking-at "\\s-*\\($\\|\\s<\\)")
  688. (looking-at octave-continuation-regexp)))
  689. (zerop (forward-line 1)))))
  690. (end-of-line)))
  691. (defun octave-mark-block ()
  692. "Put point at the beginning of this Octave block, mark at the end.
  693. The block marked is the one that contains point or follows point."
  694. (interactive)
  695. (unless (or (looking-at "\\s(")
  696. (save-excursion
  697. (let* ((token (funcall smie-forward-token-function))
  698. (level (assoc token smie-grammar)))
  699. (and level (null (cadr level))))))
  700. (backward-up-list 1))
  701. (mark-sexp))
  702. (defun octave-beginning-of-defun (&optional arg)
  703. "Move backward to the beginning of an Octave function.
  704. With positive ARG, do it that many times. Negative argument -N means
  705. move forward to Nth following beginning of a function.
  706. Returns t unless search stops at the beginning or end of the buffer."
  707. (let* ((arg (or arg 1))
  708. (inc (if (> arg 0) 1 -1))
  709. (found nil)
  710. (case-fold-search nil))
  711. (and (not (eobp))
  712. (not (and (> arg 0) (looking-at "\\_<function\\_>")))
  713. (skip-syntax-forward "w"))
  714. (while (and (/= arg 0)
  715. (setq found
  716. (re-search-backward "\\_<function\\_>" inc)))
  717. (if (octave-not-in-string-or-comment-p)
  718. (setq arg (- arg inc))))
  719. (if found
  720. (progn
  721. (and (< inc 0) (goto-char (match-beginning 0)))
  722. t))))
  723. ;;; Filling
  724. (defun octave-auto-fill ()
  725. "Perform auto-fill in Octave mode.
  726. Returns nil if no feasible place to break the line could be found, and t
  727. otherwise."
  728. (let (fc give-up)
  729. (if (or (null (setq fc (current-fill-column)))
  730. (save-excursion
  731. (beginning-of-line)
  732. (and auto-fill-inhibit-regexp
  733. (octave-looking-at-kw auto-fill-inhibit-regexp))))
  734. nil ; Can't do anything
  735. (if (and (not (octave-in-comment-p))
  736. (> (current-column) fc))
  737. (setq fc (- fc (+ (length octave-continuation-string) 1))))
  738. (while (and (not give-up) (> (current-column) fc))
  739. (let* ((opoint (point))
  740. (fpoint
  741. (save-excursion
  742. (move-to-column (+ fc 1))
  743. (skip-chars-backward "^ \t\n")
  744. ;; If we're at the beginning of the line, break after
  745. ;; the first word
  746. (if (bolp)
  747. (re-search-forward "[ \t]" opoint t))
  748. ;; If we're in a comment line, don't break after the
  749. ;; comment chars
  750. (if (save-excursion
  751. (skip-syntax-backward " <")
  752. (bolp))
  753. (re-search-forward "[ \t]" (line-end-position)
  754. 'move))
  755. ;; If we're not in a comment line and just ahead the
  756. ;; continuation string, don't break here.
  757. (if (and (not (octave-in-comment-p))
  758. (looking-at
  759. (concat "\\s-*"
  760. (regexp-quote
  761. octave-continuation-string)
  762. "\\s-*$")))
  763. (end-of-line))
  764. (skip-chars-backward " \t")
  765. (point))))
  766. (if (save-excursion
  767. (goto-char fpoint)
  768. (not (or (bolp) (eolp))))
  769. (let ((prev-column (current-column)))
  770. (if (save-excursion
  771. (skip-chars-backward " \t")
  772. (= (point) fpoint))
  773. (progn
  774. (octave-maybe-insert-continuation-string)
  775. (indent-new-comment-line t))
  776. (save-excursion
  777. (goto-char fpoint)
  778. (octave-maybe-insert-continuation-string)
  779. (indent-new-comment-line t)))
  780. (if (>= (current-column) prev-column)
  781. (setq give-up t)))
  782. (setq give-up t))))
  783. (not give-up))))
  784. (defun octave-fill-paragraph (&optional _arg)
  785. "Fill paragraph of Octave code, handling Octave comments."
  786. ;; FIXME: difference with generic fill-paragraph:
  787. ;; - code lines are only split, never joined.
  788. ;; - \n that end comments are never removed.
  789. ;; - insert continuation marker when splitting code lines.
  790. (interactive "P")
  791. (save-excursion
  792. (let ((end (progn (forward-paragraph) (copy-marker (point) t)))
  793. (beg (progn
  794. (forward-paragraph -1)
  795. (skip-chars-forward " \t\n")
  796. (beginning-of-line)
  797. (point)))
  798. (cfc (current-fill-column))
  799. comment-prefix)
  800. (goto-char beg)
  801. (while (< (point) end)
  802. (condition-case nil
  803. (indent-according-to-mode)
  804. (error nil))
  805. (move-to-column cfc)
  806. ;; First check whether we need to combine non-empty comment lines
  807. (if (and (< (current-column) cfc)
  808. (octave-in-comment-p)
  809. (not (save-excursion
  810. (beginning-of-line)
  811. (looking-at "^\\s-*\\s<+\\s-*$"))))
  812. ;; This is a nonempty comment line which does not extend
  813. ;; past the fill column. If it is followed by a nonempty
  814. ;; comment line with the same comment prefix, try to
  815. ;; combine them, and repeat this until either we reach the
  816. ;; fill-column or there is nothing more to combine.
  817. (progn
  818. ;; Get the comment prefix
  819. (save-excursion
  820. (beginning-of-line)
  821. (while (and (re-search-forward "\\s<+")
  822. (not (octave-in-comment-p))))
  823. (setq comment-prefix (match-string 0)))
  824. ;; And keep combining ...
  825. (while (and (< (current-column) cfc)
  826. (save-excursion
  827. (forward-line 1)
  828. (and (looking-at
  829. (concat "^\\s-*"
  830. comment-prefix
  831. "\\S<"))
  832. (not (looking-at
  833. (concat "^\\s-*"
  834. comment-prefix
  835. "\\s-*$"))))))
  836. (delete-char 1)
  837. (re-search-forward comment-prefix)
  838. (delete-region (match-beginning 0) (match-end 0))
  839. (fixup-whitespace)
  840. (move-to-column cfc))))
  841. ;; We might also try to combine continued code lines> Perhaps
  842. ;; some other time ...
  843. (skip-chars-forward "^ \t\n")
  844. (delete-horizontal-space)
  845. (if (or (< (current-column) cfc)
  846. (and (= (current-column) cfc) (eolp)))
  847. (forward-line 1)
  848. (if (not (eolp)) (insert " "))
  849. (or (octave-auto-fill)
  850. (forward-line 1))))
  851. t)))
  852. ;;; Completions
  853. (defun octave-initialize-completions ()
  854. "Create an alist for Octave completions."
  855. (if octave-completion-alist
  856. ()
  857. (setq octave-completion-alist
  858. (append octave-reserved-words
  859. octave-text-functions
  860. octave-variables))))
  861. (defun octave-completion-at-point-function ()
  862. "Find the text to complete and the corresponding table."
  863. (let* ((beg (save-excursion (skip-syntax-backward "w_") (point)))
  864. (end (point)))
  865. (if (< beg (point))
  866. ;; Extend region past point, if applicable.
  867. (save-excursion (skip-syntax-forward "w_")
  868. (setq end (point))))
  869. (list beg end octave-completion-alist)))
  870. (define-obsolete-function-alias 'octave-complete-symbol
  871. 'completion-at-point "24.1")
  872. ;;; Electric characters && friends
  873. (defun octave-abbrev-start ()
  874. "Start entering an Octave abbreviation.
  875. If Abbrev mode is turned on, typing ` (grave accent) followed by ? or
  876. \\[help-command] lists all Octave abbrevs. Any other key combination is
  877. executed normally.
  878. Note that all Octave mode abbrevs start with a grave accent."
  879. (interactive)
  880. (if (not abbrev-mode)
  881. (self-insert-command 1)
  882. (let (c)
  883. (insert last-command-event)
  884. (if (if (featurep 'xemacs)
  885. (or (eq (event-to-character (setq c (next-event))) ??)
  886. (eq (event-to-character c) help-char))
  887. (or (eq (setq c (read-event)) ??)
  888. (eq c help-char)))
  889. (let ((abbrev-table-name-list '(octave-abbrev-table)))
  890. (list-abbrevs))
  891. (setq unread-command-events (list c))))))
  892. (define-skeleton octave-insert-defun
  893. "Insert an Octave function skeleton.
  894. Prompt for the function's name, arguments and return values (to be
  895. entered without parens)."
  896. (let* ((defname (substring (buffer-name) 0 -2))
  897. (name (read-string (format "Function name (default %s): " defname)
  898. nil nil defname))
  899. (args (read-string "Arguments: "))
  900. (vals (read-string "Return values: ")))
  901. (format "%s%s (%s)"
  902. (cond
  903. ((string-equal vals "") vals)
  904. ((string-match "[ ,]" vals) (concat "[" vals "] = "))
  905. (t (concat vals " = ")))
  906. name
  907. args))
  908. \n "function " > str \n \n
  909. octave-block-comment-start "usage: " str \n
  910. octave-block-comment-start \n octave-block-comment-start
  911. \n _ \n
  912. "endfunction" > \n)
  913. ;;; Communication with the inferior Octave process
  914. (defun octave-kill-process ()
  915. "Kill inferior Octave process and its buffer."
  916. (interactive)
  917. (if inferior-octave-process
  918. (progn
  919. (process-send-string inferior-octave-process "quit;\n")
  920. (accept-process-output inferior-octave-process)))
  921. (if inferior-octave-buffer
  922. (kill-buffer inferior-octave-buffer)))
  923. (defun octave-show-process-buffer ()
  924. "Make sure that `inferior-octave-buffer' is displayed."
  925. (interactive)
  926. (if (get-buffer inferior-octave-buffer)
  927. (display-buffer inferior-octave-buffer)
  928. (message "No buffer named %s" inferior-octave-buffer)))
  929. (defun octave-hide-process-buffer ()
  930. "Delete all windows that display `inferior-octave-buffer'."
  931. (interactive)
  932. (if (get-buffer inferior-octave-buffer)
  933. (delete-windows-on inferior-octave-buffer)
  934. (message "No buffer named %s" inferior-octave-buffer)))
  935. (defun octave-send-region (beg end)
  936. "Send current region to the inferior Octave process."
  937. (interactive "r")
  938. (inferior-octave t)
  939. (let ((proc inferior-octave-process)
  940. (string (buffer-substring-no-properties beg end))
  941. line)
  942. (with-current-buffer inferior-octave-buffer
  943. (setq inferior-octave-output-list nil)
  944. (while (not (string-equal string ""))
  945. (if (string-match "\n" string)
  946. (setq line (substring string 0 (match-beginning 0))
  947. string (substring string (match-end 0)))
  948. (setq line string string ""))
  949. (setq inferior-octave-receive-in-progress t)
  950. (inferior-octave-send-list-and-digest (list (concat line "\n")))
  951. (while inferior-octave-receive-in-progress
  952. (accept-process-output proc))
  953. (insert-before-markers
  954. (mapconcat 'identity
  955. (append
  956. (if octave-send-echo-input (list line) (list ""))
  957. (mapcar 'inferior-octave-strip-ctrl-g
  958. inferior-octave-output-list)
  959. (list inferior-octave-output-string))
  960. "\n")))))
  961. (if octave-send-show-buffer
  962. (display-buffer inferior-octave-buffer)))
  963. (defun octave-send-block ()
  964. "Send current Octave block to the inferior Octave process."
  965. (interactive)
  966. (save-excursion
  967. (octave-mark-block)
  968. (octave-send-region (point) (mark))))
  969. (defun octave-send-defun ()
  970. "Send current Octave function to the inferior Octave process."
  971. (interactive)
  972. (save-excursion
  973. (mark-defun)
  974. (octave-send-region (point) (mark))))
  975. (defun octave-send-line (&optional arg)
  976. "Send current Octave code line to the inferior Octave process.
  977. With positive prefix ARG, send that many lines.
  978. If `octave-send-line-auto-forward' is non-nil, go to the next unsent
  979. code line."
  980. (interactive "P")
  981. (or arg (setq arg 1))
  982. (if (> arg 0)
  983. (let (beg end)
  984. (beginning-of-line)
  985. (setq beg (point))
  986. (octave-next-code-line (- arg 1))
  987. (end-of-line)
  988. (setq end (point))
  989. (if octave-send-line-auto-forward
  990. (octave-next-code-line 1))
  991. (octave-send-region beg end))))
  992. (defun octave-eval-print-last-sexp ()
  993. "Evaluate Octave sexp before point and print value into current buffer."
  994. (interactive)
  995. (inferior-octave t)
  996. (let ((standard-output (current-buffer))
  997. (print-escape-newlines nil)
  998. (opoint (point)))
  999. (terpri)
  1000. (prin1
  1001. (save-excursion
  1002. (forward-sexp -1)
  1003. (inferior-octave-send-list-and-digest
  1004. (list (concat (buffer-substring-no-properties (point) opoint)
  1005. "\n")))
  1006. (mapconcat 'identity inferior-octave-output-list "\n")))
  1007. (terpri)))
  1008. ;;; Bug reporting
  1009. (defun octave-submit-bug-report ()
  1010. "Submit a bug report on the Emacs Octave package via mail."
  1011. (interactive)
  1012. (require 'reporter)
  1013. (and
  1014. (y-or-n-p "Do you want to submit a bug report? ")
  1015. (reporter-submit-bug-report
  1016. octave-maintainer-address
  1017. (concat "Emacs version " emacs-version)
  1018. (list
  1019. 'octave-blink-matching-block
  1020. 'octave-block-offset
  1021. 'octave-comment-char
  1022. 'octave-continuation-offset
  1023. 'octave-continuation-string
  1024. 'octave-send-echo-input
  1025. 'octave-send-line-auto-forward
  1026. 'octave-send-show-buffer))))
  1027. ;; provide ourself
  1028. (provide 'octave-mod)
  1029. ;;; octave-mod.el ends here