;;;	Elisp minutes hack to make taking minutes fun 
;;;	 (and if you believe that...)
;;;     kerr 9/3/88

;; To autoload this from your .emacs, add:
;; (autoload 'minutes "/afs/sipb.mit.edu/admin/minutes/minutes"
;;   "Loads a minutes template into a buffer if none exists else switches to it" t)
;; (autoload 'sipb-minutes-mode "/afs/sipb.mit.edu/admin/minutes/minutes"
;;   "Major mode for editing SIPB minutes files." t)

(defvar attach-program "/bin/athena/attach")
(defvar attach-directory "charon:u1")
(defvar attach-test-file "/afs/sipb.mit.edu/admin/minutes")
(defvar minutes-dir "/afs/sipb.mit.edu/admin/minutes")
(defvar minutes-format "minutes-format.text")
(defvar minutes-file-prefix "minutes")

(defun name-of-maybe-attached-file (file-name)
  (cond ((file-exists-p file-name) file-name)
	((file-exists-p attach-test-file) (error "No such template %s" file-name))
	(t (call-process attach-program nil nil nil attach-directory)
	   (name-of-maybe-attached-file file-name))))

;;; Generic major mode stuff.
(defvar sipb-minutes-mode-hook nil
  "Normal hook run when entering `sipb-minutes-mode'.")

(defvar sipb-minutes-mode-abbrev-table nil
  "Abbrev table used while in `sipb-minutes-mode'.")
(if sipb-minutes-mode-abbrev-table
    ()
  (define-abbrev-table 'sipb-minutes-mode-abbrev-table
    '(("jwoc"
       "at the discretion of the Office Czar in Charge of Special Operations"
       nil 0))))

(defvar sipb-minutes-mode-syntax-table nil
  "Syntax table used while in `sipb-minutes-mode'.")
(if sipb-minutes-mode-syntax-table
    ()
  (setq sipb-minutes-mode-syntax-table text-mode-syntax-table)
  )

(defvar sipb-minutes-mode-map nil
  "Keymap for `sipb-minutes-mode'.")
(if sipb-minutes-mode-map
    ()
  (setq sipb-minutes-mode-map text-mode-map)
  (define-key sipb-minutes-mode-map "\t" 'tab-to-tab-stop)
  )

(defvar sipb-minutes-mode-keywords nil
  "Font lock keywords used while in `sipb-minutes-mode'.")
(if sipb-minutes-mode-keywords
    ()
  (setq sipb-minutes-mode-keywords
	'(("^\t\\<\\([^:\n]+:\\)" (1 font-lock-variable-name-face))
	  ("^\\([^:\n]+:\\)$" (1 font-lock-keyword-face))
	  ("\\(\\*\\*\\*[^*]*\\*\\*\\*\\)" (1 font-lock-warning-face))
	  ("\\(\\[[^]]*\\]\\)" (1 font-lock-string-face)))))

(defun sipb-minutes-mode ()
  "Major mode for editing SIPB minutes files.
This is essentially derived from `text-mode'; see its documentation for
more details.  The `minutes' command can be used to automagically create
an appropriate buffer for editing current minutes.
\\{sipb-minutes-mode-map}
Turning on SIPB minutes mode runs the normal hooks `text-mode-hook' and
`sipb-minutes-mode-hook'."
  (interactive)
  (kill-all-local-variables)
  (use-local-map sipb-minutes-mode-map)
  (setq local-abbrev-table sipb-minutes-mode-abbrev-table)
  (set-syntax-table sipb-minutes-mode-syntax-table)
  (make-local-variable 'paragraph-start)
  (setq paragraph-start (concat page-delimiter "\\|[ \t]*$"))
  (make-local-variable 'paragraph-separate)
  (setq paragraph-separate paragraph-start)
  (setq fill-prefix "		")
  (setq mode-name "SIPB-Minutes")
  (setq major-mode 'sipb-minutes-mode)
  (setq font-lock-defaults '(sipb-minutes-mode-keywords nil t))
  (run-hooks 'text-mode-hook)
  (run-hooks 'sipb-minutes-mode-hook)
  (abbrev-mode 1)
)

(defun substitute (key replacement)
  (search-forward key)
  (delete-backward-char (length key))
  (insert replacement))

(defun minutes ()
  "Loads a minutes template into a buffer if none exists else switches to it"
  (interactive)
  (let* ((date (format-time-string "%Y-%m-%d"))
	 (file-name (format "%s.%s" minutes-file-prefix date))
	 (file-path-name (concat minutes-dir "/" file-name))
	 (maybe-buffer (get-buffer file-name)))
    (cond ((not maybe-buffer) 
	   (message "Loading in a new minutes template...") 
	   (set-buffer (get-buffer-create file-name))
	   (setq buffer-file-name file-path-name) 
	   (insert-file (name-of-maybe-attached-file (concat minutes-dir "/" minutes-format)))
	   (switch-to-buffer file-name)
	   (sipb-minutes-mode)
	   (substitute "<date>" date)
	   (substitute "<name>" (user-login-name))
	   (set-buffer-modified-p nil)
	   (goto-char (point-min))
	   (search-forward "at ")
	   (cond ((file-exists-p file-name) (format "Warning %s already exists" file-path-name))))
	  ((eq (current-buffer) maybe-buffer))
	  ((get-buffer-window maybe-buffer) (select-window (get-buffer-window maybe-buffer)))
	  (t (switch-to-buffer file-name)))))

(provide 'sipb-minutes)
