/
retrie.el
262 lines (216 loc) · 11.3 KB
/
retrie.el
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
;;; retrie.el --- Refactoring Haskell code with retrie
;; Copyright (C) 2020 Junyoung Clare Jang
;; Author: Junyoung Clare Jang <jjc9310@gmail.com>
;; Maintainer: Junyoung Clare Jang <jjc9310@gmail.com>
;; Created: 2020/05/16
;; Version: 0.0.1
;; Keywords: files, languages, tools
;; URL: https://github.com/Ailrun/emacs-retrie
;; Package-Requires: ((emacs "24.5"))
;; 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:
;; This package is an Emacs wrapper of retrie (URL `https://github.com/facebookincubator/retrie').
;;
;; Currently this provides the following 21 functions
;;
;; - retrie-fold-in-current-buffer
;; - retrie-fold-in-buffer
;; - retrie-fold-in-file
;; - retrie-unfold-in-current-buffer
;; - retrie-unfold-in-buffer
;; - retrie-unfold-in-file
;; - retrie-rule-forward-in-current-buffer
;; - retrie-rule-forward-in-buffer
;; - retrie-rule-forward-in-file
;; - retrie-rule-backward-in-current-buffer
;; - retrie-rule-backward-in-buffer
;; - retrie-rule-backward-in-file
;; - retrie-type-forward-in-current-buffer
;; - retrie-type-forward-in-buffer
;; - retrie-type-forward-in-file
;; - retrie-type-backward-in-current-buffer
;; - retrie-type-backward-in-buffer
;; - retrie-type-backward-in-file
;; - retrie-adhoc-in-current-buffer
;; - retrie-adhoc-in-buffer
;; - retrie-adhoc-in-file
;;; Change Log:
;;; Code:
(require 'subr-x)
;; Customize variables
(defgroup retrie
nil
"Settings for Haskell tool retrie"
:tag "Retrie"
:group 'convenience
:group 'haskell
:group 'tools
:package-version '(retrie . "0.0.1")
:prefix "retrie-")
(defcustom retrie-executable "retrie"
"The name of the retrie executable to find."
:group 'retrie
:package-version '(retrie . "0.0.1")
:type 'string
:risky t)
(defcustom retrie-use-single-thread nil
"A boolean to disable parallel executions of retrie (usually for debugging).
Disabling works only when `retrie-log-level' is not loud."
:group 'retrie
:package-version '(retrie . "0.0.1")
:type 'boolean)
(defcustom retrie-log-level 0
"The level of verbosity used to print retrie log.
Setting loud makes parallel executions of retrie disabled"
:group 'retrie
:package-version '(retrie . "0.0.1")
:type '(choice
(const :tag "silent" 0)
(const :tag "normal" 1)
(const :tag "loud" 0)))
(defcustom retrie-other-arguments nil
"Other arguments that are not specified as a custom variable in this plugin.
They will be passed to retrie along with the options created from the retrie- custom variables."
:group 'retrie
:package-version '(retrie . "0.0.1")
:type '(list string)
:risky t)
;; Constants
(defconst retrie--fold-option "--fold")
(defconst retrie--unfold-option "--unfold")
(defconst retrie--rule-forward-option "--rule-forward")
(defconst retrie--rule-backward-option "--rule-backward")
(defconst retrie--type-forward-option "--type-forward")
(defconst retrie--type-backward-option "--type-backward")
(defconst retrie--adhoc-option "--adhoc")
;; Interactive commands
(defun retrie-fold-in-current-buffer (name)
"Fold the function definition of the fully-qualified name NAME in the current buffer."
(interactive "sname of function to fold in the current buffer:")
(retrie--call-retrie-for-buffer (current-buffer) retrie--fold-option name))
(defun retrie-fold-in-buffer (buffername name)
"Fold the function definition of the fully-qualified name NAME in the buffer BUFFERNAME."
(interactive "*bbuffer:\nsname of function to fold in %s:")
(retrie--call-retrie-for-buffer (get-buffer buffername) retrie--fold-option name))
(defun retrie-fold-in-file (filepath name)
"Fold the function definition of the fully-qualified name NAME in the file FILEPATH."
(interactive "ffile path:\nsname of function to fold in %s:")
(retrie--call-retrie-for-file filepath retrie--fold-option name))
(defun retrie-unfold-in-current-buffer (name)
"Unfold the function definition of the fully-qualified name NAME in the current buffer."
(interactive "sname of function to unfold in the current buffer:")
(retrie--call-retrie-for-buffer (current-buffer) retrie--unfold-option name))
(defun retrie-unfold-in-buffer (buffername name)
"Unfold the function definition of the fully-qualified name NAME in the buffer BUFFERNAME."
(interactive "*bbuffer:\nsname of function to unfold in %s:")
(retrie--call-retrie-for-buffer (get-buffer buffername) retrie--unfold-option name))
(defun retrie-unfold-in-file (filepath name)
"Unfold the function definition of the fully-qualified name NAME in the file FILEPATH."
(interactive "ffile path:\nsname of function to unfold in %s:")
(retrie--call-retrie-for-file filepath retrie--unfold-option name))
(defun retrie-rule-forward-in-current-buffer (name)
"Apply the rule of the fully-qualified name NAME in left-to-right manner in the current buffer."
(interactive "sname of rule to apply (left-to-right) in the current buffer:")
(retrie--call-retrie-for-buffer (current-buffer) retrie--rule-forward-option name))
(defun retrie-rule-forward-in-buffer (buffername name)
"Apply the rule of the fully-qualified name NAME in left-to-right manner in the buffer BUFFERNAME."
(interactive "*bbuffer:\nsname of rule to apply (left-to-right) in %s:")
(retrie--call-retrie-for-buffer (get-buffer buffername) retrie--rule-forward-option name))
(defun retrie-rule-forward-in-file (filepath name)
"Apply the rule of the fully-qualified name NAME in left-to-right manner in the file FILEPATH."
(interactive "ffile path:\nsname of rule to apply (left-to-right) in %s:")
(retrie--call-retrie-for-file filepath retrie--rule-forward-option name))
(defun retrie-rule-backward-in-current-buffer (name)
"Apply the rule of the fully-qualified name NAME in right-to-left manner in the current buffer."
(interactive "sname of rule to apply (right-to-left) in the current buffer:")
(retrie--call-retrie-for-buffer (current-buffer) retrie--rule-backward-option name))
(defun retrie-rule-backward-in-buffer (buffername name)
"Apply the rule of the fully-qualified name NAME in right-to-left manner in the buffer BUFFERNAME."
(interactive "*bbuffer:\nsname of rule to apply (right-to-left) in %s:")
(retrie--call-retrie-for-buffer (get-buffer buffername) retrie--rule-backward-option name))
(defun retrie-rule-backward-in-file (filepath name)
"Apply the rule of the fully-qualified name NAME in right-to-left manner in the file FILEPATH."
(interactive "ffile path:\nsname of rule to apply (right-to-left) in %s:")
(retrie--call-retrie-for-file filepath retrie--rule-backward-option name))
(defun retrie-type-forward-in-current-buffer (name)
"Apply the type of the fully-qualified name NAME in left-to-right manner in the current buffer."
(interactive "sname of type to apply (left-to-right) in the current buffer:")
(retrie--call-retrie-for-buffer (current-buffer) retrie--type-forward-option name))
(defun retrie-type-forward-in-buffer (buffername name)
"Apply the type of the fully-qualified name NAME in left-to-right manner in the buffer BUFFERNAME."
(interactive "*bbuffer:\nsname of type to apply (left-to-right) in %s:")
(retrie--call-retrie-for-buffer (get-buffer buffername) retrie--type-forward-option name))
(defun retrie-type-forward-in-file (filepath name)
"Apply the type of the fully-qualified name NAME in left-to-right manner in the file FILEPATH."
(interactive "ffile path:\nsname of type to apply (left-to-right) in %s:")
(retrie--call-retrie-for-file filepath retrie--type-forward-option name))
(defun retrie-type-backward-in-current-buffer (name)
"Apply the type of the fully-qualified name NAME in right-to-left manner in the current buffer."
(interactive "sname of type to apply (right-to-left) in the current buffer:")
(retrie--call-retrie-for-buffer (current-buffer) retrie--type-backward-option name))
(defun retrie-type-backward-in-buffer (buffername name)
"Apply the type of the fully-qualified name NAME in right-to-left manner in the buffer BUFFERNAME."
(interactive "*bbuffer:\nsname of type to apply (right-to-left) in %s:")
(retrie--call-retrie-for-buffer (get-buffer buffername) retrie--type-backward-option name))
(defun retrie-type-backward-in-file (filepath name)
"Apply the type of the fully-qualified name NAME in right-to-left manner in the file FILEPATH."
(interactive "ffile path:\nsname of type to apply (right-to-left) in %s:")
(retrie--call-retrie-for-file filepath retrie--type-backward-option name))
(defun retrie-adhoc-in-current-buffer (adhoc)
"Apply the rule ADHOC in the current buffer."
(interactive "srewrite rule to apply in the current buffer:")
(retrie--call-retrie-for-buffer (current-buffer) retrie--adhoc-option adhoc))
(defun retrie-adhoc-in-buffer (buffername adhoc)
"Apply the rule ADHOC in the buffer BUFFERNAME."
(interactive "*bbuffer:\nsrewrite rule to apply in %s:")
(retrie--call-retrie-for-buffer (get-buffer buffername) retrie--adhoc-option adhoc))
(defun retrie-adhoc-in-file (filepath adhoc)
"Apply the rule ADHOC in the file FILEPATH."
(interactive "ffile path:\nsrewrite rule to apply in %s:")
(retrie--call-retrie-for-file filepath retrie--adhoc-option adhoc))
;; Internal functions
(defun retrie--call-retrie-for-buffer (buffer &rest args)
"Internal function to call retrie in BUFFER with ARGS."
(let ((filepath (buffer-file-name buffer)))
(unless filepath
(error "%s is not visiting any file. Abort" (buffer-name buffer)))
(apply #'retrie--call-retrie-for-file filepath args)))
(defun retrie--call-retrie-for-file (filepath &rest args)
"Internal function to call retrie in FILEPATH with ARGS."
(apply #'retrie--call-retrie "--target-file" filepath args))
(defun retrie--call-retrie (&rest args)
"Internal function to call retrie with ARGS."
(let ((logbuf (generate-new-buffer "*retrie*"))
(args
(append
args
(if retrie-use-single-thread '("--single-threaded") nil)
(list "--verbosity" (number-to-string retrie-log-level))
retrie-other-arguments))
(retrie (executable-find retrie-executable)))
(unless retrie
(error "Cannot find %s in exec-path. Please check your exec-path" retrie-executable))
(let ((process (apply #'start-process retrie logbuf retrie-executable args)))
(set-process-sentinel process 'retrie--sentinel))))
(defun retrie--sentinel (process event)
"Internal function taking PROCESS and EVENT."
(if (and (= 0 (process-exit-status process))
(process-live-p process))
(retrie--print-log "retrie is %s" (string-trim-right event))
(with-current-buffer (process-buffer process)
(retrie--print-log "retrie is %s\n%s" (string-trim-right event) (string-trim-right (buffer-string))))
(kill-buffer (process-buffer process))))
(defun retrie--print-log (string &rest args)
"Internal function taking STRING and ARGS."
(apply #'message string args))
(provide 'retrie)
;;; retrie.el ends here