annotate lisp/games/conx.el @ 90:99da576a67e7 xemacs-20-0

Import from CVS: tag xemacs-20-0
author cvs
date Mon, 13 Aug 2007 09:10:46 +0200
parents b9518feda344
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
1 ;;; conx.el --- Yet another dissociater
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
2
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
3 ;; Copyright status unknown
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
4
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
5 ;; Author: Jamie Zawinski <jwz@netscape.com>
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
6 ;; Keywords: games
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
7
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
8 ;; This file is part of XEmacs.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
9
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
10 ;; XEmacs is free software; you can redistribute it and/or modify it
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
11 ;; under the terms of the GNU General Public License as published by
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
13 ;; any later version.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
14
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
15 ;; XEmacs is distributed in the hope that it will be useful, but
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
18 ;; General Public License for more details.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
19
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
20 ;; You should have received a copy of the GNU General Public License
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
23 ;; 02111-1307, USA.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
24
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
25 ;;; Synched up with: Not in FSF
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
26
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
27 ;;; Commentary:
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;;; conx.el: Yet Another Dissociator.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;;; Original design by Skef Wholey <skef@cs.cmu.edu>;
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
31 ;;; ported to Emacs-Lisp by Jamie Zawinski <jwz@netscape.com>, 5-mar-91.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;;; Run this compiled. It will be an order of magnitude faster.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;;; Select a buffer with a lot of text in it. Say M-x conx-buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;;; or M-x conx-region. Repeat on as many other bodies of text as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;;; you like.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;;; M-x conx will use the word-frequency tree the above generated
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;;; to produce random sentences in a popped-up buffer. It will pause
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;;; at the end of each paragraph for two seconds; type ^G to stop it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 ;;; M-x conx-init will clear the data structures so you can start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 ;;; over. Note that if you run it twice consecutively on the same
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ;;; body of text, word sequences in that buffer will be twice as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 ;;; likely to be generated.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 ;;; Once you have sucked in a lot of text and like the kinds of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 ;;; sentences conx is giving you, you can save the internal data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 ;;; structures to a file with the M-x conx-save command. Loading
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 ;;; this file with M-x conx-load will be a lot faster and easier
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 ;;; than re-absorbing all of the text files. Beware that loading a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 ;;; saved conx-file clears the conx database in memory.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 ;;; M-x conx-emit-c will write out C source code which, when compiled,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 ;;; will produce a standalone program which generates sentences from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 ;;; a copy of the database currently loaded.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 ;;; Ideas for future improvement:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 ;;; o It would be nice if we could load in more than one saved
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 ;;; file at a time.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 ;;; o use it to collect statistics on newsgroup conversations by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 ;;; examining the tree for the most common words and phrases
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 ;;; o when replying to mail, insert an X-CONX: header field which
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 ;;; contains a sentence randomly generated from the body of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 ;;; message being replied to.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 ;;; o It could stand to be faster...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
72 ;;; Code:
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
73 (defconst conx-version "1.6, 6-may-94.")
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
74
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 (defvar conx-bounce 10) ; 1/x
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 (defvar conx-hashtable-size 9923) ; 9923 is prime
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 (defconst conx-words-hashtable nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 (defconst conx-words-vector nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (defconst conx-words-vector-fp 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (defconst conx-last-word nil)
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
82 p
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 (defvar conx-files nil "FYI")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (defun conx-init ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 "Forget the current word-frequency tree."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (if (and conx-words-hashtable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (>= (length conx-words-hashtable) conx-hashtable-size))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (fillarray conx-words-hashtable 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (setq conx-words-hashtable (make-vector conx-hashtable-size 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (if conx-words-vector
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (fillarray conx-words-vector nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (setq conx-words-vector (make-vector 1000 nil))) ; this grows
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (setq conx-words-vector-fp 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 (setq conx-last-word nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 conx-files nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (defun conx-rehash ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 ;; misnomer; this just grows the linear vector, growing the hash table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 ;; is too hard.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (message "Rehashing...")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (let* ((L (length conx-words-vector))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (v2 (make-vector (+ L L) nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (while (< 0 L)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (aset v2 (1- L) (aref conx-words-vector (setq L (1- L)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (setq conx-words-vector v2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (message "Rehashing...done"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (defmacro conx-count (word) (list 'aref word 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (defmacro conx-cap (word) (list 'aref word 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (defmacro conx-comma (word) (list 'aref word 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (defmacro conx-period (word) (list 'aref word 3))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (defmacro conx-quem (word) (list 'aref word 4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (defmacro conx-bang (word) (list 'aref word 5))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 (defmacro conx-succ (word) (list 'aref word 6))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (defmacro conx-pred (word) (list 'aref word 7))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (defmacro conx-succ-c (word) (list 'aref word 8))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (defmacro conx-pred-c (word) (list 'aref word 9))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (defconst conx-length 10)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 (defmacro conx-make-word ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 '(copy-sequence '[1 0 0 0 0 0 nil nil 0 0]))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (defmacro conx-setf (form val) ; mind-numbingly simple
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (setq form (macroexpand form (and (boundp 'byte-compile-macro-environment)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 byte-compile-macro-environment)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 (cond ((symbolp form) (list 'setq form val))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 ((eq (car form) 'aref) (cons 'aset (append (cdr form) (list val))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 ((eq (car form) 'cdr) (list 'setcdr (nth 1 form) val))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 ((eq (car form) 'car) (list 'setcar (nth 1 form) val))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 (t (error "can't setf %s" form))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (defmacro conx-push (thing list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (list 'conx-setf list (list 'cons thing list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (defconst conx-most-positive-fixnum (lsh -1 -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 "The largest positive integer that can be represented in this emacs.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (defmacro conx-rand (n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (list '% (list 'logand 'conx-most-positive-fixnum '(random)) n))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (defmacro conx-relate-succ (word related)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 (` (let ((vec (symbol-value (, word))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (conx-setf (conx-succ-c vec) (1+ (conx-succ-c vec)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (let ((rel (assq (, related) (conx-succ vec))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (if rel
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 (setcdr rel (1+ (cdr rel)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (conx-push (cons (, related) 1) (conx-succ vec)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 (defmacro conx-relate-pred (word related)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (` (let ((vec (symbol-value (, word))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (conx-setf (conx-pred-c vec) (1+ (conx-pred-c vec)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (let ((rel (assq (, related) (conx-pred vec))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 (if rel
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 (setcdr rel (1+ (cdr rel)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (conx-push (cons (, related) 1) (conx-pred vec)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 (defmacro conx-add-word (word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 (` (let* ((word (, word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (fc (aref word 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (setq word (intern (downcase word) conx-words-hashtable))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (let ((vec (and (boundp word) (symbol-value word))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (if vec
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (conx-setf (conx-count vec) (1+ (conx-count vec)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (if (= conx-words-vector-fp (length conx-words-vector))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 (conx-rehash))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 (set word (setq vec (conx-make-word)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 (aset conx-words-vector conx-words-vector-fp word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 (setq conx-words-vector-fp (1+ conx-words-vector-fp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 (or (< fc ?A) (> fc ?Z)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (conx-setf (conx-cap vec) (1+ (conx-cap vec)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (if conx-last-word
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (conx-relate-succ conx-last-word word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (conx-relate-pred word conx-last-word)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 (setq conx-last-word word))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 (defmacro conx-punx (char)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 (` (if conx-last-word
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 (let ((char (, char))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 (vec (symbol-value conx-last-word)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 (cond ((eq char ?\,)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 (conx-setf (conx-comma vec) (1+ (conx-comma vec))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 ((or (eq char ?\.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 (eq char ?\;))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 (conx-setf (conx-period vec) (1+ (conx-period vec)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 (setq conx-last-word nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 ((eq char ?\?)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 (conx-setf (conx-quem vec) (1+ (conx-quem vec)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 (setq conx-last-word nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 ((eq char ?\!)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 (conx-setf (conx-bang vec) (1+ (conx-bang vec)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 (setq conx-last-word nil)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 (defun conxify-internal ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 (let (p w)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 (while (not (eobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 (skip-chars-forward "^A-Za-z0-9'")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 (while (memq (following-char) '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?\'))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 ;; ignore words beginning with digits
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 (skip-chars-forward "A-Za-z0-9'")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 (skip-chars-forward "^A-Za-z0-9'"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 (setq p (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 (skip-chars-forward "A-Za-z0-9'")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 (if (= ?\' (preceding-char)) (forward-char -1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 (if (eq p (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 (setq w (buffer-substring p (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 (if (equal "nil" w) ; hey, nil is totally magic, this doesn't work!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 (conx-add-word w)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 (setq n (1+ n))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 (skip-chars-forward " \t\n\r")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 (if (memq (setq p (following-char)) '(?\, ?\. ?\! ?\? ?\;))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 (conx-punx p)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 (defun conx-buffer ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 "Absorb the text in the current buffer into the tree."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 (or conx-words-vector (conx-init))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 (let ((i conx-words-vector-fp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 (n 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 (pm (point-max)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 (save-restriction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 (widen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 (while (< (setq p (point)) pm)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 (search-forward "\n\n" pm 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 (narrow-to-region p (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 (goto-char (prog1 p (setq p (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 (conxify-internal)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 (widen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 (message "%d%%..." (/ (* p 100) (point-max))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 (if buffer-file-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 (setq conx-files (nconc conx-files (list buffer-file-name))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 (message "%s words, %d unique" n (- conx-words-vector-fp i))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (defun conx-region (p m)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 "Absorb the text in the current region into the tree."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 (interactive "r")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 (save-restriction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 (widen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 (narrow-to-region p m)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 (conx-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 (defun conx-mail-buffer ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 "Conxify a buffer in /bin/mail format."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 (skip-chars-forward "\n \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 (let ((case-fold-search nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 (buffer-file-name nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 p p2 p3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 (or (looking-at "^From ") (error "not in /bin/mail format"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 (while (not (eobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 (search-forward "\n\n" nil 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 (setq p (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 (search-forward "\nFrom " nil 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 (setq p3 (setq p2 (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 ;; don't count ".signature" sections.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 (and (re-search-backward "\n--+\n" nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 (< (count-lines (point) p2) 9)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 (setq p2 (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 (conx-region p (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 (goto-char p3)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 (if buffer-file-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 (setq conx-files (nconc conx-files (list buffer-file-name))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 ;;; output
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 (defun conx-random-related (count list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 (let ((foll (if (= 0 count) 0 (conx-rand count)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 ans)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 (while list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 (if (<= foll (cdr (car list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 (setq ans (car (car list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 list nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 (setq foll (- foll (cdr (car list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 list (cdr list))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 ans))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 (defun conx-random-succ (word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 (if (= 0 (conx-succ-c (symbol-value word)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 word
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 (let ((next (conx-random-related
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 (conx-succ-c (symbol-value word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 (conx-succ (symbol-value word)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 (if (= 0 (conx-rand conx-bounce))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 (conx-random-succ
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 (conx-random-related
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 (conx-pred-c (symbol-value next))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 (conx-pred (symbol-value next))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 next))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 (defun conx-sentence ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 (or (> conx-words-vector-fp 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 (error "no conx data is loaded; see `conx-buffer'."))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 (let* ((word (aref conx-words-vector (conx-rand conx-words-vector-fp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 (first-p t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 (p (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 vec punc str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 (while word
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 (setq punc (conx-rand (conx-count (setq vec (symbol-value word)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 (if (or first-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 ;; (< (conx-rand (conx-count vec)) (conx-cap vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 (= (conx-count vec) (conx-cap vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 (setq first-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 (setq str (symbol-name word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 (insert (+ (- ?A ?a) (aref str 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 (insert (substring str 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 (insert (symbol-name word)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 (cond ((< punc (conx-comma vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 (insert ", "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 ((< (setq punc (- punc (conx-comma vec))) (conx-period vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 (setq word nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 (if (= 0 (conx-rand 5))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 (if (= 0 (conx-rand 4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 (insert ": ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 (insert "; "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 (insert ". ")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 ((< (setq punc (- punc (conx-period vec))) (conx-quem vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 (setq word nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 (insert "? "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 ((< (setq punc (- punc (conx-quem vec))) (conx-bang vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 (setq word nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 (insert "! "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 (insert " ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 (if (= 0 (conx-succ-c vec)) (setq word nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 (if word
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 (setq word (conx-random-succ word))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 (fill-region-as-paragraph (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 (goto-char p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 (if (= (preceding-char) ?\n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 (if (= 0 (conx-rand 4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 (insert "\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 (delete-char -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 (insert " "))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 (defun conx ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 "Generate some random sentences in the *conx* buffer."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 (display-buffer (set-buffer (get-buffer-create "*conx*")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 (select-window (get-buffer-window "*conx*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 (message "type ^G to stop.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 (while t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 (goto-char (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 (sit-for (if (= (preceding-char) ?\n) 2 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 (conx-sentence)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 ;;; GNUS interface; grab words from the current message.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 (defun conx-gnus-snarf ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 "For use as a gnus-select-article-hook."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 (set-buffer gnus-article-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 (save-restriction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 (widen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 (search-forward "\n\n" nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 (conx-region (point) (point-max)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 ;;(add-hook 'gnus-select-article-hook 'conx-gnus-snarf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 (defun psychoanalyze-conx ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 "Mr. Random goes to the analyst."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 (doctor) ; start the psychotherapy
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 (message "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 (switch-to-buffer "*doctor*")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 (sit-for 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 (while (not (input-pending-p))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 (conx-sentence)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 (if (= (random 2) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 (conx-sentence))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 (sit-for 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 (doctor-ret-or-read 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 ;;; Saving the database
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 (defun conx-save (file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 "Save the current CONX database to a file for future retrieval.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 You can re-load this database with the \\[conx-load] command."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 (interactive "FSave CONX corpus to file: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 (let (b)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 (set-buffer (setq b (get-buffer-create "*conx-save-tmp*")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 (delete-region (point-min) (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 (insert ";;; -*- Mode:Emacs-Lisp -*-\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 (insert ";;; This is a CONX database file. Load it with `conx-load'.\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 (if conx-files
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 (insert ";;; Corpus: " (mapconcat 'identity conx-files ", ") "\n"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 (insert ";;; Date: " (current-time-string) "\n\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 ;; The file format used here is such a cute hack that I'm going to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 ;; leave it as an excercise to the reader to figure it out.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 (let ((p (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 (fill-column 78)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 (fill-prefix "\t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 (i 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 (insert "(!! [\t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 (while (< i conx-words-vector-fp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 (prin1 (aref conx-words-vector i) (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 (insert " ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 (setq i (1+ i)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 (insert "])\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 (fill-region-as-paragraph p (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 (insert "\n"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 (mapatoms (function (lambda (sym)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 (if (not (boundp sym))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 (insert "\(! ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 (prin1 sym (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 (insert " ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 (prin1 (symbol-value sym) (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 (insert "\)\n"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 conx-words-hashtable)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 (while (re-search-forward "\\bnil\\b" nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 (replace-match "()"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 (set-visited-file-name file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 (save-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 (and b (kill-buffer b)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 (defun conx-load (file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 "Load in a CONX database written by the \\[conx-save] command.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 This clears the database currently in memory."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 (interactive "fLoad CONX corpus from file: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 (conx-init)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 (fset (intern "!!" conx-words-hashtable)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 (function (lambda (vec)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 (setq conx-words-vector vec
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 conx-words-vector-fp (length vec)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 (fset (intern "!" conx-words-hashtable)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 (symbol-function 'setq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 (let ((obarray conx-words-hashtable))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 (load file)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 ;;; Emitting C code
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 (defun conx-emit-c-data (&optional ansi-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 (let ((all '())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 (standard-output (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 (after-change-functions nil) ; turning off font-lock speeds it up x2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 (before-change-functions nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 (after-change-function nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 (before-change-function nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 (float-output-format "%.2f")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 count total total100)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 (or conx-words-hashtable (error "no words"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 (let ((i 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 (mapatoms (function (lambda (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 (if (boundp x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 (setq all (cons (cons i x) all)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 i (1+ i)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 conx-words-hashtable))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 (setq all (nreverse all))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 (setq total (* 4 (length all))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 total100 (max 1 (if (featurep 'lisp-float-type)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 (/ (float total) 100)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 (/ total 100)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 count 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 (let ((rest all)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 (i 5)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 rest2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 (insert "static unsigned short D[] = {")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 (while rest
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 (setq word (symbol-value (cdr (car rest))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 (setq rest2 (conx-pred word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 (setq count (1+ count))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 (while rest2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 (princ (cdr (car rest2))) (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 (princ (car (rassq (car (car rest2)) all)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 (setq i (1+ i))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 (cond ((> i 10)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 (insert "\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 (setq i 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 (setq rest2 (cdr rest2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 (message "Writing C code... %s%%" (/ count total100))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 (setq count (1+ count))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 (setq rest2 (conx-succ word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 (while rest2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 (princ (cdr (car rest2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 (princ (car (rassq (car (car rest2)) all)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 (setq i (1+ i))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 (cond ((> i 10)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 (insert "\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 (setq i 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 (setq rest2 (cdr rest2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 (message "Writing C code... %s%%" (/ count total100))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 (setq count (1+ count))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 (setq rest (cdr rest))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 (insert "0};\nstatic char T[] = \"")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 (let ((rest all)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 (i 0) (j 20)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 k word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 (while rest
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 (setq word (symbol-name (cdr (car rest))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 (setq k (1+ (length word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 i (+ i k)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 j (+ j k 3))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 (cond ((> j 77)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 (insert (if ansi-p "\"\n\"" "\\\n"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 (setq j (+ k 3))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 (insert word) ; assumes word has no chars needing backslashes
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 (insert "\\000")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 (message "Writing C code... %s%%" (/ count total100))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 (setq count (1+ count))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 (setq rest (cdr rest))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 (insert "\";\nstatic struct conx_word words [] = {")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 (let ((rest all)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 (i 0) (j 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 cons name word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 (while rest
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 (setq cons (car rest)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 name (symbol-name (cdr cons))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 word (symbol-value (cdr cons)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 (insert "{") (princ (conx-count word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 (insert ",") (princ (conx-cap word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 (insert ",") (princ (conx-comma word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 (insert ",") (princ (conx-period word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 (insert ",") (princ (conx-quem word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 (insert ",") (princ (conx-bang word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 (if (null (conx-pred word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 (insert ",0")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 (princ i)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 (setq i (+ i (* 2 (length (conx-pred word))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553 (if (null (conx-succ word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 (insert ",0,")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 (princ i)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558 (setq i (+ i (* 2 (length (conx-succ word))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559 (princ (conx-pred-c word)) (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 (princ (conx-succ-c word)) (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561 (princ j)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 (setq j (+ j (length name) 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 (insert (if (cdr rest) (if (= 0 (% (car cons) 2)) "},\n" "},") "}"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564 (message "Writing C code... %s%%" (/ count total100))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565 (setq count (1+ count))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566 (setq rest (cdr rest))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568 (insert "};\n#define conx_bounce ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569 (princ conx-bounce)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 (insert "\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 (message "Writing C code... done.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 (defvar conx-c-prolog "\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575 #if __STDC__
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 #include <stddef.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 #include <unistd.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 extern long random (void);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579 extern void srandom (int);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 extern void abort (void);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583 #include <time.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 struct conx_word {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 unsigned short count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587 unsigned short cap;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588 unsigned short comma;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589 unsigned short period;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 unsigned short quem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 unsigned short bang;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 unsigned short pred;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 unsigned short succ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 unsigned short npred;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 unsigned short nsucc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596 unsigned short text;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600 (defvar conx-c-code "\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601 #define countof(x) (sizeof((x)) / sizeof(*(x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602 #define conx_rand(n) (random()%(n))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 static struct conx_word *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 conx_random_related (count, which_list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 unsigned short count, which_list;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608 unsigned short *list = D + which_list;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609 int i = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610 unsigned short foll = (count == 0 ? 0 : conx_rand (count));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611 while (1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613 if (foll <= list [i * 2])
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
615 if ((list [i * 2 + 1]) > countof (words))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617 return &words [list [i * 2 + 1]];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 foll -= list [i * 2];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620 i++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624 static struct conx_word *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625 conx_random_succ (word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 struct conx_word *word;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 if (word->nsucc == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 return word;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632 struct conx_word *next = conx_random_related (word->nsucc, word->succ);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 if (conx_rand (conx_bounce) != 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634 return next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635 return conx_random_succ (conx_random_related (next->npred, next->pred));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640 conx_sentence ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
642 static int x = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
643 struct conx_word *word = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644 int first_p = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645 int done = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 int count = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647 while (!done)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649 int punc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650 char *text;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 int L;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652 if (word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 word = conx_random_succ (word);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655 word = &words [conx_rand (countof (words))];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 count++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657 punc = conx_rand (word->count);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658 text = T + word->text;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 L = strlen (text);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660 if (x + L > 70)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
662 putchar ('\\n');
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
663 x = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
664 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
665 x += L+1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667 if (first_p || (word->count == word->cap))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669 putchar ((*text >= 'a' && *text <= 'z') ? *text + ('A'-'a') : *text);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670 fputs (text+1, stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671 first_p = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
672 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674 fputs (text, stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676 if (punc < word->comma)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
677 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
678 fputs (\", \", stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
679 x++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681 else if ((punc -= word->comma) < word->period)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683 x++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 if (count > 120 || conx_rand (5) != 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 done = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
687 fputs (\". \", stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688 x++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
691 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
692 word = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693 if (conx_rand (4) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694 fputs (\": \", stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
695 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
696 fputs (\"; \", stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
697 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
698 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699 else if ((punc -= word->period) < word->quem)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
700 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
701 done = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
702 fputs (\"? \", stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
703 x += 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705 else if ((punc -= word->quem) < word->bang)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
706 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
707 done = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
708 fputs (\"! \", stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709 x += 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
710 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
711 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
712 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
713 if (word->nsucc == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
715 fputs (\". \", stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
716 x += 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
717 done = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
719 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
720 putchar (' ');
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
721 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
722 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
723 if (conx_rand (3) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
725 fputs (\"\\n\\n\", stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
726 x = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
727 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730 main (argc, argv)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
731 int argc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
732 char **argv;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
733 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
734 unsigned int howmany, delay;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
735 char dummy;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
736 if (argc == 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
737 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
738 howmany = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
739 delay = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
740 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
741 else if (argc == 2 &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
742 1 == sscanf (argv[1], \"%ud%c\", &howmany, &dummy))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
743 delay = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744 else if (argc == 3 &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 1 == sscanf (argv[1], \"%ud%c\", &howmany, &dummy) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
746 1 == sscanf (argv[2], \"%ud%c\", &delay, &dummy))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
747 ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
748 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
749 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
750 fprintf (stderr, \"usage: %s [count [delay]]\\n\", argv [0]);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
751 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
752 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
753
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
754 srandom (time (0));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
755 if (howmany == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
756 howmany = ~0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
757 while (howmany > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
758 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759 conx_sentence ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
760 fflush (stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
761 howmany--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
762 if (delay) sleep (delay);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
763 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
764 putchar ('\\n');
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
765 exit (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
766 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767 ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
768
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
769 (defun conx-emit-c (file &optional non-ansi-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
770 "Write the current CONX database to a file as C source code.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
771 The generated program will have the same effect as M-x conx,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
772 except that it runs without emacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
773
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
774 With a prefix argument, write K&R C instead of ANSI C. ANSI is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
775 the default because, without a certain ANSI feature, large databases
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
776 will overflow static limits in most K&R preprocessors."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
777 (interactive "FWrite C file: \nP")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
778 (find-file file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
779 (erase-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
780 (let ((buffer-undo-list t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
781 (insert conx-c-prolog)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
782 (if (not non-ansi-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
783 (insert "\n#if !__STDC__\n"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
784 "error! this file requires an ANSI C compiler\n"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
785 "#endif\n\n"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
786 (conx-emit-c-data (not non-ansi-p))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
787 (insert conx-c-code))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788 (goto-char (point-min)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
789
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
790
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
791 ;;; Reporting stats
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
792
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
793 (defun conx-stats ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
794 (set-buffer (get-buffer-create "*conx-stats*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
795 (delete-region (point-min) (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
796 (mapatoms (function (lambda (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
797 (or (not (boundp x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
798 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
799 (insert (format "%s" (conx-count (symbol-value x))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
800 (insert "\t\t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
801 (insert (symbol-name x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
802 (insert "\n")))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
803 conx-words-hashtable)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
804 (sort-numeric-fields -1 (point-min) (point-max)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
805
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
806 ;;; conx.el ends here