emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] lisp/org-table.el: Allow named columns on lhs


From: Gavin Downard
Subject: Re: [PATCH] lisp/org-table.el: Allow named columns on lhs
Date: Wed, 26 Jul 2023 14:50:21 -0700
User-agent: mu4e 1.10.4; emacs 29.0.92

Max Nikulin <manikulin@gmail.com> writes:
> The regexp for parsing formulas does not allow named references after @.
> I have no idea if other code should be modified as well. I consider it
> as more important than detection of duplicated definitions.

Yeah, besides changing the regexp, the main thing that will need to be
changed is the interface of `org-table-get-stored-formulas'. Currently,
a field name on the lhs can only exist by itself (that is, it can't be a
part of any other expression, such as a range), so the leading '$' is
removed. So, '$name=1+2' is parsed as '("name" . "1+2")'. This is at
odds with field/column names in the middle of an expression.

I think the best way to do this is to change
`org-table-get-stored-formulas' to not strip the leading '$', and
instead of using '(assoc FIELD-NAME org-table-named-field-locations)' or
'(assoc COL-NAME org-table-column-names)', we can a modified* version of
`org-table-formula-substitute-names' (which is used for field and column
names on the rhs of a formula). That would work as a replacement for the
existent handling of field names that would also work for column names
as we want them.

As a side note, this would allow ranges on the lhs that contain field
names in them:

|   |     1 | 2 |   3 |
| # |       |   |     |
| ^ | begin |   | end |
#+TBLFM: $begin..$end=@1*@1

(*): the issue with `org-table-formula-substitute-names' as it is, is
that it will replace field names with the field /value/, not the field
index. Hopefully modifying the function to replace field names with
their indices won't break anything, so we can use the same function for
both sides of the formula.

> A crazy idea: several columns may have the same name:
>
> |   | Jun sum | Jun count | Jun avg | Jul sum | Jul count | Jul avg |
> | ! |         |           | average |         |           | average |
> |---+---------+-----------+---------+---------+-----------+---------|
> |   |  150    | 10        |         | 200     |        14 |         |
>
> #+tblfm: $average=$-2/$-1
>
> It has no sense for numeric references but with names it can help to
> avoid repeated expressions.

Or, if you want to get really crazy, we can let fields and columns with
the same name share a formula. :)

| ! | Apple budget |  Pear Budget | Orange Budget |         total |
|---+--------------+--------------+---------------+---------------|
| # |            1 |            2 |             3 |             6 |
| # |            4 |            5 |             6 |            15 |
|---+--------------+--------------+---------------+---------------|
| # |            2 |            3 |             5 |            26 |
| ^ | extra-cost-1 | extra-cost-2 |         total | ultimatetotal |
#+TBLFM: $total=vsum($<<..$-1) :: $ultimatetotal=vsum(@I..II) + $-1

But in all seriousness, that does seem like it could be useful. I guess
on the rhs we could give each of those "shared names" the value of an array of
that name's connected columns (or fields). But I think support for that should
probably be added in a separate patch, since it's a bit separate from
the main issue.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]