[WIP] Popupmnu cleanup

*DO NOT MERGE*
This is my ongoing work to clean up `popupmnu.c`, ref #238
Adding as a pull so others more easily see there is work going on in that file.

PS: There are so many changes that looking at the diff is likely to be very confusing. If you wish to help review this in it's current state, just look at the raw output.