;;----------------------------------------------------------------------------;;
;;--------------------------------= Joakimmj =--------------------------------;;
;;----------------------------------------------------------------------------;;

;;--------------------------------[ PACKAGES ]--------------------------------;;
(package-initialize)
(setq package-archives
      '(("gnu" . "http://elpa.gnu.org/packages/")
	("marmalade" . "http://marmalade-repo.org/packages/")
	("melpa" . "http://melpa.milkbox.net/packages/")))
;;----------------------------------------------------------------------------;;

;;-------------------------------[ COLOR-THEME ]------------------------------;;
(let (;(default-directory "~/.emacs.d/site-lisp")
      (default-directory (concat user-emacs-directory "site-lisp"))
      )
  (normal-top-level-add-to-load-path '("."))
  (normal-top-level-add-subdirs-to-load-path))

(require 'color-theme)
(color-theme-initialize)

(if window-system
    (require 'color-theme-tango)
)

;;FONT
;;(set-frame-font "inconsolata-9")

;;----------------------------------------------------------------------------;;

;;------------------------------[ KEY-BINDINGS ]------------------------------;;

;; JUMP TO 'bla'
(key-chord-mode 1)
(key-chord-define-global ";'" 'ace-jump-mode)

;; kills the active buffer, not asking what buffer to kill.
(global-set-key (kbd "C-x k") 'kill-this-buffer)

;;----------------------------------------------------------------------------;;

;;--------------------------------[ CLEANUP ]---------------------------------;;

;; defining a function that sets the right indentation to the marked
;; text, or the entire buffer if no text is selected.
(defun tidy ()
  "Ident, untabify and unwhitespacify current buffer, or region if active."
  (interactive)
  (let ((beg (if (region-active-p) (region-beginning) (point-min)))
	(end (if (region-active-p) (region-end)       (point-max))))
    (whitespace-cleanup)
    (indent-region beg end nil)
    (untabify beg end)))

;; bindes the tidy-function to C-TAB
(global-set-key (kbd "<C-tab>") 'tidy)

;;----------------------------------------------------------------------------;;

;;----------------------[ start eshell and bind to key ]----------------------;;

(global-set-key (kbd "C-x t")
		(lambda () (interactive)
		  (if (string= (buffer-name) "*eshell*")
		      (switch-to-buffer (second (buffer-list)))
		    (eshell))))

;;----------------------------------------------------------------------------;;

;;-----------------------------[ LOOKS AND FEELS ]----------------------------;;
;;REMOVE SPLASH-SCREEN
(setq inhibit-splash-screen t)

;;LINE-NUMB
(global-linum-mode 0)

;;PARENTESES
(show-paren-mode t)

;;YES-OR-NO -> Y-OR-N
(fset 'yes-or-no-p 'y-or-n-p)

;;OPEN ETC
(ido-mode t)

;;MARK TO OVERWRITE
(delete-selection-mode 1)

;;SHOW COLUMN-NUMB
(setq column-number-mode t)

;;BATTERY-MODE
;;(display-battery-mode t)

;;TOOL-BAR
(tool-bar-mode -1)

;;SCROLL-BAR
(scroll-bar-mode -1)

;;MENU-BAR
(menu-bar-mode -1)

;;CLOCK
(display-time-mode t)

;;SPLIT VERTIVALLY
(setq split-width-threshold 0
      split-height-threshold nil)

;;MARK COLUMN 80
(require 'fill-column-indicator)
(add-hook 'after-change-major-mode-hook
	  '(lambda ()
	     (setq fci-rule-column 80)
	     (fci-mode)))

;;----------------------------------------------------------------------------;;

;;-------------------------------[ AUTOSAVE ]---------------------------------;;
;;AUTOSAVE
(defvar emacs-autosave-directory (concat user-emacs-directory "autosaves/"))
(setq backup-directory-alist
      `((".*" . ,emacs-autosave-directory))
      auto-save-file-name-transforms
      `((".*" ,emacs-autosave-directory t)))
;;----------------------------------------------------------------------------;;

;;------------------------------[ Auto-complete ]-----------------------------;;

(require 'auto-complete-config)
(ac-config-default)

;;----------------------------------------------------------------------------;;

;;-----------------------------[ Resize buffers ]-----------------------------;;

(global-set-key (kbd "<M-left>")
		(lambda () (interactive)
		  (enlarge-window -1 t)))
(global-set-key (kbd "<M-right>")
		(lambda () (interactive)
		  (enlarge-window 1 t)))
(global-set-key (kbd "<M-up>")
		(lambda () (interactive)
		  (enlarge-window -1)))
(global-set-key (kbd "<M-down>")
		(lambda () (interactive)
		  (enlarge-window 1)))
;;----------------------------------------------------------------------------;;

;;----------------------------------------------------------------------------;;
;;----------------[ * Programming-language-modes specifics * ]----------------;;
;;----------------------------------------------------------------------------;;

;;----------------------------------[ Java ]----------------------------------;;

;; adding shortcuts to java-mode, writing the shortcut folowed by a
;; non-word character will cause an expansion.
(defun java-shortcuts ()
  (define-abbrev-table 'java-mode-abbrev-table
    '(("psv" "public static void main(String[] args) {" nil 0)
      ("sop" "System.out.printf" nil 0)
      ("sopl" "System.out.println" nil 0)))
  (abbrev-mode t))

;; the shortcuts are only useful in java-mode so we'll load them to
;; java-mode-hook.
(add-hook 'java-mode-hook 'java-shortcuts)

;; we want to set a default compile-command for java-mode, we make
;; this variable buffer-local so that changing it for java-mode does
;; not effect other modes
(make-variable-buffer-local 'compile-command)

;; defining a function that guesses a compile command and bindes the
;; compile-function to C-c C-c
(defun compile-java ()
  (setq compile-command (concat "javac " (buffer-name)))
  (local-set-key (kbd "C-c C-c") 'compile))

;; this is a java-spesific function, so we only load it when entering
;; java-mode
(add-hook 'java-mode-hook 'compile-java)

;;(autoload 'java-extras "java-extras" "Fold brackets")
;;(add-hook 'java-mode-hook 'java-extras)
;;TODO Fix this ugliness

;;----------------------------------------------------------------------------;;


;;------------------------------------[ C ]-----------------------------------;;

;; defining a function that sets more accessible keyboard-bindings to
;; hiding/showing code-blocs
(defun hideshow-on ()
  (local-set-key (kbd "C-c <right>") 'hs-show-block)
  (local-set-key (kbd "C-c C-<right>") 'hs-show-block)
  (local-set-key (kbd "C-c <left>")  'hs-hide-block)
  (local-set-key (kbd "C-c C-<left>")  'hs-hide-block)
  (local-set-key (kbd "C-c <up>")    'hs-hide-all)
  (local-set-key (kbd "C-c C-<up>")    'hs-hide-all)
  (local-set-key (kbd "C-c <down>")  'hs-show-all)
  (local-set-key (kbd "C-c C-<down>")  'hs-show-all)
  (hs-minor-mode t))

;; now we have to tell emacs where to load these functions. Showing
;; and hiding codeblocks could be useful for all c-like programming
;; (java is c-like) languages, so we add it to the c-mode-common-hook.
(add-hook 'c-mode-common-hook 'hideshow-on)

;;----------------------------------------------------------------------------;;


;;----------------------------------[ Lisp ]----------------------------------;;

;; Show λ in Lisp code.
(defun sm-lambda-mode-hook ()
  (font-lock-add-keywords
   nil `(("\\<lambda\\>"
	  (0 (progn (compose-region (match-beginning 0) (match-end 0)
				    ,(make-char 'greek-iso8859-7 107))
		    nil))))))
(dolist (h '(lisp-mode-hook
	     scheme-mode-hook
	     emacs-lisp-mode-hook
	     slime-repl-mode-hook
	     inferior-lisp-mode-hook
	     inferior-scheme-mode-hook
	     lisp-interaction-mode-hook))
  (progn
    (add-hook h (lambda () (paredit-mode 1)))
    (add-hook h 'sm-lambda-mode-hook)))

(when (file-exists-p "~/quicklisp/slime-helper.el")
  (load (expand-file-name "~/quicklisp/slime-helper.el")))

(setq inferior-lisp-program "sbcl")

;;----------------------------------------------------------------------------;;


;;---------------------------------[ Scheme ]---------------------------------;;

(require 'quack)

(autoload 'scheme-smart-complete "scheme-complete" nil t)
(eval-after-load 'scheme
  '(define-key scheme-mode-map "\t" 'scheme-complete-or-indent))
(autoload 'scheme-get-current-symbol-info "scheme-complete" nil t)
(add-hook 'scheme-mode-hook
	  (lambda ()
	    (make-local-variable 'eldoc-documentation-function)
	    (setq eldoc-documentation-function
		  'scheme-get-current-symbol-info)
	    (eldoc-mode)))

;;----------------------------------------------------------------------------;;