dissociate.el 3.1 KB

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