;;; This is -*- Emacs-Lisp -*-
;;; Mostly snarfed from Robert Krawitz (rlk@athena)

;;; The load path is a variable that controls where Lisp files
;;; will be loaded in the same fashion that the PATH variable
;;; controls where the shell will search.

;;; This is a special function to take any number of arguments
;;; and do nothing whatsoever.  It is useful on occasion.

(defun nop (&rest ignore) (interactive))

(defun my-kill-help-window ()
  "Kill off the stupid help window."
  (interactive)
  (delete-windows-on "*Help*"))

;;; This is an example of a minor mode.  Note the interactive
;;; specification.  The auto-fill-hook is called whenever a
;;; whitespace character is typed when a line spills over the
;;; fill column.  This is similar to normal auto-fill-mode
;;; except that it additionally justifies paragraphs.

(defun auto-justify-mode (arg)
  "Toggle auto-justify mode or turn auto-fill mode on if arg is positive.
In auto-fill mode, inserting a space at a column beyond  fill-column
automatically breaks the line at a previous space. Also automatically
justifies paragraphs."
  (interactive "P")
  (setq auto-fill-hook
        (and (if (null arg)
                 (not auto-fill-hook)
               (> (prefix-numeric-value arg) 0))
             (function (lambda ()
                (fill-paragraph t)
                (insert " ")))))
  (set-minor-mode 'auto-justify-mode "Justify"
                  (not (null auto-fill-hook))))

;;; A simple interface to an important variable.  This function
;;; provides a way for the user to do something simple (set tab
;;; stops every n columns) on a variable that is complicated to
;;; set.  Useful and simple.  This can also be called from a
;;; program.

(defun set-tabs (n)
  "Sets tabs every N columns."
  (interactive "p")
  (let ((count n) (tabs nil))
    (while (<= count 120)
      (setq tabs (cons count tabs))
      (setq count (+ count n)))
    (setq tab-stop-list (reverse tabs))))

;;; C-mode stuff courtesy of Bill Sommerfeld (wesommer@athena)

(defun c-tab ()
  "A REAL tab key for C mode."
  (interactive)
  (if (save-excursion
	(skip-chars-backward " \t")
	(bolp))
      (c-indent-line)
    (insert-tab)))

(defun c-alternate-tab ()
  "Another way of looking at it."
  (interactive)
  (if (eolp)
      (insert-tab)
    (c-indent-line)))

;;; Ken Raeburn's (spook@athena) hack for a much faster .emacs.elc
;;; Basically, M-x byte-compile-file only tokenized defuns.  The
;;; solution?  Put everything that isn't already a defun (nested
;;; defuns lose) into one large function, and then call the
;;; function.

(defun startup ()

  (interactive)

;;; This variable controls the appearance of the mode line.  It
;;; is a reasonably complicated example of formatting
;;; specifications.  Do C-h v on mode-line-format to find out
;;; more information.

  (setq my-system-name (system-name))

  (setq-default mode-line-format 
	      '("-%1*%1*-" my-system-name ": " (buffer-file-name "%f" "%b")
		" " global-mode-string " %[(" mode-name minor-mode-alist
		mode-line-process ")%]--" (-3 . "%p") "-%-"))

