;;; Mostly snarfed from Robert Krawitz (rlk@athena.mit.edu)

;;; 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))

;;; This deletes the Help window, or, with a prefix argument, kills
;;; the Help buffer, leaving you with two windows.

(defun kill-help-buffer (&optional arg)
  "Delete the windows on *Help*.  With prefix arg, does not delete window,
but kills the *Help* buffer."
  (interactive "p")
  (let ((help-buffer (get-buffer "*Help*")))
    (if help-buffer
	(if (and (numberp arg) (> arg 1))
	    (kill-buffer help-buffer))
      (delete-windows-on help-buffer)
      (message "No *Help* buffer to kill.")
      (ding))))

;;; 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))
             '(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.mit.edu)

	(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.mit.edu) hack for a much
;;; faster .emacs.elc.  Basically, M-x byte-compile-file
;;; only tokenizes 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)	;;; otherwise, (startup) can't be called interactively.

;;; 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 default-mode-line-format    ;to distinguish from CCA lusemacs
      (concat "%*%*%*GNU " emacs-version " %b  %M %[(%m)%] %3p %-"))

(setq mode-line-format default-mode-line-format)

;;; 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)

;;; 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.mit.edu)

         (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
      '(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
      '(lambda nil
               (if to
                   (forward-line -1)
                 (forward-line 2))
               (insert
                "Reply-To:      jru@athena.mit.edu\n"
                "Full-Name:     Joe Random User\n"
		"Address:       W20-500, 84 Mass Ave.\n"
		"               Cambridge, MA  02139\n"
                "Yow!:          " (yow) "\n")
;;; A classic example of a silly header.The function (yow)
;;; picks a random Zippy-the-Pinhead quotation.  This code
;;; inserts it into the header.  Courtesy of Simson
;;; Garfinkel (simsong@athena.mit.edu).
               (if to
                   (goto-char (point-max))   ; If a reply, To: field is
					     ; already initialized, so
					     ; move below headers.
                 (goto-char (point-min))     ; Else go to the To: line
                 (re-search-forward "^To: "))))

;;; 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" 'kill-help-buffer)

;;; ^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 nuking a
;;; default binding.

;;; closing the huge defun, remember?

)

;;; and calling it.  Wheee!

(startup)
