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, 2 Feb 2025 09:07:38 -0700 [thread overview]
Message-ID: <CAMbmz5=mktonKjSG1SPt_1Rsa4xAKjaDfnaPgXcRTUrycPBO3g@mail.gmail.com> (raw)
In-Reply-To: <87msfodvsm.fsf@localhost>
[-- Attachment #1.1: Type: text/plain, Size: 1521 bytes --]
I submitted my assignment form this week, but I'm not sure how long it
takes for that to get processed. In the meantime, I'm attaching the latest
version of my patches for capture buffer tangling, updated with feedback.
If there are any other suggestions or concerns with the patches I can
address them while we wait on assignment.
Thanks,
Derek
On Sat, Jan 18, 2025 at 7:44 AM Ihor Radchenko <yantar92@posteo.net> wrote:
> Derek Chen-Becker <derek@chen-becker.org> writes:
>
> > I'm still sorting out the copyright assignment, but I have another style
> > question: It feels like I should move the `should' clauses closer to the
> > checks so that I get a more concise report when something is wrong:
> > ...
> > Is that OK?
>
> Yes, it is fine. Also, no need to overthink things - as long as the test
> is readable, we should be good.
>
> --
> Ihor Radchenko // yantar92,
> Org mode maintainer,
> Learn more about Org mode at <https://orgmode.org/>.
> Support Org development at <https://liberapay.com/org-mode>,
> or support my work at <https://liberapay.com/yantar92>
>
--
+---------------------------------------------------------------+
| Derek Chen-Becker |
| GPG Key available at https://keybase.io/dchenbecker and |
| https://pgp.mit.edu/pks/lookup?search=derek%40chen-becker.org |
| Fngrprnt: EB8A 6480 F0A3 C8EB C1E7 7F42 AFC5 AFEE 96E4 6ACC |
+---------------------------------------------------------------+
[-- Attachment #1.2: Type: text/html, Size: 3455 bytes --]
[-- Attachment #2: 0001-testing-README-Add-details-on-use-of-TEST_NO_AUTOCLE.patch --]
[-- Type: application/octet-stream, Size: 1167 bytes --]
From eb92991889d9b8d2f9ff8b30febdafc743a4de93 Mon Sep 17 00:00:00 2001
From: Derek Chen-Becker <github@chen-becker.org>
Date: Sun, 12 Jan 2025 15:27:59 -0700
Subject: [PATCH 1/2] testing/README: Add details on use of TEST_NO_AUTOCLEAN
* testing/README: Add a section on the use of the TEST_NO_AUTOCLEAN make variable
While doing some testing I needed this functionality to debug a test and
found this variable was helpful. Add a section to the README so that
others may benefit without having to dig through makefiles.
---
testing/README | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/testing/README b/testing/README
index 1c4dd9e76..72a28d75c 100644
--- a/testing/README
+++ b/testing/README
@@ -86,6 +86,15 @@ 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.
+
+#+begin_src sh :dir (expand-file-name "..") :results silent
+make TEST_NO_AUTOCLEAN=1 test-dirty
+#+end_src
+
* Interactive testing from within Emacs
To run the Org mode test suite from a current Emacs instance simply
--
2.43.0
[-- Attachment #3: 0002-ob-tangle-Ensure-proper-tangling-within-a-capture-bu.patch --]
[-- Type: application/octet-stream, Size: 5966 bytes --]
From 5839b8b6569fd755f409620b4a26da688119cc32 Mon Sep 17 00:00:00 2001
From: Derek Chen-Becker <github@chen-becker.org>
Date: Mon, 13 Jan 2025 19:54:57 -0700
Subject: [PATCH 2/2] 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/lisp/test-org-macs.el (test-org-base-buffer-file-name): Add a
unit test to exercise the behavior of the new org-base-buffer-file-name
function, particulary around handling base vis indirect buffers.
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.
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 | 9 +++++++++
testing/lisp/test-ob-tangle.el | 23 +++++++++++++++++++++++
testing/lisp/test-org-macs.el | 29 +++++++++++++++++++++++++++++
4 files changed, 62 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..c75661b49 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -1821,6 +1821,15 @@ 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."
+ (if-let ((base-buffer (buffer-base-buffer buffer)))
+ (buffer-file-name base-buffer)
+ (buffer-file-name buffer)))
+
(provide 'org-macs)
;; Local variables:
diff --git a/testing/lisp/test-ob-tangle.el b/testing/lisp/test-ob-tangle.el
index 4c953b15d..ab6b1d891 100644
--- a/testing/lisp/test-ob-tangle.el
+++ b/testing/lisp/test-ob-tangle.el
@@ -741,6 +741,29 @@ 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>"
+ (let ((tangle-filename (format "%s.el" (buffer-file-name))))
+ (unwind-protect
+ (progn
+ (let ((org-capture-templates '(("t" "Test" entry (here) "* Test Header\n\n"))))
+ (org-capture nil "t")
+ (goto-char (point-max))
+ (insert
+ (format "
+#+begin_src elisp :tangle \"%s\" :comments org
+ (message \"FOO\")
+#+end_src" tangle-filename))
+ (search-backward "message")
+ ;; Confirm that we tangled to the right file
+ (should (equal (org-babel-tangle 4) (list tangle-filename)))))
+ ;; Clean up the tangled file with the filename from org-test-with-temp-text-in-file
+ (delete-file tangle-filename)))))
+
(provide 'test-ob-tangle)
;;; test-ob-tangle.el ends here
diff --git a/testing/lisp/test-org-macs.el b/testing/lisp/test-org-macs.el
index 93f00a4c5..dde1dccff 100644
--- a/testing/lisp/test-org-macs.el
+++ b/testing/lisp/test-org-macs.el
@@ -145,5 +145,34 @@
(butlast (decode-time (org-matcher-time "<+14h>"))
3))))))
+\f
+;;; Buffers
+
+(ert-deftest test-org-base-buffer-file-name ()
+ "Test `org-base-buffer-file-name'."
+ ;; Test direct buffer resolution
+ (org-test-with-temp-text-in-file
+ "File"
+ (let ((base-filename (buffer-file-name)))
+ ;; Confirm that we get the same answer whether we provide the buffer or use the default
+ (should (equal base-filename (org-base-buffer-file-name)))
+ (should (equal base-filename (org-base-buffer-file-name (current-buffer))))))
+ ;; Test indirect buffer resolution
+ (org-test-with-temp-text-in-file
+ "File with indirect buffer"
+ (let ((base-filename (buffer-file-name))
+ (base-buffer (current-buffer))
+ (indirect-test-buffer (make-indirect-buffer (current-buffer) "test")))
+ (set-buffer indirect-test-buffer)
+ ;; Confirm that we get the same answer for the default, the indirect buffer, and the base buffer
+ (should (equal base-filename (org-base-buffer-file-name)))
+ (should (equal base-filename (org-base-buffer-file-name base-buffer)))
+ (should (equal base-filename (org-base-buffer-file-name indirect-test-buffer)))
+ (kill-buffer indirect-test-buffer)))
+ ;; Test for a buffer with no associated file
+ (org-test-with-temp-text
+ "Buffer without file"
+ (should-not (org-base-buffer-file-name))))
+
(provide 'test-org-macs)
;;; test-org-macs.el ends here
--
2.43.0
next prev parent reply other threads:[~2025-02-02 16:08 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
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 [this message]
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='CAMbmz5=mktonKjSG1SPt_1Rsa4xAKjaDfnaPgXcRTUrycPBO3g@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).