[Pythonmac-SIG] IDE patch to convert UNIX/MSDOS line feeds to carriage returns

Oliver Steele steele@cs.brandeis.edu
Fri, 21 May 1999 17:19:43 -0400


Despite the best intentions of Anarchie and Internet Explorer, I often end
up with Python source files (and other text files that I'd like to edit with
PythonIDE) that use '\n' instead of '\r' as a line separator (and therefore
show up as garbage in the text editing window).

The patch at
<http://www.cs.brandeis.edu/~steele/sources/PythonIDE-crlf-patch.txt> causes
PythonIDE to notice when you open such a file, and offer to convert its
'\n's to '\r's.  It consists of 14 lines of code that go at the end of
Editor.__init__ in {Python}:IDE:IDELib:IDE:PyEdit.py.

I've also enclosed the code below, but I expect my mailer to mangle it in
three ways:  tabs will have been turned into spaces; the call to
AskYesNoCancel will have been broken across two or more lines; and the
initial '%s' on that line is supposed to be enclosed by curly braces
(option-[ and shift-option-[).
    if '\n' in text:
        import EasyDialogs
        if string.find(text, '\r\n') >= 0:
            sourceOS = 'DOS'
            searchString = '\r\n'
        else:
            sourceOS = 'UNIX'
            searchString = '\n'
        change = EasyDialogs.AskYesNoCancel('³%s² contains %s-style line
feeds.  Change them to MacOS carriage returns?' % (self.title, sourceOS), 1)
        # bug: Cancel is treated as No
        if change > 0:
            text = string.replace(text, searchString, '\r')
            self.editgroup.editor.set(text, self.getfilename())
            self.editgroup.editor.changed = 1