Regarding this entire thread, I know an automatic method is attractive, and may make sense in some cases, but with programming examples, I think going fully automated is dangerous.
There are two competing questions:
1) Does the example scan well to the eye?
2) If the reader types (or cuts and pastes) the example into a compiler/interpreter verbatim, will it work?
I don't know of any automated approach that can satisfy these two conditions consistently.
I think you're better off having the stylesheet warn you when you exceed a maximum line length, and then fixing the offending examples directly. If you do that, you can be assured that examples will work the same for readers who type or cut/paste the example as it the original example did.
Jirka does make a good point that you may not have that luxury, but if you don't, and you need to accept un-reviewed line breaks, then at least make files that contain the "true, unbroken" examples available for download.
Best Regards,
Dick Hamilton
-------
XML Press
XML for Technical Communicators
http://xmlpress.net
(970) 231-3624