Thanks to Paul Kim for a great talk on Cocoa's text system. Paul showed how to implement two features you might want if you were writing a programmer's code editor: line numbering and syntax highlighting.
Here are Paul's Keynote slides and example code. I've also embedded the slides below.
Paul posted a blog post about line numbering a while back. You can find more of his code on GitHub.
[UPDATE: At Paul's request I've made a correction. I deleted the third bullet from the second-to-last slide ("Things to Improve").]