summaryrefslogtreecommitdiff
path: root/chess-display.el
blob: 526d8ca0780e8ddbb7256658f478d4a945079e61 (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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Code shared by all chess displays
;;
;; $Revision$

;;; Code:

(require 'chess-session)
(require 'chess-game)
(require 'chess-algebraic)
(require 'chess-fen)

(defgroup chess-display nil
  "Common code used by chess displays."
  :group 'chess)

(defcustom chess-display-use-faces t
  "If non-nil, provide colored faces for ASCII displays."
  :type 'boolean
  :group 'chess-display)

(defface chess-display-black-face
  '((((class color) (background light)) (:foreground "Green"))
    (((class color) (background dark)) (:foreground "Green"))
    (t (:bold t)))
  "*The face used for black pieces on the ASCII display."
  :group 'chess-display)

(defface chess-display-white-face
  '((((class color) (background light)) (:foreground "Yellow"))
    (((class color) (background dark)) (:foreground "Yellow"))
    (t (:bold t)))
  "*The face used for white pieces on the ASCII display."
  :group 'chess-display)

(defface chess-display-highlight-face
  '((((class color) (background light)) (:background "#add8e6"))
    (((class color) (background dark)) (:background "#add8e6")))
  "Face to use for highlighting pieces that have been selected."
  :group 'chess-display)

;;; Code:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; User interface
;;

(defvar chess-display-session)
(defvar chess-display-game)
(defvar chess-display-search-function)
(defvar chess-display-variation)
(defvar chess-display-index)
(defvar chess-display-ply)
(defvar chess-display-position)
(defvar chess-display-perspective)
(defvar chess-display-draw-function nil)
(defvar chess-display-highlight-function nil)
(defvar chess-display-edit-mode nil)
(defvar chess-display-mode-line "")

(make-variable-buffer-local 'chess-display-session)
(make-variable-buffer-local 'chess-display-game)
(make-variable-buffer-local 'chess-display-search-function)
(make-variable-buffer-local 'chess-display-variation)
(make-variable-buffer-local 'chess-display-index)
(make-variable-buffer-local 'chess-display-ply)
(make-variable-buffer-local 'chess-display-position)
(make-variable-buffer-local 'chess-display-perspective)
(make-variable-buffer-local 'chess-display-draw-function)
(make-variable-buffer-local 'chess-display-highlight-function)
(make-variable-buffer-local 'chess-display-edit-mode)
(make-variable-buffer-local 'chess-display-mode-line)

(defmacro chess-with-current-buffer (buffer &rest body)
  `(let ((buf ,buffer))
     (if buf
	 (with-current-buffer buf
	   ,@body)
       ,@body)))

(defun chess-display-create (style perspective &optional session search-func)
  "Create a chess display, for displaying chess objects."
  (let* ((name (symbol-name style))
	 (draw (intern-soft (concat name "-draw")))
	 (highlight (intern-soft (concat name "-highlight")))
	 (initialize (intern-soft (concat name "-initialize"))))
    (unless initialize
      (error "There is no known chessboard display style '%s'" name))
    (with-current-buffer (generate-new-buffer "*Chessboard*")
      (setq cursor-type nil
	    chess-display-session session
	    chess-display-draw-function draw
	    chess-display-highlight-function highlight
	    chess-display-perspective perspective
	    chess-display-search-function search-func)
      (chess-display-mode)
      (funcall initialize)
      (if session
	  (let ((game (chess-session-data session 'current-game)))
	    (if game
		(chess-display-set-game nil game))))
      (current-buffer))))

(defsubst chess-display-destroy (display)
  "Destroy a chess display object, killing all of its buffers."
  (let ((buf (or display (current-buffer))))
    (if (buffer-live-p buf)
	(kill-buffer buf))))

(defsubst chess-display-session (display)
  (chess-with-current-buffer display
    chess-display-session))

(defsubst chess-display-perspective (display)
  (chess-with-current-buffer display
    chess-display-perspective))

(defun chess-display-set-perspective (display perspective)
  (chess-with-current-buffer display
    (setq chess-display-perspective perspective)
    (chess-display-update nil)))

(defun chess-display-set-search-function (display search-func)
  (chess-with-current-buffer display
    (if chess-display-game
	(error "Cannot alter a display's search function when viewing a game")
      (setq chess-display-search-function search-func))))

(defun chess-display-search-function (display)
  (chess-with-current-buffer display
    (if chess-display-game
	(chess-game-search-function chess-display-game)
      (or chess-display-search-function
	  'chess-standard-search-position))))

(defsubst chess-display-search-position (display position target piece)
  (chess-with-current-buffer display
    (funcall (chess-display-search-function nil)
	     position target piece)))

(defun chess-display-set-position (display position &optional search-func)
  "Set the display position.
Note that when a single position is being displayed, out of context of
a game, the user's move will cause a new variation to be created,
without a game object.
If the position is merely edited, it will change the POSITION object
that was passed in."
  (chess-with-current-buffer display
    (setq chess-display-game nil
	  chess-display-search-function search-func
	  chess-display-variation nil
	  chess-display-index nil
	  chess-display-ply nil
	  chess-display-position position)
    (chess-display-update nil)))

(defun chess-display-position (display)
  "Return the position currently viewed."
  (chess-with-current-buffer display
    (or (and chess-display-game
	     (chess-game-pos chess-display-game chess-display-index))
	(and chess-display-variation
	     (chess-ply-next-pos
	      (nth chess-display-index chess-display-variation)))
	(and chess-display-ply
	     (chess-ply-next-pos chess-display-ply))
	chess-display-position)))

(defun chess-display-set-ply (display ply &optional search-func)
  "Set the display ply.
This differs from a position display, only in that the algebraic form
of the move made to the reach the displayed position will be shown in
the modeline."
  (chess-with-current-buffer display
    (setq chess-display-game nil
	  chess-display-search-function search-func
	  chess-display-variation nil
	  chess-display-index nil
	  chess-display-ply ply
	  chess-display-position nil)
    (chess-display-update display)))

(defun chess-display-ply (display)
  (chess-with-current-buffer display
    (or (and chess-display-game
	     (chess-game-ply chess-display-game chess-display-index))
	(and chess-display-variation
	     (nth chess-display-index chess-display-variation))
	chess-display-ply)))

(defun chess-display-set-variation (display plies &optional index search-func)
  "Set the display variation.
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."
  (chess-with-current-buffer display
    (setq chess-display-game nil
	  chess-display-search-function search-func
	  chess-display-variation plies
	  chess-display-index (or index 0)
	  chess-display-ply nil
	  chess-display-position nil)
    (chess-display-update nil)))

(defun chess-display-variation (display)
  (chess-with-current-buffer display
    (or (and chess-display-game
	     (chess-game-plies chess-display-game))
	chess-display-variation)))

(defun chess-display-set-game (display game &optional index)
  "Set the display game.
This will cause the first ply in the game's main variation to be
displayed.  Also, information about the game is shown in the
modeline."
  (chess-with-current-buffer display
    (setq chess-display-game game
	  chess-display-search-function nil
	  chess-display-variation nil
	  chess-display-index (or index 0)
	  chess-display-ply nil
	  chess-display-position nil)
    (chess-display-update nil)))

(defsubst chess-display-game (display)
  (chess-with-current-buffer display
    chess-display-game))

(defun chess-display-set-index (display index)
  (chess-with-current-buffer display
    (unless chess-display-index
      (error "There is no game or variation currently being displayed."))
    (if chess-display-game
	(unless (chess-game-pos chess-display-game index)
	  (error "There is no position in the displayed game at index %d."
		 index))
      (if (or (not (integerp index))
	      (< index 0)
	      (>= index (length chess-display-variation)))
	  (error "There is no position in the display variation at index %d."
		 index)))
    (setq chess-display-index index)
    (chess-display-update nil)))

(defsubst chess-display-index (display)
  (chess-with-current-buffer display
    chess-display-index))

(defun chess-display-update (display)
  "This should be called if any object passed in has been changed.
That is, if you call `chess-display-set-position', and then later
change that position object, the display can be updated by calling
`chess-display-update'."
  (chess-with-current-buffer display
    (if chess-display-draw-function
	(funcall chess-display-draw-function))
    (chess-display-set-modeline)))

(defun chess-display-move (display ply)
  "Move a piece on DISPLAY, by applying the given PLY.
The position of PLY must match the currently displayed position.
If only START is given, it must be in algebraic move notation."
  (chess-with-current-buffer display
    (cond
     ((chess-display-active-p)
      (chess-session-event chess-display-session 'move ply))
     (chess-display-game
      ;; jww (2002-03-28): This should beget a variation within the
      ;; game, or alter the game, just as SCID allows
      (if (= (chess-display-index nil)
	     (chess-game-index chess-display-game))
	  (setq chess-display-index
		(1+ (chess-game-index chess-display-game))))
      (chess-game-move chess-display-game ply))
     (chess-display-variation
      ;; jww (2002-04-02): what if we're in the middle?
      (nconc chess-display-variation (list ply))
      (setq chess-display-index (1- (length chess-display-variation))))
     (chess-display-ply
      (setq chess-display-ply ply))
     (t					; an ordinary position
      (setq chess-display-position (chess-ply-next-pos ply))))
    (chess-display-update nil)))

(defun chess-display-highlight (display index &optional mode)
  "Highlight the square at INDEX on the current position.
The given highlighting 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."
  (chess-with-current-buffer display
    (if (chess-display-active-p)
	(chess-session-event chess-display-session 'highlight
			     index (or mode 'selected))
      (if chess-display-highlight-function
	  (funcall chess-display-highlight-function index
		   (or mode 'selected))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Module method
;;

;;;###autoload
(defun chess-display (session display event &rest args)
  "This display module presents a standard chessboard.
See `chess-display-type' for the different kinds of displays."
  (if (eq event 'initialize)
      (chess-display-create
       (car args) (chess-session-data session 'my-color) session)
    (ignore
     (cond
      ((eq event 'shutdown)
       (chess-display-destroy display))

      ((eq event 'setup)
       (chess-display-set-game display (car args)))

      ((eq event 'highlight)
       ;; calling `chess-display-highlight' here would be recursive
       (if chess-display-highlight-function
	   (funcall chess-display-highlight-function
		    (car args) (cadr args))))

      ((eq event 'pass)
       (chess-display-set-perspective
	display (not (chess-display-perspective display))))

      ((eq event 'move)
       (chess-display-set-index
	display (chess-game-index (chess-display-game display)))
       (chess-display-update display))

      (t
       (chess-display-update display))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; chess-display-mode
;;

(defvar chess-display-mode-map
  (let ((map (make-keymap)))
    (suppress-keymap map)
    (set-keymap-parent map nil)

    (define-key map [(control ?i)] 'chess-display-invert)
    (define-key map [tab] 'chess-display-invert)

    (define-key map [??] 'describe-mode)
    (define-key map [?C] 'chess-display-clear-board)
    (define-key map [?E] 'chess-display-edit-board)
    (define-key map [?F] 'chess-display-set-from-fen)
    (define-key map [?I] 'chess-display-invert)
    (define-key map [?X] 'chess-display-quit)
    (define-key map [?M] 'chess-display-manual-move)

    (define-key map [?<] 'chess-display-move-backward)
    (define-key map [?,] 'chess-display-move-backward)
    (define-key map [(meta ?<)] 'chess-display-move-first)
    (define-key map [?>] 'chess-display-move-forward)
    (define-key map [?.] 'chess-display-move-forward)
    (define-key map [(meta ?>)] 'chess-display-move-last)

    (define-key map [(meta ?w)] 'chess-display-copy-board)
    (define-key map [(control ?y)] 'chess-display-paste-board)

    (define-key map [(control ?l)] 'chess-display-redraw)

    (dolist (key '(?a ?b ?c ?d ?e ?f ?g ?h
		      ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8
		      ?r ?n ?b ?q ?k ?o))
      (define-key map (vector key) 'chess-keyboard-shortcut))
    (define-key map [backspace] 'chess-keyboard-shortcut-delete)

    (define-key map [(control ?m)] 'chess-display-select-piece)
    (define-key map [return] 'chess-display-select-piece)
    (cond
     ((featurep 'xemacs)
      (define-key map [(button1)] 'chess-display-mouse-select-piece)
      (define-key map [(button2)] 'chess-display-mouse-select-piece))
     (t
      (define-key map [mouse-1] 'chess-display-mouse-select-piece)
      (define-key map [mouse-2] 'chess-display-mouse-select-piece)))
    map)
  "The mode map used in a chessboard display buffer.")

(defun chess-display-redraw ()
  "Just redraw the current display."
  (interactive)
  (chess-display-update nil))

(defun chess-display-mode ()
  "A mode for displaying and interacting with a chessboard.
The key bindings available in this mode are:
\\{chess-display-mode-map}"
  (interactive)
  (setq major-mode 'chess-display-mode mode-name "Chessboard")
  (use-local-map chess-display-mode-map)
  (buffer-disable-undo)
  (setq buffer-auto-save-file-name nil
	mode-line-format 'chess-display-mode-line))

(defun chess-display-set-modeline ()
  "Set the modeline to reflect the current game position."
  (let ((ply (chess-display-ply nil))
	(color (chess-pos-side-to-move (chess-display-position nil)))
	(index (chess-display-index nil)))
    (if (and index (= index 0))
	(setq chess-display-mode-line
	      (format "   %s   START" (if color "White" "BLACK")))
      (setq chess-display-mode-line
	    (concat
	     "  " (if color "White" "BLACK")
	     (if index
		 (concat "   " (int-to-string
				(if (> index 1)
				    (/ index 2) (1+ (/ index 2))))))
	     (if ply
		 (concat ". " (if color "... ")
			 (chess-ply-to-algebraic
			  ply nil (chess-display-search-function nil)))))))))

(defsubst chess-display-active-p ()
  "Return non-nil if the displayed chessboard reflects an active game.
Basically, it means we are not editing or reviewing.
The game must be part of an active session (i.e., not just reviewing a
game object), and the board must represent the current position in
that game (i.e., not editing the position, or reviewing an earlier
position within the game)."
  (and chess-display-session
       chess-display-game
       (= (chess-display-index nil)
	  (chess-game-index chess-display-game))
       (not chess-display-edit-mode)))

(defun chess-display-invert ()
  "Invert the perspective of the current chess board."
  (interactive)
  (chess-display-set-perspective nil (not (chess-display-perspective nil))))

(defun chess-display-set-from-fen (fen)
  "Send the current board configuration to the user."
  (interactive "sSet from FEN string: ")
  (chess-display-set-position nil (chess-fen-to-pos fen)))

(defun chess-display-copy-board ()
  "Send the current board configuration to the user."
  (interactive)
  (let* ((x-select-enable-clipboard t)
	 (fen (chess-fen-from-pos (chess-display-position nil))))
    (kill-new fen)
    (message "Copied board: %s" fen)))

(defun chess-display-paste-board ()
  "Send the current board configuration to the user."
  (interactive)
  (let* ((x-select-enable-clipboard t)
	 (fen (current-kill 0)))
    (chess-display-set-from-fen fen)
    (message "Pasted board: %s" fen)))

(defun chess-display-set-piece ()
  "Set the piece under point to command character, or space for clear."
  (interactive)
  (unless (chess-display-active-p)
    (chess-pos-set-piece (chess-display-position nil)
			 (get-text-property (point) 'chess-coord)
			 last-command-char)
    (chess-display-update nil)))

(defun chess-display-quit ()
  "Quit the current game."
  (interactive)
  (if (chess-display-active-p)
      (chess-session-event chess-display-session 'shutdown)
    (chess-display-destroy nil)))

(defun chess-display-manual-move (move)
  "Move a piece manually, using chess notation."
  (interactive
   (list (read-string
	  (format "%s(%d): "
		  (if (chess-pos-side-to-move (chess-display-position nil))
		      "White" "Black")
		  (1+ (/ (or (chess-display-index nil) 0) 2))))))
  (chess-display-move nil (chess-algebraic-to-ply
			   (chess-display-position nil) move
			   (chess-display-search-function nil))))

(defun chess-display-set-current (dir)
  "Change the currently displayed board.
Direction may be - or +, to move forward or back, or t or nil to jump
to the end or beginning."
  (let ((index (cond ((eq dir ?-) (1- chess-display-index))
		     ((eq dir ?+) (1+ chess-display-index))
		     ((eq dir t) nil)
		     ((eq dir nil) 0))))
    (chess-display-set-index
     nil (or index
	     (if chess-display-game
		 (chess-game-index chess-display-game)
	       (1- (length chess-display-variation)))))
    (message "Use '>' to return to the current position")))

(defun chess-display-move-backward ()
  (interactive)
  (if chess-display-index
      (chess-display-set-current ?-)))

(defun chess-display-move-forward ()
  (interactive)
  (if chess-display-index
      (chess-display-set-current ?+)))

(defun chess-display-move-first ()
  (interactive)
  (if chess-display-index
      (chess-display-set-current nil)))

(defun chess-display-move-last ()
  (interactive)
  (if chess-display-index
      (chess-display-set-current t)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; chess-display-edit-mode (for editing the position directly)
;;

(defvar chess-display-edit-mode-map
  (let ((map (make-keymap)))
    (suppress-keymap map)
    (set-keymap-parent map chess-display-mode-map)

    (define-key map [?C] 'chess-display-clear-board)
    (define-key map [?G] 'chess-display-restore-board)
    (define-key map [?S] 'chess-display-send-board)

    (let ((keys '(?  ?p ?r ?n ?b ?q ?k ?P ?R ?N ?B ?Q ?K)))
      (while keys
	(define-key map (vector (car keys)) 'chess-display-set-piece)
	(setq keys (cdr keys))))
    map)
  "The mode map used for editing a chessboard position.")

(defun chess-display-edit-board ()
  "Setup the current board for editing."
  (interactive)
  (setq cursor-type t
	chess-display-edit-mode t)
  ;; Take us out of any game/ply/variation we might be looking at,
  ;; since we are not moving pieces now, but rather placing them --
  ;; for which purpose the movement keys can still be used.
  (chess-display-set-position nil (chess-display-position nil))
  ;; jww (2002-03-28): setup edit-mode keymap here
  (message "Now editing position directly, use S when complete..."))

(defun chess-display-send-board ()
  "Send the current board configuration to the user."
  (interactive)
  (if chess-display-session
      (chess-session-event chess-display-session 'setup
			   (chess-game-create (chess-display-position nil))))
  (setq cursor-type nil
	chess-display-edit-mode nil))

(defun chess-display-restore-board ()
  "Setup the current board for editing."
  (interactive)
  (setq cursor-type nil
	chess-display-edit-mode nil)
  ;; jww (2002-03-28): NYI
  (chess-display-update nil))

(defun chess-display-clear-board ()
  "Setup the current board for editing."
  (interactive)
  (when (y-or-n-p "Really clear the chessboard? ")
    (let ((position (chess-display-position nil)))
      (dotimes (rank 8)
	(dotimes (file 8)
	  (chess-pos-set-piece position (cons rank file) ? ))))
    (chess-display-update nil)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Allow for quick entry of algebraic moves via keyboard
;;

(defvar chess-move-string nil)
(defvar chess-legal-moves-pos nil)
(defvar chess-legal-moves nil)

(make-variable-buffer-local 'chess-move-string)
(make-variable-buffer-local 'chess-legal-moves-pos)
(make-variable-buffer-local 'chess-legal-moves)

(defun chess-keyboard-shortcut-delete ()
  (interactive)
  (setq chess-move-string
	(substring chess-move-string 0
		   (1- (length chess-move-string)))))

(defun chess-keyboard-shortcut (&optional display-only)
  (interactive)
  (unless (memq last-command '(chess-keyboard-shortcut
			       chess-keyboard-shortcut-delete))
    (setq chess-move-string nil))
  (unless display-only
    (setq chess-move-string
	  (concat chess-move-string
		  (char-to-string (downcase last-command-char)))))
  (let ((position (chess-display-position nil))
	(search-func (chess-display-search-function nil)))
    (unless (and chess-legal-moves
		 (eq position chess-legal-moves-pos))
      (setq chess-legal-moves-pos position
	    chess-legal-moves
	    (sort (mapcar (function
			   (lambda (ply)
			     (chess-ply-to-algebraic ply nil search-func)))
			  (chess-legal-plies position search-func))
		  'string-lessp)))
    (let ((moves (mapcar
		  (function
		   (lambda (move)
		     (let ((i 0) (x 0)
			   (l (length move))
			   (xl (length chess-move-string))
			   (match t))
		       (unless (or (and (equal chess-move-string "ok")
					(equal move "O-O"))
				   (and (equal chess-move-string "oq")
					(equal move "O-O-O")))
			 (while (and (< i l) (< x xl))
			   (if (= (aref move i) ?x)
			       (setq i (1+ i)))
			   (if (/= (downcase (aref move i))
				   (aref chess-move-string x))
			       (setq match nil i l)
			     (setq i (1+ i) x (1+ x)))))
		       (if match move))))
		  chess-legal-moves)))
      (setq moves (delq nil moves))
      (cond
       ((= (length moves) 1)
	(chess-display-move nil (chess-algebraic-to-ply
				 (chess-display-position nil) (car moves)
				 (chess-display-search-function nil)))
	(setq chess-move-string nil
	      chess-legal-moves nil
	      chess-legal-moves-pos nil))
       ((null moves)
	(chess-keyboard-shortcut-delete))
       (t
	(message "[%s] %s" chess-move-string
		 (mapconcat 'identity moves " ")))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Manage a face cache for textual displays
;;

(defvar chess-display-face-cache '((t . t)))

(defsubst chess-display-get-face (color)
  (or (cdr (assoc color chess-display-face-cache))
      (let ((face (make-face 'chess-display-highlight)))
	(set-face-attribute face nil :background color)
	(add-to-list 'chess-display-face-cache (cons color face))
	face)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Mousing around on the chess-display
;;

(defvar chess-display-last-selected nil)
(make-variable-buffer-local 'chess-display-last-selected)

(defun chess-display-select-piece ()
  "Select the piece under the cursor.
Clicking once on a piece selects it; then click on the target location."
  (interactive)
  (let ((coord (get-text-property (point) 'chess-coord)))
    (when coord
      (if chess-display-last-selected
	  (let ((last-sel chess-display-last-selected))
	    ;; if they select the same square again, just deselect it
	    (if (/= (point) (car last-sel))
		(chess-display-move
		 nil (chess-ply-create (chess-display-position nil)
				       (cadr last-sel) coord))
	      ;; put the board back to rights
	      (chess-display-update nil))
	    (setq chess-display-last-selected nil))
	(setq chess-display-last-selected (list (point) coord))
	;; just as in a normal chess game, if you touch the piece,
	;; your opponent will see this right away
	(chess-display-highlight nil coord 'selected)))))

(defun chess-display-mouse-select-piece (event)
  "Select the piece the user clicked on."
  (interactive "e")
  (cond ((fboundp 'event-window)	; XEmacs
	 (set-buffer (window-buffer (event-window event)))
	 (and (event-point event) (goto-char (event-point event))))
	((fboundp 'posn-window)		; Emacs
	 (set-buffer (window-buffer (posn-window (event-start event))))
	 (goto-char (posn-point (event-start event)))))
  (chess-display-select-piece))

(provide 'chess-display)

;;; chess-display.el ends here