Commit 96115ee4 authored by Kurt B. Kaiser's avatar Kurt B. Kaiser

Merge Py Idle changes:

Rev 1.10 (string methods)
parent 543cb977
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
# spaces, they will not be considered part of the same block. # spaces, they will not be considered part of the same block.
# * Fancy comments, like this bulleted list, arent handled :-) # * Fancy comments, like this bulleted list, arent handled :-)
import string
import re import re
class FormatParagraph: class FormatParagraph:
...@@ -42,14 +41,14 @@ class FormatParagraph: ...@@ -42,14 +41,14 @@ class FormatParagraph:
find_paragraph(text, text.index("insert")) find_paragraph(text, text.index("insert"))
if comment_header: if comment_header:
# Reformat the comment lines - convert to text sans header. # Reformat the comment lines - convert to text sans header.
lines = string.split(data, "\n") lines = data.split("\n")
lines = map(lambda st, l=len(comment_header): st[l:], lines) lines = map(lambda st, l=len(comment_header): st[l:], lines)
data = string.join(lines, "\n") data = "\n".join(lines)
# Reformat to 70 chars or a 20 char width, whichever is greater. # Reformat to 70 chars or a 20 char width, whichever is greater.
format_width = max(70-len(comment_header), 20) format_width = max(70-len(comment_header), 20)
newdata = reformat_paragraph(data, format_width) newdata = reformat_paragraph(data, format_width)
# re-split and re-insert the comment header. # re-split and re-insert the comment header.
newdata = string.split(newdata, "\n") newdata = newdata.split("\n")
# If the block ends in a \n, we dont want the comment # If the block ends in a \n, we dont want the comment
# prefix inserted after it. (Im not sure it makes sense to # prefix inserted after it. (Im not sure it makes sense to
# reformat a comment block that isnt made of complete # reformat a comment block that isnt made of complete
...@@ -60,7 +59,7 @@ class FormatParagraph: ...@@ -60,7 +59,7 @@ class FormatParagraph:
block_suffix = "\n" block_suffix = "\n"
newdata = newdata[:-1] newdata = newdata[:-1]
builder = lambda item, prefix=comment_header: prefix+item builder = lambda item, prefix=comment_header: prefix+item
newdata = string.join(map(builder, newdata), '\n') + block_suffix newdata = '\n'.join(map(builder, newdata)) + block_suffix
else: else:
# Just a normal text format # Just a normal text format
newdata = reformat_paragraph(data) newdata = reformat_paragraph(data)
...@@ -76,7 +75,7 @@ class FormatParagraph: ...@@ -76,7 +75,7 @@ class FormatParagraph:
text.see("insert") text.see("insert")
def find_paragraph(text, mark): def find_paragraph(text, mark):
lineno, col = map(int, string.split(mark, ".")) lineno, col = map(int, mark.split("."))
line = text.get("%d.0" % lineno, "%d.0 lineend" % lineno) line = text.get("%d.0" % lineno, "%d.0 lineend" % lineno)
while text.compare("%d.0" % lineno, "<", "end") and is_all_white(line): while text.compare("%d.0" % lineno, "<", "end") and is_all_white(line):
lineno = lineno + 1 lineno = lineno + 1
...@@ -101,7 +100,7 @@ def find_paragraph(text, mark): ...@@ -101,7 +100,7 @@ def find_paragraph(text, mark):
return first, last, comment_header, text.get(first, last) return first, last, comment_header, text.get(first, last)
def reformat_paragraph(data, limit=70): def reformat_paragraph(data, limit=70):
lines = string.split(data, "\n") lines = data.split("\n")
i = 0 i = 0
n = len(lines) n = len(lines)
while i < n and is_all_white(lines[i]): while i < n and is_all_white(lines[i]):
...@@ -122,18 +121,18 @@ def reformat_paragraph(data, limit=70): ...@@ -122,18 +121,18 @@ def reformat_paragraph(data, limit=70):
word = words[j] word = words[j]
if not word: if not word:
continue # Can happen when line ends in whitespace continue # Can happen when line ends in whitespace
if len(string.expandtabs(partial + word)) > limit and \ if len((partial + word).expandtabs()) > limit and \
partial != indent1: partial != indent1:
new.append(string.rstrip(partial)) new.append(partial.rstrip())
partial = indent2 partial = indent2
partial = partial + word + " " partial = partial + word + " "
if j+1 < len(words) and words[j+1] != " ": if j+1 < len(words) and words[j+1] != " ":
partial = partial + " " partial = partial + " "
i = i+1 i = i+1
new.append(string.rstrip(partial)) new.append(partial.rstrip())
# XXX Should reformat remaining paragraphs as well # XXX Should reformat remaining paragraphs as well
new.extend(lines[i:]) new.extend(lines[i:])
return string.join(new, "\n") return "\n".join(new)
def is_all_white(line): def is_all_white(line):
return re.match(r"^\s*$", line) is not None return re.match(r"^\s*$", line) is not None
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment