(require 'iso-transl)

;; Packages
(require 'package) ;; You might already have this line
(add-to-list 'package-archives
             '("melpa" . "http://melpa.org/packages/"))
(add-to-list 'package-archives
             '("melpa" . "http://melpa.milkbox.net/packages/"))
(when (< emacs-major-version 24)
  ;; For important compatibility libraries like cl-lib
  (add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/")))
(package-initialize) ;; You might already have this line

;; Encoding
(set-language-environment "UTF-8")
(delete-selection-mode 1)

;; Indentation mode
(setq-default indent-tabs-mode nil)
(setq inhibit-startup-screen t)
(setq tab-width 4)

;; Auto complete

(yas-global-mode 1)

(ac-config-default)

(require 'auto-complete-clang-async)
(add-to-list 'ac-modes 'latex-mode)   ; make auto-complete aware of `latex-mode`
(require 'ac-math)

(defun ac-cc-mode-setup ()
  (require 'auto-complete-c-headers)
  (setq ac-clang-complete-executable "~/.emacs.d/clang-complete")
  (setq ac-clang-cflags (list "-std=c++11" "-I../inc"))
  (setq ac-sources '(ac-source-clang-async))
  (add-to-list 'ac-sources 'ac-source-c-headers)
  (ac-clang-launch-completion-process)
  )

(defun ac-latex-mode-setup () ; add ac-sources to default ac-sources
  (setq ac-sources
        (append '(ac-source-math-unicode
                  ac-source-math-latex
                  ac-source-latex-commands)
                ac-sources))
  )

(defun my-ac-config ()
  (add-hook 'c++-mode-common-hook 'ac-cc-mode-setup)
  (add-hook 'c-mode-common-hook 'ac-cc-mode-setup)
  (add-hook 'LaTeX-mode-hook 'ac-latex-mode-setup)
  (add-hook 'auto-complete-mode-hook 'ac-common-setup)
  (global-auto-complete-mode t))

(my-ac-config)

(setq ac-math-unicode-in-math-p t)
(add-to-list 'ac-modes 'org-mode)
(ac-flyspell-workaround)

;; Latex
(require 'tex-mik)
(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq-default TeX-master nil)

(add-hook 'LaTeX-mode-hook 'visual-line-mode)
(add-hook 'LaTeX-mode-hook 'flyspell-mode)
(add-hook 'LaTeX-mode-hook 'LaTeX-math-mode)

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

(require 'tex)
(TeX-global-PDF-mode t)

;; C style
(setq c-default-style "k&r")
(setq c-basic-offset 4)

;; CSS style
(setq css-indent-offset 2)

;; No menu, scrollbar, display line and column
(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)
(column-number-mode t)
(line-number-mode t)

;; Markdown mode
(autoload 'markdown-mode "markdown-mode"
   "Major mode for editing Markdown files" t)
(add-to-list 'auto-mode-alist '("\\.text\\'" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.markdown\\'" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))

;; White space mode
(setq whitespace-line-column 100)
(add-hook 'python-mode-hook (lambda () (setq whitespace-line-column 140)))

(global-whitespace-mode 1)

;; Electric pair mode
(electric-pair-mode 1)
(show-paren-mode 1)
(setq show-paren-delay 0)

;; R
(require 'ess-site)

;; Run C programs directly from within emacs
(setq execute-command nil)
(setq compile-command nil)
(setq execute-buffer-name "*output*")
(setq execute-process-name "execute")

(defun set-execute-command ()
  (interactive)
  (setq sp (split-string (read-from-minibuffer "Execute command: ")))
  (setq execute-command (combine-and-quote-strings (cons (file-truename (car sp)) (cdr sp)))))

(defun kill-execute-c-program ()
  (interactive)
  (if (get-process execute-process-name)
      (delete-process (get-process execute-process-name))))

(defun execute-c-program ()
  (interactive)
  (if (not execute-command) (set-execute-command))
  (kill-execute-c-program)
  (setq old-buffer (current-buffer))
  (setq output-buffer (get-buffer-create execute-buffer-name))
  (switch-to-buffer-other-window output-buffer)
  (end-of-buffer)
  (insert (concat (propertize execute-command 'face 'bold) "\n"))
  (switch-to-buffer-other-window old-buffer)
  (start-process-shell-command execute-process-name execute-buffer-name execute-command)
  )

(defun set-compile-command ()
  (interactive)
  (setq compile-command (read-from-minibuffer "Compile command: ")))

(defun compile-c-program ()
  (interactive)
  (if (not compile-command) (set-compile-command))
  (compile compile-command))

(defun key-c-mode-setup ()
  (local-set-key [f1] 'compile-c-program)
  (local-set-key [f2] 'execute-c-program)
  (local-set-key [f3] 'kill-execute-c-program)
  (local-set-key [f4] 'set-execute-command)
  (local-set-key [f5] 'set-compile-command))

(add-hook 'c++-mode-common-hook 'key-c-mode-setup)
(add-hook 'c-mode-common-hook 'key-c-mode-setup)

;; Flyspell dictionarie
(defun fd-switch-dictionary ()
  (interactive)
  (let* ((dic ispell-current-dictionary)
         (change (if (string= dic "francais") "english" "francais")))
    (ispell-change-dictionary change)
    (message "Dictionary switched from %s to %s" dic change)
    ))

(defun key-latex-mode-setup ()
  (local-set-key [f8] 'fd-switch-dictionary))

(add-hook 'LaTeX-mode-hook 'key-latex-mode-setup)

;; CUSTOM EMACS
(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.
 '(safe-local-variable-values (quote ((encoding . utf-8)))))
(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.
 )
