From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms8.migadu.com with LMTPS id 8GTVJR+hz2VDugAAqHPOHw:P1 (envelope-from ) for ; Fri, 16 Feb 2024 18:53:35 +0100 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id 8GTVJR+hz2VDugAAqHPOHw (envelope-from ) for ; Fri, 16 Feb 2024 18:53:35 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=iJ89Ssf1; dmarc=pass (policy=none) header.from=gmail.com; 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" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1708106015; 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=pBeBNLWYxkb6NLRdJmFBtHSKA59LG5CKJXGBxRnmqI8=; b=oJKK00aOpGVZQgToOatZBo32zHyYvJd+e48Qq91rtKJZwyQTxW8hJ9dcno9FVJ/NK3SrTz 0ap28jCxqRYLRI2jt6jcrlIPfIqSU8z4CtoLCmqYszvmZGfqWZKUlq5VH3BWwCBBcAdKDu mJ7CtAxJ6P/sXaW1zhECP64FN2EnTt/tH44KqP+E9+Uj04mXMj/RvTrKlYfQIZFudsChYm P2Ga1vyxSKJA4MgYC5XfjCtMZHN3xHE42PiF9dAE9BSiGe9N6df7PRJHGxJc5VjZtOrVmA cD+LxLAnjw1wihcuPw36IGcNc1JsouGBLC5OWp3FnDR+4y7RmvA/eHvfqfcGMA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=iJ89Ssf1; dmarc=pass (policy=none) header.from=gmail.com; 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" ARC-Seal: i=1; s=key1; d=yhetil.org; t=1708106015; a=rsa-sha256; cv=none; b=rvbAdNNoU9hHGhTXC9sgXnqaSEt64lk7CH0dSTRbLMN2Bv/ilZ3fMtJI6V77eNfXBb9aVb vzLWeLkpOm4qz1vtVo50Yqt6lE7D3a22GEJXd0mzX9hUZDxs0A1CNd3sjrM/lJpPV6bgns ShH/E9nOinlQmROwsiDRPAFtErtcYp550aXbzE7nkdZJyuQ2a2OArOLp27DIMHf4kPc/l5 a/CHzxnjuSwLibiOAbdNls9SvPhWW2HSd11ds3mCG/IYdVoEWUVVw+kwUGIXgUouMSvNxs aiTlmOL/qO/myZGDWpq4xE7WQRf4W4SalOIcQdUxXE7Xu6RFIAFwhN0K/Z4gxw== 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 267B23B012 for ; Fri, 16 Feb 2024 18:53:35 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rb2O6-0008B7-31; Fri, 16 Feb 2024 12:52:38 -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 1rb2O2-0008Ar-Mm for emacs-orgmode@gnu.org; Fri, 16 Feb 2024 12:52:35 -0500 Received: from mail-lj1-x231.google.com ([2a00:1450:4864:20::231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rb2Nw-0001Yp-So for emacs-orgmode@gnu.org; Fri, 16 Feb 2024 12:52:34 -0500 Received: by mail-lj1-x231.google.com with SMTP id 38308e7fff4ca-2d0aabed735so29048881fa.0 for ; Fri, 16 Feb 2024 09:52:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708105945; x=1708710745; darn=gnu.org; h=mime-version:date:references:in-reply-to:subject:cc:to:from :message-id:from:to:cc:subject:date:message-id:reply-to; bh=pBeBNLWYxkb6NLRdJmFBtHSKA59LG5CKJXGBxRnmqI8=; b=iJ89Ssf1VDE/DFZ9815TMnVMwHdL/Nnu04yPkr+Ji/JRxMtl1qpR0y2jvZlxOWTu2i t36etRZnJHrfyGlVoSW3axP/sanNkKKcoScc1/h2nG0BC8JsdlciEk7fV5dLX61yRuIL dFq5Pj/t3qmnf/CjIMpr8bcihEFePQ+Xgz3IXeweiilaHlCQHuIpVyqe/q8BxgAnBfoC gz3xTgM+NXADUDC+Gc/gi8iQbsMfPZBQqSDvv89eit9+5zOuV+ZkLRa+9Gf/8OgLtqFl HSlK+xU9VRFhlnlbyl/PJsAZGHKvqeSLGn5dFXcgUZMlau78euTDmdwmbFbEjSq+/i2J LW/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708105945; x=1708710745; h=mime-version:date:references:in-reply-to:subject:cc:to:from :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=pBeBNLWYxkb6NLRdJmFBtHSKA59LG5CKJXGBxRnmqI8=; b=r4a2Ako5re82P6CrqlqtCLws6uXeJmbs+zev4zkO+Y8DRgbsc74d5T8QhfSHJI3482 uEZvxW4BNWdeQnSO7bh1HtKbOlJ8BqKOmgVniavi1fpg0imIt/kqY7T85BI7k3m+rlk0 37dGZHsKJEroWt3DE9xSKSXv8TadzZLBz00ac0WbJlcAWRKFCLk0IJo4Q4YjWbi+HS/N sPWtTK6Ro7zfvPMoPLIqXD04/IgQMWh2/oCls41uWCdXIfwdpT3bRpK8nzMSI+Mqa0Af pHxGhEE+2+oKLobT51BUmxsVgHwoaF3CPVR6XupDNV8YY2ESZDv03+h8ZOPun+BhjXBY AVvg== X-Forwarded-Encrypted: i=1; AJvYcCWTgUrXpTzb3JaToavkUJHW/oBE4L3a8R67eWqk+2NduMOkjXevf0XTG/5nb0sWGh2xT7VJ89hDxsHsW6dDKOQ7H2IrSk8= X-Gm-Message-State: AOJu0YzpB3+mCnEHB2bojfwqQoGg2gIAk1E6zjYq/UZSzw3H1ByVpjgv hAz9Wi+/F04oi9FEhdUW6ShTl2ra8nBi4FilGGfwV34/ne3zqhmj X-Google-Smtp-Source: AGHT+IHAdtds2C5jfKQXv8JSvYZKEx2UHC1MByP102tAbRkmzqIhJGd4KZNpTWdjpZQtU3e4P0NyeA== X-Received: by 2002:a2e:be9b:0:b0:2d2:200e:3bc with SMTP id a27-20020a2ebe9b000000b002d2200e03bcmr941324ljr.53.1708105944858; Fri, 16 Feb 2024 09:52:24 -0800 (PST) Received: from keynux ([2a01:e0a:505:3460:1c18:688d:ece4:372e]) by smtp.gmail.com with ESMTPSA id l8-20020a05600c1d0800b00410c04e5455sm3037637wms.20.2024.02.16.09.52.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 09:52:24 -0800 (PST) Message-ID: <65cfa0d8.050a0220.cb569.ce34@mx.google.com> Received: by keynux (sSMTP sendmail emulation); Fri, 16 Feb 2024 18:52:22 +0100 From: Bruno Barbier To: Matt Cc: Ihor Radchenko , Jack Kamm , emacs-orgmode Subject: Re: Asynchronous blocks for everything (was Re: [BUG] Unexpected result when evaluating python src block asynchronously [9.7-pre (release_9.6.17-1131-gc9ed03.dirty @ /home/yantar92/.emacs.d/straight/build/org/)]) In-Reply-To: <18dae5cab1d.bf1c7563863897.4896289306902277373@excalamus.com> References: <87o7d0mm54.fsf@localhost> <65bbb108.050a0220.b60fd.6790@mx.google.com> <87jznm8hcu.fsf@gmail.com> <875xz42rp9.fsf@localhost> <874jen8zec.fsf@gmail.com> <87o7cv9e80.fsf@localhost> <65c2875f.050a0220.caf6d.8291@mx.google.com> <18dae5cab1d.bf1c7563863897.4896289306902277373@excalamus.com> Date: Fri, 16 Feb 2024 18:52:22 +0100 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2a00:1450:4864:20::231; envelope-from=brubar.cs@gmail.com; helo=mail-lj1-x231.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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_FROM=0.001, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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-Spam-Score: -6.73 X-Spam-Score: -6.73 X-Migadu-Queue-Id: 267B23B012 X-Migadu-Scanner: mx13.migadu.com X-TUID: whxAxVYtTtXI --=-=-= Content-Type: text/plain Hi Matt, Jack, Ihor, Sorry for the late reply. Cleaning the code took me longer than expected. Jack Kamm writes: > Bruno Barbier writes: > >> FWIW, I've been trying to use asynchronous blocks for everything, not >> only the source blocks that are based on the comint mode. >> ... > > Sounds interesting, a couple questions: > > 1. Which languages have you implemented/tested this on? I'm not using it with official org backends (yet). I'm using it with several custom backends that I'm working on. One of the backend delegate the block executions to emacs subprocesses: so I have a kind of asynchronous executions for free for any language, including elisp itself. > 2. Does it apply for sessions, nonsessions, or both? > The new API itself is more about how to wait for and display one block result. So, it's not really aware of sessions. But, I usually try to think as "no session" as a "one shot" session (like Matt wrote in his email). So, in that sense, it works for both anyway ;-) > Also interesting, I think it's worth exploring/testing this overlay idea > out. Does that mean that output is asynchronously printing into the Org > buffer? It sounds cool but I wonder if it might cause problems while > trying to edit another part of the buffer. Currently, I've limited the progress feedback to fit on one line to avoid anoying vertical display jumps. When the computation is successful, the result is inserted as usual, and, that may be annoying; when it updates a previous result of the same height, it's ok. Else, we could fold it to stay on one line too (using the overlay), until the user explicitly request to see it. Jack Kamm writes: > > That all sounds reasonable...if you work on this, let me know if you > want any help with testing. Thanks. I'll definitely appreciate your help, to test the current code with the Python backend or any other backend if you prefer. Matt writes: > If I followed correctly, the topic switched to discussing async > generally within Babel. I've started a new thread accordingly. Indeed. Thank you! > > FWIW, I've been trying to use asynchronous blocks for everything, not only the source blocks that are based on the comint mode. > > I've been trying to figure out how to make everything async by default. I'm super interested to see what you've come up with. Good to know. Let's we make this happen! > > I think it would be good if ob-core itself could provide an asynchronous API. > > Fortunately or unfortunately, depending on how you look at it, Babel already does. > > The challenge is that the Org Babel API has grown piecemeal over 14 years. It's been written by several authors with limited time and knowledge. The result, while powerful and useful, is a bit of a hodgepodge. A prime example is that the concepts of "persistence" and "synchronicity" are conflated. "Session" is often used to mean "asynchronous" even though the two ideas are orthogonal. Emacs provides primitives that could make non-session blocks asynchronous. It's historical accident that blocks aren't async by default. > For me, the issue is that the Babel API needs some high level perspective in order to make it consistent. > I see the following terms as guides. If we can separate these concepts within the API, then Babel to *feel* like an API: > > - "Session" means a shell environment is "persistent." Each call is executed in the same environment. State exists between calls. > > - "Non-session" means a shell environment is "temporary." Each call is executed in an independent environment. State does not exist between calls. > > - "Synchronous" means that execution prevents the user from editing the document while results are obtained. > > - "Asynchronous" means that execution does not prevent the user from editing the document while results are obtained. I mostly think the same. Sessions (including the "none" session) definitely need some generic API and some generic tests that all backends could just reuse. To execute Python blocks, using the proposed async API: - I've (re)implemented the "asynchronous with session" case (copying/pasting the relevant part from ob-python). - The "synchronous case" is just artificially blocking the user until the asynchronous result is known (which looks incredibly tricky to implement if even possible...). - The "no session" case is just about creating a new unique session and throwing it away immediately. But, some users may rely on some particular behavior, of the current implementations, that may be hard to implement in such a generic way. > The use of the overlay is a really cool idea! > > I hesitate to say that's a good way to convey success or failure. If a process failed, I want to see the output which tells me why so that I can correct it. Or, I might actually want the failure output. Maybe I want to literally demonstrate what a code failure looks like. Maybe I want to use that output in another block. For example, shell blocks have multiple output types. A shell process may return standard output/error or a failure code. The result of the failure may trigger something else. I'm not sure I fully understand what you mean. The API just assumes the backend returns the outcome: either success or failure, where failure means "no result" (the previous result, if it exists, is even preserved in the document). The backend is free to transform a failure into a success to make that result available though. > However, using an overlay to communicate "the process is still running" could be good. We'd need to be careful about accessibility, though, and make sure the overlay is apparent, visually and otherwise. You should be able to test my current implementation, see below. It's almost too visual in my opinion. But, that's probably something that we should make easy to configure. > > If that technique looks safe enough and interesting, I can prepare a set of patches so that we can discuss it further and, maybe, add it in Org. > > Please do! I'm super interested. I've put a lot of thought into how we might make Babel async by default. I'm excited to see your interest in the topic and look forward to seeing what you've come up with. Good to know! So, here we go. You'll find attach a set of patchs. It works for me with Emacsc 30.50 and 9.7-pre (from today). I didn't check yet that the code and the commits follow all the guidelines. This is just a preliminary version for feedbacks. Corrections/critiques are welcome, but don't check the details until I check them myself. The 5 first patchs provide an API to handle asynchronous execution in ob-core, i.e. an API to report progress and to insert results in the asynchronous case. The 5th one isn't really about asynchronicity; but it adds a new keyword `:execute-with' that allows to delegate block executions to some other package; it's useful for testing or plugging other execution engines. That's a cleaned-up version of what I been using myself for a while, with 4 different values for `:execute-with'. The remaining patchs are new code that I've just written to show how to use this new API. I tried first to use it for ob-python, as an example. I just needed to figure out where to place the callbacks ... well ... "just" ... :) So, I decided to rewrite the whole thing, taking code from the synchronous case (following `org-babel-python-evaluate-session'). I also created a package that contains all the functions that should be reusable for any language. The patch [1] adds some generic functions to help dealing with asynchronicity (process, comint, etc.). The patch [2] shows a new possible way to execute python code blocks, both synchronously and asynchronously, with or without sessions. You should just need to open [3] and follow what's written there, and execute the existing bash and python code blocks. Note that this will create a folder `scratch/bba-ob-core-async' in your repository to place the temporary files there. If you know a better way to do this, let me know, thanks. I think the first 5 patchs could be included almost as-is in org. About the emaining ones, I'm not sure exactly how we should proceed. Let me know if you need help to test it. Feedbacks, corrections, critiques, etc are most welcome! Thanks! Bruno [1] lisp/org-elib-async.el: New package about async helpers [2] scratch/bba-ob-core-async: Some temporary test files [3] scratch/bba-ob-core-async/my-async-tests.org --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-ob-core-async-Add-faces-1-5.patch Content-Description: 0001-ob-core-async-Add-faces-1-5.patch >From f67829454ac0d3cd142da1bd0006efa37acce588 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Fri, 16 Feb 2024 14:31:36 +0100 Subject: [PATCH 1/8] ob-core async: Add faces [1/5] lisp/org-faces.el (org-async-scheduled, org-async-pending, org-async-failure): new faces --- lisp/org-faces.el | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lisp/org-faces.el b/lisp/org-faces.el index 0e20de51a..5a8a8fd51 100644 --- a/lisp/org-faces.el +++ b/lisp/org-faces.el @@ -736,6 +736,24 @@ (defface org-mode-line-clock-overrun "Face used for clock display for overrun tasks in mode line." :group 'org-faces) +(defface org-async-scheduled '((t :inherit org-tag :background "gray")) + "Face for babel results for code blocks that are scheduled for execution." + :group 'org-faces + :version "27.2" + :package-version '(Org . "9.5")) + +(defface org-async-pending '((t :inherit org-checkbox :background "dark orange")) + "Face for babel results for code blocks that are running." + :group 'org-faces + :version "27.2" + :package-version '(Org . "9.5")) + +(defface org-async-failure '((t :inherit org-warning)) + "Face for babel results for code blocks that have failed." + :group 'org-faces + :version "27.2" + :package-version '(Org . "9.5")) + (provide 'org-faces) ;;; org-faces.el ends here -- 2.43.0 --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: attachment; filename=0002-ob-core-async-Add-org-babel-async-tools-2-5.patch Content-Transfer-Encoding: quoted-printable Content-Description: 0002-ob-core-async-Add-org-babel-async-tools-2-5.patch >From 8ecdc2159d85648949b477359793f007005bb7ca Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Fri, 16 Feb 2024 14:32:00 +0100 Subject: [PATCH 2/8] ob-core async: Add org-babel--async tools [2/5] --- lisp/ob-core.el | 219 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 219 insertions(+) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index bfeac257b..bb44f91cb 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -792,6 +792,225 @@ (defun org-babel-session-buffer (&optional info) (when (org-babel-comint-buffer-livep buffer-name) buffer-name))) =20 +(defun org-babel--async-status-face (status) + (pcase status + (:scheduled 'org-async-scheduled) + (:pending 'org-async-pending) + (:failure 'org-async-failure) + (:success nil) + (_ (error "Not a status")) + )) + +(defun org-babel--async-make-overlay (beg end) + "Create an overlay between positions BEG and END and return it." + (let ((overlay (make-overlay beg end)) + (read-only + (list + (lambda (&rest _) + (user-error + "Cannot modify an area being updated")))) + ) + (cl-flet ((make-read-only + (ovl) + (overlay-put ovl 'modification-hooks read-only) + (overlay-put ovl 'insert-in-front-hooks read-only) + (overlay-put ovl 'insert-behind-hooks read-only)) + ) + (overlay-put overlay 'org-babel--async-type 'org-babel--async-note) + (overlay-put overlay 'face 'secondary-selection) + (overlay-put overlay 'help-echo "Pending src block result...") + (make-read-only overlay) + overlay))) + +(defun org-babel--async-result-region (inline-elem &optional info) + "Return the region of the results, for the source block at point." + (unless info (setq info (org-babel-get-src-block-info))) + (save-excursion + (when-let ((res-begin (org-babel-where-is-src-block-result nil info))) + (cons res-begin + (save-excursion + (goto-char res-begin) + (if inline-elem + ;; Logic copy/pasted from org-babel-where-is-src-block-r= esult. + (let ((result (org-element-context))) + (and (org-element-type-p result 'macro) + (string=3D (org-element-property :key result) + "results") + (progn + (goto-char (org-element-end result)) + (skip-chars-backward " \t") + (point)))) + ;; Logic copy/pasted from hide-result + (beginning-of-line) + (let ((case-fold-search t)) + (unless (re-search-forward org-babel-result-regexp nil t) + (error "Not looking at a result line"))) + (org-babel-result-end) + )))))) + +(defun org-babel--async-feedbacks (info handle-result + result-params exec-start-time) + "Flag the result as 'scheduled' and return how to handle feedbacks. + +Use overlays to report progress and status to the user. Do not delete +the existing result unless a new one is available. When the result is +available, remove the async overlays and insert the result as usual, +like for a synchronous result. In case of failure, use an overlay to +report the error. + +The returned function handles 3 types of feedbacks: + - (:success R): Evaluation is successful; result is R. + - (:failure ERR): Evaluation failed; error is ERR. + - (:pending P): Outcome still pending; current progress is P." + ;; FIXME: INFO CMD ... Nothing is used but handle-result here !! + (let (;; copy/pasted from org-babel-insert-result + (inline-elem (let ((context (org-element-context))) + (and (memq (org-element-type context) + '(inline-babel-call inline-src-block)) + context))) + result-indent + ) + (cl-labels + ((eot-point (start) + "Move to End Of Title after START" + (if inline-elem + (org-element-end inline-elem) + (save-excursion (goto-char start) + (forward-line 1) (point)))) + (after-indent (pt) + "Move after indentation, starting at PT." + (save-excursion (goto-char pt) (re-search-forward "[[:blank:]]*= "))) + (mk-result-overlays () + ;; Make 2 overlays to handle the pending result: one title + ;; (first line) and one for the body. + (pcase-let ((`(,start . ,end) (org-babel--async-result-region + inline-elem info))) + (let ((anchor-end (eot-point start))) + (cons (org-babel--async-make-overlay + (after-indent start) + (1- anchor-end)) + (org-babel--async-make-overlay + anchor-end end))))) + (add-style (status txt) + ;; Add the style matching STATUS over the text TXT. + (propertize txt 'face (org-babel--async-status-face status))) + + (short-version-of (msg) + ;; Compute the short version of MSG, to display in the header. + ;; Must return a string. + (if msg + (car (split-string (format "%s" msg) "\n" :omit-nulls)) + "")) + (update (ovl-title status msg) + ;; Update the title overlay to match STATUS and MSG. + (let (header) + (overlay-put ovl-title + 'face + (org-babel--async-status-face status) + ) + (overlay-put ovl-title + 'before-string (pcase status + (:scheduled "=E2=8F=B1") + (:pending "=E2=8F=B3") + (:failure "=E2=9D=8C") + (:success "=E2=9C=94=EF=B8=8F")= )) + + (overlay-put ovl-title + 'after-string + (propertize (format " |%s|" + (if (eq :failure status) + (if (consp msg) (car msg) + (format "%s" msg)) + (short-version-of msg))) + 'face (org-babel--async-status-face = status))) + )) + (remove-previous-overlays () + ;; Remove previous title and body overlays. + (mapc (lambda (ovl) + (when (eq 'org-babel--async-note + (overlay-get ovl 'org-babel--async-type)) + (delete-overlay ovl))) + (when-let ((region (org-babel--async-result-region + inline-elem info))) + ;; Not sure why, but we do need to start before + ;; point min, else, in some cases, some overlays + ;; are not found. + (overlays-in (max (1- (car region)) (point-min)) + (cdr region)))))) + + (remove-previous-overlays) + + ;; Ensure there is a non-empty region for the result. + (save-excursion + (unless (org-babel-where-is-src-block-result (not inline-elem) ni= l nil) + (org-babel-insert-result + ;; Use " " for the empty result. That cannot be nil, else it's = interpreted + ;; as a list. We need at least one char, to separate markers if= any. + " \n" + result-params + info nil + (nth 0 info) ; lang + exec-start-time + ))) + + ;; Create the overlays that span the result title and its body. + (pcase-let ((`(,title-ovl . ,body-ovl) (mk-result-overlays))) + ;; Flag the result as ":scheduled". + (update title-ovl :scheduled nil) + + ;; The callback, that runs in the org buffer at point. + (let ((buf (current-buffer)) + (pt (point-marker))) + (lambda (feedback) + (message "ob-core: Handling outcome at %s@%s: %s" pt buf feedb= ack) + (with-current-buffer buf + (save-excursion + (goto-char pt) + (pcase feedback + (`(:success ,r) + ;; Visual beep that the result is available. + (update title-ovl :success r) + (sit-for 0.2) + ;; We remove all overlays and let org insert the result + ;; as it would in the synchronous case. + (delete-overlay title-ovl) + (delete-overlay body-ovl) + (funcall handle-result r)) + + (`(:pending ,r) + ;; Still waiting for the outcome. Update our + ;; overlays with the progress info R. + (message "Updating block at %s@%s" pt buf) + (update title-ovl :pending r)) + + (`(:failure ,err) + ;; We didn't get a result. We update our overlays + ;; to report that failure. And unlock the old + ;; result. + (overlay-put title-ovl 'face nil) + (update title-ovl :failure err) + (delete-overlay body-ovl)) + + (_ (error "Invalid outcome")) + ) + )) + nil)))))) + + +(cl-defun org-babel--async-p (params &key default) + "Return a non-nil value when the execution is asynchronous. +Get the value of the :nasync argument and convert it." + (if-let ((binding (assq :nasync params))) + (pcase (cdr binding) + ((pred (not stringp)) + (error "Invalid value for :nasync argument")) + ((or "no" "n") nil) + ((or "yes" "y") t) + (_ (error "Invalid value for :nasync argument"))) + default)) + + + ;;;###autoload (defun org-babel-execute-src-block (&optional arg info params executor-typ= e) "Execute the current source code block and return the result. --=20 2.43.0 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0003-ob-core-async-Refactor-handle-result-3-5.patch Content-Description: 0003-ob-core-async-Refactor-handle-result-3-5.patch >From e8fe2f922a992de48ca80c52f530b0aa5fb45ea3 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Fri, 16 Feb 2024 14:32:22 +0100 Subject: [PATCH 3/8] ob-core async: Refactor handle-result [3/5] lisp/ob-core.el (org-babel-execute-src-block): Refactor the code to prepare for the next change: move the part handling the result in its own function `handle-result'. --- lisp/ob-core.el | 105 ++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 52 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index bb44f91cb..f8071a534 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -1092,7 +1092,58 @@ (defun org-babel-execute-src-block (&optional arg info params executor-type) (make-directory d 'parents) d)))) (cmd (intern (concat "org-babel-execute:" lang))) - result exec-start-time) + (exec-start-time (current-time)) + (handle-result + (lambda (result) + (setq result + (if (and (eq (cdr (assq :result-type params)) 'value) + (or (member "vector" result-params) + (member "table" result-params)) + (not (listp result))) + (list (list result)) + result)) + (let ((file (and (member "file" result-params) + (cdr (assq :file params))))) + ;; If non-empty result and :file then write to :file. + (when file + ;; If `:results' are special types like `link' or + ;; `graphics', don't write result to `:file'. Only + ;; insert a link to `:file'. + (when (and result + (not (or (member "link" result-params) + (member "graphics" result-params)))) + (with-temp-file file + (insert (org-babel-format-result + result + (cdr (assq :sep params))))) + ;; Set file permissions if header argument + ;; `:file-mode' is provided. + (when (assq :file-mode params) + (set-file-modes file (cdr (assq :file-mode params))))) + (setq result file)) + ;; Possibly perform post process provided its + ;; appropriate. Dynamically bind "*this*" to the + ;; actual results of the block. + (let ((post (cdr (assq :post params)))) + (when post + (let ((*this* (if (not file) result + (org-babel-result-to-file + file + (org-babel--file-desc params result) + 'attachment)))) + (setq result (org-babel-ref-resolve post)) + (when file + (setq result-params (remove "file" result-params)))))) + (unless (member "none" result-params) + (org-babel-insert-result + result result-params info + ;; append/prepend cannot handle hash as we accumulate + ;; multiple outputs together. + (when (member "replace" result-params) new-hash) + lang + (time-subtract (current-time) exec-start-time))) + (run-hooks 'org-babel-after-execute-hook) + result)))) (unless (fboundp cmd) (error "No org-babel-execute function for %s!" lang)) (message "Executing %s %s %s..." @@ -1107,57 +1158,7 @@ (defun org-babel-execute-src-block (&optional arg info params executor-type) (if name (format "(%s)" name) (format "at position %S" (nth 5 info))))) - (setq exec-start-time (current-time) - result - (let ((r (save-current-buffer (funcall cmd body params)))) - (if (and (eq (cdr (assq :result-type params)) 'value) - (or (member "vector" result-params) - (member "table" result-params)) - (not (listp r))) - (list (list r)) - r))) - (let ((file (and (member "file" result-params) - (cdr (assq :file params))))) - ;; If non-empty result and :file then write to :file. - (when file - ;; If `:results' are special types like `link' or - ;; `graphics', don't write result to `:file'. Only - ;; insert a link to `:file'. - (when (and result - (not (or (member "link" result-params) - (member "graphics" result-params)))) - (with-temp-file file - (insert (org-babel-format-result - result - (cdr (assq :sep params))))) - ;; Set file permissions if header argument - ;; `:file-mode' is provided. - (when (assq :file-mode params) - (set-file-modes file (cdr (assq :file-mode params))))) - (setq result file)) - ;; Possibly perform post process provided its - ;; appropriate. Dynamically bind "*this*" to the - ;; actual results of the block. - (let ((post (cdr (assq :post params)))) - (when post - (let ((*this* (if (not file) result - (org-babel-result-to-file - file - (org-babel--file-desc params result) - 'attachment)))) - (setq result (org-babel-ref-resolve post)) - (when file - (setq result-params (remove "file" result-params)))))) - (unless (member "none" result-params) - (org-babel-insert-result - result result-params info - ;; append/prepend cannot handle hash as we accumulate - ;; multiple outputs together. - (when (member "replace" result-params) new-hash) - lang - (time-subtract (current-time) exec-start-time)))) - (run-hooks 'org-babel-after-execute-hook) - result))))))) + (funcall handle-result (save-current-buffer (funcall cmd body params)))))))))) (defun org-babel-expand-body:generic (body params &optional var-lines) "Expand BODY with PARAMS. -- 2.43.0 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0004-ob-core-async-Handle-nasync-param-4-5.patch Content-Description: 0004-ob-core-async-Handle-nasync-param-4-5.patch >From c9d64263af8d9d9b8efc83585323cc6d876018dc Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Fri, 16 Feb 2024 14:32:45 +0100 Subject: [PATCH 4/8] ob-core async: Handle :nasync param [4/5] --- lisp/ob-core.el | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index f8071a534..0f0a36b70 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -1091,7 +1091,10 @@ (defun org-babel-execute-src-block (&optional arg info params executor-type) (let ((d (file-name-as-directory (expand-file-name dir)))) (make-directory d 'parents) d)))) - (cmd (intern (concat "org-babel-execute:" lang))) + (async (org-babel--async-p params)) + (cmd (intern (concat "org-babel-" + (if async "schedule" "execute") + ":" lang))) (exec-start-time (current-time)) (handle-result (lambda (result) @@ -1145,8 +1148,8 @@ (defun org-babel-execute-src-block (&optional arg info params executor-type) (run-hooks 'org-babel-after-execute-hook) result)))) (unless (fboundp cmd) - (error "No org-babel-execute function for %s!" lang)) - (message "Executing %s %s %s..." + (error "No org-babel-execute function for %s: %s!" lang (symbol-name cmd))) + (message "Executing %s %s %s %s..." (capitalize lang) (pcase executor-type ('src-block "code block") @@ -1154,11 +1157,17 @@ (defun org-babel-execute-src-block (&optional arg info params executor-type) ('babel-call "call") ('inline-babel-call "inline call") (e (symbol-name e))) + (if async "async" "") (let ((name (nth 4 info))) (if name (format "(%s)" name) (format "at position %S" (nth 5 info))))) - (funcall handle-result (save-current-buffer (funcall cmd body params)))))))))) + (if (not async) + (funcall handle-result (save-current-buffer (funcall cmd body params))) + (let ((handle-feedback + (org-babel--async-feedbacks info handle-result result-params exec-start-time))) + (funcall cmd body params handle-feedback)))))))))) + (defun org-babel-expand-body:generic (body params &optional var-lines) "Expand BODY with PARAMS. -- 2.43.0 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0005-ob-core-async-Add-execute-with-5-5.patch Content-Description: 0005-ob-core-async-Add-execute-with-5-5.patch >From 8b311516505f693488bfef661905f34e7375539c Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Fri, 16 Feb 2024 14:33:09 +0100 Subject: [PATCH 5/8] ob-core async: Add :execute-with [5/5] --- lisp/ob-core.el | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 0f0a36b70..1de94330a 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -1092,9 +1092,18 @@ (defun org-babel-execute-src-block (&optional arg info params executor-type) (make-directory d 'parents) d)))) (async (org-babel--async-p params)) - (cmd (intern (concat "org-babel-" - (if async "schedule" "execute") - ":" lang))) + (execute-with (let ((be (cdr (assq :execute-with params)))) + (when (equal be "none") (setq be nil)) + be)) + (cmd (intern (or (and execute-with + (concat execute-with "-" (if async "schedule" "execute"))) + (concat "org-babel-" + (if async "schedule" "execute") + ":" lang)))) + (cmd-args (let ((ps (list body params))) + (when execute-with + (setq ps (cons lang ps))) + ps)) (exec-start-time (current-time)) (handle-result (lambda (result) @@ -1163,10 +1172,10 @@ (defun org-babel-execute-src-block (&optional arg info params executor-type) (format "(%s)" name) (format "at position %S" (nth 5 info))))) (if (not async) - (funcall handle-result (save-current-buffer (funcall cmd body params))) + (funcall handle-result (save-current-buffer (apply cmd cmd-args))) (let ((handle-feedback (org-babel--async-feedbacks info handle-result result-params exec-start-time))) - (funcall cmd body params handle-feedback)))))))))) + (apply cmd (nconc cmd-args (list handle-feedback)))))))))))) (defun org-babel-expand-body:generic (body params &optional var-lines) -- 2.43.0 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0006-lisp-org-elib-async.el-New-package-about-async-helpe.patch Content-Description: 0006-lisp-org-elib-async.el-New-package-about-async-helpe.patch >From 50ecb37089b96cd0fe4107e4d74357c151540e34 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Fri, 16 Feb 2024 14:33:23 +0100 Subject: [PATCH 6/8] lisp/org-elib-async.el: New package about async helpers --- lisp/org-elib-async.el | 323 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 323 insertions(+) create mode 100644 lisp/org-elib-async.el diff --git a/lisp/org-elib-async.el b/lisp/org-elib-async.el new file mode 100644 index 000000000..ba4e08869 --- /dev/null +++ b/lisp/org-elib-async.el @@ -0,0 +1,323 @@ +;;; org-elib-async.el --- Helper to write asynchronous functions -*- lexical-binding: t -*- + +;; Copyright (C) 2024 Bruno BARBIER + +;; Author: Bruno BARBIER +;; Version: 0.0.0 +;; Maintainer: Bruno BARBIER +;; Keywords: +;; Status: WORK IN PROGRESS. DO NOT USE. +;; URL: +;; Compatibility: GNU Emacs 30.0.50 +;; +;; This file is NOT (yet) part of GNU Emacs. + +;; This program is free software; you can redistribute it and/or +;; modify it under the terms of the GNU General Public License as +;; published by the Free Software Foundation; either version 2 of +;; the License, or (at your option) any later version. + +;; This program is distributed in the hope that it will be +;; useful, but WITHOUT ANY WARRANTY; without even the implied +;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +;; PURPOSE. See the GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public +;; License along with this program; if not, write to the Free +;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, +;; MA 02111-1307 USA + + +;;; Commentary: +;; Names with "--" are for functions and variables that are meant to be for +;; internal use only. + +;;;; Description +;; Some functions to help dealing with asynchronous tasks. + +;; The prefix 'org-elib' means that this package should evenutally be +;; moved into core Emacs. The functions defined here do NOT depend +;; nor rely on org itself. + +;;; TODOs +;; +;; - Keywords +;; + + +;;; Code: + +;;;; Process +;; +(cl-defun org-elib-async-process (command &key input callback) + "Execute COMMAND. + +A quick naive featureless boggus wrapper around `make-process' to +receive the result when the process is done. + +When INPUT is non-nil, use it as the COMMAND standard input. Let DATA +be the COMMAND output, if COMMAND succeeds, call CALLBACK with +'(:success DATA), else, call CALLBACK with '(:failure DATA)." + (let* ((stdout-buffer (generate-new-buffer "*org-elib-async-process*")) + (get-outcome + (lambda (process) + (with-current-buffer stdout-buffer + (let* ((exit-code (process-exit-status process)) + (real-end ;; Getting rid of the user message. + (progn (goto-char (point-max)) + (forward-line -1) + (point))) + (txt (string-trim (buffer-substring-no-properties + (point-min) real-end)))) + (list (if (eq 0 exit-code) :success :failure) + (if (not (string-empty-p txt)) txt + (and (not (eq 0 exit-code)) exit-code))))))) + (process (make-process + :name "*org-elib-async-process*" + :buffer stdout-buffer + :command command + :connection-type 'pipe)) + (sentinel + (lambda (&rest _whatever) + (pcase (process-status process) + ('run ) + ('stop) + ((or 'exit 'signal) + (funcall callback (funcall get-outcome process))) + (_ (error "Not a real process")))))) + (add-function :after (process-sentinel process) sentinel) + (when input + (process-send-string process input) + (process-send-eof process)) + process)) +;; (org-elib-async-process (list "date") :callback (lambda (o) (message "outcome: %S" o))) +;; (org-elib-async-process (list "false") :callback (lambda (o) (message "outcome: %S" o))) +;; (org-elib-async-process (list "true") :callback (lambda (o) (message "outcome: %S" o))) +;; (org-elib-async-process (list "bash" "-c" "bash") :input "date" :callback (lambda (o) (message "outcome: %S" o))) +;; (org-elib-async-process (list "bash") :input "date" :callback (lambda (o) (message "outcome: %S" o))) +;; (org-elib-async-process (list "bash") :input "false" :callback (lambda (o) (message "outcome: %S" o))) +;; (org-elib-async-process (list "bash") :input "true" :callback (lambda (o) (message "outcome: %S" o))) +;; (org-elib-async-process (list "bash") :input "sleep 2; date" :callback (lambda (o) (message "outcome: %S" o))) + + +;;;; Wait for a process until some condition becomes true. + +(define-error 'org-elib-async-timeout-error + "Timeout waiting for a process.") + +(cl-defun org-elib-async-wait-condition ( cond-p + &key + (tick .3) (message "Waiting") + (nb_secs_between_messages 5) + timeout) + "Wait until the condition COND-P returns non-nil. +Repeatedly call COND-P with no arguments, about every TICK seconds, +until it returns a non-nil value. Return that non-nil value. When +TIMEOUT (seconds) is non-nil, raise an `org-elib-async-timeout-error' if +the COND-P is still nil after TIMEOUT seconds. Assume COND-P calls cost +0s. Do NOT block display updates. Do not block process outputs. Do +not block idle timers. Do block the user, letting him/her know why, but +do not display more messages than one every NB_SECS_BETWEEN_MESSAGES. +Default MESSAGE is \"Waiting\". Use 0.3s as the default for TICK." + ;; FIXME: Still not sure if it's possible to write such a function. + (let ((keep-waiting t) + (result nil) + (start (float-time)) + elapsed + last-elapsed + ) + (while keep-waiting + (setq result (funcall cond-p)) + (if result + (setq keep-waiting nil) + (sleep-for 0.01) + (redisplay :force) + (setq elapsed (- (float-time) start)) + (when (and timeout (> elapsed timeout)) + (signal 'org-timeout-error message elapsed)) + ;; Let the user know, without flooding the message area. + (if (and last-elapsed (> (- elapsed last-elapsed) nb_secs_between_messages)) + (message (format "%s ...(%.1fs)" message elapsed))) + (unless (sit-for tick :redisplay) + ;; Emacs has something to do; let it process new + ;; sub-processes outputs in case there are some. + (accept-process-output nil 0.01)))) + result)) + + + +;;;; Comint: a FIFO queue of tasks with callbacks +;; org-elib-async-comint-queue executes tasks in a FIFO order. For each +;; task, it identifies the text output for that +;; task. org-elib-async-comint-queue does NOT remove prompts, or other +;; useless texts; this is the responsibility of the user. Currently, +;; org-elib-async-comint-queue assume it has the full control of the +;; session: no user interaction, no other direct modifications. + +(defvar-local org-elib-async-comint-queue--todo :NOT-SET + "A FIFO queue of pending executions.") + + +(defvar-local org-elib-async-comint-queue--unused-output "" + "Process output that has not been used yet.") + +(defvar-local org-elib-async-comint-queue--incoming-text "" + "Newly incoming text, added by the process filter, not yet handled.") + +(defvar-local org-elib-async-comint-queue--current-task nil + "The task that is currently running.") + +(defvar-local org-elib-async-comint-queue--process-filter-running nil + "non-nil when filter is running.") + +(defvar-local org-elib-async-comint-queue--incoming-timer nil + "A timer, when handling incoming text is scheduled or running.") + + +(defvar-local org-elib-async-comint-queue--handle-incoming-running + nil + "True when the incoming text handler is running.") + +(defun org-elib-async-comint-queue--handle-incoming () + (when org-elib-async-comint-queue--handle-incoming-running + (error "Bad filter call detected: Kill buffer %s!" (current-buffer))) + (setq org-elib-async-comint-queue--handle-incoming-running t) + + ;; Take the incoming text. + (setq org-elib-async-comint-queue--unused-output + (concat org-elib-async-comint-queue--unused-output + org-elib-async-comint-queue--incoming-text)) + (setq org-elib-async-comint-queue--incoming-text "") + + ;; Process the unused text with the queued tasks + (unless org-elib-async-comint-queue--current-task + (when org-elib-async-comint-queue--todo + (setq org-elib-async-comint-queue--current-task (pop org-elib-async-comint-queue--todo)))) + (when-let ((task org-elib-async-comint-queue--current-task)) + (let ((unused org-elib-async-comint-queue--unused-output) + (session-buffer (current-buffer)) + task-start) + (setq org-elib-async-comint-queue--unused-output + (with-temp-buffer + (insert unused) + (goto-char (point-min)) + (while (and task + (setq task-start (point)) + (search-forward (car task) nil t)) + (when (cdr task) + (let ((txt (buffer-substring-no-properties task-start + (- (point) (length (car task)))))) + (save-excursion (funcall (cdr task) txt)))) + (setq task (and (buffer-live-p session-buffer) + (with-current-buffer session-buffer (pop org-elib-async-comint-queue--todo))))) + (buffer-substring (point) (point-max)))) + (setq org-elib-async-comint-queue--current-task task))) + + ;; Signal that we are done. If we already have some new incoming text, + ;; reschedule to run. + (setq org-elib-async-comint-queue--incoming-timer + (if (string-empty-p org-elib-async-comint-queue--incoming-text) + nil + (org-elib-async-comint-queue--wake-up-handle-incoming))) + + ;; We reset it only on success. If it failed for some reason, the + ;; comint buffer is in an unknown state: you'll need to kill that + ;; buffer. + (setq org-elib-async-comint-queue--handle-incoming-running nil)) + + +(defun org-elib-async-comint-queue--wake-up-handle-incoming () + "Wake up the handling of incoming chunks of text. +Assume we are called from the comint buffer." + (setq org-elib-async-comint-queue--incoming-timer + (run-with-timer + 0.01 nil + (let ((comint-buffer (current-buffer))) + (lambda () + (with-local-quit + (with-current-buffer comint-buffer + (org-elib-async-comint-queue--handle-incoming)))))))) + + +(defun org-elib-async-comint-queue--process-filter (chunk) + "Accept the arbitrary CHUNK of text." + (setq org-elib-async-comint-queue--incoming-text + (concat org-elib-async-comint-queue--incoming-text + chunk)) + :; We delegate the real work outside the process filter, as it is + ; not reliable to do anything here. + (unless org-elib-async-comint-queue--incoming-timer + (org-elib-async-comint-queue--wake-up-handle-incoming))) + + + +(define-error 'org-elib-async-comint-queue-task-error + "Task failure.") + +(cl-defun org-elib-async-comint-queue--push (exec &key handle-feedback) + "Push the execution of EXEC into the FIFO queue. +When the task completed, call HANDLE-FEEDBACK with its outcome. Return +a function that waits for and return the result on succes, raise on +failure." + (let* ((tid (org-id-uuid)) + (start-tag (format "ORG-ELIB-ASYNC_START_%s" tid)) + (end-tag (format "ORG-ELIB-ASYNC_END___%s" tid)) + (result-sb (make-symbol "result")) + (on-start + (lambda (_) + ;; TODO: Use (point) in session to link back to it. + (when handle-feedback + (funcall handle-feedback '(:pending "running"))))) + (on-result + (lambda (result) + ;; Get the result, and report success using HANDLE-FEEDBACK. + ;; If something fails, report failure using HANDLE-FEEDBACK. + (unwind-protect + (let ((outcome + (condition-case-unless-debug exc + (list :success (funcall exec :post-process result)) + (error (list :failure exc))))) + (when handle-feedback (save-excursion (funcall handle-feedback outcome))) + (set result-sb outcome)) + (funcall exec :finally))))) + + (let ((comint-buffer (funcall exec :get-comint-buffer))) + (with-current-buffer comint-buffer + (setq org-elib-async-comint-queue--todo + (nconc org-elib-async-comint-queue--todo + (list (cons start-tag on-start) + (cons end-tag on-result)))) + (funcall exec :send-instrs-to-session + (funcall exec :instrs-to-enter)) + (funcall exec :send-instrs-to-session + (funcall exec :instr-to-emit-tag start-tag)) + (funcall exec :send-instrs-to-session + (funcall exec :get-code)) + (funcall exec :send-instrs-to-session + (funcall exec :instr-to-emit-tag end-tag)) + (funcall exec :send-instrs-to-session + (funcall exec :instrs-to-exit)) + + (lambda () + (org-elib-async-wait-condition (lambda () + (boundp result-sb))) + (pcase (symbol-value result-sb) + (`(:success ,r) r) + (`(:failure ,err) (signal (car err) (cdr err))))) + )))) + + + +(defun org-elib-async-comint-queue-init-if-needed (buffer) + "Initialize the FIFO queue in BUFFER if needed." + (with-current-buffer buffer + (unless (local-variable-p 'org-elib-async-comint-queue--todo) + (setq-local org-elib-async-comint-queue--todo nil) + (add-hook 'comint-output-filter-functions + #'org-elib-async-comint-queue--process-filter nil :local)))) + + + +;;;; Provide +(provide 'org-elib-async) +;;; org-elib-async.el ends here -- 2.43.0 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0007-lisp-ob-core.el-Notify-when-execution-fails.patch Content-Description: 0007-lisp-ob-core.el-Notify-when-execution-fails.patch >From 8db393a06efc3e8a4a3a101be2e59125d1666817 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Fri, 16 Feb 2024 14:33:40 +0100 Subject: [PATCH 7/8] lisp/ob-core.el: Notify when execution fails lisp/ob-core.el (org-babel-popup-failure-details): New function. (org-babel-execute-src-block): For synchronous execution, use `org-babel-popup-failure-details' on failure. (org-babel--async-feedbacks): Add call to `org-babel-popup-failure-details' on user request. --- lisp/ob-core.el | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 1de94330a..c0f6a398a 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -923,6 +923,16 @@ (defun org-babel--async-feedbacks (info handle-result (format "%s" msg)) (short-version-of msg))) 'face (org-babel--async-status-face status))) + (when (eq :failure status) + (overlay-put ovl-title + 'keymap + (let ((km (make-sparse-keymap))) + (define-key km (kbd "") + (lambda () + "Display failure details." + (interactive) + (org-babel-popup-failure-details msg))) + km))) )) (remove-previous-overlays () ;; Remove previous title and body overlays. @@ -1172,11 +1182,26 @@ (defun org-babel-execute-src-block (&optional arg info params executor-type) (format "(%s)" name) (format "at position %S" (nth 5 info))))) (if (not async) - (funcall handle-result (save-current-buffer (apply cmd cmd-args))) + (let ((res-sb (make-symbol "result"))) + (condition-case exc + (set res-sb (save-current-buffer (apply cmd cmd-args))) + (error (org-babel-popup-failure-details exc))) + (when (boundp res-sb) + (funcall handle-result (symbol-value res-sb)))) (let ((handle-feedback (org-babel--async-feedbacks info handle-result result-params exec-start-time))) (apply cmd (nconc cmd-args (list handle-feedback)))))))))))) +(defun org-babel-popup-failure-details (exc) + "Notify/display" + (when-let ((buf (get-buffer org-babel-error-buffer-name))) + (with-current-buffer buf (erase-buffer))) + (org-babel-eval-error-notify + 127 ; Don't have exit-code + (if (consp exc) + (format "%s\n%s\n" (car exc) (cdr exc)) + (format "%s\n" exc)))) + (defun org-babel-expand-body:generic (body params &optional var-lines) "Expand BODY with PARAMS. -- 2.43.0 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0008-scratch-bba-ob-core-async-Some-temporary-test-files.patch Content-Description: 0008-scratch-bba-ob-core-async-Some-temporary-test-files.patch >From 69db5eef49c69287270b1f171e53ddb5e76625dd Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Fri, 16 Feb 2024 14:33:33 +0100 Subject: [PATCH 8/8] scratch/bba-ob-core-async: Some temporary test files --- scratch/bba-ob-core-async/my-async-tests.el | 172 +++++++ scratch/bba-ob-core-async/my-async-tests.org | 484 +++++++++++++++++++ 2 files changed, 656 insertions(+) create mode 100644 scratch/bba-ob-core-async/my-async-tests.el create mode 100644 scratch/bba-ob-core-async/my-async-tests.org diff --git a/scratch/bba-ob-core-async/my-async-tests.el b/scratch/bba-ob-core-async/my-async-tests.el new file mode 100644 index 000000000..3550fc88f --- /dev/null +++ b/scratch/bba-ob-core-async/my-async-tests.el @@ -0,0 +1,172 @@ +;;; my-async-tests.el --- Scratch/temporary file: some tests about async -*- lexical-binding: t -*- + +;; Copyright (C) 2024 Bruno BARBIER + +;; Author: Bruno BARBIER +;; Status: Temporary tests. +;; Compatibility: GNU Emacs 30.0.50 +;; +;; This file is NOT part of GNU Emacs. + +;; This program is free software; you can redistribute it and/or +;; modify it under the terms of the GNU General Public License as +;; published by the Free Software Foundation; either version 2 of +;; the License, or (at your option) any later version. + +;; This program is distributed in the hope that it will be +;; useful, but WITHOUT ANY WARRANTY; without even the implied +;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +;; PURPOSE. See the GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public +;; License along with this program; if not, write to the Free +;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, +;; MA 02111-1307 USA + +(require 'org) +(require 'org-elib-async) + +(defun my-shell-babel-schedule (lang body params handle-feedback) + "Execute the bash script BODY. +Execute the shell script BODY using bash. Use HANDLE-FEEDBACK to report +the outcome (success or failure)." + (unless (equal "bash" lang) + (error "Only for bash")) + (funcall handle-feedback (list :pending "started")) + (org-elib-async-process (list "bash") :input body :callback handle-feedback)) + + +(defun my-org-babel-python-how-to-execute (body params) + "Return how to execute BODY using python. +Return how to execute, as expected by +`org-elib-async-comint-queue--execution'." + ;; Code mostly extracted from ob-python, following + ;; `org-babel-python-evaluate-session'. + ;; Results are expected to differ from ob-python as we follow the + ;; same process for all execution paths: asynchronous or not, with + ;; session or without. + (let* ((org-babel-python-command + (or (cdr (assq :python params)) + org-babel-python-command)) + (session-key (org-babel-python-initiate-session + (cdr (assq :session params)))) + (graphics-file (and (member "graphics" (assq :result-params params)) + (org-babel-graphical-output-file params))) + (result-params (cdr (assq :result-params params))) + (result-type (cdr (assq :result-type params))) + (results-file (when (eq 'value result-type) + (or graphics-file + (org-babel-temp-file "python-")))) + (return-val (when (eq result-type 'value) + (cdr (assq :return params)))) + (preamble (cdr (assq :preamble params))) + (full-body + (concat + (org-babel-expand-body:generic + body params + (org-babel-variable-assignments:python params)) + (when return-val + (format "\n%s" return-val)))) + (post-process + (lambda (r) + (setq r (string-trim r)) + (when (string-prefix-p "Traceback (most recent call last):" r) + (signal 'user-error (list r))) + (when (eq 'value result-type) + (setq r (org-babel-eval-read-file results-file))) + (org-babel-reassemble-table + (org-babel-result-cond result-params + r + (org-babel-python-table-or-string r)) + (org-babel-pick-name (cdr (assq :colname-names params)) + (cdr (assq :colnames params))) + (org-babel-pick-name (cdr (assq :rowname-names params)) + (cdr (assq :rownames params)))))) + (tmp-src-file (org-babel-temp-file "python-")) + (session-body + ;; The real code we evaluate in the session. + (pcase result-type + (`output + (format (string-join + (list "with open('%s') as f:\n" + " exec(compile(f.read(), f.name, 'exec'))\n")) + (org-babel-process-file-name + tmp-src-file 'noquote))) + (`value + ;; FIXME: In this case, any output is an error. + (org-babel-python-format-session-value + tmp-src-file results-file result-params)))) + comint-buffer + finally) + + + (unless session-key + ;; No session. We create a temporary one and use 'finally' to + ;; destroy it once we are done. + ;; + ;; FIXME: This session code should be refactored and moved into + ;; ob-core. + (setq session-key (org-babel-python-initiate-session + ;; We can't use a simple `generate-new-buffer' + ;; due to the earmuffs game. + (org-babel-python-without-earmuffs + (format "*ob-python-no-session-%s*" (org-id-uuid))))) + (setq finally (lambda () + (when-let ((s-buf + (get-buffer (org-babel-python-with-earmuffs session-key)))) + ;; We cannot delete it immediately as we are called from it. + (run-with-idle-timer + 0.1 nil + (lambda () + (when (buffer-live-p s-buf) + (let ((kill-buffer-query-functions nil) + (kill-buffer-hook nil)) + (kill-buffer s-buf))))))))) + + (org-elib-async-comint-queue-init-if-needed session-key) + (setq comint-buffer + (get-buffer (org-babel-python-with-earmuffs session-key))) + (with-temp-file tmp-src-file + (insert (if (and graphics-file (eq result-type 'output)) + (format org-babel-python--output-graphics-wrapper + full-body graphics-file) + full-body))) + + (lambda (&rest q) + (pcase q + (`(:instrs-to-enter) + ;; FIXME: This is wrong. + "import sys; sys.ps1=''; sys.ps2=''") + (`(:instrs-to-exit)) + (`(:finally) (when finally (funcall finally))) + (`(:instr-to-emit-tag ,tag) (format "print ('%s')" tag)) + (`(:post-process ,r) (when post-process (funcall post-process r))) + (`(:send-instrs-to-session ,code) + ;; See org-babel-python-send-string + (with-current-buffer comint-buffer + (let ((python-shell-buffer-name + (org-babel-python-without-earmuffs session-key))) + (python-shell-send-string (concat code "\n"))))) + (`(:get-code) session-body) + (`(:get-comint-buffer) comint-buffer) + (_ (error "Unknown query")))))) + + + +(defun my-org-babel-schedule (lang body params handle-feedback) + "Schedule the execution of BODY according to PARAMS. +This function is called by `org-babel-execute-src-block'. Return a +function that waits and returns the result on success, raise on failure." + (cl-assert (equal "python" lang)) + (let ((exec (my-org-babel-python-how-to-execute body params))) + (org-elib-async-comint-queue--push exec :handle-feedback handle-feedback))) + + +(defun my-org-babel-execute (lang body params) + "Execute Python BODY according to PARAMS. +This function is called by `org-babel-execute-src-block'." + ;; We just start the asynchronous execution, wait for it, and return + ;; the result (or raise the exception). No custom code, and, + ;; synchronous and asynchronous should just mix nicely together. + (cl-assert (equal "python" lang)) + (funcall (my-org-babel-schedule lang body params nil))) diff --git a/scratch/bba-ob-core-async/my-async-tests.org b/scratch/bba-ob-core-async/my-async-tests.org new file mode 100644 index 000000000..263ca77a2 --- /dev/null +++ b/scratch/bba-ob-core-async/my-async-tests.org @@ -0,0 +1,484 @@ +#+PROPERTY: HEADER-ARGS+ :eval no-export :exports both +* Intro + +An org document with code blocks to help test the proposed patches. + +You need to load: + #+begin_src elisp :results silent + (load-file "my-async-tests.el") + #+end_src + + + +Emacs and org versions: + #+begin_src elisp + (mapcar (lambda (sb) (list sb (symbol-value sb))) + '(emacs-version org-version)) + #+end_src + + #+RESULTS: + | emacs-version | 30.0.50 | + | org-version | 9.7-pre | + +Note that we've disabled eval on export: export doesn't know it needs +to wait for asynchronous results. + +* A simple bash example + :PROPERTIES: + :header-args:bash: :execute-with my-shell-babel :nasync yes + :END: + +The package `my-async-tests.el' contains the function +`my-shell-babel-schedule' to evaluate shell script asynchronously. + +The header-args properties above request asynchronous execution for +bash (:nasync yes), and, tells ob-core to use the prefix +`my-shell-babel' when looking for functions to evaluate a source +block. Thus, org will delegate execution to `my-shell-babel-schedule'. +We don't have `my-shell-babel-execute', so, in this case, :nasync must +be yes. + +A simple execution: + #+begin_src bash + date + #+end_src + + #+RESULTS: + : Fri Feb 16 18:11:08 CET 2024 + +A tricky computation takes some time: + #+begin_src bash + sleep 5; date + #+end_src + + #+RESULTS: + : Fri Feb 16 17:58:23 CET 2024 + +An example of a failure: + #+begin_src bash + sleepdd 1; false + #+end_src + + #+RESULTS: + +* Python + :PROPERTIES: + :header-args:python: :execute-with my-org-babel :nasync yes + :header-args:python+: :session py-async + :END: + +Used =header-args= properties: + - =:execute-with my-org-babel=: look for functions with the prefix `my-org-babel' to execute + blocks (for the asynchronous case use + `my-org-babel-schedule', and, for the synchronous case + `my-org-babel-execute'). These functions are defined in [[file:my-async-tests.el]]. + + - =:nasync yes=: by default, execute asynchronously (use `my-org-babel-schedule'). + + - =:session py-async= by default, use a session named "py-async". + +** basic examples +*** async with a session +A very simple test: + #+begin_src python + 2+3 + #+end_src + + #+RESULTS: + : 5 + +Let's import the module time in our session. + #+begin_src python :results silent + import time + #+end_src + + #+RESULTS: + +(Yes, =:results silent= needs some work.) + + + +A table that requires some time to compute: + #+begin_src python + start = time.time() + time.sleep(1) + end = time.time() + ["%.1fs" % t for t in [start, end, end-start]] + #+end_src + + #+RESULTS: + | 1708103472.9s | 1708103473.9s | 1.0s | + + +An error (click on the error , , to see the details): + #+begin_src python + 2/0 + #+end_src + + #+RESULTS: + + +*** async with no session + :PROPERTIES: + :header-args:python+: :session none + :END: + +A very simple test: + #+begin_src python + 2+3 + #+end_src + + #+RESULTS: + : 5 + +Let's import the module time in our session. + #+begin_src python :results silent + import time + #+end_src + + #+RESULTS: + +(Yes, =:results silent= needs some work.) + + + +A table that requires some time to compute: + #+begin_src python + start = time.time() + time.sleep(1) + end = time.time() + ["%.1fs" % t for t in [start, end, end-start]] + #+end_src + + #+RESULTS: + | 1708083470.9s | 1708083471.9s | 1.0s | + +Yes, it failed, as expected. "import time" was done in its own +temporary session. The old result is preserved; the error is display +as an overlay. Click on it to get more info about the error. + + +Let's fix it, adding the import line: + #+begin_src python + import time + start = time.time() + time.sleep(1) + end = time.time() + ["%.1fs" % t for t in [start, end, end-start]] + #+end_src + + #+RESULTS: + | 1708102948.9s | 1708102949.9s | 1.0s | + + +An error (click on the error , , to see the details): + #+begin_src python + 2/0 + #+end_src + + #+RESULTS: + + + +*** sync with a session + :PROPERTIES: + :header-args:python+: :session py-sync-session :nasync no + :END: + +A very simple test: + #+begin_src python + 2+3 + #+end_src + + #+RESULTS: + : 5 + +Let's import the module time in our session. + #+begin_src python :results silent + import time + #+end_src + +(Yes, =:results silent= needs some work.) + + + +A table that requires some time to compute: + #+begin_src python + start = time.time() + time.sleep(1) + end = time.time() + ["%.1fs" % t for t in [start, end, end-start]] + #+end_src + + #+RESULTS: + | 1708102997.5s | 1708102998.5s | 1.0s | + + + +An error (click on the error , , to see the details): + #+begin_src python + 2/0 + #+end_src + + #+RESULTS: + + +*** sync with no session + :PROPERTIES: + :header-args:python+: :session none :nasync no + :END: + +A very simple test: + #+begin_src python + 2+3 + #+end_src + + #+RESULTS: + : 5 + +Let's import the module time in our session. + #+begin_src python :results silent + import time + #+end_src + +(Yes, =:results silent= needs some work.) + + + +A table that requires some time to compute: + #+begin_src python + start = time.time() + time.sleep(1) + end = time.time() + ["%.1fs" % t for t in [start, end, end-start]] + #+end_src + + #+RESULTS: + | 1708083470.9s | 1708083471.9s | 1.0s | + +Yes, that fails (no session), displaying the details in a popup. Let's +fix it: + #+begin_src python + import time + start = time.time() + time.sleep(1) + end = time.time() + ["%.1fs" % t for t in [start, end, end-start]] + #+end_src + + #+RESULTS: + | 1708103039.0s | 1708103040.0s | 1.0s | + + + +An error (click on the error , , to see the details): + #+begin_src python + 2/0 + #+end_src + + #+RESULTS: + + +** worg examples + +Let's import matplotlib in our session. + + #+begin_src python + import matplotlib + import matplotlib.pyplot as plt + #+end_src + + #+RESULTS: + : None + +A figure in a PDF, asynchronous case. + #+begin_src python :results file link + fig=plt.figure(figsize=(3,2)) + plt.plot([1,3,2]) + fig.tight_layout() + + fname = 'myfig-async.pdf' + plt.savefig(fname) + fname # return this to org-mode + #+end_src + + #+RESULTS: + [[file:myfig-async.pdf]] + + +A figure in a PDF, synchronous case. + #+begin_src python :results file link :nasync no + fig=plt.figure(figsize=(3,2)) + plt.plot([1,3,2]) + fig.tight_layout() + + fname = 'myfig-sync.pdf' + plt.savefig(fname) + fname # return this to org-mode + #+end_src + + #+RESULTS: + [[file:myfig-sync.pdf]] + + + +A PNG figure, asynchronous case. + #+begin_src python :results graphics file output :file boxplot.png + fig=plt.figure(figsize=(3,2)) + plt.plot([1,3,2]) + fig.tight_layout() + fig + #+end_src + + #+RESULTS: + [[file:boxplot.png]] + +Same, but using the =:return= keyword. + #+begin_src python :return "plt.gcf()" :results graphics file output :file boxplot.png + fig=plt.figure(figsize=(3,2)) + plt.plot([1,3,2]) + fig.tight_layout() + #+end_src + + #+RESULTS: + [[file:boxplot.png]] + +Same, asynchronous but without a session this time. + #+begin_src python :return "plt.gcf()" :results graphics file output :file boxplot-no-sess-a-y.png :session none + import matplotlib + import matplotlib.pyplot as plt + fig=plt.figure(figsize=(3,2)) + plt.plot([1,3,2]) + fig.tight_layout() + #+end_src + + #+RESULTS: + [[file:boxplot-no-sess-a-y.png]] + + +Lists are table, + #+begin_src python + [1,2,3] + #+end_src + + #+RESULTS: + | 1 | 2 | 3 | + +unless requested otherwise. + #+begin_src python :results verbatim + [1,2,3] + #+end_src + + #+RESULTS: + : [1, 2, 3] + + +Dictionaries are tables too. + #+begin_src python :results table + {"a": 1, "b": 2} + #+end_src + + #+RESULTS: + | a | 1 | + | b | 2 | + + +Let's try the example with Panda. + #+begin_src python :results none + import pandas as pd + import numpy as np + #+end_src + + #+RESULTS: + : None + + #+begin_src python :results table + pd.DataFrame(np.array([[1,2,3],[4,5,6]]), + columns=['a','b','c']) + #+end_src + + #+RESULTS: + | | a | b | c | + |---+---+---+---| + | 0 | 1 | 2 | 3 | + | 1 | 4 | 5 | 6 | + +And the synchronous case? + + #+begin_src python :results table :nasync no + pd.DataFrame(np.array([[1,2,3],[4,5,6]]), + columns=['a','b','c']) + #+end_src + + #+RESULTS: + | | a | b | c | + |---+---+---+---| + | 0 | 1 | 2 | 3 | + | 1 | 4 | 5 | 6 | + + + +Without session ? + + #+begin_src python :results table :session none + pd.DataFrame(np.array([[1,2,3],[4,5,6]]), + columns=['a','b','c']) + #+end_src + + #+RESULTS: + | | a | b | c | + |---+---+---+---| + | 0 | 1 | 2 | 3 | + | 1 | 4 | 5 | 6 | + +Right, we need to import the libraries (no session). + + #+begin_src python :results table :session none + import pandas as pd + import numpy as np + pd.DataFrame(np.array([[1,2,3],[4,5,6]]), + columns=['a','b','c']) + #+end_src + + #+RESULTS: + | | a | b | c | + |---+---+---+---| + | 0 | 1 | 2 | 3 | + | 1 | 4 | 5 | 6 | + + +** inline examples + + A simple asynchronous inline src_python{3*2} {{{results(=6=)}}}. + + An other one containing a mistake src_python{2/0} {{{results(=6=)}}} + (click on the error to see the details). + + + Some very slow inline asynchronous computations that all run in + the same session. You need to execute the 3 of them at once. Here + is the first one src_python[:return "\"OK1\""]{import time; + time.sleep(5)} {{{results(=OK1=)}}} and a second one + src_python[:return "\"OK1 bis\""]{import time; time.sleep(5)} + {{{results(=OK1 bis=)}}} and the third one src_python[:return + "\"OK2\""]{import time; time.sleep(5)} {{{results(=OK2=)}}}. + + Yes, the previous paragraph is unreadable; it's on purpose, to + check that ob-core can figure it out. + + Let's repeat, in a more readable way, and making the last one + synchronous. + + Some very slow inline computations that all run in the same + session. Here is the first asynchronous one + src_python[:return"\"OK1\""]{import time; time.sleep(5)} {{{results(=None=)}}} + and a second one, asynchronous too: + src_python[:return "\"OK1 bis\""]{import time; time.sleep(5)} {{{results(=OK1 bis=)}}} + and finally, a third one, synchronous this one: + src_python[:nasync no :return "\"OK2\""]{import time; time.sleep(5)} {{{results(=OK2=)}}}. + + Note that, once the user executes the last synchronous block, the + user is blocked until the synchronous execution can start + (i.e. all previous asynchronous executions are done) and until + it's done. The display is updated though, to see the asynchronous + progress. -- 2.43.0 --=-=-=--