简体   繁体   中英

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).

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.

You should not quote an expression that you want to evaluate. Note also that in a regular expression, . matches any character.

Here is an example of how to set the file, the location (eg, main heading) in the file, and whether or not to include additional archive information:

(let* (
    (org-archive-location "~/todo.org::* TASKS")
    (org-archive-save-context-info nil))

  ...)

You can try this: #+ARCHIVE: %s.ref:: at the beginning of your org file. Read more about it here .

Also, other interesting option is to set inside your headtree the following, for instance:

* Main Header of tree
:PROPERTIES:
:ARCHIVE: toto.ref:: * Main Header of tree in archive file
:END:
** sub tree of main header and so on

The latter I took from this video .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM