Searching forward and backward from a position

CQL offers several filters that match a position depending on what happened previously or subsequently in the game. Each of these filters' names' begins with the string next or previous, depending on whether the filter is looking forward or backward in the game:

filter name example description
next next (check check mate)
next (check+ mate)
Filters in the argument list match subsequent positions, starting with current position
next2 next2 (check check mate)
next2 (check+ mate)
Filters in the argument list match every other subsequent position, starting with current position
next* next* stalemate Some subsequent (or current) position matches the argument filter
previous previous (check check mate)
previous (check+ mate)
Filters in the argument list match preceding positions, starting with current position
previous2 previous2 (check check mate)
previous2 (check+ mate)
Filters in the argument list match every other preceding position, starting with current position
previous* previous* Ka-h8 Some subsequent (or current) position matches the argument filter

Interaction with the current position

Each of these filters modifies the current position before its argument filters are evaluated. For example, previous sets the current position to every position equal to or a predecessor of the current position when the previous filter was first reached. After the current position is set to such a new position, the appropriate argument filter is evaluated at the new current position.

Of course, after these filters have finished matching their arguments, the current position is restored to what it was originally.

Interaction with variations

When the variations parameter is set in the CQL header, these filters will search in both variations and the mainline. Otherwise, the filters only search in the mainline.

Counting

Each of these filters is countable and accepts an optional range argument following its name. The meaning of these ranges is described in the following table:

filter name with range example description
next range next 5 100 (check+ mate) Length of longest sequence of subsequent positions starting with current position matching arguments must be in range
next2 range next2 5 100 (check+ mate) Length of longest sequence of every other subsequent (or current) positions matching arguments, starting with current position, must be in range
next* range next* 10 1000 check Number of subsequent (or current) positions that matches the argument filter is in range
previous range previous 5 100 (check+ initial) Length of longest sequence of preceding positions starting with current position matching arguments must be in range
previous2 range previous2 5 100 (check+ initial) Length of longest sequence of every other preceding (or current) positions matching arguments, starting with current position, must be in range
previous* range previous* 10 1000 check Number of previous (or current) positions that matches the argument filter is in range

Because these filters can be counted, they can also be used as to sort the CQL output.