summaryrefslogtreecommitdiff
path: root/modes/beginend/evil-collection-beginend.el
blob: 715a4711fbb036575094237312fa550aff6a65f1 (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
;;; evil-collection-beginend.el --- Evil bindings for beginend -*- lexical-binding: t -*-

;; Copyright (C) 2021 Balaji Sivaraman

;; Author: Balaji Sivaraman <balaji@balajisivaraman.com>
;; Maintainer: Balaji Sivaraman <balaji@balajisivaraman.com>
;; Pierre Neidhardt <mail@ambrevar.xyz>
;; URL: https://github.com/emacs-evil/evil-collection
;; Version: 0.0.1
;; Package-Requires: ((emacs "26.3"))
;; Keywords: evil, 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 bindings for beginend.

;;; Code:

(require 'evil-collection)
(require 'beginend nil t)

(eval-and-compile
  (defconst evil-collection-beginend-modes '(bs-mode
                                             rg-mode
                                             org-mode
                                             deft-mode
                                             prog-mode
                                             LaTeX-mode
                                             dired-mode
                                             latex-mode
                                             nroam-mode
                                             occur-mode
                                             vc-dir-mode
                                             ibuffer-mode
                                             message-mode
                                             outline-mode
                                             prodigy-mode
                                             org-agenda-mode
                                             compilation-mode
                                             epa-key-list-mode
                                             magit-status-mode
                                             elfeed-search-mode
                                             elfeed-show-mode
                                             magit-revision-mode
                                             notmuch-search-mode
                                             recentf-dialog-mode)))


(defmacro evil-beginend--define-goto-beginning-motion (ec-mode-name)
  "Macro to define new Evil motion that will use the corresponding
beginend-goto-beginning function for EC-MODE-NAME when count is not
provided for the motion; otherwise behave like `evil-goto-first-line'
if count is provided.

This will also associate the regular Evil `gg' keybinding with the
newly defined motion."
  (let ((motion-name (intern (format "evil-beginend-%s-goto-beginning" ec-mode-name)))
        (beginend-beginning-fn-name (intern (format "beginend-%s-goto-beginning" ec-mode-name)))
        (beginend-map-name (intern (format "beginend-%s-map" ec-mode-name))))
    `(progn
       (declare-function ,motion-name "evil-collection")
       (declare-function ,beginend-beginning-fn-name "ext:beginend")
       (evil-define-motion ,motion-name (count)
         :jump t
         :type line
         (if count
             (evil-goto-first-line count)
           (,beginend-beginning-fn-name)))
       (evil-collection-define-key 'normal ',beginend-map-name
         "gg" ',motion-name))))

(defmacro evil-beginend--define-goto-end-motion (ec-mode-name)
  "Macro to define new Evil motion that will use the corresponding
beginend-goto-end function for EC-MODE-NAME when count is not
provided for the motion; otherwise behave like `evil-goto-line'
if count is provided.

This will also associate the regular Evil `G' keybinding with the
newly defined motion."
  (let ((motion-name (intern (format "evil-beginend-%s-goto-end" ec-mode-name)))
        (beginend-end-fn-name (intern (format "beginend-%s-goto-end" ec-mode-name)))
        (beginend-map-name (intern (format "beginend-%s-map" ec-mode-name))))
    `(progn
       (declare-function ,motion-name "evil-collection")
       (declare-function ,beginend-end-fn-name "ext:beginend")
       (evil-define-motion ,motion-name (count)
         :jump t
         :type line
         (if count
             (evil-goto-line count)
           (,beginend-end-fn-name)))
       (evil-collection-define-key 'normal ',beginend-map-name
         "G" ',motion-name))))

(defmacro evil-beginend--define-mode-motions ()
  "Define the goto-beginning and goto-end motions for all modes in
`evil-collection-beginend-modes'."
  `(progn
     ,@(mapcar
        (lambda (mode-symbol)
          (let ((mode-string (symbol-name mode-symbol)))
            `(progn
               (evil-beginend--define-goto-beginning-motion ,mode-string)
               (evil-beginend--define-goto-end-motion ,mode-string))))
        evil-collection-beginend-modes)))

;;;###autoload
(defun evil-collection-beginend-setup ()
  "Set up `evil' bindings for `beginend'."
  (evil-beginend--define-mode-motions))

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