Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2f77dd

Browse files
committed
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.
1 parent 9635268 commit c2f77dd

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

Lib/idlelib/IOBinding.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ def open(self, event=None, editFile=None):
178178
self.text.focus_set()
179179
return "break"
180180

181+
eol = r"(\r\n)|\n|\r" # \r\n (Windows), \n (UNIX), or \r (Mac)
182+
eol_re = re.compile(eol)
183+
eol_convention = os.linesep # Default
184+
181185
def loadfile(self, filename):
182186
try:
183187
# open the file in binary mode so that we can handle
@@ -191,8 +195,10 @@ def loadfile(self, filename):
191195

192196
chars = self.decode(chars)
193197
# We now convert all end-of-lines to '\n's
194-
eol = r"(\r\n)|\n|\r" # \r\n (Windows), \n (UNIX), or \r (Mac)
195-
chars = re.compile( eol ).sub( r"\n", chars )
198+
firsteol = self.eol_re.search(chars)
199+
if firsteol:
200+
self.eol_convention = firsteol.group(0)
201+
chars = self.eol_re.sub(r"\n", chars)
196202

197203
self.text.delete("1.0", "end")
198204
self.set_filename(None)
@@ -306,8 +312,10 @@ def save_a_copy(self, event):
306312
def writefile(self, filename):
307313
self.fixlastline()
308314
chars = self.encode(self.text.get("1.0", "end-1c"))
315+
if self.eol_convention != "\n":
316+
chars = chars.replace("\n", self.eol_convention)
309317
try:
310-
f = open(filename, "w")
318+
f = open(filename, "wb")
311319
f.write(chars)
312320
f.close()
313321
return True

0 commit comments

Comments
 (0)