| 1 | ;;; dissociate.el --- scramble text amusingly for Emacs |
| 2 | |
| 3 | ;; Copyright (C) 1985, 2001-2014 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Maintainer: emacs-devel@gnu.org |
| 6 | ;; Keywords: games |
| 7 | |
| 8 | ;; This file is part of GNU Emacs. |
| 9 | |
| 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 11 | ;; it under the terms of the GNU General Public License as published by |
| 12 | ;; the Free Software Foundation, either version 3 of the License, or |
| 13 | ;; (at your option) any later version. |
| 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 |
| 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | ;;; Commentary: |
| 24 | |
| 25 | ;; The single entry point, `dissociated-press', applies a travesty |
| 26 | ;; generator to the current buffer. The results can be quite amusing. |
| 27 | |
| 28 | ;;; Code: |
| 29 | |
| 30 | ;;;###autoload |
| 31 | (defun dissociated-press (&optional arg) |
| 32 | "Dissociate the text of the current buffer. |
| 33 | Output goes in buffer named *Dissociation*, |
| 34 | which is redisplayed each time text is added to it. |
| 35 | Every so often the user must say whether to continue. |
| 36 | If ARG is positive, require ARG chars of continuity. |
| 37 | If ARG is negative, require -ARG words of continuity. |
| 38 | Default 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)) |
| 47 | (if (= (point-max) (point-min)) |
| 48 | (error "The buffer contains no text to start from")) |
| 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) |
| 64 | (with-current-buffer inbuf |
| 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))))) |
| 82 | (with-current-buffer inbuf |
| 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)))) |
| 90 | (goto-char (1+ (random (1- (point-max))))) |
| 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)))) |
| 96 | |
| 97 | (provide 'dissociate) |
| 98 | |
| 99 | ;;; dissociate.el ends here |