figlegend: ensure it fits in the figure


I am generating a figure with 4 subplots, then using the "figlegend"
command to generate a legend on the right side of the four plots.
This is part of a script designed to handle varrying numbers of lines
to be plotted, so sometimes the legend has many entries and sometimes
it has a few. Is there an easy way to prevent the legend from
extending beyond the figure boundaries when the legend has many
entries, ie. to automatically adjust the vertical spacing in the
legend, font size, etc. to make it fit?