/usr/share/emacs/site-lisp/chess/chess-pgn.el is in emacs-chess 2.0b6-1.1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Convert a chess game to/from PGN notation
;;
(require 'chess-algebraic)
(require 'chess-fen)
(require 'chess-message)
(defvar chess-pgn-fill-column 60)
(chess-message-catalog 'english
'((pgn-read-error . "Error reading move: %s")
(pgn-parse-error . "Error parsing PGN syntax")))
(defun chess-pgn-read-plies (game position &optional top-level)
(let ((plies (list t)) prevpos)
(catch 'done
(while (not (eobp))
(cond
((looking-at "[1-9][0-9]*\\.[. ]*")
(goto-char (match-end 0)))
((looking-at chess-algebraic-regexp)
(goto-char (match-end 0))
(setq prevpos position)
(let* ((move (match-string-no-properties 0))
(ply (chess-algebraic-to-ply position move)))
(unless ply
(chess-error 'pgn-read-error move))
(setq position (chess-ply-next-pos ply))
(nconc plies (list ply))))
((and top-level
(looking-at "\\(\\*\\|1-0\\|0-1\\|1/2-1/2\\)"))
(goto-char (match-end 0))
(chess-game-set-tag game "Result" (match-string-no-properties 0))
(unless (eq t (car (last plies)))
(cond
((string= "1/2-1/2" (match-string 1))
(nconc plies (list (chess-ply-create
(chess-ply-next-pos (car (last plies)))
t :drawn))))
(t
(nconc plies (list (chess-ply-create*
(chess-ply-next-pos (car (last plies)))))))))
(throw 'done t))
((looking-at "{")
(forward-char)
(let ((begin (point)))
(search-forward "}")
(forward-char)
(chess-pos-add-annotation position (buffer-substring-no-properties
begin (- (point) 2)))))
((looking-at "(")
(forward-char)
(skip-chars-forward " \t\n")
(chess-pos-add-annotation prevpos
(chess-pgn-read-plies game prevpos)))
((and (not top-level)
(looking-at ")"))
(forward-char)
(throw 'done t))
(t
(nconc plies (list (chess-ply-create*
(chess-ply-next-pos (car (last plies))))))
(throw 'done t)))
(skip-chars-forward " \t\n\r")))
(cdr plies)))
(defun chess-pgn-to-game (&optional string)
"Convert PGN notation at point into a chess game.
Optionally use the supplied STRING instead of the current buffer."
(if string
(with-temp-buffer
(insert string)
(goto-char (point-min))
(chess-pgn-parse))
(chess-pgn-parse)))
(defun chess-pgn-parse ()
(when (or (looking-at "\\[")
(and (search-forward "[" nil t)
(goto-char (match-beginning 0))))
(let ((game (chess-game-create)))
(chess-game-set-tags game nil)
(while (looking-at "\\[\\(\\S-+\\)\\s-+\\(\".+?\"\\)\\][ \t\n\r]+")
(chess-game-set-tag game (match-string-no-properties 1)
(read (match-string-no-properties 2)))
(goto-char (match-end 0)))
(let ((fen (chess-game-tag game "FEN")) plies)
(if fen
(chess-game-set-start-position game (chess-fen-to-pos fen)))
(setq plies (chess-pgn-read-plies game (chess-game-pos game) t))
(if plies
(chess-game-set-plies game plies)))
game)))
(defun chess-pgn-insert-annotations (game index ply)
(dolist (ann (chess-pos-annotations (chess-ply-pos ply)))
(if (stringp ann)
(insert "\n{" ann "}")
(assert (listp ann))
(chess-pgn-insert-plies game index ann))))
(defun chess-pgn-insert-plies (game index plies &optional
for-black indented no-annotations)
"NYI: Still have to implement INDENTED argument."
(while plies
(unless for-black
(when (chess-ply-changes (car plies))
(if (> (current-column) chess-pgn-fill-column)
(insert ?\n))
(insert (format "%d. %s" index (chess-ply-to-algebraic (car plies))))
(unless no-annotations
(chess-pgn-insert-annotations game index (car plies))))
(setq plies (cdr plies) index (1+ index)))
(when plies
(when (chess-ply-changes (car plies))
(when for-black
(if (> (current-column) chess-pgn-fill-column)
(insert ?\n))
(insert (format "%d. ..." index))
(setq for-black nil))
(insert (format " %s" (chess-ply-to-algebraic (car plies))))
(unless no-annotations
(chess-pgn-insert-annotations game index (car plies))))
(setq plies (cdr plies)))
(if plies
(insert ? ))))
(defvar chess-pgn-tag-order
'("Event" "Site" "Date" "Round"
"White" "WhiteElo" "Black" "BlackElo"
"Result" "TimeControl"))
(defun chess-game-to-pgn (game &optional indented to-string)
"Convert a chess GAME to PGN notation.
If INDENTED is non-nil, indent the move texts.
If TO-STRING is non-nil, return a string instead of inserting the resulting
PGN text."
(if to-string
(with-temp-buffer
(chess-insert-pgn game indented)
(buffer-string))
(chess-insert-pgn game indented)))
(defun chess-member-index (tag)
(let ((index 0)
(tags chess-pgn-tag-order))
(while tags
(if (equal tag (car tags))
(setq tags nil)
(setq index (1+ index)
tags (cdr tags))))
index))
(defun chess-insert-pgn (game &optional indented)
(let ((fen (chess-game-tag game "FEN"))
(first-pos (chess-game-pos game 0)))
(when (and fen (not (string= fen (chess-pos-to-fen first-pos))))
(chess-game-del-tag game "FEN")
(setq fen nil))
(if (and (not fen)
(not (eq chess-starting-position first-pos)))
(chess-game-set-tag game "FEN" (chess-pos-to-fen first-pos))))
(dolist (tag (sort (copy-alist (chess-game-tags game))
(function
(lambda (left right)
(setq left (car left) right (car right))
(let ((l-idx (chess-member-index left))
(r-idx (chess-member-index right)))
(cond
((and l-idx (not r-idx)) t)
((and (not l-idx) r-idx) nil)
((and l-idx r-idx) (< l-idx r-idx))
(t (string-lessp left right))))))))
(insert (format "[%s \"%s\"]\n" (car tag) (cdr tag))))
(insert ?\n)
(let ((begin (point)))
(chess-pgn-insert-plies game 1 (chess-game-plies game))
(insert (or (chess-game-tag game "Result") "*") ?\n)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; chess-pgn-mode for editing and browsing PGN files.
;;
(require 'chess-database)
(require 'chess-file)
(defvar chess-pgn-database nil
"Chess database object.")
(make-variable-buffer-local 'chess-pgn-database)
(defvar chess-pgn-display nil
"If non-nil, the chess display object used for this buffer.")
(make-variable-buffer-local 'chess-pgn-display)
(defvar chess-pgn-current-game)
(defvar chess-pgn-current-index)
(make-variable-buffer-local 'chess-pgn-current-game)
(make-variable-buffer-local 'chess-pgn-current-index)
(chess-message-catalog 'english
'((could-not-read-pgn . "Could not read or find a PGN game")))
;;;###autoload
(defun chess-pgn-read (&optional file)
"Read and display a PGN game after point."
(interactive "P")
(if (or file (not (search-forward "[Event " nil t)))
(setq file (read-file-name "Read a PGN game from file: ")))
(if file
(find-file file))
(let ((game (chess-pgn-to-game)))
(if game
(chess-display-set-game
(setq chess-pgn-display (chess-create-display t))
game)
(chess-error 'could-not-read-pgn))))
;;;###autoload
(define-derived-mode chess-pgn-mode text-mode "PGN"
"A mode for editing chess PGN files."
(make-variable-buffer-local 'comment-start)
(make-variable-buffer-local 'comment-end)
(setq comment-start "{"
comment-end "}")
(modify-syntax-entry ?\{ "<")
(modify-syntax-entry ?\} ">")
(modify-syntax-entry ?\" "\"")
(if (fboundp 'font-lock-mode)
(font-lock-mode 1))
(let ((map (current-local-map)))
(define-key map [(control ?c) (control ?c)] 'chess-pgn-show-position)
(define-key map [mouse-2] 'chess-pgn-mouse-show-position)
;;(define-key map [(control ?m)] 'chess-pgn-move)
;;(define-key map [space] 'chess-pgn-move)
;;(define-key map [? ] 'chess-pgn-move)
(when (require 'pcomplete nil t)
(set (make-variable-buffer-local 'pcomplete-default-completion-function)
'chess-pgn-completions)
(set (make-variable-buffer-local 'pcomplete-command-completion-function)
'chess-pgn-completions)
(set (make-variable-buffer-local 'pcomplete-parse-arguments-function)
'chess-pgn-current-word)
(define-key map [tab] 'chess-pgn-complete-move))))
;;;###autoload
(defalias 'pgn-mode 'chess-pgn-mode)
(defvar chess-pgn-bold-face 'bold)
(defconst chess-pgn-move-regexp
(concat "[^0-9]\\(\\([1-9][0-9]*\\)\\.\\s-+"
"\\(\\.\\.\\.\\|" chess-algebraic-regexp "\\)"
"\\(\\s-+\\(" chess-algebraic-regexp "\\)\\)?\\)"))
(if (fboundp 'font-lock-add-keywords)
(font-lock-add-keywords
'chess-pgn-mode
(list (list "\\[\\(\\S-+\\)\\s-+\".*\"\\]" 1 'font-lock-keyword-face)
(cons "\\(1-0\\|0-1\\|1/2-1/2\\|\\*\\)$" 'chess-pgn-bold-face))))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.pgn\\'" . chess-pgn-mode))
(eval-after-load "mm-decode"
'(unless (fboundp 'mm-display-pgn-inline)
(defun mm-display-pgn-inline (handle)
(mm-display-inline-fontify handle 'chess-pgn-mode))
(push '("application/x-chess-pgn" mm-display-pgn-inline identity)
mm-inline-media-tests)
(push "application/x-chess-pgn" mm-inlined-types)
(push "application/x-chess-pgn" mm-automatic-display)))
(defun chess-pgn-completions ()
"Return a list of possible completions for the current move."
(let ((position (chess-game-pos chess-pgn-current-game
chess-pgn-current-index)))
(while (pcomplete-here
(mapcar 'chess-ply-to-algebraic
(chess-legal-plies position :color
(chess-pos-side-to-move position)))))))
(defun chess-pgn-current-word ()
(let ((here (point)))
(if (setq chess-pgn-current-index (chess-pgn-index))
(save-restriction
(narrow-to-region (match-beginning 3) here)
(pcomplete-parse-buffer-arguments)))))
(defun chess-pgn-complete-move ()
(interactive)
(save-restriction
(narrow-to-region (point-min) (point))
(chess-pgn-read-game))
(if (eq last-command 'chess-pgn-complete-move)
(setq last-command 'pcomplete))
(call-interactively 'pcomplete))
(defun chess-pgn-index (&optional location)
"Return the move index associated with point."
(save-excursion
(when location (goto-char location))
(if (re-search-backward chess-pgn-move-regexp nil t)
(let* ((index (string-to-int (match-string 2)))
(first-move (match-string 3))
(second-move (match-string 14))
(ply (1+ (* 2 (1- index)))))
(if second-move
(setq ply (1+ ply)))
ply))))
(defun chess-pgn-read-game ()
"Load a database to represent this file if not already up."
(unless chess-pgn-database
(setq chess-pgn-database
(chess-database-open buffer-file-name 'chess-file)))
;; a hack for speed's sake to read the current game text
(save-excursion
(let ((locations chess-file-locations)
(here (point))
last-location index)
(while locations
(if (> (car locations) here)
(setq locations nil)
(setq last-location locations
locations (cdr locations))))
(setq index (if last-location
(- (length chess-file-locations) (length last-location))
0))
(when (or (null chess-pgn-current-game)
(/= index (chess-game-data chess-pgn-current-game
'database-index)))
(setq chess-pgn-current-game
(chess-database-read chess-pgn-database index))))))
(defun chess-pgn-create-display ()
"Return the move index associated with point."
;; now find what position we're at in the game
(save-excursion
(when chess-pgn-current-game
(let ((index (chess-pgn-index)))
(if (or (and (or (null chess-pgn-display)
(not (buffer-live-p chess-pgn-display)))
(let ((chess-game-inhibit-events t))
(setq chess-pgn-display (chess-create-display t))))
(/= (chess-game-data chess-pgn-current-game 'database-index)
(or (chess-game-data (chess-display-game chess-pgn-display)
'database-index) -1)))
(progn
(chess-display-disable-popup chess-pgn-display)
(chess-display-set-game chess-pgn-display
chess-pgn-current-game index)
(chess-game-set-tag (chess-display-game chess-pgn-display)
'database-index
(chess-game-data chess-pgn-current-game
'database-index)))
(chess-display-set-index chess-pgn-display index))
(chess-display-popup chess-pgn-display)))))
(defun chess-pgn-visualize ()
"Visualize the move for the PGN game under point.
This does not require that the buffer be in PGN mode."
(let (game)
(save-excursion
(if (search-backward "[Event " nil t)
(setq game (chess-pgn-to-game))))
(if game
(let ((chess-pgn-current-game game))
(chess-pgn-show-position))
(chess-error 'could-not-read-pgn))))
(defun chess-pgn-show-position ()
(interactive)
(if (not (eq major-mode 'chess-pgn-mode))
(chess-pgn-visualize)
(chess-pgn-read-game)
(chess-pgn-create-display)))
(defun chess-pgn-mouse-show-position (event)
(interactive "e")
(if (fboundp 'event-window) ; XEmacs
(progn
(set-buffer (window-buffer (event-window event)))
(and (event-point event) (goto-char (event-point event))))
(set-buffer (window-buffer (posn-window (event-start event))))
(goto-char (posn-point (event-start event))))
(chess-pgn-show-position))
(provide 'chess-pgn)
;;; chess-pgn.el ends here
|