Better seed support for (random).
[bpt/emacs.git] / lisp / play / dissociate.el
CommitLineData
55535639 1;;; dissociate.el --- scramble text amusingly for Emacs
c0274f38 2
acaf905b 3;; Copyright (C) 1985, 2001-2012 Free Software Foundation, Inc.
9750e079 4
2f14b48d 5;; Maintainer: FSF
e5167999 6;; Keywords: games
2f14b48d 7
a2535589
JA
8;; This file is part of GNU Emacs.
9
b1fc2b50 10;; GNU Emacs is free software: you can redistribute it and/or modify
a2535589 11;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
a2535589
JA
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
b1fc2b50 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
a2535589 22
e41b2db1
ER
23;;; Commentary:
24
eb8c3be9 25;; The single entry point, `dissociated-press', applies a travesty
e41b2db1
ER
26;; generator to the current buffer. The results can be quite amusing.
27
2f14b48d 28;;; Code:
a2535589 29
f9f9507e 30;;;###autoload
a2535589
JA
31(defun dissociated-press (&optional arg)
32 "Dissociate the text of the current buffer.
33Output goes in buffer named *Dissociation*,
34which is redisplayed each time text is added to it.
35Every so often the user must say whether to continue.
36If ARG is positive, require ARG chars of continuity.
37If ARG is negative, require -ARG words of continuity.
38Default is 2."
39 (interactive "P")
40 (setq arg (if arg (prefix-numeric-value arg) 2))
41 (let* ((inbuf (current-buffer))
42 (outbuf (get-buffer-create "*Dissociation*"))
43 (move-function (if (> arg 0) 'forward-char 'forward-word))
44 (move-amount (if (> arg 0) arg (- arg)))
45 (search-function (if (> arg 0) 'search-forward 'word-search-forward))
46 (last-query-point 0))
8bce0867
RS
47 (if (= (point-max) (point-min))
48 (error "The buffer contains no text to start from"))
a2535589
JA
49 (switch-to-buffer outbuf)
50 (erase-buffer)
51 (while
52 (save-excursion
53 (goto-char last-query-point)
54 (vertical-motion (- (window-height) 4))
55 (or (= (point) (point-max))
56 (and (progn (goto-char (point-max))
57 (y-or-n-p "Continue dissociation? "))
58 (progn
59 (message "")
60 (recenter 1)
61 (setq last-query-point (point-max))
62 t))))
63 (let (start end)
937e6a56 64 (with-current-buffer inbuf
a2535589
JA
65 (setq start (point))
66 (if (eq move-function 'forward-char)
67 (progn
68 (setq end (+ start (+ move-amount (random 16))))
69 (if (> end (point-max))
70 (setq end (+ 1 move-amount (random 16))))
71 (goto-char end))
72 (funcall move-function
73 (+ move-amount (random 16))))
74 (setq end (point)))
75 (let ((opoint (point)))
76 (insert-buffer-substring inbuf start end)
77 (save-excursion
78 (goto-char opoint)
79 (end-of-line)
80 (and (> (current-column) fill-column)
81 (do-auto-fill)))))
937e6a56 82 (with-current-buffer inbuf
a2535589
JA
83 (if (eobp)
84 (goto-char (point-min))
85 (let ((overlap
86 (buffer-substring (prog1 (point)
87 (funcall move-function
88 (- move-amount)))
89 (point))))
82305cbd 90 (goto-char (1+ (random (1- (point-max)))))
a2535589
JA
91 (or (funcall search-function overlap nil t)
92 (let ((opoint (point)))
93 (goto-char 1)
94 (funcall search-function overlap opoint t))))))
95 (sit-for 0))))
c0274f38 96
896546cd
RS
97(provide 'dissociate)
98
c0274f38 99;;; dissociate.el ends here