Przeglądaj źródła

Allow file names in capture templates to be functions, forms, or variables

* lisp/org-capture.el (org-capture-expand-file): New function.
(org-capture-target-buffer):
(org-capture-set-target-location): Use `org-capture-expand-file'.

* doc/org.texi (Template elements): Document that files can be given
as function, form, or variable.
Carsten Dominik 14 lat temu
rodzic
commit
c654620d73
2 zmienionych plików z 18 dodań i 2 usunięć
  1. 2 1
      doc/org.texi
  2. 16 1
      lisp/org-capture.el

+ 2 - 1
doc/org.texi

@@ -6316,7 +6316,8 @@ Specification of where the captured item should be placed.  In Org-mode
 files, targets usually define a node.  Entries will become children of this
 node.  Other types will be added to the table or list in the body of this
 node.  Most target specifications contain a file name.  If that file name is
-the empty string, it defaults to @code{org-default-notes-file}.
+the empty string, it defaults to @code{org-default-notes-file}.  A file can
+also be given as a variable, function, or Emacs Lisp form.
 
 Valid values are:
 @table @code

+ 16 - 1
lisp/org-capture.el

@@ -669,7 +669,9 @@ already gone.  Any prefix argument will be passed to the refile comand."
 	    (beginning-of-line 0))))
 
        ((eq (car target) 'file+olp)
-	(let ((m (org-find-olp (cdr target))))
+ 	(let ((m (org-find-olp
+		  (cons (org-capture-expand-file (nth 1 target))
+			(cddr target)))))
 	  (set-buffer (marker-buffer m))
 	  (goto-char m)))
 
@@ -729,8 +731,21 @@ already gone.  Any prefix argument will be passed to the refile comand."
       (org-capture-put :buffer (current-buffer) :pos (point)
 		       :target-entry-p target-entry-p))))
 
+(defun org-capture-expand-file (file)
+  "Expand functions and symbols for FILE.
+When FILE is a function, call it.  When it is a form, evaluate
+it.  When it is a variable, retrieve the value.  Return whatever we get."
+  (setq file
+	(cond
+	 ((org-string-nw-p file) file)
+	 ((functionp file) (funcall file))
+	 ((and (symbolp file) (boundp file)) (symbol-value file))
+	 ((and file (consp file)) (eval file))
+	 t file)))
+
 (defun org-capture-target-buffer (file)
   "Get a buffer for FILE."
+  (setq file (org-capture-expand-file file))
   (setq file (or (org-string-nw-p file)
 		 org-default-notes-file
 		 (error "No notes file specified, and no default available")))