-
Notifications
You must be signed in to change notification settings - Fork 0
/
adjust-parens-3.1.tar
480 lines (423 loc) · 30 KB
/
adjust-parens-3.1.tar
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
adjust-parens-3.1/ 0000775 0001750 0001750 00000000000 14051364140 012623 5 ustar kimr kimr adjust-parens-3.1/adjust-parens.el 0000664 0001750 0001750 00000031642 14023021756 015735 0 ustar kimr kimr ;;; adjust-parens.el --- Indent and dedent Lisp code, automatically adjust close parens -*- lexical-binding: t; -*-
;; Copyright (C) 2013 Free Software Foundation, Inc.
;; Author: Barry O'Reilly <[email protected]>
;; Version: 3.1
;; 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:
;;
;; This package provides commands for indenting and dedenting Lisp
;; code such that close parentheses and brackets are automatically
;; adjusted to be consistent with the new level of indentation.
;;
;; When reading Lisp, the programmer pays attention to open parens and
;; the close parens on the same line. But when a sexp spans more than
;; one line, she deduces the close paren from indentation alone. Given
;; that's how we read Lisp, this package aims to enable editing Lisp
;; similarly: automatically adjust the close parens programmers ignore
;; when reading. A result of this is an editing experience somewhat
;; like python-mode, which also offers "indent" and "dedent" commands.
;; There are differences because lisp-mode knows more due to existing
;; parens.
;;
;; To use:
;; (require 'adjust-parens)
;; (add-hook 'emacs-lisp-mode-hook #'adjust-parens-mode)
;; (add-hook 'clojure-mode-hook #'adjust-parens-mode)
;; ;; etc
;;
;; This binds two keys in Lisp Mode:
;; (local-set-key (kbd "TAB") 'lisp-indent-adjust-parens)
;; (local-set-key (kbd "<backtab>") 'lisp-dedent-adjust-parens)
;;
;; lisp-indent-adjust-parens potentially calls indent-for-tab-command
;; (the usual binding for TAB in Lisp Mode). Thus it should not
;; interfere with other TAB features like completion-at-point.
;;
;; Some examples follow. | indicates the position of point.
;;
;; (let ((x 10) (y (some-func 20))))
;; |
;;
;; After one TAB:
;;
;; (let ((x 10) (y (some-func 20)))
;; |)
;;
;; After three more TAB:
;;
;; (let ((x 10) (y (some-func 20
;; |))))
;;
;; After two Shift-TAB to dedent:
;;
;; (let ((x 10) (y (some-func 20))
;; |))
;;
;; When dedenting, the sexp may have sibling sexps on lines below. It
;; makes little sense for those sexps to stay at the same indentation,
;; because they cannot keep the same parent sexp without being moved
;; completely. Thus they are dedented too. An example of this:
;;
;; (defun func ()
;; (save-excursion
;; (other-func-1)
;; |(other-func-2)
;; (other-func-3)))
;;
;; After Shift-TAB:
;;
;; (defun func ()
;; (save-excursion
;; (other-func-1))
;; |(other-func-2)
;; (other-func-3))
;;
;; If you indent again with TAB, the sexps siblings aren't indented:
;;
;; (defun func ()
;; (save-excursion
;; (other-func-1)
;; |(other-func-2))
;; (other-func-3))
;;
;; Thus TAB and Shift-TAB are not exact inverse operations of each
;; other, though they often seem to be.
;;; Code:
;; Future work:
;; - Consider taking a region as input in order to indent a sexp and
;; its siblings in the region. Dedenting would not take a region.
(require 'cl-lib)
(defun last-sexp-with-relative-depth (from-pos to-pos rel-depth)
"Parsing sexps from FROM-POS (inclusive) to TO-POS (exclusive),
return the position of the last sexp that had depth REL-DEPTH relative
to FROM-POS. Returns nil if REL-DEPTH is not reached.
May change point.
Examples:
Region: a (b c (d)) e (f g (h i)) j
Evaluate: (last-sexp-with-relative-depth pos-a (1+ pos-j) 0)
Returns: position of j
Evaluate: (last-sexp-with-relative-depth pos-a (1+ pos-j) 1)
Returns: position of (h i)
This function assumes FROM-POS is not in a string or comment."
(goto-char from-pos)
(let (the-last-pos
(parse-state '(0 nil nil nil nil nil nil nil nil)))
(while (< (point) to-pos)
(setq parse-state
(parse-partial-sexp (point)
to-pos
nil
t ; Stop before sexp
parse-state))
(and (not (eq (point) to-pos))
(eq (car parse-state) rel-depth)
(setq the-last-pos (point)))
;; The previous parse may not advance. To advance and maintain
;; correctness of depth, we parse over the next char.
(when (< (point) to-pos)
(setq parse-state
(parse-partial-sexp (point)
(1+ (point))
nil
nil
parse-state))))
the-last-pos))
(defun adjust-parens-check-prior-sexp ()
"Returns true if there is a full sexp before point, else false.
May change point."
(let ((pos1 (progn (backward-sexp)
(point)))
(pos2 (progn (forward-sexp)
(backward-sexp)
(point))))
(>= pos1 pos2)))
(defun adjust-close-paren-for-indent ()
"Adjust a close parentheses of a sexp so as
lisp-indent-adjust-parens can indent that many levels.
If a close paren was moved, returns a two element list of positions:
where the close paren was moved from and the position following where
it moved to.
If there's no close parens to move, either return nil or allow
scan-error to propogate up."
(save-excursion
(let* ((deleted-paren-char nil)
(deleted-paren-pos
(save-excursion
(beginning-of-line)
;; Account for edge case when point has no sexp before it
;;
;; This is primarily to avoid funny behavior when there
;; is no sexp between bob and point.
(if (not (adjust-parens-check-prior-sexp))
nil
;; If the sexp at point is a list,
;; delete its closing paren
(when (eq (scan-lists (point) 1 0)
(scan-sexps (point) 1))
(forward-sexp)
(setq deleted-paren-char (char-before))
(delete-char -1)
(point))))))
;; Invariant: deleted-paren-pos nil iff deleted-paren-char nil
(when deleted-paren-pos
(let ((sexp-to-close
(save-excursion
(last-sexp-with-relative-depth (point)
(progn (end-of-line)
(point))
0))))
(when sexp-to-close
(goto-char sexp-to-close)
(forward-sexp))
;; Note: when no sexp-to-close found, line is empty. So put
;; close paren after point.
(insert deleted-paren-char)
(list deleted-paren-pos (point)))))))
(defun adjust-close-paren-for-dedent ()
"Adjust a close parentheses of a sexp so as
lisp-dedent-adjust-parens can dedent that many levels.
If a close paren was moved, returns a two element list of positions:
where the close paren was moved from and the position following where
it moved to.
If there's no close parens to move, either return nil or allow
scan-error to propogate up."
(save-excursion
(let* ((deleted-paren-char nil)
(deleted-paren-pos
(save-excursion
(when (< (point)
(progn (up-list)
(point)))
(setq deleted-paren-char (char-before))
(delete-char -1)
(point)))))
;; Invariant: deleted-paren-pos nil iff deleted-paren-char nil
(when deleted-paren-pos
(let ((sexp-to-close
;; Needs to work when dedenting in an empty list, in
;; which case backward-sexp will signal scan-error and
;; sexp-to-close will be nil.
(condition-case nil
(progn (backward-sexp)
(point))
(scan-error nil))))
;; Move point to where to insert close paren
(if sexp-to-close
(forward-sexp)
(backward-up-list)
(forward-char 1))
(insert deleted-paren-char)
;; The insertion makes deleted-paren-pos off by 1
(list (1+ deleted-paren-pos)
(point)))))))
(defun adjust-parens-p ()
"Whether to adjust parens."
(save-excursion
(let ((orig-pos (point)))
(back-to-indentation)
(and (= orig-pos (point))
(not (use-region-p))
;; Current line indented?
(let ((indent (calculate-lisp-indent)))
(and indent
(= (current-column)
(if (listp indent)
(car indent)
indent))))))))
(defun adjust-parens-and-indent (raw-parg
adjust-function
adjust-function-negative
fallback-function)
"Adjust close parens and indent the region over which the parens
moved."
(if (adjust-parens-p)
(let* ((parg (prefix-numeric-value raw-parg))
(adjust-function (if (and parg (< parg 0))
adjust-function-negative
adjust-function))
(region-of-change (list (point) (point))))
(cl-loop for i from 1 to (or (and parg (abs parg)) 1)
with finished = nil
while (not finished)
do
(condition-case err
(let ((close-paren-movement
(funcall adjust-function)))
(if close-paren-movement
(setq region-of-change
(list (min (car region-of-change)
(car close-paren-movement)
(cadr close-paren-movement))
(max (cadr region-of-change)
(car close-paren-movement)
(cadr close-paren-movement))))
(setq finished t)))
(scan-error (setq finished err))))
(apply 'indent-region region-of-change)
(back-to-indentation)
t)
(funcall fallback-function raw-parg)))
(defcustom adjust-parens-fallback-indent-function 'indent-for-tab-command
"The function to call with prefix arg instead of
adjust-parens-and-indent when adjust-parens-p returns false."
:type 'function
:group 'adjust-parens)
(defun lisp-indent-adjust-parens (&optional raw-parg)
"Indent Lisp code to the next level while adjusting sexp balanced
expressions to be consistent.
Returns t if adjust-parens changed the buffer, else returns the
result of calling adjust-parens-fallback-indent-function.
This command can be bound to TAB instead of indent-for-tab-command. It
potentially calls the latter."
(interactive "P")
(adjust-parens-and-indent raw-parg
#'adjust-close-paren-for-indent
#'adjust-close-paren-for-dedent
adjust-parens-fallback-indent-function))
(defcustom adjust-parens-fallback-dedent-function 'indent-for-tab-command
"The function to call with prefix arg instead of
adjust-parens-and-indent when adjust-parens-p returns false."
:type 'function
:group 'adjust-parens)
(defun lisp-dedent-adjust-parens (&optional raw-parg)
"Dedent Lisp code to the previous level while adjusting sexp
balanced expressions to be consistent.
Returns t if adjust-parens changed the buffer, else returns the
result of calling adjust-parens-fallback-dedent-function.
Binding to <backtab> (ie Shift-Tab) is a sensible choice."
(interactive "P")
(adjust-parens-and-indent raw-parg
#'adjust-close-paren-for-dedent
#'adjust-close-paren-for-indent
adjust-parens-fallback-dedent-function))
(defgroup adjust-parens nil
"Indent and dedent Lisp code, automatically adjust close parens."
:prefix "adjust-parens-"
:group 'convenience)
(defvar adjust-parens-mode-map (make-sparse-keymap)
"Keymap for `adjust-parens-mode'")
(define-key adjust-parens-mode-map (kbd "TAB") 'lisp-indent-adjust-parens)
(define-key adjust-parens-mode-map (kbd "<backtab>") 'lisp-dedent-adjust-parens)
;;;###autoload
(define-minor-mode adjust-parens-mode
"Indent and dedent Lisp code, automatically adjust close parens."
:group 'adjust-parens
:keymap adjust-parens-mode-map)
(provide 'adjust-parens)
;;; adjust-parens.el ends here