summaryrefslogtreecommitdiff
path: root/doc/chess.texi
blob: 6503c65d849d76c377810b621aa992a4197194c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
\input texinfo  @c -*-texinfo-*-

@setfilename chess.info
@documentencoding UTF-8
@documentlanguage en
@settitle Emacs Chess: chess.el

@dircategory Emacs
@direntry
* Chess: (chess).     Chess.el is an Emacs chess client.
@end direntry
@setchapternewpage on

@ifinfo
Copyright @copyright{} 2001, 2002, 2014 Free Software Foundation, @abbr{Inc.}

Permission is granted to copy, distribute and/or modify this document
under the terms of the @acronym{GNU} Free Documentation License, Version 1.1 or
any later version published by the Free Software Foundation.
@end ifinfo

@synindex vr fn
@c The titlepage section does not appear in the Info file.
@titlepage
@sp 4
@c The title is printed in a large font.
@center @titlefont{User's Guide}
@sp 1
@center @titlefont{to}
@sp 1
@center @titlefont{Emacs Chess: chess.el}
@ignore
@sp 2
@center release 2.0
@c -release-
@end ignore
@sp 3
@center John Wiegley and Mario Lang
@c -date-

@c  The following two commands start the copyright page for the printed
@c  manual.  This will not appear in the Info file.
@page
@vskip 0pt plus 1filll
Copyright @copyright{} 2001, 2002, 2014 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the @acronym{GNU} Free Documentation License, Version 1.1 or
any later version published by the Free Software Foundation.
@end titlepage

@contents

@c ================================================================
@c                   The real text starts here
@c ================================================================

@ifinfo
@node Top, The chess.el library, (dir), (dir)
@top Emacs Chess: chess.el

Chess.el is an Emacs chess client and library, designed to be used for
writing chess-related programs, or for playing games of chess against
various chess engines, including Internet servers.  The library can be
used for analyzing variations, browsing historical games, or a
multitude of other purposes.

The purpose of this manual is to help you understand how Chess.el is
structured for use as a library, and also how to use it as a client.
@end ifinfo

@menu
* The chess.el library::        Basic objects required to deal with Chess
* Modules::                     The module-system explained
* Chessboard displays::         Different types of chessboards in a buffer
* Engines::                     Internal and external chess-playing engines
* Chess Session::               Tying it all together
* Internet Chess Servers::      Playing chess with other people
* Concept Index::
* Function and Variable Index::
* Key Index::
@end menu

@node The chess.el library, Modules, Top, Top
@chapter The chess.el library
@cindex library

  This chapter documents the low-level aspects of chess.el, mostly targeting
developers interested in understanding the underlying @acronym{API}s@.

@ref{Chessboard displays} and following chapters
if you are interested in the more user-visible aspects of chess.el.

@menu
* Positions::
* Plies::
* Variations::
* Games::
* Collections::
* Chess Opening Books::
@end menu

@node Positions, Plies, The chess.el library, The chess.el library
@section Positions
@cindex position

A chess @dfn{position} is a given layout of pieces on a chess board,
also reflecting which side is next to move, and what privileges are
currently available to each side (castling short or long, en passant
capture, etc).

A position may be represented in @acronym{ASCII} using @acronym{FEN} (or EPD), or
graphically by displaying a chess board.  It is rather inconvenient to
render them verbally.

The position can be represented on a remote terminal using X windows, or
by transmitting the @acronym{FEN} string via a network connection, or clipboard,
to another chess board rendering tool.  It may of course also be
represented physically, by setting up the pieces to match the @acronym{FEN}
specification.

Chess puzzles are most often provided as a set of positions.

@menu
* Creating positions::
* Position coordinates::
* Position details::
* Annotations::
* FEN notation::
* EPD notation::
@end menu

@node Creating positions, Position coordinates, Positions, Positions
@subsection Creating positions

@defun chess-pos-create &optional blank
Create a new chess position, set at the starting position.
If @var{blank} is non-nil, all of the squares will be empty.
The current side-to-move is always white.
@end defun

@defun chess-pos-copy position
Copy the given chess @var{position}.
If there are annotations or @acronym{EPD} opcodes set, these lists are copied as
well, so that the two positions do not share the same lists.
@end defun

@defvar chess-starting-position
Starting position of a chess game.
@end defvar

@defun chess-fischer-random-position 
Generate a Fischer Random style position.
@end defun

@node Position coordinates, Position details, Creating positions, Positions
@subsection Position coordinates
@cindex cooridnates

First of all, a coordinate system of octal indices is
used, where ?\044 signifies rank 4 file 4 (i.e., "e4").  Rank is
numbered 0 to 7, top to bottom, and file is 0 to 7, left to right.

@defun chess-index-rank index
Return the rank component of the given @var{index}.
@end defun

@defun chess-index-file index
Return the file component of the given @var{index}.
@end defun

@defun chess-rf-to-index rank file
Convert @var{rank} and @var{file} coordinates into an octal index.
@end defun

For those who wish to use @acronym{ASCII} coordinates, such as "e4", there
are two conversion functions:

@defun chess-coord-to-index coord
Convert a @var{coord} string into an index value.
@end defun

@defun chess-index-to-coord index
Convert the chess position @var{index} into a coord string.
@end defun

For fast manipulation of chess position indices, the following
constants and functions are provided:

@cindex queen
@cindex rook
For queens and rooks:

@cindex north
@defvr Constant chess-direction-north
Signify one step north, as seen from the perspective of the white player.
@end defvr

@cindex east
@defvr Constant chess-direction-east
Signify one step east, as seen from the perspective of the white player.
@end defvr

@cindex south
@defvr Constant chess-direction-south
Signify one step south, as seen from the perspective of the white player.
@end defvr

@cindex west
@defvr Constant chess-direction-west
Signify one step west, as seen from the perspective of the white player.
@end defvr

@cindex queen
@cindex bishop
For queens and bishops:

@cindex northeast
@defvr Constant chess-direction-northeast
Signify one step northeast, as seen from the perspective of the white player.
@end defvr

@cindex southeast
@defvr Constant chess-direction-southeast
Signify one step southeast, as seen from the perspective of the white player.
@end defvr

@cindex southwest
@defvr Constant chess-direction-southwest
Signify one step southwest, as seen from the perspective of the white player.
@end defvr

@cindex northwest
@defvr Constant chess-direction-northwest
Signify one step northwest, as seen from the perspective of the white player.
@end defvr

@defun chess-next-index index direction
Create a new @var{index} from an old one, by advancing it into @var{direction}.
If the resulting index is not valid (outside the board), nil is returned.
@end defun

Due to the underlying technique used to efficiently detect off-board squares,
a direction specifier should at most do two steps in any direction.
Directions can be combined, so that @code{(* chess-direction-north 2)}
will give a typical initial white pawn push.

@node Position details, Annotations, Position coordinates, Positions
@subsection Position details

With an octal index value, you can look up what's on a particular
square, or set that square's value:

@defun chess-pos-piece position index
Return the piece on @var{position} at @var{index}.
@end defun

@defun chess-pos-piece-p position index piece-or-color
Return non-nil if at @var{position}/@var{index} there is the given @var{piece-or-color}.
If @var{piece-or-color} is t for white or nil for black, any piece of that
color will do.
@end defun

@defun chess-pos-set-piece position index piece
Set the piece on @var{position} at @var{index} to @var{piece}.
@var{piece} must be one of @code{?K} @code{?Q} @code{?N} @code{?B}
@code{?R} or @code{?P}.  Use lowercase to set black pieces.
@end defun

@defun chess-pos-search position piece-or-color
Look on @var{position} anywhere for @var{piece-or-color}, returning all coordinates.
If @var{piece-or-color} is t for white or nil for black, any piece of that
color will do.
@end defun

@defun chess-pos-search* position &rest pieces
Look on @var{position} for any of @var{pieces}.

The result is an alist where each element looks like (@var{piece} . @var{indices}).
Pieces which did not appear in @var{position} will be present in the resulting
alist, but the `cdr' of their entries will be nil.
@end defun

@defun chess-search-position position target piece &optional check-only no-castling
Look on @var{position} from @var{target} for a @var{piece} that can move there.
This routine looks along legal paths of movement for @var{piece}.  It
differs from `chess-pos-search', which is a more basic function that
doesn't take piece movement into account.

If @var{piece} is t or nil, legal piece movements for any piece of that
color will be considered (t for white, nil for black).  Otherwise, the
case of the @var{piece} determines color.

The return value is a list of candidates, which means a list of
indices which indicate where a piece may have moved from.

If @var{check-only} is non-nil and @var{piece} is either t or nil, only consider
pieces which can give check (not the opponents king).
If @var{no-castling} is non-nil, do not consider castling moves.
@end defun

@c lispfun chess-pos-can-castle

@defun chess-pos-can-castle position side
Return whether the king on @var{position} can castle on @var{side}.
@var{side} must be either ?K for the king side, or ?Q for the queen side (use
lowercase to query if black can castle).
@end defun

@c lispfun chess-pos-set-can-castle

@defun chess-pos-set-can-castle position side value
Set whether the king can castle on the given @var{position} on @var{side}.

See `chess-pos-can-castle'.

It is only necessary to call this function if setting up a position
manually.  Note that all newly created positions have full castling
privileges set, unless the position is created blank, in which case
castling privileges are unset.  See `chess-pos-copy'.
@end defun

@c lispfun chess-pos-en-passant

@defun chess-pos-en-passant position
Return the index of any pawn on @var{position} that can be captured en passant.
Returns nil if en passant is unavailable.
@end defun

@c lispfun chess-pos-set-en-passant

@defun chess-pos-set-en-passant position index
Set the index of any pawn on @var{position} that can be captured en passant.
@end defun

@c lispfun chess-pos-status

@defun chess-pos-status position
Return whether the side to move in the @var{position} is in a special state.
nil is returned if not, otherwise one of the symbols: `check',
`checkmate', `stalemate'.
@end defun

@c lispfun chess-pos-set-status

@defun chess-pos-set-status position value
Set whether the side to move in @var{position} is in a special state.
@var{value} should either be nil, to indicate that the @var{position} is normal,
or one of the symbols: `check', `checkmate', `stalemate'.
@end defun

@c lispfun chess-pos-side-to-move

@defun chess-pos-side-to-move position
Return the color whose move it is in @var{position}.
@end defun

@c lispfun chess-pos-set-side-to-move

@defun chess-pos-set-side-to-move position color
Set the color whose move it is in @var{position}.
@end defun

@c lispfun chess-pos-passed-pawns

@defun chess-pos-passed-pawns position color &optional pawn-indices
If @var{color} has Passed Pawns in @var{position}, return a list of their indices.
Optionally, if @var{indices} is non-nil those indices are considered as candidates.

A Pawn whose advance to the eighth rank is not blocked by an
opposing Pawn in the same file and who does not have to pass one
on an adjoining file is called a passed Pawn.
@end defun

@defvar chess-pos-always-white
When set, it is assumed that white is always on move.
This is really only useful when setting up training positions.
This variable automatically becomes buffer-local when changed.
@end defvar

@c lispfun chess-pos-move

@defun chess-pos-move position &rest changes
Move a piece on the @var{position} directly, using the indices in @var{changes}.
This function does not check any rules, it only makes sure you are not
trying to move a blank square.
@end defun

@node Annotations, FEN notation, Position details, Positions
@subsection Annotations

@c lispfun chess-pos-annotations

@defun chess-pos-annotations position
Return the list of annotations for this position.
@end defun

@c lispfun chess-pos-add-annotation

@defun chess-pos-add-annotation position annotation
Add an annotation for this position.
@end defun

@node FEN notation, EPD notation, Annotations, Positions
@subsection FEN notation
@cindex fen

@dfn{@acronym{FEN, Forsyth-Edwards Notation}} encodes a chess position using
a simple string.  The format is:

   @var{position} @var{side} @var{castling} @var{en-passant}

The @var{position} gives all eight ranks, by specifying a letter for each
piece on the position, and a number for any intervening spaces, ranks
separated by slashes.
Trailing spaces need not be counted.  Uppercase letters signify
white, and lowercase black.  For example, if your position only had
a black king on d8, your @var{position} string would be:

@example
3k////////
@end example

For the three spaces (a, b and c file), the black king, and then
all the remaining ranks (which are all empty, so their spaces can
be ignored).

The @var{side} is @samp{w} or @samp{b}, to indicate whose move it is.

@var{castling} can contain @samp{K}, @samp{Q}, @samp{k} or @samp{q}, to signify
whether the white or black king can still castle on the king or queen side.
If neither colour can castle on any side, @samp{-} should be provided.

@var{en-passant} signifies the target square of an en passant capture, such as
@samp{e3} or @samp{a6}.

@defun chess-fen-to-pos fen
Convert a @var{fen}-like string to a chess position.
@end defun

@defun chess-pos-to-fen position &optional full
Convert a chess @var{position} to a @acronym{FEN} string.
If @var{full} is non-nil, represent trailing spaces as well.
@end defun

This is how the starting position looks like:

@vindex chess-starting-position
@lisp
(chess-pos-to-fen chess-starting-position)
@result{} "rnbqkbnr/pppppppp/////PPPPPPPP/RNBQKBNR w KQkq -"
@end lisp

Some external programs might have problems parsing terse
FEN strings.  If you are unsure, use the more verbose form:

@lisp
(chess-pos-to-fen chess-starting-position t)
@result{} "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -"
@end lisp

@node EPD notation,  , FEN notation, Positions
@subsection EPD notation

@dfn{@acronym{EPD, Extended Position Description}} is a standard for describing chess
positions along with an extended set of structured attribute values using the
@acronym{ASCII} character set.  It is intended for data and command interchange
among chess-playing programs.  It is also intended for the representation of
portable opening library repositories.

A single EPD uses one text line of variable length composed of four data field
followed by zero or more operations.  The four fields of the @acronym{EPD} specification
are the same as the first four fields of the @acronym{FEN} specification.

A text file composed exclusively of @acronym{EPD} data records should have a file name
with the suffix @file{.epd}.

@defun chess-epd-to-pos &optional string
Convert extended position description @var{string} to a chess position.
If @var{string} is not specified, look for an @acronym{epd} string in the current buffer,
and advance point after the correctly parsed position.
@end defun

@defun chess-pos-to-epd position
Convert a chess @var{position} to a string representation in extended
position description format.
@end defun

@defun chess-epd-read-file file
Return a list of positions contained in @var{file}.
@end defun

@menu
* Operations::
* Opcode "acd" analysis count depth::
* Opcode "acn" analysis count nodes::
* Opcode "acs" analysis count seconds::
* Opcode "am" avoid move(s)::
* Opcode "bm" best move(s)::
@end menu

@node Operations, Opcode "acd" analysis count depth, EPD notation, EPD notation
@subsubsection Operations

An @acronym{EPD} operation is composed of an opcode followed by zero or
more operands and is concluded by a semicolon.

Multiple operations are separated by a single space character.  If there is at
least one operation present in an @acronym{EPD} record, it is separated from the last
(fourth) data field by a single space character.

Some opcodes that allow for more than one operand may have special ordering
requirements for the operands.  For example, the "pv" (predicted variation)
opcode requires its operands (moves) to appear in the order in which they would
be played.  All other opcodes that allow for more than one operand should have
operands appearing in @acronym{ASCII} order.  An example of the latter set is the "bm"
(best move[s]) opcode; its operands are moves that are all immediately playable
from the current position.

@node Opcode "acd" analysis count depth, Opcode "acn" analysis count nodes, Operations, EPD notation
@subsubsection Opcode "acd" analysis count depth

The opcode "acd" takes a single non-negative integer operand.  It is used to
represent the ply depth examined in an analysis.

@node Opcode "acn" analysis count nodes, Opcode "acs" analysis count seconds, Opcode "acd" analysis count depth, EPD notation
@subsubsection Opcode "acn" analysis count nodes

The opcode "acn" takes a single non-negative integer operand.  It is used to
represent the number of nodes examined in an analysis.  Note that the value may
be quite large for some extended searches and so use of (at least) a long (four
byte) representation is suggested.

@node Opcode "acs" analysis count seconds, Opcode "am" avoid move(s), Opcode "acn" analysis count nodes, EPD notation
@subsubsection Opcode "acs" analysis count seconds

The opcode "acs" takes a single non-negative integer operand.  It is used to
represent the number of seconds used for an analysis.  Note that the value may
be quite large for some extended searches and so use of (at least) a long (four
byte) representation is suggested.

@node Opcode "am" avoid move(s), Opcode "bm" best move(s), Opcode "acs" analysis count seconds, EPD notation
@subsubsection Opcode "am" avoid move(s)

The opcode "am" indicates a set of zero or more moves, all immediately playable
from the current position, that are to be avoided in the opinion of the EPD
writer.  Each operand is a @acronym{SAN} move; they appear in @acronym{ASCII} order.

@node Opcode "bm" best move(s),  , Opcode "am" avoid move(s), EPD notation
@subsubsection Opcode "bm" best move(s)

The opcode "bm" indicates a set of zero or more moves, all immediately playable
from the current position, that are judged to the best available by the EPD
writer.  Each operand is a @acronym{SAN} move; they appear in @acronym{ASCII} order.

@node Plies, Variations, Positions, The chess.el library
@section Plies

A @dfn{ply} is the differential between two positions.  Or, it is the
coordinate transformations applied to one position in order to arrive at
the following position.  It is also informally called "a move".

A ply may be represented in @acronym{ASCII} by printing the @acronym{FEN} string
of the base position, and then printing the positional transformation in algebraic
notation.  Since the starting position is usually known, the @acronym{FEN} string
is optional.  A ply may be represented graphically by moving the chess
piece(s) involved.  It may be rendered verbally by voicing which piece
is to move, where it will move to, and what will happen a result of the
move (piece capture, check, etc).

Plies may be sent over network connections, postal mail, e-mail, @abbr{etc.},
so long as the current position is maintained at both sides.
Transmitting the base position's @acronym{FEN} string along with the ply offers a
form of confirmation during the course of a game.

@menu
* Creating plies::
* Ply details::
* The "next" position::
* Algebraic notation::
@end menu

@node Creating plies, Ply details, Plies, Plies
@subsection Creating plies

@defun chess-ply-create position &optional valid-p &rest changes
Create a ply from the given @var{position} by applying the supplied @var{changes}.
This function will guarantee the resulting ply is legal, and will also
annotate the ply with :check or other modifiers as necessary.  It will
also extend castling, and will prompt for a promotion piece.

Note: Do not pass in the rook move if @var{changes} represents a castling
maneuver.
@end defun

@defun chess-legal-plies position &rest keywords
Return a list of all legal plies in @var{position}.
@var{keywords} allowed are:

  :any   return t if any piece can move at all
  :color <t or nil>
  :piece <piece character>
  :file <number 0 to 7> [can only be used if :piece is present]
  :index <coordinate index>
  :target <specific target index>
  :candidates <list of indices>

These will constrain the plies generated to those matching the above
criteria.

NOTE: All of the returned plies will reference the same copy of the
position object passed in.
@end defun

@node Ply details, The "next" position, Creating plies, Plies
@subsection Ply details

@defun chess-ply-pos ply
Returns the base position associated with @var{ply}.
@end defun

@defun chess-ply-set-pos ply position
Set the base position of @var{ply}.
@end defun

@defun chess-ply-changes
Return the coordinate transformations and keywords associated with this @var{ply}.

A list of a pair of indices (or two, in case of castling) followed by
optional keywords.
@end defun

@defun chess-ply-set-changes
Set the coordinate transformations and keywords associated with this @var{ply}.
@end defun

@defun chess-ply-source ply
Return the source square index value of @var{ply}.
@end defun

@defun chess-ply-target ply
Return the target square index value of @var{ply}.
@end defun

For example, here is how to find the source square of a freshly created ply:

@lisp
(chess-ply-source (chess-ply-create chess-starting-position nil
                                    (chess-coord-to-index "e2")
                                    (chess-coord-to-index "e4")))
@result{} 52
@end lisp

@node The "next" position, Algebraic notation, Ply details, Plies
@subsection The "next" position

@defun chess-ply-next-pos ply
Return the position that results from executing @var{ply}.
@end defun

@defun chess-ply-final-p ply
Return non-nil if this is the last ply of a game/variation.
@end defun

@node Algebraic notation,  , The "next" position, Plies
@subsection Algebraic notation

A thing to deal with in chess is algebraic move notation, such as
@samp{Nxf3+}.  This notation is a shorthand way of representing where
a piece is moving from and to, by specifying the piece involved,
where it's going, and whether or not a capture or check is
involved.

You can convert from algebraic notation to a ply using the following function:

@defun chess-algebraic-to-ply position move &optional trust
Convert the algebraic notation @var{move} for @var{position} to a ply.
@end defun

The function also checks if a move is legal, and will raise an
error if not.

To convert from a ply to algebraic notation, use:

@defun chess-ply-to-algebraic ply &optional type
Convert the given @var{ply} to algebraic notation (a string).

Optional argument @var{type} specifies the kind of algebraic notation to
generate.
@code{:san} (the default) generates short (or standard) algebraic notation.
@code{:lan} generates long algebraic notation (like @samp{Nb1-c3}).
@code{:fan} generates figurine algebraic notation (uppercase letters will
be replaced by Unicode chess figures).
@end defun

Lastly, there is a regexp for quickly checking if a string is in
algebraic notation or not, or searching out algebraic strings in a
buffer:

@defvar chess-algebraic-regexp
A regular expression that matches all possible algebraic moves.
This regexp handles both long and short form.
@end defvar

@node Variations, Games, Plies, The chess.el library
@section Variations

A @dfn{variation} is a sequence of plies that occur after some starting
position.  If the starting position represents the initial setup of a
chess board, and if the final ply results in completion of the game, it
is called the "main variation".  Otherwise, variations typically
represented interesting tangents during a game---but not actually
played---as envisioned by the player, an annotator, or someone studying
the game.

Variations may be represented in @acronym{ASCII} by stating the @acronym{FEN}
string for starting position, followed by the list of plies that follow that
position.  They are difficult to represent graphically, except for
showing each position in turn with a slight pause between---or by
allowing the user to navigate each of the subsequent positions in turn.
They may be represented verbally by announcing each of the plies in
turn, as mentioned above.

@menu
* Creating variations::
* Variation positions::
* Variation plies::
* Making a move in a variation::
@end menu

@node Creating variations, Variation positions, Variations, Variations
@subsection Creating variations

@c lispfun chess-var-create

@defun chess-var-create &optional position
Create a new chess variation object.
Optionally use the given starting @var{position}.
@end defun

@node Variation positions, Variation plies, Creating variations, Variations
@subsection Variation positions

@c lispfun chess-var-pos

@defun chess-var-pos var &optional index
Return the position related to @var{var}'s @var{index} ply.
@end defun

@c lispfun chess-var-index

@defun chess-var-index var
Return the @var{var}'s current position index.
@end defun

@c lispfun chess-var-seq

@defun chess-var-seq var
Return the current @var{var} sequence.
@end defun

@c lispfun chess-var-side-to-move

@defun chess-var-side-to-move var &optional index
Return the color whose move it is in @var{var} at @var{index} (or at the last position
of the variation if @var{index} is nil).
@end defun

@node Variation plies, Making a move in a variation, Variation positions, Variations
@subsection Variation plies

@c lispfun chess-var-ply

@defun chess-var-ply var &optional index
Return @var{var}'s @var{index}th ply.
@end defun

@c lispfun chess-var-plies

@defun chess-var-plies var
Return the plies of @var{var}.
@end defun

@c lispfun chess-var-to-algebraic

@defun chess-var-to-algebraic var &optional long
Reveal the plies of @var{var} by converting them to algebraic
notation.
@end defun

@node Making a move in a variation,  , Variation plies, Variations
@subsection Making a move in a variation

@c lispfun chess-var-move

@defun chess-var-move var ply
Make a move in the current @var{var} by applying the changes of @var{ply}.
This creates a new position and adds it to the main variation.
The 'changes' of the last ply reflect whether the var is currently in
progress (nil), if it is drawn, resigned, mate, etc.
@end defun

@defun chess-var-add-ply var ply
Add to @var{var} the given @var{ply}.
@end defun

@node Games, Collections, Variations, The chess.el library
@section Games

A @dfn{game} includes its main variation, incidental information about
the game (who played it, where, when, who won, etc), and any
sub-variations of interest to those studying the game afterwards.

Where TAGS is an alist that associates arbitrary English tag names to
their values.

A game may be represented in @acronym{ASCII} using @acronym{PGN,
Portable Game Notation}.
Representing them graphically or verbally is similar to what is done
for variations.

@defun chess-game-add-hook game function &optional data prepend
Add to @var{game} an event hook @var{function}.
@end defun

@defun chess-game-add-ply game ply
Add @var{ply} to the main variation of @var{game}.
@end defun

@defun chess-game-hooks game
Return the event hooks associated with @var{game}.
@end defun

@defun chess-game-plies game
Return the main variation of @var{game} as a list of plies.
@end defun

@defun chess-game-remove-hook game function &optional data
Remove from @var{game} all event hooks that match @var{function}.
If @var{data} is specified, only remove those hooks whose associated data
matches.
@end defun

@defun chess-game-run-hooks game &rest args
Run the event hooks of @var{game} and pass @var{args}.
@end defun

@defun chess-game-set-hooks game hooks
Set the event hooks associated with @var{game}.
@end defun

@defun chess-game-set-plies game plies
Set the list of plies which represents the main variation of @var{game}.
@end defun

@menu
* Creating games::
* Game tags::
* Game positions::
* Game plies::
* Making a move::
* PGN notation::
@end menu

@node Creating games, Game tags, Games, Games
@subsection Creating games

@c lispfun chess-game-create

@defun chess-game-create &optional position tags
Create a new chess game object.
Optionally use the given starting @var{position} (see also
`chess-game-set-start-position').
@var{tags} is the starting set of game tags (which can always be changed
later using the various tag-related methods).
@end defun

@node Game tags, Game positions, Creating games, Games
@subsection Game tags

@c lispfun chess-game-tags

@defun chess-game-tags game
Return the tags alist associated with @var{game}.
@end defun

@c lispfun chess-game-set-tags

@defun chess-game-set-tags game tags
Set the tags alist associated with @var{game}.
After the @var{tags} alist was set the 'set-tags event is triggered.
@end defun

@c lispfun chess-game-tag

@defun chess-game-tag game tag
Return the value for @var{tag} in @var{game}.
@end defun

@c lispfun chess-game-set-tag

@defun chess-game-set-tag game tag value
Set a @var{tag} for @var{game} to @var{value}.
@end defun

@c lispfun chess-game-del-tag

@defun chess-game-del-tag game tag
Delete a @var{tag} from @var{game}.
@end defun

@node Game positions, Game plies, Game tags, Games
@subsection Game positions

@c lispfun chess-game-pos

@defun chess-game-pos game &optional index
Return the current position of @var{game} or a position of a given @var{index}.
@end defun

@c lispfun chess-game-index

@defun chess-game-index game
Return the @var{game}'s current position index.
@end defun

@c lispfun chess-game-seq

@defun chess-game-seq game
Return the current @var{game} sequence number.
@end defun

@c lispfun chess-game-side-to-move

@defun chess-game-side-to-move game &optional index
Return the color whose move it is in @var{game} at @var{index} (or at the last position
if @var{index} is nil).
@code{t} for white and @code{nil} for black.
@end defun

@node Game plies, Making a move, Game positions, Games
@subsection Game plies

@c lispfun chess-game-ply

@defun chess-game-ply game &optional index
Return a ply of @var{game}.
If @var{index} is non-nil, the last played ply is returned.
@end defun

@node Making a move, PGN notation, Game plies, Games
@subsection Making a move

@c lispfun chess-game-move

@defun chess-game-move game ply
Make a move in the current @var{game} using @var{ply}.
This creates a new position and adds it to the main variation.
The 'changes' of the last ply reflect whether the game is currently in
progress (nil), if it is drawn, resigned, mate, etc.
@end defun

@node PGN notation,  , Making a move, Games
@subsection PGN notation

@c lispfun chess-pgn-to-game

@defun chess-pgn-to-game &optional string
Convert @dfn{PGN notation} at point into a chess game.
Optionally use the supplied @var{string} instead of the current buffer.
@end defun

@c lispfun chess-game-to-pgn

@defun chess-game-to-pgn game &optional indented to-string
Convert a chess @var{game} to @dfn{PGN notation}.
If @var{indented} is non-nil, indent the move texts.
If @var{to-string} is non-nil, return a string instead of inserting the resulting
@var{pgn} text.
@end defun

@c lispfun chess-pgn-insert-plies

@defun chess-pgn-insert-plies game index plies &optional for-black indented no-annotations
@var{nyi}: Still have to implement @var{indented} argument.
@end defun

@menu
* PGN mode::
@end menu

@node PGN mode,  , PGN notation, PGN notation
@subsubsection PGN mode

@c lispfun chess-pgn-visualize

@defun chess-pgn-visualize 
Visualize the move for the @var{pgn} game under point.
This does not require that the buffer be in @var{pgn} mode.
@end defun

@node Collections, Chess Opening Books, Games, The chess.el library
@section Collections

A @dfn{collection} is a set of games archived for later perusal.  A set
of games conceptually represents a large tree of branching variations,
and can be used for studying current theory, examining Master
preferences, etc.

Chess.el itself does not attempt to provide library services, nor does it
ever represent library collections in memory.  Instead, it interacts
with a chess database engine for the purpose of storing and retrieving
games from the library, or performing library-wide analyses and
searches.

@menu
* Opening Databases::
* Querying Databases::
* Modifying Databases::
* Finalising Databases::
* Database Modules::
@end menu

@node Opening Databases, Querying Databases, Collections, Collections
@subsection Opening Databases

@defvar chess-database-modules
List of database modules to try when `chess-database-open' is called.
@end defvar

@c lispfun chess-database-open

@defun chess-database-open file &optional module
Returns the opened database object, or nil.
@end defun

@node Querying Databases, Modifying Databases, Opening Databases, Collections
@subsection Querying Databases

@c lispfun chess-database-filename

@defun chess-database-filename database
Return the filename of an already opened @var{database}.
@end defun

@c lispfun chess-database-count

@c lispfun chess-database-read

@defun chess-database-read database index
Return from @var{database} the chess game object at @var{index}.
@end defun

@c lispfun chess-database-query

@defun chess-database-query database &rest terms
Run a query on @var{database}.
@var{terms} is partly dependent on the chess-database module in use.
chess-scid:
 tree-search @var{game}: Perform a tree search on the last position of @var{game}.
@end defun

@node Modifying Databases, Finalising Databases, Querying Databases, Collections
@subsection Modifying Databases

@c lispfun chess-database-read-only-p

@defun chess-database-read-only-p database
Return non-nil if @var{database} is read only.
@end defun

@c lispfun chess-database-write

@c lispfun chess-database-replace

@node Finalising Databases, Database Modules, Modifying Databases, Collections
@subsection Finalising Databases

@c lispfun chess-database-save

@c lispfun chess-database-close

@node Database Modules,  , Finalising Databases, Collections
@subsection Database Modules

Currently, there are two subclasses of the above defined
database base-class:

@menu
* chess-file::
* chess-scid::
@end menu

@node chess-file, chess-scid, Database Modules, Database Modules
@subsubsection chess-file

This module does not use an external chess database program
to store and retrieve games.  It uses the PGN of EPD format parsing
routines provided in `chess-pgn.el' and `chess-epd.el' to implement Collections
for ordinary PGN and EPD files.

EPD file collections are represented as a collection of games originating
at the given position.  One might argue that conceptually, they represent
a collection of positions, but it is more convenient to merge all
collections into one uniform concept.

@node chess-scid,  , chess-file, Database Modules
@subsubsection chess-scid

This modules implement basic reading and writing functionality
for SCID (Shane's Chess Information Database) files.

@node Chess Opening Books,  , Collections, The chess.el library
@section Chess Opening Books

There are two different modules/libraries provided for looking up
chess positions in opening books.

@menu
* ECO Classification::
* Polyglot opening book format support::
@end menu

@node ECO Classification, Polyglot opening book format support, Chess Opening Books, Chess Opening Books
@subsection ECO Classification

@vindex chess-default-modules
Module @code{chess-eco} provides a database of well known names
for chess opening positions.  If this module is activated (see variable
@code{chess-default-modules}) known chess opening positions will be announced
in the minibuffer during a game.

@node Polyglot opening book format support,  , ECO Classification, Chess Opening Books
@subsection Polyglot opening book format support

The popular and freely documented Polyglot opening book format
is supported.  There is a default polyglot book file shipped with chess.el
to support engines which do not have built-in support for looking up positions
in opening books (such as some UCI protocol based engines).

@defvar chess-polyglot-book-file
Path to default polyglot book file.
@end defvar

@defvar chess-polyglot-book
If non-nil, the buffer holding the currently loaded polyglot book data.

This is used by UCI based engine modules as well as the internal AI.
@end defvar

@defun chess-polyglot-book-open file
Open a polyglot book @var{file}.

Returns a buffer object which contains the binary data.
@end defun

@defun chess-polyglot-book-plies book position
Return a list of plies found in @var{book} for @var{position}.
The resulting list is ordered, most interesting plies come first.
The @code{:polyglot-book-weight} ply keyword is used to store the actual move weights.
Use `chess-ply-keyword' on elements of the returned list to retrieve them.
@end defun

@defun chess-polyglot-book-ply book position &optional strength
If non-nil a (randomly picked) ply from @var{book} for @var{position}.
Random distribution is defined by the relative weights of the found plies.
If non-nil, @var{strength} defines the bias towards better moves.
A value below 1.0 will penalize known good moves while a value
above 1.0 will prefer known good moves.  The default is the value
of `chess-polyglot-book-strength'.
A strength value of 0.0 will completely ignore move weights and evenly
distribute the probability that a move gets picked.
@end defun

@node Modules, Chessboard displays, The chess.el library, Top
@chapter Modules

Positions, plies and variations are typically accessed in reference to
a game object, which has a main variation containing the plies and
positions that represent the number of moves made within that game up
to the final position.

Another thing that the game object does is to manage events that occur
within that game.  If a move is made from the final position, for
example, it will cause a new ply to be created, adding it to the end
of the main variation.  Then, a `move' event is triggered within the
game and passed to any chess modules which are currently associated
with that game.  The concept of modules allows far more complex
aspects of chess playing to be dealt with, while allowing the library
itself to still operate solely in terms of the game object.

For example, although the plies of a game object contain all the
information the computer needs to follow the game, a user needs much
more.  He wants to see the pieces move.  To support this, a display
module (see next chapter) can be created, and linked to the game.  The
first effect of this association will be to create a chess board
display and show the game's final position on it.  Now whenever plies
are added to the game, the chess board will be updated to show the
effect of that move on the board.  The display module realizes that a
move has been made by receiving the `move' event which is passed to
all modules associated with the game object.

There may be any number of modules associated with a chess game, and
they may do anything you like.  Basically, for a module called
chess-sample, a function must exist called `chess-sample-handler'.
This takes two or more arguments: a game object, the event symbol, and
whatever other arguments were passed along with the event symbol.

When an event is triggered on a game object (and this may happen as a
byproduct of manipulating the game, or events may be manually
generated), every associated module, in order, is called with that
event and whatever arguments were passed along with the event.  The
game object is passed also, so that the module knows which game this
event has occurred in reference to.

Once called, the module can do whatever it likes.  Some events expect
certain values to be returned, to indicate success or failure in
processing the event.  There are many different events, each depicting
something specific that might happen in the context of playing or
manipulating a chess game.  Some events relate only to the chess game
itself, some are triggered by the various chess engines that might be
associated with that game.  Modules may even trigger events in
response to event.  The game itself remains unaware of events, except
for the fact that it will pass them along to every module associated
with that game.

This is how displays get updated, for example, because once a 'move'
event is triggered, each display knows that it must now look at the
new final position and update its display.  It may even trigger new
events special to displays, to cause a refresh to happen after update
calculations have been performed, for example.  All such details are
left to the module, and the game does not interfere with such
intra-module messaging.

Looked at as an object-oriented design, these are typical polymorphic
events.  Certain generic situations frequently occur, such as moves,
which trigger events so that everyone concerned with the game can be
updated as to the move that occurred.  This way, no one need to
actively query the game to find out if something new has happened.
The game will notify every listening module by sending an event.

The core library, which consists of code to manipulate games, does not
define any modules.  The rest of the chess.el library is strictly a
set of module implementations, of various types.  Display modules
react to moves, and may modify the game based on user input; engine
modules react to moves by notifying the engine of the move; network
client modules react to moves by sending the move text over the
network.  Engine and network modules may also trigger new events when
the engine or network player has decided on their move, and this move
is then applied to the game object.

At the moment, no negotiation is done to determine which module may
modify the game object.  All modules have equal privilege.  This means
it is the programmer's duty not to associate conflicting modules with
a single game object.  If two artificial intelligence engines were
linked, for example, they would quickly start stepping on each other's
toes.  But it perfectly fine to have one artificial intelligence
engine, and another passive engine whose only purpose is to relay the
moves to a networked observer on another computer.  The possibilities
are endless.

Modules are very easy to write, although engines and displays are
rather different from each other in their principles.  There is a base
engine, and a base display, which receive the same events as any other
module.  But then there are derived engines and derived displays which
trigger a whole family of events specific to those module types.  If
you suspect a bug in your module, put a breakpoint in your handler
function, and wait for the offending event to come through.  Then you
can watch what your module does in response to that event.  If it
leaves the game object alone, it should be easy to locate the problem,
since it will always be within the module itself.  But if your module
also modifies the game object in response to certain events, you may
induce a feedback loop that is much more difficult to sort out.  Test
often and keep in mind that *many* events might end up coming through
as a result of the game changes your module makes!

That, in essence, is how the module system works.  From the game
object's perspective, it is a very simple mechanism, much like a
function ring or a hook.  The hook is called at certain points, so
that any listener can react to changes in the game.  But from each
module's perspective, it is a rich way to allow inter-operation
between both passive and reactive modules, all of them acting together
to enrich the context of play involving the central game object.

The only other rule to be mentioned is that each module instance
should be associated with only one game object at a time, although a
game object may have unlimited modules of any type linked to it.
Otherwise, trying to update a chess board based on input from two
different games would get impossible to sort out.  Better to create a
new board for every game---the way ordinary humans would do it in the
real world.

@node Chessboard displays, Engines, Modules, Top
@chapter Chessboard displays

The previous chapter described all the objects found in
chess---positions, plies, variations, games and collections.  However,
these objects can only be manipulated programmatically using the
functions given so far.  In order to present them in a meaningful
fashion to a human reader, it is necessary to create and use a display
object.

@menu
* Generic display manipulation functions::
* Chess display mode::
* Plain ASCII diagram displays::
* ICS1 style ASCII displays::
* Graphical displays::
@end menu

@node Generic display manipulation functions, Chess display mode, Chessboard displays, Chessboard displays
@section Generic display manipulation functions

@c lispfun chess-display-create

@defun chess-display-create game style perspective
Create a chess display, for displaying chess objects.
Where @var{game} is the chess game object to use, @var{style} should be the display
type to use (a symbol) and @var{perspective} determines the viewpoint
of the board, if non-nil, the board is viewed from White's perspective.
@end defun

@c lispfun chess-display-destroy

@c lispfun chess-display-active-p

@defun chess-display-active-p 
Return non-nil if the displayed chessboard reflects an active game.
Basically, it means we are playing, not editing or reviewing.
@end defun

@c lispfun chess-display-clear-board

@defun chess-display-clear-board 
Setup the current board for editing.
@end defun

@c lispfun chess-display-game

@c lispfun chess-display-highlight

@defun chess-display-highlight display &rest args
Highlight the square at @var{index} on the current position.
The given highlighting @var{mode} is used, or the default if the style you
are displaying with doesn't support that mode.  `selected' is a mode
that is supported by most displays, and is the default mode.
@end defun

@c lispfun chess-display-index

@c lispfun chess-display-invert

@defun chess-display-invert 
Invert the perspective of the current chess board.
@end defun

@c lispfun chess-display-move

@defun chess-display-move display ply
Move a piece on @var{display}, by applying the given @var{ply}.
The position of @var{ply} must match the currently displayed position.
@end defun

@c lispfun chess-display-move-backward

@c lispfun chess-display-move-first

@c lispfun chess-display-move-forward

@c lispfun chess-display-move-last

@c lispfun chess-display-perspective

@defun chess-display-perspective display
Return the current perspective of @var{display}.
@end defun

@c lispfun chess-display-ply

@c lispfun chess-display-position

@defun chess-display-position display
Return the position currently viewed on @var{display}.
@end defun

@c lispfun chess-display-quit

@defun chess-display-quit 
Quit the game associated with the current display.
@end defun

@c lispfun chess-display-set-game

@defun chess-display-set-game display game &optional index
Set the given @var{display} to display the @var{game} object, optionally at @var{index}.
This is the function to call to cause a display to view a game.  It
will also update all of the listening engines and other displays to
also view the same game.
@end defun

@c lispfun chess-display-set-index

@c lispfun chess-display-set-perspective

@defun chess-display-set-perspective display perspective
Set @var{perspective} of @var{display}.
@end defun

@c lispfun chess-display-set-ply

@c lispfun chess-display-set-position

@defun chess-display-set-position display &optional position my-color
Set the game associated with @var{display} to use @var{position} and @var{my-color}.
@end defun

@c lispfun chess-display-set-variation

@defun chess-display-set-variation display variation &optional index
Set @var{display} @var{variation}.
If @var{index} is not specified, this will cause the first ply in the variation
to be displayed, with the user able to scroll back and forth through the
moves in the variation.  Any moves made on the board will extend/change the
variation that was passed in.
@end defun

@c lispfun chess-display-update

@defun chess-display-update display &optional popup
Update the chessboard @var{display}.  @var{popup} too, if that arg is non-nil.
@end defun

@c lispfun chess-display-variation

@node Chess display mode, Plain ASCII diagram displays, Generic display manipulation functions, Chessboard displays
@section Chess display mode

Chess display mode is a special major mode (@pxref{Major Modes, , , emacs})
that allows to select pieces to move with the mouse or by moving
point to the desired square/piece.  Additionally, you can enter
moves in a variant of algebraic notation via the keyboard.

All the chessboard displays described in following sections
share the basic behaviour provided by chess display mode.
They basically only differ in appearance of the various chessboards.

@menu
* Basic operations::
* Selecting pieces with the keyboard::
* Selecting pieces with the mouse::
* Entering moves with algebraic notation::
@end menu

@node Basic operations, Selecting pieces with the keyboard, Chess display mode, Chess display mode
@subsection Basic operations

@table @kbd
@kindex C-i
@kindex TAB
@item C-i
@itemx @key{TAB}
@findex chess-display-invert
Invert the perspective of the current chess board.

@kindex ,
@item ,
@findex chess-display-move-backward
Show the previous move in the current game.

@cindex algebraic notation, searching moves
@kindex C-r
@item C-r
@findex chess-display-search-backward
Find previous move which algebraic notation matches a regular expression
@code{chess-display-search-backward}.

@kindex C-s
@item C-s
@findex chess-display-search-forward
Find next move which algebraic notation matches a regular expression
@code{chess-display-search-forward}.

@kindex .
@item .
@findex chess-display-move-forward
Show the next move in the current game.

@kindex <
@item <
@findex chess-display-move-first
Move to the initial position of the current game (@code{chess-display-move-first}).

@kindex >
@item >
@findex chess-display-move-last
Move to the last position of the current game (@code{chess-display-move-last}).

@kindex C-c C-d
@item C-c C-d
@findex chess-display-draw
Offer to draw the current game (@code{chess-display-draw}).

@kindex C-c C-r
@item C-c C-r
@findex chess-display-resign
Resign the current game (@code{chess-display-resign}).

@kindex M-w
@item M-w
@findex chess-display-kill-board
Copy the currently displayed position to the kill ring as a FEN string
(@code{chess-display-kill-board}).

@kindex C-y
@item C-y
@findex chess-display-yank-board
Set the current display position via a FEN string from the kill ring
(@code{chess-display-yank-board}).

This is useful to copy positions from one chessboard
display to another, as well as quickly setting up a position
from a FEN string previously added to the kill ring from
somewhere else.

@kindex X
@item X
@findex chess-display-quit
Quit this chessboard display (@code{chess-display-quit}).

This destroys the session (and all related modules) associated with this
chessboard display.
@end table

@node Selecting pieces with the keyboard, Selecting pieces with the mouse, Basic operations, Chess display mode
@subsection Selecting pieces with the keyboard

In character based chessboard displays, point can be moved around
in the buffer as uaual.  You can indicate the initial square/piece
and the target square/piece by moving point to the desired position
and pressing @kbd{@key{RET}}.

@table @kbd
@kindex RET
@item @key{RET}
@findex chess-display-select-piece
Select the piece/square currently indicated by point
(@code{chess-display-select-piece}) to move from/to.
@end table

@node Selecting pieces with the mouse, Entering moves with algebraic notation, Selecting pieces with the keyboard, Chess display mode
@subsection Selecting pieces with the mouse

Similarily, you can also use the mouse (if available) to indicate
the source and target square of your move.

@table @kbd
@kindex down-mouse-1
@kindex down-mouse-2
@kindex drag-mouse-1
@kindex drag-mouse-2
@item down-mouse-1
@itemx down-mouse-2
@itemx drag-mouse-1
@itemx drag-mouse-2
@findex chess-display-select-piece
Select the piece/square currently indicated by the mouse pointer
(@code{chess-display-select-piece}) to move from/to.
@end table

@node Entering moves with algebraic notation,  , Selecting pieces with the mouse, Chess display mode
@subsection Entering moves with algebraic notation
@cindex algebraic notation, entering moves with

@table @asis
@kindex a
@kindex b
@kindex c
@kindex d
@kindex e
@kindex f
@kindex g
@kindex h
@kindex 1
@kindex 2
@kindex 3
@kindex 4
@kindex 5
@kindex 6
@kindex 7
@kindex 8
@kindex N
@kindex B
@kindex R
@kindex Q
@kindex K
@kindex x
@kindex =
@item @kbd{a} @dots{} @kbd{h}
@itemx @kbd{1} @dots{} @kbd{8}
@itemx @kbd{N}
@itemx @kbd{B}
@itemx @kbd{R}
@itemx @kbd{Q}
@itemx @kbd{K}
@itemx @kbd{x}
@itemx @kbd{=}
@findex chess-input-shortcut
Enter move in algebraic notation.

The move will be accepted as soon as it is unambiguous.
So in most situations, you do not need to type the complete
algebraic move string.  For instance, if there is only one piece
which can be taken by one of your knights, typing @kbd{N x} is
sufficient to select that move.

Additionally, the characters @kbd{x} and @kbd{=} are optional,
as there is no difference between @kbd{N x e 4} and @kbd{N e 4}.

@kindex backspace
@item @key{backspace}
@findex chess-input-shortcut-delete
Delete the last entered chess move character @code{chess-input-shortcut-delete}.

This is useful if you have accidentally typed a wrong character,
and the move was not unambiguous yet.
@end table

@node Plain ASCII diagram displays, ICS1 style ASCII displays, Chess display mode, Chessboard displays
@section Plain ASCII diagram displays

The simplest display style available is @code{chess-plain}, a very customisable
@acronym{ASCII} board diagram display.

This is how the starting position looks in its default configuration:

@example
 +---------------+
8|r n b q k b n r|
7|p p p p p p p p|
6|. . . . . . . .|
5|. . . . . . . .|
4|. . . . . . . .|
3|. . . . . . . .|
2|P P P P P P P P|
1|R N B Q K B N R|
 +---------------+
  a b c d e f g h
@end example

@defopt chess-plain-separate-frame
If non-nil, display the chessboard in its own frame.
@end defopt

@defopt chess-plain-border-style
If non-nil, a vector of Characters used to draw borders.

Otherwise, omit to draw any border around the chessboard diagram.
@end defopt

@defopt chess-plain-black-square-char
Character used to indicate empty black squares.
@end defopt

@defopt chess-plain-white-square-char
Character used to indicate black white squares.
@end defopt

@defopt chess-plain-piece-chars
Alist of pieces and their corresponding characters.
@end defopt

@defopt chess-plain-upcase-indicates
Defines what a upcase char should indicate.
The default is @code{'color}, meaning a upcase char is a white piece, a
lowercase char a black piece.  Possible values: @code{'color} (default),
@code{'square-color}.  If set to @code{'square-color}, a uppercase character
indicates a piece on a black square.  (Note that you also need to
modify `chess-plain-piece-chars' to avoid real confusion.)
@end defopt

@defopt chess-plain-spacing
Number of spaces between files.
@end defopt

@kindex M-x customize-group RET chess-plain RET
To customize options of @code{chess-plain}, use
@kbd{M-x customize-group @key{RET} chess-plain @key{RET}}.

@node ICS1 style ASCII displays, Graphical displays, Plain ASCII diagram displays, Chessboard displays
@section ICS1 style ASCII displays

@code{chess-ics1} is a more verbose @acronym{ASCII} chessboard display.

This is how the starting position looks with this chessboard display:

@example
      +---+---+---+---+---+---+---+---+
    8 | r | n | b | q | k | b | n | r |
      +---+---+---+---+---+---+---+---+
    7 | p | p | p | p | p | p | p | p |
      +---+---+---+---+---+---+---+---+
    6 |   |   |   |   |   |   |   |   |
      +---+---+---+---+---+---+---+---+
    5 |   |   |   |   |   |   |   |   |
      +---+---+---+---+---+---+---+---+
    4 |   |   |   |   |   |   |   |   |
      +---+---+---+---+---+---+---+---+
    3 |   |   |   |   |   |   |   |   |
      +---+---+---+---+---+---+---+---+
    2 | P | P | P | P | P | P | P | P |
      +---+---+---+---+---+---+---+---+
    1 | R | N | B | Q | K | B | N | R |
      +---+---+---+---+---+---+---+---+
        a   b   c   d   e   f   g   h
@end example

@defopt chess-ics1-separate-frame
If non-nil, display the chessboard in its own frame.
@end defopt

@kindex M-x customize-group RET chess-ics1 RET
To customize options of @code{chess-ics1}, use
@kbd{M-x customize-group @key{RET} chess-ics1 @key{RET}}.

@node Graphical displays,  , ICS1 style ASCII displays, Chessboard displays
@section Graphical displays

The graphical chessboard display (@code{chess-images}) uses image files
to create a visually appealing chessboard in a buffer.

@defopt chess-images-directory
A directory which contains images in @acronym{XPM} format.

If you want to draw your own images, each piece must be named
@file{@var{color}-@var{piece}.xpm}, where @var{color} is either black or white,
and @var{piece} is one of rook, knight, bishop, queen, king or pawn.

The only image format currently supported is @acronym{XPM}.
@end defopt

@defopt chess-images-separate-frame
If non-nil, display the chessboard in its own frame.
@end defopt

@kindex M-x customize-group RET chess-images RET
For all customization options of @code{chess-images}, use
@kbd{M-x customize-group @key{RET} chess-images @key{RET}}.

@node Engines, Chess Session, Chessboard displays, Top
@chapter Engines

Engines are the representation of an opponent in Chess.  THe main type
of engine interfaces with an external chess program.  However, there
can be other uses for engine objects, such as providing networked engined
for playing with opponent over different types of transports.

@menu
* Common functions::
* AI::
* Crafty::
* Fruit::
* Glaurung::
* GNU Chess::
* Phalanx::
* Sjeng::
* Stockfish::
@end menu

@node Common functions, AI, Engines, Engines
@section Common functions

@defun chess-engine-create module game &optional response-handler &rest handler-ctor-args
Create a new chess engine @var{module} (a symbol) associated with @var{game}.
Optionally supply a new @var{response-handler}.
@end defun

@defun chess-engine-set-option engine option value
Set @var{engine} @var{option} to @var{value} by invoking its handler with
the 'set-option event.
@end defun

@c lispfun chess-engine-destroy

@c lispfun chess-engine-set-position

@c lispfun chess-engine-position

@defun chess-engine-position engine
Return the current position of the game associated with @var{engine}.
@end defun

@c lispfun chess-engine-set-game

@c lispfun chess-engine-game

@c lispfun chess-engine-index

@c lispfun chess-engine-move

@c lispfun chess-engine-command

@defun chess-engine-command engine event &rest args
Call the handler of @var{engine} with @var{event} (a symbol) and @var{args}.
@end defun

@c lispfun chess-engine-send

@defun chess-engine-send engine string
Send the given @var{string} to @var{engine}.
If `chess-engine-process' is a valid process object, use `process-send-string'
to submit the data.  Otherwise, the 'send event is triggered and the engine
event handler can take care of the data.
@end defun

@node AI, Crafty, Common functions, Engines
@section AI

The @acronym{AI} engine module defines a pure Emacs Lisp implementation
of an opponent.  Contrary to all other engine modules mentioned later on,
it does not require any external programs to be installed.

@kindex C-u M-x chess RET ai RET
To explicitly select this engine as an opponent,
use @kbd{C-u M-x chess @key{RET} ai @key{RET}}.

@defopt chess-ai-depth
Defines the default search depth for this engine.
@end defopt

@defopt chess-ai-quiescence-depth
Defines the number of plies to search for a quiet position.
This is in addition to `chess-ai-depth'.
@end defopt

If you'd like to employ the search and evaluation functions provided
by this module programmatically, the following function is the top-level
entry point.

@defun chess-ai-best-move position &optional depth eval-fn
Find the supposedly best move (ply) for @var{position}.
@var{depth} defaults to the value of `chess-ai-depth'.
@end defun

@node Crafty, Fruit, AI, Engines
@section Crafty

@dfn{Crafty} is a chess program written by Michael Byrne, UAB professor
Dr. Robert Hyatt, Tracy Riegle, Peter Skinner and Ted Langreck.  It is directly
derived from Cray Blitz, winner of the 1983 and 1986 World Computer Chess
Championships.

If the @command{crafty} program is installed and can be found in the program
search path (@code{exec-path}), the @code{chess-crafty} engine module will
automatically detect it.

@vindex chess-crafty-path
If @command{crafty} is installed in a non-standard location, variable
@code{chess-crafty-path} can be set to point to the executable.

@kindex C-u M-x chess RET crafty RET
If you have multiple engines installed you can explicitly select
to play against Crafty by invoking @kbd{C-u M-x chess @key{RET} crafty @key{RET}}.

@node Fruit, Glaurung, Crafty, Engines
@section Fruit

@dfn{Fruit} is a chess engine developed by Fabien Letouzey. It was
commercially available from September 2005 until July 2007.  Now it is freeware
and you can download it for free from @uref{http://www.fruitchess.com/}.
The development on Fruit by Fabien Letouzey has ceded and it is unlikely to
continue.

Fruit was vice world computer chess champion 2005.

If the @command{fruit} command is installed and can be found in the program
search path (@code{exec-path}),
the @code{chess-fruit} engine module will automatically detect it.

@vindex chess-fruit-path
If Fruit is installed in a non-standard location, variable
@code{chess-fruit-path} can be set to point to the executable.

@kindex C-u M-x chess RET fruit RET
If you have multiple engines installed you can explicitly select
to play against Fruit by invoking @kbd{C-u M-x chess @key{RET} fruit @key{RET}}.

@node Glaurung, GNU Chess, Fruit, Engines
@section Glaurung

@dfn{Glaurung} is another freely distributed strong computer chess engine.

If the @command{glaurung} program is installed and can be found in the
program search path (@code{exec-path}),
the @code{chess-glaurung} engine module will automatically detect it.

@vindex chess-glaurung-path
If Glaurung is installed in a non-standard location, variable
@code{chess-glaurung-path} can be set to point to the executable.

@kindex C-u M-x chess RET glaurung RET
If you have multiple engines installed you can explicitly select
to play against Glaurung by invoking @kbd{C-u M-x chess @key{RET} glaurung @key{RET}}.

@node GNU Chess, Phalanx, Glaurung, Engines
@section GNU Chess

@uref{http://gnu.org/software/chess/, @dfn{@acronym{GNU} Chess}} is free
software, licensed under the terms of the @acronym{GNU} General Public License, and is
maintained by collaborating developers.
As one of the earliest computer chess programs with full source code available,
it's one of the oldest for Unix-based systems and has since been ported to many
other platforms.

If the @command{gnuchess} program is installed and can be found in the
program search path (@code{exec-path}),
the @code{chess-gnuchess} engine module will automatically detect it.

@vindex chess-gnuchess-path
If @acronym{GNU} Chess is installed in a non-standard location, variable
@code{chess-gnuchess-path} can be set to point to the executable.

@kindex C-u M-x chess RET gnuchess RET
If you have multiple engines installed you can explicitly select
to play against @acronym{GNU} Chess by invoking
@kbd{C-u M-x chess @key{RET} gnuchess @key{RET}}.

@node Phalanx, Sjeng, GNU Chess, Engines
@section Phalanx

@dfn{Phalanx} is an old, popular chess engine, with an interesting history.

If the @command{phalanx} program is installed and can be found in the
program search path (@code{exec-path}),
the @code{chess-phalanx} engine module will automatically detect it.

@vindex chess-phalanx-path
If Phalanx is installed in a non-standard location, variable
@code{chess-phalanx-path} can be set to point to the executable.

@kindex C-u M-x chess RET phalanx RET
If you have multiple engines installed you can explicitly select
to play against Phalanx by invoking @kbd{C-u M-x chess @key{RET} phalanx @key{RET}}.

@node Sjeng, Stockfish, Phalanx, Engines
@section Sjeng

@uref{http://sjeng.org/, @dfn{Sjeng}} is a championship-winner automated chess engine
developed by Gian-Carlo Pascutto from Belgium.  While its original
version was free, recent developments are for sale.

If the @command{sjeng} program is installed and can be found in the
program search path (@code{exec-path}),
the @code{chess-sjeng} engine module will automatically detect it.

@vindex chess-sjeng-path
If Sjeng is installed in a non-standard location, variable
@code{chess-sjeng-path} can be set to point to the executable.

@kindex C-u M-x chess RET sjeng RET
If you have multiple engines installed you can explicitly select
to play against Sjeng by invoking @kbd{C-u M-x chess @key{RET} sjeng @key{RET}}.

@node Stockfish,  , Sjeng, Engines
@section Stockfish

@uref{http://www.stockfishchess.org/, @dfn{Stockfish}} is one of the strongest
chess engines in the world, appearing near or at the top of most chess engine
rating lists.
Stockfish is also free software, licensed under the terms of the
@acronym{GNU} General Public License.

If the @command{stockfish} program is installed and can be found in the
program search path (@code{exec-path}),
the @code{chess-stockfish} engine module will automatically detect it.

@vindex chess-stockfish-path
If Stockfish is installed in a non-standard location, variable
@code{chess-stockfish-path} can be set to point to the executable.

@kindex C-u M-x chess RET stockfish RET
If you have multiple engines installed you can explicitly select
to play against Stockfish by invoking @kbd{C-u M-x chess @key{RET} stockfish @key{RET}}.

@node Chess Session, Internet Chess Servers, Engines, Top
@chapter Chess Session

A chess session assembles all modules mentioned in previous
chapters into a working system to interact with.  A session typically
consists of at least one display module, one engine module,
and possibly a number of optional modules.  All these mdoules
share a common game object which is used to keep track of the
currently active game.

@defun chess engine disable-popup engine-response-handler &rest engine-ctor-args
Play a game against @var{engine}.

This function constructs all the necessary modules required for a chess session.
In particular, it will start @var{engine} and create a chess display as
configured in @code{chess-default-display}.

This is the main entry-point for interactively launching
a chessboard display with associated engine.

If you want to launch a chess session as part of your own code,
the probably more expressive alias `chess-session' might be interesting
to use.
@end defun

You can have several active chess sessions.
In fact, some features later described in this manual make use
of this, @xref{Internet Chess Servers}.

@kindex M-x chess RET
@vindex chess-default-display
@vindex chess-default-engine
To interactively start a chess session, invoke
@kbd{M-x chess @key{RET}}.  This uses @code{chess-default-display}
to determine the chessboard display to use, and
@code{chess-default-engine} to determine an opponent.

@kindex C-u M-x chess RET
If you want to play against a specific engine, provide
a prefix argument as in @kbd{C-u M-x chess @key{RET}}, which will
prompt for an engine module.  The module name has the common
prefix @samp{chess-} stripped.  So you enter @samp{gnuchess}
to indicate you'd like to play against the @code{chess-gnuchess} module.

@node Internet Chess Servers, Concept Index, Chess Session, Top
@chapter Internet Chess Servers

Based on the services provided above, there is also a special mode
for communication with Internet Chess Servers.

On an Internet Chess Server you can seek to play against other
human or computer players, observe other games being player or examined,
play tournaments, chat with fellow chess players, participate in team games,
or do various other interesting chess related things.

A default set of well known servers is defined in the following variable:

@defvar chess-ics-server-list
A list of servers to connect to.
The format of each entry is:

  (@var{server} @var{port} [@var{handle}] [@var{password-or-filename}] [@var{helper}] [@var{helper args}@enddots{}])
@end defvar

Internet Chess Servers based on
@uref{http://www.freechess.org/, @acronym{FICS, Free Internet Chess Server}}
and @uref{http://www.chessclub.com/, @acronym{ICC, Internet Chess Club}}
are currently supported.

@menu
* Connecting to a server::
* Chess ICS Mode::
* Command History::
* Seeking an opponent for a new game::
* The sought game display::
* Watching other games::
@end menu

@node Connecting to a server, Chess ICS Mode, Internet Chess Servers, Internet Chess Servers
@section Connecting to a server

To open a new connection to an Internet Chess Server, use:

@c lispfun chess-ics

@defun chess-ics server port &optional handle password-or-filename helper &rest helper-args
Connect to an Internet Chess Server.

If called interactively, you will be prompted to enter a server
(from `chess-ics-server-list' and possibly identification credentials.
@end defun

@node Chess ICS Mode, Command History, Connecting to a server, Internet Chess Servers
@section Chess ICS Mode
@cindex Chess ICS mode
@cindex mode, ICS

  The major mode for ICS buffers is Chess ICS mode.  Many of its special
commands are bound to the @kbd{C-c} prefix.  Here is a list of ICS mode
commands:

@table @kbd
@item @key{RET}
@kindex RET
@findex comint-send-input
Send the current line as input to the server
(@code{comint-send-input}).  Any prompt at the beginning of the
line is omitted.  If point is at the end of
buffer, this is like submitting the command line in an ordinary
interactive shell.  However, you can also invoke @key{RET} elsewhere
in the ICS buffer to submit the current line as input.

@item C-d
@kindex C-d
@findex comint-delchar-or-maybe-eof
Either delete a character or send @acronym{EOF, End Of File}
(@code{comint-delchar-or-maybe-eof}).  Typed at the end of the ICS
buffer, this sends @acronym{EOF} to the server and terminates the
connection.  Typed at any other
position in the buffer, this deletes a character as usual.

@item C-c C-a
@kindex C-c C-a
@findex comint-bol-or-process-mark
Move to the beginning of the line, but after the prompt if any
(@code{comint-bol-or-process-mark}).  If you repeat this command twice
in a row, the second time it moves back to the process mark, which is
the beginning of the input that you have not yet sent to the server.
(Normally that is the same place---the end of the prompt on this
line---but after @kbd{C-c @key{SPC}} the process mark may be in a
previous line.)

@item C-c @key{SPC}
@kindex C-c SPC
@findex comint-accumulate
Accumulate multiple lines of input, then send them together
(@code{comint-accumulate}).  This
command inserts a newline before point, but does not send the preceding
text as input to the server---at least, not yet.  Both lines, the one
before this newline and the one after, will be sent together (along with
the newline that separates them), when you type @key{RET}.

@item C-c C-u
@kindex C-c C-u
@findex comint-kill-input
Kill all text pending at end of buffer to be sent as input
(@code{comint-kill-input}).  If point is not at end of buffer,
this only kills the part of this text that precedes point.

@item C-c C-w
@kindex C-c C-w
@findex backward-kill-word
Kill a word before point (@code{backward-kill-word}).

@item C-c C-o
@kindex C-c C-o
@findex comint-delete-output
Delete the last batch of output from an ICS server command
(@code{comint-delete-output}).  This is useful if a server command spews
out lots of output that just gets in the way.

@item C-c C-s
@kindex C-c C-s
@findex comint-write-output
Write the last batch of output from an ICS server command to a file
(@code{comint-write-output}).  With a prefix argument, the file is
appended to instead.  Any prompt at the end of the output is not
written.

@item C-c C-r
@itemx C-M-l
@kindex C-c C-r
@kindex C-M-l
@findex comint-show-output
Scroll to display the beginning of the last batch of output at the top
of the window; also move the cursor there (@code{comint-show-output}).

@item C-c C-e
@kindex C-c C-e
@findex comint-show-maximum-output
Scroll to put the end of the buffer at the bottom of the window
(@code{comint-show-maximum-output}).

@item M-x comint-truncate-buffer
@findex comint-truncate-buffer
@vindex comint-buffer-maximum-size
This command truncates the ICS buffer to a certain maximum number of
lines, specified by the variable @code{comint-buffer-maximum-size}.
Here's how to do this automatically each time you get output from the
server:

@example
(add-hook 'comint-output-filter-functions
          'comint-truncate-buffer)
@end example
@end table

@cindex Comint mode
@cindex mode, Comint
  ICS mode is a derivative of Comint mode, a general-purpose mode for
communicating with interactive subprocesses.  Most of the features of
ICS mode actually come from Comint mode, as you can see from the
command names listed above.

@node Command History, Seeking an opponent for a new game, Chess ICS Mode, Internet Chess Servers
@section ICS Command History

  ICS buffers support two ways of repeating earlier commands.  You
can use keys like those used for the minibuffer history; these work
much as they do in the minibuffer, inserting text from prior commands
while point remains always at the end of the buffer.  You can move
through the buffer to previous inputs in their original place, then
resubmit them or copy them to the end.

@menu
* ICS Command Ring::
* ICS History Copying::
@end menu

@node ICS Command Ring, ICS History Copying, Command History, Command History
@subsection ICS Command History Ring

@table @kbd
@findex comint-previous-input
@kindex M-p
@item M-p
@itemx C-@key{UP}
Fetch the next earlier old ICS command.

@kindex M-n
@findex comint-next-input
@item M-n
@itemx C-@key{DOWN}
Fetch the next later old ICS command.

@kindex M-r
@findex comint-history-isearch-backward-regexp
@item M-r
Begin an incremental regexp search of old ICS commands.

@item C-c C-x
@kindex C-c C-x
@findex comint-get-next-from-history
Fetch the next subsequent command from the history.

@item C-c .
@kindex C-c .
@findex comint-input-previous-argument
Fetch one argument from an old ICS command.

@item C-c C-l
@kindex C-c C-l
@findex comint-dynamic-list-input-ring
Display the buffer's history of ICS commands in another window
(@code{comint-dynamic-list-input-ring}).
@end table

  ICS buffers provide a history of previously entered
commands.  To reuse commands from the history, use the editing
commands @kbd{M-p}, @kbd{M-n}, @kbd{M-r} and @kbd{M-s}.  These work
just like the minibuffer history commands (@pxref{Minibuffer
History, , , emacs}), except that they operate within the ICS buffer rather
than the minibuffer.

  @kbd{M-p} fetches an earlier ICS command to the end of the ICS
buffer.  Successive use of @kbd{M-p} fetches successively earlier
commands, each replacing any text that was already present as
potential input.  @kbd{M-n} does likewise except that it finds
successively more recent ICS commands from the buffer.
@kbd{C-@key{UP}} works like @kbd{M-p}, and @kbd{C-@key{DOWN}} like
@kbd{M-n}.

  The history search command @kbd{M-r} begins an incremental regular
expression search of previous ICS commands.  After typing @kbd{M-r},
start typing the desired string or regular expression; the last
matching ICS command will be displayed in the current line.
Incremental search commands have their usual effects---for instance,
@kbd{C-s} and @kbd{C-r} search forward and backward for the next match
(@pxref{Incremental Search, , , emacs}).  When you find the desired input, type
@key{RET} to terminate the search.  This puts the input in the command
line.  Any partial input you were composing before navigating the
history list is restored when you go to the beginning or end of the
history ring.

  Often it is useful to reexecute several successive ICS commands that
were previously executed in sequence.  To do this, first find and
reexecute the first command of the sequence.  Then type @kbd{C-c C-x};
that will fetch the following command---the one that follows the command
you just repeated.  Then type @key{RET} to reexecute this command.  You
can reexecute several successive commands by typing @kbd{C-c C-x
@key{RET}} over and over.

  The command @kbd{C-c .}@: (@code{comint-input-previous-argument})
copies an individual argument from a previous command, like
@kbd{@key{ESC} .} in Bash.  The simplest use copies the last argument from the
previous ICS command.  With a prefix argument @var{n}, it copies the
@var{n}th argument instead.  Repeating @kbd{C-c .} copies from an
earlier ICS command instead, always using the same value of @var{n}
(don't give a prefix argument when you repeat the @kbd{C-c .}
command).

  These commands get the text of previous ICS commands from a special
history list, not from the ICS buffer itself.  Thus, editing the ICS
buffer, or even killing large parts of it, does not affect the history
that these commands access.

@node ICS History Copying,  , ICS Command Ring, Command History
@subsection ICS History Copying

@table @kbd
@kindex C-c C-p
@findex comint-previous-prompt
@item C-c C-p
Move point to the previous prompt (@code{comint-previous-prompt}).

@kindex C-c C-n
@findex comint-next-prompt
@item C-c C-n
Move point to the following prompt (@code{comint-next-prompt}).

@kindex C-c RET
@findex comint-copy-old-input
@item C-c @key{RET}
Copy the input command at point, inserting the copy at the end of the
buffer (@code{comint-copy-old-input}).  This is useful if you move
point back to a previous command.  After you copy the command, you can
submit the copy as input with @key{RET}.  If you wish, you can edit
the copy before resubmitting it.  If you use this command on an output
line, it copies that line to the end of the buffer.

@item Mouse-2
If @code{comint-use-prompt-regexp} is @code{nil} (the default), copy
the old input command that you click on, inserting the copy at the end
of the buffer (@code{comint-insert-input}).  If
@code{comint-use-prompt-regexp} is non-@code{nil}, or if the click is
not over old input, just yank as usual.
@end table

  Moving to a previous input and then copying it with @kbd{C-c
@key{RET}} or @kbd{Mouse-2} produces the same results---the same
buffer contents---that you would get by using @kbd{M-p} enough times
to fetch that previous input from the history list.  However, @kbd{C-c
@key{RET}} copies the text from the buffer, which can be different
from what is in the history list if you edit the input text in the
buffer after it has been sent.

@node Seeking an opponent for a new game, The sought game display, Command History, Internet Chess Servers
@section Seeking an opponent for a new game

After you connected to a server, one of the first things you will
want to do is find an oponent for a new game.  You can use the
ICS command "seek" to announce your availability for a chess game
to interested people.

For example:

@example
fics% seek 10 10 rated
@end example

This will announce your availability to play a rated game with 10 minutes
initial time-control for each player, and 10 seconds added
for every move made.

@node The sought game display, Watching other games, Seeking an opponent for a new game, Internet Chess Servers
@section The sought game display

There is a special mode for displaying games sought by other users
on an Internet Chess Server.  Provided you didn't turn off seek advertisments
manually (for instance by setting the seek variable to 0 (off) on the
ICS server by issueing "set seek 0"), the first seek advertisment
automatically pops up a new window which is in `chess-ics-ads-mode', a
derivative of `tabulated-list-mode'.

@c lispfun chess-ics-ads-mode

@defun chess-ics-ads-mode 
A mode for displaying @var{ics} game seek advertisments.

This mode runs the hook `chess-ics-ads-mode-hook', as the final step
during initialization.

key             binding
---             -------

?		describe-mode
@var{ret}		chess-ics-sought-accept
<mouse-2>	chess-ics-sought-accept


@end defun

@kindex RET
In this buffer, use mouse-2 or @kbd{@key{RET}} on a line to accept that
particular game and play it.

@node Watching other games,  , The sought game display, Internet Chess Servers
@section Watching other games

You can also watch other games currently being played on @acronym{ICS}.
Even services like @samp{LectureBot} from @acronym{FICS} can
be used.

@example
fics% observe lecturebot
You are now observing game 5.
Game 5: LectureBot (0) LectureBot (0) unrated untimed 0 0

LectureBot(TD)(----)[5] kibitzes: (Note: A passed pawn is a pawn that
          does not have enemy pawns blocking the path either on the
          same or adjacent files).
LectureBot(TD)(----)[5] kibitzes: Connected passed pawns are a pain to
          have to deal with. They are usually a winning advantage if
          they cannot be blockaded. The blockading piece has to give
          up duties elsewhere. It's almost like being a piece up.
fics% unobserv lecturebot
Removing game 5 from observation list.
fics% 
@end example

Once you start to observe a particular game or player, the
current position will pop up in a chessboard display.
As you are an observer, you will not be able to enter new moves.
However, you should be able to navigate back and forth in the history
of the game.

If a new move is made by any party in the game and you are currently
displaying the last position in the game, the chessboard display
will automaticall update to reflect the new position and show
the last move in the mode line.

@node Concept Index, Function and Variable Index, Internet Chess Servers, Top
@unnumbered Concept Index

@printindex cp

@node Function and Variable Index, Key Index, Concept Index, Top
@unnumbered Function and Variable Index

@printindex fn

@node Key Index,  , Function and Variable Index, Top
@unnumbered Key Index

@printindex ky
@bye