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: Wed, 15 Jan 2025 07:15:04 -0700	[thread overview]
Message-ID: <CAMbmz5n=npK+3-Vw9k67Mz+L2us2QFj=dXGrjGtgnbLyjq2AOA@mail.gmail.com> (raw)
In-Reply-To: <CAMbmz5kK2Nu2GAEzRZdgxxDETVGCa8ELcUBowP3O8VoOCqtqtg@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 7365 bytes --]

Before I submit the updated patch, does this look like a better approach?

In the test for `org-base-buffer-file-name' I changed it to explicitly find
the current buffer file name and use that:

(ert-deftest test-org-base-buffer-file-name ()

  "Test `org-base-buffer-file-name'."

  (should

   (org-test-with-temp-text-in-file

    "File"

    (let ((base-filename (buffer-file-name)))

      (and

       ;; Confirm that we get the same answer whether we provide the buffer
or use the default
       (equal base-filename (org-base-buffer-file-name))

       (equal base-filename (org-base-buffer-file-name
(current-buffer)))))))
  (should

   (org-test-with-temp-text-in-file

    "File with capture buffer"

    (let ((org-capture-templates '(("t" "Test" entry (here) "* Test
Header\n\n")))
          (base-filename (buffer-file-name)))

      (org-capture nil "t")

      (and

       ;; Confirm that we get the same answer whether we provide the buffer
or use the default
       (equal base-filename (org-base-buffer-file-name))

       (equal base-filename (org-base-buffer-file-name (current-buffer)))

       (equal base-filename (org-base-buffer-file-name (buffer-base-buffer
(current-buffer))))
       ))))

  (should-not

   (org-test-with-temp-text

    "Buffer without file"

    (org-base-buffer-file-name))))


For the tangle test, I also capture the base buffer filename, use an
explicit tangle filename, and ensure that the call to `org-babel-tangle'
returns the expected list of tangled files:

;; 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))))

     (should

      (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

              (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))))))



On Tue, Jan 14, 2025 at 12:26 PM Derek Chen-Becker <derek@chen-becker.org>
wrote:

> Thanks for the comments! This is my first real foray into non-trivial
> elisp so I really appreciate your patience and help. Let me try and get
> these addressed.
>
> Cheers,
>
> derek
>
> On Tue, Jan 14, 2025 at 10:54 AM Ihor Radchenko <yantar92@posteo.net>
> wrote:
>
>> Derek Chen-Becker <derek@chen-becker.org> writes:
>>
>> > Thanks for the help on this! I've reworked the changes into two patches
>> > (one for the README and one for tangle) and I think I've covered your
>> > concerns. I also added a unit test for the org-base-buffer-file-name
>> > function to cover the miss on provided buffers. Please let me know if
>> you
>> > have any questions.
>>
>> Thanks!
>> Although, I am afraid that the second set of unit tests is a bit much
>> - they no longer fit within TINYCHANGE (15-20 significant LOC).
>> We have a legal limit on how much code we can accept without requiring
>> copyright assignment. See
>> https://orgmode.org/worg/org-contribute.html#copyright
>>
>> More comments below.
>>
>> > * lisp/ob-tangle.el (org-babel-tangle): Update to utilize the new
>> > org-base-buffer-file-name function.
>>
>> Our convention is to quote Elisp symbols in commit messages like `this'.
>>
>> > 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
>>
>> Another convention is double space between sentences.
>>
>> > +(defun org-base-buffer-file-name (&optional BUFFER)
>>
>> Elisp convention is to use lower case for function arguments.
>> Upper case in the docstring is used to highlight the function arguments;
>> it does not mean that the arguments themselves need to be in upper case.
>>
>> > +  (if (buffer-base-buffer BUFFER)
>> > +      (buffer-file-name (buffer-base-buffer BUFFER))
>> > +    (buffer-file-name BUFFER)))
>>
>> Nit: Can also use `if-let*' to avoid calling `buffer-base-buffer' twice.
>>
>> > +;; 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
>> > +    (unwind-protect
>> > +        (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)))
>> > +      ;; Clean up the tangled file with the filename from
>> org-test-with-temp-text-in-file
>> > +      (delete-file (format "%s.el" file))))))
>>
>> This is not the best style.
>> While technically `file' variable will be let-bound within the
>> `org-test-with-temp-text-in-file' body, it is an internal implementation
>> detail that we should not rely upon. Instead, it is a good idea to
>> examine `buffer-file-name' inside the macro body to get the file name.
>>
>> Also, what if "%s.el" file does not exist? Say, tangling does happen,
>> but to a wrong file. Note that `delete-file' does not throw any error
>> when there is nothing to delete.
>>
>> --
>> 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  |
> +---------------------------------------------------------------+
>
>

-- 
+---------------------------------------------------------------+
| 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 #2: Type: text/html, Size: 15922 bytes --]

  reply	other threads:[~2025-01-15 14:16 UTC|newest]

Thread overview: 20+ 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 [this message]
2025-01-15 17:15                                   ` Ihor Radchenko
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='CAMbmz5n=npK+3-Vw9k67Mz+L2us2QFj=dXGrjGtgnbLyjq2AOA@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).