0

I've noticed a peculiar behavior when using auto-fill-mode editing Python code. For example if I start typing the following (fill-column is set to 79):

def some_function(keyword_argument0, keyword_argument1,
                  keyword_argument2='foobar'):
    """
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

and press Enter, it will look like this:

def some_function(keyword_argument0, keyword_argument1,
                  keyword_argument2='foobar'):
    """
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
                  tempor incididunt ut labore et dolore magna aliqua.

If the keyword arguments are all in one line, the doc string continues to be indented with 4 spaces as expected, but whenever it is extended across multiple lines, any line of the doc string after the first will match the indentation of the second line of the list of keyword arguments. Is there a good reason for this, or is this just a bug in auto-fill-mode?

0

This is the expected behaviour as resulted from the emacs manual:

When Auto Fill mode breaks a line, it tries to obey the adaptive fill prefix: if a fill prefix can be deduced from the first and/or second line of the current paragraph, it is inserted into the new line (see Adaptive Fill). Otherwise the new line is indented, as though you had typed on it (see Indentation).

To overcome this, you can locally disable adaptive-fill-mode by adding the following lines to your python mode initialisation:

 (defun comment-auto-fill ()
    (setq-local adaptive-fill-mode nil)
    (auto-fill-mode 1))

  :hook
  ((python-mode . comment-auto-fill))

Note the above code is for use-package use in emacs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.