gametree.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. ;;; gametree.el --- manage game analysis trees in Emacs
  2. ;; Copyright (C) 1997, 1999, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Ian T Zimmerman <itz@rahul.net>
  4. ;; Created: Wed Dec 10 07:41:46 PST 1997
  5. ;; Keywords: games
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This little hack has enabled me to keep track of my email chess
  19. ;; games in Emacs. For a long time I dreamt about writing a real,
  20. ;; graphical tree editor; but, then the idea struck me, why do it
  21. ;; graphically, when it can be done in Emacs? :-) And in fact Emacs
  22. ;; almost had what I needed out of the box, namely the powerful
  23. ;; Outline mode. This code is built entirely on Outline mode, it
  24. ;; only adds two commands that I found indispensable when dealing
  25. ;; with the special kind of trees that analysis trees comprise.
  26. ;; The built-in documentation should be enough to explain the use,
  27. ;; along with the following example (yes, this is a real game).
  28. ;; *** 23. f4 ef 24. Nf3 Rf3 -/+
  29. ;; 25. Rf3 Qh2 26. Kh1 Qh1 27. Ke2 Qg2 -+
  30. ;; ******* 25. gf3 Nce3 26. Qe2 Nf1 -/+
  31. ;; 27. Qe5 Ne5 28. Kf1 Nf3 -/+
  32. ;; 27. Kf1 Nh2 28. Kf2 Ng4 29. fg4 Rf8 30. Ke1 Qg3 31. Kd1 Rf2 -+
  33. ;; Place the preceding in a scratch buffer, load this code, and do
  34. ;; M-x gametree-mode. Now place the cursor just after the `Nf3' and
  35. ;; before the `Rf3' on the first line, and do C-c C-j. The result is
  36. ;; *** 23. f4 ef 24. Nf3
  37. ;; ****** 24: Rf3 -/+
  38. ;; 25. Rf3 Qh2 26. Kh1 Qh1 27. Ke2 Qg2 -+
  39. ;; ******* 25. gf3 Nce3 26. Qe2 Nf1 -/+
  40. ;; 27. Qe5 Ne5 28. Kf1 Nf3 -/+
  41. ;; 27. Kf1 Nh2 28. Kf2 Ng4 29. fg4 Rf8 30. Ke1 Qg3 31. Kd1 Rf2 -+
  42. ;; Now you can add another subvariation on Black's 24th move: with
  43. ;; the cursor still on the first line, do C-c C-v, and voila
  44. ;; *** 23. f4 ef 24. Nf3
  45. ;; 24:
  46. ;; ****** 24: Rf3 -/+
  47. ;; 25. Rf3 Qh2 26. Kh1 Qh1 27. Ke2 Qg2 -+
  48. ;; ******* 25. gf3 Nce3 26. Qe2 Nf1 -/+
  49. ;; 27. Qe5 Ne5 28. Kf1 Nf3 -/+
  50. ;; 27. Kf1 Nh2 28. Kf2 Ng4 29. fg4 Rf8 30. Ke1 Qg3 31. Kd1 Rf2 -+
  51. ;; and the cursor is positioned on the new line just after the move
  52. ;; number, so you can start typing the new analysis. That's it,
  53. ;; quite simple.
  54. ;; As of version 1.1, a simple score reducer has been implemented.
  55. ;; As you type in leaf variations, you can add a numerical score tag
  56. ;; to them with C-c ; . Then, with the cursor on a variation higher
  57. ;; up in the tree, you can do C-c ^ and the program will compute the
  58. ;; reduced score of the internal variation based on the scores of its
  59. ;; children (which are recursively computed). You can use any range
  60. ;; of numbers you wish as scores, maybe -1000 to 1000 or 0 to 100,
  61. ;; all that matters to the program is that higher means better for
  62. ;; White, lower means better for Black.
  63. ;;; Code:
  64. (require 'derived)
  65. (require 'outline)
  66. ;;;; Configuration variables
  67. (defgroup gametree nil
  68. "Manage game analysis trees in Emacs."
  69. :prefix "gametree-"
  70. :group 'games
  71. :version "20.3")
  72. (defcustom gametree-half-ply-regexp (regexp-quote ":")
  73. "Matches ends of numbers of moves by the \"second\" player.
  74. For instance, it is an almost universal convention in chess to postfix
  75. numbers of moves by Black (if considered in isolation) by the ellipsis
  76. \"...\". This is NOT a good choice for this program, though, because it
  77. conflicts with the use of ellipsis by Outline mode to denote collapsed
  78. subtrees. The author uses \":\" because it agrees nicely with a set of
  79. LaTeX macros he uses for typesetting annotated games."
  80. :type 'regexp
  81. :group 'gametree)
  82. (defcustom gametree-full-ply-regexp (regexp-quote ".")
  83. "Matches ends of numbers of moves by the \"first\" player.
  84. For instance, it is an almost universal convention in chess to postfix
  85. numbers of moves by White (if considered in isolation) by the dot \".\"."
  86. :type 'regexp
  87. :group 'gametree)
  88. (defcustom gametree-half-ply-format "%d:"
  89. "Output format for move numbers of moves by the \"second\" player.
  90. Has to contain \"%d\" to output the actual number."
  91. :type 'string
  92. :group 'gametree)
  93. (defcustom gametree-full-ply-format "%d."
  94. "Output format for move numbers of moves by the \"first\" player.
  95. Has to contain \"%d\" to output the actual number."
  96. :type 'string
  97. :group 'gametree)
  98. (defcustom gametree-make-heading-function
  99. (function (lambda (level)
  100. (insert (make-string level ?*))))
  101. "A function of one numeric argument, LEVEL, to insert a heading at point.
  102. You should change this if you change `outline-regexp'."
  103. :type 'function
  104. :group 'gametree)
  105. (defvar gametree-local-layout nil
  106. "A list encoding the layout (i.e. the show or hide state) of the file.
  107. If Emacs notices a local variable specification of this variable in
  108. the first line of the buffer while saving the buffer to the visited
  109. file, the local value will be saved there and restored the next time
  110. the file is visited (subject to the usual restriction via
  111. `enable-local-variables'), and the layout will be set accordingly.")
  112. (defcustom gametree-score-opener "{score="
  113. "The string which opens a score tag, and precedes the actual score."
  114. :type 'string
  115. :group 'gametree)
  116. (defcustom gametree-score-manual-flag "!"
  117. "String marking the line as manually (as opposed to automatically) scored."
  118. :type 'string
  119. :group 'gametree)
  120. (defcustom gametree-score-closer "}"
  121. "The string which closes a score tag, and follows the actual score."
  122. :type 'string
  123. :group 'gametree)
  124. (defcustom gametree-score-regexp
  125. (concat "[^\n\^M]*\\("
  126. (regexp-quote gametree-score-opener)
  127. "[ ]*\\("
  128. (regexp-quote gametree-score-manual-flag)
  129. "[ ]*\\)?\\([-+]?[0-9]+\\)"
  130. (regexp-quote gametree-score-closer)
  131. "[ ]*\\)[\n\^M]")
  132. "Regular expression matching lines that guide the program in scoring.
  133. Its third parenthetical group should match the actual score. Its
  134. first parenthetical group should match the entire score tag. Its
  135. second parenthetical group should be an optional flag that marks the
  136. line as *manually* (as opposed to automatically) scored, which
  137. prevents the program from recursively applying the scoring algorithm
  138. on the subtree headed by the marked line, and makes it use the manual
  139. score instead."
  140. :type 'regexp
  141. :group 'gametree)
  142. (defcustom gametree-default-score 0
  143. "Score to assume for branches lacking score tags."
  144. :type 'integer
  145. :group 'gametree)
  146. ;;;; Helper functions
  147. (defun gametree-prettify-heading ()
  148. "Insert/delete space between leading asterisks and heading text.
  149. If the current variation is an internal node (i.e. starts with one or
  150. more asterisks), ensure there's at least one space between the
  151. asterisks and the text. If on the other hand this is a leaf, there
  152. should be no leading white space."
  153. (save-excursion
  154. (beginning-of-line 1)
  155. (if (re-search-forward (concat "\\=" outline-regexp) nil t)
  156. (if (not (looking-at "[ \t]+")) (insert " "))
  157. (delete-char (save-excursion (skip-chars-forward " \t"))))
  158. (if (re-search-forward (concat "\\=[ \t]*[1-9][0-9]*\\("
  159. gametree-full-ply-regexp "\\|"
  160. gametree-half-ply-regexp "\\)") nil t)
  161. (if (not (looking-at "[ \t]+")) (insert " ")
  162. (delete-char (1- (save-excursion (skip-chars-forward " \t"))))))))
  163. (defun gametree-looking-at-ply ()
  164. "Read and return the number of the ply under point."
  165. (if (eobp) 0
  166. (let ((boundary (concat "[ \t]*\\([1-9][0-9]*\\)\\("
  167. gametree-full-ply-regexp "\\|"
  168. gametree-half-ply-regexp "\\)"))
  169. (limit (line-beginning-position 1)))
  170. (if (looking-at boundary)
  171. (+ (* 2 (string-to-number (match-string 1)))
  172. (if (string-match gametree-half-ply-regexp (match-string 2)) 1 0))
  173. (save-excursion
  174. (re-search-backward boundary limit)
  175. (skip-chars-backward "0123456789")
  176. (1+ (* 2 (string-to-number
  177. (buffer-substring (point) (match-end 1))))))))))
  178. (defun gametree-current-branch-ply ()
  179. "Return the ply number of the first move of the current variation."
  180. (save-excursion
  181. (beginning-of-line 1)
  182. (re-search-forward (concat "\\=" outline-regexp) nil t)
  183. (gametree-looking-at-ply)))
  184. (defsubst gametree-forward-line ()
  185. (re-search-forward "[\n\^M]" nil 'move))
  186. (defun gametree-current-branch-depth ()
  187. "Return the depth of the current variation in the analysis tree.
  188. This value is simply the outline heading level of the current line."
  189. (save-excursion
  190. (beginning-of-line 1)
  191. (if (looking-at outline-regexp)
  192. (outline-level) 0)))
  193. (defun gametree-transpose-following-leaves ()
  194. "Move the current leaf variation behind all others on the same level."
  195. (let ((following-leaves
  196. (save-excursion
  197. (gametree-forward-line)
  198. (let ((p (point)))
  199. (while (and (not (eobp))
  200. (= 0 (gametree-current-branch-depth)))
  201. (gametree-forward-line))
  202. (prog1 (buffer-substring p (point))
  203. (delete-region p (point)))))))
  204. (save-excursion
  205. (beginning-of-line 1)
  206. (insert following-leaves))))
  207. ;;;; Functions related to the task of saving and restoring current
  208. ;;;; outline layout
  209. (defsubst gametree-show-children-and-entry ()
  210. (show-children)
  211. (show-entry))
  212. (defun gametree-entry-shown-p ()
  213. (save-excursion
  214. (forward-line 1)
  215. (and (bolp) (not (eobp)) (not (looking-at outline-regexp)))))
  216. (defun gametree-children-shown-p ()
  217. (save-excursion
  218. (ignore-errors
  219. (let ((depth (gametree-current-branch-depth)))
  220. (outline-next-visible-heading 1)
  221. (< depth (gametree-current-branch-depth))))))
  222. (defun gametree-current-layout (depth &optional from-top-level)
  223. (let ((layout nil) (first-time t))
  224. (while (save-excursion
  225. (ignore-errors
  226. (or (and first-time from-top-level
  227. (bolp) (looking-at outline-regexp))
  228. (setq first-time nil)
  229. (outline-next-visible-heading 1))
  230. (< depth (gametree-current-branch-depth))))
  231. (if (not first-time)
  232. (outline-next-visible-heading 1))
  233. (setq first-time nil)
  234. (if (not (gametree-children-shown-p))
  235. (setq layout
  236. (nconc layout
  237. (if (gametree-entry-shown-p)
  238. (list 'show-entry)
  239. (list nil))))
  240. (setq layout (nconc layout (if (gametree-entry-shown-p)
  241. (list 'gametree-show-children-and-entry)
  242. (list 'show-children))))
  243. (let ((sub-layout
  244. (gametree-current-layout (gametree-current-branch-depth))))
  245. (setq layout (nconc layout (list sub-layout))))))
  246. layout))
  247. (defun gametree-save-layout ()
  248. (save-excursion
  249. (goto-char (point-min))
  250. (setq gametree-local-layout (gametree-current-layout 0 t))))
  251. (defun gametree-apply-layout (layout depth &optional from-top-level)
  252. (let ((first-time t))
  253. (while (and layout
  254. (save-excursion
  255. (ignore-errors
  256. (or (and first-time from-top-level
  257. (bolp) (looking-at outline-regexp))
  258. (setq first-time nil)
  259. (outline-next-visible-heading 1))
  260. (< depth (gametree-current-branch-depth)))))
  261. (if (not first-time)
  262. (outline-next-visible-heading 1))
  263. (setq first-time nil)
  264. (hide-subtree)
  265. (if (nth 0 layout)
  266. (funcall (nth 0 layout)))
  267. (if (not (and (nth 1 layout) (listp (nth 1 layout))))
  268. (setq layout (cdr layout))
  269. (gametree-apply-layout (nth 1 layout)
  270. (gametree-current-branch-depth))
  271. (setq layout (cdr (cdr layout)))))))
  272. (defun gametree-restore-layout ()
  273. (save-excursion
  274. (goto-char (point-min))
  275. (gametree-apply-layout gametree-local-layout 0 t)))
  276. (defun gametree-hack-file-layout ()
  277. (save-excursion
  278. (goto-char (point-min))
  279. (if (looking-at "[^\n]*-\*-[^\n]*gametree-local-layout: \\([^;\n]*\\);")
  280. (progn
  281. (goto-char (match-beginning 1))
  282. (delete-region (point) (match-end 1))
  283. (let ((standard-output (current-buffer)))
  284. (princ gametree-local-layout))))))
  285. ;;;; Scoring functions
  286. (defun gametree-current-branch-score ()
  287. "Return score of current variation according to its score tag.
  288. When no score tag is present, use the value of `gametree-default-score'."
  289. (if (looking-at gametree-score-regexp)
  290. (string-to-number (match-string 3))
  291. gametree-default-score))
  292. (defun gametree-compute-reduced-score ()
  293. "Return current internal node score computed recursively from subnodes.
  294. Subnodes which have been manually scored are honored."
  295. (if (or
  296. (= 0 (gametree-current-branch-depth))
  297. (save-excursion (gametree-forward-line) (eobp))
  298. (and (looking-at gametree-score-regexp)
  299. (not (null (match-string 2)))))
  300. (gametree-current-branch-score)
  301. (let ((depth (gametree-current-branch-depth)))
  302. (save-excursion
  303. (gametree-forward-line)
  304. ;; the case of a leaf node has already been handled, so here I
  305. ;; know I am on the 1st line of the current subtree. This can
  306. ;; be either a leaf child, or a subheading.
  307. (let ((running gametree-default-score)
  308. (minmax
  309. (if (= 0 (mod (gametree-current-branch-ply) 2))
  310. 'max 'min)))
  311. (while (and (not (eobp))
  312. (= 0 (gametree-current-branch-depth))) ;handle leaves
  313. (setq running (funcall minmax running
  314. (gametree-current-branch-score)))
  315. (gametree-forward-line))
  316. (let ((done (and (not (eobp))
  317. (< depth (gametree-current-branch-depth)))))
  318. (while (not done) ;handle subheadings
  319. (setq running (funcall minmax running
  320. (gametree-compute-reduced-score)))
  321. (setq done (ignore-errors (outline-forward-same-level 1)))))
  322. running)))))
  323. ;;;; Commands
  324. (defun gametree-insert-new-leaf (&optional at-depth)
  325. "Start a new leaf variation under the current branching point.
  326. The new variation can later be split to be a branching point itself,
  327. with \\[gametree-break-line-here]. If the point is currently on a
  328. leaf variation, this command won't work; use \\[gametree-break-line-here]
  329. on the current line first.
  330. With a numeric arg AT-DEPTH, first go up the tree until a node of
  331. depth AT-DEPTH or smaller is found."
  332. (interactive "*P")
  333. (if (zerop (gametree-current-branch-depth))
  334. (outline-up-heading 0))
  335. (if at-depth
  336. (while (> (gametree-current-branch-depth)
  337. (prefix-numeric-value at-depth))
  338. (outline-up-heading 1)))
  339. (beginning-of-line 1)
  340. (let ((parent-depth (gametree-current-branch-depth)))
  341. (show-entry)
  342. (condition-case nil
  343. (outline-next-visible-heading 1)
  344. (error
  345. (goto-char (point-max))
  346. (if (not (bolp)) (insert "\n"))))
  347. (let ((starting-plys
  348. (if (> (gametree-current-branch-depth) parent-depth)
  349. (gametree-current-branch-ply)
  350. (save-excursion (forward-line -1)
  351. (gametree-current-branch-ply)))))
  352. (goto-char (1- (point)))
  353. (insert "\n")
  354. (insert (format (if (= 0 (mod starting-plys 2))
  355. gametree-full-ply-format
  356. gametree-half-ply-format)
  357. (/ starting-plys 2))))))
  358. (defun gametree-break-line-here (&optional at-move)
  359. "Split the variation node at the point position.
  360. This command works whether the current variation node is a leaf, or is
  361. already branching at its end. The new node is created at a level that
  362. reflects the number of game plys between the beginning of the current
  363. variation and the breaking point.
  364. With a numerical argument AT-MOVE, split the variation before
  365. White's AT-MOVEth move, or Black's if negative. The last option will
  366. only work of Black's moves are explicitly numbered, for instance
  367. `1. e4 1: e5'."
  368. (interactive "*P")
  369. (if at-move (progn
  370. (end-of-line 1)
  371. (let ((limit (point)))
  372. (beginning-of-line 1)
  373. (re-search-forward
  374. (concat
  375. (regexp-quote
  376. (int-to-string (abs (prefix-numeric-value at-move))))
  377. (if (> at-move 0) gametree-full-ply-regexp
  378. gametree-half-ply-regexp)) limit))
  379. (goto-char (match-beginning 0))))
  380. (gametree-transpose-following-leaves)
  381. (let* ((pt (set-marker (make-marker) (point)))
  382. (plys (gametree-current-branch-ply))
  383. (depth (gametree-current-branch-depth))
  384. (old-depth depth))
  385. (if (= depth 0)
  386. (progn
  387. (save-excursion
  388. (outline-previous-visible-heading 1)
  389. (setq depth
  390. (let ((old-branch-ply
  391. (condition-case nil
  392. (gametree-current-branch-ply)
  393. (error 0))))
  394. (if (zerop old-branch-ply)
  395. (1+ (gametree-current-branch-depth))
  396. (+ (gametree-current-branch-depth)
  397. (- plys old-branch-ply))))))
  398. (save-excursion
  399. (beginning-of-line 1)
  400. (funcall gametree-make-heading-function depth)
  401. (gametree-prettify-heading))))
  402. (save-excursion
  403. (if (not (looking-at (concat "[ \t]*[1-9][0-9]*\\("
  404. gametree-full-ply-regexp "\\|"
  405. gametree-half-ply-regexp "\\)")))
  406. (progn
  407. (insert (format (if (= 0 (mod (gametree-looking-at-ply) 2))
  408. gametree-full-ply-format
  409. gametree-half-ply-format)
  410. (/ (gametree-looking-at-ply) 2)))
  411. (gametree-prettify-heading)
  412. (beginning-of-line 1)))
  413. (goto-char pt)
  414. (insert "\n")
  415. (if (not (= 0 old-depth))
  416. (funcall gametree-make-heading-function
  417. (+ depth (- (gametree-current-branch-ply) plys))))
  418. (gametree-prettify-heading))))
  419. (defun gametree-merge-line ()
  420. "Merges a variation with its only child.
  421. Does *not* check if the variation has in fact a unique child; users beware."
  422. (interactive "*")
  423. (if (zerop (gametree-current-branch-depth))
  424. (outline-up-heading 0))
  425. (if (looking-at gametree-score-regexp)
  426. (delete-region (match-beginning 1) (match-end 1)))
  427. (end-of-line 1)
  428. (let ((prev-depth (save-excursion (forward-line 1)
  429. (gametree-current-branch-depth))))
  430. (delete-char (1+ prev-depth))
  431. (if (zerop prev-depth)
  432. (save-excursion
  433. (beginning-of-line 1)
  434. (delete-char (gametree-current-branch-depth))
  435. (gametree-prettify-heading)))))
  436. (defun gametree-insert-score (score &optional auto)
  437. "Insert a score tag with value SCORE at the end of the current line.
  438. If this line already has a score tag, just jump to it and alter it.
  439. When called from a program, optional AUTO flag tells if the score is
  440. being entered automatically (and thus should lack the manual mark)."
  441. (interactive "*P")
  442. (beginning-of-line 1)
  443. (if (looking-at gametree-score-regexp)
  444. (progn
  445. (goto-char (match-beginning 3))
  446. (if (and auto (not (null (match-string 2))))
  447. (delete-region (match-beginning 2) (match-end 2)))
  448. (if (not (null score))
  449. (delete-region (match-beginning 3) (match-end 3)))
  450. (if (and (not auto) (null (match-string 2)))
  451. (insert gametree-score-manual-flag)))
  452. (end-of-line 1)
  453. (if (= 0 (save-excursion (skip-chars-backward " \t")))
  454. (insert " "))
  455. (insert gametree-score-opener)
  456. (if (not auto) (insert gametree-score-manual-flag))
  457. (save-excursion (insert gametree-score-closer)))
  458. (if (not (null score))
  459. (save-excursion
  460. (insert (int-to-string (prefix-numeric-value score))))))
  461. (defun gametree-compute-and-insert-score ()
  462. "Compute current node score, maybe recursively from subnodes. Insert it.
  463. Subnodes which have been manually scored are honored."
  464. (interactive "*")
  465. (let ((auto (not (and (looking-at gametree-score-regexp)
  466. (not (null (match-string 2))))))
  467. (score (gametree-compute-reduced-score)))
  468. (gametree-insert-score score auto)))
  469. (defun gametree-layout-to-register (register)
  470. "Store current tree layout in register REGISTER.
  471. Use \\[gametree-apply-register-layout] to restore that configuration.
  472. Argument is a character, naming the register."
  473. (interactive "cLayout to register: ")
  474. (save-excursion
  475. (goto-char (point-min))
  476. (set-register register
  477. (gametree-current-layout 0 t))))
  478. (defun gametree-apply-register-layout (char)
  479. "Return to a tree layout stored in a register.
  480. Argument is a character, naming the register."
  481. (interactive "*cApply layout from register: ")
  482. (save-excursion
  483. (goto-char (point-min))
  484. (gametree-apply-layout (get-register char) 0 t)))
  485. (defun gametree-save-and-hack-layout ()
  486. "Save the current tree layout and hack the file local variable spec.
  487. This function saves the current layout in `gametree-local-layout' and,
  488. if a local file variable specification for this variable exists in the
  489. buffer, it is replaced by the new value. See the documentation for
  490. `gametree-local-layout' for more information."
  491. (interactive)
  492. (gametree-save-layout)
  493. (let ((inhibit-read-only t))
  494. (gametree-hack-file-layout))
  495. nil)
  496. ;;;; Key bindings
  497. (defvar gametree-mode-map
  498. (let ((map (make-sparse-keymap)))
  499. (define-key map "\C-c\C-j" 'gametree-break-line-here)
  500. (define-key map "\C-c\C-v" 'gametree-insert-new-leaf)
  501. (define-key map "\C-c\C-m" 'gametree-merge-line)
  502. (define-key map "\C-c\C-r " 'gametree-layout-to-register)
  503. (define-key map "\C-c\C-r/" 'gametree-layout-to-register)
  504. (define-key map "\C-c\C-rj" 'gametree-apply-register-layout)
  505. (define-key map "\C-c\C-y" 'gametree-save-and-hack-layout)
  506. (define-key map "\C-c;" 'gametree-insert-score)
  507. (define-key map "\C-c^" 'gametree-compute-and-insert-score)
  508. map))
  509. (define-derived-mode gametree-mode outline-mode "GameTree"
  510. "Major mode for managing game analysis trees.
  511. Useful to postal and email chess (and, it is hoped, also checkers, go,
  512. shogi, etc.) players, it is a slightly modified version of Outline mode.
  513. \\{gametree-mode-map}"
  514. (auto-fill-mode 0)
  515. (make-local-variable 'write-contents-hooks)
  516. (add-hook 'write-contents-hooks 'gametree-save-and-hack-layout))
  517. ;;;; Goodies for mousing users
  518. (and (fboundp 'track-mouse)
  519. (defun gametree-mouse-break-line-here (event)
  520. (interactive "e")
  521. (mouse-set-point event)
  522. (gametree-break-line-here))
  523. (defun gametree-mouse-show-children-and-entry (event)
  524. (interactive "e")
  525. (mouse-set-point event)
  526. (gametree-show-children-and-entry))
  527. (defun gametree-mouse-show-subtree (event)
  528. (interactive "e")
  529. (mouse-set-point event)
  530. (show-subtree))
  531. (defun gametree-mouse-hide-subtree (event)
  532. (interactive "e")
  533. (mouse-set-point event)
  534. (hide-subtree))
  535. (define-key gametree-mode-map [M-down-mouse-2 M-mouse-2]
  536. 'gametree-mouse-break-line-here)
  537. (define-key gametree-mode-map [S-down-mouse-1 S-mouse-1]
  538. 'gametree-mouse-show-children-and-entry)
  539. (define-key gametree-mode-map [S-down-mouse-2 S-mouse-2]
  540. 'gametree-mouse-show-subtree)
  541. (define-key gametree-mode-map [S-down-mouse-3 S-mouse-3]
  542. 'gametree-mouse-hide-subtree))
  543. (provide 'gametree)
  544. ;;; gametree.el ends here