annotate lisp/play/gametree.el @ 23958:7094b74ce0a6

(lisp_malloc, lisp_free): New functions. Use them instead of malloc, xmalloc, and xfree, for Lisp objects. Don't set allocating_for_lisp in the callers; let lisp_malloc do it. (n_interval_blocks, n_float_blocks): New variable. (n_cons_blocks, n_vectors, n_symbol_blocks): New variable. (n_marker_blocks, n_string_blocks): New variable. (init_intervals, make_interval): Set a count variable. Use lisp_malloc instead of setting allocating_for_lisp. (init_float, make_float, init_cons, Fcons): Likewise. (allocate_vectorlike, init_symbol, Fmake_symbol): Likewise (init_marker, allocate_misc, init_strings): Likewise. (make_uninit_multibyte_string): Likewise. (gc_sweep, compact_strings): Decrement the count variables. (uninterrupt_malloc): Don't store Emacs's hooks into the old_..._hook variables.
author Richard M. Stallman <rms@gnu.org>
date Wed, 30 Dec 1998 01:07:49 +0000
parents 86fcccceba7b
children 985eb547550f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20782
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1 ;;; gametree.el --- manage game analysis trees in Emacs
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
3 ;; Copyright (C) 1997 Free Software Foundation, Inc
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
4
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5 ;; Author: Ian T Zimmerman <itz@rahul.net>
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
6 ;; Created: Wed Dec 10 07:41:46 PST 1997
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
7 ;; Keywords: games
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9 ;; This file is part of GNU Emacs.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14 ;; any later version.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19 ;; GNU General Public License for more details.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
24 ;; Boston, MA 02111-1307, USA.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
25
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26 ;;; Commentary:
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28 ;; This little hack has enabled me to keep track of my email chess
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29 ;; games in Emacs. For a long time I dreamt about writing a real,
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30 ;; graphical tree editor; but, then the idea struck me, why do it
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 ;; graphically, when it can be done in Emacs? :-) And in fact Emacs
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32 ;; almost had what I needed out of the box, namely the powerful
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
33 ;; Outline mode. This code is built entirely on Outline mode, it
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34 ;; only adds two commands that I found indispensable when dealing
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
35 ;; with the special kind of trees that analysis trees comprise.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37 ;; The built-in documentation should be enough to explain the use,
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
38 ;; along with the following example (yes, this is a real game).
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40 ;; *** 23. f4 ef 24. Nf3 Rf3 -/+
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41 ;; 25. Rf3 Qh2 26. Kh1 Qh1 27. Ke2 Qg2 -+
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42 ;; ******* 25. gf3 Nce3 26. Qe2 Nf1 -/+
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 ;; 27. Qe5 Ne5 28. Kf1 Nf3 -/+
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 ;; 27. Kf1 Nh2 28. Kf2 Ng4 29. fg4 Rf8 30. Ke1 Qg3 31. Kd1 Rf2 -+
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46 ;; Place the preceding in a scratch buffer, load this code, and do
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 ;; M-x gametree-mode. Now place the cursor just after the `Nf3' and
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 ;; before the `Rf3' on the first line, and do C-c C-j. The result is
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50 ;; *** 23. f4 ef 24. Nf3
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 ;; ****** 24: Rf3 -/+
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 ;; 25. Rf3 Qh2 26. Kh1 Qh1 27. Ke2 Qg2 -+
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53 ;; ******* 25. gf3 Nce3 26. Qe2 Nf1 -/+
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54 ;; 27. Qe5 Ne5 28. Kf1 Nf3 -/+
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 ;; 27. Kf1 Nh2 28. Kf2 Ng4 29. fg4 Rf8 30. Ke1 Qg3 31. Kd1 Rf2 -+
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57 ;; Now you can add another subvariation on Black's 24th move: with
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 ;; the cursor still on the first line, do C-c C-v, and voila
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 ;; *** 23. f4 ef 24. Nf3
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 ;; 24:
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 ;; ****** 24: Rf3 -/+
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 ;; 25. Rf3 Qh2 26. Kh1 Qh1 27. Ke2 Qg2 -+
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 ;; ******* 25. gf3 Nce3 26. Qe2 Nf1 -/+
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65 ;; 27. Qe5 Ne5 28. Kf1 Nf3 -/+
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 ;; 27. Kf1 Nh2 28. Kf2 Ng4 29. fg4 Rf8 30. Ke1 Qg3 31. Kd1 Rf2 -+
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 ;; and the cursor is positioned on the new line just after the move
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 ;; number, so you can start typing the new analysis. That's it,
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70 ;; quite simple. If you want more, read on.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
71
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72 ;;; ToDo:
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74 ;; BIG: automatic score reducer. It should be possible to label the
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 ;; leaf variations with numeric scores (instead of the Informant-like
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76 ;; symbols used in the example) and have the program apply the
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 ;; min-max algorithm to score the internal nodes. That's about as
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78 ;; far as one can go in a postal game while honestly claiming not to
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
79 ;; use computer analysis.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
80
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81 ;; I'd definitely like to hear from you if you use this, and even
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
82 ;; more if you have suggestions for improvement, ranging from bug
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
83 ;; reports to feature requests. (But be warned that I am a fan of
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84 ;; simplicity and orthogonality).
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
85
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
86 ;;; Code:
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
87
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
88 (require 'derived)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
89 (require 'outline)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
90
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91 ;;;; Configuration variables
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
92
21363
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
93 (defgroup gametree nil
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
94 "Manage game analysis trees in Emacs."
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
95 :prefix "gametree-"
21651
86fcccceba7b *** empty log message ***
Dan Nicolaescu <done@ece.arizona.edu>
parents: 21363
diff changeset
96 :group 'games
86fcccceba7b *** empty log message ***
Dan Nicolaescu <done@ece.arizona.edu>
parents: 21363
diff changeset
97 :version "20.3")
21363
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
98
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
99 (defcustom gametree-half-ply-regexp (regexp-quote ":")
20782
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100 "*Matches ends of numbers of moves by the \"second\" player.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101 For instance, it is an almost universal convention in chess to postfix
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102 numbers of moves by Black (if considered in isolation) by the ellipsis
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103 \"...\". This is NOT a good choice for this program, though, because it
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
104 conflicts with the use of ellipsis by Outline mode to denote collapsed
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 subtrees. The author uses \":\" because it agrees nicely with a set of
21363
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
106 LaTeX macros he uses for typesetting annotated games."
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
107 :type 'regexp
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
108 :group 'gametree)
20782
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109
21363
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
110 (defcustom gametree-full-ply-regexp (regexp-quote ".")
20782
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
111 "*Matches ends of numbers of moves by the \"first\" player.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112 For instance, it is an almost universal convention in chess to postfix
21363
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
113 numbers of moves by White (if considered in isolation) by the dot \".\"."
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
114 :type 'regexp
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
115 :group 'gametree)
20782
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116
21363
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
117 (defcustom gametree-half-ply-format "%d:"
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
118 "*Output format for move numbers of moves by the \"second\" player.
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
119 Has to contain \"%d\" to output the actual number."
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
120 :type 'string
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
121 :group 'gametree)
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
122
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
123 (defcustom gametree-full-ply-format "%d."
20782
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124 "*Output format for move numbers of moves by the \"first\" player.
21363
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
125 Has to contain \"%d\" to output the actual number."
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
126 :type 'string
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
127 :group 'gametree)
20782
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128
21363
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
129 (defcustom gametree-make-heading-function
20782
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 (function (lambda (level)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
131 (insert (make-string level ?*))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
132 "A function of one numeric argument, LEVEL, to insert a heading at point.
21363
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
133 You should change this if you change `outline-regexp'."
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
134 :type 'function
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20782
diff changeset
135 :group 'gametree)
20782
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
136
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
137 (defvar gametree-local-layout nil
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
138 "A list encoding the layout (i.e. the show or hide state) of the file.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
139 If Emacs notices a local variable specification of this variable in
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140 the first line of the buffer while saving the buffer to the visited
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141 file, the local value will be saved there and restored the next time
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142 the file is visited (subject to the usual restriction via
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 `enable-local-variables'), and the layout will be set accordingly.")
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 ;;;; Helper functions
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 (defun gametree-prettify-heading ()
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 "Insert/delete space between leading asterisks and heading text.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 If the current variation is an internal node (i.e. starts with one or
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 more asterisks), ensure there's at least one space between the
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 asterisks and the text. If on the other hand this is a leaf, there
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 should be no leading white space."
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 (beginning-of-line 1)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 (if (re-search-forward (concat "\\=" outline-regexp) nil t)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156 (if (not (looking-at "[ \t]+")) (insert " "))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 (delete-char (save-excursion (skip-chars-forward " \t"))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 (if (re-search-forward (concat "\\=[ \t]*[1-9][0-9]*\\("
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159 gametree-full-ply-regexp "\\|"
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 gametree-half-ply-regexp "\\)") nil t)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 (if (not (looking-at "[ \t]+")) (insert " ")
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162 (delete-char (1- (save-excursion (skip-chars-forward " \t"))))))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 (defun gametree-looking-at-ply ()
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 "Read and return the number of the ply under point."
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 (if (eobp) 0
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 (let ((boundary (concat "[ \t]*\\([1-9][0-9]*\\)\\("
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 gametree-full-ply-regexp "\\|"
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 gametree-half-ply-regexp "\\)"))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170 (limit (save-excursion (beginning-of-line 1) (point))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171 (if (looking-at boundary)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172 (+ (* 2 (string-to-int (match-string 1)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173 (if (string-match gametree-half-ply-regexp (match-string 2)) 1 0))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174 (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 (re-search-backward boundary limit)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 (skip-chars-backward "0123456789")
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 (1+ (* 2 (string-to-int
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178 (buffer-substring (point) (match-end 1))))))))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 (defun gametree-current-branch-ply ()
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181 "Return the ply number of the first move of the current variation."
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182 (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
183 (beginning-of-line 1)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
184 (re-search-forward (concat "\\=" outline-regexp) nil t)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185 (gametree-looking-at-ply)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187 (defun gametree-current-branch-depth ()
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 "Return the depth of the current variation in the analysis tree.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189 This value is simply the outline heading level of the current line."
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190 (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
191 (beginning-of-line 1)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
192 (if (looking-at outline-regexp)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193 (outline-level) 0)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195 ;;;; Functions related to the task of saving and restoring current
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196 ;;;; outline layout
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198 (defun gametree-show-children-and-entry ()
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
199 (show-children)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200 (show-entry))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 (defun gametree-entry-shown-p ()
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203 (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204 (forward-line 1)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 (and (bolp) (not (eobp)) (not (looking-at outline-regexp)))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207 (defun gametree-children-shown-p ()
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
208 (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
209 (condition-case nil
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
210 (let ((depth (gametree-current-branch-depth)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211 (outline-next-visible-heading 1)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212 (< depth (gametree-current-branch-depth)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
213 (error nil))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
215 (defun gametree-current-layout (depth &optional top-level)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
216 (let ((layout nil) (first-time t))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 (while (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218 (condition-case nil
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219 (progn
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
220 (or (and first-time top-level
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
221 (bolp) (looking-at outline-regexp))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
222 (setq first-time nil)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
223 (outline-next-visible-heading 1))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
224 (< depth (gametree-current-branch-depth)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 (error nil)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226 (if (not first-time)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227 (outline-next-visible-heading 1))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
228 (setq first-time nil)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
229 (if (not (gametree-children-shown-p))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 (setq layout
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 (nconc layout
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
232 (if (gametree-entry-shown-p)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
233 (list 'show-entry)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
234 (list nil))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
235 (setq layout (nconc layout (if (gametree-entry-shown-p)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236 (list 'gametree-show-children-and-entry)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237 (list 'show-children))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238 (let ((sub-layout
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
239 (gametree-current-layout (gametree-current-branch-depth))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
240 (setq layout (nconc layout (list sub-layout))))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
241 layout))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
242
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
243 (defun gametree-save-layout ()
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
244 (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
245 (goto-char (point-min))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
246 (setq gametree-local-layout (gametree-current-layout 0 t))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
247
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
248 (defun gametree-apply-layout (layout depth &optional top-level)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
249 (let ((first-time t))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250 (while (and layout
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
252 (condition-case nil
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253 (progn
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254 (or (and first-time top-level
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255 (bolp) (looking-at outline-regexp))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256 (setq first-time nil)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
257 (outline-next-visible-heading 1))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
258 (< depth (gametree-current-branch-depth)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
259 (error nil))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
260 (if (not first-time)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
261 (outline-next-visible-heading 1))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
262 (setq first-time nil)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
263 (hide-subtree)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
264 (if (nth 0 layout)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
265 (funcall (nth 0 layout)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
266 (if (not (and (nth 1 layout) (listp (nth 1 layout))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
267 (setq layout (cdr layout))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
268 (gametree-apply-layout (nth 1 layout)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
269 (gametree-current-branch-depth))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
270 (setq layout (cdr (cdr layout)))))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
271
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
272 (defun gametree-restore-layout ()
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
273 (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
274 (goto-char (point-min))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
275 (gametree-apply-layout gametree-local-layout 0 t)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
276
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
277 (defun gametree-hack-file-layout ()
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
278 (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
279 (goto-char (point-min))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
280 (if (looking-at "[^\n]*-\*-[^\n]*gametree-local-layout: \\([^;\n]*\\);")
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
281 (progn
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
282 (goto-char (match-beginning 1))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
283 (delete-region (point) (match-end 1))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
284 (let ((standard-output (current-buffer)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
285 (princ gametree-local-layout))))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
286
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
287 ;;;; Commands
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
288
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
289 (defun gametree-insert-new-leaf (&optional at-depth)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
290 "Start a new leaf variation under the current branching point.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
291 The new variation can later be split to be a branching point itself,
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
292 with \\[gametree-break-line-here]. If the point is currently on a
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
293 leaf variation, this command won't work; use \\[gametree-break-line-here]
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
294 on the current line first.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
295
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
296 With a numeric arg AT-DEPTH, first go up the tree until a node of
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
297 depth AT-DEPTH or smaller is found."
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
298 (interactive "P")
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
299 (if (zerop (gametree-current-branch-depth))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
300 (outline-up-heading 0))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
301 (if at-depth
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
302 (while (> (gametree-current-branch-depth)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
303 (prefix-numeric-value at-depth))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
304 (outline-up-heading 1)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
305 (beginning-of-line 1)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
306 (let ((parent-depth (gametree-current-branch-depth)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
307 (show-entry)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
308 (condition-case nil
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
309 (outline-next-visible-heading 1)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
310 (error
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
311 (goto-char (point-max))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
312 (if (not (bolp)) (insert "\n"))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
313 (let ((starting-plys
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
314 (if (> (gametree-current-branch-depth) parent-depth)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
315 (gametree-current-branch-ply)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
316 (save-excursion (forward-line -1)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
317 (gametree-current-branch-ply)))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
318 (goto-char (1- (point)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
319 (insert "\n")
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
320 (insert (format (if (= 0 (mod starting-plys 2))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
321 gametree-full-ply-format
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
322 gametree-half-ply-format)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
323 (/ starting-plys 2))))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
324
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
325 (defun gametree-break-line-here (&optional at-move)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
326 "Split the variation node at the point position.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
327 This command works whether the current variation node is a leaf, or is
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
328 already branching at its end. The new node is created at a level that
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
329 reflects the number of game plys between the beginning of the current
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
330 variation and the breaking point.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
331
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
332 With a numerical argument AT-MOVE, split the variation before
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
333 White's AT-MOVEth move, or Black's if negative. The last option will
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
334 only work of Black's moves are explicitly numbered, for instance
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
335 `1. e4 1: e5'."
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
336 (interactive "P")
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
337 (if at-move (progn
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
338 (end-of-line 1)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
339 (let ((limit (point)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
340 (beginning-of-line 1)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
341 (re-search-forward
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
342 (concat
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
343 (regexp-quote
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
344 (int-to-string (abs (prefix-numeric-value at-move))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
345 (if (> at-move 0) gametree-full-ply-regexp
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
346 gametree-half-ply-regexp)) limit))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
347 (goto-char (match-beginning 0))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
348 (let* ((pt (set-marker (make-marker) (point)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
349 (plys (gametree-current-branch-ply))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
350 (depth (gametree-current-branch-depth))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
351 (old-depth depth))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
352 (if (= depth 0)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
353 (progn
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
354 (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
355 (outline-previous-visible-heading 1)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
356 (setq depth
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
357 (let ((old-branch-ply
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
358 (condition-case nil
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
359 (gametree-current-branch-ply)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
360 (error 0))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
361 (if (zerop old-branch-ply)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
362 (1+ (gametree-current-branch-depth))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
363 (+ (gametree-current-branch-depth)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
364 (- plys old-branch-ply))))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
365 (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
366 (beginning-of-line 1)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
367 (funcall gametree-make-heading-function depth)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
368 (gametree-prettify-heading))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
369 (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
370 (if (not (looking-at (concat "[ \t]*[1-9][0-9]*\\("
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
371 gametree-full-ply-regexp "\\|"
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
372 gametree-half-ply-regexp "\\)")))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
373 (progn
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
374 (insert (format (if (= 0 (mod (gametree-looking-at-ply) 2))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
375 gametree-full-ply-format
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
376 gametree-half-ply-format)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
377 (/ (gametree-looking-at-ply) 2)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
378 (gametree-prettify-heading)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
379 (beginning-of-line 1)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
380 (goto-char pt)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
381 (insert "\n")
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
382 (if (not (= 0 old-depth))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
383 (funcall gametree-make-heading-function
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
384 (+ depth (- (gametree-current-branch-ply) plys))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
385 (gametree-prettify-heading))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
386
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
387 (defun gametree-merge-line ()
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
388 "Merges a variation with its only child.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
389 Does *not* check if the variation has in fact a unique child; users beware."
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
390 (interactive)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
391 (if (zerop (gametree-current-branch-depth))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
392 (outline-up-heading 0))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
393 (end-of-line 1)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
394 (let ((prev-depth (save-excursion (forward-line 1)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
395 (gametree-current-branch-depth))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
396 (delete-char (1+ prev-depth))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
397 (if (zerop prev-depth)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
398 (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
399 (beginning-of-line 1)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
400 (delete-char (gametree-current-branch-depth))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
401 (gametree-prettify-heading)))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
402
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
403 (defun gametree-layout-to-register (register)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
404 "Store current tree layout in register REGISTER.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
405 Use \\[gametree-apply-register-layout] to restore that configuration.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
406 Argument is a character, naming the register."
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
407 (interactive "cLayout to register: ")
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
408 (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
409 (goto-char (point-min))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
410 (set-register register
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
411 (gametree-current-layout 0 t))))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
412
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
413 (defun gametree-apply-register-layout (char)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
414 "Return to a tree layout stored in a register.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
415 Argument is a character, naming the register."
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
416 (interactive "cApply layout from register: ")
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
417 (save-excursion
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
418 (goto-char (point-min))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
419 (gametree-apply-layout (get-register char) 0 t)))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
420
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
421 (defun gametree-save-and-hack-layout ()
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
422 "Save the current tree layout and hack the file local variable spec.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
423 This function saves the current layout in `gametree-local-layout' and,
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
424 if a local file varible specification for this variable exists in the
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
425 buffer, it is replaced by the new value. See the documentation for
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
426 `gametree-local-layout' for more information."
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
427 (interactive)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
428 (gametree-save-layout)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
429 (gametree-hack-file-layout)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
430 nil)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
431
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
432 (define-derived-mode gametree-mode outline-mode "GameTree"
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
433 "Major mode for managing game analysis trees.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
434 Useful to postal and email chess (and, it is hoped, also checkers, go,
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
435 shogi, etc.) players, it is a slightly modified version of Outline mode.
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
436
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
437 \\{gametree-mode-map}"
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
438 (auto-fill-mode 0)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
439 (make-variable-buffer-local 'write-contents-hooks)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
440 (add-hook 'write-contents-hooks 'gametree-save-and-hack-layout))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
441
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
442 ;;;; Key bindings
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
443
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
444 (define-key gametree-mode-map "\C-c\C-j" 'gametree-break-line-here)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
445 (define-key gametree-mode-map "\C-c\C-v" 'gametree-insert-new-leaf)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
446 (define-key gametree-mode-map "\C-c\C-m" 'gametree-merge-line)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
447 (define-key gametree-mode-map "\C-c\C-r " 'gametree-layout-to-register)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
448 (define-key gametree-mode-map "\C-c\C-r/" 'gametree-layout-to-register)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
449 (define-key gametree-mode-map "\C-c\C-rj" 'gametree-apply-register-layout)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
450 (define-key gametree-mode-map "\C-c\C-y" 'gametree-save-and-hack-layout)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
451
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
452 ;;;; Goodies for mousing users
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
453 (and (fboundp 'track-mouse)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
454 (defun gametree-mouse-break-line-here (event)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
455 (interactive "e")
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
456 (mouse-set-point event)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
457 (gametree-break-line-here))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
458 (defun gametree-mouse-show-children-and-entry (event)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
459 (interactive "e")
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
460 (mouse-set-point event)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
461 (gametree-show-children-and-entry))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
462 (defun gametree-mouse-show-subtree (event)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
463 (interactive "e")
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
464 (mouse-set-point event)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
465 (show-subtree))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
466 (defun gametree-mouse-hide-subtree (event)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
467 (interactive "e")
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
468 (mouse-set-point event)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
469 (hide-subtree))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
470 (define-key gametree-mode-map [M-down-mouse-2 M-mouse-2]
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
471 'gametree-mouse-break-line-here)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
472 (define-key gametree-mode-map [S-down-mouse-1 S-mouse-1]
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
473 'gametree-mouse-show-children-and-entry)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
474 (define-key gametree-mode-map [S-down-mouse-2 S-mouse-2]
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
475 'gametree-mouse-show-subtree)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
476 (define-key gametree-mode-map [S-down-mouse-3 S-mouse-3]
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
477 'gametree-mouse-hide-subtree))
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
478
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
479 (provide 'gametree)
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
480
8a36e43748c0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
481 ;;; gametree.el ends here