* lisp/play/5x5.el: I/ Add an arithmetic solver to suggest positions to
[bpt/emacs.git] / lisp / play / gametree.el
index bc82261..4d514d2 100644 (file)
@@ -1,7 +1,6 @@
 ;;; gametree.el --- manage game analysis trees in Emacs
 
-;; Copyright (C) 1997, 1999, 2001, 2002, 2003, 2004,
-;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+;; Copyright (C) 1997, 1999, 2001-2011  Free Software Foundation, Inc.
 
 ;; Author: Ian T Zimmerman <itz@rahul.net>
 ;; Created: Wed Dec 10 07:41:46 PST 1997
@@ -92,7 +91,7 @@
   :version "20.3")
 
 (defcustom gametree-half-ply-regexp (regexp-quote ":")
-  "*Matches ends of numbers of moves by the \"second\" player.
+  "Matches ends of numbers of moves by the \"second\" player.
 For instance, it is an almost universal convention in chess to postfix
 numbers of moves by Black (if considered in isolation) by the ellipsis
 \"...\".  This is NOT a good choice for this program, though, because it
@@ -103,20 +102,20 @@ LaTeX macros he uses for typesetting annotated games."
   :group 'gametree)
 
 (defcustom gametree-full-ply-regexp (regexp-quote ".")
-  "*Matches ends of numbers of moves by the \"first\" player.
+  "Matches ends of numbers of moves by the \"first\" player.
 For instance, it is an almost universal convention in chess to postfix
 numbers of moves by White (if considered in isolation) by the dot \".\"."
   :type 'regexp
   :group 'gametree)
 
 (defcustom gametree-half-ply-format "%d:"
-  "*Output format for move numbers of moves by the \"second\" player.
+  "Output format for move numbers of moves by the \"second\" player.
 Has to contain \"%d\" to output the actual number."
   :type 'string
   :group 'gametree)
 
 (defcustom gametree-full-ply-format "%d."
-  "*Output format for move numbers of moves by the \"first\" player.
+  "Output format for move numbers of moves by the \"first\" player.
 Has to contain \"%d\" to output the actual number."
   :type 'string
   :group 'gametree)
@@ -138,17 +137,17 @@ the file is visited (subject to the usual restriction via
 `enable-local-variables'), and the layout will be set accordingly.")
 
 (defcustom gametree-score-opener "{score="
-  "*The string which opens a score tag, and precedes the actual score."
+  "The string which opens a score tag, and precedes the actual score."
   :type 'string
   :group 'gametree)
 
 (defcustom gametree-score-manual-flag "!"
-  "*String marking the line as manually (as opposed to automatically) scored."
+  "String marking the line as manually (as opposed to automatically) scored."
   :type 'string
   :group 'gametree)
 
 (defcustom gametree-score-closer "}"
-  "*The string which closes a score tag, and follows the actual score."
+  "The string which closes a score tag, and follows the actual score."
   :type 'string
   :group 'gametree)
 
@@ -160,7 +159,7 @@ the file is visited (subject to the usual restriction via
           "[   ]*\\)?\\([-+]?[0-9]+\\)"
           (regexp-quote gametree-score-closer)
           "[   ]*\\)[\n\^M]")
-  "*Regular expression matching lines that guide the program in scoring.
+  "Regular expression matching lines that guide the program in scoring.
 Its third parenthetical group should match the actual score.  Its
 first parenthetical group should match the entire score tag.  Its
 second parenthetical group should be an optional flag that marks the
@@ -172,7 +171,7 @@ score instead."
   :group 'gametree)
 
 (defcustom gametree-default-score 0
-  "*Score to assume for branches lacking score tags."
+  "Score to assume for branches lacking score tags."
   :type 'integer
   :group 'gametree)
 \f
@@ -201,7 +200,7 @@ should be no leading white space."
     (let ((boundary (concat "[ \t]*\\([1-9][0-9]*\\)\\("
                             gametree-full-ply-regexp "\\|"
                             gametree-half-ply-regexp "\\)"))
-          (limit (save-excursion (beginning-of-line 1) (point))))
+          (limit (line-beginning-position 1)))
       (if (looking-at boundary)
           (+ (* 2 (string-to-number (match-string 1)))
              (if (string-match gametree-half-ply-regexp (match-string 2)) 1 0))
@@ -259,23 +258,20 @@ This value is simply the outline heading level of the current line."
 
 (defun gametree-children-shown-p ()
   (save-excursion
-    (condition-case nil
+    (ignore-errors
         (let ((depth (gametree-current-branch-depth)))
           (outline-next-visible-heading 1)
-          (< depth (gametree-current-branch-depth)))
-      (error nil))))
+          (< depth (gametree-current-branch-depth))))))
 
-(defun gametree-current-layout (depth &optional top-level)
+(defun gametree-current-layout (depth &optional from-top-level)
   (let ((layout nil) (first-time t))
     (while (save-excursion
-             (condition-case nil
-                 (progn
-                   (or (and first-time top-level
-                            (bolp) (looking-at outline-regexp))
-                       (setq first-time nil)
-                       (outline-next-visible-heading 1))
-                   (< depth (gametree-current-branch-depth)))
-               (error nil)))
+             (ignore-errors
+               (or (and first-time from-top-level
+                        (bolp) (looking-at outline-regexp))
+                   (setq first-time nil)
+                   (outline-next-visible-heading 1))
+               (< depth (gametree-current-branch-depth))))
       (if (not first-time)
           (outline-next-visible-heading 1))
       (setq first-time nil)
@@ -298,18 +294,16 @@ This value is simply the outline heading level of the current line."
     (goto-char (point-min))
     (setq gametree-local-layout (gametree-current-layout 0 t))))
 
-(defun gametree-apply-layout (layout depth &optional top-level)
+(defun gametree-apply-layout (layout depth &optional from-top-level)
   (let ((first-time t))
     (while (and layout
                 (save-excursion
-                  (condition-case nil
-                      (progn
-                        (or (and first-time top-level
-                                 (bolp) (looking-at outline-regexp))
-                            (setq first-time nil)
-                            (outline-next-visible-heading 1))
-                        (< depth (gametree-current-branch-depth)))
-                    (error nil))))
+                  (ignore-errors
+                    (or (and first-time from-top-level
+                             (bolp) (looking-at outline-regexp))
+                        (setq first-time nil)
+                        (outline-next-visible-heading 1))
+                    (< depth (gametree-current-branch-depth)))))
       (if (not first-time)
           (outline-next-visible-heading 1))
       (setq first-time nil)
@@ -376,9 +370,7 @@ Subnodes which have been manually scored are honored."
             (while (not done)           ;handle subheadings
               (setq running (funcall minmax running
                                      (gametree-compute-reduced-score)))
-              (setq done (condition-case nil
-                             (outline-forward-same-level 1)
-                           (error nil)))))
+              (setq done (ignore-errors (outline-forward-same-level 1)))))
           running)))))
 \f
 ;;;; Commands
@@ -566,6 +558,20 @@ buffer, it is replaced by the new value.  See the documentation for
     (gametree-hack-file-layout))
   nil)
 
+;;;; Key bindings
+(defvar gametree-mode-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map "\C-c\C-j" 'gametree-break-line-here)
+    (define-key map "\C-c\C-v" 'gametree-insert-new-leaf)
+    (define-key map "\C-c\C-m" 'gametree-merge-line)
+    (define-key map "\C-c\C-r " 'gametree-layout-to-register)
+    (define-key map "\C-c\C-r/" 'gametree-layout-to-register)
+    (define-key map "\C-c\C-rj" 'gametree-apply-register-layout)
+    (define-key map "\C-c\C-y" 'gametree-save-and-hack-layout)
+    (define-key map "\C-c;" 'gametree-insert-score)
+    (define-key map "\C-c^" 'gametree-compute-and-insert-score)
+    map))
+
 (define-derived-mode gametree-mode outline-mode "GameTree"
   "Major mode for managing game analysis trees.
 Useful to postal and email chess (and, it is hoped, also checkers, go,
@@ -576,18 +582,6 @@ shogi, etc.) players, it is a slightly modified version of Outline mode.
   (make-local-variable 'write-contents-hooks)
   (add-hook 'write-contents-hooks 'gametree-save-and-hack-layout))
 
-;;;; Key bindings
-
-(define-key gametree-mode-map "\C-c\C-j" 'gametree-break-line-here)
-(define-key gametree-mode-map "\C-c\C-v" 'gametree-insert-new-leaf)
-(define-key gametree-mode-map "\C-c\C-m" 'gametree-merge-line)
-(define-key gametree-mode-map "\C-c\C-r " 'gametree-layout-to-register)
-(define-key gametree-mode-map "\C-c\C-r/" 'gametree-layout-to-register)
-(define-key gametree-mode-map "\C-c\C-rj" 'gametree-apply-register-layout)
-(define-key gametree-mode-map "\C-c\C-y" 'gametree-save-and-hack-layout)
-(define-key gametree-mode-map "\C-c;" 'gametree-insert-score)
-(define-key gametree-mode-map "\C-c^" 'gametree-compute-and-insert-score)
-
 ;;;; Goodies for mousing users
 (and (fboundp 'track-mouse)
      (defun gametree-mouse-break-line-here (event)
@@ -617,5 +611,4 @@ shogi, etc.) players, it is a slightly modified version of Outline mode.
 
 (provide 'gametree)
 
-;; arch-tag: aaa30943-9ae4-4cc1-813d-a46f96b7e4f1
 ;;; gametree.el ends here