1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;; Plain ASCII chess display
6 (require 'chess-display)
10 (defgroup chess-plain nil
11 "A minimal, customizable ASCII display."
12 :group 'chess-display)
14 (defcustom chess-plain-draw-border nil
15 "*Non-nil if a border should be drawn (using `chess-plain-border-chars')."
19 (defcustom chess-plain-border-chars '(?+ ?- ?+ ?| ?| ?+ ?- ?+)
20 "*Characters used to draw borders."
22 :type '(list (character :tag "Upper left corner")
23 (character :tag "Upper border")
24 (character :tag "Upper right corner")
25 (character :tag "Left border")
26 (character :tag "Right border")
27 (character :tag "Lower left corner")
28 (character :tag "Lowwer border")
29 (character :tag "Lower right corner")))
31 (defcustom chess-plain-black-square-char ?.
32 "*Character used to indicate black squares."
36 (defcustom chess-plain-white-square-char ?.
37 "*Character used to indicate white squares."
41 (defcustom chess-plain-piece-chars
54 "*Alist of pieces and their corresponding characters."
56 :type '(alist :key-type (character :tag "Internal representation")
57 :value-type (character :tag "Printed representation")))
59 (defcustom chess-plain-upcase-indicates 'color
60 "*Defines what a upcase char should indicate.
61 The default is 'color, meaning a upcase char is a white piece, a
62 lowercase char a black piece. Possible values: 'color (default),
63 'square-color. If set to 'square-color, a uppercase character
64 indicates a piece on a black square. (Note that you also need to
65 modify `chess-plain-piece-chars' to avoid real confusion.)"
67 :type '(choice (const color) (const square-color)))
69 (defcustom chess-plain-spacing 1
70 "*Number of spaces between pieces."
74 (defface chess-plain-black-face
75 '((((class color) (background light)) (:foreground "Green"))
76 (((class color) (background dark)) (:foreground "Green"))
78 "*The face used for black pieces on the ASCII display."
81 (defface chess-plain-white-face
82 '((((class color) (background light)) (:foreground "Yellow"))
83 (((class color) (background dark)) (:foreground "Yellow"))
85 "*The face used for white pieces on the ASCII display."
88 (defface chess-plain-highlight-face
89 '((((class color) (background light)) (:background "#add8e6"))
90 (((class color) (background dark)) (:background "#add8e6")))
91 "Face to use for highlighting pieces that have been selected."
94 (defcustom chess-plain-popup-function 'chess-plain-popup
95 "The function used to popup a chess-plain display."
99 (defcustom chess-plain-separate-frame nil
100 "*If non-nil, display the chessboard in its own frame."
106 (defun chess-plain-handler (event &rest args)
108 ((eq event 'initialize) t)
111 (funcall chess-plain-popup-function))
114 (apply 'chess-plain-draw args))
116 ((eq event 'draw-square)
117 (apply 'chess-plain-draw-square args))
119 ((eq event 'highlight)
120 (apply 'chess-plain-highlight args))))
122 (defun chess-plain-popup ()
123 (if chess-plain-separate-frame
124 (chess-display-popup-in-frame 9 (* (1+ chess-plain-spacing) 8) nil t)
125 (chess-display-popup-in-window)))
127 (defun chess-plain-piece-text (piece rank file)
128 (let ((white-square (= (% (+ file rank) 2) 0)))
131 chess-plain-white-square-char
132 chess-plain-black-square-char)
133 (let* ((pchar (cdr (assq piece chess-plain-piece-chars)))
135 (if (eq chess-plain-upcase-indicates 'square-color)
140 (add-text-properties 0 1 (list 'face (if (> piece ?a)
141 'chess-plain-black-face
142 'chess-plain-white-face)) p)
145 (defsubst chess-plain-draw-square (pos piece index)
146 "Draw a piece image at POS on an already drawn display."
150 (insert (chess-plain-piece-text piece (chess-index-rank index)
151 (chess-index-file index)))
152 (add-text-properties pos (point) (list 'chess-coord index))))
154 (defun chess-plain-draw (position perspective)
155 "Draw the given POSITION from PERSPECTIVE's point of view.
156 PERSPECTIVE is t for white or nil for black."
157 (let ((inhibit-redisplay t)
160 (let* ((inverted (not perspective))
161 (rank (if inverted 7 0))
162 (file (if inverted 7 0)) beg)
163 (if chess-plain-draw-border
164 (insert ? (nth 0 chess-plain-border-chars)
165 (make-string (+ 8 (* 7 chess-plain-spacing))
166 (nth 1 chess-plain-border-chars))
167 (nth 2 chess-plain-border-chars) ?\n))
168 (while (if inverted (>= rank 0) (< rank 8))
169 (if chess-plain-draw-border
170 (insert (number-to-string (- 8 rank))
171 (nth 3 chess-plain-border-chars)))
172 (while (if inverted (>= file 0) (< file 8))
173 (let ((piece (chess-pos-piece position
174 (chess-rf-to-index rank file)))
176 (insert (chess-plain-piece-text piece rank file))
177 (add-text-properties begin (point)
179 (chess-rf-to-index rank file)))
180 (when (if inverted (>= file 1) (< file 7))
181 (insert (make-string chess-plain-spacing ? ))))
182 (setq file (if inverted (1- file) (1+ file))))
183 (if chess-plain-draw-border
184 (insert (nth 4 chess-plain-border-chars)))
186 (setq file (if inverted 7 0)
187 rank (if inverted (1- rank) (1+ rank))))
188 (if chess-plain-draw-border
189 (insert ? (nth 5 chess-plain-border-chars)
190 (make-string (+ 8 (* 7 chess-plain-spacing))
191 (nth 6 chess-plain-border-chars))
192 (nth 7 chess-plain-border-chars) ?\n
193 ? ? (if (not inverted) "abcdefgh" "hgfedcba")))
194 (set-buffer-modified-p nil)
197 (defun chess-plain-highlight (index &optional mode)
198 (let ((pos (chess-display-index-pos nil index)))
199 (put-text-property pos (1+ pos) 'face
202 'chess-plain-highlight-face)
204 (chess-display-get-face mode))))))
206 (provide 'chess-plain)
208 ;;; chess-plain.el ends here