(set-foreground-color "light grey")
(set-background-color "black")
(set-cursor-color "light blue")
(set-mouse-color "light blue")
(set-border-color "light grey")

;Show current line and column in the status field.
(setq line-number-mode t)
(setq column-number-mode t)
(display-time)

; necessary support function for buffer burial
(defun crs-delete-these (delete-these from-this-list)
  "Delete DELETE-THESE FROM-THIS-LIST."
  (cond
   ((car delete-these)
    (if (member (car delete-these) from-this-list)
	(crs-delete-these (cdr delete-these) (delete (car delete-these)
                                                     from-this-list))
      (crs-delete-these (cdr delete-these) from-this-list)))
   (t from-this-list)))

; this is the list of buffers I never want to see
(defvar crs-hated-buffers
  '("KILL" "*Compile-Log*" "*Messages*" "*Completions*" "*scratch*"))

; might as well use this for both
(setq iswitchb-buffer-ignore (append '("^ " "*Buffer") crs-hated-buffers))

(defun crs-hated-buffers ()
  "List of buffers I never want to see, converted from names to buffers."
  (delete nil
	  (append
	   (mapcar 'get-buffer crs-hated-buffers)
	   (mapcar (lambda (this-buffer)
		     (if (string-match "^ " (buffer-name this-buffer))
			 this-buffer))
		   (buffer-list)))))

; I'm sick of switching buffers only to find KILL right in front of me
(defun crs-bury-buffer (&optional n)
  (interactive)
  (unless n
    (setq n 1))
  (let ((my-buffer-list (crs-delete-these (crs-hated-buffers)
					  (buffer-list (selected-frame)))))
    (switch-to-buffer
     (if (< n 0)
	 (nth (+ (length my-buffer-list) n)
	      my-buffer-list)
       (bury-buffer)
       (nth n my-buffer-list)))))

(global-set-key [(control tab)] 'crs-bury-buffer)
(global-set-key [(control shift tab)] (lambda ()
				       (interactive)			    
				       (crs-bury-buffer -1)))
(defun switch-to-makefile ()
  (switch-to-buffer "Makefile"))

(global-set-key [(control f7)] 'compile)
(global-set-key [(control f6)] (lambda ()
				 (interactive)
				 (switch-to-makefile)))

(global-set-key [(control f8)] 'eval-region)

(global-set-key [(shift button3)] 'imenu)

(add-hook 'c-mode-hook 'imenu-add-menubar-index)

;(if (boundp (tool-bar-mode))
;	    (tool-bar-mode -1))

(iswitchb-default-keybindings)

;; Good for opening header file under cursor
(global-set-key "\C-cf" 'open-file-under-cursor)

;;Goto-line n where n represents the line number
(global-set-key "\C-xg" 'goto-line)

(fset 'open-file-under-cursor
   [?\C-\M-b ?\C-  ?\C-\M-f ?\C-\M-f ?\M-w ?\C-x ?\C-f ?\C-y return])

(global-set-key "\C-cy" 'do-smart-yank)

(fset 'do-smart-yank
   "\C-y\C-c\C-q")

(custom-set-variables
 ;; Kill the whole line when using C-k
;; '(kill-whole-line t)
 ;; Compilation: Set new window height, never ask for compile command, save all files and sets new compile command.
 '(compilation-window-height 20)
 '(compilation-read-command nil)
 '(compilation-ask-about-save nil)
 '(compile-command "make -k"))

(load "desktop")
(desktop-load-default)
(desktop-read)

;; Make the % key jump to the matching {}[]() if on another, like VI
(global-set-key "%" 'match-paren)

(defun match-paren (arg)
  "Go to the matching parenthesis if on parenthesis otherwise insert %."
  (interactive "p")
  (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
        ((looking-at "\\s\)") (forward-char 1) (backward-list 1))
        (t (self-insert-command (or arg 1)))))

;;Enable opposite bracket/paranthesis highlighting
(require 'paren)
(show-paren-mode t)
(setq blink-matching-paren-on-screen t)

;; Enable font lock (colours) for all modes that support it:
(global-font-lock-mode t)

;; Maximum decoration for all modes
(setq-default font-lock-maximum-decoration t)

(setq auto-mode-alist
      (append '(("\\.C$"    . c++-mode)
        ("\\.cc$"   . c++-mode)
        ("\\.cpp$"  . c++-mode)
        ("\\.cxx$"  . c++-mode)
        ("\\.hxx$"  . c++-mode)
        ("\\.h$"    . c++-mode)
        ("\\.hh$"   . c++-mode)
        ("\\.idl$"  . c++-mode)
        ("\\.ipp$"  . c++-mode)
        ("\\.c$"    . c-mode)
        ("\\.pl$"   . perl-mode)
        ("\\.pm$"   . perl-mode)
        ("\\.java$" . java-mode)
        ("\\.txt$"  . text-mode))
          auto-mode-alist))


(add-hook 'font-lock-mode-hook
	  (lambda ()
	    ;; This is cool...
	    (setq font-lock-keywords
		  (append font-lock-keywords
			  '(("\\(FIX\\([ -_]?ME\\)?\\|FUCK\\|HELP\\([_- ]?ME\\)?\\|TODO\\|HACK\\)" (1 'font-lock-warning-face t)))))
	    ;; Lazy font lock is good. 5000 line C++ files get on my nerves
	    ;; without it. But smaller files should be fontified on open so
	    ;; they can be browsed quickly. This is the solution:
	    ;;(if (> (buffer-size) 10000) (call-if-fbound 'turn-on-lazy-lock))

))

;; Stuff to avoid when reloading ~/.emacs, but do when starting the app.
(if (not (boundp 'emacs-loaded))
	(progn
	  (defvar emacs-loaded 1)
	  ;;      (gnuserv-start)))
	  ;;	  ;; We do this here because RMS tells us to
	  ;;	  (desktop-load-default)
	  ;;	  (desktop-read))
))
(put 'upcase-region 'disabled nil)