;;
;; default.el (default .emacs file)
;; Linux and Suns
;;
;; Sets Fonts for the Font-Lock mode
;; 
;; key bindings ctrl-Arrows (move by words)
;;              shift-Arrows (move and set mark at the same time)
;;
;; Loads some extension that should reside in the site-lisp 
;; directory or another directory in the load-path
;; The extensions are loaded inside a condition-case error-handler
;; so execution won't stop in case of missing files
;;
;; Created by Stefan Strobel 
;; strobel@fh-heilbronn.de
;; Please send bug reports and comments to the above adress
;; 

;;;
;;; Check for special command-line args
;;; (actually we just check for -ne which inhibits loading
;;;  of some extensions and speeds up the startup)
;;;  

(defvar load-exts 
  (if (member "-ne" command-line-args)
      (progn
	(setq command-line-args (delete "-ne" command-line-args))
	nil)
    t)
  "should we load our extensions")
    

;;;
;;; Settings
;;;

;; if files are in a different directory and don't get loaded 
;; extend the load-path here !
;; example : 

(setq load-path (cons "/usr/local/lib/emacs/site-lisp/auctex" load-path))
(setq load-path (cons "/usr/local/lib/emacs/site-lisp/swi" load-path))
(setq load-path (cons "/usr/local/lib/emacs/site-lisp/ilisp" load-path))

;; On a Linux System you will need the following 
;; (and you can delete the above path settings)

(setq load-path (cons "/usr/lib/emacs/site-lisp/auctex" load-path))
(setq load-path (cons "/usr/lib/emacs/site-lisp/swi" load-path))
;(setq load-path (cons "/usr/lib/emacs/site-lisp/ilisp" load-path))


