;;; fortune.el --- Interface to write fortunes quickly

;; Copyright (C) 1999 by Association April

;; Author: Benjamin Drieu <drieu@bocal.cs.univ-paris8.fr>
;; Keywords: unix, games

;; This file is NOT part of GNU Emacs.

;; GNU Emacs as well as this software are free software; you can
;; redistribute them and/or modify them 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 software 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 GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; This simple hack allows you to write fortunes into bases.  This is
;; very simple, and the only goal was my amusement in emacs-lisp
;; programming, and having fun with friends citations.

;;; $Id: fortune.el,v 1.1.1.1 2002/07/01 17:04:37 benj Exp $

;;; $Log: fortune.el,v $
;;; Revision 1.1.1.1  2002/07/01 17:04:37  benj
;;; - version initiale
;;;
;;; Revision 1.2  1999/07/14 20:49:52  benj
;;; - Fix minors bugs
;;; - Document more
;;; - Add $HOME in fortune-directory (much cleaner)
;;;

;;; Code:


(defvar fortune-buffer "*FORTUNE*")
(defvar fortune-keymap nil "Fortune keymap")
(defvar fortune-directory (concat (getenv "HOME") "/.fortune/") "*User dir where fortunes are stored")
(defvar fortune-cite-string "\t\t\t\t--" "*String to insert when citing an author")



(defun fortune-get-filename ()
  (let ((insert-default-directory nil))
    (concat fortune-directory (read-file-name "Write to base: " 
					      fortune-directory 
					      nil
					      nil))))



(defun fortune-save-fortune ()
  "Actualy appends the fortune buffer to the end of a fortune file"
  (beginning-of-buffer)
  (let ((filename (fortune-get-filename)))
    (cond
     ((file-exists-p filename)
      (insert-char ?% 1)
      (newline)
      (append-to-file (point-min) (point-max) filename)
      t)
     (t
      (cond
       ((y-or-n-p (concat filename " doesn't exist.  Create ?"))
	(append-to-file (point-min) (point-max) filename)))))))




(defun fortune-write ()
  "Write the fortune to the base"
  (interactive)
  (end-of-buffer)
  (newline)
  (delete-blank-lines)
  (cond
   ((fortune-save-fortune)
    (if (not buffer-file-name)
	(progn
	  (set-buffer-modified-p nil)
	  (delete-auto-save-file-if-necessary t)))
    (delete-window (selected-window)))))




(defun fortune-cite-author ()
  "Insert the \"cite author\" string at point.  Go to begining of line if necessary"
  (interactive)
  (beginning-of-line)
  (insert fortune-cite-string))




(defun fortune-create-window ()
  (cond 
   ((get-buffer-window fortune-buffer))
   (t
    (let ((w (window-at 0 (- (frame-height) 3))))
      (if (< (window-height w) 19)
	  w
	(split-window w (- (window-height w) 10)))))))




(defvar fortune-mode-syntax-table nil
  "Syntax table used while in fortune mode")
(if fortune-mode-syntax-table
    ()

  (setq fortune-mode-syntax-table (make-syntax-table))

  (defvar fortune-mode-abbrev-table nil
    "Abbrev table used while in fortune mode")
  (define-abbrev-table 'fortune-mode-abbrev-table ())

  (defvar fortune-mode-map nil)
  (if fortune-mode-map
      ()
    (setq fortune-mode-map (make-sparse-keymap "fortune-keymap"))
    (define-key fortune-mode-map "\C-c\C-a" 'fortune-cite-author)
    (define-key fortune-mode-map "\C-c\C-c" 'fortune-write)))
  
  


(defun fortune-mode ()
  "Major mode for fortunes editing"
  (interactive)
  (kill-all-local-variables)
  (use-local-map fortune-mode-map)
  (setq local-abbrev-table fortune-mode-abbrev-table)
  (set-syntax-table fortune-mode-syntax-table)
  (setq mode-name "Fortune")
  (setq major-mode 'fortune-mode)
  (run-hooks 'fortune-mode-hook))




(defun fortune ()
  "Interface to add fortunes to various bases"
  (interactive)
  (let ((w (fortune-create-window)))
    (select-window w)
    (switch-to-buffer (get-buffer-create fortune-buffer))
    (erase-buffer)
    (fortune-mode)))


;;; fortune.el ends here
