summaryrefslogtreecommitdiffstats
path: root/Lib/idlelib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2003-04-25 18:36:31 (GMT)
committerGuido van Rossum <guido@python.org>2003-04-25 18:36:31 (GMT)
commitc2f77dddf3a4d4476dd95439f670cbab1c65e288 (patch)
tree148b2053fdc34594afcf2a41f02538f7f3f5cb03 /Lib/idlelib
parent9635268ea961da46b9d9ee2b40d8fb526f573e1f (diff)
downloadcpython-c2f77dddf3a4d4476dd95439f670cbab1c65e288.zip
cpython-c2f77dddf3a4d4476dd95439f670cbab1c65e288.tar.gz
cpython-c2f77dddf3a4d4476dd95439f670cbab1c65e288.tar.bz2
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.
Diffstat (limited to 'Lib/idlelib')
-rw-r--r--Lib/idlelib/IOBinding.py14
1 files changed, 11 insertions, 3 deletions
diff --git a/Lib/idlelib/IOBinding.py b/Lib/idlelib/IOBinding.py
index d321d8c..6f46a60 100644
--- a/Lib/idlelib/IOBinding.py
+++ b/Lib/idlelib/IOBinding.py
@@ -178,6 +178,10 @@ class IOBinding:
self.text.focus_set()
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):
try:
# open the file in binary mode so that we can handle
@@ -191,8 +195,10 @@ class IOBinding:
chars = self.decode(chars)
# We now convert all end-of-lines to '\n's
- eol = r"(\r\n)|\n|\r" # \r\n (Windows), \n (UNIX), or \r (Mac)
- chars = re.compile( eol ).sub( r"\n", chars )
+ firsteol = self.eol_re.search(chars)
+ if firsteol:
+ self.eol_convention = firsteol.group(0)
+ chars = self.eol_re.sub(r"\n", chars)
self.text.delete("1.0", "end")
self.set_filename(None)
@@ -306,8 +312,10 @@ class IOBinding:
def writefile(self, filename):
self.fixlastline()
chars = self.encode(self.text.get("1.0", "end-1c"))
+ if self.eol_convention != "\n":
+ chars = chars.replace("\n", self.eol_convention)
try:
- f = open(filename, "w")
+ f = open(filename, "wb")
f.write(chars)
f.close()
return True