(add-to-list 'default-frame-alist
             '(font . "Hack 11"))
(setq visible-bell 1)
(setq inhibit-startup-screen t)
(setq debug-on-error t)

;;(require 'init-security)

(require 'package)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
                    (not (gnutls-available-p))))
       (proto (if no-ssl "http" "https")))
  (when no-ssl (warn "\
Your version of Emacs does not support SSL connections,
which is unsafe because it allows man-in-the-middle attacks.
There are two things you can do about this warning:
1. Install an Emacs version that does support SSL and be safe.
2. Remove this warning from your init file so you won't see it again."))
  (add-to-list 'package-archives (cons "melpa" (concat proto "://melpa.org/packages/")) t)
  ;; Comment/uncomment this line to enable MELPA Stable if desired.  See `package-archive-priorities`
  ;; and `package-pinned-packages`. Most users will not need or want to do this.
  ;;(add-to-list 'package-archives (cons "melpa-stable" (concat proto "://stable.melpa.org/packages/")) t)
  )
(package-initialize)
(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.
 ;;'(custom-enabled-themes 'doom-solarized-dark)
 '(package-selected-packages
   (quote
    (nyan-mode neotree flycheck))))
;;(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.
;; )

;;(package-initialize)
;;(require 'cask "~/.cask/cask.el")
;;(cask-initialize)

(require 'rtags)
(require 'company-rtags)

(setq rtags-completions-enabled t)
(eval-after-load 'company
  '(add-to-list
    'company-backends 'company-rtags))
(setq rtags-autostart-diagnostics t)
(rtags-enable-standard-keybindings)

(add-hook 'c++-mode-hook 'irony-mode)
(add-hook 'c-mode-hook 'irony-mode)
(add-hook 'objc-mode-hook 'irony-mode)

(defun my-irony-mode-hook ()
  (define-key irony-mode-map [remap completion-at-point]
    'irony-completion-at-point-async)
  (define-key irony-mode-map [remap complete-symbol]
    'irony-completion-at-point-async))

(add-hook 'irony-mode-hook 'my-irony-mode-hook)
(add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)

(add-hook 'irony-mode-hook 'company-irony-setup-begin-commands)
(setq company-backends (delete 'company-semantic company-backends))
(eval-after-load 'company
  '(add-to-list
    'company-backends 'company-irony))
(setq company-idle-delay 0)
(define-key c-mode-map [(tab)] 'company-complete)
(define-key c++-mode-map [(tab)] 'company-complete)

(add-hook 'c++-mode-hook 'flycheck-mode)
(add-hook 'c-mode-hook 'flycheck-mode)
(require 'flycheck-rtags)

(defun my-flycheck-rtags-setup ()
  (flycheck-select-checker 'rtags)
  (setq-local flycheck-highlighting-mode nil) ;; RTags creates more accurate overlays.
  (setq-local flycheck-check-syntax-automatically nil))
;; c-mode-common-hook is also called by c++-mode
(add-hook 'c-mode-common-hook #'my-flycheck-rtags-setup)
;;Integrate to irony
(eval-after-load 'flycheck
  '(add-hook 'flycheck-mode-hook #'flycheck-irony-setup))

(eval-after-load 'flycheck
  '(add-hook 'flycheck-mode-hook #'flycheck-clang-tidy-setup))

(cmake-ide-setup)

(add-hook 'irony-mode-hook 'highlight-numbers-mode)
(add-hook 'irony-mode-hook 'preproc-font-lock-mode)

(require 'srefactor)
(require 'srefactor-lisp)

;; OPTIONAL: ADD IT ONLY IF YOU USE C/C++. 
(semantic-mode 1) ;; -> this is optional for Lisp

(define-key c-mode-map (kbd "M-RET") 'srefactor-refactor-at-point)
(define-key c++-mode-map (kbd "M-RET") 'srefactor-refactor-at-point)
(global-set-key (kbd "M-RET o") 'srefactor-lisp-one-line)
(global-set-key (kbd "M-RET m") 'srefactor-lisp-format-sexp)
(global-set-key (kbd "M-RET d") 'srefactor-lisp-format-defun)
(global-set-key (kbd "M-RET b") 'srefactor-lisp-format-buffer)

(load-file "~/.emacs.d/elpa/discord-emacs.el/discord-emacs.el")

(require 'smex) ; Not needed if you use package.el
  (smex-initialize) ; Can be omitted. This might cause a (minimal) delay
                    ; when Smex is auto-initialized on its first run.

(add-to-list 'load-path
              "~/.emacs.d/plugins/yasnippet")
(require 'yasnippet)
(yas-global-mode 1)

(add-hook 'c-mode-hook #'yas-minor-mode)
(add-hook 'c++-mode-hook #'yas-minor-mode)

(drag-stuff-mode t)

(require 'org-bullets)
(add-hook 'org-mode-hook (lambda () (org-bullets-mode 1)))

(nyan-mode)

;;(add-to-list 'load-path "/some/path/neotree")
(require 'neotree)
(global-set-key [f8] 'neotree-toggle)

(define-key c-mode-base-map (kbd "M-.") (function rtags-find-symbol-at-point))
(define-key c-mode-base-map (kbd "M-,") (function rtags-find-references-at-point))
(define-key c-mode-base-map (kbd "M-;") (function rtags-find-file))
(define-key c-mode-base-map (kbd "C-.") (function rtags-find-symbol))
(define-key c-mode-base-map (kbd "C-,") (function rtags-find-references))
(define-key c-mode-base-map (kbd "C-<") (function rtags-find-virtuals-at-point))
(define-key c-mode-base-map (kbd "M-i") (function rtags-imenu))

(global-set-key (kbd "C-c o") (lambda () (interactive)
  (find-file "/home/balazs/.emacs.d/conf.org")
  (message "Opened:  %s" (buffer-name))))
(global-set-key (kbd "C-c r") (lambda () (interactive)
   (kill-buffer (buffer-name))
   (load-file "/home/balazs/.emacs.d/init.el")
   (message "Configuration reloaded")  
))

(defun cmake-build-and-run ()
  "Asks for cmake target, then compiles and runs it."
  (interactive)
  (let ((target
	 (ido-completing-read "Choose build target: " (remove "" (split-string
								  (shell-command-to-string "cmake --build ./cmake-build-debug --target help | awk '{if($2 != \"all\" && $2!= \"clean\" && $2 != \"following\" && $2 != \"edit_cache\" && $2 != \"rebuild_cache\" && $2 != \"depend\" && $2 !~ /\\./){print $2}}'") "\n")))))
    (setq cmake-last-target target)
    (async-shell-command (format "cmake --build ./cmake-build-debug --target %s && ./cmake-build-debug/%s" target target))))

(defun cmake-build-last-target ()
   (interactive)
   (if (not (null cmake-last-target))
   (async-shell-command (format "cmake --build ./cmake-build-debug --target %s && ./cmake-build-debug/%s" cmake-last-target cmake-last-target))
   (cmake-build-and-run))
)
(define-key c-mode-base-map (kbd "C-c C-r") (function cmake-build-and-run))
(define-key c-mode-base-map [f5] 'cmake-build-last-target)

(defun indent-buffer ()
  (interactive)
  (save-excursion
    (indent-region (point-min) (point-max) nil)))
(global-set-key [f6] 'indent-buffer)



(define-key org-mode-map (kbd "M-p") 'org-latex-export-to-pdf)

(global-set-key (kbd "C-x g") 'magit-status)

(global-set-key (kbd "M-x") 'smex)
(global-set-key (kbd "M-X") 'smex-major-mode-commands)
;; This is your old M-x.
(global-set-key (kbd "C-c C-c M-x") 'execute-extended-command)



(setq minibuffer-completion-confirm nil)

;;(load-theme "humanoid-dark" t)
;;(add-hook 'after-init-hook (lambda () (load-theme "humanoid-dark")))

(setq org-todo-keywords
   '((squence "TODO" "WAITING" "|" "DONE(!)")))
(setq org-highlight-latex-and-related '(latex script entities))

(add-hook 'org-mode-hook '(lambda () (setq fill-column 80)))
(add-hook 'org-mode-hook 'auto-fill-mode)

;; Source: http://www.emacswiki.org/emacs-en/download/misc-cmds.el
(global-auto-revert-mode 1)
(setq org-format-latex-options (plist-put org-format-latex-options :scale 2.0))

(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq-default TeX-master nil)

(setq doc-view-continuous t)

(setq ispell-program-name "hunspell"
      ispell-local-dictionary "hu_HU"
      ispell-skip-html t
      ispell-local-dictionary-alist
      '(("hu_HU" "\[\[:alpha:\]\]" "[^[:alpha:]]" "[']" nil ("-d" "hu_HU") nil utf-8)))
(setq ispell-dictionary "hu_HU")

;(cd "~/CLionProjects/prog2/")
