(autoload 'nc "nc" "Emulate MS-DOG file shell" t)
(autoload 'getris "getris" "Tetris clone for emacs" t)

;; Enable debugging when something goes wrong in the .emacs file.
;(setq debug-on-error t)

;; Required packages to get this file to compile
(require 'cl)
(require 'completion)

(setq diff-switches "--side-by-side")

;;; Setup paths
;; Append paths to an emacs-lisp load-path (this path will be the
;; preferred path)
;; Return path.  (Not modified inline)
(defun add-on-path (path string &optional head)
  "Add a dir to the head of a search-path.  This will expand all
filenames to an absolute name.  The path will NOT be added if it is
already in the list.  I'm not certain if the path should be moved to
front if it do exist (the function doesn't presently do that).
  -EE (eivind@bitbox, eivind@vestnett.no)"
  (let* ((exp-string (expand-file-name string)))
    (if (not (member exp-string path))
	(if head
	    (cons exp-string path)
	  (append path (list exp-string)))
      path)))



(defun insert-c++-function-header()
  "Insert a doxygen function header template"
  (interactive)
  (insert-file "~/cpp_header.templ")
)

(defun insert-c++-class-header()
  "Insert a doxygen class header template"
  (interactive)
  (insert-file "~/cpp_class_header.templ")
)

(global-set-key "\C-h1" 'insert-c++-function-header)
(global-set-key "\C-h2" 'insert-c++-class-header)


;; Setup lisp source-path
(setq load-path (add-on-path load-path "/usr/emacs/share/emacs/site-lisp/" t))
(setq load-path (add-on-path load-path "/usr/emacs/share/emacs/site-lisp/w3" t))


(setq dired-listing-switches "-alB")
(setq vc-mistrust-permissions t)
(setq vc-consult-headers nil)
;(setq vc-handle-cvs nil)
;; Info files


(setq Info-default-directory-list
      (add-on-path Info-default-directory-list "/usr/info"))


; Macros ripped from tinylibm.el to make W3 work
(defmacro defgroup (&rest args) nil)
(defmacro defcustom (var value doc &rest args)
  (` (defvar (, var) (, value) (, doc))))

(setq scroll-step 2)
(setq grep-command "find ./ -name \"*.c\" | xargs grep -n ")
(setq grep-null-device "")
;;(setq grep-null-device "/dev/null")

(setq asm-comment-char 255)
;; Put imenu on a key.
;(global-set-key "\C-cf" 'imenu)

(global-set-key "\C-cf" 'search-forward)
(global-set-key [f3] 'nonincremental-repeat-search-forward)
(global-set-key [f9] 'woman)
(global-set-key [f11] 'desktop-save)
(global-set-key [f12] 'desktop-read)
(global-set-key [C-f4] 'kill-this-buffer)

(global-set-key "\C-f" 'font-lock-fontify-buffer)
(global-set-key [C-f1] 'call-last-kbd-macro)

(global-set-key "\C-z" 'redraw-display) ;; Got tired of suspending emacs by accident
(global-set-key "\C-x\C-z" 'redraw-display) ;; Got tired of suspending emacs by accident
(global-set-key [f5] 'shell )

;;; Set flags (nil/t/other variables)
;; Make the cursor stop at the end of the buffer (don't make more file...)
(setq next-line-add-newlines nil)

;; Display time using 24 hour format. (Default to nil == AM/PM format)
(setq display-time-24hr-format t)
(display-time)

;; Line and column numbers
(setq line-number-mode t)
(setq column-number-mode t)


;; Ask if lacking newline at end of file at save
(setq require-final-newline 'maybe)


;; Incremental search highlight
(setq search-highlight t)


;; I want to see the marked region, and that only happen with
;; transient-mark-mode.
(setq transient-mark-mode t)
(transient-mark-mode t)

;; But the mark can be used even if inactive :)
(setq mark-even-if-inactive t)




;;; W3 - the emacs WWW browser
; Setup loading
(setq load-path (cons (expand-file-name "/usr/local/share/emacs/site-lisp/w3")
		      load-path))
(autoload 'w3-preview-this-buffer "w3" "WWW Previewer" t)
(autoload 'w3-follow-url-at-point "w3" "Find document at pt" t)
(autoload 'w3 "w3" "WWW Browser" t)
(autoload 'w3-open-local "w3" "Open local file for WWW browsing" t)
(autoload 'w3-fetch "w3" "Open remote file for WWW browsing" t)
(autoload 'w3-use-hotlist "w3" "Use shortcuts to view WWW docs" t)
(autoload 'w3-show-hotlist "w3" "Use shortcuts to view WWW docs" t)
(autoload 'w3-follow-link "w3" "Follow a hypertext link." t)
(autoload 'w3-batch-fetch "w3" "Batch retrieval of URLs" t)


(global-set-key "\C-cw" 'w3-open-local)

(setq w3-delimit-links nil)
(setq w3-delimit-emphasis nil)
;(setq w3-style-chars-assoc nil)
;(setq w3-header-chars-assoc nil)

;; Woman - the emacs man browser

(autoload 'woman "woman"
  "Decode and browse a UN*X man page." t)
;(load "woman")
(autoload 'woman-find-file "woman"
  "Find, decode and browse a specific UN*X man-page file." t)

(setq man-path "/boot/altos/man/")


(setq auto-save-default t)
(setq auto-save-timeout 60)
(setq auto-save-interval 300)

(pc-selection-mode)

(setq tab-with 8)

(setq c-basic-offset 4)
(setq indent-tabs-mode 4)

(defconst my-c-style
  '(
    (c-tab-always-indent . nil)
    (c-comment-only-line-offset . 2)
    (c-hanging-braces-alist . ((substatement-open after)(brace-list-open)))
    (c-hanging-colons-alist . ((member-init-intro before) (inher-intro) (case-label after) (label after) (access-label after)))
    (c-cleanup-list . (scope-operator empty-defun-braces defun-close-semi))
    (c-offsets-alist . ((arglist-close . c-lineup-arglist) (substatement-open . 0) (case-label . 4) (label . 0) (block-open . 0) (knr-argdecl-intro . -)))
    (c-echo-syntactic-information-p . t)
    )
  "My C Programming Style"
)
 
;;Customizations for all of c-mode, c++-mode, and objc-mode
(defun my-c-mode-common-hook ()
  ;; add my personal style and set it for the current buffer
  (c-add-style "PERSONAL" my-c-style t)
  ;; offset customizations not in my-c-style
  (c-set-offset 'member-init-intro '++)
  ;; other customizations
  (setq tab-width 8
  ;; this will make sure spaces are used instead of tabs
	indent-tabs-mode t)
  ;; we like auto-newline and hungry-delete
;  (c-toggle-auto-hungry-state 1)
;  (c-toggle-auto-newline-state 1)

  ;; keybindings for C, C++, and Objective-C.  We can put these in
  ;; c-mode-map because c++-mode-map and objc-mode-map inherit it
  (define-key c-mode-map "\C-m" 'newline-and-indent)
  (define-key c-mode-map [f4] 'next-error)
  (define-key c-mode-map [f7] 'compile)
  )

(global-set-key [f4] 'next-error)
(global-set-key [f7] 'compile)
;(add-hook 'c-mode-common-hook '(lambda () (c-toggle-auto-newline-state 1)))

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

(set-background-color "White")
(set-foreground-color "Black")

(show-paren-mode 1)
(standard-display-european 1)
(setq dabbrev-case-replace nil)


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

(setq compile-command "make -j 4")

;;; Go into c++-mode or perl-mode depending on file extention

(setq auto-mode-alist
      (append '(("\\.c$"   . c++-mode)
		("\\.C$"   . c++-mode)
		("\\.cc$"  . c++-mode)
		("\\.cpp$" . c++-mode)
		("\\.cxx$" . c++-mode)
		("\\.hxx$" . c++-mode)
		("\\.hpp$" . c++-mode)
		("\\.h$"   . c++-mode)
		("\\.hh$"  . c++-mode)
		("\\.idl$" . c++-mode)
		("\\.inl$" . c++-mode)
		("\\.c$"   . c-mode)
                ("\\.pl$" . perl-mode)
                ("\\.pm$" . perl-mode)
                ("\\.java$" . java-mode)
                ("\\.?[Ff][Aa][Qq]$" . faq-mode)
                ("\\.txt$" . text-mode))
	      auto-mode-alist))

(global-set-key [C-up]   'enlarge-window)
(global-set-key [C-down]  'shrink-window)

;;; Append ~/lisp to the load-path if it exist.
(cond ((file-exists-p "~/lisp")
       (setq load-path (cons (expand-file-name "~/lisp") load-path))))

(cond ((fboundp 'global-font-lock-mode)
       ;; Turn on font-lock in all modes that support it
       (global-font-lock-mode t)
       ;; Maximum colors
       (setq font-lock-maximum-decoration t)
       ;; Customize face attributes
       (setq font-lock-face-attributes
	     ;; Symbol-for-Face Foreground Background Bold Italic Underline
	     '((font-lock-comment-face       "red")
	       (font-lock-string-face        "lightmagenta")
	       (font-lock-keyword-face       "lightblue")
	       (font-lock-function-name-face "black")
	       (font-lock-variable-name-face "black")
	       (font-lock-type-face          "magenta")
	       (font-lock-reference-face     "magenta")))
       ;; Create the faces from the attributes
       (font-lock-make-faces)))

(set-face-background 'region "yellow")
(set-face-foreground 'underline "blue")
(set-face-foreground 'bold "lightblue")
;(set-face-foreground 'info-xref "lightblue")
;(set-face-foreground 'woman-bold-face "lightblue")

;; A litle hack to work arount somting that looks like a wrong key-mapping
(global-set-key [\C-next] 'redraw-display)

(global-set-key [M-return] 'dabbrev-expand)


(global-set-key [\M-right] 'other-window)
(global-set-key [\M-left] 'other-window)

;; Put goto-line on a key.
(global-set-key "\C-c\g" 'goto-line)

;(require 'cvs)

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


(setq c++-font-lock-keywords-3
  (append c++-font-lock-keywords-3
   (list
    '("\\<\\(dynamic_cast\\|static_cast\\|reinterpret_cast\\|using\\|namespace\\)\\>" . font-lock-keyword-face)
    )))

(setq c++-font-lock-keywords-3
  (append c++-font-lock-keywords-3
   (list
    '("\\<\\(uint\\|uint8\\|uint16\\|uint32\\|uint64\\|int8\\|int16\\|int32\\|int64\\|true\\|false\\|NULL\\)\\>" . font-lock-type-face)
    )))

;; Disable debugger when finnished with .emacs
;(setq debug-on-error nil)

(put 'downcase-region 'disabled nil)

(put 'upcase-region 'disabled nil)