;;; Random customization

  (setq inhibit-startup-message t)	; I've already read it

  (setq startup-major-mode 'text-mode)

  (setq default-major-mode 'text-mode)

  (setq display-time-day-and-date t)

  (display-time)

;;;
;;; Functions
;;; courtesy of Jim Fulton
;;; Bound to ESC-h under 'setting keys'
;;; Enabling some commands.  Note that the disabled property is
;;; implemented by use of the property list of the symbol.

  (put 'eval-expression 'disabled nil)	; <Esc> <Esc> or Meta-<Esc>

  (put 'rmail 'disabled nil)

  (put 'narrow-to-region 'disabled nil)


;;; Miscellaneous rmail variables

  (setq mail-self-blind t)

  (setq rmail-delete-after-output t)

  (setq rmail-file-name (expand-file-name "~/Mail/RMAIL"))

;;; File Handling

;;; These all customize the internal operations of the editor.
;;; If you wish to find out what these do, you should read the
;;; documentation on the variables.

  (setq auto-mode-alist
	'(("\\.txt$" . text-mode)
	  ("\\.tex$" . TeX-mode)
	  ("\\.texinfo$" . texinfo-mode)
	  ("\\.c$" . c-mode)
	  ("^/tmp/Re" . non-saved-text-mode)
	  ("\\.scm$" . scheme-mode)
	  ("/\\..*emacs" . emacs-lisp-mode)
	  ("\\.el$" . emacs-lisp-mode)))

  (setq backup-before-writing t)

  (setq backup-by-copying-when-linked t)

  (setq completion-ignored-extensions
	(append completion-ignored-extensions
		(quote
		  (".bak" "~" ".lpt" ".bin" ".aux" ".otl"
		   ".err" ".lib" ".dvi" ".PS"))))

  (setq delete-auto-save-files t)

  (setq require-final-newline t)

;;; (setq make-backup-files nil)
;;; You may wish to do this if you are having quota problems,
;;; but it's not the best of ideas, particularly considering
;;; how hard it is to get backups from Athena (basically
;;; impossible).

;;; Hairy c-mode stuff.  Fixes many bugs of c-mode.
;;; C mode for real C programmers.
;;; Courtesy of Bill Sommerfeld (wesommer@athena)

  (setq c-indent-level 8
	c-argdecl-indent 8
	comment-column 40
	c-label-offset -8
	c-continued-statement-indent 0
	c-continued-statement-offset 8
	c-auto-newline nil
	c-brace-offset 8)

  (define-key c-mode-map "\t" 'c-tab)
;;; This binds the tab key to the newly defun'ed c-tab, but only
;;; when in c-mode.
  (define-key c-mode-map "\e\t" 'c-indent-line)
;;; This binds Meta-tab to c-indent-line.  Again, only in c-mode.

;;; Define some hooks (mode customizing functions). Note that
;;; defun will not work (use lambda) because defun shoves code
;;; into function slot of atom, while setq shoves into value
;;; slot. All modes check the value slot for hooks.
;;; Alternatively, you can use (setq <mode name> (defun <mode
;;; name> ...)) but this is wasteful.

;;; These are typical hooks that you would want to set up to
;;; handle specific modes.

  (setq text-mode-hook
	(function (lambda nil
		    (setq fill-column 72)
		    (auto-fill-mode 1))))

;;; A reasonably complicated example of a hook being used to
;;; customize a major mode. This inserts some random headers,
;;; and places the cursor in the To: field.  It will be called
;;; whenever a piece of mail is being initialized.

  (setq mail-setup-hook
	(function (lambda ()
		    ;; Save excursion means that point is not moved
		    ;; by the commands contained inside the form.
		    ;; When this is executed, point is after "To: " if 
		    ;; there is no destination specified, or after the 
		    ;; --text follows this line-- if there is.
		    (save-excursion 
		      (if to
			  (forward-line -1)
			(forward-line 2))
		      ;; If you don't like the way the registrar spelled 
		      ;; your name, fix it using the `chfn' command,
		      ;; or replace (user-full-name) with your name in quotes
		      (insert
			"Reply-To: " (user-login-name) "@athena.mit.edu\n"
			"Full-Name: " (user-full-name) "\n"
			"Address:       W20-500, 84 Mass Ave.\n"
			"               Cambridge, MA  02139\n")))))

;;; Setting keys

  (define-key ctl-x-map "c" 'byte-compile-file)

;;; byte-compile-file allows you to crunch big .emacs.el
;;; files (like this one) into compiled .emacs.elc files
;;; that load faster.  This binds it to ^Xc

  (define-key esc-map "h" 'my-kill-help-window)

;;; ^H gets you help, and Meta-H kills it.  Sounds mnemonic
;;; to me.  Meta-H is initally bound to mark-paragraph, but I
;;; find myself killing help windows a lot more often than I
;;; mark paragraphs.  Just be aware that you're replacing a
;;; default binding.

;;; closing the huge defun, remember?

  )

;;; and calling it.  Wheee!

(startup)
