diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index fc2ff9e00..df785dc24 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -83,6 +83,11 @@ transcoders. Users can disable citations processors by customizing new ~org-export-process-citations~ option. +*** =ox-org= preserves header non-default arguments in src blocks + +Previously, all the header arguments where stripped from src blocks +during export. Now, header arguments are preserved as long as their +values are not equal to the default header argument values. *** =ox-org= disables citation processors by default diff --git a/lisp/ob-exp.el b/lisp/ob-exp.el index af726dc2c..80eaeeb27 100644 --- a/lisp/ob-exp.el +++ b/lisp/ob-exp.el @@ -238,7 +238,10 @@ this template." ((not (string= replacement (buffer-substring begin end))) (delete-region begin end) - (insert replacement)))))) + (insert replacement)) + ;; Replacement is the same as the source + ;; block. Continue onwards. + (t (goto-char end)))))) ((or `babel-call `inline-babel-call) (org-babel-exp-do-export (or (org-babel-lob-get-info element) @@ -367,7 +370,7 @@ The function respects the value of the :exports header argument." nil)))) (defcustom org-babel-exp-code-template - "#+begin_src %lang%switches%flags\n%body\n#+end_src" + "#+begin_src %lang%switches%flags%header-args\n%body\n#+end_src" "Template used to export the body of code blocks. This template may be customized to include additional information such as the code block name, or the values of particular header @@ -379,6 +382,7 @@ and the following %keys may be used. body ------ the body of the code block switches -- the switches associated to the code block flags ----- the flags passed to the code block + header-args the non-default header arguments of the code block In addition to the keys mentioned above, every header argument defined for the code block may be used as a key and will be @@ -388,7 +392,7 @@ replaced with its value." :package-version '(Org . "9.6")) (defcustom org-babel-exp-inline-code-template - "src_%lang[%switches%flags]{%body}" + "src_%lang[%switches%flags%header-args]{%body}" "Template used to export the body of inline code blocks. This template may be customized to include additional information such as the code block name, or the values of particular header @@ -400,6 +404,7 @@ and the following %keys may be used. body ------ the body of the code block switches -- the switches associated to the code block flags ----- the flags passed to the code block + header-args the non-default header arguments of the code block In addition to the keys mentioned above, every header argument defined for the code block may be used as a key and will be @@ -432,6 +437,41 @@ replaced with its value." (and (org-string-nw-p f) (concat " " f)))) ("flags" . ,(let ((f (assq :flags (nth 2 info)))) (and f (concat " " (cdr f))))) + ("header-args" + . + ,(let* ((header-args + (mapcar + (lambda (pair) + ;; Do no include special parameters, parameters with + ;; their values equal to defaults. + (unless (or + ;; Special parameters that are not real header + ;; arguments. + (memq (car pair) + '( :result-params :result-type + ;; This is an obsolete parameter still + ;; used in some tests. + :flags)) + ;; Global defaults. + (equal (cdr pair) + (alist-get + (car pair) + (if (eq type 'inline) org-babel-default-inline-header-args + org-babel-default-header-args))) + ;; Per-language defaults. + (let ((lang-headers + (intern + (concat "org-babel-default-header-args:" + (nth 0 info))))) + (and (boundp lang-headers) + (equal (cdr pair) + (alist-get (car pair) + (eval lang-headers t)))))) + (format "%s %s" (car pair) (cdr pair)))) + (nth 2 info))) + (header-arg-string + (mapconcat #'identity (delq nil header-args) " "))) + (unless (seq-empty-p header-arg-string) (concat " " header-arg-string)))) ,@(mapcar (lambda (pair) (cons (substring (symbol-name (car pair)) 1) (format "%S" (cdr pair)))) diff --git a/testing/lisp/test-ob-exp.el b/testing/lisp/test-ob-exp.el index e6fbf14a1..d029dadfb 100644 --- a/testing/lisp/test-ob-exp.el +++ b/testing/lisp/test-ob-exp.el @@ -205,14 +205,14 @@ Here is one at the end of a line. {{{results(=2=)}}} (ert-deftest ob-exp/exports-inline-code () (should - (equal "src_emacs-lisp[]{(+ 1 1)}" + (equal "src_emacs-lisp[ :exports code]{(+ 1 1)}" (org-test-with-temp-text "src_emacs-lisp[:exports code]{(+ 1 1)}" (let ((org-babel-inline-result-wrap "=%s=") (org-export-use-babel t)) (org-babel-exp-process-buffer)) (buffer-string)))) (should - (equal "src_emacs-lisp[]{(+ 1 1)}" + (equal "src_emacs-lisp[ :exports code]{(+ 1 1)}" (org-test-with-temp-text "src_emacs-lisp[ :exports code ]{(+ 1 1)}" (let ((org-babel-inline-result-wrap "=%s=") (org-export-use-babel t)) @@ -220,14 +220,14 @@ Here is one at the end of a line. {{{results(=2=)}}} (buffer-string)))) ;; Do not escape characters in inline source blocks. (should - (equal "src_c[]{*a}" + (equal "src_c[ :exports code]{*a}" (org-test-with-temp-text "src_c[ :exports code ]{*a}" (let ((org-babel-inline-result-wrap "=%s=") (org-export-use-babel t)) (org-babel-exp-process-buffer)) (buffer-string)))) (should - (equal "src_emacs-lisp[]{(+ 1 1)} {{{results(=2=)}}}" + (equal "src_emacs-lisp[ :exports both]{(+ 1 1)} {{{results(=2=)}}}" (org-test-with-temp-text "src_emacs-lisp[:exports both]{(+ 1 1)}" (let ((org-babel-inline-result-wrap "=%s=") (org-export-use-babel t)) @@ -262,10 +262,10 @@ Here is one at the end of a line. {{{results(=2=)}}} (string-match (replace-regexp-in-string "\\\\\\[]{" "\\(?:\\[]\\)?{" ;accept both src_sh[]{...} or src_sh{...} - (regexp-quote "Here is one in the middle src_sh[]{echo 1} of a line. -Here is one at the end of a line. src_sh[]{echo 2} -src_sh[]{echo 3} Here is one at the beginning of a line. -Here is one that is also evaluated: src_sh[]{echo 4} {{{results(=4=)}}}") + (regexp-quote "Here is one in the middle src_sh[ :exports code]{echo 1} of a line. +Here is one at the end of a line. src_sh[ :exports code]{echo 2} +src_sh[ :exports code]{echo 3} Here is one at the beginning of a line. +Here is one that is also evaluated: src_sh[ :exports both]{echo 4} {{{results(=4=)}}}") nil t) (org-test-at-id "cd54fc88-1b6b-45b6-8511-4d8fa7fc8076" (org-narrow-to-subtree) @@ -301,7 +301,7 @@ be evaluated." (ert-deftest ob-exp/exports-inline-code-double-eval-exports-both () (let ((org-export-use-babel t)) (should - (string-match (concat "\\`src_emacs-lisp\\(?:\\[]\\)?{(\\+ 1 1)} " + (string-match (concat "\\`src_emacs-lisp\\(?:\\[.+?]\\)?{(\\+ 1 1)} " "{{{results(src_emacs-lisp\\[ :exports code\\]{2})}}}$") (org-test-with-temp-text (concat "src_emacs-lisp[:exports both :results code " @@ -403,7 +403,7 @@ be evaluated." : 2 #+NAME: src1 -#+begin_src emacs-lisp +#+begin_src emacs-lisp :exports both \(+ 1 1) #+end_src" (org-test-with-temp-text