Search code examples
emacselisporg-mode

How to configure cleverly org-archive-location in org-mode


BACKGROUND: In org-mode, the variable org-archive-location is set to "%s_archive::" by default, so that a file "toto.org" archives into a file "toto.org_archive". I would like it to archive to "toto.ref" instead. I am using org-mode version 7.4 (out of the git server).

I would have thought it to be as simple as

(setq org-archive-location 
      `(replace-regexp-in-string ".org" ".ref" %s)
      )

But I was pointed out that this was not proper in LISP (plus, it did not work). My final solution is as follow, you should be able to adapt to most clever configurations of org-archive-location:

(setq org-archive-location "%s::* ARCHIVES")
(defadvice org-extract-archive-file (after org-to-ref activate)
  (setq ad-return-value
        (replace-regexp-in-string "\\.org" ".ref" ad-return-value)
    )
  )

Note that:

1) I voluntarily did not add a $ at the end of ".org" so that it would properly alter "test.org.gpg" into "test.ref.gpg".

2) It seems that one should use the regular expression "\.org" (rather than, say, ".org") (longer explanation below in the answers).


Solution

  • You can't define a variable in Emacs such that its value is obtained by running code; variables have simple, static values.

    You can achieve the effect you described by advising the function org-extract-archive-file, which is the one that generates an archive location from org-archive-location:

    (defadvice org-extract-archive-file (after org-to-ref activate)
      (setq ad-return-value
            (replace-regexp-in-string "\\.org" ".ref" ad-return-value)))
    

    This works for me now, but of course the internals of org-mode are subject to change and this solution may not work forever.