From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms13.migadu.com with LMTPS id 8F/9G9gnd2e2XgAA62LTzQ:P1 (envelope-from ) for ; Thu, 02 Jan 2025 23:57:12 +0000 Received: from aspmx1.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id 8F/9G9gnd2e2XgAA62LTzQ (envelope-from ) for ; Fri, 03 Jan 2025 00:57:12 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=bEcPJ1aa; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1735862231; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=e1GunuMX9AyZaQZpD+f4XLZOrU8WXh2qBwJUizXITGQ=; b=W5yVX/X2xVNm3cB3vXSROqZl+8G7/mPH65kgVABIUUZc3DpcpnqPgkhP01ngJuOWdTeeNH 3zACNnd3I4jw49fxcRS5qE+nSBfrx7Z/Hy/IkDjhaelC/W+JPGCOgVx1UG1rbAHgbPkuay CX02fNvdW6o7DOBKH5W9FjCiJCBNA5wFNB7mPotdF4EKJ4OR669SDEN9bfFpjS2znZ89rE WaIB0pKwRMnu6ySzdevkDj2smoXqCpL8xH17t9SDrKt2Ydm+WsJP+CpkSiwaiGw+0CuAXV 9y6PfMN1LqZq8ksAbfHWFUndhEoGMMmwVbej6xUY0hKI+RiiHhXCIKXeUh8T3g== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=bEcPJ1aa; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=key1; d=yhetil.org; t=1735862231; a=rsa-sha256; cv=none; b=kmOuZgs6/8i2YX9UUOrKohmKdxRa3xEWbNZi/dlBdvaDrm0/cvCAbyZlGYVtCCthhNMWE+ 2SNo9kM0Xu5YQnWAUXea+WuqT5r932yTKimktceyAQ3RNDb1ibGTyECzMHNc3ANiNF4fUY fYdXCq6MIfti1yBFLtXZWkhUHMpF7nh6jUS7L6Vflz3FEVuKDfVWY9y8C1S7RHwNW8sYA7 QHbEpZe5HPlt9fRXVZt9O5+9GS028h10vJPw47grz6/LOg7i53uwIzhMi3DvtwozfXeHOt 4KykLtnWzb6qIyMHy9N0Iq+JHcmve505+IyRAxUvRIOLN67OK0JLuVD2lGcCmQ== Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 32982831F8 for ; Fri, 03 Jan 2025 00:57:10 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tTV32-0000x6-LW; Thu, 02 Jan 2025 18:56:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tTV31-0000wb-2g for emacs-orgmode@gnu.org; Thu, 02 Jan 2025 18:56:15 -0500 Received: from mail-ed1-x52f.google.com ([2a00:1450:4864:20::52f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tTV2x-0004sZ-Vo for emacs-orgmode@gnu.org; Thu, 02 Jan 2025 18:56:14 -0500 Received: by mail-ed1-x52f.google.com with SMTP id 4fb4d7f45d1cf-5d84179ef26so17399285a12.3 for ; Thu, 02 Jan 2025 15:56:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735862168; x=1736466968; darn=gnu.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=e1GunuMX9AyZaQZpD+f4XLZOrU8WXh2qBwJUizXITGQ=; b=bEcPJ1aalLv4eE+LckTUwXAaVmq88UkBW1fntbKoVvN3pDDYzhdwPiUXMWhNNNzGwh qNGbUPP0nYBUErDOGOsA8BKQkkOG2VkbmhGVLzDI5vFEDG6KTdgG5Q8bollqnu3QUUgx uGSGqc/ly2fcrj6OatW5ldd0FzHa4g9nINR/ObFv2kLfHcq+HdW8c9wPBex6GqNkkzCm ZGMvDM3fRtSOmp1NAC8S5Rgh+4RicBA1Hvpf91mL0XBDCUU6VSocwHNpoSlkJRI40BCV 7j542s3b5g8/UFjebvYT5f6MmBXLkWwgkREhusssCcIeonXjmv2XZ5V/DILdwqE01EOe r4IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735862168; x=1736466968; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=e1GunuMX9AyZaQZpD+f4XLZOrU8WXh2qBwJUizXITGQ=; b=E+7x2FC72K4ULT23y22IljC+JPlX7VfBmFnkg6VD50LQ5iUe3wtv1APKYUHsO4UsUu dtJvAIeCaXc2Y+a4BdH/DY0r/zHk/0fkEItGQ/Wf84VHFFPXC8dSB2dPJN3Rwrk5m8/x 38EEoL0tU+17si1gGvi7EyWixiGbws3i6/yzvFkOMzRtRJ+EMkfb5kOX1rB0mCkAPhms 2SRG+L7G+SNG9jfPX1iLt0wKSWbaWmFpArP0wrgU8n1JM3FHwjeLdwhsuLcqyuIhLrDX F0B3dH8+KPOPYvruvQciHgjain7PAUXBmq+FeNH+Ym/1ZUouoxonIUl7tKiy+nfUWa4V m3/A== X-Gm-Message-State: AOJu0Yznj5a+JtYiGHUpiXtiEiF3UlhMI0am//+AOF8UuW8HjqoAVATc aDY4mv0SS19ilY6/p7jECID7edEfETJyqTIF6Al0mYQORMupfEujse3Fxav1Elxkmptg3dmTpC0 dQCD9XFCvHGQ5vs9dyHaqHi11wPBBPf3T X-Gm-Gg: ASbGncsCAAvKvVewydyQPfVZqVg/OAVb9QTm3jEbsKIIm57lL7RC0ib0RPx9oy2uQiR HLszv9E4tiDPg026cy9VGVFN8BSyqGbiMQx3Bdw== X-Google-Smtp-Source: AGHT+IGquvcApNihm3p5Ff2NAauLhYI00cwItZpzM/BvPzul1spN18FKDPh7lgz0VJdqmuKyHb0kj4IbZ/4Mljx4npU= X-Received: by 2002:a05:6402:270a:b0:5cf:c97c:8206 with SMTP id 4fb4d7f45d1cf-5d81ddfdb2emr35609699a12.25.1735862167651; Thu, 02 Jan 2025 15:56:07 -0800 (PST) MIME-Version: 1.0 References: <87pln4g3b2.fsf@localhost> <87cyim9c4t.fsf@localhost> <87o712lv3t.fsf@localhost> <87msgfvssp.fsf@localhost> In-Reply-To: <87msgfvssp.fsf@localhost> From: John C Date: Thu, 2 Jan 2025 18:55:55 -0500 Message-ID: Subject: Re: ob-octave: improve MATLAB support To: Ihor Radchenko Cc: emacs-orgmode@gnu.org Content-Type: multipart/alternative; boundary="000000000000e1cf46062ac1e715" Received-SPF: pass client-ip=2a00:1450:4864:20::52f; envelope-from=john.ciolfi.32@gmail.com; helo=mail-ed1-x52f.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -9.26 X-Spam-Score: -9.26 X-Migadu-Queue-Id: 32982831F8 X-Migadu-Scanner: mx10.migadu.com X-TUID: ffkESffM81e6 --000000000000e1cf46062ac1e715 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Thanks for the feedback. See updated org-matlab.patch. Note code evaluation will now error out when using an out-of-date matlab-mode (rather than generating a warning) because when using an older matlab-mode, code block evaluation doesn't work. We need to wait for the matlab-shell to be ready. Without that capability found in newer matlab-mode, the errors are very hard to understand. I also added tests. Using steps outlined in the ob-matlab-test.el comment to setup for MATLAB testing, I get on Linux: matlab-mode, version 6.3 Detected MATLAB R2024b (24.2) -- Loading history file matlab-shell: starting server with name server-2055614 passed 127/1250 ob-matlab/results-file-graphics (9.712776 sec) passed 128/1250 ob-matlab/results-file-graphics-with-space (0.332004 sec) passed 129/1250 ob-matlab/results-output (0.013681 sec) passed 130/1250 ob-matlab/results-output-latex (0.459980 sec) passed 131/1250 ob-matlab/results-output-preserve-whitespace (0.281001 sec) passed 132/1250 ob-matlab/results-output-reuse-a (0.039130 sec) passed 133/1250 ob-matlab/results-output-reuse-b (0.043762 sec) passed 134/1250 ob-matlab/results-output-reuse-clear (0.301597 sec) passed 135/1250 ob-matlab/results-verbatim (0.172567 sec) You mentioned that we should expect org users to understand sessions, however the doc on sessions is a bit light. Searching https://orgmode.org/org.html for "session", we see it used two different ways "Emacs session" and babel code block evaluations sessions described in "Using sessions". What is there is okay, but doesn't really help one understand when they should/shouldn't use babel code block evaluation sessions. What would be nice is if "Using sessions" were titled "Babel code block evaluation sessions" containing the existing content minus the "Only languages that provide interactive evaluation ..." paragraph because this is vague. In addition, each language should have a way of describing what :session means to them, perhaps by extracting this info from ob-LANGUAGE.el or maybe ob-LANGUAGE.org, or maybe something else that inserts the language specific into the org manual. With good MATLAB integration in org-mode, we'll see scientists and engineers who have limited programming background leverage org-mode for their scientific papers. Not having to worry about more advanced concepts like sessions is nice, so thanks for letting us default the :session for MATLAB to make it work as one would expect. Here's the commit info: ob-matlab.el: improve MATLAB support * lisp/ob-matlab.el (header): Update URL for MATLAB * lisp/ob-octave.el (org-babel-octave-evaluate): Fixed MATLAB support - Deprecate variables related to MATLAB Emacs Link and removed the code. Emacs Link capability was removed from MATLAB release R2009a, 15 years ago. - Fixed the following type of org block evaluation: 1) #+begin_src matlab :results verbatim 2) #+begin_src matlab :results output 3) #+begin_src matlab :results output latex 4) #+begin_src matlab :results file graphics which aid in writing scientific papers. - Minor point, the correct spelling of MATLAB when referencing the product is all upper case. * lisp/ob-comint.el: enhanced org-babel-comint-with-output - The META argument of org-babel-comint-with-output now supports an optional STRIP-REGEXPS which can be used to remove content from the returned output. * lisp/org.el - Add MATLAB as one of the options for org-babel-load-languages * mk/default.mk - Add support for testing matlab code blocks. * testing/examples/ob-matlab-test.org, testing/lisp/test-ob-matlab.el - Test for matlab code block. * testing/org-test.el - Added org-test-get-code-block which is for use by testing/lisp/test*.el files to extract code blocks from testing/examples/*.org files for on-the-fly testing using org-test-with-temp-text. * etc/ORG-NEWS (New functions and changes in function arguments): Added entry "ob-octave: improved MATLAB support" Thanks John On Sun, Dec 29, 2024 at 2:41=E2=80=AFAM Ihor Radchenko wrote: > John C writes: > > > See attached org-matlab.patch which addresses all feedback. Here's the > > commit info. > > Thanks! > See my comments inline. > > > +*** ob-matlab: fixed MATLAB support > > + > > +Fixed MATLAB babel code blocks processing. MATLAB code blocks, > ~#+begin_src matlab~, with ~:results > > +verbatim~, ~:results output~, ~:results output latex~, or ~:results > file graphics~ now work. Fixes > > +include (1) waiting for matlab-shell to start before evaluating MATLAB > code, (2) correctly showing > > +the results using writematrix, (3) removing the code block lines from > the result, (4) correctly > > +handling graphics results by invoking print correctly. To use MATLAB > with org, you need > > +https://github.com/MathWorks/Emacs-MATLAB-Mode. > > There is no need to provide so many details. > Just leave the most important things: > > 1. MATLAB is no longer broken > 2. Emacs-MATLAB-Mode is required now > > > +*** ob-matlab: MATLAB behavior change > > + > > +MATLAB code blocks now reuse the ~MATLAB*~ buffer created by ~M-x > matlab-shell~, whereas the > > +prior version started a new shell for each evaluation. The benefit of > this is that > > +evaluations are very fast after the first evaluation and that state is > maintained between > > +evaluations, which you can clear using the MATLAB ~clear~ command. > Another benefit of this > > +behavior is that it is consistent with how MATLAB works. > > No need to explain in so much details, I think. > Just say that MATLAB uses session by default and them mention that users > may customize `org-babel-default-header-args:matlab' to disable session. > > > +(defun org-babel-comint--strip-regexps (result strip-regexps) > > + "STRIP-REGEXPS from RESULT list of strings." > > + (dolist (strip-regexp strip-regexps) > > + (let ((new-result '())) > > + (dolist (line result) > > + (setq line (replace-regexp-in-string strip-regexp "" line)) > > + (when (not (string=3D line "")) > > + (setq new-result (append new-result `(,line))))) > > It is more efficient to use `push' + `nreverse' instead of `append'. > > > -(defvar org-babel-default-header-args:matlab '()) > > +;; With `org-babel-default-header-args:matlab' set to > > +;; '((:session . "*MATLAB*"))) > > +;; ... > > +;; If you want a new session each time you evaluate a MATLAB code bloc= k, > > +;; (setq 'org-babel-default-header-args:matlab '()) > > +;; However, this will make evaluations slower and is not consistent > with how > > +;; MATLAB works. MATLAB is designed for many evaluations. > > +(defvar org-babel-default-header-args:matlab '((:session . "*MATLAB*")= )) > > You don't need that long comment in the source code. > If you think that explaining the details about session is necessary (it > may or may not be, but we should assume that Org users are familiar with > the notion of sessions in code blocks), please do it in the > documentation, not in the code. > > More generally, your motivation is not specific to matlab. Yet, we > default to no session in most babel backends. So, it is not a question > of session being faster or slower, but a question of consistency. > > That said, some babel backends do default to session, so I do not oppose > this change too much. > > > +(make-obsolete-variable 'org-babel-matlab-with-emacs-link > > + "MATLAB removed EmacsLink in R2009a." "2009") > > + > > +(make-obsolete-variable 'org-babel-matlab-emacs-link-wrapper-method > > + "MATLAB removed EmacsLink in R2009a." "2009") > > Please use Org version in WHEN argument of `make-obsolete-variable'. > The WHEN should be "9.8". > > > +(defun org-babel-matlab-shell () > > + "Start and/or wait for MATLAB shell." > > + (require 'matlab-shell) ;; make `matlab-shell-busy-checker' availabl= e > > + (cond > > + ((fboundp 'matlab-shell-busy-checker) > > + ;; Start the shell if needed. `matlab-shell' will reuse existing > if already running. > > + (matlab-shell) > > + ;; If we just started the matlab-shell, wait for the prompt. If w= e > do not > > + ;; wait, then the startup messages will show up in the evaluation > results. > > + (matlab-shell-busy-checker 'wait-for-prompt)) > > + (t > > + (message (concat "You version of matlab-mode is old.\n" > > + "Please update, see > https://github.com/mathworks/Emacs-MATLAB-Mode\n" > > + "Updating will eliminate unexpected output in you= r > results\n")) > > + (sit-for 3) > > Instead of `message' + `sit-fit', you can simply use `display-warning'. > It will give users more control. > > > +(defun org-babel-body-for-output (body matlabp) > > + "If MATLABP, fixup BODY for MATLAB output result-type." > > + (when matlabp > > + ;; When we send multi-line input to `matlab-shell', we'll see the > "body" > > + ;; code lines echoed in the output which is not what one would > expect. To > > + ;; remove these unwanted lines, we append a comment "%-" > to each > > + ;; line in the body MATLAB code. After we collect the results fro= m > > + ;; evaluation, we leverage the "%-" to remove the > unwanted lines. > > + ;; Example of desired behavior: > > ... > > I think that an important point here is that MATLAB does not echo the > whole BODY all at once and instead mixes it with the output. Which is > why we need to do something non-standard to filter out the body in > matlab specifically. > > > + (setq body (replace-regexp-in-string "\n" " %--\n" body)= ) > > + (when (not (string-match "\n\\'" body)) > > + (setq body (concat body " %--")))) > > + body) > > Please put this %- into an internal constant and then reuse > it when building the regexp to filter. > > > + (when matlabp > > + '(;; MATLAB echo's all input lines, so use the > %- comments to strip > > + ;; them from the output > > + "^[^\n]*%--\n" > > + ;; Remove starting blank line caused by stripping > %- > > + "\\`[[:space:]\r\n]+" > > + ;; Strip and matlab-shell > error indicators > > + "\n"))) > > Same here. Please put these regexps into a constant. > > -- > Ihor Radchenko // yantar92, > Org mode maintainer, > Learn more about Org mode at . > Support Org development at , > or support my work at > --000000000000e1cf46062ac1e715 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi

Thanks for the feedback. See updated= org-matlab.patch. Note code=C2=A0evaluation will now error out when using = an out-of-date matlab-mode (rather than generating a warning) because when = using an older matlab-mode, code block evaluation doesn't work. We need= to wait for the matlab-shell to be ready. Without that capability found=C2= =A0in newer matlab-mode, the errors are very hard to understand. I also add= ed tests. Using steps outlined in the ob-matlab-test.el comment to setup fo= r MATLAB testing, I get on Linux:

matlab-mode, version 6.3
Dete= cted MATLAB R2024b (24.2) =C2=A0-- Loading history file
matlab-shell: st= arting server with name server-2055614
=C2=A0 =C2=A0passed =C2=A0 127/12= 50 =C2=A0ob-matlab/results-file-graphics (9.712776 sec)
=C2=A0 =C2=A0pas= sed =C2=A0 128/1250 =C2=A0ob-matlab/results-file-graphics-with-space (0.332= 004 sec)
=C2=A0 =C2=A0passed =C2=A0 129/1250 =C2=A0ob-matlab/results-out= put (0.013681 sec)
=C2=A0 =C2=A0passed =C2=A0 130/1250 =C2=A0ob-matlab/r= esults-output-latex (0.459980 sec)
=C2=A0 =C2=A0passed =C2=A0 131/1250 = =C2=A0ob-matlab/results-output-preserve-whitespace (0.281001 sec)
=C2=A0= =C2=A0passed =C2=A0 132/1250 =C2=A0ob-matlab/results-output-reuse-a (0.039= 130 sec)
=C2=A0 =C2=A0passed =C2=A0 133/1250 =C2=A0ob-matlab/results-out= put-reuse-b (0.043762 sec)
=C2=A0 =C2=A0passed =C2=A0 134/1250 =C2=A0ob-= matlab/results-output-reuse-clear (0.301597 sec)
=C2=A0 =C2=A0passed =C2= =A0 135/1250 =C2=A0ob-matlab/results-verbatim (0.172567 sec)

You mentioned that we should expect org users to understand sessio= ns, however the doc on sessions is a bit light. Searching https://orgmode.org/org.html for "session&= quot;, we see it used two different ways "Emacs session" and babe= l code block evaluations sessions described in "Using sessions". = What is there is okay, but doesn't really help one understand when they= should/shouldn't use babel code block evaluation sessions. What would = be nice is if "Using sessions" were titled "Babel code block= evaluation sessions" containing the existing content minus the "= Only languages that provide interactive evaluation ..." paragraph beca= use this is vague. In addition, each language should have a way of describi= ng what :session means to them, perhaps by extracting this info from ob-LAN= GUAGE.el or maybe ob-LANGUAGE.org, or maybe something else that inserts the= language specific into the org manual.

With good MATLAB integration= in org-mode, we'll see scientists and engineers who have limited progr= amming background leverage org-mode for their scientific papers. Not having= to worry about more advanced concepts like sessions is nice, so thanks for= letting us default the :session for MATLAB to make it work as one would ex= pect.

Here's the commit info:
ob-matlab.el: improve MATLAB support<= br>
* lisp/ob-matlab.el (header): Update URL for MATLAB

* lisp/ob= -octave.el (org-babel-octave-evaluate): Fixed MATLAB support
=C2=A0 - De= precate variables related to MATLAB Emacs Link and removed the code.
=C2= =A0 =C2=A0 Emacs Link capability was removed from MATLAB release R2009a, 15= years ago.
=C2=A0 - Fixed the following type of org block evaluation:=C2=A0 =C2=A0 1) #+begin_src matlab :results verbatim
=C2=A0 =C2=A0 2)= #+begin_src matlab :results output
=C2=A0 =C2=A0 3) #+begin_src matlab = :results output latex
=C2=A0 =C2=A0 4) #+begin_src matlab :results file = graphics
=C2=A0 =C2=A0 which aid in writing scientific papers.
=C2=A0= - Minor point, the correct spelling of MATLAB when referencing the product= is
=C2=A0 =C2=A0 all upper case.

