From 7e75b55ccb4e82d9341b6b308b558e5d01df5128 Mon Sep 17 00:00:00 2001 From: Evgenii Klimov Date: Thu, 13 Jul 2023 18:16:08 +0100 Subject: [PATCH v2] ob-tangle.el: Add the capability to specify lexical scope in tangled files * lisp/ob-tangle.el (org-babel-tangle): Add the ability to enable lexical binding in Elisp tangled file. * etc/ORG-NEWS (=:lexical= header argument now influences tangled files): Document this change. 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. --- etc/ORG-NEWS | 7 +++++++ lisp/ob-tangle.el | 9 +++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index a4725ae8c..c632c4814 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -284,6 +284,13 @@ setting the ~STYLE~ property for each sub-task. The change is breaking when ~org-use-property-inheritance~ is set to ~t~. ** New and changed options +*** =:lexical= header argument now influences tangled files + +When extracting an Elisp src block, the target's file +~lexical-binding~ variable is set according to the src block's +=:lexical= parameter. If at least one block is set to lexical scope, +then the whole file will be with lexical scope. + *** Commands affected by ~org-fold-catch-invisible-edits~ can now be customized New user option ~org-fold-catch-invisible-edits-commands~ controls diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el index 1274d0db7..10fe5dcf5 100644 --- a/lisp/ob-tangle.el +++ b/lisp/ob-tangle.el @@ -262,7 +262,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 +273,8 @@ 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 (org-babel-emacs-lisp-lexical + (funcall get-spec :lexical))) (tangle-mode (funcall get-spec :tangle-mode))) (unless (string-equal block-lang lang) (setq lang block-lang) @@ -294,7 +296,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