- Got rid of newmodule.c
[python/dscho.git] / Lib / idlelib / FormatParagraph.py
blobcf5e36589b3379529a101b8a7bb97c1eab56681e
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
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 :-)
17 import string
18 import re
20 class FormatParagraph:
22 menudefs = [
23 ('format', [ # /s/edit/format dscherer@cmu.edu
24 ('Format Paragraph', '<<format-paragraph>>'),
28 def __init__(self, editwin):
29 self.editwin = editwin
31 def close(self):
32 self.editwin = None
34 def format_paragraph_event(self, event):
35 text = self.editwin.text
36 first, last = self.editwin.get_selection_indices()
37 if first and last:
38 data = text.get(first, last)
39 comment_header = ''
40 else:
41 first, last, comment_header, data = \
42 find_paragraph(text, text.index("insert"))
43 if comment_header:
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,
57 # so we hack away
58 block_suffix = ""
59 if not newdata[-1]:
60 block_suffix = "\n"
61 newdata = newdata[:-1]
62 builder = lambda item, prefix=comment_header: prefix+item
63 newdata = string.join(map(builder, newdata), '\n') + block_suffix
64 else:
65 # Just a normal text format
66 newdata = reformat_paragraph(data)
67 text.tag_remove("sel", "1.0", "end")
68 if newdata != data:
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()
74 else:
75 text.mark_set("insert", last)
76 text.see("insert")
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):
82 lineno = lineno + 1
83 line = text.get("%d.0" % lineno, "%d.0 lineend" % lineno)
84 first_lineno = 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:]):
89 lineno = lineno + 1
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:]):
98 lineno = lineno - 1
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")
105 i = 0
106 n = len(lines)
107 while i < n and is_all_white(lines[i]):
108 i = i+1
109 if i >= n:
110 return data
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])
114 else:
115 indent2 = indent1
116 new = lines[:i]
117 partial = indent1
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):
122 word = words[j]
123 if not word:
124 continue # Can happen when line ends in whitespace
125 if len(string.expandtabs(partial + word)) > limit and \
126 partial != indent1:
127 new.append(string.rstrip(partial))
128 partial = indent2
129 partial = partial + word + " "
130 if j+1 < len(words) and words[j+1] != " ":
131 partial = partial + " "
132 i = i+1
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 ""
147 return m.group(1)