;; kkk-evil.el --- Evil config                      -*- lexical-binding: t; -*-

;; Copyright (C) 2023  Vinicius Teshima <vini.tes@pm.me>

;; Author: Vinicius Teshima <vini.tes@pm.me>

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Evil Config

;;; Code:

(defun kkk-evil--yank-pop ()
  "A wrapper on `yank-pop' because evil fucks with it."
  (interactive)
  (yank-pop))

(use-package evil
  :demand t
  :functions (evil-mode evil-set-initial-state evil-define-key
	      evil-beginning-of-line evil-end-of-line evil-normal-state)
  :defines (evil-want-integration evil-want-keybinding evil-want-Y-yank-to-eol
	    evil-want-C-d-scroll evil-move-beyond-eol evil-split-window-below
	    evil-vsplit-window-below evil-want-fine-undo evil-want-C-i-jump
	    evil-emacs-state-modes)
  :init
  (setq evil-want-integration t
	evil-want-Y-yank-to-eol t
	evil-want-keybinding nil)
  (evil-mode t)
  :config
  (dolist (mode '(custom-mode eshell-mode vterm-mode dired-mode
		  notmuch-search-mode notmuch-hello-mode xref--xref-buffer-mode
		  notmuch-show-mode term-mode Info-mode))
    (add-to-list 'evil-emacs-state-modes mode))

  (setq evil-move-beyond-eol t
	evil-want-fine-undo t)

  (evil-set-initial-state 'messages-buffer-mode 'normal)
  (evil-set-initial-state 'dashboard-mode 'normal)

  (evil-define-key '(normal insert visual)
    'global (kbd "C-a") #'evil-beginning-of-line)
  (evil-define-key '(normal insert visual)
    'global (kbd "C-e") #'evil-end-of-line)

  (evil-define-key 'insert 'global (kbd "C-g") #'evil-normal-state)

  (evil-define-key '(emacs normal insert visual)
    'global (kbd "C-w") #'kill-region)
  (evil-define-key '(emacs normal insert visual)
    'global (kbd "C-y") #'yank)
  (evil-define-key '(emacs normal insert visual)
    'global (kbd "M-y") #'kkk-evil--yank-pop)
  )

(use-package evil-surround
  :defer 5
  :after (evil)
  :functions (global-evil-surround-mode)
  :init (global-evil-surround-mode t))

(use-package evil-numbers
  :defer 6
  :after evil
  :functions (evil-numbers/inc-at-pt evil-numbers/dec-at-pt
	      evil-numbers/inc-at-pt-incremental
	      evil-numbers/dec-at-pt-incremental)
  :config
  (let ((modes '(normal visual)))
    (evil-define-key modes 'global (kbd "C-c +") #'evil-numbers/inc-at-pt)
    (evil-define-key modes 'global (kbd "C-c -") #'evil-numbers/dec-at-pt))

  (evil-define-key
    'visual 'global (kbd "g C-a") #'evil-numbers/inc-at-pt-incremental)
  (evil-define-key
    'visual 'global (kbd "g C-x") #'evil-numbers/dec-at-pt-incremental))

(use-package evil-exchange
  :defer 7
  :after (evil)
  :functions (evil-exchange-cx-install)
  :init (evil-exchange-cx-install))

(provide 'kkk-evil)
;;; kkk-evil.el ends here
