 
 (require 'package)
 
-(setq package-archives '(
-	("melpa" . "http://melpa.milkbox.net/packages/")
-	("org" . "http://orgmode.org/elpa/")
-	("gnu" . "http://elpa.gnu.org/packages/")))
+(setq package-archives '(("melpa" . "http://melpa.milkbox.net/packages/")
+                         ("org"   . "http://orgmode.org/elpa/")
+                         ("gnu"   . "http://elpa.gnu.org/packages/")))
 
 (package-initialize)
 
 (when (not package-archive-contents)
-	(package-refresh-contents))
+  (package-refresh-contents))
 
 (when (not (package-installed-p 'use-package ))
-	(package-install 'use-package))
+  (package-install 'use-package))
 
 (eval-when-compile
   (require 'use-package))
 (require 'diminish)                ;; if you use :diminish
 (require 'bind-key)                ;; if you use any :bind variant
-(setq use-package-always-ensure t)
-
-(setq use-package-minimum-reported-time 0.03
-		use-package-verbose t)
+(setq use-package-always-ensure t
+      use-package-minimum-reported-time 0.03
+      use-package-verbose t)
 
 ;; configure the chrome
 (set-default-font "DejaVu Sans Mono 9")


;; N.B. evil-mode must be enabled after global-evil-leader-mode
(use-package evil
	:init
		(setq evil-shift-width 3)
		(setq-default evil-symbol-word-search t)
		(setq evil-search-module 'evil-search)
	:config
		(use-package evil-args)
		(use-package evil-textobj-anyblock)
		(use-package evil-commentary
		  :config (evil-commentary-mode)
		  :diminish evil-commentary-mode)
		(use-package evil-leader
			:init
				(setq evil-leader/in-all-states 1)
			:config
				(global-evil-leader-mode t)
				(evil-leader/set-leader "<SPC>")
				(evil-leader/set-key
					";" 'evil-jump-forward
					"," 'evil-jump-backward
					"w" 'save-buffer
					"x" 'kill-buffer
					"e" 'pp-eval-last-sexp
					"v"  'exchange-point-and-mark
					"c" 'use-evil-clipboard-register
					"s" 'use-evil-selection-register
					"a" 'align
					)
			)  ; evil-leader
		(use-package powerline-evil
			:config
				(powerline-default-theme)
				(display-time-mode t)
			)
		; use SPC prefix before motion to see helpers
		(use-package evil-easymotion
			:config
			(evilem-default-keybindings "SPC"))
		(evil-set-initial-state 'git-rebase-mode 'emacs)
		(evil-set-initial-state 'deft-mode 'insert)
		(evil-set-initial-state 'magit-branch-manager-mode 'emacs)
		; note evil-set-initial-state didn't work for this mode
		(add-hook 'with-editor-mode-hook 'evil-insert-state)
		(define-key evil-inner-text-objects-map "a" 'evil-inner-arg)
		(define-key evil-outer-text-objects-map "a" 'evil-outer-arg)
		(define-key evil-inner-text-objects-map "b" 'evil-textobj-anyblock-inner-block)
		(define-key evil-outer-text-objects-map "b" 'evil-textobj-anyblock-a-block)
		(define-key evil-normal-state-map (kbd "j") 'evil-next-visual-line)
		(define-key evil-normal-state-map (kbd "k") 'evil-previous-visual-line)
		; buffer swapping, next/previous
		(define-key evil-normal-state-map (kbd "<RET>") 'switch-to-previous-buffer)
		; esc key (from WikEmacs)
		(define-key evil-normal-state-map [escape] 'keyboard-quit)
		(define-key evil-visual-state-map [escape] 'keyboard-quit)
		; scroll keeping cursor in place
		(define-key evil-normal-state-map (kbd "C-j")
			(lambda () (interactive)  (evil-scroll-line-down 1) (evil-next-visual-line 0)))
		(define-key evil-normal-state-map (kbd "C-k")
			(lambda () (interactive) (evil-scroll-line-up 1) (evil-previous-visual-line 0)))
		; Overload shifts so that they don't lose the selection
		(define-key evil-visual-state-map (kbd ">") 'me/evil-shift-right-visual)
		(define-key evil-visual-state-map (kbd "<") 'me/evil-shift-left-visual)
		(define-key evil-visual-state-map [tab] 'me/evil-shift-right-visual)
		(define-key evil-visual-state-map [S-tab] 'me/evil-shift-left-visual)
		(evil-mode 1)
	) ; evil


(use-package diminish
	:config
		(diminish 'undo-tree-mode)
	)

(use-package ws-butler
	:config
	(ws-butler-global-mode t)
	:defer 3
	)

(use-package shell-pop
	:init
		(setq shell-pop-internal-mode "ansi-term")
		(setq shell-pop-term-shell "/bin/bash")
		(setq shell-pop-window-size 40)
		(setq shell-pop-window-position "top")
		(setq shell-pop-universal-key "<f8>")
	:bind
		("<f8>" . shell-pop)
	)

(use-package guide-key
	:defer 3
	:init
	(setq guide-key/guide-key-sequence '(
		"<SPC>" "C-x" "C-h" "C-w" "C-c"
		"<f4>" "<f6>" "<f7>" "z" "g" ))
	:config
		(guide-key-mode 1)
	:diminish guide-key-mode)

(use-package magit
	:init
	:config
	; get out of magit blame mode
		(define-key magit-blame-mode-map (kbd "<f7> b") 'magit-blame-quit)
	; keep me accidently hitting k key due to evil habits
	; this should suffice until magit mapping is figured out
		(define-key magit-mode-map (kbd "<DEL>") 'magit-delete-thing)
		(define-key magit-mode-map "k" nil)
	:bind
		("<f7> g" . magit-status)
		("<f7> b" . magit-blame)
		("<f7> a" . magit-run-git-gui-blame)
	:defer t)

(display-time-mode t)                                       ; display time in mode-line

;; compilation
(setq compilation-scroll-output 'first-error
      compilation-auto-jump-to-first-error t)
(add-hook 'compilation-mode-hook
  (lambda ()
    (next-error-follow-minor-mode t)
    (local-set-key (kbd "k") 'compilation-previous-error)
    (local-set-key (kbd "j") 'compilation-next-error)
    (local-set-key (kbd "C-k") 'compilation-previous-file)
    (local-set-key (kbd "C-j") 'compilation-next-file)
    (local-set-key (kbd "<prior>") 'compilation-previous-error)
    (local-set-key (kbd "<next>") 'compilation-next-error)
    (local-set-key (kbd "<home>") 'compilation-previous-file)
    (local-set-key (kbd "<end>") 'compilation-next-file)))


(defun me:replace-prefix (prefix input)
  (replace-regexp-in-string ( concat "^" (regexp-quote prefix)) "" input))

(setq frame-title-format '((:eval (if (buffer-file-name)
                                      (me:replace-prefix (abbreviate-file-name default-directory) (abbreviate-file-name buffer-file-name))
                                    "%b"))
                           " %* ["
                           (:eval (abbreviate-file-name default-directory))
                           "]")
      icon-title-format frame-title-format)                 ; use same title for unselected frame

;; tame scrolling
(setq scroll-margin 5                                       ; leave 5 lines at top/bottom
      scroll-conservatively 100                             ; scroll # to bring point in view
      scroll-preserve-screen-position 'always               ; move cursor when scrolling
      mouse-wheel-scroll-amount '(3 ((shift) . 9))          ; 3 lines, or 9 line when shift held
      mouse-wheel-follow-mouse 't                           ; scroll window under mouse
      mouse-wheel-progressive-speed nil)                    ; don't speed up
(setq-default scroll-up-aggressively 0.01
              scroll-down-aggressively 0.01)

;; Operational preferences
(fset 'yes-or-no-p 'y-or-n-p)
