-
Notifications
You must be signed in to change notification settings - Fork 79
/
tuareg-compat.el
426 lines (387 loc) · 16 KB
/
tuareg-compat.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
;;; tuareg-compat.el -*- lexical-binding:t -*-
;; FIX: make sure `comment-region' supports `comment-continue' made
;; only of spaces (and in a consistent fashion even for older Emacs).
(require 'newcomment)
;; Emacs < 26
(defun tuareg--comment-padright--advice (orig-fun &rest args)
(let ((str (nth 0 args)))
(unless (and (eq major-mode 'tuareg-mode)
(stringp str) (not (string-match "\\S-" str)))
(apply orig-fun args))))
(when (and (< emacs-major-version 26) (fboundp 'comment-region-default))
(advice-add 'comment-padright :around #'tuareg--comment-padright--advice))
;; Emacs < 27
(defun tuareg--comment-region-default (beg end &optional arg)
(let* ((numarg (prefix-numeric-value arg))
(style (cdr (assoc comment-style comment-styles)))
(lines (nth 2 style))
(block (nth 1 style))
(multi (nth 0 style)))
;; We use `chars' instead of `syntax' because `\n' might be
;; of end-comment syntax rather than of whitespace syntax.
;; sanitize BEG and END
(goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line)
(setq beg (max beg (point)))
(goto-char end) (skip-chars-backward " \t\n\r") (end-of-line)
(setq end (min end (point)))
(if (>= beg end) (error "Nothing to comment"))
;; sanitize LINES
(setq lines
(and
lines ;; multi
(progn (goto-char beg) (beginning-of-line)
(skip-syntax-forward " ")
(>= (point) beg))
(progn (goto-char end) (end-of-line) (skip-syntax-backward " ")
(<= (point) end))
(or block (not (string= "" comment-end)))
(or block (progn (goto-char beg) (re-search-forward "$" end t)))))
;; don't add end-markers just because the user asked for `block'
(unless (or lines (string= "" comment-end)) (setq block nil))
(cond
((consp arg) (uncomment-region beg end))
((< numarg 0) (uncomment-region beg end (- numarg)))
(t
(let ((multi-char (/= (string-match "[ \t]*\\'" comment-start) 1))
indent triple)
(if (eq (nth 3 style) 'multi-char)
(save-excursion
(goto-char beg)
(setq indent multi-char
;; Triple if we will put the comment starter at the margin
;; and the first line of the region isn't indented
;; at least two spaces.
triple (and (not multi-char) (looking-at "\t\\| "))))
(setq indent (nth 3 style)))
;; In Lisp and similar modes with one-character comment starters,
;; double it by default if `comment-add' says so.
;; If it isn't indented, triple it.
(if (and (null arg) (not multi-char))
(setq numarg (* comment-add (if triple 2 1)))
(setq numarg (1- (prefix-numeric-value arg))))
(comment-region-internal
beg end
(let ((s (comment-padright comment-start numarg)))
(if (string-match comment-start-skip s) s
(comment-padright comment-start)))
(let ((s (comment-padleft comment-end numarg)))
(and s (if (string-match comment-end-skip s) s
(comment-padright comment-end))))
(if multi (or (comment-padright comment-continue numarg)
(and (stringp comment-continue) comment-continue)))
(if multi
(comment-padleft (comment-string-reverse comment-continue) numarg))
block
lines
indent))))))
(defun tuareg--comment-region-default--advice (orig-fun &rest args)
(apply (if (eq major-mode 'tuareg-mode)
'tuareg--comment-region-default
orig-fun)
args))
(when (and (< emacs-major-version 27) (fboundp 'comment-region-default))
(advice-add 'comment-region-default :around
#'tuareg--comment-region-default--advice))
;; Emacs 27
(defun tuareg--comment-region-default-1 (beg end &optional arg noadjust)
"Comment region between BEG and END.
See `comment-region' for ARG. If NOADJUST, do not skip past
leading/trailing space when determining the region to comment
out."
(let* ((numarg (prefix-numeric-value arg))
(style (cdr (assoc comment-style comment-styles)))
(lines (nth 2 style))
(block (nth 1 style))
(multi (nth 0 style)))
(if noadjust
(when (bolp)
(setq end (1- end)))
;; We use `chars' instead of `syntax' because `\n' might be
;; of end-comment syntax rather than of whitespace syntax.
;; sanitize BEG and END
(goto-char beg)
(skip-chars-forward " \t\n\r")
(beginning-of-line)
(setq beg (max beg (point)))
(goto-char end)
(skip-chars-backward " \t\n\r")
(end-of-line)
(setq end (min end (point)))
(when (>= beg end)
(error "Nothing to comment")))
;; sanitize LINES
(setq lines
(and
lines ;; multi
(progn (goto-char beg) (beginning-of-line)
(skip-syntax-forward " ")
(>= (point) beg))
(progn (goto-char end) (end-of-line) (skip-syntax-backward " ")
(<= (point) end))
(or block (not (string= "" comment-end)))
(or block (progn (goto-char beg) (re-search-forward "$" end t)))))
;; don't add end-markers just because the user asked for `block'
(unless (or lines (string= "" comment-end)) (setq block nil))
(cond
((consp arg) (uncomment-region beg end))
((< numarg 0) (uncomment-region beg end (- numarg)))
(t
(let ((multi-char (/= (string-match "[ \t]*\\'" comment-start) 1))
indent triple)
(if (eq (nth 3 style) 'multi-char)
(save-excursion
(goto-char beg)
(setq indent multi-char
;; Triple if we will put the comment starter at the margin
;; and the first line of the region isn't indented
;; at least two spaces.
triple (and (not multi-char) (looking-at "\t\\| "))))
(setq indent (nth 3 style)))
;; In Lisp and similar modes with one-character comment starters,
;; double it by default if `comment-add' says so.
;; If it isn't indented, triple it.
(if (and (null arg) (not multi-char))
(setq numarg (* comment-add (if triple 2 1)))
(setq numarg (1- (prefix-numeric-value arg))))
(comment-region-internal
beg end
(let ((s (comment-padright comment-start numarg)))
(if (string-match comment-start-skip s) s
(comment-padright comment-start)))
(let ((s (comment-padleft comment-end numarg)))
(and s (if (string-match comment-end-skip s) s
(comment-padright comment-end))))
(if multi
(or (comment-padright comment-continue numarg)
;; `comment-padright' returns nil when
;; `comment-continue' contains only whitespace
(and (stringp comment-continue) comment-continue)))
(if multi
(comment-padleft (comment-string-reverse comment-continue) numarg))
block
lines
indent))))))
(defun tuareg--comment-region-default-1--advice (orig-fun &rest args)
(apply (if (eq major-mode 'tuareg-mode)
'tuareg--comment-region-default-1
orig-fun)
args))
(when (and (= emacs-major-version 27) (fboundp 'comment-region-default-1))
(advice-add 'comment-region-default-1 :around
#'tuareg--comment-region-default-1--advice))
;; FIX: uncommenting
;; Emacs < 27
(defun tuareg--uncomment-region-default (beg end &optional arg)
"Uncomment each line in the BEG .. END region.
The numeric prefix ARG can specify a number of chars to remove from the
comment delimiters.
This function is the default value of `uncomment-region-function'."
(goto-char beg)
(setq end (copy-marker end))
(let* ((numarg (prefix-numeric-value arg))
(ccs comment-continue)
(srei (or (comment-padright ccs 're)
(and (stringp comment-continue) comment-continue)))
(csre (comment-padright comment-start 're))
(sre (and srei (concat "^\\s-*?\\(" srei "\\)")))
spt)
(while (and (< (point) end)
(setq spt (comment-search-forward end t)))
(let ((ipt (point))
;; Find the end of the comment.
(ept (progn
(goto-char spt)
(unless (or (comment-forward)
;; Allow non-terminated comments.
(eobp))
(error "Can't find the comment end"))
(point)))
(box nil)
(box-equal nil)) ;Whether we might be using `=' for boxes.
(save-restriction
(narrow-to-region spt ept)
;; Remove the comment-start.
(goto-char ipt)
(skip-syntax-backward " ")
;; A box-comment starts with a looong comment-start marker.
(when (and (or (and (= (- (point) (point-min)) 1)
(setq box-equal t)
(looking-at "=\\{7\\}")
(not (eq (char-before (point-max)) ?\n))
(skip-chars-forward "="))
(> (- (point) (point-min) (length comment-start)) 7))
(> (count-lines (point-min) (point-max)) 2))
(setq box t))
;; Skip the padding. Padding can come from comment-padding and/or
;; from comment-start, so we first check comment-start.
(if (or (save-excursion (goto-char (point-min)) (looking-at csre))
(looking-at (regexp-quote comment-padding)))
(goto-char (match-end 0)))
(when (and sre (looking-at (concat "\\s-*\n\\s-*" srei)))
(goto-char (match-end 0)))
(if (null arg) (delete-region (point-min) (point))
(let ((opoint (point-marker)))
(skip-syntax-backward " ")
(delete-char (- numarg))
(unless (and (not (bobp))
(save-excursion (goto-char (point-min))
(looking-at comment-start-skip)))
;; If there's something left but it doesn't look like
;; a comment-start any more, just remove it.
(delete-region (point-min) opoint))))
;; Remove the end-comment (and leading padding and such).
(goto-char (point-max)) (comment-enter-backward)
;; Check for special `=' used sometimes in comment-box.
(when (and box-equal (not (eq (char-before (point-max)) ?\n)))
(let ((pos (point)))
;; skip `=' but only if there are at least 7.
(when (> (skip-chars-backward "=") -7) (goto-char pos))))
(unless (looking-at "\\(\n\\|\\s-\\)*\\'")
(when (and (bolp) (not (bobp))) (backward-char))
(if (null arg) (delete-region (point) (point-max))
(skip-syntax-forward " ")
(delete-char numarg)
(unless (or (eobp) (looking-at comment-end-skip))
;; If there's something left but it doesn't look like
;; a comment-end any more, just remove it.
(delete-region (point) (point-max)))))
;; Unquote any nested end-comment.
(comment-quote-nested comment-start comment-end t)
;; Eliminate continuation markers as well.
(when sre
(let* ((cce (comment-string-reverse (or comment-continue
comment-start)))
(erei (and box (comment-padleft cce 're)))
(ere (and erei (concat "\\(" erei "\\)\\s-*$"))))
(goto-char (point-min))
(while (progn
(if (and ere (re-search-forward
ere (line-end-position) t))
(replace-match "" t t nil (if (match-end 2) 2 1))
(setq ere nil))
(forward-line 1)
(re-search-forward sre (line-end-position) t))
(replace-match "" t t nil (if (match-end 2) 2 1)))))
;; Go to the end for the next comment.
(goto-char (point-max))))))
(set-marker end nil))
(defun tuareg--uncomment-region-default--advice (orig-fun &rest args)
(apply (if (eq major-mode 'tuareg-mode)
'tuareg--uncomment-region-default
orig-fun)
args))
(when (and (< emacs-major-version 27) (fboundp 'uncomment-region-default))
(advice-add 'uncomment-region-default :around
#'tuareg--uncomment-region-default--advice))
;; Emacs 27
(defun tuareg--uncomment-region-default-1 (beg end &optional arg)
"Uncomment each line in the BEG .. END region.
The numeric prefix ARG can specify a number of chars to remove from the
comment delimiters.
This function is the default value of `uncomment-region-function'."
(goto-char beg)
(setq end (copy-marker end))
(let* ((numarg (prefix-numeric-value arg))
(ccs comment-continue)
(srei (or (comment-padright ccs 're)
(and (stringp comment-continue) comment-continue)))
(csre (comment-padright comment-start 're))
(sre (and srei (concat "^\\s-*?\\(" srei "\\)")))
spt)
(while (and (< (point) end)
(setq spt (comment-search-forward end t)))
(let ((ipt (point))
;; Find the end of the comment.
(ept (progn
(goto-char spt)
(unless (or (comment-forward)
;; Allow non-terminated comments.
(eobp))
(error "Can't find the comment end"))
(point)))
(box nil)
(box-equal nil)) ;Whether we might be using `=' for boxes.
(save-restriction
(narrow-to-region spt ept)
;; Remove the comment-start.
(goto-char ipt)
(skip-syntax-backward " ")
;; A box-comment starts with a looong comment-start marker.
(when (and (or (and (= (- (point) (point-min)) 1)
(setq box-equal t)
(looking-at "=\\{7\\}")
(not (eq (char-before (point-max)) ?\n))
(skip-chars-forward "="))
(> (- (point) (point-min) (length comment-start)) 7))
(> (count-lines (point-min) (point-max)) 2))
(setq box t))
;; Skip the padding. Padding can come from comment-padding and/or
;; from comment-start, so we first check comment-start.
(if (or (save-excursion (goto-char (point-min)) (looking-at csre))
(looking-at (regexp-quote comment-padding)))
(goto-char (match-end 0)))
(when (and sre (looking-at (concat "\\s-*\n\\s-*" srei)))
(goto-char (match-end 0)))
(if (null arg) (delete-region (point-min) (point))
(let ((opoint (point-marker)))
(skip-syntax-backward " ")
(delete-char (- numarg))
(unless (and (not (bobp))
(save-excursion (goto-char (point-min))
(looking-at comment-start-skip)))
;; If there's something left but it doesn't look like
;; a comment-start any more, just remove it.
(delete-region (point-min) opoint))))
;; Remove the end-comment (and leading padding and such).
(goto-char (point-max)) (comment-enter-backward)
;; Check for special `=' used sometimes in comment-box.
(when (and box-equal (not (eq (char-before (point-max)) ?\n)))
(let ((pos (point)))
;; skip `=' but only if there are at least 7.
(when (> (skip-chars-backward "=") -7) (goto-char pos))))
(unless (looking-at "\\(\n\\|\\s-\\)*\\'")
(when (and (bolp) (not (bobp))) (backward-char))
(if (null arg) (delete-region (point) (point-max))
(skip-syntax-forward " ")
(delete-char numarg)
(unless (or (eobp) (looking-at comment-end-skip))
;; If there's something left but it doesn't look like
;; a comment-end any more, just remove it.
(delete-region (point) (point-max)))))
;; Unquote any nested end-comment.
(comment-quote-nested comment-start comment-end t)
;; Eliminate continuation markers as well.
(when sre
(let* ((cce (comment-string-reverse (or comment-continue
comment-start)))
(erei (and box (comment-padleft cce 're)))
(ere (and erei (concat "\\(" erei "\\)\\s-*$"))))
(goto-char (point-min))
(while (progn
(if (and ere (re-search-forward
ere (line-end-position) t))
(replace-match "" t t nil (if (match-end 2) 2 1))
(setq ere nil))
(forward-line 1)
(re-search-forward sre (line-end-position) t))
(replace-match "" t t nil (if (match-end 2) 2 1)))))
;; Go to the end for the next comment.
(goto-char (point-max)))
;; Remove any obtrusive spaces left preceding a tab at `spt'.
(when (and (eq (char-after spt) ?\t) (eq (char-before spt) ? )
(> tab-width 0))
(save-excursion
(goto-char spt)
(let* ((fcol (current-column))
(slim (- (point) (mod fcol tab-width))))
(delete-char (- (skip-chars-backward " " slim)))))))))
(set-marker end nil))
(defun tuareg--uncomment-region-default-1--advice (orig-fun &rest args)
(apply (if (eq major-mode 'tuareg-mode)
'tuareg--uncomment-region-default-1
orig-fun)
args))
(when (and (<= emacs-major-version 28) (fboundp 'uncomment-region-default-1))
(advice-add 'uncomment-region-default-1 :around
#'tuareg--uncomment-region-default-1--advice))
(provide 'tuareg-compat)