  A method is described to count simple diagonal walks on $\mathbb{Z}^2$ with a
fixed starting point and endpoint on one of the axes and a fixed winding angle
around the origin. The method involves the decomposition of such walks into
smaller pieces, the generating functions of which are encoded in a commuting
set of Hilbert space operators. The general enumeration problem is then solved
by obtaining an explicit eigenvalue decomposition of these operators involving
elliptic functions. By further restricting the intermediate winding angles of
the walks to some open interval, the method can be used to count various
classes of walks restricted to cones in $\mathbb{Z}^2$ of opening angles that
are integer multiples of $\pi/4$.
  We present three applications of this main result. First we find an explicit
generating function for the walks in such cones that start and end at the
origin. In the particular case of a cone of angle $3\pi/4$ these walks are
directly related to Gessel's walks in the quadrant, and we provide a new proof
of their enumeration. Next we study the distribution of the winding angle of a
simple random walk on $\mathbb{Z}^2$ around a point in the close vicinity of
its starting point, for which we identify discrete analogues of the known
hyperbolic secant laws and a probabilistic interpretation of the Jacobi
elliptic functions. Finally we relate the spectrum of one of the Hilbert space
operators to the enumeration of closed loops in $\mathbb{Z}^2$ with fixed
winding number around the origin.
