'Formats paragraph to a max width specified in idleConf.
If text is selected, format_paragraph_event will start breaking lines
at the max width, starting from the beginning selection.
If no text is selected, format_paragraph_event uses the current
cursor location to determine the paragraph (lines of text surrounded
by blank lines) and formats it.
The length limit parameter is for testing with a known value.'

def format_paragraph_event(self, event, limit=None):

    if (limit is None):
        limit = idleConf.GetOption('extensions', 'FormatParagraph', 'max-width', type='int', default=72)
    text = self.editwin.text
    (first, last) = self.editwin.get_selection_indices()
    if (first and last):
        data = text.get(first, last)
        comment_header = get_comment_header(data)
    else:
        (first, last, comment_header, data) = find_paragraph(text, text.index('insert'))
    if comment_header:
        newdata = reformat_comment(data, limit, comment_header)
    else:
        newdata = reformat_paragraph(data, limit)
    text.tag_remove('sel', '1.0', 'end')
    if (newdata != data):
        text.mark_set('insert', first)
        text.undo_block_start()
        text.delete(first, last)
        text.insert(first, newdata)
        text.undo_block_stop()
    else:
        text.mark_set('insert', last)
    text.see('insert')
    return 'break'
