(defvar this-barfed nil)
(defvar student "???")
(defvar comment-file-default "comments")
(defvar style-directory "/mit/eos/emacs/style/")
(defvar default-accessed-file "/mit/eos/emacs/style/accessed")
(defvar default-chapter "contents")
;;;some of these have to be hacked to be the right place

(setq max-lisp-eval-depth 1000)
(setq indent-tabs-mode nil)
(setq default-major-mode 'text-mode)
(setq text-mode-hook '(lambda () (auto-fill-mode 1)))

(put 'eval-expression 'disabled nil) ;;get rid of this someday

(defun file-does-not-exist ()
  "called by (retrieve-style-guide-entry).  This sets things up
so that if you ask for a non-existent chapter, you get the contents."
  ;;this whole thing is probably done better somehow
  (message "")
  (setq this-barfed t)
  (find-file "contents"))

(defun retrieve-style-guide-entry (style-chapter) "pulls up a chapter
of your choice from the 21_780 style guide.  If you do not know which
chapter you want, type: 'contents'"
;  (setq find-file-run-dired nil)
  (setq reset-directory default-directory)
  (setq default-directory style-directory)
  (setq find-file-not-found-hooks (list 'file-does-not-exist))
;;prompts for chapter
  (interactive "sstyle chapter (type 'contents' for listing of options):")

;;finds the chapter if it is there - if the file is not there, mild lossage:
;;you get an empty bottom screen.  fix this someday.
  (find-file-other-window style-chapter)

  (if this-barfed
      (kill-buffer style-chapter))

  (setq this-barfed nil)

  ;;appends the style chapter and the date to access-file so that we can keep
;;track of style guide usage.
  (shell-command (concat "(echo " style-chapter ";date) >> " default-accessed-file))

;keep people from writing jibberish into the style manual even if the
;file is not write protected by unix.                                

  (setq buffer-read-only t)  

;;puts your cursor in your own file
  (other-window 1)

;;help to figure out how to get to the style chapter window to browse
                
  (message "C-xo toggles between windows.  C-x0 kills window.  C-x1 kills other windows")
;  (setq find-file-run-dired t)  
  (setq default-directory reset-directory)
    )


(defun format-for-light-valve ()
  "formats paragraphs to forty columns and justifies them 10 columns from
   the left.  This is so that things will show up nicely in a giant font
   displayed on a light valve."

  (interactive)
  (save-excursion
  (let ((return-fill-column fill-column))
    ;; the current fill column to go
    ;; back to when you are done formatting

    ;; squashing things to 50 columns and indenting
    (set-fill-column 50) 
    (message "This may take a while, especially if your file is large.")
    (fill-region (point-min) (point-max) t)
    (indent-region (point-min) (point-max) 10);;identing

    ;;setting things back the way they were before all this started
    (set-fill-column return-fill-column)
    (message "") ;clears jiberish
    )))


(defun hide-annotations ()"Hides all lines that start more than 15 columns
from the left.  Comments are hidden so that the text is easier to read without
the distraction of the comments."
  (interactive)
  (setq selective-display t)
  (save-excursion
  (goto-char 1)
    (hide-loop)
    (message "type C-cC-s to see the annotations")
    ))

(defun hide-loop () "called by hide-annotations.  Looks for strings
starting with a bunch of spaces followed by a specific number of
*+*+*+*+'s which indicates the beginning of a comment.  Then, it looks
for the end of a comment (+*+*+*+*+) and makes everything inbetween
invisible. "
       (if (equal (re-search-forward "^ [ ]*\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*" nil t) t)
             (let ((beginning-of-hide-region  (- (point) 69)))
	       (if (equal   (re-search-forward "^ [ ]*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+" nil t) t)
                   (progn
		   (outline-flag-region beginning-of-hide-region
					(+ (point) 1)  ?\^M)
;;;someday make this beefy by seeing if anyone has put anything on the
;;;next line that will disappear with the point + 1 setting.		   
		   (hide-loop)
					)))))


(defun show-annotations ()"Shows annotations which have been hidden by
the (hide-annotations) command."
  (interactive)
  (outline-flag-region (point-min) (point-max) ?\n)
  (setq selective-display nil)
  (message "type C-cC-h to hide the annotations"))


;;I stole outline-flag region right out of outline.el  it is not
;; available until you go into outline mode otherwise. ><
(defun outline-flag-region (from to flag)
  "Hides or shows lines from FROM to TO, according to FLAG.  If FLAG
is \\n (newline character) then text is hidden, while if FLAG is \\^M
\(control-M) the text is shown."
  (let ((modp (buffer-modified-p)))
    (unwind-protect
        (subst-char-in-region from to
			      (if (= flag ?\n) ?\^M ?\n)
			      flag t)
     (set-buffer-modified-p modp))))

(defun continue-annotation ()"sets the left margin up to change an existing
comment."
  (interactive)
        (setq left-margin 16)
        (if (< (current-column) 16)
            (forward-char (- 16 (current-column))))          
        )

(defun discontinue-annotation ()"sets the left margin back to zero when you
are done changing an annotation."
  (interactive)
        (setq left-margin 0))

(defun start-annotation () "sets things up nicely to comment on a document."
  (interactive)
  (auto-fill-mode 0)
  (end-of-line nil)
  (insert "

                *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
                ")
  (auto-fill-mode 1)
  (setq left-margin 16))

(defun end-annotation () "undoes things that were set up in (start-annotaion)"
  (interactive)
  (insert "
                +*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
")
  (setq left-margin 0))

(defun append-loop (filename)
;;; goes through file looking for beginning and ends of comments, and
;;; appends the reion in between to a file.  Called by append-annotations
     (if (equal (re-search-forward "^ [ ]*\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*" nil t) t)
            (let ((begin-evaporate (- (point) 68)))

  (re-search-forward "^ [ ]*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+\\*\\+" nil t)
              
                     (append-to-file begin-evaporate (+ (point) 1) filename)
                     (append-loop filename))
             (message "")))

(defun append-annotations (filename) "appends the date and copies of all
the annotations from a document to a file."
    (interactive "Fappend to file:")
    (save-excursion
       (goto-char 1)
       (shell-command (concat "date >> " (expand-file-name filename)) )
       (append-loop filename)
       ))

(defun margin-for-style-guide ()
  "toggles the left margin between 5 and 0 so that things aren't jammed up
against the left margin when you look at them on the screen"
	(interactive)
	(message "foo")
	(if (equal left-margin 5)
	    (setq left-margin 0)
	  (setq left-margin 5))
	(message "left margin=%d" left-margin))

(global-set-key "\C-c\C-a" 'start-annotation)
(global-set-key "\C-c\C-c" 'continue-annotation)
(global-set-key "\C-c\C-d" 'discontinue-annotation)
(global-set-key "\C-c\C-e" 'end-annotation)
(global-set-key "\C-c\C-g" 'margin-for-style-guide)
(global-set-key "\C-c\C-h" 'hide-annotations)
(global-set-key "\C-c\C-l" 'format-for-light-valve)
(global-set-key "\C-c\C-p" 'append-annotations)
(global-set-key "\C-c\C-s" 'show-annotations)
(global-set-key "\C-c\C-y" 'retrieve-style-guide-entry)

(setq completion-ignored-extensions
      '(".o" ".elc" "~" ".bak" ".ckp" ".out" ".lpt" ".PS"))

(setq scribe-mode-hook '(lambda () (auto-fill-mode 1)))
(setq text-mode-hook '(lambda () (auto-fill-mode 1)))
(setq mail-mode-hook '(lambda () (auto-fill-mode 1)))

(setq auto-mode-alist
      '(("\\.mss" . scribe-mode)
        ("\\.c$" . c-mode)
        ("\\.h$" . c-mode)
        ("\\.l$" . lisp-mode)
        ("\\.scm$" . scheme-mode)
	("\\.info" . text-mode)
	("\\.1" . nroff-mode)
        ("\\.el$" . emacs-lisp-mode)))
