1

How can I automatically set fill-column based on flake8 configuration for python-mode buffers, specifically the max-line-length value in .flake8? Solutions that employs existing extensions are welcome.

xuhdev
  • 1,839
  • 13
  • 26
  • Can you supply us with a shell command (something built into flake8?) which will read and output that value? – phils Sep 14 '20 at 02:27
  • @phils I don't know what command would do that and I couldn't find one. Perhaps I should go to flake8 team and request such a feature. – xuhdev Sep 14 '20 at 18:35
  • I imagine flake8 is using some such thing behind the scenes to read its config. You could do it in elisp, but I couldn't immediately find an elisp parser for that conf format, so I don't know whether that exists. You *could* just do a simple text search for the expected `max-line-length` entry, though, which would probably be "good enough". Perhaps you could add an example `.flake8` file to the question. – phils Sep 15 '20 at 22:56
  • As an alternative, you could of course add a `.dir-locals.el` file in the same location, if you didn't mind the duplication (although my impression is that the duplication is precisely what you're looking to avoid). – phils Sep 15 '20 at 22:59
  • @phils Yes, that is correct. The point is that when Emacs opens a file in a project, it should automatically detect the `max-line-length` settings by flake8 to avoid manually setting up every time. – xuhdev Sep 16 '20 at 02:50
  • FWIW you could probably hack together a solution with a `python-mode-hook` function which uses `locate-dominating-file` to look for a `flake8` file and, if it finds one, does a simple regexp search for `^max-line-length=\([0-9]+\)` (or whatever the expected syntax is), and does `(setq-local fill-column (string-to-number (match-string 1)))` if there was a match. – phils Sep 16 '20 at 10:48

0 Answers0