;; Initializations
(require 'package)

(let ((default-directory "~/.emacs-ii/emacs.d/"))
  (normal-top-level-add-subdirs-to-load-path))
(load-library "init-hooks")
(load-library "init-funcs")

(add-to-list 'package-archives '("melpa"           . "http://melpa.org/packages/") t)
(add-to-list 'package-archives '("gnu"             . "http://elpa.gnu.org/packages/"))
(add-to-list 'package-archives '("nongnu"          . "https://elpa.nongnu.org/nongnu/"))

(package-initialize)
(require 'auto-complete-config)
(require 'auto-complete)
(require 'plantuml-mode)
(require 'yasnippet)

(add-to-list 'auto-mode-alist '("\\.plantuml\\'"   . plantuml-mode))
(add-to-list 'auto-mode-alist '("\\.puml\\'"       . plantuml-mode))

(add-to-list 'auto-mode-alist '("\\.tex\\'"       . LaTeX-mode))

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

(autoload 'gfm-mode "markdown-mode"
   "Major mode for editing GitHub Flavored Markdown files" t)
(add-to-list 'auto-mode-alist '("README\\.md\\'" . gfm-mode))

(add-to-list 'ac-modes 'plantuml-mode)
(add-to-list 'ac-modes 'TeX-latex-mode)
(add-to-list 'ac-modes 'LaTeX-mode)
(add-to-list 'ac-modes 'tex-mode)
(add-to-list 'ac-modes 'org-mode)

;; Library functions
(global-display-line-numbers-mode 1)
(prefer-coding-system 'utf-8)

;; Key bindings
(global-set-key (kbd "C-c e")          'revert-cp1251)
(global-set-key (kbd "C-c r")          'revert-buf)
(global-set-key (kbd "C-c h")          'replace-string)
;(global-set-key (kbd "C-c u")          'untabify)
;(global-set-key (kbd "C-c s")          'remove-spaces)

(global-set-key (kbd "ESC M-o")        'org-mode)
(global-set-key (kbd "ESC M-i")        'whitespace-mode)
(global-set-key (kbd "ESC M-u")        'outline-mode)
(global-set-key (kbd "ESC M-t")        'text-mode)
(global-set-key (kbd "ESC M-h")        'hexl-mode)
(global-set-key (kbd "M-o M-a")        'org-agenda)

(global-set-key (kbd "C-c y")          'yas-describe-tables)	
(global-set-key (kbd "C-c w")          'toggle-word-wrap)
(global-set-key (kbd "C-c t")          'toggle-truncate-lines)
(global-set-key (kbd "C-c f")          'outline-toggle-children)
(global-set-key (kbd "C-c F")          'outline-show-all)
(global-set-key (kbd "C-c d")          'delete-trailing-whitespace)

(global-set-key (kbd "C->")            'next-buffer)
(global-set-key (kbd "C-<")            'previous-buffer)
(global-set-key (kbd "<M-up>")         'm-line-up)
(global-set-key (kbd "<M-down>")       'm-line-down)

;; Variables
(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-command-extra-options "--shell-escape")
 '(TeX-engine 'xetex)
 '(TeX-output-dir "build")
 '(ac-ignore-case 'smart)
 '(ac-use-fuzzy t)
 '(ansi-color-faces-vector
   [default default default italic underline success warning error])
 '(ansi-color-names-vector
   ["#212526" "#ff4b4b" "#b4fa70" "#fce94f" "#729fcf" "#e090d7" "#8cc4ff" "#eeeeec"])
 '(backup-directory-alist `(("." . "~/.saves")))
 '(calendar-date-style 'iso)
 '(calendar-mark-diary-entries-flag t)
 '(calendar-week-start-day 1)
 '(column-number-mode t)
 '(custom-enabled-themes '(deeper-blue))
 '(dired-recursive-deletes 'top)
 '(display-time-24hr-format t)
 '(doc-view-continuous t)
 '(erc-port 6660)
 '(erc-server "irc.rusnet.org.ru")
 '(european-calendar-style t)
 '(frame-title-format "GNU Emacs: %b" t)
 '(indent-tabs-mode nil)
 '(inhibit-startup-screen t)
 '(message-send-mail-function 'smtpmail-send-it)
 '(org-agenda-include-diary t)
 '(org-support-shift-select t)
 '(package-selected-packages
   '(dockerfile-mode markdown-mode+ markdown-mode magit alert-termux pomidor groovy-mode auctex auto-complete auto-complete-auctex auto-complete-c-headers company-irony company-irony-c-headers flycheck-irony irony irony-eldoc exec-path-from-shell go-autocomplete go-mode))
 '(plantuml-default-exec-mode 'jar)
 '(plantuml-jar-path "~/plantuml.jar")
 '(plantuml-output-type "png" t)
 '(show-paren-style 'expression)
 '(tab-width 4)
 '(verilog-auto-indent-on-newline t)
 '(verilog-auto-newline nil)
 '(verilog-indent-level 4)
 '(verilog-indent-level-behavioral 4)
 '(verilog-indent-level-declaration 4)
 '(verilog-indent-level-module 4)
 '(verilog-mode-hook '(verilog-set-compile-command))
 '(whitespace-line-column 999999999)
 '(word-wrap t)
 '(ispell-program-name "aspell")
 '(ispell-dictionary "ru-yeyo"))

; System
(delete-selection-mode t)
(size-indication-mode t)
(tool-bar-mode -1)
(set-input-method 'russian-computer) ;russian-mac
(flyspell-mode 1)

; Look and feel
(global-visual-line-mode t)
(blink-cursor-mode -1)
(cua-mode t)
(show-paren-mode t)
(electric-pair-mode)
(menu-bar-mode -1)
(when (eq system-type 'darwin)
  (menu-bar-mode 1))
(global-display-line-numbers-mode 1)

; Autocomplete
(global-auto-complete-mode t)
(ac-config-default)
(yas-global-mode 1)

; Diary
(display-time)
(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.
 )

(define-key yas-minor-mode-map (kbd "TAB") nil)
;; Bind `C-c s' to `yas-expand' ONLY.
(define-key yas-minor-mode-map (kbd "C-c s") #'yas-expand)

;; nomenclature for latex
(eval-after-load "tex"
  '(add-to-list 'TeX-command-list 
                '("Nomenclature" "makeindex build/%s.nlo -s nomencl.ist -o build/%s.nls"
                  (lambda (name command file)
                    (TeX-run-compile name command file)
                    (TeX-process-set-variable file 'TeX-command-next TeX-command-default))
                  nil t :help "Create nomenclature file")))

  ;; Helper for compilation. Close the compilation window if
  ;; there was no error at all.
  (defun compilation-exit-autoclose (status code msg)
    ;; If M-x compile exists with a 0
    (when (and (eq status 'exit) (zerop code))
      ;; then bury the *compilation* buffer, so that C-x b doesn't go there
      (bury-buffer)
      ;; and delete the *compilation* window
      (delete-window (get-buffer-window (get-buffer "*compilation*"))))
    ;; Always return the anticipated result of compilation-exit-message-function
    (cons msg code))
  ;; Specify my function (maybe I should have done a lambda function)
  (setq compilation-exit-message-function 'compilation-exit-autoclose)

