;; Load my local paths
(setq load-path (cons "/usr/share/emacs/site-lisp" load-path))
(add-to-list 'load-path "~/.elisp")
(add-to-list 'load-path "~/.elisp/matlab")

;; Load Gentoo generated files
(load "/usr/share/emacs/site-lisp/site-gentoo")

;; Enable normal selection with shift+arrows
(cua-mode)
(setq cua-enable-cua-keys nil)

;; Subversion stuff
;;(require 'psvn)
(require 'vc-svn)

;;; Load the fucking undo/redo fix. morons...
(require 'redo)
(global-set-key [(alt z)] 'undo)
(global-set-key [(alt shift z)] 'redo)

;;; Load the code folding mode
;;(hs-minor-mode)
(global-set-key (kbd "C-c <left>")  'hs-hide-block)
(global-set-key (kbd "C-c <right>") 'hs-show-block)

;; Set colors
(set-background-color "gray96")

;;;; Keyboard shortcuts
;; Pager stuff (scroll uten aa flytte cursoren)
(require 'pager)
(global-set-key "\C-v"    'pager-page-down)
(global-set-key [next]    'pager-page-down)
(global-set-key "\ev"     'pager-page-up)
(global-set-key [prior]   'pager-page-up)
(global-set-key "\M-p"    'pager-row-up)
(global-set-key '[M-up]   'pager-row-up)
(global-set-key '[M-kp-8] 'pager-row-up)
(global-set-key "\M-n"    'pager-row-down)
(global-set-key '[M-down] 'pager-row-down)
(global-set-key '[M-kp-2] 'pager-row-down)

;; Fix some useful shortcuts
(global-set-key '[A-right] 'next-buffer)
(global-set-key '[A-left] 'prev-buffer)

;;;;;;;; Scrolling stuff ;;;;;;;;

;; Scroll without moving cursor
(defun scroll-down-keep-cursor () 
;; the text one line down while keeping the cursor 
    (interactive) 
    (scroll-down 1))
(defun scroll-up-keep-cursor () 
    ;; Scroll the text one line up while keeping the cursor 
    (interactive) 
    (scroll-up 1))
(global-set-key '[C-M-up] 'scroll-down-keep-cursor)
(global-set-key '[C-M-down] 'scroll-up-keep-cursor)

;; Scroll margin and stop-that-bloody-halfpage-jump
(setq scroll-margin 3)
(setq scroll-step 0)
(setq scroll-conservatively 100)

;;;;;;;; Shortcut stuff ;;;;;;;;

;; Shortcuts to make it more consistent with Mac
(global-set-key [(alt a)] 'mark-whole-buffer)
(global-set-key [(alt v)] 'yank)
(global-set-key [(alt c)] 'kill-ring-save)
(global-set-key [(alt x)] 'kill-region)
(global-set-key [(alt s)] 'save-buffer)
(global-set-key [(alt l)] 'goto-line)
(global-set-key [(alt o)] 'find-file)
(global-set-key [(alt f)] 'isearch-forward)
(global-set-key [(alt g)] 'isearch-repeat-forward)
(global-set-key [(alt w)]
                 (lambda () (interactive) (kill-buffer (current-buffer))))
;;(global-set-key [(alt w)] 'kill-buffer)
(global-set-key [(alt .)] 'keyboard-quit)

;; I disabled this since I want to avoid hitting Cmd-q accidentally.
(global-set-key [(alt q)] 'save-buffers-kill-emacs)
;; Confirm on exit :-)
(setq confirm-kill-emacs 'y-or-n-p)

;; Minimize window
(global-set-key [(alt m)] 'iconify-frame)

;; More powerful tab-completion
(add-hook 'minibuffer-setup-hook    
          '(lambda ()  
         (define-key minibuffer-local-map "\t" 'comint-dynamic-complete)))

;;;;;;;; Matlab stuff ;;;;;;;;
(autoload 'tlc-mode "tlc" "tlc Editing Mode" t)
(add-to-list 'auto-mode-alist '("\\.tlc$" . tlc-mode))
(setq tlc-indent-function t)

(autoload 'matlab-mode "matlab" "Enter Matlab mode." t)
(setq auto-mode-alist (cons '("\\.m\\'" . matlab-mode) auto-mode-alist))
(autoload 'matlab-shell "matlab" "Interactive Matlab mode." t) 
(setq matlab-shell-command-switches '("-nodesktop" "-nosplash" "-display :0.0"))
;(setq matlab-shell-command-switches "-nojvm -nosplash -r 'oldpwd = pwd;cd ~/phase-project/functions/my/various/;startup;cd(oldpwd);istmatlab'")
(setq matlab-indent-function t)     ; if you want function bodies indented 
(setq matlab-verify-on-save-flag nil) ; turn off auto-verify on save 
(defun my-matlab-mode-hook () 
  (setq fill-column 76)          ; where auto-fill should wrap 
  (setq matlab-comment-region-s "% ") 
  (defun matlab-insert-parentheses () 
    (interactive) 
    (let ((parens-require-spaces nil)) 
      (insert-parentheses 0))) 
  (define-key matlab-mode-map "\M-(" 'matlab-insert-parentheses))
(add-hook 'matlab-mode-hook 'my-matlab-mode-hook) 
(defun my-matlab-shell-mode-hook () 
  '()) 
(add-hook 'matlab-shell-mode-hook 'my-matlab-shell-mode-hook) 
(setq matlab-comment-anti-indent -2)
(setq matlab-indent-level 4)
(setq matlab-shell-command "/Applications/MATLAB704/bin/matlab")



;;;;;;;; Editing stuff ;;;;;;;;
;;(show-paren-mode t nil (paren)) ;; funkerikke
(setq blink-matching-paren )
(setq c-tab-always-indent nil)
(setq standard-indent 4)
(setq tab-always-indent nil)

;;;;;;;; ECB and GUI stuff ;;;;;;;;

;; Various settings
(setq ecb-layout-name "leftright2")
(setq ecb-layout-window-sizes (quote (("leftright2" (0.24427480916030533 . 0.46) (0.24427480916030533 . 0.52) (0.183206106870229 . 0.46) (0.183206106870229 . 0.52)))))
(setq ecb-primary-secondary-mouse-buttons (quote mouse-1--mouse-2))
(setq ecb-source-path (quote (("~/phase-project/functions/my" #("My project functions" 0 20 (help-echo tree-buffer-help-echo-fn mouse-face highlight))) ("~/phase-project/functions/magnitude_only" #("Hal's project functions" 0 23 (help-echo tree-buffer-help-echo-fn mouse-face highlight))) ("~/school/functions" #("Other functions" 0 15 (help-echo tree-buffer-help-echo-fn mouse-face highlight))) ("~" #("home" 0 4 (help-echo tree-buffer-help-echo-fn mouse-face highlight))) ("/" #("root" 0 4 (help-echo tree-buffer-help-echo-fn mouse-face highlight))))))
(setq ecb-tip-of-the-day nil)
(setq ecb-tree-buffer-style (quote image))
(setq ecb-tree-indent 4)
(setq ecb-windows-height 0.5)
(setq ecb-windows-width 0.15)
(setq semantic-load-turn-useful-things-on t)

;; Configuration variables here:
;; Load the semantic mode
(require 'semantic-util)
(setq semantic-load-turn-everything-on t)
(require 'semantic-load)

;; Load and activate ECB
(require 'ecb)
(ecb-activate)

;; Other GUI stuff
(scroll-bar-mode (quote right))
(setq mouse-wheel-scroll-amount (quote (1 ((shift) . 1) ((control)))))

;; Turn off that FUCKING toolbar crap:
(tool-bar-mode 0)

;; Show column number at bottom of screen
(column-number-mode 1)


;;;;;;;; Cursor stuff ;;;;;;;;

;; Set a narrow and blinking cursor
(blink-cursor-mode t)
(setq-default cursor-type '(bar . 2))

;;;;;;;; Colors stuff ;;;;;;;;

;; Set highlight line, and color
(setq hl-line-face 'highlight)
(global-hl-line-mode t)
(require 'color-theme)
(global-font-lock-mode t)


;;;;;;;; LaTeX Bonanza ;;;;;;;;

(require 'tex-site)
(setq reftex-plug-into-AUCTeX t)
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)

(setq-default TeX-master nil)
(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq reftex-enable-partial-scans t)
(setq reftex-save-parse-info t)
(setq reftex-use-multiple-selection-buffers t)
(setq reftex-include-file-commands '("include" "input" "includedoc"))
(setq reftex-vref-is-default t)
(setq TeX-electric-escape t)

;; Enable the latex preview
;;(require 'tex-site)
(load "preview-latex.el" nil t t)
(add-hook 'LaTeX-mode-hook 'LaTeX-install-toolbar)
(setq preview-auto-cache-preamble t)

;; Enable the TeXniscope stuff:
;;(setq dvi-previewer-program (if darwinp
;;                              "open -a TeXniscope"
;;                            "xdvi")) ;; causes error. don't need it anyway
(load (expand-file-name
       "/Users/thomas/Applications/TeXniscope.app/Contents/Resources/txs-search"))
(add-hook 'LaTeX-mode-hook (lambda ()
                             (local-set-key "\C-c\C-j" 'txs-jump-to-line)))
(setq txs-path "/Users/thomas/Applications/TeXniscope.app")

;; Enable cocoAspell to do spell checking
(setq ispell-program-name "/Users/thomas/Library/PreferencePanes/Spelling.prefPane/Contents/MacOS/cocoAspell")

;; Run auto-fill-mode when working with tex
(auto-fill-mode)

;; Generally neat for editing tables
(require 'table)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; MacOS X specific stuff (Macintosh)

;;;;;;;;;;;;;;;;
;; 8-bit characters in Mac!
(set-language-environment "Latin-1")
(set-keyboard-coding-system 'iso-latin-1)

;; Disabled due to Tiger bug
;(if (string= window-system "mac")
;    (setq Mac-keyboard-text-encoding kTextEncodingISOLatin1))

;(set-input-mode t nil '8bit)   ;; Input
(set-input-mode
 (car (current-input-mode))
 (nth 1 (current-input-mode))
 0
 7)

(setq mac-command-key-is-meta nil)
;; (global-set-key [(meta a)] '(lambda () (interactive) (insert-char "e")))
;; (global-set-key [(meta o)] '(lambda () (interactive) (insert-char "e")))
;; (global-set-key [(meta \')] '(lambda () (interactive) (insert-char "e")))

;; Define the return key to avoid problems on MacOS X
(define-key function-key-map [return] [13])

;; Functions for open functions
(defun open-finder-here ()
  (interactive)
  (shell-command "open ."))
(defun open-file-in-mac ()
  (interactive)
  (shell-command
   (concat "open " (buffer-file-name))))

;; Open terminal
(defun mac-open-terminal ()
   (interactive)
   (let ((dir ""))
     (cond
      ((and (local-variable-p 'dired-directory) dired-directory)
       (setq dir dired-directory))
      ((stringp (buffer-file-name))
       (setq dir (file-name-directory (buffer-file-name))))
      )
     (do-applescript
      (format "
 tell application \"Terminal\"
   activate
   try
     do script with command \"cd %s\"
   on error
     beep
   end try
 end tell" dir))
     ))

;; Set paths that don't get set if we launch from other than cli
(setenv "PATH"
     (concat (getenv "PATH")     
 ":")) ;; none extra needed at this time
 (setq exec-path (append exec-path
     '("/usr/libexec/emacs/22.0.50/powerpc-apple-darwin")))


;;;;;;;; Custom-set stuff ;;;;;;;;

(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.
 '(TeX-electric-escape t)
 '(exec-path (quote ("/usr/bin" "/bin" "/usr/sbin" "/sbin" "/Users/thomas" "/usr/libexec/emacs/21.3.50/powerpc-apple-darwin7.6.0" "/usr/local/bin")))
 '(show-paren-mode t nil (paren))
 '(standard-indent 4))
(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.
 '(ecb-default-highlight-face ((((class color) (background light)) (:background "#ccccff"))))
 '(ecb-mode-line-prefix-face ((((class color) (background light)) (:foreground "forestgreen"))))
 '(ecb-source-face ((((class color) (background light)) (:inherit ecb-default-highlight-face :background "cornflower blue" :foreground "yellow"))))
 '(ecb-tag-header-face ((((class color) (background light)) (:background "seagreen1"))))
 '(font-lock-keyword-face ((((class color) (background light)) (:foreground "blue" :weight bold))))
 '(highlight ((((class color) (background light)) (:background "#ffff99"))))
 '(highline-face ((t (:background "#ffff99"))))
 '(matlab-commands-keyword-face ((t (:foreground "blue"))))
 '(matlab-region-face ((t (:background "lightblue"))))
 '(matlab-types-keyword-face ((t (:foreground "purple" :weight bold))))
 '(region ((((class color) (min-colors 88) (background light)) (:background "azure3"))))
 '(show-paren-match-face ((((class color) (background light)) (:background "green"))))
 '(show-paren-mismatch-face ((((class color)) (:background "red" :foreground "white")))))