;; pick-a-string.el -- Pick a random string from a file. ;; -*- Mode: Emacs-Lisp -*- ;; Copyright(C) 2003 Daisuke Kakura "2003-11-04 Tue 02:11:02 CCT" ;; This program 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 2, or (at your option) ;; any later version. ;; ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with this program; if not, you can either send email to this ;; program's maintainer or write to: The Free Software Foundation, ;; Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA. ;; HISTORY: ;; ;; 2003-11-01 v0.1 Daisuke Kakura: created. ;; INSTALL: ;; ;; (require 'pick-a-string) ;; HOW TO USE: ;; ;; `pick-a-string' randomly picks a string from a file and displays it ;; to mini-buffer. The each data string should be terminated by ;; `pick-a-string-terminator'. ;; ;; `pick-a-string-and-insert' inserts the string where the cursor is. ;; ;; `pick-a-string-and-insert-last-one' inserts the last picked string. (defvar pick-a-string-file (expand-file-name "~/.pick-a-string") "Name of a file containing strings terminated with `pick-a-string-terminater'.") (defvar pick-a-string-terminator "\n%\n" "Terminator character or string between data strings in `pick-a-string-file'.") (defvar pick-a-string-last-one nil "The last picked string.") (defun pick-a-string (&optional silent) "Randomly pick a string from a file and display it to mini-buffer. If &optional argument t was given, nothing appears in mini-buffer." (interactive) (let ((buf (find-file-noselect pick-a-string-file)) (num-strings 0) random-num beg end) (save-excursion ;; count delimiters. (set-buffer buf) (goto-char (point-min)) (while (search-forward pick-a-string-terminator nil t) (setq num-strings (1+ num-strings))) ;; generate a random number. (random t) (setq random-num (random num-strings)) ;; then pick it up. (goto-char (point-min)) (search-forward pick-a-string-terminator nil nil random-num) (setq beg (point)) (search-forward pick-a-string-terminator) (setq end (match-beginning 0)) (goto-char beg) (setq pick-a-string-last-one (buffer-substring-no-properties beg end)) ;; done. (kill-buffer buf))) (if silent pick-a-string-last-one (message pick-a-string-last-one))) (defun pick-a-string-and-insert () "Insert a random string at current point." (interactive) (pick-a-string) (insert pick-a-string-last-one)) (defun pick-a-string-and-insert-last-one () "Insert the last picked string at current point." (interactive) (insert pick-a-string-last-one)) (provide 'pick-a-string) ;;; pick-a-string.el ends here.