Re: [BUG] LaTeX preview should use a subdirectory in /tmp

2024-07-03 Thread Ihor Radchenko
Ihor Radchenko  writes:

>> Nope, same problem : the *Org Preview LaTeX Output* buffer says :
>>
>> ../../../../../tmp/orgtexSyy18r.dvi: No such file or directory
>> This is dvipng 1.15 Copyright 2002-2015 Jan-Ake Larsson
>>
>> If I understand it correctly, the %F placeholder should be an
>> *absolute* filename. It is not...
>
> May you try the attached patch?
> I changed the default command templates to use absolute paths
> everywhere.

No response after 2 months.
Since I do not have a reproducer, I cannot confirm that the patch is an
improvement. So, not merging.

Canceled.

If anyone else is experiencing the same problem, feel free to jump in to
revive the thread.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: [BUG] LaTeX preview should use a subdirectory in /tmp

2024-05-02 Thread Ihor Radchenko
Emmanuel Charpentier  writes:

>> (plist-put (alist-get 'dvipng org-preview-latex-process-alist)
>>     :image-converter
>>    '("dvipng -D %D -T tight -o %O %F"))
>
> Nope, same problem : the *Org Preview LaTeX Output* buffer says :
>
> ../../../../../tmp/orgtexSyy18r.dvi: No such file or directory
> This is dvipng 1.15 Copyright 2002-2015 Jan-Ake Larsson
>
> If I understand it correctly, the %F placeholder should be an
> *absolute* filename. It is not...

May you try the attached patch?
I changed the default command templates to use absolute paths
everywhere.

>From 7ac1aacb977ded45dc713b24e18d9068baaa5148 Mon Sep 17 00:00:00 2001
Message-ID: <7ac1aacb977ded45dc713b24e18d9068baaa5148.1714645744.git.yanta...@posteo.net>
From: Ihor Radchenko 
Date: Thu, 2 May 2024 13:19:37 +0300
Subject: [PATCH] org-preview-latex-process-alist: Work around relative path
 oddities in WSL

* lisp/org-macs.el (org-compile-file-commands): Add new allowed
placeholder %P - absolute path to output dir.
* lisp/org.el (org-preview-latex-process-alist): Use absolute paths
in commands.  List all the allowed placeholders in the docstring.

Reported-by: Emmanuel Charpentier 
Link: https://orgmode.org/list/87zftr4uaj.fsf@localhost
---
 lisp/org-macs.el | 18 ++
 lisp/org.el  | 18 ++
 2 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index 1254ddb54..53dc75425 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -1690,12 +1690,12 @@ (defun org-compile-file-commands (source process ext  spec err-msg)
 the SOURCE file.
 
 If PROCESS is a list of commands, each of them is called using
-`shell-command'.  By default, in each command, %b, %f, %F, %o and
-%O are replaced with, respectively, SOURCE base name, relative
-file name, absolute file name, relative directory and absolute
-output file name.  It is possible, however, to use more
-place-holders by specifying them in optional argument SPEC, as an
-alist following the pattern
+`shell-command'.  By default, in each command, %b, %f, %F, %o, %P, and
+%O are replaced with, respectively, SOURCE base name, relative file
+name, absolute file name, relative output directory, absolute output
+directory, and absolute output file name.  It is possible, however, to
+use more place-holders by specifying them in optional argument SPEC,
+as an alist following the pattern
 
   (CHARACTER . REPLACEMENT-STRING).
 
@@ -1707,9 +1707,10 @@ (defun org-compile-file-commands (source process ext  spec err-msg)
  (pwd (file-truename default-directory))
  (absname (expand-file-name source pwd))
  (relname (if (file-name-absolute-p source)
-(file-relative-name source pwd)
-  source))
+  (file-relative-name source pwd)
+source))
 	 (relpath (or (file-name-directory relname) "./"))
+ (abspath (expand-file-name relpath))
 	 (output (concat (file-name-sans-extension absname) "." ext))
 	 (err-msg (if (stringp err-msg) (concat ".  " err-msg) "")))
 (pcase process
@@ -1720,6 +1721,7 @@ (defun org-compile-file-commands (source process ext  spec err-msg)
 			 (?f . ,(shell-quote-argument relname))
 			 (?F . ,(shell-quote-argument absname))
 			 (?o . ,(shell-quote-argument relpath))
+ (?P . ,(shell-quote-argument abspath))
 			 (?O . ,(shell-quote-argument output))
  (mapcar (lambda (command) (format-spec command spec)) process)))
   (_ (error "No valid command to process %S%s" source err-msg)
diff --git a/lisp/org.el b/lisp/org.el
index 809fd8ba3..1c78c2024 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -3326,10 +3326,10 @@ (defcustom org-preview-latex-process-alist
  :image-input-type "dvi"
  :image-output-type "png"
  :image-size-adjust (1.0 . 1.0)
- :latex-compiler ("latex -interaction nonstopmode -output-directory %o %f")
- :image-converter ("dvipng -D %D -T tight -o %O %f")
+ :latex-compiler ("latex -interaction nonstopmode -output-directory %P %F")
+ :image-converter ("dvipng -D %D -T tight -o %O %F")
  :transparent-image-converter
- ("dvipng -D %D -T tight -bg Transparent -o %O %f"))
+ ("dvipng -D %D -T tight -bg Transparent -o %O %F"))
 (dvisvgm
  :programs ("latex" "dvisvgm")
  :description "dvi > svg"
@@ -3337,7 +3337,7 @@ (defcustom org-preview-latex-process-alist
  :image-input-type "dvi"
  :image-output-type "svg"
  :image-size-adjust (1.7 . 1.5)
- :latex-compiler ("latex -interaction nonstopmode -output-directory %o %f")
+ :latex-compiler ("latex -interaction nonstopmode -output-directory %P %F")
  :image-converter ("dvisvgm %f --no-fonts --exact-bbox --scale=%S --output=%O"))
 (imagemagick
  :programs ("latex" "convert")
@@ -3346,9 +3346,9 @@ (defcustom org-preview-latex-process-alist
  :image-input-type "pdf"
  :image-output-type "png"
   

Re: [BUG] LaTeX preview should use a subdirectory in /tmp

2024-04-20 Thread Max Nikulin

On 19/04/2024 22:00, emm.charpent...@free.fr wrote:

Embedding 
/home/charpent/WinFiles/Temporaire/Org/file:ltximg/EssaiSnippetsLaTeX_2923a1941c0e63eab69544afa1be2ec0d95a01c4.png
 as Images/0001.png...


See

Ihor Radchenko. Re: strange export problem with a file: link. Sat, 20 
Apr 2024 12:25:56 +.

https://list.orgmode.org/87cyqkmd9n.fsf@localhost




Re: [BUG] LaTeX preview should use a subdirectory in /tmp

2024-04-19 Thread emm . charpentier
I can M-x trace-function org-compile-file-commands (whic now appears in the 
auocompletions) but, curiously, I *dp* **not** get a ttace output buffer.

Exporting still fails with :

org-babel-exp process emacs-lisp at position 194...
Formatting LaTeX using dvipng
Creating LaTeX Image 1...
Creating LaTeX Image 2...
Embedding 
/home/charpent/WinFiles/Temporaire/Org/file:ltximg/EssaiSnippetsLaTeX_2923a1941c0e63eab69544afa1be2ec0d95a01c4.png
 as Images/0001.png...
condition-case: OpenDocument export failed: Opening input file: No such file or 
directory, 
/home/charpent/WinFiles/Temporaire/Org/file:ltximg/EssaiSnippetsLaTeX_2923a1941c0e63eab69544afa1be2ec0d95a01c4.png
Making completion list...
Quit [8 times]

in the *Messages* buffer.

I am at loss..

--
Emmanuel Charpentier


- Mail original -
> De: "Ihor Radchenko" 
> À: "Emmanuel Charpentier" 
> Cc: emacs-orgmode@gnu.org
> Envoyé: Vendredi 19 Avril 2024 13:56:52
> Objet: Re: [BUG] LaTeX preview should use a subdirectory in /tmp
> 
> Emmanuel Charpentier  writes:
> 
> >> What if you do M-x trace-function  org-compile-file-commands
> >
> > No such function : I just have  org-compile-file , which I traced.
> 
> May you try the development branch of Org mode (main)?
> 
> --
> Ihor Radchenko // yantar92,
> Org mode contributor,
> Learn more about Org mode at <https://orgmode.org/>.
> Support Org development at <https://liberapay.com/org-mode>,
> or support my work at <https://liberapay.com/yantar92>
> 



Re: [BUG] LaTeX preview should use a subdirectory in /tmp

2024-04-19 Thread Ihor Radchenko
Emmanuel Charpentier  writes:

>> What if you do M-x trace-function  org-compile-file-commands
>
> No such function : I just have  org-compile-file , which I traced.

May you try the development branch of Org mode (main)?

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: [BUG] LaTeX preview should use a subdirectory in /tmp

2024-04-18 Thread Emmanuel Charpentier
Le mercredi 17 avril 2024 à 22:41 +0200, Emmanuel Charpentier a écrit :
[ Snip... ]
A couple more data points :
 * The problem I described happens when I run `emacs` on files
   belonging in a Windows tree, symlinked to the Linux tree. Running
   `emacs` on files outside this symlinked tree works OK.
   This suggests that it might be worth testing trhese exports from
   network directories.
   
 * Trying this with the current Org from the git directory succesfully
   builds the images but fails at inserting them in the `.odt` file
   (Org mistakenly inserts the "file:" string in the name of the needed
   image file).

 * Trying `dvisvgm` fails because Org does not recognize this as a
   valid format and insists on `verbatim`.

HTH,



Re: [BUG] LaTeX preview should use a subdirectory in /tmp

2024-04-17 Thread Emmanuel Charpentier
Le mercredi 17 avril 2024 à 20:18 +, Ihor Radchenko a écrit :
> Emmanuel Charpentier  writes:

[ Snip... ]


> What if you do M-x trace-function  org-compile-file-commands

No such function : I just have  org-compile-file , which I traced.

> 
> and run the preview.
> Then, a buffer should appear listing the command expansions used
> during
> the preview process. May you then share that buffer?

Here it comes :

Buffer "Trace output" :

*
==
1 -> (org-compile-file "/tmp/orgtexD0afvR.tex" ("latex -interaction
nonstopmode -output-directory %o %f") "dvi" "Please adjust `dvipng'
part of `org-preview-latex-process-alist'." #)
1 <- org-compile-file: "/tmp/orgtexD0afvR.dvi"
==
1 -> (org-compile-file "/tmp/orgtexD0afvR.dvi" ("dvipng -D %D -T tight
-bg Transparent -o %O %f") "png" "Please adjust `dvipng' part of `org-
preview-latex-process-alist'." #
((68 . "140.0") (83 . "1.0")))
1 <- org-compile-file: !non-local\ exit!
*

Buffer "*Org Preview LaTeX Output*" :

*
../../../../../tmp/orgtexD0afvR.dvi: No such file or directory
This is dvipng 1.15 Copyright 2002-2015 Jan-Ake Larsson
*

HTH,





Re: [BUG] LaTeX preview should use a subdirectory in /tmp

2024-04-17 Thread Ihor Radchenko
Emmanuel Charpentier  writes:

>> Assuming that your `org-preview-latex-default-process' is using the
>> default value of 'dvipng, does it help if you change the image
>> convertor
>> command to use absolute path?
>
> I didn't know that one could do that : the placeholders are not well
> documented...

Yeah. This particular option is missing from the docstring of
`org-preview-latex-process-alist'.

>> (plist-put (alist-get 'dvipng org-preview-latex-process-alist)
>>     :image-converter
>>    '("dvipng -D %D -T tight -o %O %F"))
>
> Nope, same problem : the *Org Preview LaTeX Output* buffer says :
>
> ../../../../../tmp/orgtexSyy18r.dvi: No such file or directory
> This is dvipng 1.15 Copyright 2002-2015 Jan-Ake Larsson
>
> If I understand it correctly, the %F placeholder should be an
> *absolute* filename. It is not...
>
> Couldn't the output directory of the :latex-compiler element being
> hardcoded to, say, the curerent directory or a subdirectory thereof,
> and ditto for the input directory of :image-converter ?

What if you do M-x trace-function  org-compile-file-commands 
and run the preview.
Then, a buffer should appear listing the command expansions used during
the preview process. May you then share that buffer?

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: [BUG] LaTeX preview should use a subdirectory in /tmp

2024-04-17 Thread Emmanuel Charpentier
Le mercredi 17 avril 2024 à 16:48 +, Ihor Radchenko a écrit :
> Emmanuel Charpentier  writes:
> 
> > I have a case where the current way of forcing the temporary
> > directory
> > to me `/tmp` is wrong. Running emacs on Ubuntu **under WSL2**,,
> > exporting latex snippets to ODT *as images* fails : the `.dvi`
> > files
> > are correctly compiled and placed in `/tmp{, but the convert
> > program
> > tries to read them in `../../../../tmp/`, which is indeed `/tmp` in
> > a
> > "normal" filesystem but **is not** in WSL, where the root (`/`) is
> > in
> > fact a mounted tree.
> > 
> > Admittedly, this is a corner case, but it turned out to be
> > necessary
> > (exporting via mathml gave unsatisfying results).
> 
> It looks like is a different bug. (probably even in Emacs, when
> calculating relative path)

That might also be an idiosyncratic bug of WSL2' implementation of the
virtual machine filesystem...
> 
> Assuming that your `org-preview-latex-default-process' is using the
> default value of 'dvipng, does it help if you change the image
> convertor
> command to use absolute path?

I didn't know that one could do that : the placeholders are not well
documented...

> 
> (plist-put (alist-get 'dvipng org-preview-latex-process-alist)
>      :image-converter
>    '("dvipng -D %D -T tight -o %O %F"))

Nope, same problem : the *Org Preview LaTeX Output* buffer says :

../../../../../tmp/orgtexSyy18r.dvi: No such file or directory
This is dvipng 1.15 Copyright 2002-2015 Jan-Ake Larsson

If I understand it correctly, the %F placeholder should be an
*absolute* filename. It is not...

Couldn't the output directory of the :latex-compiler element being
hardcoded to, say, the curerent directory or a subdirectory thereof,
and ditto for the input directory of :image-converter ?

Sincerely,





Re: [BUG] LaTeX preview should use a subdirectory in /tmp

2024-04-17 Thread Ihor Radchenko
Emmanuel Charpentier  writes:

> I have a case where the current way of forcing the temporary directory
> to me `/tmp` is wrong. Running emacs on Ubuntu **under WSL2**,,
> exporting latex snippets to ODT *as images* fails : the `.dvi` files
> are correctly compiled and placed in `/tmp{, but the convert program
> tries to read them in `../../../../tmp/`, which is indeed `/tmp` in a
> "normal" filesystem but **is not** in WSL, where the root (`/`) is in
> fact a mounted tree.
>
> Admittedly, this is a corner case, but it turned out to be necessary
> (exporting via mathml gave unsatisfying results).

It looks like is a different bug. (probably even in Emacs, when
calculating relative path)

Assuming that your `org-preview-latex-default-process' is using the
default value of 'dvipng, does it help if you change the image convertor
command to use absolute path?

(plist-put (alist-get 'dvipng org-preview-latex-process-alist)
   :image-converter
   '("dvipng -D %D -T tight -o %O %F"))

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: [BUG] LaTeX preview should use a subdirectory in /tmp

2024-04-17 Thread Emmanuel Charpentier
On 25/03/2024 19:49, Max Nikulin wrote:

> > (defun org-ensure-tmp-dir (dir-symbol prefix)
> >    (let ((dir (symbol-value dir-symbol)))
> >      ;; Temporary directory has not been cleaned.
> >      (or (and dir (file-directory-p dir) dir)
>
>
>`if' should be used instead of `or' here.
>
>
>
>>
>> (setf (symbol-value dir-symbol)
>>   (make-temp-file (or prefix "orgtmp-") 'dir)
>> 
>> (defvar org-tex-tmpdir nil)
>> 
>> Usage example: (org-ensure-tmp-dir 'org-tex-tmpdir "orgtex-")

> I do not like that the function may be called with
different `temporary-file-directory' and I can not figure out how to
adjust API to handle such case. On the other hand I am unsure if it is
a realistic case when this function is called with
alternating `temporary-file-directory'.  

I have a case where the current way of forcing the temporary directory
to me `/tmp` is wrong. Running emacs on Ubuntu **under WSL2**,,
exporting latex snippets to ODT *as images* fails : the `.dvi` files
are correctly compiled and placed in `/tmp{, but the convert program
tries to read them in `../../../../tmp/`, which is indeed `/tmp` in a
"normal" filesystem but **is not** in WSL, where the root (`/`) is in
fact a mounted tree.

Admittedly, this is a corner case, but it turned out to be necessary
(exporting via mathml gave unsatisfying results).





Re: [BUG] LaTeX preview should use a subdirectory in /tmp

2024-04-17 Thread copropriete27ruemoret
On 25/03/2024 19:49, Max Nikulin wrote:

> > (defun org-ensure-tmp-dir (dir-symbol prefix)
> >    (let ((dir (symbol-value dir-symbol)))
> >      ;; Temporary directory has not been cleaned.
> >      (or (and dir (file-directory-p dir) dir)
>
>
>`if' should be used instead of `or' here.
>
>
>
>>
>> (setf (symbol-value dir-symbol)
>>   (make-temp-file (or prefix "orgtmp-") 'dir)
>> 
>> (defvar org-tex-tmpdir nil)
>> 
>> Usage example: (org-ensure-tmp-dir 'org-tex-tmpdir "orgtex-")

> I do not like that the function may be called with different `temporary-file-directory' and I can not figure out how to adjust API to handle such case. On the other hand I am unsure if it is a realistic case when this function is called with alternating `temporary-file-directory'.  

I have a case where the current way of forcing the temporary directory to me `/tmp` is wrong. Running emacs on Ubuntu **under WSL2**,, exporting latex snippets to ODT *as images* fails : the `.dvi` files are correctly compiled and placed in `/tmp{, but the convert program tries to read them in `../../../../tmp/`, which is indeed `/tmp` in a "normal" filesystem but **is not** in WSL, where the root (`/`) is in fact a mounted tree.

Admittedly, this is a corner case, but it turned out to be necessary (exporting via mathml gave unsatisfying results).



Re: [BUG] LaTeX preview should use a subdirectory in /tmp

2024-03-26 Thread Max Nikulin

On 25/03/2024 19:49, Max Nikulin wrote:


(defun org-ensure-tmp-dir (dir-symbol prefix)
   (let ((dir (symbol-value dir-symbol)))
     ;; Temporary directory has not been cleaned.
     (or (and dir (file-directory-p dir) dir)


`if' should be used instead of `or' here.


 (setf (symbol-value dir-symbol)
   (make-temp-file (or prefix "orgtmp-") 'dir)

(defvar org-tex-tmpdir nil)

Usage example: (org-ensure-tmp-dir 'org-tex-tmpdir "orgtex-")


I do not like that the function may be called with different 
`temporary-file-directory' and I can not figure out how to adjust API to 
handle such case. On the other hand I am unsure if it is a realistic 
case when this function is called with alternating 
`temporary-file-directory'.






Re: [BUG] LaTeX preview should use a subdirectory in /tmp

2024-03-25 Thread Max Nikulin

On 25/03/2024 18:40, Ihor Radchenko wrote:

Max Nikulin writes:

This feature should not write temporary files to /tmp directly


See the attached tentative patch.


Thanks for prompt reaction.


+++ b/lisp/org.el
@@ -16361,7 +16361,7 @@ (defun org-create-formula-image
   org-format-latex-header
   'snippet)))
 (latex-compiler (plist-get processing-info :latex-compiler))
-(tmpdir temporary-file-directory)
+(tmpdir (concat temporary-file-directory "orgtex/"))
 (texfilebase (make-temp-name
   (expand-file-name "orgtex" tmpdir)))


Since directory name already contains "org", it may be shortened to just 
"tex"



 (texfile (concat texfilebase ".tex"))


I would use `make-temp-file' to create TEXFILE and would derive 
TEXFILEBASE from it. In general it is safer.


To create directory I expect a call of a function similar to the 
following one. Perhaps it exists already somewhere.


(defun org-ensure-tmp-dir (dir-symbol prefix)
  (let ((dir (symbol-value dir-symbol)))
;; Temporary directory has not been cleaned.
(or (and dir (file-directory-p dir) dir)
(setf (symbol-value dir-symbol)
  (make-temp-file (or prefix "orgtmp-") 'dir)

(defvar org-tex-tmpdir nil)

Usage example: (org-ensure-tmp-dir 'org-tex-tmpdir "orgtex-")

Fixed directory name is not friendly for multi-user systems and 
predictable name in /tmp might be a source of security issues.





Re: [BUG] LaTeX preview should use a subdirectory in /tmp

2024-03-25 Thread Ihor Radchenko
Max Nikulin  writes:

> This feature should not write temporary files to /tmp directly, some 
> subdirectory should be created for this purpose. The idea is to mitigate 
> consequences if a user opens a file from a compromised or a malicious 
> source and gets /tmp flooded with a crowd of files. It is easier to 
> delete single directory than to spent time trying to figure out what 
> files are necessary for other applications and what ones are generated 
> by LaTeX code.

See the attached tentative patch.

>From df4d827d98063ee665d71906f358ca08ad4d0023 Mon Sep 17 00:00:00 2001
Message-ID: 
From: Ihor Radchenko 
Date: Mon, 25 Mar 2024 14:38:42 +0300
Subject: [PATCH] org-create-formula-image: Keep temporary LaTeX files in
 subfir of /tmp

* lisp/org.el (org-create-formula-image): When compiling formula
images, keep temporary LaTeX logs in a subdirectory of
`temporary-file-directory' instead of littering the top-level /tmp.

Reported-by: Max Nikulin 
Link: https://orgmode.org/list/utrkah$4s0$1...@ciao.gmane.io
---
 lisp/org.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/org.el b/lisp/org.el
index f0f85822f..0793b72d6 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16361,7 +16361,7 @@ (defun org-create-formula-image
 	   org-format-latex-header
 	   'snippet)))
 	 (latex-compiler (plist-get processing-info :latex-compiler))
-	 (tmpdir temporary-file-directory)
+	 (tmpdir (concat temporary-file-directory "orgtex/"))
 	 (texfilebase (make-temp-name
 		   (expand-file-name "orgtex" tmpdir)))
 	 (texfile (concat texfilebase ".tex"))
-- 
2.44.0


-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 


[BUG] LaTeX preview should use a subdirectory in /tmp

2024-03-25 Thread Max Nikulin

This is a follow-up to recent changes related to LaTeX preview.

This feature should not write temporary files to /tmp directly, some 
subdirectory should be created for this purpose. The idea is to mitigate 
consequences if a user opens a file from a compromised or a malicious 
source and gets /tmp flooded with a crowd of files. It is easier to 
delete single directory than to spent time trying to figure out what 
files are necessary for other applications and what ones are generated 
by LaTeX code.


P.S. I do not mind double level structure: all temporary Org mode files 
(babel, etc.) are created in single (per Emacs process) directory in tmp 
and each task creates its own subdirectory there.