home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / functions / insert-random-quote.el < prev    next >
Encoding:
Text File  |  1993-03-03  |  2.4 KB  |  67 lines

  1. ;; Insert a random quote from a file.
  2. ;; Copyright (C) 1992 Joe Wells
  3.  
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation; either version 2 of the License, or
  7. ;; (at your option) any later version.
  8.  
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ;; GNU General Public License for more details.
  13.  
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program; if not, write to the Free Software
  16. ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. ;; Created by: Joe Wells, jbw@bigbird.bu.edu
  19. ;; Created on: Sat Oct  3 23:26:08 1992
  20. ;; Last modified by: Dave Brennan
  21. ;; Last modified on: Thu Mar  4 11:20:29 1993
  22. ;; Filename: insert-random-quote.el
  23. ;; Purpose: insert a random quote from a file
  24.  
  25. ;; LCD Archive Entry:
  26. ;; insert-random-quote|Joe Wells|jbw@cs.bu.edu|
  27. ;; Insert a random quote from a file|
  28. ;; 1992-10-07||~/functions/insert-random-quote.el.Z|
  29.  
  30. (defvar random-quote-file (expand-file-name "~/.quotes")
  31.   "*Pathname of a file containing quotes terminated by \"\\n%%\\n\".
  32. Any text past the last terminator is ignored.")
  33.  
  34. (defun insert-random-quote ()
  35.   "Inserts a randomly selected quote from random-quote-file.
  36. If invoked twice in a row by keystroke, deletes the previously selected
  37. random quote before inserting the new one.  Indents the inserted quote by
  38. two columns."
  39.   (interactive)
  40.   (let ((quote-buf (let ((inhibit-local-variables nil))
  41.              (find-file-noselect random-quote-file)))
  42.     (rnd (random (not (equal last-command 'insert-random-quote))))
  43.     (num-quotes 0)
  44.     beg end insert-point)
  45.     (if (< rnd 0)
  46.     (setq rnd (- (1+ rnd))))
  47.     (save-excursion
  48.       (set-buffer quote-buf)
  49.       (bury-buffer quote-buf)
  50.       (goto-char (point-min))
  51.       (while (search-forward "\n%%\n" nil t)
  52.     (setq num-quotes (1+ num-quotes)))
  53.       (setq rnd (% rnd num-quotes))
  54.       (goto-char (point-min))
  55.       (or (= rnd 0)
  56.       (search-forward "\n%%\n" nil nil rnd))
  57.       (setq beg (point))
  58.       (search-forward "\n%%\n")
  59.       (setq end (1+ (match-beginning 0))))
  60.     (cond ((equal last-command 'insert-random-quote)
  61.        (delete-region (point) (mark))
  62.        (undo-boundary))
  63.       (t
  64.        (push-mark)))
  65.     (insert-buffer-substring quote-buf beg end)
  66.     (indent-rigidly (mark) (point) 2)))
  67.