;;;; -*-Mode:LISP; Package:LISP; Base:10; Syntax:EmacsLisp -*-
;;;; Date:	2009/08/25
;;;; Title:	.emacs
;;;; Author:	C. Jullien

(defvar running-xemacs (string-match "XEmacs\\|Lucid" emacs-version))

(require 'cl)
(require 'cc-mode)

(autoload 'View-scroll-line-forward  "view" nil t)
(autoload 'View-scroll-line-backward "view" nil t)
(autoload 'bat-mode                  "bat-mode" "DOS and WIndows BAT files" t)

;; Set  up  the  keyboard  so  the  delete  key  on  both the regular
;; keyboard  and the keypad delete the character under the cursor and
;; to the right under X, instead of the default, backspace behavior.

(global-set-key [delete]    'delete-char)
(global-set-key [kp-delete] 'delete-char)
(global-set-key (kbd "C-$") 'dabbrev-expand)
(global-set-key (kbd "M-$") 'compare-windows)
(global-set-key [f5]        'undo)
(global-set-key [f9]        'call-last-kbd-macro)
(global-set-key [C-home]    'View-scroll-line-backward)
(global-set-key [C-end]     'View-scroll-line-forward)
(global-set-key [f1]        (lambda ()
                               (interactive)
                               (manual-entry (current-word))))

;; c-mode-base-map is common to C/C++/Java ...

(define-key c-mode-base-map "\C-m"   'newline-and-indent)

;; Always end a file with a newline

(setq require-final-newline t)

;; Required with Emacs 23 so that C-n and C-p move to physical line
(setq line-move-visual nil)

;; Stop at the end of the file, not just add lines

(setq next-line-add-newlines nil)

(defvar max-width    80)
(defvar max-height   25)

;;
;; set font
;;

(defvar current-font
        nil
;       "-outline-Courier New-normal-r-*-*-13-97-*-*-c-*-*-iso8859-1"
)

(defvar current-top  28)   ;; 10
(defvar current-left 28)   ;; 10
(defvar use-cmd-shell nil) ;; t == cmdproxy

(cond
      ((eq system-type 'windows-nt)
       ;; Add support for nmake
       ;;
       (setq compile-command '("nmake " . 6))
       ;;
       ;; This  assumes  that  Cygwin  is  installed in C:\cygwin (the
       ;; default)  and  that  C:\cygwin\bin  is  not  already in your
       ;; Windows Path (it generally should not be).
       ;;
       ;; (setq exec-path (cons "C:/cygwin/bin" exec-path))
       ;; (setenv "PATH" (concat "C:\\cygwin\\bin;" (getenv "PATH")))
       ;;
       ;; NT-emacs assumes a Windows command shell, which you change
       ;; here.
       ;;
       (cond
             (use-cmd-shell
               (setq explicit-shell-file-name "cmdproxy")
               (setq shell-file-name "cmdproxy")
               (setq shell-command-switch "\\/u")
               (setq w32-quote-process-args t))
             (t
               (setq process-coding-system-alist '(("bash" . undecided-unix)))
               (setq w32-quote-process-args ?\")
               (setq shell-file-name "bash")
               (setenv "SHELL" shell-file-name) 
               (setq explicit-shell-file-name shell-file-name)))
       (setq current-top  0)
       (setq current-left 72) ;; leave room for icons.
       ;;
       ;; This removes unsightly ^M characters that would otherwise
       ;; appear in the output of java applications.
       ;;
       (add-hook 'comint-output-filter-functions 'comint-strip-ctrl-m))
      (t
       ;; Clipboard
       (setq x-select-enable-clipboard t)
       (setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
       (setq current-left 72) ;; leave room for icons.
       t))

(when window-system
      (let ((screen-height (x-display-pixel-height))
	    (screen-width  (x-display-pixel-width)))
           (when current-font
                 (set-default-font current-font))
	   (setq max-height (/ screen-height (frame-char-height)))
;          (setq max-width  (/ screen-width  (frame-char-width)))
	   ;; reserve some characters (for menu bar).
	   (cond
		 ((eq system-type 'windows-nt)
;		  (setq max-width  (+ max-width  12))
		  (setq max-height (- max-height 6))
                  t)
		 (t
;		  (setq max-width  (+ max-width  3))
		  (setq max-height (- max-height 8))))))

;; Don't display welcome message when Emacs starts.

(setq inhibit-startup-message t)

;; With  default-frame-alist,  we  set  the  top  left  corner of new
;; frames  to  be at pixel offset +10+10,  the width and height to be
;; computed,  the cursor to be white, the foreground to be white, the
;; background  to  be NavyBlue,  and the font to be Courier 10.  With
;; initial-frame-alist,  we  override  the  top  left  corner  of the
;; initial  frame  to  be  at  pixel  offset +10+10,  and inherit the
;; remaining properties from initial-frame-alist.

(setq default-frame-alist
      (list
            (cons 'width            max-width)
	    (cons 'height           max-height)
;	    (cons 'font             current-font)
	    (cons 'top              current-top)
	    (cons 'left             current-left)
	    (cons 'cursor-type      'box)
	    (cons 'cursor-color     "white")
	    (cons 'foreground-color "LightBlue")
	    (cons 'background-color "rgb:00/20/60")))
;	    (cons 'background-color "NavyBlue")))

(setq initial-frame-alist default-frame-alist) ;; '((top . 10) (left . 10))

(setq frame-title-format (concat "%b - emacs@" system-name))

;; The  above  code  uses  the  default faces for decoration.  If you
;; would  like to customize the attributes of the faces,  you can use
;; the following startup code to get started

(cond
      ((fboundp 'global-font-lock-mode)
       ;; Load the font-lock package.
       (require 'font-lock)
       ;; Customize face attributes
       (custom-set-faces
        '(left-margin ((t (:background "black"))) t)
        `(font-lock-builtin-face       ((t (:foreground "Light Steel Blue"))))
        '(font-lock-comment-face       ((t (:foreground "Green" :italic t))))
        '(font-lock-constant-face      ((t (:foreground "Orchid"))))
        '(font-lock-doc-face           ((t (:foreground "Wheat3"))))
        '(font-lock-doc-string-face    ((t (:foreground "Wheat3"))))
        '(font-lock-function-name-face ((t (:foreground "Orange" t))))
        '(font-lock-keyword-face       ((t (:foreground "Cyan" :bold t))))
        '(font-lock-preprocessor-face  ((t (:foreground "Wheat" t))))
        '(font-lock-reference-face     ((t (:foreground "orangered"))))
        '(font-lock-string-face        ((t (:foreground "Yellow"))))
        '(font-lock-type-face          ((t (:foreground "Grey" t))))
        '(font-lock-variable-name-face ((t (:foreground "LightGrey" t))))
        '(font-lock-warning-face       ((t (:foreground "Red" t))))
        )
       ;; Maximum colors
       (setq font-lock-maximum-decoration t)
       ;; Turn on font-lock in all modes that support it
       (global-font-lock-mode t)))

;; mouse ptr when cursor is too close
(mouse-avoidance-mode 'jump)

;; ... on the right
(set-scroll-bar-mode 'right)

;; To  highlight  the region between the point and the mark,  use the
;; function transient-mark-mode: 

(transient-mark-mode t)

;; To  highlight  matching  parenthesis,  add  the  following to your
;; startup file: 

(show-paren-mode 1)

(add-hook 'shell-mode-hook
          '(lambda () (setq comint-completion-addsuffix '("/" . "")))
          t)

(defun unicode-shell ()
   ;; Execute the shell buffer in UTF-8 encoding.
   ;; Note that you'll need to set the environment variable LANG and othersd
   ;; appropriately.
   (interactive)
   (let ((coding-system-for-read 'utf-16)
         (coding-system-for-write 'utf-16)
         (coding-system-require-warning t))
        (call-interactively 'shell)))

(transient-mark-mode 1)

;; (add-hook 'c-mode-common-hook '(lambda () (setq c-basic-offset 8)) t)

(setq line-indent-mode t)

(defun my-c-mode-hook ()
   "Hook for running C file..."
   ;;don't indent braces
   (c-set-offset 'substatement-open 0)
   (c-set-offset 'statement-case-open 0)
   (c-set-offset 'case-label 0) ; '+ to indent
   (setq tab-width 8)
   (setq c-basic-offset 8)
   (setq c-auto-newline nil)
;  (setq c-indent-comments-syntactically-p t)
   ;; make sure spaces are used instead of tabs
   (setq indent-tabs-mode t)
   t)

(add-hook 'c-mode-hook 'my-c-mode-hook)

(defun my-c++-mode-hook ()
   "Hook for running C file..."
   ;;don't indent braces
   (c-set-offset 'substatement-open 0)
   (c-set-offset 'statement-case-open 0)
   (c-set-offset 'case-label 0) ; '+ to indent
   (setq tab-width 2)
   (setq c-basic-offset 1)
   (setq c-auto-newline nil)
;  (setq c-indent-comments-syntactically-p t)
   ;; make sure spaces are used instead of tabs
   (setq indent-tabs-mode nil)
   t)

(add-hook 'c++-mode-hook 'my-c++-mode-hook)

(defun my-java-mode-hook ()
   "Hook for running Java file..."
   ;;don't indent braces
   (c-set-offset 'substatement-open 0)
   (c-set-offset 'statement-case-open 0)
   (c-set-offset 'case-label 0) ; '+ to indent
   (setq tab-width 2)
   (setq c-basic-offset 2)
   (setq c-auto-newline nil)
;  (setq c-indent-comments-syntactically-p t)
   ;; make sure spaces are used instead of tabs
   (setq indent-tabs-mode nil)
   t)

(add-hook 'java-mode-hook 'my-java-mode-hook)

;; Add more 'makefile' patterns

(setq auto-mode-alist
      (append
         '(("makefile\\." . makefile-mode)
           ("\\.mak"      . makefile-mode)
           ("Makefile\\." . makefile-mode))
         auto-mode-alist))

;; Add PHP extension to c-mode mode.

(setq auto-mode-alist
      (append
         '(("\\.php\\'" . c-mode))
         auto-mode-alist))

;; Add BAT extension to bat-mode mode.

(setq auto-mode-alist 
      (append 
         (list (cons "\\.[bB][aA][tT]$" 'bat-mode))
         ;; For DOS init files
         (list (cons "CONFIG\\."   'bat-mode))
         (list (cons "AUTOEXEC\\." 'bat-mode))
         auto-mode-alist))

;; Customize c-mode

(setq c-default-style     '((other . "user")))

;; Basic indent is 8 characters.
(setq c-basic-offset      8)

;; insert a tab if point is in the middle of a line.
(setq c-tab-always-indent nil)

;; Make searches case sensitive by default (in all buffers that do not
;; override this). 

(setq-default case-fold-search nil)

;; Rebind ESG-g to goto-line

(global-set-key "\M-g" 'goto-line)

;; Add support for European character sets

;(standard-display-european 1)

(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(font-lock-builtin-face ((t (:foreground "Light Steel Blue"))))
 '(font-lock-comment-face ((t (:foreground "Green" :italic t))))
 '(font-lock-constant-face ((t (:foreground "Orchid"))))
 '(font-lock-doc-face ((t (:foreground "Wheat3"))))
 '(font-lock-doc-string-face ((t (:foreground "Wheat3"))))
 '(font-lock-function-name-face ((t (:foreground "Orange" t))))
 '(font-lock-keyword-face ((t (:foreground "Cyan" :bold t))))
 '(font-lock-preprocessor-face ((t (:foreground "Wheat" t))))
 '(font-lock-reference-face ((t (:foreground "orangered"))))
 '(font-lock-string-face ((t (:foreground "Yellow"))))
 '(font-lock-type-face ((t (:foreground "Grey" t))))
 '(font-lock-variable-name-face ((t (:foreground "LightGrey" t))))
 '(font-lock-warning-face ((t (:foreground "Red" t))))
 '(left-margin ((t (:background "black"))) t))

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(safe-local-variable-values (quote ((indent-tabs-mode . 1) (Syntax . ANSI-Common-Lisp) (Syntax . EmacsLisp) (Mode . LISP) (Package . LISP) (Base . 10) (Syntax . ISLISP) (Encoding . utf-8) (Encoding . utf-16)))))

(defun clozure (&optional and-go)
   "Run Clozure"
   (interactive)
   (setq inferior-lisp-program "f:/ccl/wx86cl64.exe") ; your Lisp system
   (add-to-list 'load-path "f:/slime/") ; your SLIME directory
   (require 'slime)
   (slime-setup)
   (slime))

(defun check-openlisp (path)
   ;; check if openlisp.el exists and load it.
   (let ((path     (getenv path))
         (found    nil)
         (openlisp nil)
         (batmode  nil))
        (when path
              (setq openlisp (concat path "/emacs/openlisp.el"))
              (when (file-exists-p openlisp)
                    (load openlisp)
                    (setf found t))
              (setq batmdode (concat path "/emacs/bat-mode.el"))
              (when (file-exists-p batmdode)
                    (load batmdode)))
        found))

(or
    (check-openlisp "HOME")
    (check-openlisp "HOMEPATH")
    (check-openlisp "OPENLISP"))

(put 'downcase-region 'disabled nil)
(put 'upcase-region 'disabled nil)