Eric Abrahamsen <e...@ericabrahamsen.net> writes:

> I think the last thing I'm missing before my Org set up is Absolutely
> Perfect and never needs to be tweaked again (ha!) is the ability to
> export Org files/subtrees to their relevant ATTACH directories, if any.
> It might be overkill to have a global setting for this, but I would
> love to be able to say:
>
> * Report #25                                                 :ATTACH:
> :PROPERTIES:
> :ID:       3da327aa-b51e-444a-ae5c-95bb56d025a9
> :EXPORT_FILE_NAME: report_25
> :EXPORT_TO_ATTACH: t
> :END:

Nobody seems super excited about this but...

I went poking to see how difficult it would be to do this only using
hooks and whatnot. It seemed a bit awkward: I can munge the export file
name altogether, but there's no good way to say "use whatever file name
you would have come up with, but put the resulting file in such-and-such
directory".

Mostly it's not possible to sneak in and add/alter the PUB-DIR argument
to `org-export-output-file-name' -- that argument only seems to be used
in the publishing framework.

The attached patch to `org-export-output-file-name' has it also check
for a EXPORT_PUB_DIR property at the heading level or document level;
that would allow libraries like org-attach.el a way to get at exporting
via a hook. (Maybe EXPORT_DIRECTORY would be a better name.)

Would something along these lines be considered for inclusion?

Thanks,
Eric


diff --git a/lisp/ox.el b/lisp/ox.el
index 9cf62078a..77cafb20d 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -6417,6 +6417,20 @@ Return file name as a string."
 	      "Output file: " pub-dir nil nil nil
 	      (lambda (n) (string= extension (file-name-extension n t))))))
 	   extension))
+	 (pub-dir (or pub-dir
+		      (and subtreep (org-entry-get
+				     nil "EXPORT_PUB_DIR" 'selective))
+		      (org-with-point-at (point-min)
+			(catch :found
+			  (let ((case-fold-search t))
+			    (while (re-search-forward
+				    "^[ \t]*#\\+EXPORT_PUB_DIR:[ \t]+\\S-"
+				    nil t)
+			      (let ((element (org-element-at-point)))
+				(when (eq 'keyword (org-element-type element))
+				  (throw :found
+					 (org-element-property
+					  :value element))))))))))
 	 (output-file
 	  ;; Build file name.  Enforce EXTENSION over whatever user
 	  ;; may have come up with.  PUB-DIR, if defined, always has

Reply via email to