8

I'm writing a Python script in Emacs and have activated Auto-Fill minor mode with M-x auto-fill-mode. An issue I always seem to come across is that this fill mode tends to break quoted strings across multiple lines without making any compensating adjustments, resulting in an error when the script is run.

For example:

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

which results in SyntaxError: EOL while scanning string literal when run.

Is there a fill mode in Emacs that is Python "string literal aware" and automatically makes for example one of the line continuation/related adjustments discussed in Python style - line continuation with strings?, rather than naively splitting the string inducing an error?

Community
  • 1
  • 1
Bryce Thomas
  • 10,479
  • 26
  • 77
  • 126
  • 1
    Maybe consider a feature request at https://bugs.launchpad.net/python-mode. – Andreas Röhler May 20 '14 at 09:50
  • 2
    IMHO you are describing a bug on `python-fill-string` function of python.el (assuming you use a modern Emacs). By looking at the source code I can see that it makes no attempt to prevent syntax errors. It might or might not have been solved by maintainers; my advice is to report it via `report-emacs-bug` and get back here with their answer if useful. I am sure maintainers will appreciate a pointer to the accepted answer of the link you provide. – juanleon May 20 '14 at 10:01
  • 1
    I've reported this as a bug at https://bugs.launchpad.net/python-mode/+bug/1321266. I'll report back if there's a fix. – Bryce Thomas May 20 '14 at 12:28
  • BTW what about use of triple-quoted-string and output via print? – Andreas Röhler May 21 '14 at 07:35
  • 1
    @AndreasRöhler I believe that inserts undesired newlines in the printed output corresponding to the newlines in the triple-quoted string. The use case I'm thinking of is that one shouldn't have to know whether the string will reach the wrapping column limit when they begin typing it - it should be up to the fill mode to trigger any of the valid quotation changes as soon as the string wraps. – Bryce Thomas May 21 '14 at 11:06

2 Answers2

2

EDIT I've disabled this myself because it was causing Emacs to eat lots of processor time on certain documents. I just live with the bad fill mode -- would love if something would properly do this correctly:

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

/EDIT

I've been beating my head against this same problem for a while now, and finally found this solution.

This seems to work for me. Unfortunately I think this turns off filling for any quoted strings in all modes, not just Python. I'm sure someone with stronger elisp-fu than me can come up with a modification that restricts it to python-mode, but this is a better solution that that proposed above, IMO.

This solution taken from this related answer -- go give this answer some upvote 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)

'''

Michael Bacon
  • 2,600
  • 1
  • 12
  • 8
0

With current python-mode.el

it should behave like that:

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

Andreas Röhler
  • 4,804
  • 14
  • 18