0

I have a python program that I edit in emacs. However, sometimes when I save it, it gets re-indented, and in doing so, messes up the indentation of sections, where I have some code and then some undented code (because the nested block ended) but the formatter decides that it should all be indented.

def foo():
  if xyz:
     abcd = defg
  hijk()
  if not wxyz:
     pqrs()

sometimes becomes:

def foo():
  if xyz:
     abcd = defg
     hijk()
  if not wxyz:
     pqrs()

So, I want to turn off whatever formatter is being run, without turning off the rest of "python mode".

However, I don't know what hook has installed the formatter and how to turn it off. My .emacs file is well over 10,000 lines long (when all the files I bring in are included) with all the code I have in it. But that's what you get when you have "lived in" emacs, since the 1980s and don't hesitate to customize it.

4
  • This may be because you invoked indent-region or python-indent-region when saving, which indents the entire region line by line. You can try checking the write-file-functions in python-mode. Another suggestion is to locate the problem by continuously commenting out configurations through binary search. Commented Oct 22, 2023 at 4:09
  • Formatters for Python are in the following Python modules: Black/isort/autopep8/Yapf. Inside Emacs, only apheleia package calls Black if installed. Cannot reproduce your description, on Emacs 29.1 and vscode. Do not remove the formatters, get the habit to use them, as there is the standard PEP8 referring to format. If such case as yours is frequent , try to simply add a blank line before hijk(). Commented Oct 22, 2023 at 12:15
  • Yes, I prefer automatic formatting generally, but I hate when it changes the meaning of code (that I didn't write and I didn't modify). On this server, the version of emacs is 27.1. Commented Oct 22, 2023 at 13:38
  • I am going to try adding blank lines after indented blocks to see if that helps the issue. For now, I have been keeping a 2nd copy of the code and use ediff to find lines that were changed. Now, I will not only set them back to their previous indentation (undented) but add a blank line before them. Commented Oct 22, 2023 at 13:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.