From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms13.migadu.com with LMTPS id QDAdE3P4W2fUjwAAe85BDQ:P1 (envelope-from ) for ; Fri, 13 Dec 2024 09:03:47 +0000 Received: from aspmx1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id QDAdE3P4W2fUjwAAe85BDQ (envelope-from ) for ; Fri, 13 Dec 2024 10:03:47 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HSVT5vif; 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=1734080616; a=rsa-sha256; cv=none; b=XU8d8HmUsHboFi/4tpybrPJo1s9+YJm+7ZUhG4CwzD6G1IzHcsRVV3KLEhfpijeW5ajsqr mzDU/lBHVEHfmf5kFweJTUmYmCi+RnwWdhvy7g5rrqIjAdWQFMO5PXs7b163u8jU5t8uSE 2vz2sS9H2TUxHMWjth7RhReYVUtPb7swwI3bNuWdu8hKOQhR9Ng8jgyDair/5/9SujK7VB GMTSbBKG9AmI39VMKKzfT0IU7MaP1sQw/RMNNvEK/3JmFOpCP5Qi0iOjChH5w48tPSbnr3 0k48wSTjN/i+stZs4yXVwuqO0gVmbQkPuOKW3Tjn9uITuLpTVcqScmYwkq68vA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HSVT5vif; 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=1734080616; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=ddKCZ4OKpAwUhSrzvGZIDj1Sxzira2yrMY9My3jYz58=; b=ixpOHUkqxo9e9Dv+3O5I6Qw2mNok6WNSCLWBPnUTx8uThEW3ELyzCTg6zMsVWPpoiOZKDr xH83uZjRsM7QOijxT1duy1d092ZAsj0chFQitbUz9bktTNx16kS83L6k62SbgVvpzm5aio FcCPTuaMt24k/o80d4XRVmrF6WJkwFkjd7np7mYLBW3/cPc4qiYvUTyKag5P+EhZkPex1i MkkTzF5gq5g89KFAQzxAp5TxJfjo9lxtStn3Xs5X3pvPGNfKOH997LFxiQ0oo+oOzztiKa 0bmwGc0NQZCFjjKrHwjxDn5i2Gzj7IZ4frqQao1/lM+z6QcagyUGXhnkoG2Zmg== 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 77FD886455 for ; Fri, 13 Dec 2024 10:03:36 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tM1Z9-0000j2-4S; Fri, 13 Dec 2024 04:02:31 -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 1tM1Z1-0000i0-Nk for emacs-orgmode@gnu.org; Fri, 13 Dec 2024 04:02:28 -0500 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tM1Yv-0005yZ-JH for emacs-orgmode@gnu.org; Fri, 13 Dec 2024 04:02:22 -0500 Received: by mail-pf1-x441.google.com with SMTP id d2e1a72fcca58-728e3826211so1290266b3a.0 for ; Fri, 13 Dec 2024 01:02:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734080536; x=1734685336; darn=gnu.org; h=content-transfer-encoding:mime-version:user-agent:message-id:date :references:in-reply-to:subject:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ddKCZ4OKpAwUhSrzvGZIDj1Sxzira2yrMY9My3jYz58=; b=HSVT5vifaLLj3czWdkGCjxfGgQZfu6Rpt3GyeAS77JjaeQjXP+gGeX4noX14AEI+P8 pw3f8d7oS8bmFp+k8dSBsbHeVeEsbl6+XvvwGrzj4aIepqsgOa3yzW6JcTXlF+n1ULVr HjKJOFL15L9o/vhr9cPN2oUBtFgRwpjWtGsFdIdr1PNScPIScwEBKb2JDsbHwDkdOo9t kV7sFTms6ZBhzUG9MOtbr4/7hkdFOkwDS4NHJnDyrx7+mf5WrIHYLpZCujLE2VN1rPk3 CVOGvKFnDDRDsj68r1UQGPX4LX7jAsfh7lK/wZiF5j4bG5zvmpVmVx2OFXdq6nQ/3sFM LnRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734080536; x=1734685336; h=content-transfer-encoding:mime-version:user-agent:message-id:date :references:in-reply-to:subject:to:from:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=ddKCZ4OKpAwUhSrzvGZIDj1Sxzira2yrMY9My3jYz58=; b=j4drJjwTUVqK904Vej4y1/FxqkIZ3cJ3hlN4z5FBr+tVO5uKOdVqJrcIfiDv0DbFyh pHPyyld48xAOpP87uyj6Z/mg/YOyLiibWnE2QrugY7b2iPnyVgZLrImaObxw0ziIsm0d 5slIdstXGM2ppFFohF0Q/8GKbyx/oqMg3iukNJQ6BrqllYIKhwcSW3LiPifgswoh4xK/ 1fs6PbDqLm7ZbWk9BHfXo3nRFN/Oq2tqT3Qrus1YdKgg4lIxXnFKRclodt9MxSx9ozMZ Df1D8/0Z++VGJoxDZjzlOAz42y1t1nhxtVTnQe0YgBYOYcTD8026x+/Bz30ZNFxRN6+Y Gtlw== X-Gm-Message-State: AOJu0YzQ58OTFUrIYBm5r3yataDKAgGHZMXuTc29P2zCF8DmpM8vhQ2M 8PBLpYsrJjiNdrexfC959CCRr33wjbM1+SD4/Waj+KCGiqrZCrMiFO17I+f4 X-Gm-Gg: ASbGncvok/4w/XJmW0j48Vmqm1WeUMuagIF867P4/hrXCC/dT0djp7Nr0v1xEFuTEUW zP6VaG38MKbLhHcwsHK26LzD4SYECcZhzydmR3cRyoCCwvRdEVfaNggRnkXg3diFAn8wNtVlt5k 0QFJLiRzhDkA/n57uuoQabYiUED+irHEOpb440hT8LliCkUxNIxo4R+kAJE1Gkc1NzQTpcWlJZx lpk0bJG4JyggJPoPHiPpgV0yk9TH0w0Ff+RIWDYF+tNXzYxr14V7uly3A== X-Google-Smtp-Source: AGHT+IEV/YXyE7vonjLJ+Qw1e746Qs5WTk962gbkOFgmKnoXeNrZidKoTgZKfMboYnshwwSGJLAr+g== X-Received: by 2002:a05:6a21:150a:b0:1e1:c943:4e8e with SMTP id adf61e73a8af0-1e1dfe17f77mr2770699637.41.1734080535643; Fri, 13 Dec 2024 01:02:15 -0800 (PST) Received: from localhost ([1.7.159.70]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-725c91baf30sm11346262b3a.80.2024.12.13.01.02.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Dec 2024 01:02:15 -0800 (PST) From: Visuwesh To: Org Mode List Subject: Re: [PATCH] Add support for tables in Calc src block :var In-Reply-To: <87pllw2g4l.fsf@gmail.com> (Visuwesh's message of "Fri, 13 Dec 2024 13:01:38 +0530") References: <877ci2261h.fsf@gmail.com> <87y1aiijv3.fsf@localhost> <8734sq1k70.fsf@gmail.com> <87bk7eid26.fsf@localhost> <87y1aiz76v.fsf@gmail.com> <87jzlzhfiw.fsf@ucl.ac.uk> <87cyrbeure.fsf@gmail.com> <87sf0239bm.fsf@ucl.ac.uk> <87pllw2g4l.fsf@gmail.com> Date: Fri, 13 Dec 2024 14:32:11 +0530 Message-ID: <87jzc42bxo.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Received-SPF: pass client-ip=2607:f8b0:4864:20::441; envelope-from=visuweshm@gmail.com; helo=mail-pf1-x441.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, 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-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Spam-Score: -2.40 X-Spam-Score: -2.40 X-Migadu-Queue-Id: 77FD886455 X-Migadu-Scanner: mx12.migadu.com X-TUID: IWVgC1BkYmHa [=E0=AE=B5=E0=AF=86=E0=AE=B3=E0=AF=8D=E0=AE=B3=E0=AE=BF =E0=AE=9F=E0=AE=BF= =E0=AE=9A=E0=AE=AE=E0=AF=8D=E0=AE=AA=E0=AE=B0=E0=AF=8D 13, 2024] Visuwesh w= rote: > [...] > Some caveats: > > 1. You need to ensure that all the calc-embedded variables that you > use in the formula need to be active and evaluated beforehand. > 2. The calc-embedded var is considered at last after everything else > in org-table-get-constant in the advice. This would be the best > way to go forward. > > It would be nice to lift the restrict in (1) but I think it would be > better to leave it to the user to ensure everything stays updated since > the user may want to use the old value. However, ensuring all the vars > are active and eval-ed would be a royal pain when you're quickly > evaluating a table formula. > > Here's the case I used to test this hack: > > x :=3D 3 > > y :=3D 5 > > z :=3D 5 x - y =3D> 10 > > > | 1 | 20 | > > #+TBLFM: $2=3D$z*2 OK, this was not so simple as I thought! Consider the following example: x :=3D 3 a :=3D 3 x =3D> 9 y :=3D 5 x :=3D 2 z :=3D 5.2 x - y =3D> 5.4 Update x :=3D 3 and y first. Then update `a' and `z'. `z' will be 10.6. Now update x :=3D 2, and update `z' and `a'. `z' updates to 5.4 as shown above but `a' remains 9! Calc looks for the nearest value of the variable `x' from the definition of `z'. The Elisp variable that stores the var information looks like this: (cdar calc-embedded-active) ([# # # # # # #("x :=3D 3" 0 6 (fontified t)) nil (calcFunc-assign (var x var-x) 3) (var x var-x) nil (calcFunc-assign (var x var-x) 3) nil nil nil nil] [# # # # # # #("a :=3D 3 x =3D> 9" 0 13 (fontified t)) nil (calcFunc-evalto (calcFunc-assign (var a var-a) (* 3 (var x var-x))) 9) (var a var-a) (((var x var-x))) 9 nil nil nil nil] [# # # # # # #("y :=3D 5" 0 6 (fontified t)) nil (calcFunc-assign (var y var-y) 5) (var y var-y) nil (calcFunc-assign (var y var-y) 5) nil nil nil nil] [# # # # # # #("x :=3D 2" 0 6 (fontified t)) nil (calcFunc-assign (var x var-x) 2) (var x var-x) nil (calcFunc-assign (var x var-x) 2) nil nil nil nil] [# # # # # # "z :=3D 5.2 x - y =3D> 5.4" nil (calcFunc-evalto (calcFunc-assign (var z var-z) (- (* (float 52 -1) (var x var-x)) (var y var-y))) (float 54 -1)) (var z var-z) (((var y var-y)) ((var x var-x))) (float 54 -1) nil nil nil nil]) Notice how there are two entries for `x'. Worse still, change x :=3D 3 to x :=3D 3.3 and update it. Evaluating the same expression again now yields three entries for `x'! ([# # # # # # #("x :=3D 3" 0 6 (fontified t)) nil (calcFunc-assign (var x var-x) 3) (var x var-x) nil (calcFunc-assign (var x var-x) 3) nil nil nil nil] [# # # # # # #("x :=3D 3.3" 0 8 (fontified t)) nil (calcFunc-assign (var x var-x) (float 33 -1)) (var x var-x) nil (calcFunc-assign (var x var-x) (float 33 -1)) nil nil nil nil] ... ... [# # # # # # #("x :=3D 2" 0 6 (fontified t)) nil (calcFunc-assign (var x var-x) 2) (var x var-x) nil (calcFunc-assign (var x var-x) 2) nil nil nil nil] ...) Which begs the question: what should be `x'? In any case, the code I posted above had a problem with decimals. The code still picks the first value of VAR that shows up in the list above. (defun vz/calc-embedded-get-var (var) "Return the value of active `calc-embedded' VAR in current buffer." (let* ((info (cdr (assq (current-buffer) calc-embedded-active))) (var-info (seq-find (lambda (x) ;; 9 is the variable name: (var XXX var-XXX) (eq var (nth 1 (aref x 9)))) info)) old-val) (when (and info var-info) ;; This is called the `old-val' in `calc-embedded-update'. ;; This can be nil when the formula isn't evaled I think? ;; (aref VAR-INFO 8) is again repeated in 11th slot when the ;; variable is simply as assignment. (let ((calc-line-numbering)) ;; For the below trick, see `calc-embedded-update' again. (math-format-stack-value (list (or (if (and (consp (setq old-val (aref var-info 11))) (eq (car old-val) 'calcFun-assign)) (car (last old-val)) old-val) ;; 8 is the eval form: (calcFun-evalto ...) or ;; (calcFun-assign ...) (car (last (aref var-info 8)))) 1 nil)))))) (define-advice org-table-get-constant (:around (oldfun name) vz/conside= r-calc-embedded-var) "Check if NAME is a `calc-embedded' at last." (let ((val (funcall oldfun name))) (if (equal val "#UNDEFINED_NAME") (or (vz/calc-embedded-get-var (intern name)) val) val)))