From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms13.migadu.com with LMTPS id eLwdJl/9cGdmgAAA62LTzQ:P1 (envelope-from ) for ; Sun, 29 Dec 2024 07:42:23 +0000 Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id eLwdJl/9cGdmgAAA62LTzQ (envelope-from ) for ; Sun, 29 Dec 2024 08:42:23 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=posteo.net header.s=2017 header.b=iYaQezzh; 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=posteo.net ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1735458143; 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=7KRx4PCxjw5EhwaSeXFhUU5wDFqfyrNNiHPiLoIhRmY=; b=o/z1tLtHvbBeT/HANAClyru23hVM85PoVt+ui0l41HHPJisbanA4OsU5oQhkYX2fESfd9s 2DvT/xER/14zJmwI5/EX8+hkeL4AZBG0RsHa/xNS81qBQUK4GkaU1cue+3hzPgI2tYG4EH WWKepo6ARjCyINupPUZKNV5zKNgOh63zEV85EL2Tw7X/TnNxzR70FLeNjyd8xCwvj0ZrHD Y6jv2Faoso0eKnUMGVqKOZMekAB3gXEKjRUr0DrJreNCMXfwrXoJy+jqnUaCNlbACaev+b k/QQpg3xpCP68unM3BWDDh8MurAkiSfpjXLita7sWgXM7cawta+1iIqN6mvv9w== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=posteo.net header.s=2017 header.b=iYaQezzh; 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=posteo.net ARC-Seal: i=1; s=key1; d=yhetil.org; t=1735458143; a=rsa-sha256; cv=none; b=T/7rZtkNiUp6PfcvtNUwgp8PHStJXgO+7f12QsxUN+lLW+nJM4JU6P1GzOKov6uOuMXLXy xpFa9nA9UCspLwVNfXqDr1K1uPSgh2t09SC2/s1d+m+twHQgT1V6Cf8SQtOsG54e4UNzPl 3Rbz0lE9MynQiwYjGlWjTnL4P7K93w8NiHgaXLHiz1ki75xaXjpwa3ibMaZpfiiu3Tbxk3 GP26u+iXVDXYhBgLSZqzHcznmBkFRyr4tnXnor3QsG+Xw4ABD/C6tq3QJesBqe2KHA9zV1 LT/3Qc/Q0d9mlVBsxPlshdwCGZq0+mCP4gLIdrOnmtz3iZbQkgdikSPGIw+FWA== 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 DCF5E7CB9D for ; Sun, 29 Dec 2024 08:42:22 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tRnvT-0003vn-Q5; Sun, 29 Dec 2024 02:41:27 -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 1tRnvQ-0003vM-Lg for emacs-orgmode@gnu.org; Sun, 29 Dec 2024 02:41:25 -0500 Received: from mout01.posteo.de ([185.67.36.65]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tRnvN-0005JO-C5 for emacs-orgmode@gnu.org; Sun, 29 Dec 2024 02:41:24 -0500 Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 86995240027 for ; Sun, 29 Dec 2024 08:41:17 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1735458077; bh=40v1yMypsmo+1US8PC7aZM2Td2pwdxtEdAzw6TdypsQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type: From; b=iYaQezzhPOQTAsZZ29dLYbI76kR3ujxh8mexn0MvTU+WWFUxDWUHAdh2ip9cTIs/S 3L2ewAGNguBcv2MPhdjEKqJ8FV1TzGgsd5p8EcPTxF+P4PoG/GAHOZKQl5D2FdDUPj alZPtJhmIqd9uFgynQ4twAe3SX1YLyOrgbat+eTBGKjfMhHkShqCXXkUxKXzwEWNoc zkn64hXMd3PRMDtPaUXL1IOvZKBvhg4djcMA/ZBd/kt/OwGxrLgEcj3ognLot536qv FVXfMlSyoHxHxU4K1vmV0rOfRp88Sel9ckvyYwJTpjCV5EkbajZqv5nc3hkfQp0PS4 gg45nzfBRyDMg== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4YLWPm6wvXz9rxF; Sun, 29 Dec 2024 08:41:16 +0100 (CET) From: Ihor Radchenko To: John C Cc: emacs-orgmode@gnu.org Subject: Re: ob-octave: improve MATLAB support In-Reply-To: References: <87pln4g3b2.fsf@localhost> <87cyim9c4t.fsf@localhost> <87o712lv3t.fsf@localhost> Date: Sun, 29 Dec 2024 07:42:46 +0000 Message-ID: <87msgfvssp.fsf@localhost> MIME-Version: 1.0 Content-Type: text/plain Received-SPF: pass client-ip=185.67.36.65; envelope-from=yantar92@posteo.net; helo=mout01.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 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, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, 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-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Scanner: mx11.migadu.com X-Migadu-Spam-Score: -8.79 X-Spam-Score: -8.79 X-Migadu-Queue-Id: DCF5E7CB9D X-TUID: UJyKyKrwUovP 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= 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 block, > +;; (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' available > + (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 we 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 your 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 from > + ;; 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