Prefer `write-region' to `save-file'

* lisp/ob-haskell.el (org-babel-haskell-export-to-lhs): Use
non-interactive `insert-file-contents' + `write-region' to avoid
triggering various interactive hooks.  Ensure that temp files are
always deleted.
* lisp/org-agenda.el (org-agenda-write):
* lisp/org-table.el: Simplify code using `write-region'.
* lisp/ox-odt.el (org-odt-template): Use `insert-file-contents' +
`write-region' instead of `find-file-noselect' that may trigger
various hooks.  The new approach makes `revert-buffer' not
necessary (and do not trigger `revert-buffer' hooks).  Also, the
problem with backups will no longer exists.

Original idea: https://list.orgmode.org/orgmode/20221002035931.12191-1-dafydd.lukes@gmail.com/
This commit is contained in:
Ihor Radchenko 2023-03-18 12:34:17 +01:00
parent 6f742685d7
commit 2289739ed4
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
4 changed files with 57 additions and 77 deletions

View File

@ -255,26 +255,27 @@ constructs (header arguments, no-web syntax etc...) are ignored."
t t) t t)
(indent-code-rigidly (match-beginning 0) (match-end 0) indentation))) (indent-code-rigidly (match-beginning 0) (match-end 0) indentation)))
(save-excursion (save-excursion
;; export to latex w/org and save as .lhs (unwind-protect
(with-temp-buffer
;; Export to latex w/org and save as .lhs
(require 'ox-latex) (require 'ox-latex)
(find-file tmp-org-file) (insert-file-contents tmp-org-file)
;; Ensure we do not clutter kill ring with incomplete results. ;; Ensure we do not clutter kill ring with incomplete results.
(let (org-export-copy-to-kill-ring) (let (org-export-copy-to-kill-ring)
(org-export-to-file 'latex tmp-tex-file)) (org-export-to-file 'latex tmp-tex-file)))
(kill-buffer nil) (delete-file tmp-org-file))
(delete-file tmp-org-file) (unwind-protect
(find-file tmp-tex-file) (with-temp-buffer
(insert-file-contents tmp-tex-file)
(goto-char (point-min)) (forward-line 2) (goto-char (point-min)) (forward-line 2)
(insert "%include polycode.fmt\n") (insert "%include polycode.fmt\n")
;; ensure all \begin/end{code} statements start at the first column ;; ensure all \begin/end{code} statements start at the first column
(while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t) (while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t)
(replace-match (save-match-data (org-remove-indentation (match-string 0))) (replace-match (save-match-data (org-remove-indentation (match-string 0)))
t t)) t t))
(setq contents (buffer-string))
(save-buffer) (kill-buffer nil))
(delete-file tmp-tex-file)
;; save org exported latex to a .lhs file ;; save org exported latex to a .lhs file
(with-temp-file lhs-file (insert contents)) (write-region nil nil lhs-file))
(delete-file tmp-tex-file)))
(if (not arg) (if (not arg)
(find-file lhs-file) (find-file lhs-file)
;; process .lhs file with lhs2tex ;; process .lhs file with lhs2tex

View File

@ -3667,13 +3667,8 @@ the agenda to write."
"ox-icalendar" (file)) "ox-icalendar" (file))
(org-icalendar-export-current-agenda (expand-file-name file))) (org-icalendar-export-current-agenda (expand-file-name file)))
(t (t
(let ((bs (buffer-string))) (write-region nil nil file)
(find-file file) (message "Plain text written to %s" file)))))))
(erase-buffer)
(insert bs)
(save-buffer 0)
(kill-buffer (current-buffer))
(message "Plain text written to %s" file))))))))
(set-buffer (or agenda-bufname (set-buffer (or agenda-bufname
;; FIXME: I'm pretty sure called-interactively-p ;; FIXME: I'm pretty sure called-interactively-p
;; doesn't do what we want here! ;; doesn't do what we want here!

View File

@ -4332,14 +4332,8 @@ extension of the given file name, and finally on the variable
(table (org-table-to-lisp))) (table (org-table-to-lisp)))
(unless (fboundp transform) (unless (fboundp transform)
(user-error "No such transformation function %s" transform)) (user-error "No such transformation function %s" transform))
(let (buf) (with-temp-file file
(with-current-buffer (find-file-noselect file) (insert (funcall transform table params) "\n"))
(setq buf (current-buffer))
(erase-buffer)
(fundamental-mode)
(insert (funcall transform table params) "\n")
(save-buffer))
(kill-buffer buf))
(message "Export done.")) (message "Export done."))
(user-error "TABLE_EXPORT_FORMAT invalid"))))) (user-error "TABLE_EXPORT_FORMAT invalid")))))

View File

@ -1365,10 +1365,10 @@ original parsed data. INFO is a plist holding export options."
;; Ensure we have write permissions to this file. ;; Ensure we have write permissions to this file.
(set-file-modes (concat org-odt-zip-dir "styles.xml") #o600) (set-file-modes (concat org-odt-zip-dir "styles.xml") #o600)
;; FIXME: Who is opening an empty styles.xml before this point? (let ((styles-xml (concat org-odt-zip-dir "styles.xml")))
(with-current-buffer (with-temp-buffer
(find-file-noselect (concat org-odt-zip-dir "styles.xml") t) (when (file-exists-p styles-xml)
(revert-buffer t t) (insert-file-contents styles-xml))
;; Write custom styles for source blocks ;; Write custom styles for source blocks
;; Save STYLES used for colorizing of source blocks. ;; Save STYLES used for colorizing of source blocks.
@ -1383,32 +1383,22 @@ original parsed data. INFO is a plist holding export options."
(insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))) (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n"))))
;; Update styles.xml - take care of outline numbering ;; Update styles.xml - take care of outline numbering
;; Don't make automatic backup of styles.xml file. This setting
;; prevents the backed-up styles.xml file from being zipped in to
;; odt file. This is more of a hackish fix. Better alternative
;; would be to fix the zip command so that the output odt file
;; includes only the needed files and excludes any auto-generated
;; extra files like backups and auto-saves etc etc. Note that
;; currently the zip command zips up the entire temp directory so
;; that any auto-generated files created under the hood ends up in
;; the resulting odt file.
(setq-local backup-inhibited t)
;; Outline numbering is retained only up to LEVEL. ;; Outline numbering is retained only up to LEVEL.
;; To disable outline numbering pass a LEVEL of 0. ;; To disable outline numbering pass a LEVEL of 0.
(goto-char (point-min))
(let ((regex (let ((regex
"<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>") "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
(replacement (replacement
"<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">")) "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
(goto-char (point-min))
(while (re-search-forward regex nil t) (while (re-search-forward regex nil t)
(unless (let ((sec-num (plist-get info :section-numbers)) (unless (let ((sec-num (plist-get info :section-numbers))
(level (string-to-number (match-string 2)))) (level (string-to-number (match-string 2))))
(if (wholenump sec-num) (<= level sec-num) sec-num)) (if (wholenump sec-num) (<= level sec-num) sec-num))
(replace-match replacement t nil)))) (replace-match replacement t nil))))
(save-buffer 0)))
;; Write back the new contents.
(write-region nil nil styles-xml))))
;; Update content.xml. ;; Update content.xml.
(let* ( ;; `org-display-custom-times' should be accessed right (let* ( ;; `org-display-custom-times' should be accessed right