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 <yantar92@posteo.net>

TINYCHANGE
This commit is contained in:
Olivier Lischer 2024-01-23 21:02:20 +01:00 committed by Ihor Radchenko
parent 9029470eb9
commit dd4d05a159
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 40 additions and 1 deletions

View File

@ -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. robust by using the file id instead of the file path.
** New features ** 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 *** ~org-bibtex-yank~ accepts a prefix argument
When called with a prefix argument, ~org-bibtex-yank~ adds data to the When called with a prefix argument, ~org-bibtex-yank~ adds data to the

View File

@ -166,6 +166,23 @@ read-write permissions for the user, read-only for everyone else."
:package-version '(Org . "9.6") :package-version '(Org . "9.6")
:type 'integer) :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) (defun org-babel-find-file-noselect-refresh (file)
"Find file ensuring that the latest changes on disk are "Find file ensuring that the latest changes on disk are
represented in the file." represented in the file."
@ -323,7 +340,19 @@ matching a regular expression."
(error "Not allowed to tangle into the same file as self")) (error "Not allowed to tangle into the same file as self"))
;; We do not erase, but overwrite previous file ;; We do not erase, but overwrite previous file
;; to preserve any existing symlinks. ;; 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)) (mapc (lambda (mode) (set-file-modes file-name mode)) modes))
(push file-name path-collector)))))) (push file-name path-collector))))))
(if (equal arg '(4)) (if (equal arg '(4))