;(setq Info-directory "/afs/athena.mit.edu/project/gnu/info")
;;; Make Text Mode the default major mode with auto-fill-mode turned on.
(setq default-major-mode '(lambda () (interactive)
			    (text-mode)
			    (auto-fill-mode 1))
      ;; Gimme a load path with my directory in it.
      load-path (append
		 '("/afs/athena/user/g/a/gamadrid/elisp"
		   "/afs/athena/contrib/sipb/lib/elisp"
		   "/afs/athena/user/k/k/kkkken/elisp"
		   "/afs/athena/user/k/k/kkkken/lib/elisp"
		   "/afs/athena/user/b/j/bjaspan/elisp"
		   )
		 load-path)
      auto-mode-alist '(("\\.C$" . c++-mode)
			("\\.c$" . c-mode)
			("\\.h$" . c-mode)
			("\\.H$" . c++-mode)
			("\\.y$" . c-mode)
			("\\.l$" . c-mode)
			("\\.tex$" . TeX-mode)
			("\\.el$" . emacs-lisp-mode)
			("\\.gwm$" . scheme-mode)
			("\\.clu$" . clu-mode)
			("\\.equ$" . clu-mode)
			("\\.pl$"  . perl-mode)
			("\\.lisp$" . clisp-mode))
      rmail-file-name "~/private/RMAIL"
      )

(setq inhibit-startup-message t)

(load-library "mutant-scrolling")
(load-library "zwgc")

