15

I would like to have auto-fill set to 79 columns for code sections and 72 for docstrings to get automatic PEP8 compliance. There seems to be an option to do this for Lisp mode (emacs-lisp-docstring-fill-column) but not for Python.

Is there an enhanced python-mode.el around somewhere that includes this?

Tim D
  • 1,645
  • 1
  • 25
  • 46

4 Answers4

4

I don't know how to do that, but I've never felt the need. It is so easy to use C-x f to change the fill column. And you can just hit M-p to reuse the last value you entered. Just C-x f M-p --- 3 keystrokes.

Drew
  • 29,895
  • 7
  • 74
  • 104
3

With the current python.el mode as dstributed with Emacs 24.3 you can redefine the python-fill-string as follows (in this example, I also set the fill-column to 85 and change the python-fill-docstring-style):

;; Python customizations
(defun my-python-fill-string (&optional justify)
  (let ((old-fill-column fill-column))
    (setq fill-column 72)
    (python-fill-string justify)
    (setq fill-column old-fill-column)
  ))

(add-hook 'python-mode-hook
          (lambda () (interactive)
            (setq python-fill-docstring-style 'pep-257-nn)
            (set-fill-column 85)
            (setq python-fill-string-function my-python-fill-string)
            ))
mforbes
  • 7,062
  • 3
  • 16
  • 21
3

Only slightly tested:

(defadvice current-fill-column (around handle-docstring activate)
  (flet ((docp (p) (let ((q (get-text-property p 'face))
                         (r 'font-lock-string-face))
                     (or (eq r q) (memq r q)))))
    (if (or (docp (point)) (docp (point-at-bol)) (docp (point-at-eol)))
        (setq ad-return-value 72)
      ad-do-it)))

This depends on font-lock-mode being enabled to detect the docstrings.

huaiyuan
  • 26,129
  • 5
  • 57
  • 63
  • Would I tuck this into my python-mode.el? – Tim D Jan 20 '12 at 16:09
  • You may put it in your [init file](http://www.gnu.org/savannah-checkouts/gnu/emacs/manual/html_node/emacs/Init-File.html). – huaiyuan Jan 20 '12 at 18:56
  • I'm getting an error: "Wrong type argument: listp, font-lock-keyword-face" with font-lock-mode enabled or disabled. – Tim D Jan 24 '12 at 14:37
1

Current python-mode.el provides

(defcustom py-docstring-fill-column 72 [...]

(defcustom py-comment-fill-column 79 [...]

while for code value of fill-column is used.

Andreas Röhler
  • 4,804
  • 14
  • 18
  • @Drew Indeed posted the beginning of the definitions only, hoping it conveys all info needed. Added some ellipsis to make that clear. – Andreas Röhler May 21 '15 at 16:11
  • @Drew Hmm, don't understand your concern. Said: python-mode.el provides, i.e. it's a copy from there. The question was about compliance. – Andreas Röhler May 21 '15 at 17:45
  • Sorry, I misunderstood. I thought you were telling the OP to put those `defcustom`s in his init file. – Drew May 21 '15 at 18:07
  • @AndreasRöhler, sorry for the noob request, but please could you expand this answer a bit to show how an Emacs user can take advantage of those lines you have quoted from python-mode.el? Thanks! –  Jan 05 '16 at 13:30
  • @sampablokuper With a defcustom macro, M-x customize-variable RET.. will allow user-defined values. HTH – Andreas Röhler Jan 06 '16 at 07:00
  • @AndreasRöhler, thanks for your reply, and apologies: I should have been clearer about what I was asking. Suppose I'm in Python mode, and am editing a Python file. How can I ensure that when I perform Edit > Fill (or in Evil, `gq`), the wrapping is performed based on the py-docstring-fill-column value if my selected text is in a docstring, or likewise on the py-comment-fill-column value if my selected text is instead in a comment? –  Jan 06 '16 at 13:58
  • @sampablokuper M-x fill-paragraph RET should DTRT – Andreas Röhler Jan 07 '16 at 07:14
  • 1
    @AndreasRöhler, I was using Emacs 24's built-in Python mode, which I have now learned is *not* `python-mode.el` but rather `python.el`. So, I probably need to go and investigate whether I can get `python-mode.el` working for me. Thanks again :) –  Jan 08 '16 at 14:45