Round #2 of http://lists.gnu.org/archive/html/emacs-devel/2012-10/msg00483.html, re-applied on top of trunk revision 110817. Dmitry