(fmakunbound 'c-mode)
(autoload 'c-mode "c-mode" "" t nil)
(autoload 'clu-mode "clu-mode" "" t nil)
(autoload 'c++-mode "c++-mode" "" t nil)
(autoload 'discuss "discuss" "" t nil)
(autoload 'perl-mode "/afs/athena.mit.edu/user/m/marc/elisp/perl-mode" "" t nil)
(autoload 'perldb "/afs/athena.mit.edu/contrib/elisp/perldb" "" t nil)
(autoload 'zwrite-personal "myzwrite" "" t nil)
(autoload 'zwrite-instance "myzwrite" "" t nil)
(autoload 'zwrite-class "myzwrite" "" t nil)
(autoload 'zsend "/afs/athena/user/m/a/marc/elisp/zsend" "" t nil)
(autoload 'cmd "cmd" "" t nil)
(autoload 'cmd-goto "cmd" "" t nil)
(autoload 'znol-start "znol" "" t nil)
;(autoload 'zwgc "zwgc" "" t nil)
(autoload 'man "man" "man pages" t)
(autoload 'gnus "gnus" "Emacs NetNews reader" t)
(autoload 'clisp-startup "myclisp" "" t)
(autoload 'clisp-mode "myclisp" "" t)
(autoload 'hosttype "machtype.el" "" nil)

;(defvar clisp-program-name (concat "/mit/lucidcl/" (hosttype) "bin/climlisp"))

;;; Global bindings here
(let ((bindings '(("\eg" . goto-line)
		  ("\e\C-g" . what-line)
		  ("\ez" . nil)		;Remove whatever was there
		  ("\ez " . zwrite-personal)
		  ("\ezi" . zwrite-instance)
		  ("\ezc" . zwrite-class)
		  ("\ezs" . zsend)
		  ("\C-xf" . find-file)
		  ("\C-c\eG" . gnus)
		  ("\C-x&" . compile)
		  ("\C-x7" . compile)
		  ("\C-s" . isearch-forward-regexp)
		  ("\e\C-s" . isearch-forward)
		  ("\C-r" . isearch-backward-regexp)
		  ("\e\C-r" . isearch-backward)
		  ("\ev" . scroll-down-in-place)
		  ("\C-v" . scroll-up-in-place)
		  ("[" . nil)		; Unbind this first.
		  ("\C-z" . cmd)        ; See cmd-mode-hook about C-z
		  ("\C-\\1" . (lambda nil (interactive) (cmd-goto 1)))
		  ("\C-\\2" . (lambda nil (interactive) (cmd-goto 2)))
		  ("\C-\\3" . (lambda nil (interactive) (cmd-goto 3)))
		  ("\C-\\4" . (lambda nil (interactive) (cmd-goto 4)))
		  ("[11~" . cmd)	; See cmd-mode-hook about F1
		  ("[12~" . (lambda nil (interactive) (switch-to-buffer "*zwgc*")))
		  ("[13~" . zwgc-znol-show)
		  ("\C-h" . nil)
		  ("\C-h" . backward-delete-char-untabify)
		  )))
  (while bindings
    (define-key global-map (car (car bindings)) (cdr (car bindings)))
    (setq bindings (cdr bindings))))

;;; Stuff to make eznol work.
;;; Now just 'zwgc, znol-start' to get eznol up.
;;; Change this when bjaspan releases his znol
(defvar zwgc-prog-additional-args '("-subfile" "/mit/gamadrid/.eznol.subs")
  "*A list containing extra arguments to pass to zwgc.")

;;;; Set Major Mode Hooks
;;; Text-mode
(setq text-mode-hook '(lambda nil (progn 
				    (abbrev-mode 1)
				    (auto-fill-mode 1)
				    (setq fill-column 78))))


;;; LaTeX-mode (currently does nothing)
(setq LaTeX-mode-hook '(lambda nil (progn ())))

;;; Set mode hook for C
(setq c-mode-hook '(lambda nil (progn 
				 ;; Otherwise, you get backward-delete-untabify
				 (local-set-key "\177" 'backward-delete-char)
				 (local-set-key "\e\C-l" 'c-section-line)
				 (auto-fill-mode 1)
				 (setq fill-column 78
				       c-tab-always-indent t
				       c-auto-newline nil
				       c-indent-level 5
				       c-continued-statement-offset 3
				       c-brace-offset -5
				       c-brace-imaginary-offset 0
				       c-argdecl-indent 0
				       c-label-offset -2))))
	
;; Set mode hook for C++
(setq c++-mode-hook 
      '(lambda nil (progn 
		     (auto-fill-mode 1)
		     (local-set-key "\177"
				    'backward-delete-char) 
		     (local-set-key "\015"
				    'newline-and-indent)
		     ;; For typing in those specification comments quickly.
		     (fset 'c++-spec-comment
			   "	// Requires: jjEffects:  jjModifies: ")
		     (local-set-key "\C-ct" 'c++-spec-comment)
		     (setq 
		      fill-column 78
		      c-tab-always-indent t
		      c-auto-newline nil
		      c-indent-level 4
		      c-continued-statement-offset 4
		      c-brace-offset -4
		      c-brace-imaginary-offset 0
		      c-argdecl-indent 4
		      c-label-offset -2
		      c-friend-offset 0
		      )
		     )))

;; Set mode hook for Discuss mode
(setq discuss-mode-hooks '(lambda nil (progn
					(setq discuss-DWIM t))))

;; Set mode hook for cmd mode.
(setq cmd-mode-hook '(lambda nil (interactive)
		       ;; Make F1 in other buffers warp to cmd buffer.
		       (define-key global-map "[11~"
			 '(lambda nil
			    (interactive)
			    (switch-to-buffer "*cmd*")))
		       ;; Make F1 in cmd buffer warp to other buffer.
		       (define-key cmd-mode-map 
			 "[11~"
			 '(lambda nil
			    (interactive) 
			    (switch-to-buffer (other-buffer) t)))
		       ;; Remember working
		       (setq cmd-remember-working-directory t)
		       (setq cmd-specials (append cmd-specials '(("pushd" . cmd-pushd)
								 ("popd" . cmd-popd)
								 ("make" . cmd-make))))
		       ))
						  

;; Some functionality that kkkken left out of cmd mode.  :-)
(defun cmd-pushd (arg)
  (if cmd-remember-working-directory
      (progn
	(cmd-send-shell (concat "pushd " (substring arg 0 -1) "; echo current-directory-is $cwd\n"))
	(setq cmd-working-directory nil))
    (cmd-send-shell (concat "pushd " arg))))

(defun cmd-popd (arg)
  (if cmd-remember-working-directory
      (progn
	(cmd-send-shell (concat "popd " (substring arg 0 -1) "; echo current-directory-is $cwd\n"))
	(setq cmd-working-directory nil))
    (cmd-send-shell (concat "popd " arg))))

(defun cmd-make (arg)
  (cmd-get-cwd)
  (compile (concat "make " (substring arg 0 -1))))

(defun c-section-line ()
  (interactive)
  (beginning-of-line)
  (insert "/* ")
  (insert-char ?- (- fill-column 6))
  (insert " */\n"))

(if (equal (getenv "HOSTTYPE") "decmips")
    (defvar man-executable "/usr/athena/bin/man"))

;;; Set printer to nearest printer according to /etc/clusterinfo
(setq lpr-switches (list (getenv "LPR")))

;;; Don't check completions with these endings
(setq completion-ignored-extensions
      (append completion-ignored-extensions
	      (quote
	       (".lpt" ".PS" ".bin" ".dvi" ".o"))))
	
;;; kkkken's afs convenience hack
(defun athena-path ()
  (interactive)
  (let ((ch (read-char))
	(ch2 (read-char)))
    (insert (format "/afs/athena/user/%c/%c/%c%c" ch ch2 ch ch2))))
(global-set-key "\M-a" 'athena-path)

(defun sipb-path ()
  (interactive)
  (let ((ch (read-char)))
    (insert (format "/afs/sipb.mit.edu/user/%c" ch ch))))
(global-set-key "\M-s" 'sipb-path)

;;; hooks for kkkken's zwrite
(defvar zwrite-hook nil)
(setq zwrite-hook
      (cons '(lambda nil (local-set-key "\C-j" 'zwrite-send))
	    zwrite-hook))

;;; Start the server if DISPLAY is set.
(if (not (eq nil (getenv "DISPLAY")))
    (server-start))

;;; No more annoying novice mode!
(put 'eval-expression 'disabled nil)
(put 'narrow-to-region 'disabled nil)

;;; Speaks for itself!
(setq enable-recursive-minibuffers t)
	
;;; Change the status bar
(display-time)

;;; Zfaking hacks.
(defvar zfake-args-temp nil)
(defvar zsend-before-zwrite-hook nil)
(setq zsend-before-zwrite-hook
      (cons 'zfake-before zsend-before-zwrite-hook))
(defvar zsend-after-zwrite-hook nil)
(setq zsend-after-zwrite-hook
      (cons 'zfake-after zsend-after-zwrite-hook))
(defvar zsend-mode-hook nil)		; In case it's undefined.
(setq zsend-mode-hook
      (cons '(lambda nil (progn
			   (define-key zsend-mode-map "\C-j" 'zsend-send-zgram)
			   (auto-fill-mode 1)))
	    zsend-mode-hook))


(defun zfake-before ()
;  (debug)
  (if (string-match "^\\(.*\\)(\\([^@]*\\)\\(@.*\\)?)$" (cdr zsend-dest))
      (let* ((str (cdr zsend-dest))
	     (dest (substring str (match-beginning 1) (match-end 1)))
	     (fake-sender (substring str (match-beginning 2) (match-end 2)))
	     (fake-host (if (match-beginning 3)
			    (substring str (1+ (match-beginning 3)) (match-end 3))
			  "")))
	(if (> (length fake-sender) 0)
	    (progn
	      (setq zfake-args-temp zsend-zwrite-args)
	      (setq zsend-zwrite-args (cons "-w" (cons fake-sender zsend-zwrite-args)))
	      (if (> (length fake-host) 0)
		  (setq zsend-zwrite-args (cons "-h" (cons fake-host zsend-zwrite-args))))
	      (setq zsend-dest (cons (car zsend-dest) dest))))
	(message (format "%s:%s:%s" dest fake-sender fake-host)))))

;(defun zfake-before () )
	  
(defun zfake-after ()
  (if zfake-args-temp
      (setq zsend-zwrite-args zfake-args-temp)))

;(setq zsend-zwrite-args '("-l" "-n"))
;(setq zsend-zwrite "/afs/sipb.mit.edu/user/gamadrid/src/zwrite/zwrite.rt")
;(setq zsend-zwrite "/usr/athena/bin/zwrite")
    
;    (message "Hi George")))		;


;(defvar zsend-after-zwrite-hook nil)

;(setq zsend-after-zwrite-hook		;
;      (cons 'zfake-after zsend-after-zwrite-hook))


;; Stolen from Chee.
(defun nformat-reply-region () 
  (interactive) 
  (let ((foo (read-from-minibuffer (format "What character?"))))
  (save-excursion
    (if (< (mark) (point)) (exchange-point-and-mark))
    (while (< (point) (mark)) 
      (beginning-of-line)
      (insert foo)
      (next-line 1)))))

;; Stuff for 6.034
(defun setupcl ()
  (interactive)
  (shell-command "attach -n 6.034 lucidcl > /dev/null")
  (load-file "/mit/6.034/emacs/6034-climlisp.el"))

(defvar attach-program "/bin/athena/attach")

(defun attach (locker &optional noauth)
  "Attach LOCKER (or lockers - may be a list.)
Optional second NOAUTH is true for -n option."
  (interactive "sAttach: ")
  (setq locker 
	(filter (function (lambda (locker) (not (file-exists-p (concat "/mit/" locker)))))
		(if (listp locker) locker (list locker))))
  (if locker 
      (progn
	(message "Attaching %s..." locker)
	(apply 'call-process (append (list attach-program nil nil nil)
				     (if noauth (list "-n") nil)
				     locker))
	(message "Attaching %s...done" locker))))
    
      
;; Some emacs 19 stuff
(if (fboundp 'menu-bar-mode)
    (menu-bar-mode -1))
(if (fboundp 'scroll-bar-mode)
    (scroll-bar-mode -1))