(setq default-major-mode 'text-mode)          ; Default mode for unknown files
(setq default-fill-column 78)                 ; Text width
(setq transient-mark-mode t)                  ; Make the region visible
(put 'eval-expression 'disabled nil)          ; allow eval with Meta-esc

;;(setq special-display-buffer-names '("*compilation*" "*grep*" "*TeX Shell*"))
;; there are still bugs in emacs 19.23 with this !

(if load-exts
    (progn
      (standard-display-european t)           ; enable display of accents.
      (condition-case nil
	  (quietly-read-abbrev-file)          ; read our abreviation file
	(error nil))))

;;
;; Mode for files that begin with #!
;; scotty is an exception because is uses -f
(setq interpreter-mode-alist (append '(("scotty -f" .tcl-mode)
				       ("scotty -nf" .tcl-mode)
				       ("atclsh" . tcl-mode))
				     interpreter-mode-alist))
				     

;;;
;;; Set the default faces for the font-lock-mode
;;;

(setq font-lock-function-name-face 'bold)
(setq font-lock-comment-face       'italic)
(setq font-lock-string-face        'default)
(setq font-lock-doc-string-face    'default)

;;;
;;; Set special face attributes 
;;; (inside an error handler in case something is not available)
;;;

(if window-system
    (condition-case nil 
	(progn
	  (set-face-foreground  'region "black")
	  (set-face-background  'region "grey90")
	  (if (not (x-display-color-p))
	      (set-face-underline-p 'region t))
	  
	  (set-face-foreground  'bold   "black")
	  (set-face-background  'bold   "white")
	  (set-face-underline-p 'bold    nil)
	  
	  (if (x-display-color-p)
	      (progn
		(copy-face 'default   'comment)
		(set-face-foreground  'comment "grey60")
		(set-face-background  'comment "white")
		(setq font-lock-comment-face 'comment)
		
		(copy-face 'default   'string)
		(set-face-foreground  'string "dark slate grey")
		(set-face-background  'string "white")
		(setq font-lock-string-face 'string)
		
		(copy-face 'default   'docstr)
		(set-face-foreground  'docstr "blue")
		(set-face-background  'docstr "white")
		(setq font-lock-doc-string-face 'docstr))))
      
      (error nil)))


;;;
;;; Set the fonts in Emacs
;;; (also in an error handler in case some fonts dont exist)
;;;

(if window-system
    (condition-case nil 
	(progn
	  (set-face-font 'bold 
			 "-adobe-courier-bold-r-normal--*-*-75-75-m-*-*-1")
	  (set-face-font 'italic 
			 "-adobe-courier-medium-o-normal--*-*-75-75-m-*-*-1")
	  (set-face-font 'comment 
			 "-adobe-courier-medium-o-normal--*-*-75-75-m-*-*-1")
	  (set-face-font 'bold-italic 
			 "-adobe-courier-bold-o-normal--*-*-75-75-m-*-*-1"))
      (error nil)))




;;;
;;; Load some additions / extensions
;;;



(if load-exts
    (progn

      (condition-case nil         ; autoinsert - insert a file in new files
	  (progn                  ; depending on its extension (for templates)
	    (load "autoinsert")
	    ;; default path for insert files is ~/insert
	    (setq auto-insert-alist
		  (nconc '(("\\.el$" . "elisp-insert.el"))
			 auto-insert-alist)))
	(error nil))

      
      (if window-system
	  (condition-case nil         ; functions for shift-arrows (allow to 
	      (load "add-functions")  ; set mark and move at the same time)
	    (error nil)))
      

      ;;
      ;;Try to load func-menu
      ;; 
      (condition-case nil     
	  (progn 	
	    (load "func-menu")    ; load function menu
	    (define-key global-map [S-down-mouse-3] 
	      'function-menu))	
	(error nil))	       


      (condition-case nil         ; functions and configuration for ilisp 
	  (load "add-ilispconf")  ; with the cmu-lisp system
        (error nil))


      (if window-system
	  (condition-case nil         ; Mode line dragging function
	      (progn 
		(load "mldrag")
		(global-set-key [mode-line down-mouse-1] 
				'mldrag-drag-mode-line)
		(global-set-key [vertical-line down-mouse-1] 
				'mldrag-drag-vertical-line)
		(global-set-key [vertical-scroll-bar S-down-mouse-1]
				'mldrag-drag-vertical-line))
	    (error nil)))

      ;;
      ;; set up a new menu in the elisp mode
      ;;
      (define-key emacs-lisp-mode-map [menu-bar elisp]
	(cons "Elisp" (make-sparse-keymap "elisp")))
      (define-key emacs-lisp-mode-map [menu-bar elisp debonenoff]
	'("Cancel Debug on Entry" . cancel-debug-on-entry))
      (define-key emacs-lisp-mode-map [menu-bar elisp debonen]
	'("Debug on Entry" . debug-on-entry))
      (define-key emacs-lisp-mode-map [menu-bar elisp debdefun]
	'("Debug Defun" . edebug-defun))
      (define-key emacs-lisp-mode-map [menu-bar elisp evalbuff]
	'("Eval Buffer" . eval-buffer))
      (define-key emacs-lisp-mode-map [menu-bar elisp evalreg]
	'("Eval Region" . eval-region))
      (define-key emacs-lisp-mode-map [menu-bar elisp evaldef]
	'("Eval Defun" . eval-defun))
      ))
      


;;;
;;; Set up hooks for other modes
;;;

(add-hook 'text-mode-hook 
	  '(lambda ()
	     (turn-on-auto-fill)))           ; Auto fill in Text mode
	 

(add-hook 'emacs-lisp-mode-hook 
	  '(lambda () 
	     (line-number-mode 1)            ; line-numbers
	     (if window-system 
		 (font-lock-mode 1))))


(add-hook 'c++-mode-hook
	  '(lambda () 
	     (line-number-mode 1)
	     (if window-system 
		 (font-lock-mode 1))))


(add-hook 'c-mode-hook
	  '(lambda () 
	     (line-number-mode 1)
	     (if window-system 
		 (font-lock-mode t))))


(add-hook 'lisp-mode-hook
	  '(lambda () 
	     (line-number-mode 1)))
 
(add-hook 'gud-mode-hook 
	  '(lambda ()
	     (load-inspect-mode)))

(add-hook 'tcl-mode-hook
	  '(lambda ()
	     (line-number-mode 1)
	     (if window-system
		 (font-lock-mode t))))


(add-hook 'protalk-mode-hook 
	  '(lambda ()
	     (line-number-mode 1)
	     (condition-case nil
		 (load "pkhelp")
	       (error nil))))



;;
;; Modes to autoload
;; -----------------
;;


;;
;; Inspector -- Inspect variables in another window while debugging with gdb
;;

(autoload 'load-inspect-mode "inspect" 
  "Inspector addition" t)


;;
;; Folding Mode - turn emacs into a folding editor
;;

(autoload 'folding-mode "folding" 
  "Minor mode that simulates a folding editor" t)
(condition-case nil    
    (progn             
      (folding-mode t)
      (folding-mode-add-find-file-hook))
      (error nil))


;;
;; HTML mode
;;

(autoload 'html-mode "html-mode" "HTML major mode." t)
(or (assoc "\\.html$" auto-mode-alist)
    (setq auto-mode-alist (cons '("\\.html$" . html-mode) 
				auto-mode-alist)))



;;
;; Postscript Mode
;; --------------
;;

(autoload 'postscript-mode "postscript.el" "" t)
(setq auto-mode-alist
      (cons '("\\.c?ps$".postscript-mode) auto-mode-alist))


;;
;; Smalltalk-mode
;; --------------
;;
(setq auto-mode-alist
      (append  '(("\\.st$" . smalltalk-mode))
	       auto-mode-alist))

(autoload 'smalltalk-mode "st.el" "" t)


;;
;; lispdir -- Search for/retrieve additional packages in Emacs-Lisp-Archive
;; -------
;;

(autoload 'format-lisp-code-directory "lispdir" nil t)
(autoload 'lisp-dir-apropos "lispdir" nil t)
(autoload 'lisp-dir-retrieve "lispdir" nil t)
(autoload 'lisp-dir-verify "lispdir" nil t)


;;
;; auc-tex
;; -------
;;

(autoload 'tex-mode "auc-tex" "Automatic select TeX or LaTeX mode" t)
(autoload 'plain-tex-mode "auc-tex" "Mode for Plain TeX" t)
(autoload 'latex-mode "auc-tex" "Mode for LaTeX" t)
(autoload 'LaTeX-math-mode    "tex-math"      "Math mode for TeX." t)
(autoload 'outline-minor-mode "outline-m" "Minor Outline Mode." t)



;;
;; swi-prolog 
;; ----------
;;

(autoload 'run-prolog "swi-prolog" "Run an inferior prolog process" t)
(autoload 'prolog-mode "swi-prolog" "SWI-Prolog mode" t)


;;
;; use clisp as inferior lisp
;; --------------------------
;;
(setq inferior-lisp-program "clisp")

;;
;; an inferior lisp process can be started with M-x run-lisp
;; 
;; The script clisp (which is normaly located in /usr/bin 
;; on linux systems should contain the following :
;;
;;#!/bin/sh 
;;/usr/lib/lisp/lisp.run -M /usr/lib/lisp/lispinit.mem
;;


;;;
;;; WinMGR-mode (Fvwm config Files)
;;; -------------------------------
;;;
;;
(setq auto-mode-alist
      (append '(("\\.[A-Za-z]*wm$" . winmgr-mode)
		("\\.[A-Za-z]*wmrc" . winmgr-mode))
	      auto-mode-alist))	

(autoload 'winmgr-mode "winmgr-mode"
  "Mode for editing window manager config files")

(add-hook 'winmgr-mode-hook
	  '(lambda ()
	     (font-lock-mode t)
	     (setq font-lock-keywords winmgr-font-lock-keywords)
	     (font-lock-fontify-buffer)))


;;;
;;; Rect-Mark
;;; ---------
;;;

(autoload 'rm-set-mark "rect-mark"
  "Set mark for rectangle." t)
(autoload 'rm-exchange-point-and-mark "rect-mark"
  "Exchange point and mark for rectangle." t)

(define-key ctl-x-map "r\C-@"     'rm-set-mark)
(define-key ctl-x-map [?r ?\C-\ ] 'rm-set-mark)
(define-key ctl-x-map "r\C-x"     'rm-exchange-point-and-mark)


;;;
;;; Keys
;;;

;;
;; We only need to define the function Keys 
;; That are not defined in Termcap !
;; The arrows (at least for Sun and Linux) 
;; Work well without any setting
;;

;; Correct some Function Keys for Sun Console
(define-key function-key-map  "\e[214z" [home])
(define-key function-key-map  "\e[216z" [prior])
(define-key function-key-map  "\e[218z" [recenter])
(define-key function-key-map  "\e[220z" [end])

;; Correct some Function Keys for Sun under X11
(global-set-key [f31]      'blink-matching-open) 
(global-set-key [f27]      'beginning-of-line)              ; Home
(global-set-key [f33]      'end-of-line)                    ; End
(global-set-key [f29]      'scroll-down)                    ; PgUp
(global-set-key [f35]      'scroll-up)                      ; PgDn
(global-set-key [C-f29]    'beginning-of-buffer)            ; C-PgUp
(global-set-key [C-f35]    'end-of-buffer)                  ; C-PgDn

;; Function Keys under Linux (console)
(define-key function-key-map   "\e[1~"   [home])
(define-key function-key-map   "\e[4~"   [end])
(define-key function-key-map   "\e[2~"   [insert])



;; redefine home and end
(global-set-key [home]     'beginning-of-line)              ; Home
(global-set-key [end]      'end-of-line)                    ; End

;; Control-Arrows
(global-set-key [C-right]  'forward-word)
(global-set-key [C-left]   'backward-word)
(global-set-key [C-prior]  'beginning-of-buffer)
(global-set-key [C-next]   'end-of-buffer)

;; special function keys
(global-set-key [cancel]   'keyboard-quit)
(global-set-key [find]     'search-forward)

(global-set-key [f5]       'goto-line)
(global-set-key [S-f5]     'what-line)                           
(global-set-key [f6]       'tags-search)
(global-set-key [S-f6]     'visit-tags-table)
                           
(global-set-key [f9]       'compile)                           
(global-set-key [M-f8]     'next-error)
(global-set-key [f10]      'next-error)                           
(global-set-key [f12]      'advertised-undo)

;;;
;;; remap Backspace and Delete
;;;
         
(define-key function-key-map [delete]    [deletechar])
(define-key function-key-map [backspace] [DEL])
(global-set-key [DEL] 'delete-backward-char)


;;;
;;; divers
;;;

(define-key ctl-x-map "p" 'toggle-save-place)
(define-key lisp-interaction-mode-map [M-return] 'eval-print-last-sexp)

