Commit c2f77ddd authored by Guido van Rossum's avatar Guido van Rossum

New feature: when saving a file, keep the eol convention of the

original.  New files are written using the eol convention of the
platform, given by os.linesep.  All files are read and written in
binary mode.
parent 9635268e
...@@ -178,6 +178,10 @@ class IOBinding: ...@@ -178,6 +178,10 @@ class IOBinding:
self.text.focus_set() self.text.focus_set()
return "break" return "break"
eol = r"(\r\n)|\n|\r" # \r\n (Windows), \n (UNIX), or \r (Mac)
eol_re = re.compile(eol)
eol_convention = os.linesep # Default
def loadfile(self, filename): def loadfile(self, filename):
try: try:
# open the file in binary mode so that we can handle # open the file in binary mode so that we can handle
...@@ -191,8 +195,10 @@ class IOBinding: ...@@ -191,8 +195,10 @@ class IOBinding:
chars = self.decode(chars) chars = self.decode(chars)
# We now convert all end-of-lines to '\n's # We now convert all end-of-lines to '\n's
eol = r"(\r\n)|\n|\r" # \r\n (Windows), \n (UNIX), or \r (Mac) firsteol = self.eol_re.search(chars)
chars = re.compile( eol ).sub( r"\n", chars ) if firsteol:
self.eol_convention = firsteol.group(0)
chars = self.eol_re.sub(r"\n", chars)
self.text.delete("1.0", "end") self.text.delete("1.0", "end")
self.set_filename(None) self.set_filename(None)
...@@ -306,8 +312,10 @@ class IOBinding: ...@@ -306,8 +312,10 @@ class IOBinding:
def writefile(self, filename): def writefile(self, filename):
self.fixlastline() self.fixlastline()
chars = self.encode(self.text.get("1.0", "end-1c")) chars = self.encode(self.text.get("1.0", "end-1c"))
if self.eol_convention != "\n":
chars = chars.replace("\n", self.eol_convention)
try: try:
f = open(filename, "w") f = open(filename, "wb")
f.write(chars) f.write(chars)
f.close() f.close()
return True return True
......
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