about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/evil-text-objects-haskell-20180316.1833/evil-text-objects-haskell.el
blob: 4edb5810f2701de531add204162a29fcb5b96302 (plain) (blame)
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
132
;;; evil-text-objects-haskell.el --- Text objects for Haskell source code
;; Package-Version: 20180316.1833

;;; License:

;; Copyright (C) 2018 Off Market Data, Inc. DBA Urbint
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to
;; deal in the Software without restriction, including without limitation the
;; rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
;; sell copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
;; IN THE SOFTWARE.

;;; Commentary:

;; evil-text-objects-haskell provides text-object definitions that
;; should make working with Haskell in Emacs more enjoyable.
;;
;; Currently supporting:
;;   - functions
;;   - multi-line comments
;;
;; See the README.md for installation instructions.

(require 'evil)
(require 'pcre2el)
(require 'dash)
(require 'bind-key)

;;; Code:

;; Helper Functions
(defun string-symbol-at-point ()
  "Return a string version of the symbol at point after stripping away
after all of the text properties."
  (->
   (symbol-at-point)
   symbol-name
   substring-no-properties))

;; multi-line comments
(evil-define-text-object
  evil-inner-haskell-comment-block (count &optional beg end type)
  "Inner text object for a Haskell comment block."
  (let ((beg (save-excursion
               (search-backward "{-")
               (right-char 2)
               (point)))
        (end (save-excursion
               (search-forward  "-}")
               (left-char 2)
               (point))))
    (evil-range beg end type)))

(evil-define-text-object
  evil-outer-haskell-comment-block (count &optional beg end type)
  "Outer text object for a Haskell comment block."
  (let ((beg (save-excursion
               (search-backward "{-")
               (point)))
        (end (save-excursion
               (search-forward  "-}")
               (point))))
    (evil-range beg end type)))

;; functions
(evil-define-text-object
  evil-inner-haskell-function (count &optional beg end type)
  "Inner text object for a Haskell function."
  (evil-range 0 0 type))

(evil-define-text-object
  evil-outer-haskell-function (count &optional beg end type)
  "Outer text object for a Haskell function."
  (beginning-of-line)
  (when (looking-at "--")
    (while (looking-at "--")
      (forward-line -1)))
  (when (looking-at "[[:space:]]")
    (while (looking-at "[[:space:]]")
      (forward-line -1)))
  (let* ((fn-name (save-excursion
                    (search-backward-regexp "^\\w")
                    (string-symbol-at-point)))
         (fn-name-regexp (concat "^" fn-name "\\b"))
         (end (save-excursion
                (while (search-forward-regexp fn-name-regexp nil t))
                (unless (search-forward-regexp "^\\w" nil t)
                  (goto-char (point-max)))
                (search-backward-regexp "^[[:space:]]+[^ ]")
                (end-of-line)
                (point)))
         (beg (save-excursion
                (goto-char end)
                (while (search-backward-regexp fn-name-regexp nil t))
                (beginning-of-line)
                (forward-line -1)
                (while (looking-at "--")
                  (forward-line -1))
                (point))))
    (evil-range beg end type)))

;; Installation Helper
(defun evil-text-objects-haskell/install ()
  "Register keybindings for the text objects defined herein.  It is
recommended to run this after something like `haskell-mode-hook'.  See
README.md for additional information."
  (bind-keys :map evil-operator-state-local-map
             ("af" . evil-outer-haskell-function)
             ("if" . evil-inner-haskell-function)
             ("iC" . evil-inner-haskell-comment-block)
             ("aC" . evil-outer-haskell-comment-block))
  (bind-keys :map evil-visual-state-local-map
             ("af" . evil-outer-haskell-function)
             ("if" . evil-inner-haskell-function)
             ("iC" . evil-inner-haskell-comment-block)
             ("aC" . evil-outer-haskell-comment-block)))

(provide 'evil-text-objects-haskell)

;;; evil-text-objects-haskell.el ends here