Org capture stuff

Capturing a file containing a table as plain in the destination (target) file results in a mess because that file doesn’t align the columns, since it is inserted within a header.

How to get it aligned?

How to understand the “:hook” property ?

Thanks for all the hours you’ve spent to teach us emacs ways of getting the job done.

Hi Chris.

I have the following defined in my org-capture config and it works for me.

Define a function that aligns tables:

(defun my-align-table ()
  "Align tables in the current buffer."
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "|.*|" nil t)
      (org-table-align))))

Add a hook to the capture template:

(setq org-capture-templates
      '(("t" "Todo" entry (file+headline "~/path/to/your/org-file.org" "Tasks")
         "* TODO %?\n  %i\n  %a"
         :hook (my-align-table))))

This setup ensures that the function my-align-table is called after the capture process to align any tables present in the buffer.

My attempt at defining the :hook property:

The :hook property in Emacs allows you to attach a function to a specific event or action. In the above context, it’s being used with org-capture-templates to specify a function to be executed after the capture process is complete.

In the example above, :hook (my-align-table) tells Emacs to execute the function my-align-table after the template has been used to capture the entry. This is useful for performing additional actions or modifications to the captured content, such as aligning tables.

Let me know if this helps or not.

1 Like

Hi Glenneth,

Thanks. I tried your function putting it before my capture’s configs.
the :hook property has to be after the file to be inserted to avoid being rejected (msg : invalid capture template).
But the file containing the table continues to be messed, and the same in the result.
Here is part of my code :
(“bp” “Procedure pain suite” plain (file “~/org/boulangerie.org”)
(file “~/org/painstableau.txt”)
:hook (my-align-table) )

As you see, the type of my template is “plain” and the stuff to be inserted in the target file “boulangerie.org” is the table contained in “paintableau.txt”.

Till now, when entering “boulangerie.org” to play with the messed tables, I used M-x variable-pitch-mode.

Any other idea ?
Thanks.

Hi Chris.

Since you’re capturing a plain entry and inserting a table from another file into the target file, the :hook property might not be triggering as expected because it operates on the buffer after the entry is captured, and in this case, the table from the file is inserted before that.

To align the table from painstableau.txt when it’s inserted into boulangerie.org, you can define a function that aligns tables and advise the function responsible for inserting the file to call your alignment function afterward.

Try this:

(defun my-align-table ()
  "Align tables in the current buffer."
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "|.*|" nil t)
      (org-table-align))))

;; Advice the function `org-capture-place-template` to call `my-align-table`
;; after inserting the file.
(advice-add 'org-capture-place-template
            :after (lambda ()
                     (when (eq org-capture-plist :file)
                       (my-align-table))))

(setq org-capture-templates
      '(("bp" "Procedure pain suite" plain
         (file "~/org/boulangerie.org")
         (file "~/org/painstableau.txt"))))

when you capture a plain entry using the template "bp", the file painstableau.txt will be inserted into boulangerie.org, and then the function my-align-table will be called to align any tables in the buffer.

Try this out and let me know if it works for you.

Hi Glenn,
Thanks again for your interest for this (my) mini-problem.

Your code is accepted and the init.el has no error opening emacs.
But when I enter “C-c C b p” here is the message in the bottom mini-buffer:
“Capture-template ‘bp’ : Wrong number of arguments:
(lambda nil (if (eq org-capture-plist :file) (progn (my-align-table)))), 1”

Nevertheless, the table contained in painstableau.txt has been inserted in the target file (boulangerie.org) but without resulting aligned (same with the rest of this target file.

Chris.

This is my last best guess :slight_smile: TBH I am learning as I go and am having similar problems, but I think I have mine more or less ironed out.

I think the problem was with the lambda function.

(defun my-align-table ()
  "Align tables in the current buffer."
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "|.*|" nil t)
      (org-table-align))))

(advice-add 'org-capture-place-template
            :after
            (lambda (&rest _args)
              (when (eq org-capture-plist :file)
                (with-current-buffer (find-file-noselect (plist-get org-capture-plist :file))
                  (my-align-table)))))

(setq org-capture-templates
      '(("bp" "Procedure pain suite" plain
         (file "~/org/boulangerie.org")
         (file "~/org/painstableau.txt"))))

This should correctly advise the org-capture-place-template function to call my-align-table after inserting the file into the buffer. If I have this correct, the tables should now be aligned after the file insertion.

Try it out and let me know how you get along.

Thanks.

Glenn,

No way till now. No error in the code, the file is inserted but un-aligned.

We know that using emacs is a constant learning path. I am learning emacs-life each day my computer is on. So I’ll tell here if I progress.
For now thanks ; I’ll continue to use M-x variable-pitch-mode in the target file.
Thanks.