blob: 671b30303167bc54612c672f8fa77fa1303baa38 [file] [log] [blame]
Guido van Rossum94e82ce1999-01-04 13:04:54 +00001# Extension to format a paragraph
2
Guido van Rossum3dd36891999-06-10 17:48:02 +00003# 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.
7
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
12# applied.
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 :-)
16
Guido van Rossum94e82ce1999-01-04 13:04:54 +000017import string
18import re
19
20class FormatParagraph:
21
22 menudefs = [
23 ('edit', [
24 ('Format Paragraph', '<<format-paragraph>>'),
25 ])
26 ]
27
28 keydefs = {
29 '<<format-paragraph>>': ['<Alt-q>'],
30 }
Guido van Rossume911c3e1999-01-04 16:34:41 +000031
32 unix_keydefs = {
33 '<<format-paragraph>>': ['<Meta-q>'],
34 }
Guido van Rossum94e82ce1999-01-04 13:04:54 +000035
36 def __init__(self, editwin):
37 self.editwin = editwin
38
39 def format_paragraph_event(self, event):
40 text = self.editwin.text
Guido van Rossum13205601999-06-11 15:03:00 +000041 first, last = self.editwin.get_selection_indices()
Guido van Rossum94e82ce1999-01-04 13:04:54 +000042 if first and last:
43 data = text.get(first, last)
Guido van Rossum3dd36891999-06-10 17:48:02 +000044 comment_header = ''
Guido van Rossum94e82ce1999-01-04 13:04:54 +000045 else:
Guido van Rossum3dd36891999-06-10 17:48:02 +000046 first, last, comment_header, data = \
47 find_paragraph(text, text.index("insert"))
48 if comment_header:
49 # Reformat the comment lines - convert to text sans header.
50 lines = string.split(data, "\n")
51 lines = map(lambda st, l=len(comment_header): st[l:], lines)
52 data = string.join(lines, "\n")
53 # Reformat to 70 chars or a 20 char width, whichever is greater.
54 format_width = max(70-len(comment_header), 20)
55 newdata = reformat_paragraph(data, format_width)
56 # re-split and re-insert the comment header.
57 newdata = string.split(newdata, "\n")
58 # If the block ends in a \n, we dont want the comment
59 # prefix inserted after it. (Im not sure it makes sense to
60 # reformat a comment block that isnt made of complete
61 # lines, but whatever!) Can't think of a clean soltution,
62 # so we hack away
63 block_suffix = ""
64 if not newdata[-1]:
65 block_suffix = "\n"
66 newdata = newdata[:-1]
67 builder = lambda item, prefix=comment_header: prefix+item
68 newdata = string.join(map(builder, newdata), '\n') + block_suffix
69 else:
70 # Just a normal text format
71 newdata = reformat_paragraph(data)
Guido van Rossum94e82ce1999-01-04 13:04:54 +000072 text.tag_remove("sel", "1.0", "end")
73 if newdata != data:
74 text.mark_set("insert", first)
Guido van Rossum318a70d1999-05-03 15:49:52 +000075 text.undo_block_start()
Guido van Rossum94e82ce1999-01-04 13:04:54 +000076 text.delete(first, last)
77 text.insert(first, newdata)
Guido van Rossum318a70d1999-05-03 15:49:52 +000078 text.undo_block_stop()
Guido van Rossum94e82ce1999-01-04 13:04:54 +000079 else:
80 text.mark_set("insert", last)
81 text.see("insert")
82
83def find_paragraph(text, mark):
84 lineno, col = map(int, string.split(mark, "."))
85 line = text.get("%d.0" % lineno, "%d.0 lineend" % lineno)
Guido van Rossum1e899cd1999-01-04 21:19:09 +000086 while text.compare("%d.0" % lineno, "<", "end") and is_all_white(line):
Guido van Rossum94e82ce1999-01-04 13:04:54 +000087 lineno = lineno + 1
88 line = text.get("%d.0" % lineno, "%d.0 lineend" % lineno)
89 first_lineno = lineno
Guido van Rossum3dd36891999-06-10 17:48:02 +000090 comment_header = get_comment_header(line)
91 comment_header_len = len(comment_header)
92 while get_comment_header(line)==comment_header and \
93 not is_all_white(line[comment_header_len:]):
Guido van Rossum94e82ce1999-01-04 13:04:54 +000094 lineno = lineno + 1
95 line = text.get("%d.0" % lineno, "%d.0 lineend" % lineno)
96 last = "%d.0" % lineno
97 # Search back to beginning of paragraph
98 lineno = first_lineno - 1
99 line = text.get("%d.0" % lineno, "%d.0 lineend" % lineno)
Guido van Rossum3dd36891999-06-10 17:48:02 +0000100 while lineno > 0 and \
101 get_comment_header(line)==comment_header and \
102 not is_all_white(line[comment_header_len:]):
Guido van Rossum94e82ce1999-01-04 13:04:54 +0000103 lineno = lineno - 1
104 line = text.get("%d.0" % lineno, "%d.0 lineend" % lineno)
105 first = "%d.0" % (lineno+1)
Guido van Rossum3dd36891999-06-10 17:48:02 +0000106 return first, last, comment_header, text.get(first, last)
Guido van Rossum94e82ce1999-01-04 13:04:54 +0000107
Guido van Rossum629082e1999-01-07 00:12:15 +0000108def reformat_paragraph(data, limit=70):
Guido van Rossum94e82ce1999-01-04 13:04:54 +0000109 lines = string.split(data, "\n")
110 i = 0
111 n = len(lines)
112 while i < n and is_all_white(lines[i]):
113 i = i+1
114 if i >= n:
115 return data
116 indent1 = get_indent(lines[i])
117 if i+1 < n and not is_all_white(lines[i+1]):
118 indent2 = get_indent(lines[i+1])
119 else:
120 indent2 = indent1
121 new = lines[:i]
122 partial = indent1
123 while i < n and not is_all_white(lines[i]):
124 # XXX Should take double space after period (etc.) into account
125 words = re.split("(\s+)", lines[i])
126 for j in range(0, len(words), 2):
127 word = words[j]
128 if not word:
129 continue # Can happen when line ends in whitespace
130 if len(string.expandtabs(partial + word)) > limit and \
131 partial != indent1:
132 new.append(string.rstrip(partial))
133 partial = indent2
134 partial = partial + word + " "
135 if j+1 < len(words) and words[j+1] != " ":
136 partial = partial + " "
137 i = i+1
138 new.append(string.rstrip(partial))
139 # XXX Should reformat remaining paragraphs as well
140 new.extend(lines[i:])
141 return string.join(new, "\n")
142
143def is_all_white(line):
144 return re.match(r"^\s*$", line) is not None
145
146def get_indent(line):
147 return re.match(r"^(\s*)", line).group()
Guido van Rossum3dd36891999-06-10 17:48:02 +0000148
149def get_comment_header(line):
150 m = re.match(r"^(\s*#*)", line)
151 if m is None: return ""
152 return m.group(1)