0

In my org-mode archive files, I like having buffer-read-only set as a file-local variable so that if I open the archive to find something, I don't accidentally mess anything up. It seems to be in the right spirit for something called an "archive".

But with something like -*- buffer-read-only: t; -*-, when I use org-archive-subtree or similar, it fails because the buffer is read-only, of course.

I'd like the archiving commands to ignore that, while keeping the buffer read-only when I visit it normally.

I could advise org-archive-subtree, and set inhibit-read-only before the function is called, and reset it afterwards. But there's all kinds of (regular) advice that says emacs lisp advice is a last resort, should be avoided, etc, although here that seems not so risky, something like:

(advice-add
 'org-archive-subtree
 :before
 (lambda (&rest r)
   (message "Inhibiting buffer-read-only before archiving subtree...")
   (setq inhibit-read-only t)))

(advice-add
 'org-archive-subtree
 :after
 (lambda (&rest r)
   (message "Clearing inhibit-read-only after archiving subtree.")
   (setq inhibit-read-only nil)))

If I want to take that advice about advice, how could I make the archiving commands ignore buffer-read-only, while preserving it when I visit the file as usual? There's only one hook related to archiving, and that runs after.

4
  • One interesting note: in a read-only archive file, if I use org-store-link, it will ignore buffer-read-only and create a PROPERTIES drawer to make an ID property. It seems strange that store-link by default (?) seems to ignore buffer-read-only but archiving doesn't.
    – Dan Drake
    Commented Oct 20, 2023 at 13:28
  • org-store-link does not add an ID property by default: try it with emacs -Q.
    – NickD
    Commented Oct 21, 2023 at 1:39
  • 1
    All that the linked (regular) advice says IMO is: use a hook to modify the behavior if one exists; use advice otherwise. That assumes that you have thought about the consequences of changing behavior (something that you should always do of course - all such changes introduce incompatibilities between your setup and the default one, so they should be done sparingly and documented copiously), but if you need the change, then using advice is perfectly reasonable IMO.
    – NickD
    Commented Oct 21, 2023 at 14:40
  • (1) my idiosyncratic configuration for org-store-link does add an ID property; I should go see how/why it ignores buffer-read-only; (2) your comment about using hooks when available is what I settled on. There's org-archive-hook , but that runs after, and I would need both before and after hooks.
    – Dan Drake
    Commented Oct 22, 2023 at 14:14

0

Your Answer

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

Browse other questions tagged or ask your own question.