Fill comments in emacs python-mode

Alex cut_me_out at hotmail.com
Mon Sep 4 11:29:45 EDT 2000


Here's an elisp function that will fill only a multiline comment, if
it's called while the cursor is inside one.  It's poorly tested, and
only with FSF emacs.  Please let me know if it blows up on you so I can
fix it (and if you don't know how to recover if, for instance, it fills
your entire source code as a single paragraph, you probably shouldn't be
using it.)

(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-+[^ 	#]\\|\\'\\)")
	      (previous-line 1)
	      (end-of-line)

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

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

(define-key py-mode-map "\eq" 'py-fill-paragraph)

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