From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms8.migadu.com with LMTPS id yJiOJEYT32X7SAAAe85BDQ:P1 (envelope-from ) for ; Wed, 28 Feb 2024 12:04:38 +0100 Received: from aspmx1.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id yJiOJEYT32X7SAAAe85BDQ (envelope-from ) for ; Wed, 28 Feb 2024 12:04:38 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=posteo.net header.s=2017 header.b=ZTMbG9zV; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"; dmarc=pass (policy=none) header.from=posteo.net ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1709118278; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=R4OMDGckxB7t95naFbRJoawsNTuur32XvLF+mhb6RQQ=; b=iTOUrX8wBBfm2CEtg7Sp0fzgYFzPW/ADxBXMKrfGDjzTD35KSj8a66pM/o/2c6LtGpvQ49 fvXRn8kP8XbFFVJIJW1KxEFGOSu196WAfDtgh+11Q82r6BM1T9kvwzDWYE2fVClBVBRvvM GAnaoK3Y5xTixyq2XLCA6aFGEHGaBIIT9vlJDPVGrQrpPoJNFXvSep/gbPr6CtdGAWTPVa N86rHH7RgjXq6LilrUYbCKYuU2CyKCwmdapoVeQURn+5TUqYnkmQFaXPqyIfR4JFKS1Db5 PRTMdvcb8ziWw/iNcscTapEjuWdogcgb8fJWZWJGAAVsiXcKl9nLeKkHqoaBSw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=posteo.net header.s=2017 header.b=ZTMbG9zV; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"; dmarc=pass (policy=none) header.from=posteo.net ARC-Seal: i=1; s=key1; d=yhetil.org; t=1709118278; a=rsa-sha256; cv=none; b=senXrqMwlOBsWm5SNzsTu3M6bMTqC3i3GDHmI87/ol53CQ6cG0ekpmO4RiOWmCitdXBdXz gC5eioNrsgdW7i1zSv4DDSGXS2aBB/0cKFeDq3D3IjUSogmvkzTCzVys+8bbVaVqbeUUV9 RgyCcNZx9a124I1xPnq4Kk0cp2072PxkwQpItdEVXHfUtTW96LfhF87vijT5AhJyIjmqKc lXUb7JdPjVYPO3KILq7fSRPZfoGxtbrOAAaQeY+0GUsBd9PvZp/Kkqu+n4KVmKpn+nBbCP 1qM8BPtxT7K+nHmGhQ693p/Z0FQb/r+c/wOGDN5w0TFp1onaGP8ddKDdnsBmPw== Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 48D9B6A1C1 for ; Wed, 28 Feb 2024 12:04:38 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rfHj0-0000Qj-1c; Wed, 28 Feb 2024 06:03:46 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rfHiy-0000OS-Hn for emacs-orgmode@gnu.org; Wed, 28 Feb 2024 06:03:44 -0500 Received: from mout02.posteo.de ([185.67.36.66]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rfHiv-0007ts-Vs for emacs-orgmode@gnu.org; Wed, 28 Feb 2024 06:03:44 -0500 Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id DF7C1240103 for ; Wed, 28 Feb 2024 12:03:38 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1709118218; bh=DX0cfb9jejis7Y5GjXtT9aSSTdMdtDcJvtlEVFdCO/o=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type: From; b=ZTMbG9zVhGtR4ySLN9YXYtL5oiNkt+gs+RJpC7fGzML8IxlYp2UbmfDtFwKsL84y4 H8AdSUHVaRL9Z3mIRNUefzXbprfU7/YWaV7/c39BIclgaGRr43VygZd9NbPCzAuWLd ryMHl3QKLbHn+j1GkLej8tE9uCCo99dF26mDNfKP/zkIG7wcqLPsuhL8/eNLQGlsyS UgolSCFgbfvxb1kGj1qb6z5z3bA7W2kb2CGzYKZ6ctA+uLobmrHSwgU/iclx1uoDuC mFVdcV1RPkUlprbn3Nzf+fHYrR/Cx12HhCA2JxsIGVS0WzjFUDxdgM8WHVMEYgOwjD y4KGD31mxr3IA== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4TlBL16S4yz6ty0; Wed, 28 Feb 2024 12:03:37 +0100 (CET) From: Ihor Radchenko To: Olivier Lischer Cc: emacs-orgmode@gnu.org Subject: Re: [PATCH] ob-tangle: Add flag to optionally remove files before writing In-Reply-To: <87jzmrggcw.fsf@liolin.ch> References: <877cjzhjtg.fsf@liolin.ch> <87wmryelfo.fsf@localhost> <878r49i78e.fsf@liolin.ch> <87le89qi2c.fsf@localhost> <87y1c8dm65.fsf@liolin.ch> <87y1c78343.fsf@localhost> <87sf1fmwnc.fsf@localhost> <87jzmrggcw.fsf@liolin.ch> Date: Wed, 28 Feb 2024 11:07:31 +0000 Message-ID: <8734tc4zkc.fsf@localhost> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=185.67.36.66; envelope-from=yantar92@posteo.net; helo=mout02.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Scanner: mx10.migadu.com X-Migadu-Spam-Score: -7.62 X-Spam-Score: -7.62 X-Migadu-Queue-Id: 48D9B6A1C1 X-TUID: Z3QI2wf6whx7 --=-=-= Content-Type: text/plain [ Moving the discussion back to public in Org mailing list ] Olivier Lischer writes: > I hope that this time I have changed the patch the way you wanted it. > If anything is still wrong, I am happy to improve it. Thanks for the update! Unfortunately, your patch has unbalanced parenthesis and will not work. I have modified your patch to make things work and to make it conform to Elisp code conventions. I also removed spurious change in the existing variable. See the attached. Please, let me know if you want to change anything. Also, if you can, please test my patch with your use-case. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-ob-tangle-Add-flag-to-optionally-remove-files-before.patch >From b8d2a8506c9b63ad9fefbf8caaefb5de94001eb7 Mon Sep 17 00:00:00 2001 Message-ID: From: Olivier Lischer 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 new custom option `org-babel-tangle-remove-file-before-write'. (org-babel-tangle): Remove file before writing according to the value of `org-babel-tangle-remove-file-before-write'. The variable `org-babel-tangle-remove-file-before-write' adds support for the current and old behaviour of `org-babel-tangle'. Link: https://list.orgmode.org/orgmode/877cjzhjtg.fsf@liolin.ch/ Co-authored-by: Ihor Radchenko TINYCHANGE --- etc/ORG-NEWS | 10 ++++++++++ lisp/ob-tangle.el | 31 ++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index dc0d9c0ad..26f0c90f9 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -928,6 +928,16 @@ properties, links to headlines in the file can also be made more robust by using the file id instead of the file path. ** New features +*** =ob-tangle.el=: New flag to remove tangle targets before writing + +When ~org-babel-tangle-remove-file-before-write~ is set to ~t~ the +tangle target is removed before writing. This will allow overwriting +read-only tangle targets. However, when tangle target is a symlink, +this will convert the tangle target into an ordinary file. + +The default value is ~auto~ -- overwrite tangle targets when they are +read-only. + *** ~org-bibtex-yank~ accepts a prefix argument When called with a prefix argument, ~org-bibtex-yank~ adds data to the diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el index 281ab13d4..9bb69e5da 100644 --- a/lisp/ob-tangle.el +++ b/lisp/ob-tangle.el @@ -166,6 +166,23 @@ (defcustom org-babel-tangle-default-file-mode #o644 :package-version '(Org . "9.6") :type 'integer) +(defcustom org-babel-tangle-remove-file-before-write 'auto + "How to overwrite the existing tangle target. +When set to nil, `org-babel-tangle' will replace contents of an existing +tangle target (and fail when tangle target is read-only). +When set to t, the tangle target (including read-only) will be deleted +first and a new file, possibly with different ownership and +permissions, will be created. +When set to symbol `auto', overwrite read-only tangle targets and +replace contents otherwise." + :group 'org-babel-tangle + :package-version '(Org . "9.7") + :type '(choice + (const :tag "Replace contents, but keep the same file" nil) + (const :tag "Re-create file" t) + (const :tag "Re-create when read-only" auto)) + :safe t) + (defun org-babel-find-file-noselect-refresh (file) "Find file ensuring that the latest changes on disk are represented in the file." @@ -323,7 +340,19 @@ (defun org-babel-tangle (&optional arg target-file lang-re) (error "Not allowed to tangle into the same file as self")) ;; We do not erase, but overwrite previous file ;; to preserve any existing symlinks. - (write-region nil nil file-name) + ;; This behavior is modified using + ;; `org-babel-tangle-remove-file-before-write' to + ;; tangle to read-only files. + (when (and + (file-exists-p file-name) + (pcase org-babel-tangle-remove-file-before-write + (`auto (not (file-writable-p file-name))) + (`t t) + (`nil nil) + (_ (error "Invalid value of `org-babel-tangle-remove-file-before-write': %S" + 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 --=-=-= Content-Type: text/plain (For records, below is the original patch, sent off-list) > From 1523056a0dff5de64ed361d5bb1ad7d92ec7e369 Mon Sep 17 00:00:00 2001 > From: Olivier Lischer > 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 | 15 +++++++++++++-- > 2 files changed, 17 insertions(+), 2 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..b77a19d3b 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 'auto > + "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." > @@ -315,7 +321,12 @@ matching a regular expression." > tangle-buf nil nil))))))) > ;; We do not erase, but overwrite previous file > ;; to preserve any existing symlinks. > - (write-region nil nil file-name) > + ;; This behavior can be changed using > + ;; org-babel-tangle-remove-file-before-write > + ;; to tangle to read-only files. > + (when (and (file-exists-p file-name) (not (file-writeable-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.44.0 > -- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at . Support Org development at , or support my work at --=-=-=--