We derive an exchange energy functional of generalized gradient form with a corresponding potential that changes discontinuously at integer particle numbers. The functional is semilocal, yet incorporates key features that are connected to the derivative discontinuity of Kohn-Sham density-functional theory. We validate our construction for several paradigm systems and explain how it addresses central well-known deficiencies of antecedent semilocal methods, i.e., the description of charge transfer, properly localized orbitals, and band gaps. We find, e.g., an improved shell structure for atoms, eigenvalues that more closely correspond to ionization energies, and an improved description of band structure where localized states are lowered in energy.