all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH] Make command org-babel-detangle work interactively
@ 2024-01-19 12:43 gerard.vermeulen
  2024-01-19 15:20 ` Ihor Radchenko
  0 siblings, 1 reply; 6+ messages in thread
From: gerard.vermeulen @ 2024-01-19 12:43 UTC (permalink / raw)
  To: Emacs orgmode

[-- 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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-01-20 18:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-19 12:43 [PATCH] Make command org-babel-detangle work interactively gerard.vermeulen
2024-01-19 15:20 ` 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

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.