* lisp/ob-comint.el: enhanced o= rg-babel-comint-with-output
=C2=A0 - The META argument of org-babel-comi= nt-with-output now supports an optional
=C2=A0 =C2=A0 STRIP-REGEXPS whic= h can be used to remove content from the returned output.

* lisp/org= .el
=C2=A0 - Add MATLAB as one of the options for org-babel-load-languag= es

* mk/default.mk
=C2=A0 - Add= support for testing matlab code blocks.

* testing/examples/ob-matlab-test.org, testing/lisp/test-ob= -matlab.el
=C2=A0 - Test for matlab code block.

* testing/org-tes= t.el
=C2=A0 - Added org-test-get-code-block which is for use by testing/= lisp/test*.el files
=C2=A0 =C2=A0 to extract code blocks from testing/ex= amples/*.org files for on-the-fly
=C2=A0 =C2=A0 testing using org-test-w= ith-temp-text.

* etc/ORG-NEWS (New functions and changes in function= arguments):
=C2=A0 Added entry "ob-octave: improved MATLAB support= "

Thanks
John
=

On Sun, Dec 29, 2024 at 2:41=E2=80=AFAM Iho= r Radchenko <yantar92@posteo.net<= /a>> wrote:
J= ohn C <joh= n.ciolfi.32@gmail.com> writes:

> See attached org-matlab.patch which addresses all feedback. Here's= the
> commit info.

Thanks!
See my comments inline.

> +*** ob-matlab: fixed MATLAB support
> +
> +Fixed MATLAB babel code blocks processing. MATLAB code blocks, ~#+beg= in_src matlab~, with ~:results
> +verbatim~, ~:results output~, ~:results output latex~, or ~:results f= ile graphics~ now work.=C2=A0 Fixes
> +include (1) waiting for matlab-shell to start before evaluating MATLA= B code, (2) correctly showing
> +the results using writematrix, (3) removing the code block lines from= the result, (4) correctly
> +handling graphics results by invoking print correctly. To use MATLAB = with org, you need
> +https://github.com/MathWorks/Emacs-MATLAB-Mode.

There is no need to provide so many details.
Just leave the most important things:

1. MATLAB is no longer broken
2. Emacs-MATLAB-Mode is required now

> +*** ob-matlab: MATLAB behavior change
> +
> +MATLAB code blocks now reuse the ~MATLAB*~ buffer created by ~M-x mat= lab-shell~, whereas the
> +prior version started a new shell for each evaluation.=C2=A0 The bene= fit of this is that
> +evaluations are very fast after the first evaluation and that state i= s maintained between
> +evaluations, which you can clear using the MATLAB ~clear~ command.=C2= =A0 Another benefit of this
> +behavior is that it is consistent with how MATLAB works.

No need to explain in so much details, I think.
Just say that MATLAB uses session by default and them mention that users may customize `org-babel-default-header-args:matlab' to disable session= .

> +(defun org-babel-comint--strip-regexps (result strip-regexps)
> +=C2=A0 "STRIP-REGEXPS from RESULT list of strings."
> +=C2=A0 (dolist (strip-regexp strip-regexps)
> +=C2=A0 =C2=A0 (let ((new-result '()))
> +=C2=A0 =C2=A0 =C2=A0 (dolist (line result)
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq line (replace-regexp-in-string stri= p-regexp "" line))
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 (when (not (string=3D line ""))=
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq new-result (append new-resul= t `(,line)))))

It is more efficient to use `push' + `nreverse' instead of `append&= #39;.

> -(defvar org-babel-default-header-args:matlab '())
> +;; With `org-babel-default-header-args:matlab' set to
> +;;=C2=A0 '((:session . "*MATLAB*")))
> +;; ...
> +;; If you want a new session each time you evaluate a MATLAB code blo= ck,
> +;;=C2=A0 =C2=A0(setq 'org-babel-default-header-args:matlab '(= ))
> +;; However, this will make evaluations slower and is not consistent w= ith how
> +;; MATLAB works.=C2=A0 MATLAB is designed for many evaluations.
> +(defvar org-babel-default-header-args:matlab '((:session . "= *MATLAB*")))

You don't need that long comment in the source code.
If you think that explaining the details about session is necessary (it
may or may not be, but we should assume that Org users are familiar with the notion of sessions in code blocks), please do it in the
documentation, not in the code.

More generally, your motivation is not specific to matlab. Yet, we
default to no session in most babel backends. So, it is not a question
of session being faster or slower, but a question of consistency.

That said, some babel backends do default to session, so I do not oppose this change too much.

> +(make-obsolete-variable 'org-babel-matlab-with-emacs-link
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 "MATLAB removed EmacsLink in R2009a." "2009&q= uot;)
> +
> +(make-obsolete-variable 'org-babel-matlab-emacs-link-wrapper-meth= od
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 "MATLAB removed EmacsLink in R2009a." "2009&q= uot;)

Please use Org version in WHEN argument of `make-obsolete-variable'. The WHEN should be "9.8".

> +(defun org-babel-matlab-shell ()
> +=C2=A0 "Start and/or wait for MATLAB shell."
> +=C2=A0 (require 'matlab-shell) ;; make `matlab-shell-busy-checker= ' available
> +=C2=A0 (cond
> +=C2=A0 =C2=A0((fboundp 'matlab-shell-busy-checker)
> +=C2=A0 =C2=A0 ;; Start the shell if needed.=C2=A0 `matlab-shell' = will reuse existing if already running.
> +=C2=A0 =C2=A0 (matlab-shell)
> +=C2=A0 =C2=A0 ;; If we just started the matlab-shell, wait for the pr= ompt.=C2=A0 If we do not
> +=C2=A0 =C2=A0 ;; wait, then the startup messages will show up in the = evaluation results.
> +=C2=A0 =C2=A0 (matlab-shell-busy-checker 'wait-for-prompt))
> +=C2=A0 =C2=A0(t
> +=C2=A0 =C2=A0 (message (concat "You version of matlab-mode is ol= d.\n"
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0"Please update, see
https://github.com= /mathworks/Emacs-MATLAB-Mode\n"
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0"Updating will eliminate unexpected output in your results\n&qu= ot;))
> +=C2=A0 =C2=A0 (sit-for 3)

Instead of `message' + `sit-fit', you can simply use `display-warni= ng'.
It will give users more control.

> +(defun org-babel-body-for-output (body matlabp)
> +=C2=A0 "If MATLABP, fixup BODY for MATLAB output result-type.&qu= ot;
> +=C2=A0 (when matlabp
> +=C2=A0 =C2=A0 ;; When we send multi-line input to `matlab-shell',= we'll see the "body"
> +=C2=A0 =C2=A0 ;; code lines echoed in the output which is not what on= e would expect.=C2=A0 To
> +=C2=A0 =C2=A0 ;; remove these unwanted lines, we append a comment &qu= ot;%-<org-eval>" to each
> +=C2=A0 =C2=A0 ;; line in the body MATLAB code.=C2=A0 After we collect= the results from
> +=C2=A0 =C2=A0 ;; evaluation, we leverage the "%-<org-eval>= " to remove the unwanted lines.
> +=C2=A0 =C2=A0 ;; Example of desired behavior:
> ...

I think that an important point here is that MATLAB does not echo the
whole BODY all at once and instead mixes it with the output. Which is
why we need to do something non-standard to filter out the body in
matlab specifically.

> +=C2=A0 =C2=A0 (setq body (replace-regexp-in-string "\n" &qu= ot; %-<org-eval>-\n" body))
> +=C2=A0 =C2=A0 (when (not (string-match "\n\\'" body)) > +=C2=A0 =C2=A0 =C2=A0 (setq body (concat body " %-<org-eval>= ;-"))))
> +=C2=A0 body)

Please put this %-<org-eval> into an internal constant and then reuse=
it when building the regexp to filter.

> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(when matlabp<= br> > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0'(;= ; MATLAB echo's all input lines, so use the %-<org-eval> comments= to strip
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= ;; them from the output
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= "^[^\n]*%-<org-eval>-\n"
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= ;; Remove starting blank line caused by stripping %-<org-eval>
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= "\\`[[:space:]\r\n]+"
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= ;; Strip <ERRORTXT> and </ERRORTXT> matlab-shell error indicato= rs
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= "</?ERRORTXT>\n")))

Same here. Please put these regexps into a constant.

--
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,=
or support my work at <https://liberapay.com/yantar92>
--000000000000e1cf46062ac1e715--