;;;
;;; My init stuff
;;;
;; Allow use of cursor keys, '['
(setq sun-esc-bracket t)
;; Set C mode indentation rules to what I like
(setq c-indent-level 4)
(setq c-continued-statement-offset 4)
(setq c-argdecl-indent 4)
;; Don't show GNU Emacs ad
(setq inhibit-startup-message t)
;; Don't invoke site default.el
(setq inhibit-default-init t)
;; No header page on prints
;; Delete old backups without asking
(setq trim-versions-without-asking t)
;; Slow down the Auto-Save frequency
(setq auto-save-interval 600)
;; Use buffer-menu instead of buffer-list
(define-key global-map "\C-x\C-b" 'buffer-menu)
;; Force \n at end of buffer on save & ask
(setq require-final-newline 1)
;; Add .F files to fortran-mode
(setq auto-mode-alist (cons (cons "\\.F$" 'fortran-mode) auto-mode-alist))
;; Add .l & .y files to c-mode
(setq auto-mode-alist (cons (cons "\\.l$" 'c-mode) auto-mode-alist))
(setq auto-mode-alist (cons (cons "\\.y$" 'c-mode) auto-mode-alist))
;; Add Lsim .M files to c-mode
(setq auto-mode-alist (cons (cons "\\.M$" 'c-mode) auto-mode-alist))
;; Add .cc & .hxx files to c++-mode
(autoload 'c++-mode "c++-mode" nil t)
(setq auto-mode-alist (cons (cons "\\.cc$" 'c++-mode) auto-mode-alist))
(setq auto-mode-alist (cons (cons "\\.hxx$" 'c++-mode) auto-mode-alist))
;; Add .pl files to perl-mode
(autoload 'perl-mode "perl-mode" nil t)
(setq auto-mode-alist (cons (cons "\\.pl$" 'perl-mode) auto-mode-alist))
;; Use gnumake for compilations
(setq compile-command "gnumake")
;; Wrap windows less than screen width
(setq truncate-partial-width-windows)

;; Add read-only edit to Dired
(defun my-dired-find-file-read-only ()
  "In dired, visit read-only the file or directory named on this line."
  (interactive)
  (find-file-read-only (dired-get-filename)))
;
; Dired extra key defintions
(defun my-dired()
  (define-key dired-mode-map "R" 'my-dired-find-file-read-only)
  )
(setq dired-mode-hook 'my-dired)

;;
;; General purpose scrolling functions for key mapping
;;
(defun my-mv2top() (interactive) (recenter 0))
(defun my-mv2bot() (interactive) (recenter -1))

;;
;; Keymap setup for Suntools
;;
(if (and (equal window-system nil) (not (null (getenv "WINDOW_PARENT"))))
    (setq suntool-map-hooks
	  (list
					; Left keys
	   '(define-key suntool-map "cl" 'buffer-menu)      ; l3
	   '(define-key suntool-map "cL"
	      'sun-select-current-line)                     ; L3
					; Right keys
	   '(define-key suntool-map "ar" 'compile)          ; r1
	   '(define-key suntool-map "aR" 'add-sccsid)       ; R1
	   '(define-key suntool-map "br" 'grep)             ; r2
					;	 '(define-key suntool-map "bR" 'bison-widen)      ; R2
	   '(define-key suntool-map "cr" 'other-window)     ; r3
					;	 '(define-key suntool-map "cR" 'bison-block-mode) ; R3
	   '(define-key suntool-map "dr" 'my-mv2top)        ; r4
	   '(define-key suntool-map "er" 'my-mv2bot)        ; r5
	   '(define-key suntool-map "eR"
	      'cad-change-to-prev-directory)                ; R5
	   '(define-key suntool-map "fr"
	      'add-change-log-entry)                        ; r6
	   '(define-key suntool-map "fR" 'build-c-func)     ; R6
					; Top keys
	   '(define-key suntool-map "dT" 'rcs-log-buffer)   ; F4
	   '(define-key suntool-map "eT" 'rcs-ci-buffer)    ; F5
	   '(define-key suntool-map "fT" 'rcs-co-buffer)    ; F6
	   '(define-key suntool-map "gt" 'find-this-error)  ; f7
	   '(define-key suntool-map "gT" 'add-rcsid)        ; F7
	   )
	  )
  )

;;
;; Keymap setup for my Intext terminal
;;
(if (string-equal (getenv "TERM") "intext")
    (progn
      ; Stop loading of any .../term terminal setup file
      (setq term-file-prefix nil)
      (defvar it-map (make-sparse-keymap)
	"*Keymap for my Intext terminal.")
      (define-key global-map "\C-_" 'backward-char)  ; Left cursor
      (define-key global-map "\C-\\" 'previous-line) ; Up cursor
      (define-key global-map "\C-^" 'forward-char)   ; Right cursor
      (define-key global-map "\C-j" 'next-line)      ; Down cursor
      (global-unset-key "\C-v")			 ; Release Cntrl-V for it-map
      (define-key global-map "\C-v" it-map)	 ; Make Cntrl-V prefix key
      (define-key it-map "X\C-m" 'scroll-up)     ; Clear Tabs
      (define-key it-map "V\C-m" 'scroll-down)   ; Print Msg
      (define-key it-map "T\C-m" 'beginning-of-buffer) ; Send Msg
      (define-key it-map "W\C-m" 'end-of-buffer) ; Set Tab
      (define-key it-map "U\C-m" 'recenter)      ; Print Line
      (define-key it-map "Q\C-m" 'my-mv2top)     ; Send Page
      (define-key it-map "R\C-m" 'my-mv2bot)     ; Send Line
      (define-key it-map "u\C-m" 'newline-and-indent) ; Line Feed
      (define-key it-map "Y\C-m" 'backward-delete-char-untabify) ; Mult Code
      ; Swap Del and BS codes
      (let ((ktt (make-string 128 0))
	    (i 0))
	(while (< i 128)
	  (aset ktt i i)
	  (setq i (1+ i)))
	(aset ktt ?\010 ?\177)
	(aset ktt ?\177 ?\010)
	(setq keyboard-translate-table ktt)
	)
      ; Fixup newline for c++
      (defun it-c++ ()
	(local-unset-key "\C-j")
	)
      (setq c++-mode-hook 'it-c++)
      )
  )

;;
;; Keymap setup for my Amiga terminal running JRComm as vt100
;;
(if (and (string-equal (getenv "HOMETERM") "amiga")
	 (string-equal (getenv "TERM") "vt100")
	 )
    (progn
      ; Stop loading of term/vt100.el
      (setq term-file-prefix nil)
;      (defvar JR-map (make-sparse-keymap)
;	"*Keymap for my Amiga terminal.")
; "\eOl" Numpad + or F5
; "\eOm" Numpad -
; "\eOp" Numpad 0
; "\eOn" Numpad .
; "\eOM" Numpad Enter
; No Meta key
      (define-key global-map "\eOD" 'backward-char)  ; Left cursor
      (define-key global-map "\eOt" 'backward-char)  ; Numpad 4
      (define-key global-map "\eOA" 'previous-line)  ; Up cursor
      (define-key global-map "\eOx" 'previous-line)  ; Numpad 8
      (define-key global-map "\eOC" 'forward-char)   ; Right cursor
      (define-key global-map "\eOv" 'forward-char)   ; Numpad 6
      (define-key global-map "\eOB" 'next-line)      ; Down cursor
      (define-key global-map "\eOr" 'next-line)      ; Numpad 2
      (define-key global-map "\eOy" 'scroll-down)    ; Numpad 9
      (define-key global-map "\eOs" 'scroll-up)      ; Numpad 3
      (define-key global-map "\eOw" 'beginning-of-buffer) ; Numpad 7
      (define-key global-map "\eOq" 'end-of-buffer)  ; Numpad 1
      (define-key global-map "\eOu" 'recenter)       ; Nump'compile)		; Numpad / or F3
      (define-key global-map "\eOS" 'grep)		; Numpad * or F4
      ; Swap Del and BS codes
      (let ((ktt (make-string 128 0))
	    (i 0))
	(while (< i 128)
	  (aset ktt i i)
	  (setq i (1+ i)))
	(aset ktt ?\010 ?\177)
	(aset ktt ?\177 ?\010)
	(setq keyboard-translate-table ktt)
	)
      )
  )

;;
;;  Swap Delete & Backspace on specific nodes
;;
(if (or (equal (getenv "MYMACH") "hp-pa")
	(equal t t)
;	(equal (getenv "NODE") "calypso")
;	(equal (getenv "NODE") "tirpitz")
	)
    ; Swap Del and BS codes
    (let ((ktt (make-string 128 0))
	  (i 0))
      (while (< i 128)
	(aset ktt i i)
	(setq i (1+ i)))
      (aset ktt ?\010 ?\177)
      (aset ktt ?\177 ?\010)
      (setq keyboard-translate-table ktt)
      )
  )

;;
;;  Keymap setup for X Windows
;;
(if (or (equal window-system 'x11) (equal window-system 'x))
    (progn
      (global-unset-key "\M-[")		; Allow function keys on Sun,
					;  but nukes the "\M-[" command.

					; Shifted function keys not recognized
					;  at this time.
					; Left keys
      (define-key global-map "\M-[194z" 'buffer-menu)       	; l3
      (define-key global-map "\M-[195z" 'undo)                  ; l4
      (define-key global-map "\M-[197z" 'copy-region-as-kill)	; l6
      (define-key global-map "\M-[199z" 'yank) 			; l8
      (define-key global-map "\M-[201z" 'kill-region) 		; l10
					; Right keys
      (define-key global-map "\M-[208z" 'compile)              	; r1
      (define-key global-map "\M-[209z" 'grep)                	; r2
      (define-key global-map "\M-[210z" 'other-window)      	; r3
      (define-key global-map "\M-[211z" 'my-mv2top)        	; r4
      (define-key global-map "\M-[212z" 'my-mv2bot) 	        ; r5
      (define-key global-map "\M-[213z" 'add-change-log-entry)	; r6
      (define-key global-map "\M-[214z" 'beginning-of-buffer) 	; r7
      (define-key global-map "\M-[216z" 'scroll-down) 		; r9
      (define-key global-map "\M-[218z" 'recenter) 		; r11
      (define-key global-map "\M-[220z" 'end-of-buffer) 	; r13
      (define-key global-map "\M-[222z" 'scroll-up) 		; r15
					; Top keys
      (define-key global-map "\M-[224z" 'goto-line) 		; f1
      (define-key global-map "\M-[213z" 'find-this-error)	; f7
      )
  )

;;
;; Window breakup
;;
(if (> (screen-width) 159)
    (progn
      (split-window-horizontally 82)
      (other-window 1)
      (split-window-vertically)
      (other-window -1)
      )
  )



;;;
;;; Following added by GNU Emacs
;;;
;; Allow M-ESC to do eval
(put 'eval-expression 'disabled nil)




