How can I 'trim' the users returned by a user reference fields' autocomplete widget? By 'trim' I mean that I want to restrict the usernames returned according to some criterion. For example, if I enter 'S' into my user reference field, I want the drop-down list to contain all usernames begining with 'S' and where, for example, the users favourite color equals 'Red'. 'Favourite color' would be a field added on to the user entity.

My first idea is to use hook_query_alter to add a condition clause to the relevant query in user_reference module, but I'm not sure if this is possible. Has anyone done something like this before?

Thanks
You can use hook_form_alter() to provide your own autocomplete path. After that you can code it just like if you were creating brand new autocomplete field. Examples for developers module contains pretty solid autocomplete example, use it as a reference.