;;; $DOOMDIR/config.el -*- lexical-binding: t; -*-

;; Some functionality uses this to identify you
(setq user-full-name "{{ user_name }}"
      user-mail-address "{{ user_email }}")

;; Better default settings
(setq-default
 ;; Tab vs space
 indent-tabs-mode nil
 tab-width 4
 evil-shift-width 4
 standard-indent 4
 line-spacing 2
 ;; From @tecosaur
 delete-by-moving-to-trash t
 window-combination-resize t
 x-stretch-cursor t)
(setq undo-limit 80000000
      evil-want-fine-undo t
      auto-save-default t
      truncate-string-ellipsis "…")
(global-subword-mode 1) ;; Use CamelCase words

;; Font settings
(setq doom-font (font-spec :family "Iosevka" :size 18 :weight 'semi-light)
      doom-serif-font (font-spec :family "Iosevka Slab" :size 18 :weight 'semi-light)
      doom-variable-pitch-font (font-spec :family "IBM Plex Sans" :size 18)
      doom-big-font (font-spec :family "Iosevka" :size 26 :weight 'semi-light))
(after! unicode-fonts
  (setq doom-unicode-font doom-font)
  (dolist (unicode-block '("CJK Compatibility"
                           "CJK Compatibility Forms"
                           "CJK Compatibility Ideographs"
                           "CJK Compatibility Ideographs Supplement"
                           "CJK Radicals Supplement"
                           "CJK Strokes"
                           "CJK Symbols and Punctuation"
                           "CJK Unified Ideographs"
                           "CJK Unified Ideographs Extension A"
                           "CJK Unified Ideographs Extension B"
                           "CJK Unified Ideographs Extension C"
                           "CJK Unified Ideographs Extension D"
                           "CJK Unified Ideographs Extension E"))
      (push "{{ cjk_font }}" (cadr (assoc unicode-block unicode-fonts-block-font-mapping)))))
;;(setq inhibit-compacting-font-caches t)
(custom-set-faces!
  '(font-lock-comment-face :slant italic)
  '(font-lock-keyword-face :weight bold))

;; Japanese input method
;; (use-package! mozc
;;   :config
;;   (setq default-input-method "japanese-mozc"))
(after! doom-modeline
  (setq doom-modeline-major-mode-icon t
        doom-modeline-major-mode-color-icon t
        doom-modeline-buffer-encoding t
        doom-modeline-enable-word-count t
        doom-modeline-unicode-fallback t))
(custom-set-faces!
  '(doom-modeline-buffer-modified :foreground "orange"))

;; Window split position
(setq evil-vsplit-window-right t
      evil-split-window-below t)

;; Emoji
;; (after! emojify
;;   (setq emojify-display-style 'unicode))

{% if theme == 'nord' %}
(setq doom-theme 'doom-nord)
{% elif theme == 'onedark' %}
(setq doom-theme 'doom-one)
{% endif %}
(setq doom-themes-enable-bold t
      doom-themes-enable-italic t)
(after! treemacs
  (setq doom-themes-treemacs-theme 'doom-colors
        doom-themes-treemacs-enable-variable-pitch nil))

;; Pick an image for dashboard
(defun random-choice (items)
      (let* ((size (length items))
            (index (random size)))
      (nth index items)))
(defvar kawaii (random-choice (delete "."
      (delete ".." (directory-files (expand-file-name "images" doom-private-dir))))))
(setq +doom-dashboard-banner-file kawaii
      +doom-dashboard-banner-dir (expand-file-name "images" doom-private-dir)
      +doom-dashboard-banner-padding '(0 . 2)
      +doom-dashboard-functions '(doom-dashboard-widget-banner
                                  doom-dashboard-widget-shortmenu
                                  doom-dashboard-widget-loaded))

(setq display-line-numbers-type 'relative)
(setq global-hl-line-modes nil)

;; Git-gutter
(after! git-gutter
  (setq +vc-gutter-default-style nil))

;; Indent
(after! highlight-indent-guides
  (setq highlight-indent-guides-method 'character
        highlight-indent-guides-responsive t))

;; Use ranger.el instead of default dired
(after! ranger
  (setq ranger-parent-depth 0
        ranger-cleanup-eagerly t
        ranger-show-hidden t
        ranger-max-preview-size 20
        ranger-dont-show-binary t))

;; Projectile
(after! projectile
  (setq projectile-project-search-path '("~/Code/")
        projectile-auto-discover nil))

(use-package! all-the-icons-ivy-rich
  :defer-incrementally counsel-projectile
  :init (all-the-icons-ivy-rich-mode 1)
  :config
  (setq all-the-icons-ivy-rich-icon-size 1.0))
(use-package! ivy-rich
  :after all-the-icons-ivy-rich)

;; Company
(after! company
  (setq company-idle-delay 0.5
        company-minimum-prefix-length 2
        company-show-numbers nil))

;; lsp
(use-package! lsp-treemacs
  :after (treemacs lsp)
  :config
  (lsp-treemacs-sync-mode 1))
(setq lsp-clients-clangd-args '("-j=2"
                                "--background-index"
                                "--clang-tidy"
                                "--completion-style=detailed"
                                "--pch-storage=memory"
                                "--header-insertion=iwyu"
                                "--header-insertion-decorators"))
(after! lsp-clangd (set-lsp-priority! 'clangd 2))  ;; Prefer clangd than the default ccls

;; Quicker which-key
(after! which-key
  (setq which-key-idle-delay 0.5))

(use-package! parinfer-rust-mode
  :init
  (setq parinfer-rust-library (concat doom-etc-dir "parinfer-rust/target/release/libparinfer_rust.so")
        parinfer-rust-auto-download nil))

(setq org-directory "~/Documents/Org/")
(after! org
  (setq org-default-notes-file "~/Documents/Org/notes.org"
        org-agenda-files '("~/Documents/Org/agenda/agenda.org" "~/Documents/Org/agenda/archive.org")
        org-confirm-babel-evaluate nil
        org-ellipsis "▾"
        org-log-done t
        org-log-into-drawser t
        org-insert-heading-respect-content nil
        org-hide-emphasis-markers t)
  (add-to-list 'org-modules 'ol-info)
  ;; Org-noter
  (setq org-noter-notes-search-path '("~/Documents/Org/noter/"))
  ;; Org-journal
  (setq org-journal-dir "~/Documents/Org/journal/"))
(defun follie/comfy-org-editing ()
  (setq visual-fill-column-width 100
        visual-fill-column-center-text t)
  (visual-fill-column-mode 1)
  (display-line-numbers-mode -1))
(add-hook 'org-mode-hook 'follie/comfy-org-editing)
(add-hook! org-mode (electric-indent-local-mode -1))
(defun follie/org-toggle-emphasis-markers ()
  "Toggle emphasis markers in an Org buffer"
  (interactive)
  (if org-hide-emphasis-markers
      (setq org-hide-emphasis-markers nil)
    (setq org-hide-emphasis-markers t))
  (org-mode-restart))
(map! :map org-mode-map
      :localleader :desc "org-toggle-emphasis-markers"
      "z" #'follie/org-toggle-emphasis-markers)

;; Org-tree-slide
(defun follie/presentation-start ()
  (text-scale-set 2)
  (hide-mode-line-mode 1))
(defun follie/presentation-stop ()
  (text-scale-set 0)
  (hide-mode-line-mode -1))
(use-package! org-tree-slide
  :commands org-tree-slide-mode
  :config
  (setq org-tree-slide-skip-outline-level 0
        org-image-actual-width nil
        org-tree-slide-heading-emphasis t
        org-tree-slide-modeline-display nil
        org-tree-slide-header t
        org-tree-slide-slide-in-effect t
        org-tree-slide-breadcrumbs " ▶ "
        org-tree-slide-activate-message "Presentation starto..."
        org-tree-slide-deactivate-message "Thanks for listening!")
  (add-hook! 'org-tree-slide-mode-hook #'evil-normalize-keymaps)
  (add-hook! 'org-tree-slide-play-hook #'follie/presentation-start)
  (add-hook! 'org-tree-slide-stop-hook #'follie/presentation-stop))

;; Latex
(setq +latex-viewers '(pdf-tools))

;; Spell checker
(after! flyspell
  (setq flyspell-lazy-idle-seconds 2))

;; Magit
(after! magit
  (setq magit-diff-refine-hunk 'all))

;; Keycast
(use-package! keycast
  :commands keycast-mode
  :config
  (define-minor-mode keycast-mode
    "Show current command and its key binding in the mode line."
    :global t
    (if keycast-mode
        (progn
          (add-hook 'pre-command-hook 'keycast--update t)
          (add-to-list 'global-mode-string '("" mode-line-keycast " ")))
      (remove-hook 'pre-command-hook 'keycast--update)
      (setq global-mode-string (remove '("" mode-line-keycast " ") global-mode-string))))
  (custom-set-faces!
    '(keycast-command :inherit doom-modeline-debug
                      :height 0.9)
    '(keycast-key :inherit custom-modified
                  :height 1.1
                  :weight bold)))
