;;---------------------------------------------------------------------- load paths
(setq load-path (append (list "/usr/local/share/emacs/site-lisp") load-path ))
(setq load-path (append (list "/usr/share/emacs/site-lisp") load-path ))
(setq load-path (append (list "/usr/local/share/emacs/site-lisp/vm") load-path ))
(setq load-path (append (list "/usr/share/emacs/site-lisp/vm") load-path ))
(setq load-path (append (list "/usr/local/share/emacs/site-lisp/jde") load-path ))
(setq load-path (append (list "/usr/share/emacs/site-lisp/jde") load-path ))

; that's the place where private lisp stuff resides...
(setq load-path (append (list "~/lib/lisp") load-path )) 

;;---------------------------------------------------------------------- all necessary requires

(require 'easymenu)
;(require 'jde)     ;; uncomment this line for Java development with the jde mode

;;---------------------------------------------------------------------- all general stuff

(server-start)

(setq tab-stop-list nil)
(setq default-tab-width 2)
(setq indent-tabs-mode nil)
(setq transient-mark-mode t) ;; region highlighting
(setq visible-bell 1)
;(setq lpr-switches (list "-Pkyo107")) ;; uncomment this and enter your printer here
(setq dabbrev-case-fold-search t) ;; case sensitive completion with M-/
(setq dabbrev-case-replace nil)   
(setq search-upper-case t)

(add-hook 'mail-setup-hook 'mail-abbrevs-setup) ;; mail alias expansion with M-TAB

(put 'eval-expression 'disabled nil)
(put 'narrow-to-region 'disabled nil)
(put 'downcase-region 'disabled nil)
(put 'upcase-region 'disabled nil)

(mapcar
   '(lambda (auto)
      (setq auto-mode-alist
       (cons auto
        (delq (assoc (car auto) auto-mode-alist)
              auto-mode-alist))))
   '(("\\.C$"   . c++-mode)
     ("\\.cc$"  . c++-mode)
     ("\\.java$"  . jde-mode)
     ("\\.hh$"  . c++-mode)
     ("\\.c$"   . c++-mode)
     ("\\.h$"   . c++-mode)
     ("\\.txt$" . text-mode)
     ("\\.sh$"  . sh-mode)
     ("\\.csh$" . sh-mode)
     ("\\.tcsh$" . sh-mode)
     ("\\.cshrc$" . sh-mode)
     ("\\.fvwm2rc$" . sh-mode)
     ("\\.fvwmrc$" . sh-mode)
     ("\\.fvwm2rc95$" . sh-mode)
     ("\\.fvwm95rc$" . sh-mode)
     ("\\.xsession$" . sh-mode)
     ("Makefile" . makefile-mode)
     ("Imakefile" . makefile-mode)
     ("\\.tar$" . tar-mode)
     ("\\.icn$" . icon-mode)
     ("\\.pl$"  . perl-mode)
     ("\\.ps$"  . postscript-mode)
     ("\\.htf$" . htf-mode)
     ("\\.html$" . html-mode)
     ("\\.wml$" . html-mode)
     ("\\.tplwml$" . html-mode)
     ("\\.htm$" . html-mode)
     ("\\.tex$" . latex-mode)
     ("\\.clo$" . latex-mode)
     ("\\.cls$" . latex-mode)
     ("\\.lzh$" . archive-mode)
     ("\\.zip$" . archive-mode)
     ("\\.zoo$" . archive-mode)
     ("\\.arc$" . archive-mode)
     ("\\.LZH$" . archive-mode)
     ("\\.ZIP$" . archive-mode)
     ("\\.ARC$" . archive-mode)
     ("\\.ZOO$" . archive-mode)
     ))

(standard-display-european t)
(line-number-mode t) ;line numbers in every buffer
(column-number-mode t)

;; file version control
(setq version-control t)
(setq kept-new-versions 2)
(setq kept-old-versions 0)
(setq scroll-step 1)

;; uncomment the following line and set your manpath
;(setq manual-program "man -M /usr/local/man:/usr/man:/usr/man/preformat:/usr/X11/man:/usr/openwin/man")

(setq truncate-lines nil)  ; do not truncate lines in full width windows
(setq truncate-partial-width-windows nil)  ; do not truncate lines in vertically split windows

