Emacs fill mode for Python that does not interrupt quoted lines causing errors

I am writing a Python script in Emacs and have activated Auto-Fill helper mode using Mx auto-fill-mode . The problem that I always encounter is that this filling mode tends to interrupt quoted lines on multiple lines without any compensation adjustments, which leads to an error when running the script.

For instance:

 print 'the quick brown fox jumped over the lazy dog and then did something else' 

which leads to SyntaxError: EOL while scanning string literal at startup.

Is there a fill mode in Emacs, which is a Python "string literal", and automatically makes, for example, one of the extensions / related line changes discussed in Python style - line continuation with lines? instead of naively breaking the line causing the error?

+7
source share
3 answers

EDIT I disabled this myself because it caused Emacs to spend a lot of CPU time on certain documents. I just live with a poor fill mode - I would like for something to do it right correctly:

 some_list.append("This is a very long string which I would like to " "break in a sensible way") 

/EDIT

I’ve been banging my head about the same problem for some time and finally found this solution.

It seems to work for me. Unfortunately, I think this disables the filling of any quoted strings in all modes, not just Python. I'm sure someone with stronger elisp-fu than me can suggest a modification that limits it to python mode, but this is the best solution suggested above, IMO.

This decision is taken from this related answer - go answer this question with some love if you like it.

 (defun odd-number-of-single-quotes-this-paragraph-so-far () (oddp (how-many "'" (save-excursion (backward-paragraph) (point)) (point)))) (defun odd-number-of-double-quotes-this-paragraph-so-far () (oddp (how-many "\"" (save-excursion (backward-paragraph) (point)) (point)))) (add-to-list 'fill-nobreak-predicate 'odd-number-of-single-quotes-this-paragraph-so-far) (add-to-list 'fill-nobreak-predicate 'odd-number-of-double-quotes-this-paragraph-so-far) 

"" "

+2
source

AFAIU pythonic will use tricycle strings and print out.

This solution offers the fill function used by autocomplete:

 (defun my-make-continuation-line-by-fill () (when (eq major-mode 'python-mode) (save-excursion (forward-line -1) (end-of-line) (unless (member (char-before) (list 92 32)) (insert-and-inherit " ") (unless (eq (char-after) ?\\) (insert-and-inherit "\\")))))) 

In this case, it is limited to python mode, but should work with others as well.

Which function of the board depends on the system settings. Here do-auto-fill

Make sure the function above is loaded and placed in the Emacs source file:

 (defadvice do-auto-fill (after my-make-continuation-line-by-fill activate)(my-make-continuation-line-by-fill)) (ad-activate 'do-auto-fill) 
0
source

With current python-mode.el

he should behave like this:

 print 'the quick brown fox jumped over the lazy dog and then did something asd asdf \ asdf elssdsd e' 

https://gitlab.com/python-mode-devs/python-mode

0
source

Source: https://habr.com/ru/post/969566/


All Articles