org-fragtog icon indicating copy to clipboard operation
org-fragtog copied to clipboard

Error in post-command-hook (org-fragtog--post-cmd): (wrong-type-argument number-or-marker-p nil)

Open xtremejames1 opened this issue 10 months ago • 7 comments

Describe the bug Using Emacs 29.4 in Doom Emacs and org-mode 9.7.11 on NixOS via WSL on Windows 11, org-fragtog does not disable Latex previews. It throws the error in the title after org-fragtog-mode is enabled and I move my cursor onto a Latex fragment.

To Reproduce Steps to reproduce the behavior:

  1. Open org-mode file with latex fragments
  2. Enable org-fragtog-mode
  3. Move cursor onto latex fragment

Expected behavior Latex preview to be disabled upon moving cursor onto it.

Version and environment

  • OS: NixOS on WSL2 Windows 11
  • Emacs version 29.4 Doom Emacs
  • Org version 9.7.11

Additional context I am also running Doom Emacs on nix-doom-emacs-unstraightened, which may cause issues. Apologies for my inexperience.

xtremejames1 avatar Jan 07 '25 01:01 xtremejames1

Can you display latex preview the normal manual way (i.e. org-preview-latex-fragment) without org-fragtog? Also, I wasn't aware nix-doom-emacs-unstraightened existed before you mentioned it and I found it useful, thanks!

io12 avatar Jan 12 '25 02:01 io12

Yes, displaying latex preview manually does work without org-fragtog. I suspect it might be an issue with org-element not working correctly, since using org-element-at-point over a latex fragment returns type paragraph. Furthermore, org-appear also does not work, which I suspect may be related. Regardless, I am a newbie to emacs, doom, and org-mode so any help would be much appreciated. Thank you so much for your response.

xtremejames1 avatar Jan 12 '25 03:01 xtremejames1

I suspect it might be an issue with org-element not working correctly, since using org-element-at-point over a latex fragment returns type paragraph.

This happens for me too, but (org-element-context) has type latex-fragment (and is what org-fragtog uses).

Can you try to get a backtrace of the error with the approach here? That could give a hint where the error came from. https://discourse.doomemacs.org/t/what-is-a-backtrace-how-to-produce-them/85

io12 avatar Jan 12 '25 23:01 io12

You may have to run (org-fragtog--post-cmd) manually to trigger the backtrace, since apparently backtraces are suppressed when the source is from post-command-hook.

io12 avatar Jan 12 '25 23:01 io12

You may have to run (org-fragtog--post-cmd) manually to trigger the backtrace, since apparently backtraces are suppressed when the source is from post-command-hook. Thank you for this, I haven't been able to produce a backtrace in my previous attempts at debugging.

Below is what is produced after manually running (org-fragtog--post-cmd):

Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p nil)
  org-fragtog--frag-end((latex-fragment (:standard-properties [193 nil nil nil 206 0 nil nil nil nil nil nil nil nil #<buffer 20250105201249-math_2940.org> nil nil (paragraph (:standard-properties [193 193 193 207 207 0 nil nil element t nil nil nil nil #<buffer 20250105201249-math_2940.org> nil nil (section ...)]))] :value [org-element-deferred org-element--substring (0 13) t])))
  org-fragtog--cursor-frag()
  org-fragtog--post-cmd()
  eval-expression((org-fragtog--post-cmd) nil nil 127)
  funcall-interactively(eval-expression (org-fragtog--post-cmd) nil nil 127)
  command-execute(eval-expression)

xtremejames1 avatar Jan 13 '25 17:01 xtremejames1

Below is what is produced after manually running (org-fragtog--post-cmd):

Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p nil)
  org-fragtog--frag-end((latex-fragment (:standard-properties [193 nil nil nil 206 0 nil nil nil nil nil nil nil nil #<buffer 20250105201249-math_2940.org> nil nil (paragraph (:standard-properties [193 193 193 207 207 0 nil nil element t nil nil nil nil #<buffer 20250105201249-math_2940.org> nil nil (section ...)]))] :value [org-element-deferred org-element--substring (0 13) t])))
  org-fragtog--cursor-frag()
  org-fragtog--post-cmd()
  eval-expression((org-fragtog--post-cmd) nil nil 127)
  funcall-interactively(eval-expression (org-fragtog--post-cmd) nil nil 127)
  command-execute(eval-expression)

Thanks! I'm still pretty confused what's going on here though. The org-fragtog--frag-end function is just

(defun org-fragtog--frag-end (frag)
  (- (org-element-property :end frag)
     (org-element-property :post-blank frag)))

So my guess would be either (org-element-property :end frag) or (org-element-property :post-blank frag) is returning nil, and the error is complaining about this because (- x y) requires that x and y are both numbers. But I'm confused how this could happen because from reading the org-mode code and experimenting, it looks like it just gets the 5th and 6th elements of :standard-properties in the org element data, which is 206 and 0 in your case, not nil.

Maybe you can try a minimal version of your config that just has org-fragtog and very little else, and if that works re-enable parts until it breaks, so you know if there's something specific that breaks it? Or you can send me your config and I can try to reproduce the issue.

io12 avatar Jan 13 '25 18:01 io12

I uploaded my config to the following GitHub repository.

https://github.com/xtremejames1/doomconfig/tree/main

xtremejames1 avatar Jan 15 '25 19:01 xtremejames1