(load "/usr/adm/std.emacs.site" t t)
(global-font-lock-mode t)
(global-set-key "\er" 'replace-regexp) 
(global-set-key "\eq" 'query-replace-regexp) 
(global-set-key "\e=" 'what-line) 
(global-set-key "\e+" 'goto-line)
(global-set-key "\e-" 'shrink-window) 
(global-set-key "\eOy" 'scroll-up) 
(global-set-key "\eOw" 'scroll-down) 
(global-set-key "\eOt" 'beginning-of-line) 
(global-set-key "\eOv" 'end-of-line)
(global-set-key "\eOx" 'top-of-window) 
(global-set-key "\eOu" 'move-to-window-line) 
(global-set-key "\eOr" 'bottom-of-window)
(global-set-key "\eOn" 'set-mark)

(global-set-key "\C-x\C-z" 'save-buffer)
(global-set-key "\es" 'isearch-forward)

(setq letter-mode-hook 'letter-cookies)
(setq talk-mode-hook 'talk-cookies)
(setq note-mode-hook 'note-cookies)
(setq lect-mode-hook 'lect-cookies)
(setq text-mode-hook 'text-cookies)
(setq bib-mode-hook 'bib-cookies)
(setq auto-mode-alist (cons '("\\.notes$" . notes-mode ) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.tex$" . mylatex-mode ) auto-mode-alist))
(setq auto-mode-alist (cons '("note.tex$" . note-mode ) auto-mode-alist))
(setq auto-mode-alist (cons '("let.tex$" . letter-mode ) auto-mode-alist))
(setq auto-mode-alist (cons '("exp.tex$" . expenses-mode ) auto-mode-alist))
(setq auto-mode-alist (cons '("talk.tex$" . talk-mode ) auto-mode-alist))
(setq auto-mode-alist (cons '("lect.tex$" . lect-mode ) auto-mode-alist))
(setq-default save-place t)

(defun mylatex-mode ()
  "no documentation"
  (interactive)
  (if (file-exists-p buffer-file-name)
    ()
    (silly)
    (forward-line -1)
    )
  (forward-line 30)
  (if   (search-backward "newcommand{\\editdate}{" nil t)
      (update-editdate)
    )
  (forward-line 30)
  (if   (search-backward "newcommand{\\useredit}{" nil t)
      (update-useredit)
    )
  (setq write-file-hooks (cons 'tex-save write-file-hooks))
  (text-cookies)
  (latex-mode)
  (end-of-buffer)
  (forward-line -4)
)


(defun silly ()
     (insert-file "~/.formlatex")
     (beginning-of-buffer)
     (replace-string "Running Header" (buffer-name))
     (end-of-buffer)
)

(defun latex-cookies ()
  "no documentation"
  (interactive)
  (if (file-exists-p buffer-file-name)
    () 
    ( silly  )
    (forward-line -1)
  )
  (local-set-key "\e{" 'begin-end)
  (setq write-file-hooks (cons 'tex-save write-file-hooks))
)
(defun update-editdate ()
      (forward-char 22)
      (insert (current-time-string) "}")
      (kill-line)
      (backward-char 9)
      (delete-char 3)
      (backward-char 16)
      (delete-char 4)
      (message "Updated editdate")
      (not-modified)
)
(defun update-lastedit ()
       (forward-line 1)
       (insert "%-----------")
       (insert (current-time-string))
       (insert "\n")
       (backward-line 2)
      (message "Updated editdate")
       (not-modified)
)

(defun update-useredit ()
      (forward-char 22)
      (insert (user-login-name) "}")
      (kill-line)
      (message "Updated useredit")
      (not-modified)
)

(defun tex-save ()
  (interactive)
  (save-excursion 
   (forward-line 30)
   (if   (search-backward "newcommand{\\editdate}{" nil t)
      (update-editdate)
    )
  (forward-line 30)
   (if   (search-backward "newcommand{\\useredit}{" nil t)
      (update-useredit)
    )
  )

)

(defun text-cookies ()
  "additions to text and TeX modes"
  (interactive)
  (turn-on-auto-fill)
  (quietly-read-abbrev-file "~/.abbrevs")
  (abbrev-mode 1)
  (local-set-key "\eOP" 'spell-word)
  (local-set-key "\eOQ" 'spell-buffer)
  (local-set-key "\eOR" 'command-apropos)
  (local-set-key "\eOS" 'describe-key)
  (local-set-key "\e-{" 'begin-end)
  (local-set-key "\e{" 'begin-end)
)


(defun letter-cookies ()
  (interactive)
  (if (file-exists-p buffer-file-name)
    (insert-file "~/.formletter")
    () 
  )
)


(defun letter-mode ()
  (interactive)
  (if (file-exists-p buffer-file-name)
    (insert-file "~/.formletter")
    (forward-line -4)
    )
  (latex-mode)
)

(defun note-mode ()
  (interactive)
  (if (file-exists-p buffer-file-name)
    (insert-file "~/.formnote")
    (forward-line -4)
    )
  (latex-mode)
)

(defun expenses-mode ()
  (interactive)
  (if (file-exists-p buffer-file-name)
    () 
    (insert-file "~/.formexpenses")
    (forward-line -4)
    )
  (latex-mode)
)

(defun lect-cookies ()
  (interactive)
  (insert-file "~/.formlect")
)
(defun lect-mode ()
  (interactive)
  (if (file-exists-p buffer-file-name)
    () 
    (insert-file "~/.formlect")
    (forward-line -4)
    )
  (latex-mode)
)
(defun talk-cookies ()
  (interactive)
  (insert-file "~/.formtalk")
)
(defun talk-mode ()
  (interactive)
  (if (file-exists-p buffer-file-name)
    () 
    (insert-file "~/.formtalk")
    (forward-line -4)
    )
  (latex-mode)
)

(defun bib-cookies ()
  (interactive)
  (local-set-key "\ea" 'add-entry))
(defun add-entry ()
  (insert ".IP \\(bu 8\n"))

 
(defun begin-end ()
  "Create a LaTeX environment, using the word before point."
  (interactive)
  (forward-word -1)
  (kill-word 1)
  (insert "\\begin{")
  (yank 1)
  (insert "}\n\\item\n\n\\end{")
  (yank 1)
  (insert "}")
  (forward-line -1)
)

(defun itemize ()
  "Create a LaTeX environment, using the word before point."
  (interactive)
  (insert "\\begin{itemize}\n\\item\n\n\\end{itemize}")
  (forward-line -2)
)

(defun nextpage ()
  "Used for lecture and talk environments"
  (interactive)
  (insert "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
  (insert "\\nextpage{}\n")
  (insert "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
  (forward-line -2)
)

(defun next-page ()
  "Used for lecture and talk environments"
  (interactive)
  (insert "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
  (insert "\\nextpage{}\n")
  (insert "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
  (forward-line -2)
)

(defun enumerate ()
  "Create a LaTeX environment, begin end enumerate."
  (interactive)
  (insert "\\begin{enumerate}\n\\item\n\n\\end{enumerate}")
  (forward-line -2)
)

( defun center ()
  "Create a LaTeX environment, begin end center."
  (interactive)
  (insert "\\begin{center}\n\\item\n\n\\end{center}")
  (forward-line -2)
)



(defun top-of-window ()
  "Move to top of window"
  (interactive)
  (move-to-window-line 0))
(defun bottom-of-window ()
  "Move to bottom of window"
  (interactive)
  (move-to-window-line -1))
(defun begin-end ()
  "Create a LaTeX environment, using the word before point."
  (interactive)
  (forward-word -1)
  (kill-word 1)
  (insert "\\begin{")
  (yank 1)
  (insert "}\n\\item\n\n\\end{")
  (yank 1)
  (insert "}")
  (forward-line -1)
)
(defun center ()
  "Create a LaTeX environment, using the word before point."
  (interactive)
  (insert "\\begin{center}\n\n\\end{center}")
  (forward-line -2)
)
(defun math ()
  "Put dollar signs around current word"
  (interactive)
  (forward-word -1)
  (insert "$")
  (forward-word 1)
  (insert "$")
)


(defun notes-mode ()
  (interactive)
  (end-of-buffer)
  (insert "\n\n<<< ")
  (insert (current-time-string))
  (insert " >>>\n")
  (text-mode)
  (turn-on-auto-fill)
)

(put 'eval-expression 'disabled nil)



(defun procname ()
  "Create a procedure heading using the word before point as proc name."
  (interactive)
  (beginning-of-line)
  (kill-line)
  (insert "/*********************************")
  (setq xxx (current-column))
  (insert "*/\n")
  (insert "/*                                */\n")
  (insert "/*    ")
  (yank 1)
  (insert "            ")
  (forward-word -1)
  (move-to-column xxx)
  (insert"*/\n")
  (insert "/*                                */\n")
  (insert "/**********************************/\n")
  (insert "void ")
  (yank 1)
  (insert "( )\n{\n\n}")
  (forward-line -3)
  (beginning-of-line)
)


(defun proc1name ()
  "Create a procedure heading using the word before point as proc name."
  (interactive)
  (beginning-of-line)
  (kill-line)
  (yank 1)
  (forward-line 1)
  (yank 1)
  (insert "\n");
  (beginning-of-line)
  (forward-line -2)

  (beginning-of-line)
  (kill-line)
  (insert "/*     ")
  (yank 1)
  (insert "    ")
  (setq xxx (current-column))
  (insert "*/\n")


  (insert "/*     ")
  (insert "                                                            ")
  (beginning-of-line)
  (move-to-column xxx)
  (insert"*/\n")
  (kill-line)
  (beginning-of-line)
  (forward-line -3)
  (end-of-line)
  (insert "\n")

  (insert "/*     ")
  (insert "                                                            ")
  (beginning-of-line)
  (move-to-column xxx)
  (insert"*/\n")
  (kill-line)
  (beginning-of-line)
  (forward-line -1)
  (insert "\n");
  (forward-line -1)

  (beginning-of-line)
  (insert "/******")
  (insert "************************************************************")
  (beginning-of-line)
  (move-to-column xxx)
  (insert"*/")
  (kill-line)
  (beginning-of-line)

  (forward-line 4)
  (insert "\n");
  (forward-line -1)

  (beginning-of-line)
  (insert "/******")
  (insert "************************************************************")
  (beginning-of-line)
  (move-to-column xxx)
  (insert"*/\n")
  (kill-line)
  (beginning-of-line)

  (forward-line 1)
  (beginning-of-line)

)

(defun garbage () 

)





      ;;=============================================================================
      ;;                    scroll on  mouse wheel
      ;;=============================================================================
      
      ;; scroll on wheel of mouses
      (define-key global-map 'button4
        '(lambda (&rest args)
          (interactive) 
          (let ((curwin (selected-window)))
            (select-window (car (mouse-pixel-position)))
            (scroll-down 5)
            (select-window curwin)
      )))
      (define-key global-map [(shift button4)]
        '(lambda (&rest args)
          (interactive) 
          (let ((curwin (selected-window)))
            (select-window (car (mouse-pixel-position)))
            (scroll-down 1)
            (select-window curwin)
      )))
      (define-key global-map [(control button4)]
        '(lambda (&rest args)
          (interactive) 
          (let ((curwin (selected-window)))
            (select-window (car (mouse-pixel-position)))
            (scroll-down)
            (select-window curwin)
      )))
      
      (define-key global-map 'button5
        '(lambda (&rest args)
          (interactive) 
          (let ((curwin (selected-window)))
            (select-window (car (mouse-pixel-position)))
            (scroll-up 5)
            (select-window curwin)
      )))
      (define-key global-map [(shift button5)]
        '(lambda (&rest args)
          (interactive) 
          (let ((curwin (selected-window)))
            (select-window (car (mouse-pixel-position)))
            (scroll-up 1)
            (select-window curwin)
      )))
      (define-key global-map [(control button5)]
        '(lambda (&rest args)
          (interactive) 
          (let ((curwin (selected-window)))
            (select-window (car (mouse-pixel-position)))
            (scroll-up)
            (select-window curwin)
      )))
      

;;=============================================================================
;;                    scroll on numpad
;;=============================================================================
      
      (define-key global-map 'kp_9 'scroll-down)
      (define-key global-map 'kp_3 'scroll-up)
      (define-key global-map 'kp_6
        '(lambda (&rest args) (interactive)  (other-window 1)))
      (define-key global-map 'kp_4
        '(lambda (&rest args) (interactive)  (other-window -1)))
      (define-key global-map 'kp_2
        '(lambda (&rest args) (interactive)  (scroll-up 1)))
      (define-key global-map 'kp_8
        '(lambda (&rest args) (interactive) (scroll-down 1)))
      (define-key global-map 'kp_7 'beginning-of-buffer)
      (define-key global-map 'kp_1 'end-of-buffer)
      (define-key global-map 'kp_5
        '(lambda (&rest args) (interactive)  (recenter 0)))
      
;;; mwheel.el --- Mouse support for MS intelli-mouse type mice

;; Copyright (C) 1998, Free Software Foundation, Inc.
;; Maintainer: William M. Perry <wmperry@cs.indiana.edu>
;; Keywords: mouse

;; This file is part of XEmacs.

;; XEmacs is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; XEmacs is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with XEmacs; see the file COPYING.  If not, write to the 
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Synched up with: Not synched.

;;; Commentary:

;;; Code:

(require 'custom)
(require 'cl)

(defconst mwheel-running-xemacs (string-match "XEmacs" (emacs-version)))

(defcustom mwheel-scroll-amount '(5 . 1)
  "Amount to scroll windows by when spinning the mouse wheel.
This is actually a cons cell, where the first item is the amount to scroll
on a normal wheel event, and the second is the amount to scroll when the
wheel is moved with the shift key depressed.
This should be the number of lines to scroll, or `nil' for near
full screen.
A near full screen is `next-screen-context-lines' less than a full screen."
  :group 'mouse
  :type '(cons
	  (choice :tag "Normal"
		  (const :tag "Full screen" :value nil)
		  (integer :tag "Specific # of lines"))
	  (choice :tag "Shifted"
		  (const :tag "Full screen" :value nil)
		  (integer :tag "Specific # of lines"))))

(defcustom mwheel-follow-mouse nil
  "Whether the mouse wheel should scroll the window that the mouse is over.
This can be slightly disconcerting, but some people may prefer it."
  :group 'mouse
  :type 'boolean)

(if (not (fboundp 'event-button))
    (defun mwheel-event-button (event)
      (let ((x (symbol-name (event-basic-type event))))
	(if (not (string-match "^mouse-\\([0-9]+\\)" x))
	    (error "Not a button event: %S" event))
	(string-to-int (substring x (match-beginning 1) (match-end 1)))))
  (fset 'mwheel-event-button 'event-button))

(if (not (fboundp 'event-window))
    (defun mwheel-event-window (event)
      (posn-window (event-start event)))
  (fset 'mwheel-event-window 'event-window))

(defun mwheel-scroll (event)
  (interactive "e")
  (let ((curwin (if mwheel-follow-mouse
		    (prog1
			(selected-window)
		      (select-window (mwheel-event-window event)))))
	(amt (if (memq 'shift (event-modifiers event))
		 (cdr mwheel-scroll-amount)
	       (car mwheel-scroll-amount))))
    (case (mwheel-event-button event)
      (4 (scroll-down amt))
      (5 (scroll-up amt))
      (otherwise (error "Bad binding in mwheel-scroll")))
    (if curwin (select-window curwin))))

(define-key global-map (if mwheel-running-xemacs 'button4 [mouse-4])
  'mwheel-scroll)

(define-key global-map (if mwheel-running-xemacs [(shift button4)] [S-mouse-4])
  'mwheel-scroll)

(define-key global-map (if mwheel-running-xemacs 'button5 [mouse-5])
  'mwheel-scroll)

(define-key global-map (if mwheel-running-xemacs [(shift button5)] [S-mouse-5])
  'mwheel-scroll)

(provide 'mwheel)
