]> code.delx.au - gnu-emacs/blobdiff - lisp/play/dissociate.el
Merge from emacs-23
[gnu-emacs] / lisp / play / dissociate.el
index cfc29d902a0abbe0ada07cb7449f7f2761fe658b..17ec375035fa92e9eab7f3ee16ab371681ee5077 100644 (file)
@@ -1,13 +1,17 @@
-;;; dissociate.el --- scramble text amusingly for Emacs.
+;;; dissociate.el --- scramble text amusingly for Emacs
 
-;; Copyright (C) 1985 Free Software Foundation, Inc.
+;; Copyright (C) 1985, 2001, 2002, 2003, 2004, 2005,
+;;   2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
+
+;; Maintainer: FSF
+;; Keywords: games
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 1, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to
-;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; The single entry point, `dissociated-press', applies a travesty
+;; generator to the current buffer.  The results can be quite amusing.
 
+;;; Code:
 
 ;;;###autoload
 (defun dissociated-press (&optional arg)
@@ -36,6 +45,8 @@ Default is 2."
         (move-amount (if (> arg 0) arg (- arg)))
         (search-function (if (> arg 0) 'search-forward 'word-search-forward))
         (last-query-point 0))
+    (if (= (point-max) (point-min))
+       (error "The buffer contains no text to start from"))
     (switch-to-buffer outbuf)
     (erase-buffer)
     (while
@@ -51,8 +62,7 @@ Default is 2."
                   (setq last-query-point (point-max))
                   t))))
       (let (start end)
-       (save-excursion
-        (set-buffer inbuf)
+       (with-current-buffer inbuf
         (setq start (point))
         (if (eq move-function 'forward-char)
             (progn
@@ -70,8 +80,7 @@ Default is 2."
           (end-of-line)
           (and (> (current-column) fill-column)
                (do-auto-fill)))))
-      (save-excursion
-       (set-buffer inbuf)
+      (with-current-buffer inbuf
        (if (eobp)
           (goto-char (point-min))
         (let ((overlap
@@ -79,13 +88,16 @@ Default is 2."
                                         (funcall move-function
                                                  (- move-amount)))
                                  (point))))
-          (let (ranval)
-            (while (< (setq ranval (random)) 0))
-            (goto-char (1+ (% ranval (1- (point-max))))))
+          (goto-char (1+ (random (1- (point-max)))))
           (or (funcall search-function overlap nil t)
               (let ((opoint (point)))
                 (goto-char 1)
                 (funcall search-function overlap opoint t))))))
       (sit-for 0))))
 
+(random t)
+
+(provide 'dissociate)
+
+;; arch-tag: 90d197d1-409b-45c5-a0b5-fbfb2e06334f
 ;;; dissociate.el ends here