emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Derek Chen-Becker <derek@chen-becker.org>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: emacs-orgmode@gnu.org
Subject: Re: [BUG] Cannot tangle src block in capture buffer [9.7.6]
Date: Sun, 12 Jan 2025 08:52:02 -0700	[thread overview]
Message-ID: <CAMbmz5n4QqAkUQ8yN7z6on4QRmJvTrrr7mx0Zk+XhZ8_P+DBiA@mail.gmail.com> (raw)
In-Reply-To: <87msfxd81c.fsf@localhost>


[-- Attachment #1.1: Type: text/plain, Size: 218 bytes --]

OK, I've attached the patch. It wasn't clear from
https://orgmode.org/worg/org-contribute.html#first-patch whether this
should be sent in the thread or as a new thread. I can start a new one if
needed.

Thanks,

Derek

[-- Attachment #1.2: Type: text/html, Size: 825 bytes --]

[-- Attachment #2: 0001-ob-tangle-Ensure-proper-tangling-within-a-capture-bu.patch --]
[-- Type: application/octet-stream, Size: 4106 bytes --]

From fbc0fe66587228401b9ce042a0b7d361c3ec2a8f Mon Sep 17 00:00:00 2001
From: Derek Chen-Becker <github@chen-becker.org>
Date: Wed, 1 Jan 2025 20:06:10 -0700
Subject: [PATCH] ob-tangle: Ensure proper tangling within a capture buffer

* lisp/org-macs.el (org-base-buffer-file-name): Add a function to properly
resolve the filename for both direct and indirect buffers
* lisp/ob-tangle.el (org-babel-tangle): Update to utilize the new
org-base-buffer-file-name function
* testing/lisp/test-ob-tangle.el (ob-tangle/tangle-from-capture-buffer): Add
a unit test to cover the use case of tangling within a capture buffer
* testing/README: Add a section on the use of the TEST_NO_AUTOCLEAN make variable

The previous use of buffer-file-name would fail inside of a capture buffer
because it is indirect and does not have an associated filename. This
appears to be a common pitfall across Org, so I've added a helper function
to resolve the filename for both indirect and direct buffers so that it can
be used in other places within the codebase.

TINYCHANGE

Reported-by: Dilip <iDLip@protonmail.com>
Link: https://list.orgmode.org/87msfxd81c.fsf@localhost/T/#t
---
 lisp/ob-tangle.el              |  2 +-
 lisp/org-macs.el               |  7 +++++++
 testing/README                 |  5 +++++
 testing/lisp/test-ob-tangle.el | 18 ++++++++++++++++++
 4 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index 38cad78ab..64bdced84 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -269,7 +269,7 @@ matching a regular expression."
 	       (or (cdr (assq :tangle (nth 2 (org-babel-get-src-block-info 'no-eval))))
 		   (user-error "Point is not in a source code block"))))
 	    path-collector
-            (source-file buffer-file-name))
+            (source-file (org-base-buffer-file-name)))
 	(mapc ;; map over file-names
 	 (lambda (by-fn)
 	   (let ((file-name (car by-fn)))
diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index 044c49efb..6577bcc4b 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -1821,6 +1821,13 @@ indirectly called by the latter."
                    (eq (window-frame) (window-frame window))))
       (window--display-buffer buffer window 'reuse alist))))
 
+(defun org-base-buffer-file-name (&optional BUFFER)
+  "Resolve the base file name for the provided BUFFER.
+If BUFFER is not provided, default to the current buffer.  If
+BUFFER does not have a file name associated with it (e.g. a
+transient buffer) then return nil."
+  (buffer-file-name (buffer-base-buffer BUFFER)))
+
 (provide 'org-macs)
 
 ;; Local variables:
diff --git a/testing/README b/testing/README
index 1c4dd9e76..8bd3ae3fc 100644
--- a/testing/README
+++ b/testing/README
@@ -86,6 +86,11 @@ Ran 2 tests, 2 results as expected (2017-12-28 15:04:45+0100)
 ...
 #+end_example
 
+** Keep test artifacts
+
+Set the ~TEST_NO_AUTOCLEAN~ variable to leave the test directory
+around for inspection.
+
 * Interactive testing from within Emacs
 
 To run the Org mode test suite from a current Emacs instance simply
diff --git a/testing/lisp/test-ob-tangle.el b/testing/lisp/test-ob-tangle.el
index 4c953b15d..171ed7780 100644
--- a/testing/lisp/test-ob-tangle.el
+++ b/testing/lisp/test-ob-tangle.el
@@ -741,6 +741,24 @@ another block
                              bib))))
       (delete-file file))))
 
+;; See https://list.orgmode.org/87msfxd81c.fsf@localhost/T/#t
+(ert-deftest ob-tangle/tangle-from-capture-buffer ()
+  "Test tangling of source blocks from within a capture buffer.
+This is to ensure that we properly resolve the buffer name."
+  (org-test-with-temp-text-in-file
+   "* Header\n\nCapture after this point:\n<point>"
+   (should
+    (progn
+      (let ((org-capture-templates '(("t" "Test" entry (here) "* Test Header\n\n"))))
+        (org-capture nil "t")
+        (goto-char (point-max))
+        (insert "
+#+begin_src elisp :tangle yes :comments org
+  (message \"FOO\")
+#+end_src")
+        (search-backward "message")
+        (org-babel-tangle 4))))))
+
 (provide 'test-ob-tangle)
 
 ;;; test-ob-tangle.el ends here
-- 
2.43.0


  reply	other threads:[~2025-01-12 15:53 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-26 16:29 [BUG] Cannot tangle src block in capture buffer [9.7.6] Dilip
2024-08-05 14:03 ` Ihor Radchenko
2024-12-16  3:26   ` Derek Chen-Becker
2024-12-16 17:39     ` Ihor Radchenko
2024-12-19 17:56       ` Derek Chen-Becker
2024-12-19 19:17         ` Ihor Radchenko
2024-12-23 23:36           ` Derek Chen-Becker
2024-12-24  9:14             ` Ihor Radchenko
2025-01-10 15:25               ` Derek Chen-Becker
2025-01-11  9:17                 ` Ihor Radchenko
2025-01-12 15:52                   ` Derek Chen-Becker [this message]
2025-01-12 16:45                     ` Ihor Radchenko
2025-01-12 22:24                       ` Derek Chen-Becker
2025-01-13 17:23                         ` Ihor Radchenko
2025-01-14  3:01                           ` Derek Chen-Becker
2025-01-14 17:56                             ` Ihor Radchenko
2025-01-14 19:26                               ` Derek Chen-Becker
2025-01-15 14:15                                 ` Derek Chen-Becker
2025-01-15 17:15                                   ` Ihor Radchenko
2025-01-16 15:35                                     ` Derek Chen-Becker
2025-01-16 19:15                                       ` Ihor Radchenko
2025-01-18 13:41                                         ` Derek Chen-Becker
2025-01-18 14:46                                           ` Ihor Radchenko
2025-02-02 16:07                                             ` Derek Chen-Becker
2025-01-14 23:10                               ` Michael Heerdegen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAMbmz5n4QqAkUQ8yN7z6on4QRmJvTrrr7mx0Zk+XhZ8_P+DBiA@mail.gmail.com \
    --to=derek@chen-becker.org \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).