Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In emacs Python mode, how do I set a different auto-fill width for docstrings and code?

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?

like image 705
Tim D Avatar asked Jan 11 '12 17:01

Tim D


3 Answers

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.

like image 26
huaiyuan Avatar answered Oct 07 '22 03:10

huaiyuan


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.

like image 188
Drew Avatar answered Oct 07 '22 01:10

Drew


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)
            ))
like image 3
mforbes Avatar answered Oct 07 '22 02:10

mforbes