Re: Handling OS specific line breaks
Re: Handling OS specific line breaks
- Subject: Re: Handling OS specific line breaks
- From: Douglas Davidson <email@hidden>
- Date: Mon, 20 May 2002 10:30:50 -0700
On Monday, May 20, 2002, at 08:25 AM, Chris Giordano wrote:
Two ways come to mind immediately: you can either treat all of your
text view text in the same manner -- that is, translate to Unix line
endings on load, and translate back to the original on save -- or you
can make the text view better able to handle the line endings. For the
latter, I'd guess that NSResponder's -(void)insertNewline:(id)sender
method would be a good candidate for overriding. There is also the
-(void)insertNewlineIgnoringFieldEditor:(id)sender. (Here is one of
those areas where I don't have the experience to tell you which is
appropriate, but I do see both of these in the documentation.) In your
overridden method, you'd just insert whichever characters were
appropriate for your file. Since NSTextView is a subclass of
NSResponder, subclassing NSTextView and overriding one of these methods
should give you the result you need. I haven't done this myself, but
this looks like the way to go on this.
insertNewline: usually will be called when return is pressed,
insertNewlineIgnoringFieldEditor: when option-return is pressed. These
are probably not the best places to intervene, since they do not deal
with e.g. line endings in pasted text. I would suggest acting as the
textview's delegate and implementing
textView:shouldChangeTextInRange:replacementString: if you wish to
sanitize text as it comes in.
Or I guess a third option would be to allow the inconsistecy, and
simply do some processing before saving, correcting any
inconsistencies. You can just search for the other two options and
replace them. These searches get a bit ugly since you have to
correctly not replace parts of the DOS version -- someone managed to
store a DOS file on our IBM mainframe (everything in EBCDIC), and
bringing it over to my OS X machine results in very consistent garbage
if done as a binary file, or in a file with blank lines between all of
the lines due to replacing the "\r" in "\r\n" with "\n", giving "\n\n"
at the end of every line.
You will have the same issue whenever you sanitize your text. The
appropriate thing would be to use the NSString method
getLineStart:end:contentsEnd:forRange: to iterate through the text by
lines; this will automatically recognize line endings of the various
types and inform you of their location (through the difference between
the line end and the contents end).
Douglas Davidson
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.