Hej fellows,
I tried to find a solution to this question on my own for quite a while now. I'm rather certain that I am also phrasing my problem the wrong way, because it is impossible that it is that hard to find.
I want to write a function that opens my org agenda file(s) in buffer(s), operates on said buffer(s) and then saves them. More concretely, I want to have `org-sort-entities` sort my [`todos.org`](https://todos.org) file every time I used org-capture (using `org-capture-after-finalize-hook`).
I am grateful for both pointers and complete solutions. As I said, for some reason I can't wrap my head around the way emacs opens files when it doesn't happen upon my initiative.
Have a good day, fellows :)
**SOLVED:** I was able to solve the problem! Thanks a bunch, u/MitchellMarquez42 and u/EternalMigrainity!
Now I will say that the pointer provided by u/MitchellMarquez42 helped me find a solution that is clearer to me on first sight than the other one. Nonetheless, thank you both - not least because I not know that `with-current-buffer` and `with-temp-file` can use `find-file` as input. I had seen both elements - `with-*-*` and `find-file` \- but would not have come up with the idea to combine the two. This will make it that much easier to work with files in the future :-\*
**EDIT:** For some reason, it did not work properly when invoked that way from a hook. I would add a new todo entry via org-capture and the org-agenda-file would not be sorted.
DOES NOT WORK
(defun org-sort-agenda-files-after-capture (file-list)
(mapcar #'(lambda (file)
(with-current-buffer (find-file file)
(mark-whole-buffer)
(org-sort-entries nil ?p)
(save-buffer)
(kill-buffer)))
file-list))
(use-package org-capture
:hook
(org-capture-after-finalize . (org-sort-agenda-files-after-capture org-agenda-files)))
Then I decided to try again removing the argument from the function, hardcoding `org-agenda-files` into the `org-sort-agenda-files-after-capture` function instead of the `file-list` parameter. It turns out that it worked. I don't know why. If you happen to know, I'd love to be enlightened :-)
DOES WORK
(defun org-sort-agenda-files-after-capture ()
(mapcar #'(lambda (file)
(with-current-buffer (find-file file)
(mark-whole-buffer)
(org-sort-entries nil ?p)
(save-buffer)
(kill-buffer)))
org-agenda-files))
(use-package org-capture
:hook
(org-capture-after-finalize . org-sort-agenda-files-after-capture))
​