Sometimes it is useful to track the mouse, which means to display something to indicate where the mouse is and move the indicator as the mouse moves. For efficient mouse tracking, you need a way to wait until the mouse actually moves.
The convenient way to track the mouse is to ask for events to represent mouse motion. Then you can wait for motion by waiting for an event. In addition, you can easily handle any other sorts of events that may occur. That is useful, because normally you don't want to track the mouse forever—only until some other event, such as the release of a button.
This special form executes body, with generation of mouse motion events enabled. Typically, body would use
read-eventto read the motion events and modify the display accordingly. See Motion Events, for the format of mouse motion events.
The value of
track-mouseis that of the last form in body. You should design body to return when it sees the up-event that indicates the release of the button, or whatever kind of event means it is time to stop tracking.
The usual purpose of tracking mouse motion is to indicate on the screen the consequences of pushing or releasing a button at the current position.
In many cases, you can avoid the need to track the mouse by using
mouse-face text property (see Special Properties).
That works at a much lower level and runs more smoothly than
Lisp-level mouse tracking.