dandavison / xenops

An editing environment for LaTeX mathematical documents
MIT License
219 stars 14 forks source link

xenops-handle-paste seems broken #31

Open thibautbenjamin opened 2 years ago

thibautbenjamin commented 2 years ago

I am trying out Xenops mode and so far I really like it, I find the mode extremely powerful and quite frankly it is something I had been looking for for some time now, so I am really glad I stumbled on it.

However I keep encountering a bug with the 'xenops-handle-paste'. Whenever I try to use it, it hangs and seems to not terminate, and I have to quit manually with C-g. For now, this function is still bound to C-y and so I keep hitting it accidentally, this happens everytime. As a workaround I use the standard yank function of emacs M-x yank, but it is not well suited to handle the maths being replaced with SVG images.

I am using emacs 28.0.60, in ubuntu 20.04 with 'auctex', 'auctex-latexmk', 'cdlatex' and 'xenops' whenever I am in Latex-mode.

Interestingly I just noticed that xenops-handle-paste seems to be working if I am trying to paste text that I copied from my firefox window. However, if I try to copy some text from emacs, the bug always happens. I have tried copying text from a latex buffer, and even copying raw text from an org-mode buffer, in both cases the result is the same, and the function hangs indefinitely

dandavison commented 2 years ago

Hi @thibautbenjamin, I'm happy to hear that you're finding xenops useful.

Do you ever touch emacs lisp code? If so, any help investigating along the lines below would be great. But no worries if not.

Here's the function in question1:

(defun xenops-handle-paste ()
  "Handle a paste event, if the clipboard data contain an element that Xenops can paste."
  (interactive)
  (or (xenops-math-handle-paste)
      (xenops-image-handle-paste)
      (xenops-handle-paste-default)))

So the first thing I'd want to know is: is the problem specifically in (xenops-math-handle-paste)?

One experiment to answer that would be to

  1. Copy the text as you were before
  2. Evaluate the lisp expression (xenops-math-handle-paste)
  3. Does the same problem occur?

One way to evaluate a lisp expression is to enter M-x eval-expression and then type / paste the expression to evaluate.

Alternatively you could temporarily comment out expressions in that function definition and re-evaluate the function definition and then just do exactly what you were doing before. It depends how comfortable you are with the lisp environment in emacs.

1 To find the definition of the function, you can do C-h f and then enter xenops-handle-paste and then click on the link to the code file xenops.el

thibautbenjamin commented 2 years ago

Thank you for your answer, I am comfortable editing and evaluating lisp expressions from within emacs, but I do not have much time at hand, so I did not investigate any further than what I reported in the first place. In hindsight, the function was simple enough and I could easily have given a more precise report.

Anyway, when I tried, I found that evaluating xenops-math-handle-paste works fine and returns nil as it should with normal text, but evaluating xenops-image-handle-paste seems to be my problem, and I am wondering if I might be missing any external dependency. I should also mention that I had ran xenops-doctor before my first report, and it said everything was ok.

I investigated a bit further and found out, by testing the following function

(defun test-xclip ()
  (let ((temp-file (make-temp-file "xenops-image-from-clipboard-" nil ".png")))
    (xenops-image-write-clipboard-image-to-file--xclip temp-file)))

that the problem occurs with the function xenops-image-write-clipboard-image-to-file--xclip.I double checked, and I do have xclip installed.

I went a bit further and defined this function

(defun test-image-xclip ()
  (let ((temp-file (make-temp-file "xenops-image-from-clipboard-" nil ".png")))
    (call-process "xclip" nil `(:file ,temp-file) nil "-selection" "clipboard" "-t" "image/png" "-o")))

which is the non-terminating function. So it seems that it is a problem with the way xclip is called. I don't know much about xclip, nor about the call-process function. Could you tell me which command it is supposed to run, so that I can try running that command manually from the terminal and see what happens

dandavison commented 2 years ago

To test what xenops is doing the procedure is

  1. Copy image data to your clipboard (in such a way that e.g. "paste" into a GitHub issue uploads the image)
  2. Run this and verify that the image is written to disk:
    xclip -selection clipboard -t image/png -o > img.png

Just to confirm, you're using Linux, right? (On MacOS pngpaste plays the role of xclip).

For reference, there's a fixed issue that was debugging an xclip issue here: #24.

thibautbenjamin commented 2 years ago

The issue happens when I try to pase some text, and not necessarily an image. Here is what I tried:

EDIT: I did what you suggested as well and called the process with a valid image in the clipboard, and everything worked as intended. So here is a recap of the situation:

  1. With a normal image in the clipboard:

    • calling xclip for the terminal works as intended
    • calling xclip via emacs and call-process works as intended
  2. With text in the clipbard, when copied from firefox

    • calling xclip from the terminal fails with Error: target image/png not available
    • calling xclip from emacs and call-process returns 1, indicatgin the failure
  3. With text in the clipboard, when copied from emacs

    • calling xclip from the terminal fails with Error: target image/png not available
    • calling xclip from emacs and call-process hangs emacs to the point where I have to kill it manually.

I tried to look up this issue online, but without a lot of success, a suggestion I saw was to add a timeout to the call, so that it gets stopped whenever it starts hanging.

dandavison commented 2 years ago

Hi @thibautbenjamin, hm, sorry about this. I'd like to solve it properly. Meanwhile, do you have a workaround, or would it help if I added a configuration variable allowing you to prevent xenops from trying to make this call-process call?

thibautbenjamin commented 2 years ago

Thanks @dandavison, adding a temporary switch would be most helpful. I don't have an easy workaround at the moment. I did a few more tries in order to try to understand what is going wrong, and here is what I tried:

None of those tests solved the issue. I went out of ideas after this. If there is any other tests I can do to provide better info please let me know, and I'll do what I can. Worth mentionning, due to a disk problem I completely reinstalled ubuntu in between the bug report and those tests, so I am pretty sure the behavior I am describing shows on an almost naked ubuntu install.

dandavison commented 2 years ago

OK, no problem. I've added a customizable variable xenops-image-try-write-clipboard-image-to-file (defaults to t). That's in master; MELPA will pick up the change automatically in the next few hours. You'll be able to see when because the package version at https://melpa.org/#/xenops will change to today's date. Currently it is

image
thibautbenjamin commented 2 years ago

Thanks for the tip, I am using the straight package manager, so I can tell it to pull the package directly from github, if it does not update on MELPA. And thanks a lot for the help, good luck with the debugging and don't hesitate to ask me if there is any additional info I can provide, or if you cannot replicate this bug

sampotter commented 1 year ago

What's the latest with this issue? I'm trying the latest MELPA xenops on Fedora with Emacs 27.2 and running into very similar issues as described above. I did not read the discussion carefully above, but the experience of trying to copy and paste text while editing equation environments or TikZ pictures which are being rendered using xenops is very glitchy.

Basically, if I C-y as described above, Emacs hangs until C-g. Very occasionally, the mini buffer will pop up some prompt above pasting text, with the yanked text displayed, but I can't figure out under what conditions this happens. I think this also not a very helpful behavior---it should be clear when the user is trying to copy and paste text...

Happy to try to help debug.

dandavison commented 1 year ago

Hi @sampotter, if you would be able to debug a bit that would be helpful. I am on MacOS and pasting text, and images, is working correctly. But of course pasting images does involve an external command which is OS-specific (pngpaste on Mac, and xclip on linux). As mentioned above, there is now an escape hatch: if it is being annoying you can (setq xenops-image-try-write-clipboard-image-to-file nil).