emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: gerard.vermeulen@posteo.net
To: Emacs orgmode <emacs-orgmode@gnu.org>
Subject: [PATCH] Make command org-babel-detangle work interactively
Date: Fri, 19 Jan 2024 12:43:57 +0000	[thread overview]
Message-ID: <4e7b198382642ed3572e3fe42e6d1abc@posteo.net> (raw)

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

Hi,

org-babel-detangle is an interactive command but
M-x org-babel-detangle always replies "Detangled 0 code blocks"
This patch makes org-babel-detangle prompt for a source code file.

Obviously, I got bitten by this.

The patch contains
(user-error "Source code file does %S not exist" source-code-file)
so it may break code that uses org-babel-detangle in a loop.

I prefer an user-error, but others may not.

Regards -- Gerard

[-- Attachment #2: 0001-Make-command-org-babel-detangle-work-interactively.patch --]
[-- Type: application/octet-stream, Size: 2877 bytes --]

From 7549165451b1ecb1b7d2ad7a9718d1aa98aa28fe Mon Sep 17 00:00:00 2001
From: Gerard Vermeulen <gerard.vermeulen@posteo.net>
Date: Fri, 19 Jan 2024 13:09:54 +0100
Subject: [PATCH] Make command org-babel-detangle work interactively

org-babel-detangle: prompt for a file name when none is given

* lisp/ob-tangle.el (org-babel-detangle): Raise a user-error when the
source code file does not exist after prompting for a file name when
none is given.
---
 lisp/ob-tangle.el | 41 +++++++++++++++++++++++------------------
 1 file changed, 23 insertions(+), 18 deletions(-)

diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index 933a3eba1..726f733bd 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -618,27 +618,32 @@ by `org-babel-get-src-block-info'."
 
 ;; de-tangling functions
 (defun org-babel-detangle (&optional source-code-file)
-  "Propagate changes in source file back original to Org file.
+  "Propagate changes in SOURCE-CODE-FILE back to original Org file.
 This requires that code blocks were tangled with link comments
 which enable the original code blocks to be found."
   (interactive)
-  (save-excursion
-    (when source-code-file (find-file source-code-file))
-    (goto-char (point-min))
-    (let ((counter 0) new-body end)
-      (while (re-search-forward org-link-bracket-re nil t)
-        (if (and (match-string 2)
-		 (re-search-forward
-		  (concat " " (regexp-quote (match-string 2)) " ends here") nil t))
-	    (progn (setq end (match-end 0))
-		   (forward-line -1)
-		   (save-excursion
-		     (when (setq new-body (org-babel-tangle-jump-to-org))
-		       (org-babel-update-block-body new-body)))
-		   (setq counter (+ 1 counter)))
-	  (setq end (point)))
-        (goto-char end))
-      (prog1 counter (message "Detangled %d code blocks" counter)))))
+  (unless source-code-file
+    (setq source-code-file (read-file-name "Source code file: ")))
+  (if (file-exists-p source-code-file)
+      (save-excursion
+        (find-file source-code-file)
+        (goto-char (point-min))
+        (let ((counter 0) new-body end)
+          (while (re-search-forward org-link-bracket-re nil t)
+            (if (and (match-string 2)
+		     (re-search-forward
+		      (concat " " (regexp-quote (match-string 2)) " ends here")
+                      nil t))
+	        (progn (setq end (match-end 0))
+		       (forward-line -1)
+		       (save-excursion
+		         (when (setq new-body (org-babel-tangle-jump-to-org))
+		           (org-babel-update-block-body new-body)))
+		       (setq counter (+ 1 counter)))
+	      (setq end (point)))
+            (goto-char end))
+          (prog1 counter (message "Detangled %d code blocks" counter))))
+    (user-error "Source code file does %S not exist" source-code-file)))
 
 (defun org-babel-tangle-jump-to-org ()
   "Jump from a tangled code file to the related Org mode file."
-- 
2.42.0


             reply	other threads:[~2024-01-19 12:45 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-19 12:43 gerard.vermeulen [this message]
2024-01-19 15:20 ` [PATCH] Make command org-babel-detangle work interactively Ihor Radchenko
2024-01-19 23:14   ` gerard.vermeulen
2024-01-20 12:18     ` Ihor Radchenko
2024-01-20 14:10       ` gerard.vermeulen
2024-01-20 18:36         ` Ihor Radchenko

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=4e7b198382642ed3572e3fe42e6d1abc@posteo.net \
    --to=gerard.vermeulen@posteo.net \
    --cc=emacs-orgmode@gnu.org \
    /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).