Ihor Radchenko <yanta...@posteo.net> writes: > Olivier Lischer <olivier.lisc...@liolin.ch> writes: > >>> Thanks for the patch, but may you please explain why introducing such >>> variable is useful? >> >> Sure. >> >> I configure all my .dotfiles in an Org mode file and tangle the >> configuration in the right places. The tangled files are all >> read-only to prevent accidentally editing of the "right" configuration >> file. With the current tangling mechanism, this results in a "Permission >> denied" error because the function writes to a read-only file. In a >> earlier version this use case was possible because the file was >> recreated before writing to it. > > Thanks for the explanation! > > I suggest changing your patch, setting the default value of > `org-babel-tangle-remove-file-before-write' to 'auto. > This will keep the current behaviour but fall back to delete + write new > file when the tangle target is read-only. > That will avoid feature regression.
I changed the path as you proposed.
>From a464592f622ba42732a5449d2c4db51f1975444a Mon Sep 17 00:00:00 2001 From: Olivier Lischer <olivier.lisc...@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 | 14 ++++++++++---- 2 files changed, 14 insertions(+), 4 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..64b7c2cf8 100644 --- a/lisp/ob-tangle.el +++ b/lisp/ob-tangle.el @@ -141,7 +141,7 @@ controlled by the :comments header argument." :version "24.1" :type 'string) -(defcustom org-babel-tangle-uncomment-comments nil +(defcustom org-babel-tangle-uncomment-comments 'auto "Inhibits automatic commenting and addition of trailing newline of tangle comments. Use `org-babel-tangle-comment-format-beg' and `org-babel-tangle-comment-format-end' to customize the format @@ -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