<commit_before>;;; evil-evilified-state.el --- A minimalistic evil state
;;
;; Copyright (c) 2012-2016 <NAME> & Contributors
;;
;; Author: <NAME> <<EMAIL>>
;; Keywords: convenience editing evil spacemacs
;; Created: 22 Mar 2015
;; Version: 1.0
;; Package-Requires: ((evil "1.0.9"))

;; This file is not part of GNU Emacs.

;; 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 <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Define a `evilified' evil state inheriting from `emacs' state and
;; setting a minimalist list of Vim key bindings (like navigation, search, ...)

;; The shadowed original mode key bindings are automatically reassigned
;; following a set of rules:

;;; Code:

(require 'evil)
(require 'bind-map)

(defvar evilified-state--modes nil
  "List of all evilified modes.")

(defvar evilified-state--evil-surround nil
  "Evil surround mode variable backup.")
(make-variable-buffer-local 'evilified-state--evil-surround)

(defvar evilified-state--normal-state-map nil
  "Local backup of normal state keymap.")
(make-variable-buffer-local 'evilified-state--normal-state-map)

(evil-define-state evilified
  "Evilified state.
 Hybrid `emacs state' with carrefully selected Vim key bindings.
 See spacemacs conventions for more info."
  :tag " <N'> "
  :enable (emacs)
  :message "-- EVILIFIED BUFFER --"
  :cursor box)

(bind-map spacemacs-default-map
  :prefix-cmd spacemacs-cmds
  :evil-keys (dotspacemacs-leader-key)
  :evil-states (evilified)
  :override-minor-modes t
  :override-mode-name spacemacs-leader-override-mode)

(evil-define-command evil-force-evilified-state ()
  "Switch to evilified state without recording current command."
  :repeat abort
  :suppress-operator t
  (evil-evilified-state))

(defun evilified-state--pre-command-hook ()
  "Redirect key bindings to `evilified-state'.
Needed to bypass keymaps set as text properties."
  (unless (bound-and-true-p isearch-mode)
    (when (memq evil-state '(evilified visual))
      (let* ((map (get-char-property (point) 'keymap))
             (evilified-map (when map (cdr (assq 'evilified-state map))))
             (command (when (and evilified-map
                                 (eq 1 (length (this-command-keys))))
                        (lookup-key evilified-map (this-command-keys)))))
        (when command (setq this-command command))))))

(defun evilified-state--setup-normal-state-keymap ()
  "Setup the normal state keymap."
  (unless evilified-state--normal-state-map
    (setq-local evilified-state--normal-state-map
                (copy-keymap evil-normal-state-map)))
  (setq-local evil-normal-state-map
              (copy-keymap evilified-state--normal-state-map))
  (define-key evil-normal-state-map [escape] 'evil-evilified-state))

(defun evilified-state--restore-normal-state-keymap ()
  "Restore the normal state keymap."
  (setq-local evil-normal-state-map evilified-state--normal-state-map))

(defun evilified-state--clear-normal-state-keymap ()
  "Clear the normal state keymap."
  (setq-local evil-normal-state-map (cons 'keymap nil))
  (evil-normalize-keymaps))

(defun evilified-state--setup-visual-state-keymap ()
  "Setup the normal state keymap."
  (setq-local evil-visual-state-map
              (cons 'keymap (list (cons ?y 'evil-yank)
                                  (cons 'escape 'evil-exit-visual-state)))))

(defun evilified-state--evilified-state-on-entry ()
  "Setup evilified state."
  (when (derived-mode-p 'magit-mode)
    ;; Courtesy of evil-magit package
    ;; without this set-mark-command activates visual-state which is just
    ;; annoying ;; and introduces possible bugs
    (remove-hook 'activate-mark-hook 'evil-visual-activate-hook t))
  (when (bound-and-true-p evil-surround-mode)
    (make-local-variable 'evil-surround-mode)
    (evil-surround-mode -1))
  (evilified-state--setup-normal-state-keymap)
  (evilified-state--setup-visual-state-keymap)
  (add-hook 'pre-command-hook 'evilified-state--pre-command-hook nil 'local)
  (add-hook 'evil-visual-state-entry-hook
            'evilified-state--visual-state-on-entry nil 'local)
  (add-hook 'evil-visual-state-exit-hook
            'evilified-state--visual-state-on-exit nil 'local))

(defun evilified-state--visual-state-on-entry ()
  "Setup visual state."
  ;; we need to clear temporarily the normal state keymap in order to reach
  ;; the mode keymap
  (when (eq 'evilified evil-previous-state)
    (evilified-state--clear-normal-state-keymap)))

(defun evilified-state--visual-state-on-exit ()
  "Clean visual state"
  (evilified-state--restore-normal-state-keymap))

(add-hook 'evil-evilified-state-entry-hook
          'evilified-state--evilified-state-on-entry)

;; default key bindings for all evilified buffers
(define-key evil-evilified-state-map "/" 'evil-search-forward)
(define-key evil-evilified-state-map ":" 'evil-ex)
(define-key evil-evilified-state-map "h" 'evil-backward-char)
(define-key evil-evilified-state-map "j" 'evil-next-visual-line)
(define-key evil-evilified-state-map "k" 'evil-previous-visual-line)
(define-key evil-evilified-state-map "l" 'evil-forward-char)
(define-key evil-evilified-state-map "n" 'evil-search-next)
(define-key evil-evilified-state-map "N" 'evil-search-previous)
(define-key evil-evilified-state-map "v" 'evil-visual-char)
(define-key evil-evilified-state-map "V" 'evil-visual-line)
(define-key evil-evilified-state-map "gg" 'evil-goto-first-line)
(define-key evil-evilified-state-map "G" 'evil-goto-line)
(define-key evil-evilified-state-map (kbd "C-f") 'evil-scroll-page-down)
(define-key evil-evilified-state-map (kbd "C-b") 'evil-scroll-page-up)
(define-key evil-evilified-state-map (kbd "C-e") 'evil-scroll-line-down)
(define-key evil-evilified-state-map (kbd "C-y") 'evil-scroll-line-up)
(define-key evil-evilified-state-map (kbd "C-d") 'evil-scroll-down)
(define-key evil-evilified-state-map (kbd "C-u") 'evil-scroll-up)
(define-key evil-evilified-state-map (kbd "C-z") 'evil-emacs-state)
(bind-map spacemacs-default-map
  :prefix-cmd spacemacs-cmds
  :evil-states (evilified)
  :evil-keys (dotspacemacs-leader-key)
  :evil-use-local t)
(setq evil-evilified-state-map-original (copy-keymap evil-evilified-state-map))

;; old macro<commit_msg>evilified: Remove stray bind-map declaration<commit_after>;;; evil-evilified-state.el --- A minimalistic evil state
;;
;; Copyright (c) 2012-2016 <NAME> & Contributors
;;
;; Author: <NAME> <<EMAIL>>
;; Keywords: convenience editing evil spacemacs
;; Created: 22 Mar 2015
;; Version: 1.0
;; Package-Requires: ((evil "1.0.9"))

;; This file is not part of GNU Emacs.

;; 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 <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Define a `evilified' evil state inheriting from `emacs' state and
;; setting a minimalist list of Vim key bindings (like navigation, search, ...)

;; The shadowed original mode key bindings are automatically reassigned
;; following a set of rules:

;;; Code:

(require 'evil)
(require 'bind-map)

(defvar evilified-state--modes nil
  "List of all evilified modes.")

(defvar evilified-state--evil-surround nil
  "Evil surround mode variable backup.")
(make-variable-buffer-local 'evilified-state--evil-surround)

(defvar evilified-state--normal-state-map nil
  "Local backup of normal state keymap.")
(make-variable-buffer-local 'evilified-state--normal-state-map)

(evil-define-state evilified
  "Evilified state.
 Hybrid `emacs state' with carrefully selected Vim key bindings.
 See spacemacs conventions for more info."
  :tag " <N'> "
  :enable (emacs)
  :message "-- EVILIFIED BUFFER --"
  :cursor box)

(bind-map spacemacs-default-map
  :prefix-cmd spacemacs-cmds
  :evil-keys (dotspacemacs-leader-key)
  :evil-states (evilified)
  :override-minor-modes t
  :override-mode-name spacemacs-leader-override-mode)

(evil-define-command evil-force-evilified-state ()
  "Switch to evilified state without recording current command."
  :repeat abort
  :suppress-operator t
  (evil-evilified-state))

(defun evilified-state--pre-command-hook ()
  "Redirect key bindings to `evilified-state'.
Needed to bypass keymaps set as text properties."
  (unless (bound-and-true-p isearch-mode)
    (when (memq evil-state '(evilified visual))
      (let* ((map (get-char-property (point) 'keymap))
             (evilified-map (when map (cdr (assq 'evilified-state map))))
             (command (when (and evilified-map
                                 (eq 1 (length (this-command-keys))))
                        (lookup-key evilified-map (this-command-keys)))))
        (when command (setq this-command command))))))

(defun evilified-state--setup-normal-state-keymap ()
  "Setup the normal state keymap."
  (unless evilified-state--normal-state-map
    (setq-local evilified-state--normal-state-map
                (copy-keymap evil-normal-state-map)))
  (setq-local evil-normal-state-map
              (copy-keymap evilified-state--normal-state-map))
  (define-key evil-normal-state-map [escape] 'evil-evilified-state))

(defun evilified-state--restore-normal-state-keymap ()
  "Restore the normal state keymap."
  (setq-local evil-normal-state-map evilified-state--normal-state-map))

(defun evilified-state--clear-normal-state-keymap ()
  "Clear the normal state keymap."
  (setq-local evil-normal-state-map (cons 'keymap nil))
  (evil-normalize-keymaps))

(defun evilified-state--setup-visual-state-keymap ()
  "Setup the normal state keymap."
  (setq-local evil-visual-state-map
              (cons 'keymap (list (cons ?y 'evil-yank)
                                  (cons 'escape 'evil-exit-visual-state)))))

(defun evilified-state--evilified-state-on-entry ()
  "Setup evilified state."
  (when (derived-mode-p 'magit-mode)
    ;; Courtesy of evil-magit package
    ;; without this set-mark-command activates visual-state which is just
    ;; annoying ;; and introduces possible bugs
    (remove-hook 'activate-mark-hook 'evil-visual-activate-hook t))
  (when (bound-and-true-p evil-surround-mode)
    (make-local-variable 'evil-surround-mode)
    (evil-surround-mode -1))
  (evilified-state--setup-normal-state-keymap)
  (evilified-state--setup-visual-state-keymap)
  (add-hook 'pre-command-hook 'evilified-state--pre-command-hook nil 'local)
  (add-hook 'evil-visual-state-entry-hook
            'evilified-state--visual-state-on-entry nil 'local)
  (add-hook 'evil-visual-state-exit-hook
            'evilified-state--visual-state-on-exit nil 'local))

(defun evilified-state--visual-state-on-entry ()
  "Setup visual state."
  ;; we need to clear temporarily the normal state keymap in order to reach
  ;; the mode keymap
  (when (eq 'evilified evil-previous-state)
    (evilified-state--clear-normal-state-keymap)))

(defun evilified-state--visual-state-on-exit ()
  "Clean visual state"
  (evilified-state--restore-normal-state-keymap))

(add-hook 'evil-evilified-state-entry-hook
          'evilified-state--evilified-state-on-entry)

;; default key bindings for all evilified buffers
(define-key evil-evilified-state-map "/" 'evil-search-forward)
(define-key evil-evilified-state-map ":" 'evil-ex)
(define-key evil-evilified-state-map "h" 'evil-backward-char)
(define-key evil-evilified-state-map "j" 'evil-next-visual-line)
(define-key evil-evilified-state-map "k" 'evil-previous-visual-line)
(define-key evil-evilified-state-map "l" 'evil-forward-char)
(define-key evil-evilified-state-map "n" 'evil-search-next)
(define-key evil-evilified-state-map "N" 'evil-search-previous)
(define-key evil-evilified-state-map "v" 'evil-visual-char)
(define-key evil-evilified-state-map "V" 'evil-visual-line)
(define-key evil-evilified-state-map "gg" 'evil-goto-first-line)
(define-key evil-evilified-state-map "G" 'evil-goto-line)
(define-key evil-evilified-state-map (kbd "C-f") 'evil-scroll-page-down)
(define-key evil-evilified-state-map (kbd "C-b") 'evil-scroll-page-up)
(define-key evil-evilified-state-map (kbd "C-e") 'evil-scroll-line-down)
(define-key evil-evilified-state-map (kbd "C-y") 'evil-scroll-line-up)
(define-key evil-evilified-state-map (kbd "C-d") 'evil-scroll-down)
(define-key evil-evilified-state-map (kbd "C-u") 'evil-scroll-up)
(define-key evil-evilified-state-map (kbd "C-z") 'evil-emacs-state)
(setq evil-evilified-state-map-original (copy-keymap evil-evilified-state-map))

;; old macro
{'fraction_non_alphanumeric': 0.14115100395610958, 'fraction_numerical': 0.003284317384489065, 'mean_word_length': 3.676439790575916, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1698869', 'n_tokens_mistral': 4699, 'n_tokens_neox': 4395, 'n_words': 1123}