emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Ihor Radchenko <yantar92@posteo.net>
To: Jeff Trull <edaskel@att.net>
Cc: emacs-orgmode@gnu.org
Subject: Re: [BUG] text cell contents have extra parentheses when used in formulas [9.6.4 ( @ /home/jet/.config/emacs/elpa/org-9.6.4/)]
Date: Sun, 23 Apr 2023 19:57:25 +0000	[thread overview]
Message-ID: <875y9mwevu.fsf@localhost> (raw)
In-Reply-To: <87leiiub1o.fsf@localhost>

Ihor Radchenko <yantar92@posteo.net> writes:

>> #+TITLE: text formula repro
>>
>> | Key | Value        |
>> |-----+--------------|
>> | A   | [40, 65, 41] |
>> | B   | [40, 66, 41] |
>> #+TBLFM: $2="$1"
>>
>> Here you can see that the contents of the Key column were wrapped in
>> parentheses (ASCII 40 and 41) when turned into strings.
>
> If you turn formula debugger on (M-x org-table-toggle-formula-debugger),
> you will see the following (I also added extra explanations):
> ...
> $1->    "(A)" ;; after handling column references; the (...) is added
>               ;; because of Calc syntax to ensure that values inside
>               ;; cell are grouped. There is no notion of strings in GNU
>               ;; Calc (AFAIK). So, (...) grouping always represents
>               ;; algebraic Calc expression.
> Result: [40, 65, 41] ;; (calc-eval "\"(A)\"")

After looking closer, the code wrapping cell value into (...) is
actually not necessarily correct.

The parenthesis wrapping has been introduced in

e7e5e25100246e5eab3a36034ac2de578dabd02f
Author:     Carsten Dominik <carsten.dominik@gmail.com>
Fix bug with algebraic evaluation in tables.

There was a problem that complex fields need a pair of parentheses
around them.  For example, if one field is "a a" and another field
is "a+b", multiplication of the two gave "a^3+b" instead of "a^3+a^2 b".
Now variable replacement puts extra parenthesis around values.

----

However, in a way, unconditional wrapping of cell value in parentheses is
against how Org table formulas intuitively work - references are
replaced by appropriate cell values.

Wrapping a cell value in (...) only makes sense as long as algebraic
operations are considered. However, when cell value is intentionally
treated as string (explicit vector of character codes in GNU Calc
conventions), extra (...) will introduce the observed 40 ... 41 into the
vector.

It might be tricky to remove the existing behaviour though - removing the
parenthesis will likely break the existing use cases with genuine
algebraic computations. We might, however, use "L" flag, described in
"3.5.2 Formula syntax for Calc":

‘L’
     Literal, for Lisp formulas only.  See the next section.

Now, it is only used for Lisp formulas, but we can make use of it in
Calc formulas as well, preventing this (...) wrap.

Or maybe others have better ideas.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
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>


      reply	other threads:[~2023-04-23 19:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CAF_DUeG_W_40Ws_8Z+0Q=f_2yzT+cE9aoUxDbLnnb1n7-HDbaQ.ref@mail.gmail.com>
2023-04-22 14:20 ` [BUG] text cell contents have extra parentheses when used in formulas [9.6.4 ( @ /home/jet/.config/emacs/elpa/org-9.6.4/)] Jeff Trull
2023-04-23 10:50   ` Ihor Radchenko
2023-04-23 19:57     ` Ihor Radchenko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=875y9mwevu.fsf@localhost \
    --to=yantar92@posteo.net \
    --cc=edaskel@att.net \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).