A generalization of the Curtis-Hedlund theorem