Repository navigation
Expand file tree
/
Copy pathfloskell.el
More file actions
263 lines (233 loc) · 9.6 KB
/
Copy pathfloskell.el
File metadata and controls
263 lines (233 loc) · 9.6 KB
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
263
;;; floskell.el --- Indent haskell code using the "floskell" program
;; Copyright (c) 2014 Chris Done. All rights reserved.
;; Author: Chris Done <chrisdone@gmail.com>
;; URL: https://github.com/ennocramer/floskell
;; Package-Requires: ((cl-lib "0.5"))
;; This file 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, or (at your option)
;; any later version.
;; This file 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/>.
;;; Code:
(require 'cl-lib)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Minor mode
(defvar floskell-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [remap indent-region] #'floskell-reformat-region)
(define-key map [remap fill-paragraph] #'floskell-reformat-decl-or-fill)
map)
"Keymap for `floskell-mode'.")
;;;###autoload
(define-minor-mode floskell-mode
"Indent code with the floskell program.
Provide the following keybindings:
\\{floskell-mode-map}"
:init-value nil
:keymap floskell-mode-map
:lighter " HI"
:group 'haskell
:require 'floskell)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Customization properties
(defcustom floskell-style
nil
"The style to use for formatting."
:group 'haskell
:type 'string
:safe #'stringp)
(defcustom floskell-process-path
"floskell"
"Location where the floskell executable is located."
:group 'haskell
:type 'string
:safe #'stringp)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Interactive functions
;;;###autoload
(defun floskell-reformat-decl ()
"Re-format the current declaration by parsing and pretty
printing it. Comments are preserved, although placement may be
funky."
(interactive)
(let ((start-end (floskell-decl-points)))
(when start-end
(let ((beg (car start-end))
(end (cdr start-end)))
(floskell-reformat-region beg end)))))
;;;###autoload
(defun floskell-reformat-buffer ()
"Reformat the whole buffer."
(interactive)
(floskell-reformat-region (point-min)
(point-max)))
;;;###autoload
(defun floskell-reformat-decl-or-fill (justify)
"Re-format current declaration, or fill paragraph.
Fill paragraph if in a comment, otherwise reformat the current
declaration."
(interactive (progn
;; Copied from `fill-paragraph'
(barf-if-buffer-read-only)
(list (if current-prefix-arg 'full))))
(if (floskell-in-comment)
(fill-paragraph justify t)
(floskell/reformat-decl)))
;;;###autoload
(defun floskell-reformat-region (beg end)
"Reformat the given region, accounting for indentation."
(interactive "r")
(if (= (save-excursion (goto-char beg)
(line-beginning-position))
beg)
(floskell-reformat-region-as-is beg end)
(let* ((column (- beg (line-beginning-position)))
(string (buffer-substring-no-properties beg end))
(new-string (with-temp-buffer
(insert (make-string column ? ) string)
(floskell-reformat-region-as-is (point-min)
(point-max))
(delete-region (point-min) (1+ column))
(buffer-substring (point-min)
(point-max)))))
(save-excursion
(goto-char beg)
(delete-region beg end)
(insert new-string)))))
;;;###autoload
(defun floskell/reformat-decl ()
"See `floskell-reformat-decl'."
(floskell-reformat-decl))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Internal library
(defun floskell-reformat-region-as-is (beg end)
"Reformat the given region as-is.
This is the place where floskell is actually called."
(let* ((original (current-buffer))
(orig-str (buffer-substring-no-properties beg end)))
(with-temp-buffer
(let ((temp (current-buffer)))
(with-current-buffer original
(let ((ret (apply #'call-process-region
(append (list beg
end
floskell-process-path
nil ; delete
temp ; output
nil)
(when floskell-style
(list "--style" floskell-style))
(floskell-extra-arguments)))))
(cond
((= ret 1)
(let ((error-string
(with-current-buffer temp
(let ((string (progn (goto-char (point-min))
(buffer-substring (line-beginning-position)
(line-end-position)))))
string))))
(if (string= error-string "floskell: Parse error: EOF")
(message "language pragma")
(error error-string))))
((= ret 0)
(let ((new-str (with-current-buffer temp
(buffer-string))))
(if (not (string= new-str orig-str))
(let ((line (line-number-at-pos))
(col (current-column)))
(delete-region beg
end)
(let ((new-start (point)))
(insert new-str)
(let ((new-end (point)))
(goto-char (point-min))
(forward-line (1- line))
(goto-char (+ (line-beginning-position) col))
(when (looking-back "^[ ]+")
(back-to-indentation))
(delete-trailing-whitespace new-start new-end)))
(message "Formatted."))
(message "Already formatted.")))))))))))
(defun floskell-decl-points (&optional use-line-comments)
"Get the start and end position of the current
declaration. This assumes that declarations start at column zero
and that the rest is always indented by one space afterwards, so
Template Haskell uses with it all being at column zero are not
expected to work."
(cond
;; If we're in a block comment spanning multiple lines then let's
;; see if it starts at the beginning of the line (or if any comment
;; is at the beginning of the line, we don't care to treat it as a
;; proper declaration.
((and (not use-line-comments)
(floskell-in-comment)
(save-excursion (goto-char (line-beginning-position))
(floskell-in-comment)))
nil)
((save-excursion
(goto-char (line-beginning-position))
(or (looking-at "^-}$")
(looking-at "^{-$")))
nil)
;; Otherwise we just do our line-based hack.
(t
(save-excursion
(let ((start
(or (cl-letf
(((symbol-function 'jump)
#'(lambda ()
(search-backward-regexp "^[^ \n]" nil t 1)
(cond
((save-excursion (goto-char (line-beginning-position))
(looking-at "|]"))
(jump))
(t (unless (or (looking-at "^-}$")
(looking-at "^{-$"))
(point)))))))
(goto-char (line-end-position))
(jump))
0))
(end
(progn
(goto-char (1+ (point)))
(or (cl-letf
(((symbol-function 'jump)
#'(lambda ()
(when (search-forward-regexp "[\n]+[^ \n]" nil t 1)
(cond
((save-excursion (goto-char (line-beginning-position))
(looking-at "|]"))
(jump))
(t (forward-char -1)
(search-backward-regexp "[^\n ]" nil t)
(forward-char)
(point)))))))
(jump))
(point-max)))))
(cons start end))))))
(defun floskell-in-comment ()
"Are we currently in a comment?"
(save-excursion
(when (and (= (line-end-position)
(point))
(/= (line-beginning-position) (point)))
(forward-char -1))
(and
(elt (syntax-ppss) 4)
;; Pragmas {-# SPECIALIZE .. #-} etc are not to be treated as
;; comments, even though they are highlighted as such
(not (save-excursion (goto-char (line-beginning-position))
(looking-at "{-# "))))))
(defun floskell-extra-arguments ()
"Pass in extra arguments, such as extensions and optionally
other things later."
(if (boundp 'haskell-language-extensions)
haskell-language-extensions
'()))
(provide 'floskell)
;;; floskell.el ends here