;;; init.el -*- lexical-binding: t; -*-
(defun chris/display-startup-time ()
  (message "Emacs loaded in %s with %d garbage collections."
           (format "%.2f seconds"
                 (float-time
                (time-subtract after-init-time before-init-time)))
         gcs-done))
(add-hook 'emacs-startup-hook #'chris/display-startup-time)

(setq inhibit-startup-message t)

(scroll-bar-mode -1)
(tool-bar-mode -1)
(tooltip-mode -1)
(set-fringe-mode 1)

(menu-bar-mode -1)
(blink-cursor-mode -1)
(column-number-mode +1)

(if (string-equal (system-name) "chris-linuxlaptop")
  (defvar chris/default-font-size 240)
(defvar chris/default-font-size 120))

(set-face-attribute 'default nil :font "VictorMono Nerd Font" :height chris/default-font-size)
(set-face-attribute 'fixed-pitch nil :font "VictorMono Nerd Font" :height chris/default-font-size)
(set-face-attribute 'variable-pitch nil :font "Cantarell" :height chris/default-font-size :weight 'regular)

(setq display-line-numbers-type 'relative)
(display-line-numbers-mode +1)
(global-visual-line-mode +1)

(setq doc-view-resolution 192)

(global-set-key (kbd "<escape>") 'keyboard-escape-quit)

(defvar bootstrap-version)
(let ((bootstrap-file
       (expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
      (bootstrap-version 5))
  (unless (file-exists-p bootstrap-file)
    (with-current-buffer
        (url-retrieve-synchronously
         "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
         'silent 'inhibit-cookies)
      (goto-char (point-max))
      (eval-print-last-sexp)))
  (load bootstrap-file nil 'nomessage))

(setq straight-use-package-by-default t)

(straight-use-package 'use-package)
(setq use-package-verbose t)

(use-package command-log-mode
  :commands command-log-mode)

(use-package all-the-icons)

(use-package doom-modeline
  :ensure t
  :init
  (doom-modeline-mode 1)
  (setq doom-modeline-height 35
        doom-modeline-bar-width 3
        all-the-icons-scale-factor 0.9))

(use-package doom-themes
  :ensure t
  :init (load-theme 'doom-snazzy t))

(use-package rainbow-delimiters
  :hook (prog-mode . rainbow-delimiters-mode))

(use-package adaptive-wrap
  :defer t)

(use-package which-key
  :config
  (setq which-key-idle-delay 0.3)
  (which-key-mode)
  :defer 1)

(let ((alist '((?! . "\\(?:!\\(?:==\\|[!=]\\)\\)")
               (?# . "\\(?:#\\(?:###?\\|_(\\|[!#(:=?[_{]\\)\\)")
               (?$ . "\\(?:\\$>\\)")
               (?& . "\\(?:&&&?\\)")
               (?* . "\\(?:\\*\\(?:\\*\\*\\|[/>]\\)\\)")
               (?+ . "\\(?:\\+\\(?:\\+\\+\\|[+>]\\)\\)")
               (?- . "\\(?:-\\(?:-[>-]\\|<<\\|>>\\|[<>|~-]\\)\\)")
               (?. . "\\(?:\\.\\(?:\\.[.<]\\|[.=?-]\\)\\)")
               (?/ . "\\(?:/\\(?:\\*\\*\\|//\\|==\\|[*/=>]\\)\\)")
               (?: . "\\(?::\\(?:::\\|\\?>\\|[:<-?]\\)\\)")
               (?\; . "\\(?:;;\\)")
               (?< . "\\(?:<\\(?:!--\\|\\$>\\|\\*>\\|\\+>\\|-[<>|]\\|/>\\|<[<=-]\\|=\\(?:=>\\|[<=>|]\\)\\||\\(?:||::=\\|[>|]\\)\\|~[>~]\\|[$*+/:<=>|~-]\\)\\)")
               (?= . "\\(?:=\\(?:!=\\|/=\\|:=\\|=[=>]\\|>>\\|[=>]\\)\\)")
               (?> . "\\(?:>\\(?:=>\\|>[=>-]\\|[]:=-]\\)\\)")
               (?? . "\\(?:\\?[.:=?]\\)")
               (?\[ . "\\(?:\\[\\(?:||]\\|[<|]\\)\\)")
               (?\ . "\\(?:\\\\/?\\)")
               (?\] . "\\(?:]#\\)")
               (?^ . "\\(?:\\^=\\)")
               (?_ . "\\(?:_\\(?:|?_\\)\\)")
               (?{ . "\\(?:{|\\)")
               (?| . "\\(?:|\\(?:->\\|=>\\||\\(?:|>\\|[=>-]\\)\\|[]=>|}-]\\)\\)")
               (?~ . "\\(?:~\\(?:~>\\|[=>@~-]\\)\\)"))))
  (dolist (char-regexp alist)
    (set-char-table-range composition-function-table (car char-regexp)
                          `([,(cdr char-regexp) 0 font-shape-gstring]))))

(use-package evil
  :init
  (setq evil-want-integration t
        evil-want-keybinding nil
        evil-want-C-i-jump nil
        evil-want-C-u-scroll t
        evil-respect-visual-line-mode t
        evil-want-C-u-delete t)
  :config
  (evil-mode +1)
  (setq evil-undo-system 'undo-tree))

(use-package evil-collection
  :after evil
  :config (evil-collection-init))

(use-package general
  :init
  (general-evil-setup)
  :config
  (general-create-definer chris/leader-keys
    :keymaps '(normal visual emacs)
    :prefix "SPC")
  (chris/leader-keys
   "b" '(:ignore t :which-key "buffer")
   "t" '(:ignore t :which-key "toggle")
   "f" '(:ignore t :which-key "file")
   "w" '(:ignore t :which-key "window")
   "s" '(:ignore t :which-key "search")
   "o" '(:ignore t :which-key "open")
   "h" '(:ignore t :which-key "help")
   "n" '(:ignore t :which-key "notes")
   "bs" '(consult-buffer :which-key "buffer search")
   "bd" '(kill-this-buffer :which-key "kill buffer")
   "tt" '(consult-theme :which-key "choose theme")
   "ff" '(find-file :which-key "find file")
   "fr" '(consult-recent-file :which-key "recent file")
   "fs" '(save-buffer :which-key "save")
   "hf" '(helpful-function :which-key "describe-function")
   "hv" '(helpful-variable :which-key "describe-variable")
   "hk" '(helpful-key :which-key "describe-key")
   "hi" '(info :which-key "info manual")
   "od" '(dired-jump :which-key "dired jump")
   "ss" '(consult-line :which-key "consult search")
   "ww" '(other-window :which-key "other window")
   "wd" '(delete-window :which-key "other window")
   ))

(use-package evil-escape
  :after evil
  :init (evil-escape-mode +1)
  :config
  (setq evil-escape-key-sequence "fd"
	evil-escape-delay 0.3))

(use-package undo-tree
  :defer 1
  :config
  (global-undo-tree-mode +1)
  :general
  (general-def 'normal undo-tree-visualize-mode-map
   "j" 'undo-tree-visualize-redo
   "k" 'undo-tree-visualize-undo))

(use-package olivetti
  :after org
  :config
  (setq olivetti-body-width 0.6
  olivetti-minimum-body-width 100))

(use-package toc-org
  :after org)

(use-package selectrum
  :init
  (selectrum-mode +1)
  :config
  (general-define-key
   :keymaps 'selectrum-minibuffer-map
   "C-j" 'selectrum-next-candidate
   "C-k" 'selectrum-previous-candidate
   "C-S-j" 'selectrum-goto-end
   "C-S-k" 'selectrum-goto-beginning
   "TAB" 'selectrum-insert-current-candidate)
  :commands completing-read)

(use-package prescient
  :config
  (prescient-persist-mode +1)
  :after selectrum)

(use-package selectrum-prescient
  :init
  (selectrum-prescient-mode +1)
  :after selectrum)

(use-package consult
  :after selectrum)

(use-package marginalia
  :bind (:map minibuffer-local-map
         ("C-M-a" . marginalia-cycle)
         ;; :map embark-general-map
         ;;     ("A" . marginalia-cycle)
         )

  ;; The :init configuration is always executed (Not lazy!)
  :init

  ;; Must be in the :init section of use-package such that the mode gets
  ;; enabled right away. Note that this forces loading the package.
  (marginalia-mode)

  ;; When using Selectrum, ensure that Selectrum is refreshed when cycling annotations.
  (advice-add #'marginalia-cycle :after
              (lambda () (when (bound-and-true-p selectrum-mode) (selectrum-exhibit))))

  ;; Prefer richer, more heavy, annotations over the lighter default variant.
  (setq marginalia-annotators '(marginalia-annotators-heavy marginalia-annotators-light nil))
  :after selectrum)

(use-package helpful
  :commands (helpful-callable helpful-variable helpful-command helpful-key))

(use-package format-all
  :config
  (format-all-mode +1)
  (setq format-all-formatters '("Emacs Lisp" emacs-lisp))
  :defer 1)

(chris/leader-keys
  "od" '(dired-jump :which-key "open dired here"))
(general-def 'normal dired-mode-map
  "h" 'dired-up-directory
  "l" 'dired-find-file
  "q" 'kill-this-buffer)

(defun chris/org-mode-setup ()
  (org-indent-mode +1)
  (toc-org-mode +1)
  (olivetti-mode +1))

(use-package org
    :config
    (setq org-startup-indented t
          org-edit-src-content-indentation 0)

    (add-hook 'org-mode-hook 'chris/org-mode-setup)

    (org-babel-do-load-languages
     'org-babel-load-languages
     '((emacs-lisp . t)
       (python . t)
       (shell . t)))

    (require 'org-tempo)
    (add-to-list 'org-structure-template-alist '("el" . "src emacs-lisp"))
    (add-to-list 'org-structure-template-alist '("py" . "src python"))
    (add-to-list 'org-structure-template-alist '("sh" . "src shell"))
    (add-to-list 'org-structure-template-alist '("q" . "quote"))

    (setq org-capture-templates
          '(("t" "Personal todo" entry
             (file+headline +org-capture-todo-file "Inbox")
             "* TODO %^{TODO name}\nSCHEDULED: %T\n%a\n%i%?" :prepend t)
            ("n" "Personal notes" entry
             (file+headline +org-capture-notes-file "Inbox")
             "* %u %?\n%i\n%a" :prepend t)
            ("j" "Journal" entry
             (file+olp+datetree +org-capture-journal-file)
             "* %U %?\n%i\n%a" :prepend t)
            ("p" "TFC Plan" entry
             (function chris/org-roam-capture-lesson-file)
             (file ".templates/tfcplantemplate.org")
             :prepend nil
             :jump-to-captured t
             :empty-lines 1)
            ("r" "Templates for projects")
            ("rt" "Project-local todo" entry
             (file+headline +org-capture-project-todo-file "Inbox")
             "* TODO %?\n%i\n%a" :prepend t)
            ("rn" "Project-local notes" entry
             (file+headline +org-capture-project-notes-file "Inbox")
             "* %U %?\n%i\n%a" :prepend t)
            ("rc" "Project-local changelog" entry
             (file+headline +org-capture-project-changelog-file "Unreleased")
             "* %U %?\n%i\n%a" :prepend t)
            ("o" "Centralized templates for projects")
            ("ot" "Project todo" entry #'+org-capture-central-project-todo-file
             "* TODO %?\n %i\n %a" :heading "Tasks" :prepend nil)
            ("on" "Project notes" entry #'+org-capture-central-project-notes-file
             "* %U %?\n %i\n %a" :heading "Notes" :prepend t)
            ("oc" "Project changelog" entry #'+org-capture-central-project-changelog-file
             "* %U %?\n %i\n %a" :heading "Changelog" :prepend t))
          org-capture-use-agenda-date t)

    ;;(setq org-superstar-headline-bullets-list '("◉" "◈" "▸" "✬" "◎" "◇" "❉" "✙" "❖"))
    (setq org-imenu-depth 4)
    (setq org-odt-styles-file "/home/chris/org/style.odt")

    (setq org-export-with-toc nil)
    (setq org-export-with-author nil)
    
    (setq org-todo-keywords
          '((sequence "TODO(t)" "PROJ(p)" "STRT(s)" "WAIT(w)" "HOLD(h)" "|" "DONE(d)" "CNCL(c)")
            (sequence "[ ](T)" "[-](S)" "[?](W)" "|" "[X](D)")))


    (setq org-agenda-files
          '("/home/chris/org/DMPREADME.org" "/home/chris/org/DMPTODO.org" "/home/chris/org/inbox.org" "/home/chris/org/notes.org" "/home/chris/org/repetition.org" "/home/chris/org/tasks.org" "/home/chris/org/tfc_plans.org" "/home/chris/org/ministry_team.org" "/home/chris/org/todo.org" "/home/chris/org/newsletter.org"))
    (setq org-id-method 'ts)
    :general
    (chris/leader-keys "o a" 'org-agenda
      "c" 'org-capture))

(defun chris/org-roam-capture-lesson-file ()
  "Function to return the lesson file that is needed for TFC plan capture and move to correct position for plan insertion"
  (interactive)
  (unless org-roam-mode (org-roam-mode))
  (let* ((completions (org-roam--get-title-path-completions))
         (title-with-tags (org-roam-completion--completing-read "Lesson: " completions))
         (res (cdr (assoc title-with-tags completions)))
         (file-path (plist-get res :path)))
    (find-file file-path)
    (goto-char (point-min))
    (search-forward "PLAN")))

(defun chris/org-babel-tangle-config ()
  (when (string-equal (buffer-file-name)
                      (expand-file-name "~/.dotemacs/README.org"))
    (let ((org-confirm-babel-evaluate nil))
      (org-babel-tangle))))

(add-hook 'org-mode-hook (lambda () (add-hook 'after-save-hook #'chris/org-babel-tangle-config
                                              :append :local)))

(use-package org-roam
  :after org
  :config
  (setq org-roam-directory "~/org")
  (setq org-roam-buffer-width 0.25)
  (setq org-roam-file-exclude-regexp ".stversion.*\|.stfolder.*\|.*~.*\|.*sync.*")
  (setq org-roam-db-location "~/.dotemacs/org-roam.db")
  (setq org-roam-capture-templates
        '(("d" "default" plain (function org-roam--capture-get-point)
           "%?"
           :file-name "${slug}"
           :head "#+TITLE: ${title}\n#+AUTHOR: Chris Cochrun\n#+CREATED: %<%D - %I:%M %p>\n\nj ")
          ("b" "bible" plain (function org-roam--capture-get-point)
           "%?"
           :file-name "${slug}"
           :head "#+TITLE: ${title}\n#+AUTHOR: Chris Cochrun\n#+CREATED: %<%D - %I:%M %p>\n- tags %^G\n\n* ")
          ("l" "TFC Lesson" plain (function org-roam--capture-get-point)
           (file ".templates/lessontemplate.org")
           :file-name "${slug}"
           :head "#+TITLE: ${title}\n#+AUTHOR: Chris Cochrun\n#+CREATED: %<%D - %I:%M %p>\n")))

  (setq org-roam-dailies-capture-templates
        '(("d" "daily" plain #'org-roam-capture--get-point ""
           :immediate-finish t
           :file-name "%<%Y-%m-%d>"
           :head "#+TITLE: %<%Y-%m-%d>\n#+AUTHOR: Chris Cochrun\n#+CREATED: %<%D - %I:%M %p>\n\n* HFL\n* Tasks\n* Family\n** How Do I Love Abbie?")
          ("b" "biblical daily" plain #'org-roam-capture--get-point ""
           :immediate-finish t
           :file-name "%<%Y-%m-%d>-bib"
           :head "#+TITLE: %<%Y-%m-%d> - Biblical\n#+AUTHOR: Chris Cochrun")))
  :general
  (chris/leader-keys
   "nf" '(org-roam-find-file :which-key "org roam ff")
   "nr" 'org-roam))

(use-package org-roam-server
  :config
  (setq org-roam-server-host "127.0.0.1"
        org-roam-server-port 8080
        org-roam-server-export-inline-images t
        org-roam-server-authenticate nil
        org-roam-server-serve-files t
        org-roam-server-network-label-truncate t
        org-roam-server-network-label-truncate-length 60
        org-roam-server-network-label-wrap-length 20)
  :after org-roam)

(add-hook 'org-roam-mode-hook org-roam-server-mode t)

(use-package magit
  :commands (magit-status magit-get-current-branch)
  :general
  (chris/leader-keys "g g" 'magit-status)
  :custom
  (magit-display-buffer-function #'magit-display-buffer-same-window-except-diff-v1))

(use-package pdf-tools
  :straight (:host github
		 :repo "flatwhatson/pdf-tools"
		 :branch "fix-macros")
  :defer 1
  :config
  (pdf-tools-install))

(setq gc-cons-threshold 2000000)