Fill comments in emacs python-mode

Alex cut_me_out at
Tue Sep 5 18:08:00 CEST 2000

> Here's an elisp function that will fill only a multiline comment, if
> it's called while the cursor is inside one.

There were a couple of boundary conditions I neglected.  Here's a
revised version:

(defun py-fill-paragraph (justify_p)

  "Restrict the fill to the surrounding comment, if there is one."
  (interactive "p")

    (if (looking-at "\\s-*#")

	  ;; This is inside a comment; restrict the fill to it.

	  ;; ...find the boundaries of the comment
	    (let ((comment-start (progn
				    "\\(^\\s-*[^ 	#]\\|\\`\\)")
				   (next-line 1)
	      (re-search-forward "\\(^\\s-*[^ 	#]\\|\\'\\)")

	      ;; That search could have taken us to either the first
	      ;; non-comment line, or the end of a comment line at the
	      ;; end of the buffer.  In the first case, need to avoid
	      ;; filling the line that we hit.
	      (if (not (looking-at "\\'"))
		    (previous-line 1)

	      ;; Fill the comment.
	      (fill-region comment-start (point)))))

      ;; Just do a normal fill, then.
      (fill-paragraph justify_p))))


The chain of destiny can only be grasped one link at a time.  
-- Sir Winston Churchill

More information about the Python-list mailing list