;;; Use-package

;; Use package and melpa as source
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/"))
(setq package-enable-at-startup nil)
(package-initialize)

;; use-package
(setq-default
 use-package-always-ensure t
 use-package-expand-minimally t
 use-package-verbose nil
 use-package-enable-imenu-support t)

(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))
(eval-when-compile (require 'use-package))

;;; Custom and options
;;;; Custom file

(setq custom-file "~/dotfiles/.emacs.d/custom.el")
(load custom-file)

;;;; Options

;;;;; Mode-line

(setq
 ;; Mnemonics for eol
 eol-mnemonic-unix "(Unix)"
 ;; Display total size of buffer and column index
 size-indication-mode t
 ;; Column numbers
 column-number-mode t
 ;; Column numbers from 1
 column-number-indicator-zero-based nil)

;;;;; Backup

(setq
 ;; delete excess backup versions silently
 delete-old-versions t
 ;; make backup files
 make-backup-files t
 ;; Use version control on backups
 version-control t
 ;; don't make backups file when in version controlled dir
 vc-make-backup-files nil
 ;; which directory to put backups file
 backup-directory-alist `(("." . "~/.emacs.d/backups")))

;;;;; Autosave

(setq
 ;; Enable
 auto-save-default t
 ;; Set interval and timeout
 auto-save-interval 300
 auto-save-timeout 30
 ;; Set directory for auto-saves
 auto-save-file-name-transforms '((".*" "~/.emacs.d/auto-save/" t)))

;;;;; Scrolling

(setq
 scroll-margin 10
 hscroll-margin 10
 scroll-conservatively 100000
 scroll-preserve-screen-position 1
 hscroll-step 1
 maximum-scroll-margin 0.5)

;;;;; Window placement

(setq
 display-buffer-alist
 `(("*R Dired"
    (display-buffer-reuse-window display-buffer-in-side-window)
    (side . right)
    (slot . -1)
    (window-width . 0.2)
    (reusable-frames . nil))
   ("*R:"
    (display-buffer-reuse-window display-buffer-at-bottom)
    (window-height . 0.3)
    (reusable-frames . nil))
   ("\\*Help\\[R"
    (display-buffer-reuse-window display-buffer-in-side-window)
    (side . right)
    (slot . 2)
    (window-width . 0.4)
    (reusable-frames . nil))
   ("*Help*"
    (display-buffer-reuse-window display-buffer-at-bottom)
    (slot . 1)
    (window-height . 0.3)
    (reusable-frames . nil))
   ("magit-diff"
    (display-buffer-reuse-window display-buffer-in-side-window)
    (side . left)
    (slot . 1)
    (window-width . 0.4)
    (reusable-frames . nil))
   ("magit"
    (display-buffer-reuse-window display-buffer-in-side-window)
    (side . left)
    (window-width . 0.4)
    (reusable-frames . nil))))

;;;;; Disable GUI

(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)
(blink-cursor-mode -1)
(tooltip-mode -1)

(setq
 inhibit-startup-screen t
 inhibit-startup-message t
 inhibit-startup-echo-area-message t)

;;;;; UTF8

(setq
  coding-system-for-read    'utf-8
  coding-system-for-write   'utf-8)

;; Prefer UTF-8
(prefer-coding-system       'utf-8)
(set-default-coding-systems 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)

;;;;; Line numbers

;; Show line numbers
(global-display-line-numbers-mode t)

 ;; Show absolute line numbers in narrowed state
(setq-default display-line-numbers-widen t)
  ;; No limit to line number displayed
(setq line-number-display-limit nil)

;;;;; Set proxy at work
(if (string-equal (system-name) "hegp")
  (setq url-proxy-services '(("no_proxy" . "127.0.0.1")
                             ("http" . "proxym-inter.aphp.fr:8080")
                             ("https" . "proxym-inter.aphp.fr:8080")))
  ())

;;;;; Misc

 (setq
  ;; Kill debug windows after use
  debugger-bury-or-kill 'kill
  ;; Set tramp terminal type
  tramp-terminal-type "tramp"
  ;; Scroll smoothly with mouse
  mouse-wheel-scroll-amount '(2 ((shift) . 1) ((control)))
  ;; Indent two spaces
  standard-indent 2
  ;; don't ask for confirmation when opening symlinked file
  vc-follow-symlinks t
  ;; silent bell when you make a mistake
  ring-bell-function 'ignore
  ;; Identity
  user-full-name "Maxime Wack"
  user-mail-address "maximewack@free.fr"
  ;; GC and file size limits
  gc-cons-threshold 50000000
  large-file-warning-threshold 100000000
  ;; Filename in title
  frame-title-format
  '((:eval (if (buffer-file-name)
               (abbreviate-file-name (buffer-file-name))
             "%b")))
  )

