1

(Disclaimer: I know this may seem like a very specific question. But: I haven't found the answers to similar questions to be close enough to what I need. I hope to get an answer that is easily understood, and that people can adapt to their own needs, without having learned elisp)

I have an already existing table, that I want to append rows to with org-capture

The workflow I want:

  1. Shortcut pressed

  2. Menu pops up asking "Should this be an internal link?". Answer with y/n

    • If yes, the name-field in pt. 3 should be this:

      • [[#PLACEHOLDER-NN] [Name]]

        • PLACEHOLDER is just the text "PLACEHOLDER". I'll fill that in later

        • - is an actual hyphen

        • NN is the next custom-ID-number. I use the "name hyphen incremented_number"-format to avoid having identical custom IDs

          • Right now, I use a one-liner to find the highest number used so far. Shown at the end of the post.
        • Space between to prevent emacs from smooshing the link together, so that I can see which PLACEHOLDERs need replacing. I'll remove the space by hand after replacing the PLACEHOLDER.

        • Name is the name of the project
    • Else: The name-column is just text entered by me

  3. Press enter

  4. Get to the next column, titled "2 Comment"

  5. Enter Comment-content. Might be empty. Hit enter to skip

  6. Hit enter when finished entering comment

  7. Get to "3 Tags"-column

  8. Enter tags

  9. Hit enter to get to next column. Skipping column #4

  10. Enter contents into 6 more columns

  11. When that's done, perform the calculation (#+TBLFM: $4=($5+$6+$7+$8-$9-$10)) right underneath the table

  12. Table is resorted by column titled "4 Total". Header- and separator-row stay where they are


Answer to similar thread: Org capture into org table - I don't know enough elisp to modify it to my needs


Possibly complicating factors:

  • My table isn't right under a top-level heading. There's a top-level heading, some text, a blank line, and then the table

  • I have that calculation right underneath the table


One-liner: grep ^\ *:CUSTOM_ID: ~/Path/To/file.org | awk '{print $2}' | awk -F '-' '{print $2}' | sort -n | tail -1

Searches for lines that start with 0 or more spaces, then have the phrase ":CUSTOM_ID:", as in the start of an internal link. The next argument specifies which file it searches through. Then awk first displays only the 2nd field (the custom ID itself. We're not interested in the phrase ":CUSTOM_ID:"). Then awk uses a hyphen as a separator, and prints only the 2nd field there: the numbers after the hyphens. Now that we have these numbers, we can numerically sort by them, and use tail -1 to display just the last (and largest) of these numbers. The next number should be largest-number + 1

1 Answer 1

2

[Not a complete answer, but too long to be a comment.]

If you insist on this workflow, it is going to be very messy.

If you limit yourself to something simpler, I think the answer that you linked to is enough to do most of what you want.

I'm going to disregard the "internal link" case for now (which is why the answer is incomplete). You define a template similar to the one in the linked answer:

(setq org-capture-templates
  `(
     .... other templates ....
     ("t" "Table" table-line
         (file+headline "/path/to/table-example.org" "my table")
         "|%?| | |COLUMN4 | | | | | | |")
   ))

Then you have a file table-example.org somewhere like this:

* Other stuff

foo bar

* my table

| Col1 | Col2 | Col3 | Col4 | Col5 | Col6 | Col7 | Col 8 | Col9 | Col10 |
|------+------+------+------+------+------+------+-------+------+-------|
#+TBLFM: $4=$5+$6+$7+$8-$9-$10

* other stuff
foo bar

Doing M-x org-capture RET t RET will then give you a buffer like this:

||   |   | COLUMN4 |   |   |   |   |   |   |
 ^

with the cursor where the arrow points. You enter stuff for the first columnt, press TAB to go to the second column, enter stuff for the second column etc. The COLUMN4 entry is there to remind you to skip this column, which you do by pressing an extra TAB. When you are done entering everything, you press C-c C-c.

For example, I entered:

| foo | bar | baz | COLUMN4 | 5 | 4 | 3 | 2 | 1 | 0 |

and pressed C-c C-c.

The file will look then like this:

* Other stuff

foo bar

* my table

| Col1 | Col2 | Col3 | Col4 | Col5 | Col6 | Col7 | Col 8 | Col9 | Col10 |
|------+------+------+------+------+------+------+-------+------+-------|
| foo  | bar  | baz  |   13 |    5 |    4 |    3 |     2 |    1 |     0 |

#+TBLFM: $4=$5+$6+$7+$8-$9-$10

* other stuff
foo bar

Depending on the setting of org-table-automatic-realign, you might get a newline between the table and the #+TBLFM: line added every time you use the capture. That happens when it is set to a non-nil value. If you normally set it that way, you might want to set it to nil locally in the file: that will prevent TAB from realigning while you are capturing, but when you finish with C-c C-c, everything will be aligned and with no extra newline.

There is no problem with either of your possible complications AFAICT.

I believe that does about 90% of what you want (depending on how you count :-). It does not deal with the "internal link" choice, it uses TAB instead of ENTER to move through the columns, and it expects you to skip column 4 on your own.

For the "internal link" stuff, I would just write a function to insert what you want at point and bind it to some key. Then when you invoke the capture and are in the right column for it, you either invoke the function with whatever key you bound it to, or you just enter the text. Something like this:

(defun my-insert-internal-link ()
 (interactive)
 (insert (format "[[#PLACEHOLDER-%s] [%s]]" (my-next-uuid) (my-project-name))))

(defun my-next-uuid ()
 ;; implementation is up to you
 "abcdefgh")

(defun my-project-name ()
 ;; implementation is up to you
 "My Project")

(define-key global-map (kbd "C-c z") 'my-insert-internal-link)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.