Fill comments in emacs python-mode

Alex cut_me_out at hotmail.com
Tue Sep 5 12:08:00 EDT 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")

  (save-excursion
    (beginning-of-line)
    (if (looking-at "\\s-*#")
	(progn

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

	  ;; ...find the boundaries of the comment
	  (save-excursion
	    (let ((comment-start (progn
				   (re-search-backward
				    "\\(^\\s-*[^ 	#]\\|\\`\\)")
				   (next-line 1)
				   (beginning-of-line)
				   (point))))
	      (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 "\\'"))
		  (progn
		    (previous-line 1)
		    (end-of-line)))

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

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

Alex.

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



More information about the Python-list mailing list