The formation of emission lines in quasars and Seyfert nuclei