1 # Extension to format a paragraph
3 # Does basic, standard text formatting, and also understands Python
4 # comment blocks. Thus, for editing Python source code, this
5 # extension is really only suitable for reformatting these comment
6 # blocks or triple-quoted strings.
8 # Known problems with comment reformatting:
9 # * If there is a selection marked, and the first line of the
10 # selection is not complete, the block will probably not be detected
11 # as comments, and will have the normal "text formatting" rules
13 # * If a comment block has leading whitespace that mixes tabs and
14 # spaces, they will not be considered part of the same block.
15 # * Fancy comments, like this bulleted list, arent handled :-)
20 class FormatParagraph
:
23 ('format', [ # /s/edit/format dscherer@cmu.edu
24 ('Format Paragraph', '<<format-paragraph>>'),
28 def __init__(self
, editwin
):
29 self
.editwin
= editwin
34 def format_paragraph_event(self
, event
):
35 text
= self
.editwin
.text
36 first
, last
= self
.editwin
.get_selection_indices()
38 data
= text
.get(first
, last
)
41 first
, last
, comment_header
, data
= \
42 find_paragraph(text
, text
.index("insert"))
44 # Reformat the comment lines - convert to text sans header.
45 lines
= string
.split(data
, "\n")
46 lines
= map(lambda st
, l
=len(comment_header
): st
[l
:], lines
)
47 data
= string
.join(lines
, "\n")
48 # Reformat to 70 chars or a 20 char width, whichever is greater.
49 format_width
= max(70-len(comment_header
), 20)
50 newdata
= reformat_paragraph(data
, format_width
)
51 # re-split and re-insert the comment header.
52 newdata
= string
.split(newdata
, "\n")
53 # If the block ends in a \n, we dont want the comment
54 # prefix inserted after it. (Im not sure it makes sense to
55 # reformat a comment block that isnt made of complete
56 # lines, but whatever!) Can't think of a clean soltution,
61 newdata
= newdata
[:-1]
62 builder
= lambda item
, prefix
=comment_header
: prefix
+item
63 newdata
= string
.join(map(builder
, newdata
), '\n') + block_suffix
65 # Just a normal text format
66 newdata
= reformat_paragraph(data
)
67 text
.tag_remove("sel", "1.0", "end")
69 text
.mark_set("insert", first
)
70 text
.undo_block_start()
71 text
.delete(first
, last
)
72 text
.insert(first
, newdata
)
73 text
.undo_block_stop()
75 text
.mark_set("insert", last
)
78 def find_paragraph(text
, mark
):
79 lineno
, col
= map(int, string
.split(mark
, "."))
80 line
= text
.get("%d.0" % lineno
, "%d.0 lineend" % lineno
)
81 while text
.compare("%d.0" % lineno
, "<", "end") and is_all_white(line
):
83 line
= text
.get("%d.0" % lineno
, "%d.0 lineend" % lineno
)
85 comment_header
= get_comment_header(line
)
86 comment_header_len
= len(comment_header
)
87 while get_comment_header(line
)==comment_header
and \
88 not is_all_white(line
[comment_header_len
:]):
90 line
= text
.get("%d.0" % lineno
, "%d.0 lineend" % lineno
)
91 last
= "%d.0" % lineno
92 # Search back to beginning of paragraph
93 lineno
= first_lineno
- 1
94 line
= text
.get("%d.0" % lineno
, "%d.0 lineend" % lineno
)
95 while lineno
> 0 and \
96 get_comment_header(line
)==comment_header
and \
97 not is_all_white(line
[comment_header_len
:]):
99 line
= text
.get("%d.0" % lineno
, "%d.0 lineend" % lineno
)
100 first
= "%d.0" % (lineno
+1)
101 return first
, last
, comment_header
, text
.get(first
, last
)
103 def reformat_paragraph(data
, limit
=70):
104 lines
= string
.split(data
, "\n")
107 while i
< n
and is_all_white(lines
[i
]):
111 indent1
= get_indent(lines
[i
])
112 if i
+1 < n
and not is_all_white(lines
[i
+1]):
113 indent2
= get_indent(lines
[i
+1])
118 while i
< n
and not is_all_white(lines
[i
]):
119 # XXX Should take double space after period (etc.) into account
120 words
= re
.split("(\s+)", lines
[i
])
121 for j
in range(0, len(words
), 2):
124 continue # Can happen when line ends in whitespace
125 if len(string
.expandtabs(partial
+ word
)) > limit
and \
127 new
.append(string
.rstrip(partial
))
129 partial
= partial
+ word
+ " "
130 if j
+1 < len(words
) and words
[j
+1] != " ":
131 partial
= partial
+ " "
133 new
.append(string
.rstrip(partial
))
134 # XXX Should reformat remaining paragraphs as well
135 new
.extend(lines
[i
:])
136 return string
.join(new
, "\n")
138 def is_all_white(line
):
139 return re
.match(r
"^\s*$", line
) is not None
141 def get_indent(line
):
142 return re
.match(r
"^(\s*)", line
).group()
144 def get_comment_header(line
):
145 m
= re
.match(r
"^(\s*#*)", line
)
146 if m
is None: return ""