reddit / emacs

Fix for Invalid Syntax errors in Python Org Mode source blocks

TheIdOfAlan · 2022-11-03 20:05
UPDATE: The root cause was a setting I flipped when I first started using Emacs and knew even less about what I was doing. Details are in the comment below. I've reverted the setting and so far things are working as expected --- I don't know if I did something weird to cause it, but I kept having problems with `SyntaxError: invalid syntax` - `IndentationError: unexpected indent` when I tried to run python source blocks. The problem came from the auto-formatting occasionally throwing spaces on a line in a way that made the interpreter freak out. I couldn't find a solution, so I came up with this: ```lisp (add-hook 'org-ctrl-c-ctrl-c-hook (function (lambda () (replace-regexp-in-region "\s+\n" "\n" (org-element-property :begin (org-element-at-point)) (org-element-property :end (org-element-at-point))) nil))) ``` It chomps spaces at the end of lines which fully eliminates them from the lines that were causing problems. (it does it for all languages and alters the contents directly. I'm okay with both those things) I did a write up with [more details and notes](https://www.alanwsmith.com/posts/invalid-syntax-fix-for-emacs-org-mode-python-scripts--2h2xhnofowhf) It took me a straight 8 hours to come up with the solution. Because I'm interested in seeing how other folks work I decided to post my [notes from the endeavor](https://www.alanwsmith.com/posts/what_8_hours_of_notes_on_a_single_problem_looks_like--2h0mtrjtrnck). It's mainly just a bunch of sequential code snippets as I try to home in on the solution I'm still new to this. If you see something wonky or dangerous, please let me know
TheIdOfAlan · 2022-11-03 20:56
And do add an example of where things change, this won't work with that code in place: ```python class Example(): def __init__(self): pass def ping(self): pass ``` The lines have to be connected. So either something like this: ```python class Example(): def __init__(self): pass def ping(self): pass ``` or this: ```python class Example(): def __init__(self): pass # def ping(self): pass ``` Basically anything that connects the lines. It's not ideal, but it's visible which make it a lot easier to deal with. It also stays the same as opposed to sometimes being updated by the formatter. I may play around with another version that doesn't require the filler lines, but that would be way more complicated. I'm running with this version for now and if it doesn't bug me I'll stick with it. And you should only have to do that on lines that have indents in them (e.g. `def` calls under classes). If all the `def` strings were flush against the side, the filler lines should be necessary
Potential-Dog4943 · 2022-11-04 02:14
Very cool! Stuff like this makes me wonder why I haven't sunk more time into EMACS in my formative years.
yantar92 · 2022-11-04 06:03
Check org-src-preserve-indentation
podtatranec · 2022-11-04 14:45
It's not quite clear what is going on and why you are hitting the issue. It seems like you are suggesting that some autoformatting/org puts some weird whitespace before it runs the code. I suggest that part is what needs fixing then. Perhaps not quite the solution (but maybe it is) - have a look at something like [https://github.com/lewang/ws-butler](https://github.com/lewang/ws-butler) or similar whitespace-cleanup packages. Also as others noted making sure org-src-preserve-indentation is set.
TheIdOfAlan · 2022-11-04 18:28
well, crap. but also, hurray! I figured out the root cause When I first started using Emacs I didn't like the way errors went to another window when running python snippets. I put this in so that errors would show up in the #+RESULTS block: ```lisp (setq org-babel-python-command "/opt/homebrew/bin/python3 -i -c \"import sys; sys.stderr = sys.stdout;\"" ) ``` It does the trick, but it seems to be what's causing the error. I've updated to use this: ```lisp (setq org-babel-python-command "/opt/homebrew/bin/python3" ) ``` and so far the issue hasn't reoccured The lack of posts form other folks having the issue made me pretty sure the problem was because of something I'd done. I just wasn't able to figure out. There's so much and it's all so different it's hard to track The good news is it looks like it's fixed The other good news is that even though my original code is going to get junked I learned a ton about how emacs and elisp work