summaryrefslogtreecommitdiff
path: root/modes/unimpaired/evil-collection-unimpaired.el
blob: fef595d06b8c830be92ef51ec1d659dcbc6e8f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
;;; evil-collection-unimpaired.el --- Evil Collection port of Unimpaired -*- lexical-binding: t -*-

;; Copyright (C) 2019 James Nguyen

;; Author: James Nguyen <james@jojojames.com>
;; Maintainer: James Nguyen <james@jojojames.com>
;; Pierre Neidhardt <mail@ambrevar.xyz>
;; URL: https://github.com/emacs-evil/evil-collection
;; Version: 0.0.1
;; Package-Requires: ((emacs "25.1"))
;; Keywords: evil, unimpaired, tools

;; 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:
;; `evil' port of unimpaired for `evil-collection'.
;; https://github.com/tpope/vim-unimpaired

;;; Code:
(require 'evil-collection)

(defgroup evil-collection-unimpaired nil
  "Evil port of unimpaired for `evil-collection'."
  :group 'evil-collection)

(defconst evil-collection-unimpaired-maps '(evil-collection-unimpaired-mode-map))

(defvar evil-collection-unimpaired-mode-map (make-sparse-keymap)
  "Keymap for `evil-collection-unimpaired-mode'.")

(define-minor-mode evil-collection-unimpaired-mode
  ""
  nil
  nil
  evil-collection-unimpaired-mode-map)

;;;###autoload
(define-global-minor-mode global-evil-collection-unimpaired-mode
  evil-collection-unimpaired-mode evil-collection-unimpaired-mode-on
  :group 'evil-collection-unimpaired)

(defun evil-collection-unimpaired-mode-on ()
  "Turn on `evil-collection-unimpaired-mode'."
  (evil-collection-unimpaired-mode 1))

(defun evil-collection-unimpaired-next-error ()
  "Go to next error."
  (interactive)
  (cond
   ((and (bound-and-true-p flycheck-mode)
         (fboundp 'flycheck-next-error))
    (flycheck-next-error))
   ((and (bound-and-true-p flymake-mode)
         (fboundp 'flymake-goto-next-error))
    (flymake-goto-next-error))
   (:default
    (message "No linting modes are on."))))

(defun evil-collection-unimpaired-previous-error ()
  "Go to previous error."
  (interactive)
  (cond
   ((and (bound-and-true-p flycheck-mode)
         (fboundp 'flycheck-previous-error))
    (flycheck-previous-error))
   ((and (bound-and-true-p flymake-mode)
         (fboundp 'flymake-goto-prev-error))
    (flymake-goto-prev-error))
   (:default
    (message "No linting modes are on."))))

(defun evil-collection-unimpaired-insert-newline-above (count)
  "Insert COUNT blank line(s) above current line."
  (interactive "p")
  (save-excursion (dotimes (_ count) (evil-insert-newline-above)))
  (when (bolp) (forward-char count)))

(defun evil-collection-unimpaired-insert-newline-below (count)
  "Insert COUNT blank line(s) below current line."
  (interactive "p")
  (save-excursion (dotimes (_ count) (evil-insert-newline-below))))

(defun evil-collection-unimpaired--encode (beg end fn)
  "Apply FN in range from BEG to END."
  (save-excursion
    (goto-char beg)
    (let* ((end (if (eq evil-this-type 'line) (1- end) end))
           (text (buffer-substring-no-properties beg end)))
      (delete-region beg end)
      (insert (funcall fn text)))))

(evil-define-operator evil-collection-unimpaired-url-encode (count &optional beg end)
  "Encode a URL string."
  (interactive "<c><r>")
  (ignore count)
  (evil-collection-unimpaired--encode beg end #'url-encode-url))

(evil-define-operator evil-collection-unimpaired-url-decode (count &optional beg end)
  "Decode a URL string."
  (interactive "<c><r>")
  (ignore count)
  (evil-collection-unimpaired--encode beg end #'url-unhex-string))

;;;###autoload
(defun evil-collection-unimpaired-setup ()
  "Set up unimpaired-like bindings."
  (global-evil-collection-unimpaired-mode 1)
  (evil-collection-define-key 'normal 'evil-collection-unimpaired-mode-map
    "[b" 'evil-prev-buffer
    "]b" 'evil-next-buffer
    "]l" 'evil-collection-unimpaired-next-error
    "[l" 'evil-collection-unimpaired-previous-error
    (kbd "[ SPC") 'evil-collection-unimpaired-insert-newline-above
    (kbd "] SPC") 'evil-collection-unimpaired-insert-newline-below)
  (evil-collection-define-key 'motion 'evil-collection-unimpaired-mode-map
    "[u" 'evil-collection-unimpaired-url-encode
    "]u" 'evil-collection-unimpaired-url-decode))

(provide 'evil-collection-unimpaired)
;;; evil-collection-unimpaired.el ends here