-
Notifications
You must be signed in to change notification settings - Fork 3
/
pygn-mode.el
2098 lines (1824 loc) · 91.8 KB
/
pygn-mode.el
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
;;; pygn-mode.el --- Major-mode for chess PGN files, powered by Python -*- lexical-binding: t -*-
;; Copyright (c) 2019-2021 Dodge Coates and Roland Walker
;; Author: Dodge Coates and Roland Walker
;; Homepage: https://github.com/dwcoates/pygn-mode
;; URL: https://raw.githubusercontent.com/dwcoates/pygn-mode/master/pygn-mode.el
;; Version: 0.6.0
;; Last-Updated: 06 Aug 2021
;; Package-Requires: ((emacs "26.1") (tree-sitter "0.15.2") (tree-sitter-langs "0.10.7") (uci-mode "0.5.4") (nav-flash "1.0.0") (ivy "0.10.0"))
;; Keywords: data, games, chess
;;; License
;; Simplified BSD License:
;; Redistribution and use in source and binary forms, with or
;; without modification, are permitted provided that the following
;; conditions are met:
;; 1. Redistributions of source code must retain the above
;; copyright notice, this list of conditions and the following
;; disclaimer.
;; 2. Redistributions in binary form must reproduce the above
;; copyright notice, this list of conditions and the following
;; disclaimer in the documentation and/or other materials
;; provided with the distribution.
;; This software is provided by the authors "AS IS" and any express
;; or implied warranties, including, but not limited to, the implied
;; warranties of merchantability and fitness for a particular
;; purpose are disclaimed. In no event shall the authors or
;; contributors be liable for any direct, indirect, incidental,
;; special, exemplary, or consequential damages (including, but not
;; limited to, procurement of substitute goods or services; loss of
;; use, data, or profits; or business interruption) however caused
;; and on any theory of liability, whether in contract, strict
;; liability, or tort (including negligence or otherwise) arising in
;; any way out of the use of this software, even if advised of the
;; possibility of such damage.
;; The views and conclusions contained in the software and
;; documentation are those of the authors and should not be
;; interpreted as representing official policies, either expressed
;; or implied, of the authors.
;;; Internal notes
;; Bugs
;; TODO
;; Extensive ert test coverage of
;; - pygn-mode-pgn-at-pos-as-if-variation
;; pygn-mode-go-searchmoves which defaults to searching move under point
;; Flash current move on selection
;; IDEA
;; UCI moves to pgn: UCI position command arguments to pgn and/or graphical display
;; count games in current file? Display in modeline?
;; evil text objects?
;;; Commentary:
;; Quickstart
;; (require 'pygn-mode)
;; M-x pygn-mode-run-diagnostic
;; Explanation
;; Pygn-mode is a major-mode for viewing and editing chess PGN files.
;; Directly editing PGN files is interesting for programmers who are
;; developing chess engines, or advanced players who are doing deep
;; analysis on games. This mode is not useful for simply playing chess.
;; Bindings
;; No keys are bound by default. Consider
;; (eval-after-load "pygn-mode"
;; (define-key pygn-mode-map (kbd "M-f") 'pygn-mode-next-move)
;; (define-key pygn-mode-map (kbd "M-b") 'pygn-mode-previous-move))
;; Customization
;; M-x customize-group RET pygn RET
;; See Also
;; http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm
;; https://github.com/dwcoates/uci-mode
;; Prior Art
;; https://github.com/jwiegley/emacs-chess
;; Notes
;; Compatibility and Requirements
;; GNU Emacs 26.1+, compiled with dynamic module support
;; tree-sitter.el and tree-sitter-langs.el
;; Python and the chess library are needed for numerous features such
;; as SVG board images:
;; https://pypi.org/project/chess/
;; A version of the Python chess library is bundled with this package.
;; Note that the chess library has its own license (GPL3+).
;;; Code:
(defconst pygn-mode-version "0.6.0")
;;; Imports
(require 'cl-lib)
(require 'comint)
(require 'uci-mode nil t)
(require 'nav-flash nil t)
(require 'ivy nil t)
(require 'tree-sitter)
(require 'tree-sitter-hl)
(require 'tree-sitter-langs)
;;; Declarations
(eval-when-compile
(require 'subr-x)
(defvar nav-flash-delay)
(defvar uci-mode-engine-buffer)
(defvar uci-mode-engine-buffer-name))
(declare-function uci-mode-engine-proc "uci-mode.el")
(declare-function uci-mode-run-engine "uci-mode.el")
(declare-function uci-mode-send-stop "uci-mode.el")
(declare-function uci-mode-send-commands "uci-mode.el")
(declare-function ivy-completing-read "ivy.el")
;;; Customizable variables
;;;###autoload
(defgroup pygn nil
"A major-mode for chess PGN files, powered by Python."
:version pygn-mode-version
:prefix "pygn-mode-"
:group 'data
:group 'games)
(defcustom pygn-mode-python-executable "python"
"Path to a Python 3.7+ interpreter."
:group 'pygn
:type 'string)
(defcustom pygn-mode-pythonpath
(expand-file-name
"lib/python/site-packages"
(file-name-directory
(or load-file-name
(bound-and-true-p byte-compile-current-file)
(buffer-file-name (current-buffer)))))
"A colon-delimited path to prepend to the `$PYTHONPATH' environment variable.
The default points to the bundled Python `chess' library. Set to nil to
ignore the bundled library and use only the system `$PYTHONPATH'."
:group 'pygn
:type 'string)
(defcustom pygn-mode-board-size 400
"Size for graphical board display, expressed as pixels-per-side."
:group 'pygn
:type 'int)
(defcustom pygn-mode-board-flipped nil
"If non-nil, display the board flipped."
:group 'pygn-mode
:type 'boolean)
(defcustom pygn-mode-flash-full-game nil
"If non-nil, flash the entire PGN on game selection actions."
:group 'pygn
:type 'boolean)
(defcustom pygn-mode-default-engine-depth 20
"Default depth for engine analysis."
:group 'pygn
:type 'int)
(defcustom pygn-mode-default-engine-time 15
"Default seconds for engine analysis."
:group 'pygn
:type 'int)
(defcustom pygn-mode-server-stderr-buffer-name nil
"Buffer name for server stderr output, nil to redirect stderr to null device."
:group 'pygn
:type 'string)
;;;###autoload
(defgroup pygn-faces nil
"Faces used by pygn-mode."
:group 'pygn)
(defface pygn-mode-tagpair-key-face
'((t (:inherit font-lock-keyword-face)))
"pygn-mode face for tagpair (header) keys."
:group 'pygn-faces)
(defface pygn-mode-tagpair-value-face
'((t (:inherit font-lock-string-face)))
"pygn-mode face for tagpair (header) values."
:group 'pygn-faces)
(defface pygn-mode-tagpair-bracket-face
'((t (:foreground "Gray50")))
"pygn-mode face for tagpair (header) square brackets."
:group 'pygn-faces)
(defface pygn-mode-annotation-face
'((t (:inherit font-lock-comment-face)))
"pygn-mode face for annotation symbols."
:group 'pygn-faces)
(defface pygn-mode-inline-comment-face
'((t (:inherit font-lock-comment-face)))
"pygn-mode face for inline comments."
:group 'pygn-faces)
(defface pygn-mode-rest-of-line-comment-face
'((t (:inherit font-lock-comment-face)))
"pygn-mode face for rest-of-line comments."
:group 'pygn-faces)
(defface pygn-mode-twic-section-comment-face
'((t (:inherit font-lock-comment-face)))
"pygn-mode face for TWIC-style section comments."
:group 'pygn-faces)
(defface pygn-mode-move-face
'((t (:inherit default)))
"pygn-mode face for moves."
:group 'pygn-faces)
(defface pygn-mode-move-number-face
'((t (:inherit default)))
"pygn-mode face for move numbers."
:group 'pygn-faces)
(defface pygn-mode-variation-move-face
'((t (:foreground "Gray50")))
"pygn-mode face for variation moves."
:group 'pygn-faces)
(defface pygn-mode-variation-move-number-face
'((t (:foreground "Gray50")))
"pygn-mode face for variation move numbers."
:group 'pygn-faces)
(defface pygn-mode-variation-delimiter-face
'((t (:foreground "Gray50")))
"pygn-mode face for variation delimiters."
:group 'pygn-faces)
(defface pygn-mode-variation-annotation-face
'((t (:inherit font-lock-comment-face)))
"pygn-mode face for annotation symbols within variations."
:group 'pygn-faces)
(defface pygn-mode-variation-inline-comment-face
'((t (:inherit font-lock-comment-face)))
"pygn-mode face for inline comments within variations."
:group 'pygn-faces)
(defface pygn-mode-variation-rest-of-line-comment-face
'((t (:inherit font-lock-comment-face)))
"pygn-mode face for rest-of-line comments within variations."
:group 'pygn-faces)
(defface pygn-mode-result-face
'((t (:inherit font-lock-builtin-face)))
"pygn-mode face for result codes."
:group 'pygn-faces)
(defface pygn-mode-invalid-face
'((t (:inherit font-lock-warning-face)))
"pygn-mode face for spans of text which are not valid PGN."
:group 'pygn-faces)
(define-obsolete-face-alias
'pygn-mode-nag-face
'pygn-mode-annotation-face
"0.6.0")
;;; Variables
(defvar pygn-mode-script-directory
(file-name-directory
(or load-file-name
(bound-and-true-p byte-compile-current-file)
(buffer-file-name (current-buffer))))
"Directory to find Python server script \"pygn_server.py\".")
(defvar pygn-mode-python-chess-succeeded nil
"Whether a simple external command using the Python chess library has succeeded.")
(defvar pygn-mode-fen-buffer-name "*pygn-mode-fen*"
"Buffer name used to display FENs.")
(defvar pygn-mode-board-buffer-name "*pygn-mode-board*"
"Buffer name used to display boards.")
(defvar pygn-mode-line-buffer-name "*pygn-mode-line*"
"Buffer name used to display SAN lines.")
(defvar pygn-mode-diagnostic-output-buffer-name "*pygn-mode-diagnostic-output*"
"Buffer name used to display results of a diagnostic check.")
(defvar pygn-mode--server-process nil
"Python-based server which powers many `pygn-mode' features.")
(defvar pygn-mode--server-buffer-name " *pygn-mode-server*"
"Buffer name used to associate a server process.")
(defvar pygn-mode--server-buffer nil
"Buffer to which the `pygn-mode' server process is associated.")
(defvar pygn-mode--server-receive-every-seconds 0.01
"How often `pygn-mode--server-receive' should check the server for output when polling.")
(defvar pygn-mode--server-receive-max-seconds 0.5
"The maximum amount of time `pygn-mode--server-receive' should check the server for output when polling.")
(defvar pygn-mode-annotation-names
(let ((names (make-hash-table :test 'equal)))
(puthash "$1" "Good move" names)
(puthash "$2" "Poor move or mistake" names)
(puthash "$3" "Very good or brilliant move" names)
(puthash "$4" "Very poor move or blunder" names)
(puthash "$5" "Speculative or interesting move" names)
(puthash "$6" "Questionable or dubious move" names)
(puthash "$7" "Forced move (all others lose quickly) or only move" names)
(puthash "$8" "Singular move (no reasonable alternatives)" names)
(puthash "$9" "Worst move" names)
(puthash "$10" "Drawish position or even" names)
(puthash "$11" "Equal chances, quiet position" names)
(puthash "$12" "Equal chances, active position" names)
(puthash "$13" "Unclear position" names)
(puthash "$14" "White has a slight advantage" names)
(puthash "$15" "Black has a slight advantage" names)
(puthash "$16" "White has a moderate advantage" names)
(puthash "$17" "Black has a moderate advantage" names)
(puthash "$18" "White has a decisive advantage" names)
(puthash "$19" "Black has a decisive advantage" names)
(puthash "$20" "White has a crushing advantage (Black should resign)" names)
(puthash "$21" "Black has a crushing advantage (White should resign)" names)
(puthash "$22" "White is in zugzwang" names)
(puthash "$23" "Black is in zugzwang" names)
(puthash "$24" "White has a slight space advantage" names)
(puthash "$25" "Black has a slight space advantage" names)
(puthash "$26" "White has a moderate space advantage" names)
(puthash "$27" "Black has a moderate space advantage" names)
(puthash "$28" "White has a decisive space advantage" names)
(puthash "$29" "Black has a decisive space advantage" names)
(puthash "$30" "White has a slight time (development) advantage" names)
(puthash "$31" "Black has a slight time (development) advantage" names)
(puthash "$32" "White has a moderate time (development) advantage" names)
(puthash "$33" "Black has a moderate time (development) advantage" names)
(puthash "$34" "White has a decisive time (development) advantage" names)
(puthash "$35" "Black has a decisive time (development) advantage" names)
(puthash "$36" "White has the initiative" names)
(puthash "$37" "Black has the initiative" names)
(puthash "$38" "White has a lasting initiative" names)
(puthash "$39" "Black has a lasting initiative" names)
(puthash "$40" "White has the attack" names)
(puthash "$41" "Black has the attack" names)
(puthash "$42" "White has insufficient compensation for material deficit" names)
(puthash "$43" "Black has insufficient compensation for material deficit" names)
(puthash "$44" "White has sufficient compensation for material deficit" names)
(puthash "$45" "Black has sufficient compensation for material deficit" names)
(puthash "$46" "White has more than adequate compensation for material deficit" names)
(puthash "$47" "Black has more than adequate compensation for material deficit" names)
(puthash "$48" "White has a slight center control advantage" names)
(puthash "$49" "Black has a slight center control advantage" names)
(puthash "$50" "White has a moderate center control advantage" names)
(puthash "$51" "Black has a moderate center control advantage" names)
(puthash "$52" "White has a decisive center control advantage" names)
(puthash "$53" "Black has a decisive center control advantage" names)
(puthash "$54" "White has a slight kingside control advantage" names)
(puthash "$55" "Black has a slight kingside control advantage" names)
(puthash "$56" "White has a moderate kingside control advantage" names)
(puthash "$57" "Black has a moderate kingside control advantage" names)
(puthash "$58" "White has a decisive kingside control advantage" names)
(puthash "$59" "Black has a decisive kingside control advantage" names)
(puthash "$60" "White has a slight queenside control advantage" names)
(puthash "$61" "Black has a slight queenside control advantage" names)
(puthash "$62" "White has a moderate queenside control advantage" names)
(puthash "$63" "Black has a moderate queenside control advantage" names)
(puthash "$64" "White has a decisive queenside control advantage" names)
(puthash "$65" "Black has a decisive queenside control advantage" names)
(puthash "$66" "White has a vulnerable first rank" names)
(puthash "$67" "Black has a vulnerable first rank" names)
(puthash "$68" "White has a well protected first rank" names)
(puthash "$69" "Black has a well protected first rank" names)
(puthash "$70" "White has a poorly protected king" names)
(puthash "$71" "Black has a poorly protected king" names)
(puthash "$72" "White has a well protected king" names)
(puthash "$73" "Black has a well protected king" names)
(puthash "$74" "White has a poorly placed king" names)
(puthash "$75" "Black has a poorly placed king" names)
(puthash "$76" "White has a well placed king" names)
(puthash "$77" "Black has a well placed king" names)
(puthash "$78" "White has a very weak pawn structure" names)
(puthash "$79" "Black has a very weak pawn structure" names)
(puthash "$80" "White has a moderately weak pawn structure" names)
(puthash "$81" "Black has a moderately weak pawn structure" names)
(puthash "$82" "White has a moderately strong pawn structure" names)
(puthash "$83" "Black has a moderately strong pawn structure" names)
(puthash "$84" "White has a very strong pawn structure" names)
(puthash "$85" "Black has a very strong pawn structure" names)
(puthash "$86" "White has poor knight placement" names)
(puthash "$87" "Black has poor knight placement" names)
(puthash "$88" "White has good knight placement" names)
(puthash "$89" "Black has good knight placement" names)
(puthash "$90" "White has poor bishop placement" names)
(puthash "$91" "Black has poor bishop placement" names)
(puthash "$92" "White has good bishop placement" names)
(puthash "$93" "Black has good bishop placement" names)
(puthash "$94" "White has poor rook placement" names)
(puthash "$95" "Black has poor rook placement" names)
(puthash "$96" "White has good rook placement" names)
(puthash "$97" "Black has good rook placement" names)
(puthash "$98" "White has poor queen placement" names)
(puthash "$99" "Black has poor queen placement" names)
(puthash "$100" "White has good queen placement" names)
(puthash "$101" "Black has good queen placement" names)
(puthash "$102" "White has poor piece coordination" names)
(puthash "$103" "Black has poor piece coordination" names)
(puthash "$104" "White has good piece coordination" names)
(puthash "$105" "Black has good piece coordination" names)
(puthash "$106" "White has played the opening very poorly" names)
(puthash "$107" "Black has played the opening very poorly" names)
(puthash "$108" "White has played the opening poorly" names)
(puthash "$109" "Black has played the opening poorly" names)
(puthash "$110" "White has played the opening well" names)
(puthash "$111" "Black has played the opening well" names)
(puthash "$112" "White has played the opening very well" names)
(puthash "$113" "Black has played the opening very well" names)
(puthash "$114" "White has played the middlegame very poorly" names)
(puthash "$115" "Black has played the middlegame very poorly" names)
(puthash "$116" "White has played the middlegame poorly" names)
(puthash "$117" "Black has played the middlegame poorly" names)
(puthash "$118" "White has played the middlegame well" names)
(puthash "$119" "Black has played the middlegame well" names)
(puthash "$120" "White has played the middlegame very well" names)
(puthash "$121" "Black has played the middlegame very well" names)
(puthash "$122" "White has played the ending very poorly" names)
(puthash "$123" "Black has played the ending very poorly" names)
(puthash "$124" "White has played the ending poorly" names)
(puthash "$125" "Black has played the ending poorly" names)
(puthash "$126" "White has played the ending well" names)
(puthash "$127" "Black has played the ending well" names)
(puthash "$128" "White has played the ending very well" names)
(puthash "$129" "Black has played the ending very well" names)
(puthash "$130" "White has slight counterplay" names)
(puthash "$131" "Black has slight counterplay" names)
(puthash "$132" "White has moderate counterplay" names)
(puthash "$133" "Black has moderate counterplay" names)
(puthash "$134" "White has decisive counterplay" names)
(puthash "$135" "Black has decisive counterplay" names)
(puthash "$136" "White has moderate time control pressure" names)
(puthash "$137" "Black has moderate time control pressure" names)
(puthash "$138" "White has severe time control pressure" names)
(puthash "$139" "Black has severe time control pressure" names)
(puthash "$140" "With the idea…" names)
(puthash "$141" "Aimed against…" names)
(puthash "$142" "Better is…" names)
(puthash "$143" "Worse is…" names)
(puthash "$144" "Equivalent is…" names)
(puthash "$145" "Editorial comment" names)
(puthash "$146" "Novelty" names)
(puthash "$238" "Space advantage" names)
(puthash "$239" "File" names)
(puthash "$240" "Diagonal" names)
(puthash "$241" "Center" names)
(puthash "$242" "Kingside" names)
(puthash "$243" "Queenside" names)
(puthash "$244" "Weak point" names)
(puthash "$245" "Ending" names)
(puthash "$246" "Bishop pair" names)
(puthash "$247" "Opposite Bishops" names)
(puthash "$248" "Same Bishops" names)
(puthash "$249" "Connected pawns" names)
(puthash "$250" "Isolated pawns" names)
(puthash "$251" "Doubled pawns" names)
(puthash "$252" "Passed pawn" names)
(puthash "$253" "Pawn majority" names)
(puthash "$254" "With" names)
(puthash "$255" "Without" names)
(puthash "!" "Good move" names)
(puthash "?" "Poor move or mistake" names)
(puthash "‼" "Very good or brilliant move" names)
(puthash "!!" "Very good or brilliant move" names)
(puthash "⁇" "Very poor move or blunder" names)
(puthash "??" "Very poor move or blunder" names)
(puthash "⁉" "Speculative or interesting move" names)
(puthash "!?" "Speculative or interesting move" names)
(puthash "⁈" "Questionable or dubious move" names)
(puthash "?!" "Questionable or dubious move" names)
(puthash "□" "Forced move (all others lose quickly) or only move" names)
(puthash "=" "Drawish position or even" names)
(puthash "∞" "Unclear position" names)
(puthash "⩲" "White has a slight advantage" names)
(puthash "⩱" "Black has a slight advantage" names)
(puthash "±" "White has a moderate advantage" names)
(puthash "∓" "Black has a moderate advantage" names)
(puthash "+−" "White has a decisive advantage" names)
(puthash "−+" "Black has a decisive advantage" names)
(puthash "⨀" "Player is in zugzwang" names)
(puthash "○" "Player has a moderate space advantage" names)
(puthash "⟳" "Player has a moderate time (development) advantage" names)
(puthash "↑" "Player has the initiative" names)
(puthash "→" "Player has the attack" names)
(puthash "=/∞" "Player has sufficient compensation for material deficit" names)
(puthash "⇆" "Player has moderate counterplay" names)
(puthash "⨁" "Player has severe time control pressure" names)
(puthash "∆" "With the idea…" names)
(puthash "∇" "Aimed against…" names)
(puthash "⌓" "Better is…" names)
(puthash "<=" "Worse is…" names)
(puthash "==" "Equivalent is…" names)
(puthash "RR" "Editorial comment" names)
(puthash "N" "Novelty" names)
(puthash "○" "Space advantage" names)
(puthash "⇔" "File" names)
(puthash "⇗" "Diagonal" names)
(puthash "⊞" "Center" names)
(puthash "⟫" "Kingside" names)
(puthash "⟪" "Queenside" names)
(puthash "✕" "Weak point" names)
(puthash "⊥" "Ending" names)
names)
"Names and descriptions of annotations recognized by `pygn-mode'.")
;;; Syntax table
(defvar pygn-mode-syntax-table
(let ((st (make-syntax-table text-mode-syntax-table)))
(with-syntax-table st
(modify-syntax-entry ?{ "<")
(modify-syntax-entry ?} ">")
(modify-syntax-entry ?\n "-")
(modify-syntax-entry ?\r "-")
(modify-syntax-entry ?\\ "\\")
(modify-syntax-entry ?\" "\"")
(modify-syntax-entry ?& "w")
(modify-syntax-entry ?! "w")
(modify-syntax-entry ?* "w")
(modify-syntax-entry ?+ "w")
(modify-syntax-entry ?- "w")
(modify-syntax-entry ?. "w")
(modify-syntax-entry ?/ "w")
(modify-syntax-entry ?< "w")
(modify-syntax-entry ?= "w")
(modify-syntax-entry ?? "w")
(modify-syntax-entry ?\⟪ "w")
(modify-syntax-entry ?\⟫ "w")
(modify-syntax-entry ?| "w")
(modify-syntax-entry ?± "w")
(modify-syntax-entry ?½ "w")
(modify-syntax-entry ?٭ "w")
(modify-syntax-entry ?᛭ "w")
(modify-syntax-entry ?‐ "w")
(modify-syntax-entry ?‑ "w")
(modify-syntax-entry ?‒ "w")
(modify-syntax-entry ?– "w")
(modify-syntax-entry ?— "w")
(modify-syntax-entry ?‼ "w")
(modify-syntax-entry ?‽ "w")
(modify-syntax-entry ?⁄ "w")
(modify-syntax-entry ?⁇ "w")
(modify-syntax-entry ?⁈ "w")
(modify-syntax-entry ?⁉ "w")
(modify-syntax-entry ?⁎ "w")
(modify-syntax-entry ?↑ "w")
(modify-syntax-entry ?→ "w")
(modify-syntax-entry ?⇆ "w")
(modify-syntax-entry ?⇔ "w")
(modify-syntax-entry ?⇗ "w")
(modify-syntax-entry ?∆ "w")
(modify-syntax-entry ?∇ "w")
(modify-syntax-entry ?− "w")
(modify-syntax-entry ?∓ "w")
(modify-syntax-entry ?∕ "w")
(modify-syntax-entry ?∗ "w")
(modify-syntax-entry ?∞ "w")
(modify-syntax-entry ?≤ "w")
(modify-syntax-entry ?⊞ "w")
(modify-syntax-entry ?⊥ "w")
(modify-syntax-entry ?⌓ "w")
(modify-syntax-entry ?└ "w")
(modify-syntax-entry ?┘ "w")
(modify-syntax-entry ?□ "w")
(modify-syntax-entry ?○ "w")
(modify-syntax-entry ?◺ "w")
(modify-syntax-entry ?◿ "w")
(modify-syntax-entry ?♂ "w")
(modify-syntax-entry ?✕ "w")
(modify-syntax-entry ?✱ "w")
(modify-syntax-entry ?➕ "w")
(modify-syntax-entry ?➖ "w")
(modify-syntax-entry ?⟋ "w")
(modify-syntax-entry ?⟳ "w")
(modify-syntax-entry ?⧸ "w")
(modify-syntax-entry ?⨀ "w")
(modify-syntax-entry ?⨁ "w")
(modify-syntax-entry ?⩱ "w")
(modify-syntax-entry ?⩲ "w")
(modify-syntax-entry ?⬒ "w")
(modify-syntax-entry ?⬓ "w"))
st)
"Syntax table used while in `pygn-mode'.")
;;; Keymaps
(defvar pygn-mode-map
(let ((map (make-sparse-keymap)))
;; menu bar and lighter
(define-key map [menu-bar PyGN]
(cons "PyGN" (make-sparse-keymap "PyGN")))
(define-key map [menu-bar PyGN pygn-mode-select-game]
'(menu-item "Select Game" pygn-mode-select-game
:help "Select the current game"))
(define-key map [menu-bar PyGN pygn-mode-ivy-jump-to-game-by-fen]
'(menu-item "Jump to Game by FEN" pygn-mode-ivy-jump-to-game-by-fen
:enable (featurep 'ivy)
:help "Jump to a game by FEN"))
(define-key map [menu-bar PyGN pygn-mode-ivy-jump-to-game-by-any-header]
'(menu-item "Jump to Game by Header" pygn-mode-ivy-jump-to-game-by-header
:enable (featurep 'ivy)
:help "Jump to a game by any header content"))
(define-key map [menu-bar PyGN pygn-mode-previous-game]
'(menu-item "Previous Game" pygn-mode-previous-game
:help "Navigate to the previous game"))
(define-key map [menu-bar PyGN pygn-mode-next-game]
'(menu-item "Next Game" pygn-mode-next-game
:help "Navigate to the next game"))
(define-key map [menu-bar PyGN sep] menu-bar-separator)
(define-key map [menu-bar PyGN pygn-mode-previous-move]
'(menu-item "Previous Move" pygn-mode-previous-move
:help "Navigate to the previous move"))
(define-key map [menu-bar PyGN pygn-mode-next-move]
'(menu-item "Next Move" pygn-mode-next-move
:help "Navigate to the next move"))
(define-key map [menu-bar PyGN sep-2] menu-bar-separator)
(define-key map [menu-bar PyGN pygn-mode-engine-go-time]
'(menu-item "Go Time at Point" pygn-mode-engine-go-time
:enable (featurep 'uci-mode)
:help "UCI Engine \"go time\" at point in separate window"))
(define-key map [menu-bar PyGN pygn-mode-engine-go-depth]
'(menu-item "Go Depth at Point" pygn-mode-engine-go-depth
:enable (featurep 'uci-mode)
:help "UCI Engine \"go depth\" at point in separate window"))
(define-key map [menu-bar PyGN pygn-mode-describe-annotation-at-pos]
'(menu-item "Annotation at Point" pygn-mode-describe-annotation-at-pos
:enable (pygn-mode--true-containing-node 'annotation)
:help "Describe annotation at point in the echo area"))
(define-key map [menu-bar PyGN pygn-mode-display-fen-at-pos]
'(menu-item "FEN at Point" pygn-mode-display-fen-at-pos
:help "Display FEN at point in separate window"))
(define-key map [menu-bar PyGN pygn-mode-display-board-at-pos]
'(menu-item "Board at Point" pygn-mode-display-board-at-pos
:help "Display board at point in separate window"))
(define-key map [menu-bar PyGN pygn-mode-display-variation-line-at-pos]
'(menu-item "Line at Point" pygn-mode-display-variation-line-at-pos
:help "Display SAN line at point in separate window"))
;; mouse
(define-key map [mouse-2] 'pygn-mode-mouse-display-variation-board)
;; example keystrokes:
;;
;; (define-key map (kbd "C-c C-n") 'pygn-mode-next-game)
;; (define-key map (kbd "C-c C-p") 'pygn-mode-previous-game)
;; (define-key map (kbd "M-f") 'pygn-mode-next-move)
;; (define-key map (kbd "M-b") 'pygn-mode-previous-move)
;; (define-key map (kbd "C-h $") 'pygn-mode-describe-annotation-at-pos)
;;
;; and note that `down-list'/`backward-up-list' already works to
;; enter/exit a parenthesized variation
map)
"Keymap for `pygn-mode'.")
;;; Utility functions
(defun pygn-mode-comment-region-contextually (beg end &optional arg)
"Wrap `comment-region-default' to remove `comment-continue' characters.
This allows multi-line `comment-region' to work in `pygn-mode'
without adding extra characters at beginning-of-line."
(if (and (pygn-mode--true-containing-node 'movetext beg)
(pygn-mode--true-containing-node 'movetext end))
(save-match-data
(comment-region-default beg end arg)
(save-restriction
(exchange-point-and-mark)
(narrow-to-region (min (point) (mark) beg) (max (point) (mark) end))
(save-excursion
(goto-char (point-min))
(while (re-search-forward (concat "^" (regexp-quote (or comment-continue "|"))) nil t)
(replace-match "")))))
;; else
(let ((comment-start ";")
(comment-end "")
(comment-style 'plain))
(comment-region-default beg end arg))))
(defun pygn-mode--get-or-create-board-buffer ()
"Get or create the `pygn-mode' board buffer."
(let ((buf (get-buffer-create pygn-mode-board-buffer-name)))
(with-current-buffer buf
(unless (eq 'pygn-board-mode major-mode)
(pygn-board-mode)))
buf))
(defun pygn-mode--opts-to-argparse (opt-plist)
"Convert OPT-PLIST into an options string consumable by Python's argparse.
To produce a flag which takes no options, give a plist value of t."
(let ((key-string nil)
(argparse-string ""))
(cl-loop for (key value) on opt-plist by (function cddr)
do (progn
(setq key-string
(replace-regexp-in-string
"^:" "-" (symbol-name key)))
(cond
((eq value t)
(setq argparse-string (concat
argparse-string
" " key-string)))
;; if option is nil then we just don't send the flag.
((not (eq value nil))
(let ((val-string (shell-quote-argument
(format "%s" value))))
(setq argparse-string (concat
argparse-string
(format " %s=%s" key-string val-string))))))))
argparse-string))
(defun pygn-mode--set-python-path ()
"Prepend `pygn-mode-pythonpath' to the system `$PYTHONPATH'."
(setenv "PYTHONPATH" (concat pygn-mode-pythonpath ":" (getenv "PYTHONPATH"))))
(defun pygn-mode--server-running-p ()
"Return non-nil iff `pygn-mode--server-process' is running."
(and pygn-mode--server-process (process-live-p pygn-mode--server-process)))
(defun pygn-mode--python-chess-guard ()
"Throw an error unless the Python chess library is available."
(unless pygn-mode-python-chess-succeeded
(let ((process-environment (cl-copy-list process-environment)))
(when pygn-mode-pythonpath
(pygn-mode--set-python-path))
(if (zerop (call-process pygn-mode-python-executable nil nil nil "-c" "import chess"))
(setq pygn-mode-python-chess-succeeded t)
(error "The Python interpreter at `pygn-mode-python-path' must have the Python chess library available")))))
(defun pygn-mode--get-stderr-buffer ()
"Get or create the buffer to which to redirect the standard error."
(when pygn-mode-server-stderr-buffer-name
(get-buffer-create pygn-mode-server-stderr-buffer-name)))
(defun pygn-mode--server-start (&optional force)
"Initialize `pygn-mode--server-process'.
Optionally FORCE recreation if the server already exists."
(pygn-mode--python-chess-guard)
(if force
(pygn-mode--server-kill)
(when (pygn-mode--server-running-p)
(error "The pygn-mode server process is already running. Use optional `force' to recreate")))
(message "Initializing pygn-mode server process%s." (if force " (forcing)" ""))
(let ((process-environment (cl-copy-list process-environment)))
(when pygn-mode-pythonpath
(pygn-mode--set-python-path))
(setenv "PYTHONIOENCODING" "UTF-8")
(setq pygn-mode--server-buffer (get-buffer-create pygn-mode--server-buffer-name))
(setq pygn-mode--server-process
(make-process :name "pygn-mode-server"
:buffer pygn-mode--server-buffer
:noquery t
:sentinel #'ignore
:coding 'utf-8
:connection-type 'pipe
:stderr (or (pygn-mode--get-stderr-buffer) null-device)
:command (list pygn-mode-python-executable
"-u"
(expand-file-name "pygn_server.py" pygn-mode-script-directory)
"-"))))
(unless (string-match-p (regexp-quote "Server started.") (pygn-mode--server-receive))
(error "Server for `pygn-mode' failed to start. Try running `pygn-mode-run-diagnostic'")))
(defun pygn-mode--server-kill ()
"Stop the currently running `pygn-mode--server-process'."
(when (pygn-mode--server-running-p)
(process-send-eof pygn-mode--server-process)
(delete-process pygn-mode--server-process)
(setq pygn-mode--server-process nil)
(message "pygn-mode server process killed.")))
(cl-defun pygn-mode--server-send (&key command options payload-type payload)
"Send a message to the running `pygn-mode--server-process'.
The server request format is documented more completely at doc/server.md
in the source distribution for `pygn-mode'.
:COMMAND should be a symbol such as :pgn-to-fen, which is a command
known by the server. :OPTIONS should be a plist such as (:pixels 400)
in which the keys correspond to argparse arguments known by the server.
:PAYLOAD-TYPE should be a symbol such as :pgn, identifying the type of the
data payload, and :PAYLOAD may contain arbitrary data."
(unless (pygn-mode--server-running-p)
(error "The pygn-mode server is not running -- cannot send a message"))
(setq payload (replace-regexp-in-string "\n" "\\\\n" payload))
(setq payload (replace-regexp-in-string "[\n\r]*$" "\n" payload))
(process-send-string
pygn-mode--server-process
(mapconcat #'identity
(list
(symbol-name :version)
pygn-mode-version
(symbol-name command)
(pygn-mode--opts-to-argparse options)
"--"
(symbol-name payload-type)
payload)
" ")))
(defun pygn-mode--server-receive ()
"Receive a response after `pygn-mode--server-send'.
Respects the variables `pygn-mode--server-receive-every-seconds' and
`pygn-mode--server-receive-max-seconds'."
(unless (pygn-mode--server-running-p)
(error "The pygn-mode server is not running -- cannot receive a response"))
(unless (get-buffer pygn-mode--server-buffer)
(error "The pygn-mode server output buffer does not exist -- cannot receive a response"))
(with-current-buffer pygn-mode--server-buffer
(let ((tries 0)
(server-message nil))
(goto-char (point-min))
(while (and (not (eq ?\n (char-before (point-max))))
(< (* tries pygn-mode--server-receive-every-seconds) pygn-mode--server-receive-max-seconds))
(accept-process-output pygn-mode--server-process pygn-mode--server-receive-every-seconds nil 1)
(cl-incf tries))
(setq server-message (buffer-substring-no-properties (point-min) (point-max)))
(erase-buffer)
server-message)))
(cl-defun pygn-mode--server-query (&key command options payload-type payload)
"Send a request to `pygn-mode--server-process', wait, and return response.
:COMMAND, :OPTIONS, :PAYLOAD-TYPE, and :PAYLOAD are as documented at
`pygn-mode--server-send'."
(unless (pygn-mode--server-running-p)
(pygn-mode--server-start))
(pygn-mode--server-send
:command command
:options options
:payload-type payload-type
:payload payload)
(pygn-mode--server-receive))
;; it is a bit muddy that the parser is permitted to restart the server
(defun pygn-mode--parse-response (response)
"Parse RESPONSE string into a list of payload-id and payload.
Restart `pygn-mode--server-process' if the response version string does
not match the client."
(let ((response-version nil))
(save-match-data
(setq response (replace-regexp-in-string "\n+\\'" "" response))
(when (string= "" response)
(error "Bad response from `pygn-mode' server -- empty response"))
(unless (string-match
"\\`:version\\s-+\\(\\S-+\\)\\s-+\\(.*\\)" response)
(pygn-mode--server-start 'force)
(error "Bad response from `pygn-mode' server -- no :version. Attempted restart"))
(setq response-version (match-string 1 response))
(setq response (match-string 2 response))
(unless (equal response-version pygn-mode-version)
(pygn-mode--server-start 'force)
(error "Bad response from `pygn-mode' server -- unexpected :version value: '%s'. Attempted restart" response-version))
(unless (string-match
"\\`\\(:\\S-+\\)\\(.*\\)" response)
(error "Bad response from `pygn-mode' server"))
(list
(intern (match-string 1 response))
(replace-regexp-in-string
"\\`\s-*" "" (match-string 2 response))))))
;; would be nicer if multi-type were chosen by how far up we must go in the
;; syntax tree to find a node of the type, instead of the latest-starting by
;; buffer position.
(cl-defun pygn-mode--true-containing-node (&optional type pos)
"Return the node of type TYPE which contains POS, adjusting whitespace.
TYPE defaults to the nearest containing node. If TYPE is a symbol, find
the first containing node of that type. If TYPE is a list of symbols,
find the latest-starting containing node of any of the given types.
POS defaults to the point.
If a node has leading or trailing whitespace, and POS is in that
whitespace, ignore the result, and consult the parent node. This is a
major difference between this function and `tree-sitter-node-at-pos'.
Also respect narrowing.
If TYPE is unset, and an appropriate containing node is not found,
return the root node."
(cl-callf or pos (point))
(save-excursion
(goto-char pos)
(let ((best-first -1)
(best-node nil)
(type-list (if (listp type) (or type '(nil)) (list type))))
(dolist (tp type-list)
(let ((node (tree-sitter-node-at-pos tp pos 'ignore-invalid-types))
(first nil)
(last nil))
(while node
(setq first (pygn-mode--true-node-first-position node))
(setq last (pygn-mode--true-node-last-position node))
(cond
((and (>= pos first)
(<= pos last)
(> first best-first))
(setq best-first first)
(setq best-node node)
(setq node nil))
(tp
(setq node nil))
(t
(setq node (tsc-get-parent node)))))))
(if best-node
best-node
(if (or (not type)
(memq 'series_of_games type-list))
(tsc-root-node tree-sitter-tree))))))
(defun pygn-mode--true-node-first-position (node)
"Return the true first position within NODE, adjusting whitespace.
Also respect narrowing."
(let ((first (max (point-min) (tsc-node-start-position node))))
(cond
((eq node (tsc-root-node tree-sitter-tree))
(setq first (point-min)))
(t
(save-excursion
(goto-char first)
(skip-syntax-forward "-")
(setq first (point)))))
first))
(defun pygn-mode--true-node-last-position (node)
"Return the true last position within NODE, adjusting whitespace.
Also respect narrowing."
(let ((last (min (point-max) (tsc-node-end-position node))))
(cond
((eq node (tsc-root-node tree-sitter-tree))
(setq last (point-max)))
(t
(save-excursion
(goto-char last)
(skip-syntax-backward "-")
(while (and (> (point) (point-min))
(looking-at-p "\\s-"))
(forward-char -1))
(setq last (point)))))
last))
(defun pygn-mode--true-node-after-position (node)
"Return the true first position after NODE, adjusting whitespace.
Also respect narrowing."