emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Olivier Lischer <olivier.lischer@liolin.ch>
To: emacs-orgmode@gnu.org
Subject: [PATCH] ob-tangle: Add flag to optionally remove files before writing
Date: Tue, 23 Jan 2024 21:25:11 +0100	[thread overview]
Message-ID: <877cjzhjtg.fsf@liolin.ch> (raw)

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

In December last year was a change introduced, that a file should not be
removed before
tangling (https://lists.gnu.org/r/emacs-orgmode/2021-05/msg00337.html).
In an older bug report
(https://lists.gnu.org/r/emacs-orgmode/2021-05/msg00337.html)
the decision was to remove the file before writing.
I added a variable to switch between both behaviors.

Any feedback or advice is welcome.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-tangle-Add-flag-to-optionally-remove-files-before.patch --]
[-- Type: text/x-patch, Size: 2695 bytes --]

From 4f2f2a0dcca82a934144636da478a9243e514f66 Mon Sep 17 00:00:00 2001
From: Olivier Lischer <olivier.lischer@liolin.ch>
Date: Tue, 23 Jan 2024 21:02:20 +0100
Subject: [PATCH] ob-tangle: Add flag to optionally remove files before writing

* lisp/ob-tangle.el: Add variable
`org-babel-tangle-remove-file-before-write'.
(org-babel-tangle): Remove file before writing when
`org-babel-tangle-remove-file-before-write' is set.

The variable `org-babel-tangle-remove-file-before-write' adds support
for the current and old behaviour of `org-babel-tangle'.

TINYCHANGE
---
 etc/ORG-NEWS      |  4 ++++
 lisp/ob-tangle.el | 12 +++++++++---
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index f537486d4..c7c3ee264 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -800,6 +800,10 @@ Completion is enabled for links to man pages added using ~org-insert-link~:
 =C-c C-l man RET emacscl TAB= to get =emacsclient=.  Of course, the ~ol-man~
 library should be loaded first.
 
+*** =ob-tangle.el=: Add flag to optionally remove files before writing
+
+When ~org-babel-tangle-remove-file-before-write~ is set to ~t~ the file is removed before writing.
+
 ** New functions and changes in function arguments
 *** New API functions to store data within ~org-element-cache~
 
diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index 72089a9a5..37fcd194c 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -166,6 +166,12 @@ read-write permissions for the user, read-only for everyone else."
   :package-version '(Org . "9.6")
   :type 'integer)
 
+(defcustom org-babel-tangle-remove-file-before-write nil
+  "Prevents the deletion of an existing file before tangle"
+  :group 'org-babel-tangle
+  :package-version '(Org . "9.7")
+  :type 'boolean)
+
 (defun org-babel-find-file-noselect-refresh (file)
   "Find file ensuring that the latest changes on disk are
 represented in the file."
@@ -313,9 +319,9 @@ matching a regular expression."
                                       (compare-buffer-substrings
                                        nil nil nil
                                        tangle-buf nil nil)))))))
-                     ;; We do not erase, but overwrite previous file
-                     ;; to preserve any existing symlinks.
-		     (write-region nil nil file-name)
+                     (when (and (file-exists-p file-name) org-babel-tangle-remove-file-before-write)
+                       (delete-file file-name))
+                     (write-region nil nil file-name)
 		     (mapc (lambda (mode) (set-file-modes file-name mode)) modes))
                    (push file-name path-collector))))))
 	 (if (equal arg '(4))
-- 
2.43.0


             reply	other threads:[~2024-01-23 20:32 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-23 20:25 Olivier Lischer [this message]
2024-01-24 16:38 ` [PATCH] ob-tangle: Add flag to optionally remove files before writing Ihor Radchenko
2024-01-28 19:07   ` Olivier Lischer
2024-01-28 21:11     ` Ihor Radchenko
2024-01-29 18:32       ` Olivier Lischer
2024-01-30 11:36         ` Ihor Radchenko
     [not found]           ` <87sf1fmwnc.fsf@localhost>
     [not found]             ` <87jzmrggcw.fsf@liolin.ch>
2024-02-28 11:07               ` Ihor Radchenko
2024-02-28 19:01                 ` Olivier Lischer
2024-02-29 10:43                   ` 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=877cjzhjtg.fsf@liolin.ch \
    --to=olivier.lischer@liolin.ch \
    --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).