From 9ba64d149aac6c807e233e34474ffe022488efe6 Mon Sep 17 00:00:00 2001 From: Mehmet Tekman Date: Wed, 20 Sep 2023 11:35:00 +0200 Subject: [PATCH] header refs --- lisp/ob-core.el | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index a7c4f2cab..0b0d1c18c 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -1753,6 +1753,42 @@ HEADER-ARGUMENTS is alist of all the arguments." header-arguments) (nreverse results))) + +(defun org-babel--split-str-quoted (str) + "Splits a string that may or may not contain quotes." + (let (result pos) + (while (string-match (rx (or (seq "\"" (group (* (not "\""))) "\"") + (group (+ (not space))))) + str pos) + (let ((match-str1 (match-string 1 str)) + (match-str2 (match-string 2 str))) + (if match-str1 + (progn + (push match-str1 result) + (setq pos (1+ (match-end 1)))) + (push match-str2 result) + (setq pos (match-end 2))))) + (nreverse result))) + +(defun org-babel--tangle-split (raw-tangle) + "Split a :tangle headerby filename and sync action." + (let* ((valid-sync-actions '("import" "export" "sync")) + (file-action (org-babel--split-str-quoted raw-tangle)) + (file (car file-action)) + (action (nth (1- (length file-action)) file-action))) + (if (member action valid-sync-actions) + ;; If last word matches, assume the previous are all part of + ;; the filename + (setq file (mapconcat #'identity (nreverse (cdr (nreverse file-action))) " ")) + ;; Otherwise set the default action and assume that the full + ;; string has no action + (if (or file action) + (setq action "export" + file (read-char raw-tangle)))) + (if (or file action) + (list file action) + (list nil)))) + (defun org-babel-process-params (params) "Expand variables in PARAMS and add summary parameters." (let* ((processed-vars (mapcar (lambda (el) @@ -1775,7 +1811,13 @@ HEADER-ARGUMENTS is alist of all the arguments." raw-result ;; FIXME: Arbitrary code evaluation. (eval raw-result t))) - (cdr (assq :result-params params)))))) + (cdr (assq :result-params params))))) + (raw-tangle (or (cdr (assq :tangle params)) "")) + (tangle-params (delete-dups + (append + (org-babel--tangle-split raw-tangle) + (cdr (assq :tangle-params params))))) + ) (append (mapcar (lambda (var) (cons :var var)) (car vars-and-names)) (list @@ -1786,7 +1828,9 @@ HEADER-ARGUMENTS is alist of all the arguments." (cons :result-params result-params) (cons :result-type (cond ((member "output" result-params) 'output) ((member "value" result-params) 'value) - (t 'value)))) + (t 'value))) + (cons :tangle-params tangle-params) + ) (cl-remove-if (lambda (x) (memq (car x) '(:colname-names :rowname-names :result-params :result-type :var))) -- 2.42.1