+;; Emacs configuration
+;; Marcel Kapfer (C) 2016 - 207
+;; MIT License
+
+;; -----------------------------------------------------------------------------
+
+;; Package configuration
+;;
+;; This configuration uses use-package, so we're first ensuring, that it is
+;; installed using the emacs package system an MELPA.
+
+(package-initialize)
+
+(require 'package)
+(setq package-enable-on-startup nil)
+
+(add-to-list 'package-archives
+	     '("melpa" . "https://melpa.milkbox.net/packages/"))
+
+;; intall use-package if not already installed
+(unless (package-installed-p 'use-package)
+  (package-refresh-contents)
+  (package-install 'use-package))
+
+;; tell use-package to always ensure
+(setq use-package-always-ensure t)
+
+;; -----------------------------------------------------------------------------
+
+;; Basic configuration
+;;
+;; The code in this block should be package independend.
+
+;; set name and password
 (setq user-full-name "Marcel Kapfer")
 (setq user-mail-address "me@mmk2410.org")
 
-(load "package")
-(package-initialize)
-(add-to-list 'package-archives
-	     '("marmalade" . "https://marmalade-repo.org/packages/"))
-(add-to-list 'package-archives
-	     '("melpa" . "https://melpa.milkbox.net/packages/"))
-(setq package-archive-enable-alist '(("melpa" deft magit)))
-
+;; y/n instead of yes/no for confirm questions
 (defalias 'yes-or-no-p 'y-or-n-p)
 
-;; Org mode settings
-(add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
-(global-set-key "\C-cl" 'org-store-link)
-(global-set-key "\C-ca" 'org-agenda)
-(global-font-lock-mode 1)
-
-;; Always show line numbers
-(global-nlinum-mode t)
-
-;; Highlight current line
+;; Save backup files in ~/.emacs-autosaves/
+(defvar user-temporary-file-directory "~/.emacs-autosaves")
+(make-directory user-temporary-file-directory t)
+(setq backup-by-copying t)
+(setq backup-directory-alist `(("." . ,user-temporary-file-directory)
+                               (tramp-file-name-regexp nil)))
+(setq auto-save-list-file-prefix (concat user-temporary-file-directory ".auto-saves-"))
+(setq auto-save-file-name-transforms `((".*" ,user-temporary-file-directory t)))
+
+;; set default input encoding
+(set-language-environment "UTF-8")
+
+;; disable startup screen
+(setq inhibit-startup-screen t)
+
+;; visual bell instead of BEEP
+(setq visible-bell 1)
+
+;; set text in scratch to nil
+(setq initial-scratch-message nil)
+
+;; Don't truncate lines
+(setq truncate-lines nil)
+
+;; Set default connection method for TRAMP
+(setq tramp-default-method "ssh")
+
+;; Maximize threshold for garbage collection to 10MB for less gc
+(setq gc-cons-treshold (* 10 1024 1024))
+
+;; confirm before closing emacs
+(setq confirm-kill-emacs #'y-or-n-p)
+
+;; activate winner mode
+(when (fboundp 'winner-mode)
+  (winner-mode 1))
+
+;; Highlight the current line
 (global-hl-line-mode t)
 
-;; Autocomplete
-(require 'auto-complete)
-(global-auto-complete-mode t)
-
-;; Save backup files in temporary directory
-(setq backup-directory-alist
-      `((".*" . ,temporary-file-directory)))
-(setq auto-save-file-name-transforms
-      `((".*" ,temporary-file-directory t)))
-
-;; Mutt support.
-(setq auto-mode-alist (append '(("/tmp/mutt.*" . mail-mode)) auto-mode-alist))
-
-(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.
- '(ansi-color-names-vector
-   ["#1B2229" "#ff6c6b" "#98be65" "#ECBE7B" "#51afef" "#c678dd" "#46D9FF" "#DFDFDF"])
- '(browse-url-browser-function (quote browse-url-firefox))
- '(csv-separators (quote (";")))
- '(custom-enabled-themes (quote (atom-one-dark)))
- '(custom-safe-themes
-   (quote
-    ("ff7625ad8aa2615eae96d6b4469fcc7d3d20b2e1ebc63b761a349bebbb9d23cb" "a4c9e536d86666d4494ef7f43c84807162d9bd29b0dfd39bdf2c3d845dcc7b2e" "e9460a84d876da407d9e6accf9ceba453e2f86f8b86076f37c08ad155de8223c" "d29231b2550e0d30b7d0d7fc54a7fb2aa7f47d1b110ee625c1a56b30fea3be0f" "c7a9a68bd07e38620a5508fef62ec079d274475c8f92d75ed0c33c45fbe306bc" "eb0a314ac9f75a2bf6ed53563b5d28b563eeba938f8433f6d1db781a47da1366" "a1289424bbc0e9f9877aa2c9a03c7dfd2835ea51d8781a0bf9e2415101f70a7e" "10e231624707d46f7b2059cc9280c332f7c7a530ebc17dba7e506df34c5332c4" "08b8807d23c290c840bbb14614a83878529359eaba1805618b3be7d61b0b0a32" "43bc55af3857f9e2dc14c4413739f36d758e4d75bcd9b67e9b7dc6d9fcc1db68" "6254372d3ffe543979f21c4a4179cd819b808e5dd0f1787e2a2a647f5759c1d1" "1160f5fc215738551fce39a67b2bcf312ed07ef3568d15d53c87baa4fd1f4d4e" default)))
- '(ecb-options-version "2.50")
- '(fci-rule-color "#5B6268")
- '(flycheck-python-pylint-executable "pylint3")
- '(inhibit-startup-screen t)
- '(jdee-global-classpath (quote ("/home/wilson/jdee-libs/")))
- '(jdee-server-dir "/opt/jdee-emacs-server/")
- '(org-agenda-files
-   (quote
-    ("~/Documents/uni/uulm-2017-2/priv/organisatorisches.org" "~/ownCloud/todo.org")))
- '(package-selected-packages
-   (quote
-    (avandu org company company-math counsel ivy beginend doom-themes neotree rainbow-delimiters projectile nlinum powerline airline-themes fic-mode markdown-preview-mode web-mode ob-dart ac-python ac-slime ant auctex-lua auto-compile auto-complete-auctex csv-mode csv ac-haskell-process ghc haskell-mode arduino-mode json-mode gruvbox-theme focus literate-coffee-mode jdee javadoc-lookup pkgbuild-mode vala-snippets vala-mode phpunit ac-php php-completion php+-mode fish-mode hugo mips-mode stumpwm-mode slime muttrc-mode diff-hl magit wanderlust ## auctex yaml-mode typescript sass-mode php-mode outlined-elisp-mode monokai-theme markdown-mode fill-column-indicator edit-server dracula-theme coffee-mode auto-complete atom-one-dark-theme atom-dark-theme)))
- '(pdf-latex-command "lualatex")
- '(pdf-view-incompatible-modes
-   (quote
-    (linum-mode linum-relative-mode helm-linum-relative-mode nlinum-hl-mode nlinum-relative-mode yalinum-mode)))
- '(safe-local-variable-values (quote ((TeX-engine . pdftex) (TeX-Engine . luatex))))
- '(send-mail-function (quote smtpmail-send-it))
- '(standard-indent 2)
- '(tool-bar-mode nil)
- '(web-mode-code-indent-offset 2)
- '(web-mode-indent-style 2))
- '(send-mail-function (quote smtpmail-send-it))
- '(tool-bar-mode nil)
-(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.
- '(default ((t (:inherit nil :stipple nil :background "#21242b" :foreground "#bbc2cf" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 80 :width normal :family "Fira Code")))))
-
-
-;; edit-server
-
-(require 'edit-server)
-(edit-server-start)
-
-
-
-; mmk2410 emacs configuration
-
-;; variabels
+;; replace selected text by typing
+(delete-selection-mode t)
+
+;; ibuffer configuration
+(setq ibuffer-saved-filter-groups
+      (quote (("default"
+               ("dired" (mode . dired-mode))
+               ("org" (name . "^.*org$"))
+               ("shell" (or (mode . eshell-mode) (mode . term-mode)))
+               ;; ("mu4e" (or
+               ;;          (mode . mu4e-compose-mode)
+               ;;          (name . "\*mu4e\*")
+               ;;          ))
+               ("programming" (or
+                               (mode . python-mode)
+                               (mode . sh-mode)
+                               (mode . web-mode)
+                               (mode . emacs-lisp-mode)
+                               (mode . lisp-mode)
+                               (mode . dart-mode)))
+               ("emacs" (or
+                         (name . "^\\*scratch\\*$")
+                         (name . "^\\*Messages\\*$")))
+               ))))
+(add-hook 'ibuffer-mode-hook
+          (lambda ()
+            (ibuffer-auto-mode 1)
+            (ibuffer-switch-to-saved-filter-groups "default")))
+
+;; Don't show filter groups if there are no buffers in that group
+(setq ibuffer-show-empty-filter-groups nil)
+
+;; Don't ask for confirmation to delete marked buffers
+(setq ibuffer-expert t)
+
+;; put a new line at the end of every file
+(setq require-final-newline t)
+
+;; delete trailing whitespaces on save
+(add-hook 'before-save-hook 'delete-trailing-whitespace)
+
+;; use german directory in ispell
+(setq ispell-dictionary "german-new8")
 
 ;;; always follow symlinks to git repos
 (setq vc-follow-symlinks t)

  (local-set-key (kbd "<C-return>") 'TeX-insert-comment-line))

;; Enable own latex-mode keybindings
(add-hook 'latex-mode 'my-latex-mode-keys)

;; use okular over synctex with auctex
;; (eval-after-load "tex"
;;                  '(setcar (cdr (assoc 'output-pdf TeX-view-program-selection)) "Okular"))

;; use pdf-tools over synctex with auctex
(setq TeX-view-program-selection '((output-pdf "PDF Tools"))
      TeX-source-correlate-start-server t)

;; Update PDF buffers after successful LaTeX runs
(add-hook 'TeX-after-TeX-LaTeX-command-finished-hook
           #'TeX-revert-document-buffer)

;; hooks

;;; hooks for outline mode
(add-hook 'LaTeX-mode-hook 'turn-on-outline-minor-mode)
(add-hook 'latex-mode-hook 'turn-on-outline-minor-mode)

;;; hook for fci-mode
(add-hook 'LaTeX-mode-hook 'fci-mode)

;;; hook for reftex
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)

;;; hook for autocomplete.
(add-hook 'TeX-mode-hook 'ac-latex-mode-setup)

;;; hook for auto fill mode
(add-hook 'TeX-mode-hook 'auto-fill-mode)

;;; hook for latex math mode
(add-hook 'LaTeX-mode-hook 'LaTeX-math-mode)

;;; hook for rainbox delimiters mode
(add-hook 'TeX-mode-hook #'rainbow-delimiters-mode)

; themes

;; enable bold and italic
(setq doom-themes-enable-bold t
      doom-themes-enable-italic t)

;;; enable doom one theme
(load-theme 'doom-one t)

;;; enable flashing mode-line on errors
(doom-themes-visual-bell-config)

;;; enable neotree theme
(doom-themes-neotree-config)

;;; ivy default configuration

;;; always enable ivy
(ivy-mode 1)

;;; ivy configuration
(setq ivy-use-virtual-buffers t)
(setq ivy-count-format "(%d/%d) ")

;;; ivy keys
(global-set-key (kbd "C-s") 'swiper)
(global-set-key (kbd "M-x") 'counsel-M-x)
(global-set-key (kbd "C-x C-f") 'counsel-find-file)
(global-set-key (kbd "<f1> f") 'counsel-describe-function)
(global-set-key (kbd "<f1> v") 'counsel-describe-variable)
(global-set-key (kbd "<f1> l") 'counsel-find-library)
(global-set-key (kbd "<f2> i") 'counsel-info-lookup-symbol)
(global-set-key (kbd "<f2> u") 'counsel-unicode-char)

(global-set-key (kbd "C-c g") 'counsel-git)
(global-set-key (kbd "C-c j") 'counsel-git-grep)
(global-set-key (kbd "C-c k") 'counsel-ag)
(global-set-key (kbd "C-x l") 'counsel-locate)
(global-set-key (kbd "C-S-o") 'counsel-rhythmbox)

(global-set-key (kbd "C-c C-r") 'ivy-resume)

;; global indent mode

(indent-guide-global-mode)

;; set default web browser
(setq browse-url-generic-program
      (substring (shell-command-to-string "gconftool-2 -g /desktop/gnome/url-handlers/https/command") 0 -4)
      browse-url-browser-function 'browse-url-generic)
;; enable pdf-tools
(pdf-tools-install)

;; reload pdf document after compliation
(add-hook 'TeX-after-compilation-finished-functions #'TeX-revert-document-buffer)


;;; set default column width
(setq-default fill-column 80)

;;; hide tool bar, menu bar and scroll bar
(menu-bar-mode 0)
(tool-bar-mode 0)
(scroll-bar-mode 0)

;;; indent using spaces, not tabs
(setq indent-tabs-mode nil)

;; tab width
(setq tab-width 2)

;; Connecting StumpWM with emacsclient
(add-hook 'after-init-hook 'server-start)
(setq server-raise-frame t)
(if window-system
    (add-hook 'server-done-hook
	      (lambda ()
		(shell-command
		 "stumpish 'eval (stumpwm::return-es-called-win stumpwm::*es-win*)'"))))

;; auto-fill mode
(add-hook 'mail-mode-hook 'auto-fill-mode)
(add-hook 'mail-mode-hook (lambda () (setq fill-column 72)))

;; set default web browser
(setq browse-url-generic-program
      (substring (shell-command-to-string "gconftool-2 -g /desktop/gnome/url-handlers/https/command") 0 -4)
      browse-url-browser-function 'browse-url-generic)

;; set font
(set-frame-font "Fira Code 8" nil t)

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

;; Custom keybindings
;;
;; here are custom keybindings for emacs or package functions configured. Custom
;; bindings for own functions are set directly after the function declaration.

;; global keys

;; Emacs Shell
(global-set-key (kbd "C-c e") 'eshell)

;; Async shell command
(global-set-key (kbd "C-c a") 'async-shell-command)

;; Open link under cursor
(global-set-key (kbd "C-c b") 'browse-url-at-point)

;; start ansi-term with fish shell
(global-set-key (kbd "C-c s") (lambda () (interactive) (ansi-term "/usr/bin/fish")))

;; eval region
(global-set-key (kbd "C-C x") 'eval-region)

;; revert buffer (reload file from disk)
(global-set-key (kbd "<f5>" ) 'revert-buffer)

;; use ibuffer as default
(global-set-key (kbd "C-x C-b") 'ibuffer)

;; toggle auto-fill-mode
(global-set-key (kbd "C-c q") 'auto-fill-mode)

;; package and mode specific keys

;; paste in term using C-x C-y
(eval-after-load "term" '(define-key term-raw-map (kbd "C-x C-y") 'term-paste))

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

;; Custom functions
;;
;; custom functions are defined here, sorted by use-case (if possible)

;; general

;; function for closing all buffers
(defun close-all-buffers ()
  "Close all buffers"
  (interactive)
  (mapc 'kill-buffer (buffer-list)))

;; shortcut for close-all-buffers
(global-set-key (kbd "C-c c") 'close-all-buffers)


;; LaTeX

;; helper for a new (La)TeX style guide concept.
(defun TeX-insert-comment-line ()
