From 3ad56137cb709182dc1bc242cd80a1474078cb95 Mon Sep 17 00:00:00 2001 From: Evgenii Klimov Date: Thu, 13 Jul 2023 18:16:08 +0100 Subject: [PATCH] ob-tangle.el: Add new :lexical header argument * lisp/ob-tangle.el (org-babel-tangle-use-lexical-binding): Add new customization variable. (org-babel-tangle): Add ability to enable lexical binding in tangled file. * lisp/ob-core.el (org-babel-common-header-args-w-values): Add new :lexical header argument. Previously one could achieve this manually, but 1) had to keep in mind to hold this source block top-most, 2) couldn't use comment header argument, as `lexical-binding' variable must be set in the first line of a file. --- lisp/ob-core.el | 1 + lisp/ob-tangle.el | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 47410b53f..c1801a677 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -437,6 +437,7 @@ then run `org-babel-switch-to-session'." (sep . :any) (session . :any) (shebang . :any) + (lexical . ((yes no))) (tangle . ((tangle yes no :any))) (tangle-mode . ((#o755 #o555 #o444 :any))) (var . :any) diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el index 1274d0db7..916dd8489 100644 --- a/lisp/ob-tangle.el +++ b/lisp/ob-tangle.el @@ -76,6 +76,11 @@ then the name of the language is used." :group 'org-babel-tangle :type 'boolean) +(defcustom org-babel-tangle-use-lexical-binding nil + "Enable lexical binding in tangled file by default." + :group 'org-babel-tangle + :type 'boolean) + (defcustom org-babel-post-tangle-hook nil "Hook run in code files tangled by `org-babel-tangle'." :group 'org-babel-tangle @@ -262,7 +267,7 @@ expression." (when file-name (let ((lspecs (cdr by-fn)) (fnd (file-name-directory file-name)) - modes make-dir she-banged lang) + modes make-dir she-banged scoped lang) ;; drop source-blocks to file ;; We avoid append-to-file as it does not work with tramp. (with-temp-buffer @@ -273,6 +278,9 @@ expression." (get-spec (lambda (name) (cdr (assq name (nth 4 spec))))) (she-bang (let ((sheb (funcall get-spec :shebang))) (when (> (length sheb) 0) sheb))) + (lexicalp (or (string-equal (funcall get-spec :lexical) + "yes") + org-babel-tangle-use-lexical-binding)) (tangle-mode (funcall get-spec :tangle-mode))) (unless (string-equal block-lang lang) (setq lang block-lang) @@ -294,7 +302,10 @@ expression." (when (and she-bang (not she-banged)) (insert (concat she-bang "\n")) (setq she-banged t)) - (org-babel-spec-to-string spec) + (org-babel-spec-to-string spec) + (when (and lexicalp (not scoped)) + (save-excursion (elisp-enable-lexical-binding)) + (setq scoped t)) (setq block-counter (+ 1 block-counter)))) lspecs) (when make-dir -- 2.34.1