5

Question

How do I immediately save the .org buffer created by org-archive-location?

Context and what I've tried so far

Current .emacs settings:

FILE: .emacs
(setq org-archive-location (concat "~/Documents/Reference/org/archive/" (format-time-string "%Y-%m") ".org::"))

Consider

FILE: main2.org
* Done item

Calling org-archive-location with C-c $ results in the following buffers

FILE: *Messages*
Subtree archived in file: ~/Documents/Reference/org/archive/2019-06.org

and

FILE: 2019-06.org
Archived entries from file /Users/janmeppe/Documents/Reference/org/main2.org


* Done item 
:PROPERTIES:
:ARCHIVE_TIME: 2019-06-18 Tue 11:26
:ARCHIVE_FILE: ~/Documents/Reference/org/main2.org
:ARCHIVE_CATEGORY: main2
:END:

Now the problem is this, I have to manually save the buffer for it to appear in the finder, how do I automatically save this buffer after calling org-archive-location?

I tried adding the following but it this help

(advice-add 'org-archive-location :after #'org-save-all-org-buffers)
0

3 Answers 3

5

org-archive-location is not a function that can be modified with advice. It is a variable. Perhaps the O.P. meant to say org-archive-subtree? Type C-h k (aka M-x describe-key) and then the keyboard shortcut to see what function is triggered ....

(advice-add 'org-archive-subtree :after #'org-save-all-org-buffers)

I would prefer to save only the buffer containing the archived subtree and the buffer from which it was archived/removed, but that would take some additional digging into the code and doing some testing ... Perhaps another forum participant would like to write up an alternate answer that uses a scalpel instead of the org-save-all-org-buffers (which is a sledgehammer).

1
  • No time to dig either, but org-archive-hook runs after successfully archiving a subtree, with point in the original file. There is also the variable org-archive-location which indicates where subtrees should be archived for the current file. Unfortunately, org-archive-location uses some bizarro syntax which isn't a file path. If someone can figure out how to expand the org-archive-location into a normal file system path, then org-archive-hook could be made to 1. save the current org file and 2. the corresponding archive file. Commented Sep 20, 2019 at 12:55
1

Since at least Org 9.4 there's a variable which controls when to save the archive file: org-archive-subtree-save-file-p. From the docs:

Conditionally save the archive file after archiving a subtree.

This variable can be any of the following symbols:

t              saves in all cases.
from-org     prevents saving from an agenda-view.
from-agenda  saves only when the archive is initiated from an agenda-view.
nil            prevents saving in all cases.

Note that, regardless of this value, the archive buffer is never
saved when archiving into a location in the current buffer.
0

I found a workaround with the same functionality but it works on calling save-buffer which I call often with :w in evil. I'm not marking this as the correct answer because it does not answer the question (i.e. how to save immediately after org-archive-location) but I'd like to share it because it results in the same functionality.

(advice-add 'save-buffer :after #'org-save-all-org-buffers)

Your Answer

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

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