(setq-default
;; Show empty lines at the end of the buffer
 indicate-empty-lines t
 ;; Spaces for indent
 tab-width 2
 ;; Don't use tabs in indentation
 indent-tabs-mode nil)

;; Set transparency
(set-frame-parameter (selected-frame) 'alpha '(85 . 90))

;; Font
(set-frame-font "Fira Code 10" nil t)

;; Replace yes/no with y/n
(fset 'yes-or-no-p 'y-or-n-p)

;; Disable package-selected-packages
(defun package--save-selected-packages (&rest opt) nil)

;;; Packages
;;;; Keys
;;;;; General

(use-package general
  :demand
  :config
  (general-create-definer spcleader
   :states '(normal visual insert emacs)
   :prefix "SPC"
   :non-normal-prefix "C-S-SPC")

;;;;;; Spcleader

  (spcleader
   "RET" 'eval-defun
   "."   'eval-expression
   "k"   '((lambda () (interactive) (kill-buffer nil)) :which-key "kill buffer")
   "K"   'kill-some-buffers
   "$"   '((lambda () (interactive) (find-file "~/.emacs")) :which-key "open config")
   "#"   '((lambda () (interactive) (load-file "~/.emacs")) :which-key "reload config")
   "l"   'reposition-window
   "W"   '(count-words-region :which-key "word count")
   "w"   'toggle-truncate-lines)

;;;;;; Narrow

   (spcleader
     "n"  '(:ignore t :which-key "Narrow")
     "nr" 'narrow-to-region
     "np" 'narrow-to-page
     "nf" 'narrow-to-defun
     "nc" 'polymode-toggle-chunk-narrowing
     "nw" 'widen))

;;;;; Hydra

(use-package hydra)

;;;;; Evil
;;;;;; Evil

(use-package evil
  :demand
  :config
  (defhydra hydra-window (:color red :timeout 1)
    "Windows"
    ("t" evil-window-left)
    ("T" evil-window-move-far-left)
    ("s" evil-window-down)
    ("S" evil-window-move-very-bottom)
    ("d" evil-window-up)
    ("D" evil-window-move-very-top)
    ("r" evil-window-right)
    ("R" evil-window-move-far-right)
    ("+" (evil-window-increase-height 4))
    ("-" (evil-window-decrease-height 4))
    ("/" (evil-window-decrease-width 4))
    ("*" (evil-window-increase-width 4))
    ("h" evil-window-split)
    ("v" evil-window-vsplit)
    ("c" evil-window-delete)
    ("o" delete-other-windows)
    ("é" nil "Cancel"))

  :general
  (:states 'visual
   "l" 'evil-delete
   "H" 'evil-change
   "J" 'evil-join)

  (:states '(visual normal motion emacs)
   "é"   'hydra-window/body)

  (:states '(normal motion)
   "t"   'evil-backward-char
   "s"   'evil-next-visual-line
   "d"   'evil-previous-visual-line
   "r"   'evil-forward-char
   "T"   'evil-window-top
   "S"   'evil-scroll-down
   "D"   'evil-scroll-up
   "R"   'evil-window-bottom
   "l"   'evil-delete
   "L"   'evil-delete-line
   "h"   'evil-replace
   "H"   'evil-replace-state
   "gs"  'evil-next-line
   "gd"  'evil-previous-line
   "M"   'evil-window-middle
   "j"   'evil-find-char-to
   ";"   'evil-repeat-find-char-reverse
   ","   'evil-repeat-find-char
   "C-a" 'evil-numbers/inc-at-pt
   "C-x" 'evil-numbers/dec-at-pt
   "C-s" 'evil-scroll-line-down
   "C-d" 'evil-scroll-line-up
   "C-o" 'evil-jump-backward)

  (:states 'insert
   "C-r" 'counsel-yank-pop
   "C-u" 'counsel-unicode-char)

  :config
  (evil-mode t)

  (setq evil-want-integration nil
        evil-want-keybinding nil
        evil-want-Y-yank-to-eol t
        ;; Colored cursors based on evil-state
        evil-emacs-state-cursor    '("red" box)
        evil-normal-state-cursor   '("green" box)
        evil-visual-state-cursor   '("orange" box)
        evil-insert-state-cursor   '("red" bar)
        evil-replace-state-cursor  '("red" bar)
        evil-operator-state-cursor '("red" hollow)
        ;; use NORMAL mode/state EVERYWHERE by default
        evil-default-state 'normal
        evil-motion-state-modes nil
        evil-normal-state-modes nil
        evil-insert-state-modes nil
        ;; Use emacs state in these selected modes
        evil-emacs-state-modes nil
        ;; and don't let modes override the INSERT state (!)
        evil-overriding-maps nil
        evil-intercept-maps nil))

;;;;;; Evil-numbers

(use-package evil-numbers)

;;;;;; Evil-escape

(use-package evil-escape
  :config
  (evil-escape-mode t)
  (setq-default evil-escape-key-sequence "ii"
                evil-escape-delay 0.2))

;;;;;; Evil-commentary

(use-package evil-commentary
  :demand

  :config
  (evil-commentary-mode t)

  :general
  (:states 'normal
   "gcc" 'evil-commentary-line))

;;;;;; Evil-surround

(use-package evil-surround
  :config
  (global-evil-surround-mode))

;;;;;; Evil-lion

(use-package evil-lion
  :general
  (spcleader
    "a" 'evil-lion-left
    "A" 'evil-lion-right))

;;;;; Which-key

(use-package which-key
  :config
  (which-key-mode t)
  (setq which-key-allow-evil-operators t))

;;;; Basics
;;;;; Built-in
;;;;;; Package

(use-package package
  :general
  (:keymaps 'package-menu-mode-map
   :states 'normal
   "q" 'quit-window))

;;;;;; Custom

(use-package cus-edit
  :ensure nil
  :config
  (add-to-list 'evil-emacs-state-modes 'Custom-mode))

;;;;;; Ediff

(use-package ediff
  :config
  (add-to-list 'evil-emacs-state-modes 'ediff-mode))

;;;;;; Debugger

(use-package debug
  :demand
  :config
  (add-to-list 'evil-emacs-state-modes 'debugger-mode))

;;;;;; Paren

(use-package paren
  :config
  (show-paren-mode t)
  (setq show-paren-delay 0))

;;;;;; Flyspell

(use-package flyspell
  :hook (prog-mode . flyspell-prog-mode)
  :general
  (:states 'insert
    "C-s" 'flyspell-check-previous-highlighted-word)
  (spcleader
    "s"  '(:ignore t :which-key "Flyspell")
    "ss" 'flyspell-goto-next-error
    "sd" 'flyspell-check-previous-highlighted-word
    "sc" 'ispell-word
    "sf" '((lambda () (interactive) (ispell-change-dictionary "francais") (flyspell-buffer)) :which-key "Switch to French")
    "sa" '((lambda () (interactive) (ispell-change-dictionary "american") (flyspell-buffer)) :which-key "Switch to American")
    ))

;;;;;; Tar-mode

(use-package tar-mode
  :general
  (:keymaps 'tar-mode-map
   :states 'normal
   "RET" 'tar-extract-other-window))

;;;;;; Whitespace

(use-package whitespace
  :config
  (setq whitespace-style
        '(face
          tabs
          spaces
          trailing
          empty
          space-mark
          tab-mark
          ))
  (global-whitespace-mode t)
  :hook (before-save . whitespace-cleanup))

;;;;;; Info

(use-package info
  :general
  (:keymaps 'Info-mode-map
   :states 'normal
   "ð"  'Info-backward-node
   "ß"  'Info-forward-node
   "Þ"  'Info-up
   "þ"  'Info-up
   "q"  'Info-exit
   "ẞ"  'Info-next
   "Ð"  'Info-prev
   "gr" 'Info-history-forward
   "gt" 'Info-history-back
   "s"  'evil-scroll-line-down
   "d"  'evil-scroll-line-up))

;;;;;; Highlight line

(use-package hl-line
  :config
  (global-hl-line-mode t)
  :custom-face
  (hl-line ((t (:background "gray10")))))

;;;;;; Simple

(use-package simple
  :ensure nil
  :config
  (global-visual-line-mode t)
  :custom-face
  (italic ((t (:family "Fira Code Italic"
               :foundry "Fira Code Italic")))))

;;;;;; Revert

(use-package autorevert
  :config
  (global-auto-revert-mode t))

;;;;;; Elisp

(use-package elisp-mode
  :ensure nil
  :config
  ;; No size limit to output in elisp mode
  (setq eval-expression-print-length nil)
  :general
  (:keymaps 'emacs-lisp-mode-map
   :states 'normal
   "RET" 'eval-defun)
  (:keymaps 'emacs-lisp-mode-map
   :states 'visual
   "RET" '(lambda (start end)
            (interactive "r")
            (eval-region start end t))))

;;;;;; Help

(use-package help-mode
  :ensure nil
  :general
   (spcleader
   "h"  '(:ignore t :which-key "Help")
   "hh" 'help-for-help
   "ha" 'apropos-command
   "hb" 'describe-bindings
   "hv" 'counsel-describe-variable
   "hf" 'counsel-describe-function
   "hF" 'Info-goto-emacs-command-node
   "ho" 'describe-symbol
   "hO" 'info-lookup-symbol
   "hm" 'describe-mode
   "hp" 'finder-by-keyword
   "hP" 'describe-package
   "hk" 'describe-key
   "hK" 'Info-goto-emacs-key-command-node
   "hi" 'info
   "hL" 'describe-language-environment)

  (:keymaps 'help-mode-map
   :states 'normal
   "TAB" 'forward-button
   "q"   'quit-window
   "P"   'help-go-back
   "N"   'help-go-forward))

;;;;;; Markdown

(use-package markdown-mode
  :custom-face
  (markdown-header-face-1 ((t (:inherit 'outline-1))))
  (markdown-header-face-2 ((t (:inherit 'outline-2))))
  (markdown-header-face-3 ((t (:inherit 'outline-3))))
  (markdown-header-face-4 ((t (:inherit 'outline-4))))
  (markdown-header-face-5 ((t (:inherit 'outline-5))))
  (markdown-bold-face ((t (:foreground "#ede0ce"))))
  (markdown-italic-face ((t (:foreground "#ede0ce"))))
  (markdown-header-delimiter-face ((t (:foreground "gray30")))))

;;;;;; Eshell

;; eshell-mode-map is not ppoulated outside of eshell
;; So we need to define bindings in a hook
(defun maxx/eshell-keys ()
  (evil-define-key*
   'insert
   eshell-mode-map
   "ð" 'eshell-previous-matching-input-from-input
   "ß" 'eshell-next-matching-input-from-input
   "þ" 'eshell-backward-argument
   "®" 'eshell-forward-argument
   "Þ" 'eshell-kill-input
   "Ð" 'eshell-previous-prompt
   "ẞ" 'eshell-next-prompt
   "C-l" 'eshell/clear)
  )

(use-package eshell
  :config
  (add-to-list 'evil-insert-state-modes 'eshell-mode)
  :hook (eshell-first-time-mode . maxx/eshell-keys))

;;;;;; Zoom

(use-package face-remap
  :config
  (defhydra hydra-zoom (:color red)
    "Zoom"
    ("+" text-scale-increase)
    ("-" text-scale-decrease)
    ("/" (text-scale-adjust 0) "Reset"))

  :general
  (:states 'normal
   "C-+" 'hydra-zoom/text-scale-increase
   "C--" 'hydra-zoom/text-scale-decrease
   "C-/" '(lambda () (interactive) (text-scale-adjust 0))))

;;;;; Undo-tree

(use-package undo-tree
  :demand
  :config
  (global-undo-tree-mode)
  (setq undo-tree-auto-save-history t
        undo-tree-history-directory-alist '(("." . "~/.emacs.d/undo"))
        undo-tree-enable-undo-in-region nil)
  :general
  (:keymaps 'undo-tree-visualizer-mode-map
   :states 'normal
   "q"     'undo-tree-visualizer-quit
   "SPC u" 'undo-tree-visualizer-quit
   "d"     'undo-tree-visualize-undo
   "s"     'undo-tree-visualize-redo
   "D"     'undo-tree-visualizer-toggle-diff)
  (spcleader
   "u"     'undo-tree-visualize))

;;;;; Avy

(use-package avy
  :config
  (setq avy-keys '(?t ?s ?r ?n)
        avy-background t
        avy-highlight-first t)
  :custom-face
  (avy-lead-face ((t (:background nil :foreground "red1"))))
  (avy-lead-face-0 ((t (:background nil :foreground "gray50"))))
  (avy-lead-face-1 ((t (:background nil :foreground "gray50"))))
  (avy-lead-face-2 ((t (:background nil :foreground "gray50"))))
  :general
  (spcleader
    "SPC" '(avy-goto-char-timer :which-key "Avy")))

;;;;; Ace-link

(use-package ace-link
  :general
  (:keymaps 'Info-mode-map
   :states 'normal
   "f" 'ace-link-info)
  (:keymaps 'help-mode-map
   :states 'normal
   "f" 'ace-link-help))

;;;;; Doom
;;;;;; Doom theme

(use-package doom-themes
  :config
  (load-theme 'doom-peacock t)
  (setq doom-themes-enable-bold t
        doom-themes-enable-italic t)
  (doom-themes-visual-bell-config))

;;;;;; Doom modeline

(use-package doom-modeline
  :config
  (setq doom-modeline-enable-word-count t)
  ;; first time run M-x all-the-icons-install-fonts
  :hook (after-init . doom-modeline-mode))

;;;;; Expand region

(use-package expand-region
  :config
  (defhydra hydra-expand-region (:color red)
    "Expand-region"
    ("m" er/expand-region)
    ("M" er/contract-region))
  :general
  (spcleader
    "m" 'hydra-expand-region/er/expand-region
    "M" 'hydra-expand-region/er/contract-region))

;;;;; Autopairs

(use-package autopair
  :config
  (autopair-global-mode))

;;;;; Minions

(use-package minions
  :config
  (minions-mode t)
  (setq minions-mode-line-lighter ""))

;;;; Completion
;;;;; Ivy
;;;;;; Ivy

(use-package ivy
  :config
  (setq ivy-wrap t
        ivy-height 15
        ivy-use-virtual-buffers t
        ivy-count-format "(%d/%d) "
        ivy-extra-directories nil
        ivy-initial-inputs-alist nil)
  :general
  (:keymaps '(ivy-mode-map ivy-minibuffer-map)
   "ð" 'ivy-previous-line
   "ß" 'ivy-next-line
   "ẞ" '(lambda ()
          (interactive)
          (scroll-other-window 1))
   "Ð" '(lambda ()
          (interactive)
          (scroll-other-window -1))
   "®" 'ivy-done
   "þ" '(lambda ()
            (interactive)
            (kill-buffer (ivy-state-current ivy-last)))
   "_" 'maxx/counsel-describe))

;;;;;; Ivy-posframe

(use-package ivy-posframe
  :config
  (setq ivy-posframe-display-functions-alist '((t . ivy-posframe-display-at-frame-top-center)))
  (when (display-graphic-p) (ivy-posframe-enable)))

;;;;;; Counsel

(use-package counsel
  :general
  ("M-x" 'counsel-M-x)
  (spcleader
   "i"   'counsel-imenu
   "f"   'counsel-find-file
   "b"   'counsel-switch-buffer
   "x"   'counsel-M-x
   "/"   'counsel-grep-or-swiper))

(defun maxx/counsel-describe ()
  "Display help for object at point in counsel"
  (interactive)
  (let ((inhibit-message t)
        (object (intern (ivy-state-current ivy-last))))
    (describe-symbol object)))

;;;;; Company

(use-package company
  :demand
  :config
  (global-company-mode t)
  (setq company-idle-delay 0
        company-minimum-prefix-length 2)
  :general
  (:keymaps 'company-active-map
   "ß" 'company-select-next
   "ð" 'company-select-previous
   "Ð" 'company-previous-page
   "ẞ" 'company-next-page
   "þ" 'company-abort
   "®" 'company-complete
   "_" 'company-show-doc-buffer))

;;;; Projectile
;;;;; Projectile

(use-package projectile
  :config
  (setq projectile-project-search-path '("~/Projects")))

;;;;; Counsel-projectile

(use-package counsel-projectile
  :general
  (spcleader
    "p"     '(:ignore t :which-key "Projectile")
    "pf"    'counsel-projectile-find-file
    "pb"    'counsel-projectile-switch-to-buffer
    "pp"    'counsel-projectile-switch-project
    "pg"    'counsel-projectile-grep
    "p SPC" 'counsel-projectile))

;;;; ESS
;;;;; ESS

(use-package ess
  :config
  (setq ess-ask-for-ess-directory nil)
  :general
  (:keymaps 'inferior-ess-r-mode-map
   :states 'insert
   "ð"   'comint-previous-matching-input-from-input
   "ß"   'comint-next-matching-input-from-input)
  (:keymaps 'inferior-ess-r-mode-map
   :states '(insert normal)
   "C-d" 'evil-scroll-line-up
   "C-s" 'evil-scroll-line-down)
  )

;;;;; Poly-R

(use-package poly-R
  :general
  (:keymaps '(markdown-mode-map
              ess-mode-map)
   :states 'normal
   "<tab>" 'markdown-cycle
   "ð"     'markdown-previous-visible-heading
   "ß"     'markdown-next-visible-heading
   "Ð"     'markdown-outline-previous-same-level
   "ẞ"     'markdown-outline-next-same-level
   "þ"     'markdown-hide-subtree
   "®"     '(lambda () (interactive)
              (markdown-show-children)
              (markdown-show-entry))
   "Þ"     'markdown-up-heading
   "M-r"   'markdown-demote
   "M-t"   'markdown-promote
   "M-d"   'markdown-move-up
   "M-s"   'markdown-move-down))

;;;; Git
;;;;; Magit

(use-package magit
  :config
  (add-to-list 'evil-emacs-state-modes 'magit-mode)
  (add-to-list 'evil-emacs-state-modes 'git-rebase-mode)
  :general
  (:keymaps '(magit-hunk-section-map
              magit-file-section-map
              magit-staged-section-map
              magit-unstaged-section-map
              magit-revision-mode-map
              magit-section-map
              magit-log-mode-map
              magit-diff-mode-map
              magit-untracked-section-map
              magit-status-mode-map
              magit-stash-section-map)
            "ð" 'magit-section-backward
            "ß" 'magit-section-forward
            "þ" 'magit-section-hide
            "®" 'magit-section-show
            "s" 'magit-next-line
            "d" 'magit-previous-line
            "q" 'quit-window)
  (:keymaps '(magit-hunk-section-map
              magit-status-mode-map
              magit-unstaged-section-map
              magit-untracked-section-map)
            "+" 'magit-stage
            "-" 'magit-unstage)
  (spcleader
   "g"  '(:ignore t :which-key "Magit")
   "gd" 'magit-file-dispatch
   "gs" 'magit-status))

;;;;; Smerge

(use-package smerge-mode
  :general
  (:keymaps 'smerge-mode-map
   :states 'normal
   "RET" 'smerge-keep-current))

;;;;; Git-gutter-fringe

(use-package git-gutter-fringe
  :config
  (global-git-gutter-mode t))

;;;; Slime

(use-package slime
  :config
  (setq inferior-lisp-program "clisp"))

;;;; Flycheck

(use-package flycheck
  :config
  (global-flycheck-mode))

;;;; Org
;;;;; Org

(use-package org
  :demand
  :config
  (org-babel-do-load-languages
   'org-babel-load-languages
   '((python . t)
     (R . t)
     (emacs-lisp . t)))
  (setq org-directory "~/Private/Org"
        org-agenda-files (directory-files-recursively "~/Private/Org/" "org$")
        org-default-notes-file "~/Private/Org/agenda.org"
        org-capture-templates
        '(("t"
           "todo"
           entry (file+headline org-default-notes-file"Tasks")
           "* TODO %^{todo}\n %? \n %a")
          ("n"
           "note"
           entry (file+olp+datetree org-default-notes-file"Notes")
           "* %^{title}\n %?"))
        org-deadline-warning-days 7
        org-startup-folded t
        org-startup-with-inline-images t
        org-agenda-span 'fortnight
        org-agenda-skip-scheduled-if-deadline-is-shown t
        org-agenda-skip-deadline-prewarning-if-scheduled 'pre-scheduled
        org-agenda-todo-ignore-deadlines 'all
        org-agenda-todo-ignore-scheduled 'all
        org-agenda-sorting-strategy
        '((agenda deadline-up priority-down)
          (todo priority-down category-keep)
          (tags priority-down category-keep)
          (search category-keep))
        org-list-demote-modify-bullet '(("+" . "-") ("-" . "+") ("*" . "+"))
        org-hide-block-startup t
        org-link-abbrev-alist
        '(("gh" . "https://github.com/")
          ("git" . "https://git.maximewack.com/"))
        )
  :custom-face
  (outline-1 ((t (:height 1.9))))
  (outline-2 ((t (:height 1.6))))
  (outline-3 ((t (:height 1.4))))
  (outline-4 ((t (:height 1.2))))
  (outline-5 ((t (:height 1.1))))

  :general
  (spcleader
   "o"     '(:ignore t :which-key "Org")
   "oo"    '((lambda () (interactive) (find-file org-default-notes-file)) :which-key "Open org file")
   "oa"    'org-agenda
   "oc"    'org-capture
   "ol"    'org-insert-link
   "oL"    'org-store-link)
  (:keymaps 'org-mode-map
   :states 'normal
   "<RET>" 'org-open-at-point
   "M-s"   'org-metadown
   "M-d"   'org-metaup
   "M-r"   'org-metaright
   "M-t"   'org-metaleft
   "M-S"   'org-shiftmetadown
   "M-D"   'org-shiftmetaup
   "M-R"   'org-shiftmetaright
   "M-T"   'org-shiftmetaleft
   "C-s"   'org-shiftdown
   "C-d"   'org-shiftup
   "C-r"   'org-shiftright
   "C-t"   'org-shiftleft
   "Þ"     'outline-up-heading
   "Ð"     'outline-backward-same-level
   "ẞ"     'outline-forward-same-level
   "®"     'org-show-children
   "þ"     'outline-hide-subtree
   "ð"     'org-previous-visible-heading
   "ß"     'org-next-visible-heading))

;;;;; Org-bullets

(use-package org-bullets
  :config
  (setq org-hide-leading-stars t)
  :hook (org-mode . org-bullets-mode))

;;;;; Outshine

(use-package outshine
  :general
  (:keymaps 'outshine-mode-map
   :states 'normal
   "<tab>"   'outshine-cycle
   "S-<tab>" 'outshine-cycle-buffer
   "ð"       'outline-previous-visible-heading
   "ß"       'outline-next-visible-heading
   "Ð"       'outline-backward-same-level
   "ẞ"       'outline-forward-same-level
   "Þ"       'outline-up-heading
   "þ"       'outshine-kbd-M-<left>
   "®"       'outshine-kbd-M-<right>
   "M-t"     'outshine-kbd-M-S-<left>
   "M-r"     'outshine-kbd-M-S-<right>
   "M-d"     'outshine-kbd-M-S-<up>
   "M-s"     'outshine-kbd-M-S-<down>
   )
  :hook (emacs-lisp-mode . outshine-mode))
