From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id wFKMJD1sQmOjGAEAbAwnHQ (envelope-from ) for ; Sun, 09 Oct 2022 08:37:49 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id wOu3Iz1sQmNo1gAAG6o9tA (envelope-from ) for ; Sun, 09 Oct 2022 08:37:49 +0200 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 222C81FEB1 for ; Sun, 9 Oct 2022 08:37:49 +0200 (CEST) Received: from localhost ([::1]:36666 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ohPwa-0002xy-85 for larch@yhetil.org; Sun, 09 Oct 2022 02:37:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:38040) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ohPv2-0002v7-V6 for emacs-orgmode@gnu.org; Sun, 09 Oct 2022 02:36:12 -0400 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]:46748) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ohPv0-0004Z0-Ii for emacs-orgmode@gnu.org; Sun, 09 Oct 2022 02:36:12 -0400 Received: by mail-pl1-x62b.google.com with SMTP id l1so7878079pld.13 for ; Sat, 08 Oct 2022 23:36:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:from:to:cc:subject:date:message-id:reply-to; bh=eyud73zoO/94ZIz+MMQ+Xcal3rGNsgIGVjvT6MKSZd8=; b=n8zWSWzq+5tx9u56Q1+9rZbJihi1ZMsHD56g4JAeyOq6t/cmZ3k1q2o6ePbaAut4E+ +k59/0T4RSgd3/mT5BVikEKLBq4WUxreK1W+vun7VRmkvirDj07NRfupMCMKfTv7iuLZ tAyQLcoM9JlCPGkda/F8pjBIE0/Syh8TaO9q5dJifxRLPuYtVk9WoXJNRx2i/R+Gs5kh dUR5pFvVMKpqAqW/Aatp8Mq9xtHe/5z9ZNcyd5XirdsTScGbaMHCtzMwB/pgsOSc7Oi7 oKPjR/dVv62j8YOkxlVWTeFlxUBdovUIrn6YEC0FQ8tb/RDKX1/xLlhURySqTH425CMG 7mRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eyud73zoO/94ZIz+MMQ+Xcal3rGNsgIGVjvT6MKSZd8=; b=RbRxUZ2CEGTGsMwLhG/VBmTWk3l5eup6CwVjW0wi4gDs6wkWX8YOJPpcqRXy93G5VF URhcfnZiAgQXFy/2rzGkgvvomNQwLgPuFyk+QDLehfjNADNps9lBRA1fqTCkVUtrf8R/ r19HvEJBxwYhRI1Seaonh4VbI/jdjWNY1xfT5NL/cQG2MUCnkgtNMxK5FqJH1+nrZuZT InQv8wZKzhzymu0KQzsRr/E4Ea78sBGTBm5fPp9wtoFd2+fPsX3A7Oeo+bDOho5O6F01 GLbrs7WNJKS6srD/o1iUgw+k5avPWUsYe1sT3L7BkyQm1yJ4H6XJGkVz98G5IwyFY2HD 7c+Q== X-Gm-Message-State: ACrzQf1anweI4ynaqrZvxdg/JQRqas9f1bKUohXuk7+zP2Pq9FxnGBx3 pgPYyWXxhbtLm2NTpGK944E= X-Google-Smtp-Source: AMsMyM72d99noUH6VQ9IUi/TqlRLlyl0MSiZp4VCSK4OR6ma0PxSdJ1pMWKYFMVDY54Ozq/QjahMQw== X-Received: by 2002:a17:90a:1d03:b0:202:d174:d7cc with SMTP id c3-20020a17090a1d0300b00202d174d7ccmr14197003pjd.73.1665297368442; Sat, 08 Oct 2022 23:36:08 -0700 (PDT) Received: from localhost ([2409:8970:af2:32f:6c23:3ad5:f37d:541b]) by smtp.gmail.com with ESMTPSA id q27-20020aa7983b000000b005625d5ae760sm4556152pfl.11.2022.10.08.23.36.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Oct 2022 23:36:07 -0700 (PDT) From: Ihor Radchenko To: Rudolf =?utf-8?Q?Adamkovi=C4=8D?= Cc: "Fraga, Eric" , "emacs-orgmode@gnu.org" Subject: Re: The Org mode in the Org Git does not export In-Reply-To: References: <87y1u7peo2.fsf@ucl.ac.uk> <871qrwgptv.fsf@localhost> <877d1nfxpj.fsf@localhost> <87a66iap48.fsf@localhost> <87o7uy5lng.fsf@localhost> <87edvs2qlj.fsf@localhost> <875ygwmc74.fsf@localhost> <87zge8j5iu.fsf@localhost> Date: Sun, 09 Oct 2022 14:36:58 +0800 Message-ID: <87sfjxec51.fsf@localhost> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::62b; envelope-from=yantar92@gmail.com; helo=mail-pl1-x62b.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 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, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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" X-Migadu-Flow: FLOW_IN X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1665297469; 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=eyud73zoO/94ZIz+MMQ+Xcal3rGNsgIGVjvT6MKSZd8=; b=bLoulXQ3yIMKgdBMIjKlqIRxE10N6Idc9qp8zuJd3/z8BfkUC+RMUxd21H2pfYpnKq4Pv5 XY3C1mtYHtmAmKxja5BkD4YO93iCa1PLCFU+wlP+P28CvInY+qH6Vw3PHhoZe1V7SzcmZC aNGkTiOnBZvG4EtlkfYJq/3kDo2Mi9769KnFiYAvGPyq4iKOUppw5hnwqAbhlPA8zmLDyP mm+ouF//oxnSF7FdueCI6QKmL4XlY+i/wwX7Oiqkf37hXzilUlrjbmCbIoRFkIlTgKAIKO ReuIGklamuKseBkUiNBe2hdDg/rTtz+h4Co9zAeIFi9UiZuH/aMQi2c081do2A== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1665297469; a=rsa-sha256; cv=none; b=F9pryLHus760tk50+gRyoxM6qOgx9FIw0F4DlIIvjQyejh0DGktIh4TXaaToMGBmVn7TMt L+JmACIjilFdRGShjRyqceZm728mSEI/Sa8B+lbmNMzZqiTIVBPb2Pr6Jqpco08Nsp7JC8 gPd+8rRDZoQeVRAoYyWR3B1naMMdSuciUlr588B9VKDt87++kUwMGRimUqTcB7bEBdi7Ld rgG0gLdH7M+sfQ2Tmc2zX0t+Jwosz7aQuiKRXH9DRpFMtnBw92s4/OwNg1cEFeiWW/PmY+ FFKXtQkQekiwzoAN4ayiNuTcMVp7FrTvUK6BUOdIb6ILavFHaDP3Xt64FySAjA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=n8zWSWzq; dmarc=pass (policy=none) header.from=gmail.com; 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" X-Migadu-Spam-Score: -3.69 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=n8zWSWzq; dmarc=pass (policy=none) header.from=gmail.com; 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" X-Migadu-Queue-Id: 222C81FEB1 X-Spam-Score: -3.69 X-Migadu-Scanner: scn0.migadu.com X-TUID: 72wzt2EkPe30 --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Rudolf Adamkovi=C4=8D writes: >>> (1) re-opening the file after export always re-opens it, and >> >> I am not sure how to test this part. > > How about the attached patch? > > It mirrors the manual steps (find-file, export, kill-buffer, find-file), > except it calls just the 'org-export-copy-buffer' procedure instead of > exporting to HTML. The test fails without your fix and passes with it. The test in the patch is erroneous. `org-export-copy-buffer', unless it kills the generated buffer, does nothing about `buffer-file-name' local variable. The patch is only working by accident because `find-file' opens the first buffer with matching `buffer-file-name'. If there are multiple such buffers, the return value depends on their order. I have updated `org-export-with-buffer-copy' macro to take care about this `buffer-file-name' issue uniformly without a need to put workarounds into every caller. I have also updated your patch using a more appropriate testing assertions. It now uses the updated macro. See the attached patches. (I'd like this patches to be tested on real export first as it is touching on very basic export routines). --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: inline; filename=0001-org-export-with-buffer-copy-Allow-safe-reusable-buff.patch Content-Transfer-Encoding: quoted-printable >From e8e4065da6e166e9859e14c95558e385d320dcbe Mon Sep 17 00:00:00 2001 Message-Id: From: Ihor Radchenko Date: Sun, 9 Oct 2022 14:16:41 +0800 Subject: [PATCH 1/2] org-export-with-buffer-copy: Allow safe reusable buffer copy MIME-Version: 1.0 Content-Type: text/plain; charset=3DUTF-8 Content-Transfer-Encoding: 8bit * lisp/ox.el (org-export--generate-copy-script): (org-export-copy-buffer): Accept cl-style key arguments. * lisp/ox.el (org-export-with-buffer-copy): Allow optional cl-style key arguments for fine-grained control on what to copy from the target buffer and which buffer to use as a target. Do not kill the target buffer upon finished, when the target is provided. Make sure that remaining buffer copy does not preserve `buffer-file-name' from the original buffer and that it never overwrites the file associated with the original buffer. * lisp/ox-html.el (org-html-format-latex): Use the new version of `org-export-with-buffer-copy' instead of managing edge cases by itself. Reported-by: Rudolf Adamkovi=C4=8D Link: https://orgmode.org/list/87zge8j5iu.fsf@localhost --- lisp/ox-html.el | 27 +++++----------- lisp/ox.el | 82 ++++++++++++++++++++++++++++++------------------- 2 files changed, 59 insertions(+), 50 deletions(-) diff --git a/lisp/ox-html.el b/lisp/ox-html.el index 3f55be2ee..cad06aebf 100644 --- a/lisp/ox-html.el +++ b/lisp/ox-html.el @@ -2879,25 +2879,14 @@ (defun org-html-format-latex (latex-frag processing= -type info) ;; temporary buffer so that dvipng/imagemagick can properly ;; turn the fragment into an image. (setq latex-frag (concat latex-header latex-frag)))) - (with-current-buffer - (org-export-copy-buffer - (get-buffer-create " *Org HTML Export LaTeX*") - 'drop-visible 'drop-narrowing 'drop-contents) - (erase-buffer) - (insert latex-frag) - (org-format-latex cache-relpath nil nil cache-dir nil - "Creating LaTeX Image..." nil processing-type) - ;; Present save dialogue to be shown for this buffer and prevent - ;; Emacs to jump into this buffer when opening - ;; `buffer-file-name' file. We need this because - ;; `org-export-copy-buffer' copies `buffer-file-name' local - ;; variable thus making Emacs think that the buffer copy is - ;; associated with file. Note that despite `buffer-file-name', - ;; `org-export-copy-buffer' arranges saving to not perform - ;; actual writing onto the disk. - (setq buffer-file-name nil) - (restore-buffer-modified-p nil) - (buffer-string)))) + (org-export-with-buffer-copy + :to-buffer (get-buffer-create " *Org HTML Export LaTeX*") + :drop-visibility t :drop-narrowing t :drop-contents t + (erase-buffer) + (insert latex-frag) + (org-format-latex cache-relpath nil nil cache-dir nil + "Creating LaTeX Image..." nil processing-type) + (buffer-string)))) =20 (defun org-html--wrap-latex-environment (contents _ &optional caption labe= l) "Wrap CONTENTS string within appropriate environment for equations. diff --git a/lisp/ox.el b/lisp/ox.el index e059983fb..69a95ffe5 100644 --- a/lisp/ox.el +++ b/lisp/ox.el @@ -2544,9 +2544,9 @@ ;;; Core functions ;; a default template (or a back-end specific template) at point or in ;; current subtree. =20 -(defun org-export-copy-buffer (&optional buffer drop-visibility - drop-narrowing drop-contents - drop-locals) +(cl-defun org-export-copy-buffer (&key to-buffer drop-visibility + drop-narrowing drop-contents + drop-locals) "Return a copy of the current buffer. The copy preserves Org buffer-local variables, visibility and narrowing. @@ -2561,61 +2561,81 @@ (defun org-export-copy-buffer (&optional buffer dro= p-visibility Emacs save dialogue. Prefer using `org-export-with-buffer-copy' macro when possible. =20 -When optional argument BUFFER is non-nil, copy into BUFFER. +When optional key `:to-buffer' is non-nil, copy into BUFFER. =20 -Optional arguments DROP-VISIBILITY, DROP-NARROWING, DROP-CONTENTS, and -DROP-LOCALS are passed to `org-export--generate-copy-script'." +Optional keys `:drop-visibility', `:drop-narrowing', `:drop-contents', +and `:drop-locals' are passed to `org-export--generate-copy-script'." (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer) - 'do-not-check-unreadable - drop-visibility - drop-narrowing - drop-contents - drop-locals)) - (new-buf (or buffer (generate-new-buffer (buffer-name))))) + :copy-unreadable 'do-not-check + :drop-visibility drop-visibility + :drop-narrowing drop-narrowing + :drop-contents drop-contents + :drop-locals drop-locals)) + (new-buf (or to-buffer (generate-new-buffer (buffer-name))))) (with-current-buffer new-buf (funcall copy-buffer-fun) (set-buffer-modified-p nil)) new-buf)) =20 -(defmacro org-export-with-buffer-copy (&rest body) +(cl-defmacro org-export-with-buffer-copy ( &rest body + &key to-buffer drop-visibility + drop-narrowing drop-contents + drop-locals + &allow-other-keys) "Apply BODY in a copy of the current buffer. The copy preserves local variables, visibility and contents of the original buffer. Point is at the beginning of the buffer -when BODY is applied." +when BODY is applied. + +Optional keys can modify what is being copied and the generated buffer +copy. `:to-buffer', `:drop-visibility', `:drop-narrowing', +`:drop-contents', and `:drop-locals' are passed as arguments to +`org-export-copy-buffer'." (declare (debug t)) (org-with-gensyms (buf-copy) - `(let ((,buf-copy (org-export-copy-buffer))) + `(let ((,buf-copy (org-export-copy-buffer + :to-buffer ,to-buffer + :drop-visibility ,drop-visibility + :drop-narrowing ,drop-narrowing + :drop-contents ,drop-contents + :drop-locals ,drop-locals))) (unwind-protect (with-current-buffer ,buf-copy (goto-char (point-min)) - (progn ,@body)) + (prog1 + (progn ,@body) + ;; `org-export-copy-buffer' carried the value of + ;; `buffer-file-name' from the original buffer. When not + ;; killed, the new buffer copy may become a target of + ;; `find-file'. Prevent this. + (setq buffer-file-name nil))) (and (buffer-live-p ,buf-copy) ;; Kill copy without confirmation. (progn (with-current-buffer ,buf-copy (restore-buffer-modified-p nil)) - (kill-buffer ,buf-copy))))))) - -(defun org-export--generate-copy-script (buffer - &optional - copy-unreadable - drop-visibility - drop-narrowing - drop-contents - drop-locals) + (unless ,to-buffer + (kill-buffer ,buf-copy)))))))) + +(cl-defun org-export--generate-copy-script (buffer + &key + copy-unreadable + drop-visibility + drop-narrowing + drop-contents + drop-locals) "Generate a function duplicating BUFFER. =20 The copy will preserve local variables, visibility, contents and narrowing of the original buffer. If a region was active in BUFFER, contents will be narrowed to that region instead. =20 -When optional argument COPY-UNREADABLE is non-nil, do not ensure that -all the copied local variables will be readable in another Emacs -session. +When optional key `:copy-unreadable' is non-nil, do not ensure that all +the copied local variables will be readable in another Emacs session. =20 -When optional arguments DROP-VISIBILITY, DROP-NARROWING, -DROP-CONTENTS, or DROP-LOCALS are non-nil, do not preserve visibility, -narrowing, contents, or local variables correspondingly. +When optional keys `:drop-visibility', `:drop-narrowing', +`:drop-contents', or `:drop-locals' are non-nil, do not preserve +visibility, narrowing, contents, or local variables correspondingly. =20 The resulting function can be evaluated at a later time, from another buffer, effectively cloning the original buffer there. --=20 2.35.1 --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0002-Add-a-regression-test-for-org-export-with-buffer-cop.patch >From 6ed84c657f4bb7a6427c0c157d69d98542531260 Mon Sep 17 00:00:00 2001 Message-Id: <6ed84c657f4bb7a6427c0c157d69d98542531260.1665297377.git.yantar92@gmail.com> In-Reply-To: References: From: Ihor Radchenko Date: Sun, 9 Oct 2022 14:31:10 +0800 Subject: [PATCH 2/2] Add a regression test for `org-export-with-buffer-copy' * testing/lisp/test-ox.el (test-org-export/org-export-copy-buffer): Make sure that `org-export-with-buffer-copy' does not show up when Emacs is searching for buffers associated with file. --- testing/lisp/test-ox.el | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el index 90a9eb3aa..42867919b 100644 --- a/testing/lisp/test-ox.el +++ b/testing/lisp/test-ox.el @@ -63,20 +63,32 @@ ;;; Internal Tests (ert-deftest test-org-export/org-export-copy-buffer () "Test `org-export-copy-buffer' specifications." - ;; The buffer copy must not cause overwriting the original file - ;; buffer under any circumstances. + ;; The copy must not overwrite the original file. (org-test-with-temp-text-in-file "* Heading" (let ((file (buffer-file-name))) (with-current-buffer (org-export-copy-buffer) - (insert "This must not go into actual file.") + (insert "This must not go into the original file.") (save-buffer) (should (equal "* Heading" (with-temp-buffer (insert-file-contents file) - (buffer-string)))))))) + (buffer-string))))))) + ;; The copy must not show when re-opening the original file. + (org-test-with-temp-text-in-file + "* Heading" + (let ((file (buffer-file-name)) + (buffer-copy (generate-new-buffer " *Org export copy*"))) + (org-export-with-buffer-copy + :to-buffer buffer-copy + (insert "This must not show as the original file.") + (save-buffer)) + ;; Unassign the original buffer from file. + (setq buffer-file-name nil) + (should-not + (equal buffer-copy (get-file-buffer file)))))) (ert-deftest test-org-export/bind-keyword () "Test reading #+BIND: keywords." -- 2.35.1 --=-=-= Content-Type: text/plain -- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at . Support Org development at , or support my work at --=-=-=--