;;---------------------------------------------------------------------- dmacro mode
(require 'dmacro)
(dmacro-load "~/lib/lisp/dmacros.dm")
(global-set-key "\C-ct" (dmacro-command "dstamp" "dtstamp" 'insert-timestamp))
(setq auto-dmacro-alist (append '(("\\.h$" . masthead_h)
                                  ("\\.C$" . masthead_cc)
                                  ("\\.java$" . masthead_java)
                                  ("\\.html$" . masthead_html)
                                  ("\\.htm$" . masthead_html)
                                  ) auto-dmacro-alist))

(defun my-insert-date()
  "Inserts the current time and date into the buffer."
  (interactive)
  (let ((mydate (current-time-string)))
    (progn
      (insert mydate))))
 
(defun my-insert-initials-date()
  "Inserts the author's initials, time and date into the buffer."
  (interactive)
  (insert "/* Author: ") ;; replace by your initials
  (my-insert-date)
  (insert " */"))

;;---------------------------------------------------------------------- global faces

(if (eq window-system 'x)
    (progn
      (set-face-background 'region "blue")
      (set-face-foreground 'region "white")
      
      (set-face-background 'secondary-selection "seagreen3")
      (set-face-foreground 'secondary-selection "white")
      
      (set-face-background 'highlight "cyan2")
      (set-face-foreground 'highlight "black")
      ))

;;---------------------------------------------------------------------- font-lock mode
(if (eq window-system 'x)
    (progn
      (require 'font-lock)
      (setq font-lock-support-mode 'fast-lock-mode)
      (setq font-lock-global-mode 't)
      (setq font-lock-maximum-decoration 999)
      ;; special font lock keywords for modes that are normally not supporting font-lock
      ;; (REGEXP LEVEL FACE OVERRIDE_EXISTING_FACE)
      (setq ps-font-lock-keywords
            '(("%.*$" 0 font-lock-comment-face t)
              ("^%%.*" 0 ps-structuring-face t)
              ("(\\(\\([^)]\\)\\|\\([\\])\\)\\)*)" 0 font-lock-string-face)
              ("/[-a-zA-Z0-9_$]+" 0 font-lock-variable-name-face)
              ("%std\\(err\\|in\\|out\\)" 0 ps-special-fd-face)
              ("%\\(lineedit\\|statementedit\\)" 0 ps-special-command-face)
              ("countexecstack\\|ex\\(ec\\(\\|stack\\)\\|it\\)\\|for\\|if\\(\\|else\\)\\|loop\\|quit\\|repeat\\|st\\(art\\|op\\(\\|ped\\)\\)" 0 font-lock-keyword-face)
              ))
      (font-lock-add-keywords 'postscript-mode ps-font-lock-keywords 'set)
      ;; compilation mode keywords
      (setq compilation-mode-keywords
            '((":[0-9]+: [Ww]arning.*" 0 comp-lock-warning-face)
              (":[0-9]+: [Nn]ote.*" 0 comp-lock-warning-face)
              (":[0-9]+: .*" 0 comp-lock-error-face)
              ("^[0-9]+ error.*$" 0 comp-lock-bailedout-face)
              ("In function `.*" 0 comp-lock-infunction-face)
              ("^\\(CC\\|cc\\|g[+][+]\\|gcc\\|cxx\\|tar\\).*" 0 comp-lock-commline-face)
              ("^\\([[]checking.*\\)\\|\\([[]loaded.*\\)$" 0 comp-lock-commline-face)
              ("^Compilation finished.*$" 0 comp-lock-finished-face)
              ("^Compilation exited abnormally.*$" 0 comp-lock-bailedout-face)
              ))
      (font-lock-add-keywords 'compilation-mode compilation-mode-keywords 'set)
      ;; sgml mode keywords
      (setq sgml-font-lock-keywords
            '(("<\\(/?[a-z0-9]+[^>]*\\)>" 0 font-lock-function-name-face)
              ("<\\([!?][a-z0-9]+[^>]*\\)>" . font-lock-keyword-face)
              ("[&%][-.A-Za-z0-9]+;?" . font-lock-variable-name-face)
              ("<!--[^<>]*-->" . font-lock-comment-face)))
      (font-lock-add-keywords 'html-mode sgml-font-lock-keywords 'set)
      ;; default variables for the special custom faces (e.g. own compilation mode faces...)
      (defvar comp-lock-error-face 'comp-lock-error-face "")
      (defface comp-lock-error-face () "")
      (defvar comp-lock-warning-face 'comp-lock-warning-face "")
      (defface comp-lock-warning-face () "")
      (defvar comp-lock-infunction-face 'comp-lock-infunction-face "")
      (defface comp-lock-infunction-face () "")
      (defvar comp-lock-commline-face 'comp-lock-commline-face "")
      (defface comp-lock-commline-face () "")
      (defvar comp-lock-finished-face 'comp-lock-finished-face "")
      (defface comp-lock-finished-face () "")
      (defvar comp-lock-bailedout-face 'comp-lock-bailedout-face "")
      (defface comp-lock-bailedout-face () "")
      ;; hooks to turn font lock on automatically
      (add-hook 'latex-mode-hook 'turn-on-font-lock)
      (add-hook 'bibtex-mode-hook 'turn-on-font-lock)
      (add-hook 'c++-mode-hook 'turn-on-font-lock)
      (add-hook 'c-mode-common-hook 'turn-on-font-lock)
      (add-hook 'perl-mode-hook 'turn-on-font-lock)
      (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)
      (add-hook 'rmail-mode-hook 'turn-on-font-lock)
      (add-hook 'rmail-summary-mode-hook 'turn-on-font-lock)
      (add-hook 'mail-mode-hook 'turn-on-font-lock)
      (add-hook 'message-mode-hook 'turn-on-font-lock)
      (add-hook 'makefile-mode-hook 'turn-on-font-lock)
      (add-hook 'dired-mode-hook 'turn-on-font-lock)
      (add-hook 'sh-mode-hook 'turn-on-font-lock)
      (add-hook 'change-log-mode-hook 'turn-on-font-lock)
      ;; special treatment of some hooks... e.g. including turning on auto-fill-mode
      (add-hook 'html-mode-hook
                '(lambda ()
                   (turn-on-font-lock)
                   (auto-fill-mode)))
      ))


;;---------------------------------------------------------------------- c/c++-mode
(fmakunbound 'c-mode)
(makunbound 'c-mode-map)
(fmakunbound 'c++-mode)

(autoload 'c++-mode "cc-mode" "C++ Editing Mode" t)
(autoload 'c-mode   "cc-mode" "C Editing Mode" t)

(require 'cc-mode)
(c-initialize-cc-mode)
(setq-default indent-tabs-mode nil)  ;; do not indent with TABs

;; electric cc-mode indents

(setq c-style-alist
      (append c-style-alist 
              '(("electric_c_indent_list"
                 (c-basic-offset . 2)
                 (c-comment-only-line-offset . 4)
                 (c-offsets-alist .
                                  ((string . -1000)
                                   (c . c-lineup-C-comments)
                                   (defun-block-intro . +)
                                   (class-open . 0)
                                   (class-close . 0)
                                   (inline-open . 0)
                                   (inline-close . 0)
                                   (knr-argdecl-intro . +)
                                   (knr-argdecl . 0)
                                   (topmost-intro . 0)
                                   (topmost-intro-cont . 0)
                                   (member-init-intro . 0)
                                   (member-init-cont . 0)
                                   (inher-intro . +)
                                   (inher-cont . c-lineup-multi-inher)
                                   (block-open . -)
                                   (block-close . 0)
                                   (brace-list-open . 0)
                                   (brace-list-close . 0)
                                   (brace-list-intro . 0)
                                   (brace-list-entry . 0)
                                   (statement . c-lineup-runin-statements)
                                   (statement-cont . c-lineup-math)
                                   (statement-block-intro . +)
                                   (statement-case-intro . +)
                                   (substatement . +)
                                   (substatement-open . 0)
                                   (case-label . 0)
                                   (access-label . 0)
                                   (label . -)
                                   (do-while-closure . 0)
                                   (else-clause . 0)
                                   (comment-intro . c-lineup-comment)
                                   (arglist-intro . +)
                                   (arglist-cont . 0)
                                   (arglist-cont-nonempty . c-lineup-arglist)
                                   (arglist-close . +)
                                   (stream-op . c-lineup-streamop)
                                   (inclass . +)
                                   (cpp-macro . -1000)
                                   (friend . 0)))))))

(add-hook 'c-mode-common-hook
          (function
           (lambda ()
             (setq comment-column 40 )
             (c-set-style ' "electric_c_indent_list")
             )))

;; ---------------------------------------------------------------------- compile window settings
(setq compilation-window-height 10)

;;---------------------------------------------------------------------- java mode
(autoload 'java-mode "cc-mode" "Java Editing Mode" t)
(c-initialize-cc-mode)
(setq c-style-alist
      (append c-style-alist 
              '(("electric_java_indent_list"
                 (c-basic-offset . 2)
                 (c-comment-only-line-offset . 4)
                 (c-offsets-alist .
                                  ((string . -1000)
                                   (c . c-lineup-C-comments)
                                   (defun-block-intro . +)
                                   (class-open . 0)
                                   (class-close . 0)
                                   (inline-open . 0)
                                   (inline-close . 0)
                                   (knr-argdecl-intro . +)
                                   (knr-argdecl . 0)
                                   (topmost-intro . 0)
                                   (topmost-intro-cont . 0)
                                   (member-init-intro . 0)
                                   (member-init-cont . 0)
                                   (inher-intro . +)
                                   (inher-cont . c-lineup-multi-inher)
                                   (block-open . -)
                                   (block-close . 0)
                                   (brace-list-open . 0)
                                   (brace-list-close . 0)
                                   (brace-list-intro . 0)
                                   (brace-list-entry . 0)
                                   (statement . c-lineup-runin-statements)
                                   (statement-cont . c-lineup-math)
                                   (statement-block-intro . +)
                                   (statement-case-intro . +)
                                   (substatement . +)
                                   (substatement-open . 0)
                                   (case-label . 0)
                                   (access-label . 0)
                                   (label . -)
                                   (do-while-closure . 0)
                                   (else-clause . 0)
                                   (comment-intro . c-lineup-comment)
                                   (arglist-intro . +)
                                   (arglist-cont . 0)
                                   (arglist-cont-nonempty . c-lineup-arglist)
                                   (arglist-close . +)
                                   (stream-op . c-lineup-streamop)
                                   (inclass . +)
                                   (cpp-macro . -1000)
                                   (friend . 0)))))))

(add-hook 'java-mode-hook
          (function
           (lambda ()
             (setq comment-column 40 )
             (c-set-style ' "electric_java_indent_list")  ; otherwise set to "Java" each time
             )))

(defvar buffer-base-name nil "filename part of buffer-file-name (used for compile command)")
(add-hook
 'java-mode-hook
 '(lambda ()
    (make-local-variable 'compile-command)
    (string-match ".*/\\(.*\\)" buffer-file-name)
    (make-local-variable 'buffer-base-name)
    (setq buffer-base-name (substring buffer-file-name (match-beginning 1) (match-end 1)))
    (setq compile-command      
          (concat "DinoJavac " buffer-base-name)
          )))

;;---------------------------------------------------------------------- jde mode


;;---------------------------------------------------------------------- ps mode
(autoload 'postscript-mode "postscript" "PostScript Editing Mode" t)

(setq ps-indent-level 2)
(setq ps-tab-width 2)

;;---------------------------------------------------------------------- latex mode
(add-hook 'latex-mode-hook 'auto-fill-mode)

;;---------------------------------------------------------------------- ftp mode
(setq ange-ftp-smart-gateway t)
(setq ange-ftp-gateway-host "firewall")
(setq ange-ftp-gateway-program-interactive nil)
(setq ange-ftp-local-host-regexp "localhost")

;;---------------------------------------------------------------------- sgml mode
(autoload 'sgml-mode "psgml" "psgml - Major SGML editing mode" t)

;; some sgml-mode settings - for psgml
(setq sgml-system-path '("/usr/local/lib/sgml" 
                         "~/lib/sgml"))
(setq sgml-public-map '("/usr/local/lib/sgml/%d"
                        "~/lib/sgml/%d"
                        "~/lib/sgml/entities/iso/1992/\%N.ent" 
                        "~/lib/sgml/entities/iso/1991/\%N.ent" 
                        "~/lib/sgml/entities/iso/1986/\%N.ent"
                        "~/lib/sgml/entities/%N.ent"
                        ))

(setq sgml-live-element-indicator t)
(setq sgml-balanced-tag-edit t)
(setq sgml-indent-data nil)
(setq sgml-indent-step 0)
(setq sgml-recompile-out-of-date-cdtd 'ask)

;;---------------------------------------------------------------------- gnus
(autoload 'gnus "gnus" "Read network news." t)
(autoload 'gnus-post-news "gnuspost" "Post a new news." t)

(setq gnus-article-save-directory "~/News")

(setq browse-url-netscape-program "opera")

(add-hook 'message-mode-hook 'auto-fill-mode)

(defun tugraz-news()
  "Get news from the TU-Graz server."
  (interactive)
  (progn
    (setq gnus-init-file "~/.gnus-tugraz-news")
    (gnus)))

(defun dinopolis-news()
  "Get news from the Dinopolis nntp server."
  (interactive)
  (progn
    (setq gnus-init-file "~/.gnus-dinopolis-news")
    (gnus)))

;;---------------------------------------------------------------------- dired mode
(setq dired-use-gzip-instead-of-compress t)
(setq dired-guess-have-gnutar "tar")
(setq dired-dwim-target t)

;;---------------------------------------------------------------------- perl mode
(autoload 'perl-mode "perl-mode")
(setq perl-indent-level 2)
(setq perl-continued-statement-offset 2)
(setq perl-continued-brace-offset -2)
(setq perl-label-offset -2)
(setq perl-tab-to-comment nil)

;;---------------------------------------------------------------------- hexl mode
(setq hexl-iso "-iso")

;;---------------------------------------------------------------------- mail mode
(setq mail-signature t)        ;insert signature by default
(setq user-mail-address "kschmar@iicm.edu")
(add-hook 'mail-mode-hook 'auto-fill-mode)
;(define-mail-alias "my_alias" "some@email.address") ;; define your mail aliases this way

;;---------------------------------------------------------------------- rmail mode
; SuperCite stuff
(add-hook 'mail-citation-hook 'sc-cite-original)
(setq sc-citation-leader "")    ; no more spaces!
(setq sc-auto-fill-region-p nil)  ; no auto-fill of citations!
(setq rmail-delete-after-output t)
;(setq rmail-mail-new-frame t) ;compose mail in a new frame
;(setq rmail-display-summary t) ;always display mail summary

;;---------------------------------------------------------------------- vm mode
(autoload 'vm "vm" "Start VM on your primary inbox." t)
(setq vm-mutable-frames nil)
(setq vm-reply-subject-prefix "Re: ")

;;---------------------------------------------------------------------- pgp
(autoload 'mc-install-write-mode "mailcrypt" nil t)
(autoload 'mc-install-read-mode "mailcrypt" nil t)
(add-hook 'mail-mode-hook 'mc-install-write-mode)
(add-hook 'rmail-mode-hook 'mc-install-read-mode)
(add-hook 'rmail-summary-mode-hook 'mc-install-read-mode)

;;---------------------------------------------------------------------- global key bindings
(global-set-key [find] 'isearch-forward-regexp)
(global-set-key "\C-cc" 'compile)
(global-set-key "\M-]" 'forward-list)
(global-set-key "\M-[" 'backward-list)
(global-set-key "\M-g" 'goto-line)

(global-set-key [S-up] "\C-u1\M-v")
(global-set-key [S-down] "\C-u1\C-v")
(global-set-key [C-M-up] "\C-u1\M-v\C-p")
(global-set-key [C-M-down] "\C-u1\C-v\C-n")
(global-set-key [C-M-S-up] 'enlarge-window)
(global-set-key [C-M-S-down] 'shrink-window)

(global-set-key [home] 'beginning-of-line)
(global-set-key [end] 'end-of-line)

;;---------------------------------------------------------------------- mouse wheel bindings

(defun wheel-mouse-scroll-up (EVENT)
  "scroll screen up on a wheel event"
  (interactive "e")
  (scroll-up 5)
  (read-event)) ;; read-event prevents beep caused by the wheel-event

(defun wheel-mouse-scroll-down (EVENT)
  "scroll screen down on a wheel event"
  (interactive "e")
  (scroll-down 5)
  (read-event)) ;; read-event prevents beep caused by the wheel-event

(global-set-key [down-mouse-4] 'wheel-mouse-scroll-down)
(global-set-key [down-mouse-5] 'wheel-mouse-scroll-up)

;;---------------------------------------------------------------------- end of user-configured part

;;---------------------------------------------------------------------- emacs customized variables

(custom-set-variables
 '(query-replace-highlight t)
 '(gnus-interactive-catchup nil)
 '(gnus-read-active-file t)
 '(mark-holidays-in-calendar nil t)
 '(calendar-today-marker (quote font-lock-keyword-face))
 '(gnus-cache-enter-articles (quote (ticked dormant)))
 '(calendar-week-start-day 1 t)
 '(paren-dingaling-mode (quote T) t)
 '(search-upper-case t)
 '(gnus-thread-hide-subtree t)
 '(gnus-group-list-inactive-groups t)
 '(ispell-dictionary-alist (append ispell-dictionary-alist-1 ispell-dictionary-alist-2) t)
 '(all-christian-calendar-holidays t t)
 '(gnus-permanently-visible-groups ".*")
 '(gnus-large-newsgroup 500)
 '(standard-indent 2)
 '(calendar-holiday-marker (quote holiday-face))
 '(kill-ring-max 50))

(custom-set-faces
 '(comp-lock-bailedout-face ((((class color) (background dark)) (:bold t :foreground "orange"))) t)
 '(ps-special-fd-face ((((class color) (background dark)) (:bold t :foreground "MediumAquamarine"))))
 '(comp-lock-error-face ((((class color) (background dark)) (:bold t :foreground "yellow"))) t)
 '(comp-lock-infunction-face ((((class color) (background dark)) (:bold t :foreground "MediumAquamarine"))) t)
 '(font-lock-comment-face ((((class color) (background dark)) (:bold t :foreground "gray60"))))
 '(font-lock-reference-face ((((class color) (background dark)) (:bold t :foreground "LightSteelBlue"))))
 '(font-lock-string-face ((((class color) (background dark)) (:bold t :foreground "DeepSkyBlue3"))))
 '(message-header-xheader-face ((((class color) (background dark)) (:bold t :foreground "LightSteelBlue"))))
 '(holiday-face ((((class color)) (:bold t :foreground "black" :background "pink"))))
 '(font-lock-keyword-face ((((class color) (background dark)) (:bold t :foreground "#ffff80"))))
 '(ps-structuring-face ((((class color) (background dark)) (:bold t :foreground "orange"))))
 '(comp-lock-commline-face ((((class color) (background dark)) (:bold t :foreground "gray60"))) t)
 '(font-lock-type-face ((((class color) (background dark)) (:bold t :foreground "PeachPuff2"))))
 '(message-header-name-face ((((class color) (background dark)) (:bold t :foreground "MediumAquamarine"))))
 '(message-header-cc-face ((((class color) (background dark)) (:bold t :foreground "green1"))))
 '(message-header-subject-face ((((class color) (background dark)) (:bold t :foreground "gray60"))))
 '(font-lock-variable-name-face ((((class color) (background dark)) (:bold t :foreground "LightSalmon"))))
 '(comp-lock-finished-face ((((class color) (background dark)) (:bold t :foreground "LightSkyBlue3"))) t)
 '(font-lock-function-name-face ((((class color) (background dark)) (:bold t :foreground "OliveDrab3"))))
 '(widget-button-face ((t (:bold t :foreground "#ff9090"))))
 '(message-separator-face ((((class color) (background dark)) (:bold t :foreground "gray60"))))
 '(comp-lock-warning-face ((((class color) (background dark)) (:bold t :foreground "orange"))) t)
 '(ps-special-command-face ((((class color) (background dark)) (:bold t :foreground "orange"))))
 '(message-header-other-face ((((class color) (background dark)) (:bold t :foreground "red2"))))
 '(font-lock-builtin-face ((((class color) (background dark)) (:bold t :foreground "MediumAquamarine"))))
 '(message-cited-text-face ((((class color) (background dark)) (:bold t :foreground "gray60")))))
