macros are easy enough: start them with .de XX and end them with .., e.g. start and end code example which I end up adding to most documents I write:
.de EX .nj .nf .CW .SM .. .de EE .fi .ju .LG .R .. There are quite a few books, see http://www.troff.org/ , my favorite is "Document formatting & typsetting on the UNIX system". I really don't know how you would add line numbers, if you are lucky there may be a well-known macro which is run at the start of every line... -Steve