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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
|
;;; smie.el --- Simple Minded Indentation Engine
;; Copyright (C) 2010 Free Software Foundation, Inc.
;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
;; Keywords: languages, lisp, internal, parsing, indentation
;; This file is part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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:
;; While working on the SML indentation code, the idea grew that maybe
;; I could write something generic to do the same thing, and at the
;; end of working on the SML code, I had a pretty good idea of what it
;; could look like. That idea grew stronger after working on
;; LaTeX indentation.
;;
;; So at some point I decided to try it out, by writing a new
;; indentation code for Coq while trying to keep most of the code
;; "table driven", where only the tables are Coq-specific. The result
;; (which was used for Beluga-mode as well) turned out to be based on
;; something pretty close to an operator precedence parser.
;; So here is another rewrite, this time following the actual principles of
;; operator precedence grammars. Why OPG? Even though they're among the
;; weakest kinds of parsers, these parsers have some very desirable properties
;; for Emacs:
;; - most importantly for indentation, they work equally well in either
;; direction, so you can use them to parse backward from the indentation
;; point to learn the syntactic context;
;; - they work locally, so there's no need to keep a cache of
;; the parser's state;
;; - because of that locality, indentation also works just fine when earlier
;; parts of the buffer are syntactically incorrect since the indentation
;; looks at "as little as possible" of the buffer make an indentation
;; decision.
;; - they typically have no error handling and can't even detect a parsing
;; error, so we don't have to worry about what to do in case of a syntax
;; error because the parser just automatically does something. Better yet,
;; we can afford to use a sloppy grammar.
;; The development (especially the parts building the 2D precedence
;; tables and then computing the precedence levels from it) is largely
;; inspired from page 187-194 of "Parsing techniques" by Dick Grune
;; and Ceriel Jacobs (BookBody.pdf available at
;; http://www.cs.vu.nl/~dick/PTAPG.html).
;;
;; OTOH we had to kill many chickens, read many coffee grounds, and practiced
;; untold numbers of black magic spells.
;;; Code:
(eval-when-compile (require 'cl))
;;; Building precedence level tables from BNF specs.
(defun smie-set-prec2tab (table x y val &optional override)
(assert (and x y))
(let* ((key (cons x y))
(old (gethash key table)))
(if (and old (not (eq old val)))
(if (and override (gethash key override))
;; FIXME: The override is meant to resolve ambiguities,
;; but it also hides real conflicts. It would be great to
;; be able to distinguish the two cases so that overrides
;; don't hide real conflicts.
(puthash key (gethash key override) table)
(display-warning 'smie (format "Conflict: %s %s/%s %s" x old val y)))
(puthash key val table))))
(defun smie-precs-precedence-table (precs)
"Compute a 2D precedence table from a list of precedences.
PRECS should be a list, sorted by precedence (e.g. \"+\" will
come before \"*\"), of elements of the form \(left OP ...)
or (right OP ...) or (nonassoc OP ...) or (assoc OP ...). All operators in
one of those elements share the same precedence level and associativity."
(let ((prec2-table (make-hash-table :test 'equal)))
(dolist (prec precs)
(dolist (op (cdr prec))
(let ((selfrule (cdr (assq (car prec)
'((left . >) (right . <) (assoc . =))))))
(when selfrule
(dolist (other-op (cdr prec))
(smie-set-prec2tab prec2-table op other-op selfrule))))
(let ((op1 '<) (op2 '>))
(dolist (other-prec precs)
(if (eq prec other-prec)
(setq op1 '> op2 '<)
(dolist (other-op (cdr other-prec))
(smie-set-prec2tab prec2-table op other-op op2)
(smie-set-prec2tab prec2-table other-op op op1)))))))
prec2-table))
(defun smie-merge-prec2s (&rest tables)
(if (null (cdr tables))
(car tables)
(let ((prec2 (make-hash-table :test 'equal)))
(dolist (table tables)
(maphash (lambda (k v)
(smie-set-prec2tab prec2 (car k) (cdr k) v))
table))
prec2)))
(defun smie-bnf-precedence-table (bnf &rest precs)
(let ((nts (mapcar 'car bnf)) ;Non-terminals
(first-ops-table ())
(last-ops-table ())
(first-nts-table ())
(last-nts-table ())
(prec2 (make-hash-table :test 'equal))
(override (apply 'smie-merge-prec2s
(mapcar 'smie-precs-precedence-table precs)))
again)
(dolist (rules bnf)
(let ((nt (car rules))
(last-ops ())
(first-ops ())
(last-nts ())
(first-nts ()))
(dolist (rhs (cdr rules))
(assert (consp rhs))
(if (not (member (car rhs) nts))
(pushnew (car rhs) first-ops)
(pushnew (car rhs) first-nts)
(when (consp (cdr rhs))
;; If the first is not an OP we add the second (which
;; should be an OP if BNF is an "operator grammar").
;; Strictly speaking, this should only be done if the
;; first is a non-terminal which can expand to a phrase
;; without any OP in it, but checking doesn't seem worth
;; the trouble, and it lets the writer of the BNF
;; be a bit more sloppy by skipping uninteresting base
;; cases which are terminals but not OPs.
(assert (not (member (cadr rhs) nts)))
(pushnew (cadr rhs) first-ops)))
(let ((shr (reverse rhs)))
(if (not (member (car shr) nts))
(pushnew (car shr) last-ops)
(pushnew (car shr) last-nts)
(when (consp (cdr shr))
(assert (not (member (cadr shr) nts)))
(pushnew (cadr shr) last-ops)))))
(push (cons nt first-ops) first-ops-table)
(push (cons nt last-ops) last-ops-table)
(push (cons nt first-nts) first-nts-table)
(push (cons nt last-nts) last-nts-table)))
;; Compute all first-ops by propagating the initial ones we have
;; now, according to first-nts.
(setq again t)
(while (prog1 again (setq again nil))
(dolist (first-nts first-nts-table)
(let* ((nt (pop first-nts))
(first-ops (assoc nt first-ops-table)))
(dolist (first-nt first-nts)
(dolist (op (cdr (assoc first-nt first-ops-table)))
(unless (member op first-ops)
(setq again t)
(push op (cdr first-ops))))))))
;; Same thing for last-ops.
(setq again t)
(while (prog1 again (setq again nil))
(dolist (last-nts last-nts-table)
(let* ((nt (pop last-nts))
(last-ops (assoc nt last-ops-table)))
(dolist (last-nt last-nts)
(dolist (op (cdr (assoc last-nt last-ops-table)))
(unless (member op last-ops)
(setq again t)
(push op (cdr last-ops))))))))
;; Now generate the 2D precedence table.
(dolist (rules bnf)
(dolist (rhs (cdr rules))
(while (cdr rhs)
(cond
((member (car rhs) nts)
(dolist (last (cdr (assoc (car rhs) last-ops-table)))
(smie-set-prec2tab prec2 last (cadr rhs) '> override)))
((member (cadr rhs) nts)
(dolist (first (cdr (assoc (cadr rhs) first-ops-table)))
(smie-set-prec2tab prec2 (car rhs) first '< override))
(if (and (cddr rhs) (not (member (car (cddr rhs)) nts)))
(smie-set-prec2tab prec2 (car rhs) (car (cddr rhs))
'= override)))
(t (smie-set-prec2tab prec2 (car rhs) (cadr rhs) '= override)))
(setq rhs (cdr rhs)))))
prec2))
(defun smie-prec2-levels (prec2)
"Take a 2D precedence table and turn it into an alist of precedence levels.
PREC2 is a table as returned by `smie-precs-precedence-table' or
`smie-bnf-precedence-table'."
;; For each operator, we create two "variables" (corresponding to
;; the left and right precedence level), which are represented by
;; cons cells. Those are the vary cons cells that appear in the
;; final `table'. The value of each "variable" is kept in the `car'.
(let ((table ())
(csts ())
(eqs ())
tmp x y)
;; From `prec2' we construct a list of constraints between
;; variables (aka "precedence levels"). These can be either
;; equality constraints (in `eqs') or `<' constraints (in `csts').
(maphash (lambda (k v)
(if (setq tmp (assoc (car k) table))
(setq x (cddr tmp))
(setq x (cons nil nil))
(push (cons (car k) (cons nil x)) table))
(if (setq tmp (assoc (cdr k) table))
(setq y (cdr tmp))
(setq y (cons nil (cons nil nil)))
(push (cons (cdr k) y) table))
(ecase v
(= (push (cons x y) eqs))
(< (push (cons x y) csts))
(> (push (cons y x) csts))))
prec2)
;; First process the equality constraints.
(let ((eqs eqs))
(while eqs
(let ((from (caar eqs))
(to (cdar eqs)))
(setq eqs (cdr eqs))
(if (eq to from)
nil ;Nothing to do.
(dolist (other-eq eqs)
(if (eq from (cdr other-eq)) (setcdr other-eq to))
(when (eq from (car other-eq))
;; This can happen because of `assoc' settings in precs
;; or because of a rhs like ("op" foo "op").
(setcar other-eq to)))
(dolist (cst csts)
(if (eq from (cdr cst)) (setcdr cst to))
(if (eq from (car cst)) (setcar cst to)))))))
;; Then eliminate trivial constraints iteratively.
(let ((i 0))
(while csts
(let ((rhvs (mapcar 'cdr csts))
(progress nil))
(dolist (cst csts)
(unless (memq (car cst) rhvs)
(setq progress t)
;; We could give each var in a given iteration the same value,
;; but we can also give them arbitrarily different values.
;; Basically, these are vars between which there is no
;; constraint (neither equality nor inequality), so
;; anything will do.
;; We give them arbitrary values, which means that we
;; replace the "no constraint" case with either > or <
;; but not =. The reason we do that is so as to try and
;; distinguish associative operators (which will have
;; left = right).
(unless (caar cst)
(setcar (car cst) i)
(incf i))
(setq csts (delq cst csts))))
(unless progress
(error "Can't resolve the precedence table to precedence levels")))
(incf i 10))
;; Propagate equalities back to their source.
(dolist (eq (nreverse eqs))
(assert (or (null (caar eq)) (eq (car eq) (cdr eq))))
(setcar (car eq) (cadr eq)))
;; Finally, fill in the remaining vars (which only appeared on the
;; right side of the < constraints).
(dolist (x table)
;; When both sides are nil, it means this operator binds very
;; very tight, but it's still just an operator, so we give it
;; the highest precedence.
;; OTOH if only one side is nil, it usually means it's like an
;; open-paren, which is very important for indentation purposes,
;; so we keep it nil, to make it easier to recognize.
(unless (or (nth 1 x) (nth 2 x))
(setf (nth 1 x) i)
(setf (nth 2 x) i))))
table))
;;; Parsing using a precedence level table.
(defvar smie-op-levels 'unset
"List of token parsing info.
Each element is of the form (TOKEN LEFT-LEVEL RIGHT-LEVEL).
Parsing is done using an operator precedence parser.")
(defalias 'smie-op-left 'car)
(defalias 'smie-op-right 'cadr)
(defun smie-backward-token ()
;; FIXME: This may be an OK default but probably needs a hook.
(buffer-substring (point)
(progn (if (zerop (skip-syntax-backward "."))
(skip-syntax-backward "w_'"))
(point))))
(defun smie-forward-token ()
;; FIXME: This may be an OK default but probably needs a hook.
(buffer-substring (point)
(progn (if (zerop (skip-syntax-forward "."))
(skip-syntax-forward "w_'"))
(point))))
(defun smie-associative-p (toklevels)
;; in "a + b + c" we want to stop at each +, but in
;; "if a then b else c" we don't want to stop at each keyword.
;; To distinguish the two cases, we made smie-prec2-levels choose
;; different levels for each part of "if a then b else c", so that
;; by checking if the left-level is equal to the right level, we can
;; figure out that it's an associative operator.
;; This is not 100% foolproof, tho, since a grammar like
;; (exp ("A" exp "C") ("A" exp "B" exp "C"))
;; will cause "B" to have equal left and right levels, even though
;; it is not an associative operator.
;; A better check would be the check the actual previous operator
;; against this one to see if it's the same, but we'd have to change
;; `levels' to keep a stack of operators rather than only levels.
(eq (smie-op-left toklevels) (smie-op-right toklevels)))
(defun smie-next-sexp (next-token next-sexp op-forw op-back halfsexp)
"Skip over one sexp.
NEXT-TOKEN is a function of no argument that moves forward by one
token (after skipping comments if needed) and returns it.
NEXT-SEXP is a lower-level function to skip one sexp.
OP-FORW is the accessor to the forward level of the level data.
OP-BACK is the accessor to the backward level of the level data.
HALFSEXP if non-nil, means skip over a partial sexp if needed. I.e. if the
first token we see is an operator, skip over its left-hand-side argument.
Possible return values:
(FORW-LEVEL POS TOKEN): we couldn't skip TOKEN because its back-level
is too high. FORW-LEVEL is the forw-level of TOKEN,
POS is its start position in the buffer.
(t POS TOKEN): same thing when we bump on the wrong side of a paren.
(nil POS TOKEN): we skipped over a paren-like pair.
nil: we skipped over an identifier, matched parentheses, ..."
(catch 'return
(let ((levels ()))
(while
(let* ((pos (point))
(token (funcall next-token))
(toklevels (cdr (assoc token smie-op-levels))))
(cond
((null toklevels)
(when (equal token "")
(condition-case err
(progn (goto-char pos) (funcall next-sexp 1) nil)
(scan-error (throw 'return (list t (caddr err)))))
(if (eq pos (point))
;; We did not move, so let's abort the loop.
(throw 'return (list t (point))))))
((null (funcall op-back toklevels))
;; A token like a paren-close.
(assert (funcall op-forw toklevels)) ;Otherwise, why mention it?
(push (funcall op-forw toklevels) levels))
(t
(while (and levels (< (funcall op-back toklevels) (car levels)))
(setq levels (cdr levels)))
(cond
((null levels)
(if (and halfsexp (funcall op-forw toklevels))
(push (funcall op-forw toklevels) levels)
(throw 'return
(prog1 (list (or (car toklevels) t) (point) token)
(goto-char pos)))))
(t
(if (and levels (= (funcall op-back toklevels) (car levels)))
(setq levels (cdr levels)))
(cond
((null levels)
(cond
((null (funcall op-forw toklevels))
(throw 'return (list nil (point) token)))
((smie-associative-p toklevels)
(throw 'return
(prog1 (list (or (car toklevels) t) (point) token)
(goto-char pos))))
;; We just found a match to the previously pending operator
;; but this new operator is still part of a larger RHS.
;; E.g. we're now looking at the "then" in
;; "if a then b else c". So we have to keep parsing the
;; rest of the construct.
(t (push (funcall op-forw toklevels) levels))))
(t
(if (funcall op-forw toklevels)
(push (funcall op-forw toklevels) levels))))))))
levels)
(setq halfsexp nil)))))
(defun smie-backward-sexp (&optional halfsexp)
"Skip over one sexp.
HALFSEXP if non-nil, means skip over a partial sexp if needed. I.e. if the
first token we see is an operator, skip over its left-hand-side argument.
Possible return values:
(LEFT-LEVEL POS TOKEN): we couldn't skip TOKEN because its right-level
is too high. LEFT-LEVEL is the left-level of TOKEN,
POS is its start position in the buffer.
(t POS TOKEN): same thing but for an open-paren or the beginning of buffer.
(nil POS TOKEN): we skipped over a paren-like pair.
nil: we skipped over an identifier, matched parentheses, ..."
(smie-next-sexp
(lambda () (forward-comment (- (point-max))) (smie-backward-token))
(indirect-function 'backward-sexp)
(indirect-function 'smie-op-left)
(indirect-function 'smie-op-right)
halfsexp))
(defun smie-forward-sexp (&optional halfsexp)
"Skip over one sexp.
HALFSEXP if non-nil, means skip over a partial sexp if needed. I.e. if the
first token we see is an operator, skip over its left-hand-side argument.
Possible return values:
(RIGHT-LEVEL POS TOKEN): we couldn't skip TOKEN because its left-level
is too high. RIGHT-LEVEL is the right-level of TOKEN,
POS is its end position in the buffer.
(t POS TOKEN): same thing but for an open-paren or the beginning of buffer.
(nil POS TOKEN): we skipped over a paren-like pair.
nil: we skipped over an identifier, matched parentheses, ..."
(smie-next-sexp
(lambda () (forward-comment (point-max)) (smie-forward-token))
(indirect-function 'forward-sexp)
(indirect-function 'smie-op-right)
(indirect-function 'smie-op-left)
halfsexp))
(defun smie-backward-sexp-command (&optional n)
"Move backward through N logical elements."
(interactive "p")
(if (< n 0)
(smie-forward-sexp-command (- n))
(let ((forward-sexp-function nil))
(while (> n 0)
(decf n)
(let ((pos (point))
(res (smie-backward-sexp 'halfsexp)))
(if (and (car res) (= pos (point)) (not (bolp)))
(signal 'scan-error
(list "Containing expression ends prematurely"
(cadr res) (cadr res)))
nil))))))
(defun smie-forward-sexp-command (&optional n)
"Move forward through N logical elements."
(interactive "p")
(if (< n 0)
(smie-backward-sexp-command (- n))
(let ((forward-sexp-function nil))
(while (> n 0)
(decf n)
(let ((pos (point))
(res (smie-forward-sexp 'halfsexp)))
(if (and (car res) (= pos (point)) (not (bolp)))
(signal 'scan-error
(list "Containing expression ends prematurely"
(cadr res) (cadr res)))
nil))))))
;;; The indentation engine.
(defcustom smie-indent-basic 4
"Basic amount of indentation."
:type 'integer)
(defvar smie-indent-rules 'unset
"Rules of the following form.
\(TOK OFFSET) how to indent right after TOK.
\(TOK O1 O2) how to indent right after TOK:
O1 is the default;
O2 is used if TOK is \"hanging\".
\((T1 . T2) . OFFSET) how to indent token T2 w.r.t T1.
\((t . TOK) . OFFSET) how to indent TOK with respect to its parent.
\(list-intro . TOKENS) declare TOKENS as being followed by what may look like
a funcall but is just a sequence of expressions.
\(t . OFFSET) basic indentation step.
\(args . OFFSET) indentation of arguments.
A nil offset defaults to `smie-indent-basic'.")
(defun smie-indent-hanging-p ()
;; A Hanging keyword is one that's at the end of a line except it's not at
;; the beginning of a line.
(and (save-excursion (smie-forward-token)
(skip-chars-forward " \t") (eolp))
(save-excursion (skip-chars-backward " \t") (not (bolp)))))
(defun smie-bolp ()
(save-excursion (skip-chars-backward " \t") (bolp)))
(defun smie-indent-offset (elem)
(or (cdr (assq elem smie-indent-rules))
(cdr (assq t smie-indent-rules))
smie-indent-basic))
(defun smie-indent-calculate (&optional virtual)
"Compute the indentation to use for point.
If VIRTUAL is non-nil, it means we're not trying to indent point but just
need to compute the column at which point should be indented
in order to figure out the indentation of some other (further down) point.
VIRTUAL can take two different non-nil values:
- :bolp: means that the current indentation of point can be trusted
to be good only if it follows a line break.
- :hanging: means that the current indentation of point can be
trusted to be good except if the following token is hanging."
;; FIXME: This has accumulated a lot of rules, some of which aren't
;; clearly orthogonal any more, so we should probably try and
;; restructure it somewhat.
(or
;; Trust pre-existing indentation on other lines.
(and virtual
(if (eq virtual :hanging) (not (smie-indent-hanging-p)) (smie-bolp))
(current-column))
;; Start the file at column 0.
(save-excursion
(forward-comment (- (point-max)))
(if (bobp) 0))
;; Align close paren with opening paren.
(save-excursion
;; (forward-comment (point-max))
(when (looking-at "\\s)")
(while (not (zerop (skip-syntax-forward ")")))
(skip-chars-forward " \t"))
(condition-case nil
(progn
(backward-sexp 1)
(smie-indent-calculate :hanging))
(scan-error nil))))
;; Align closing token with the corresponding opening one.
;; (e.g. "of" with "case", or "in" with "let").
(save-excursion
(let* ((pos (point))
(token (smie-forward-token))
(toklevels (cdr (assoc token smie-op-levels))))
(when (car toklevels)
(let ((res (smie-backward-sexp 'halfsexp)) tmp)
;; If we didn't move at all, that means we didn't really skip
;; what we wanted.
(when (< (point) pos)
(cond
((eq (car res) (car toklevels))
;; We bumped into a same-level operator. align with it.
(goto-char (cadr res))
;; Don't use (smie-indent-calculate :hanging) here, because we
;; want to jump back over a sequence of same-level ops such as
;; a -> b -> c
;; -> d
;; So as to align with the earliest appropriate place.
(smie-indent-calculate :bolp))
((equal token (save-excursion
(forward-comment (- (point-max)))
(smie-backward-token)))
;; in cases such as "fn x => fn y => fn z =>",
;; jump back to the very first fn.
;; FIXME: should we only do that for special tokens like "=>"?
(smie-indent-calculate :bolp))
((setq tmp (assoc (cons (caddr res) token)
smie-indent-rules))
(goto-char (cadr res))
(+ (cdr tmp) (smie-indent-calculate :hanging)))
(t
(+ (or (cdr (assoc (cons t token) smie-indent-rules)) 0)
(current-column)))))))))
;; Indentation of a comment.
(and (looking-at comment-start-skip)
(save-excursion
(forward-comment (point-max))
(skip-chars-forward " \t\r\n")
(smie-indent-calculate nil)))
;; Indentation inside a comment.
(and (looking-at "\\*") (nth 4 (syntax-ppss))
(let ((ppss (syntax-ppss)))
(save-excursion
(forward-line -1)
(if (<= (point) (nth 8 ppss))
(progn (goto-char (1+ (nth 8 ppss))) (current-column))
(skip-chars-forward " \t")
(if (looking-at "\\*")
(current-column))))))
;; Indentation right after a special keyword.
(save-excursion
(let* ((tok (progn (forward-comment (- (point-max)))
(smie-backward-token)))
(tokinfo (assoc tok smie-indent-rules))
(toklevel (assoc tok smie-op-levels)))
(when (or tokinfo (and toklevel (null (cadr toklevel))))
(if (or (smie-indent-hanging-p)
;; If calculating the virtual indentation point, prefer
;; looking up the virtual indentation of the alignment
;; point as well. This is used for indentation after
;; "fn x => fn y =>".
virtual)
(+ (smie-indent-calculate :bolp)
(or (caddr tokinfo) (cadr tokinfo) (smie-indent-offset t)))
(+ (current-column)
(or (cadr tokinfo) (smie-indent-offset t)))))))
;; Main loop (FIXME: whatever that means!?).
(save-excursion
(let ((positions nil)
(begline nil)
arg)
(while (and (null (car (smie-backward-sexp)))
(push (point) positions)
(not (setq begline (smie-bolp)))))
(save-excursion
;; Figure out if the atom we just skipped is an argument rather
;; than a function.
(setq arg (or (null (car (smie-backward-sexp)))
(member (progn (forward-comment (- (point-max)))
(smie-backward-token))
(cdr (assoc 'list-intro smie-indent-rules))))))
(cond
((and arg positions)
(goto-char (car positions))
(current-column))
((and (null begline) (cdr positions))
;; We skipped some args plus the function and bumped into something.
;; Align with the first arg.
(goto-char (cadr positions))
(current-column))
((and (null begline) positions)
;; We're the first arg.
;; FIXME: it might not be a funcall, in which case we might be the
;; second element.
(goto-char (car positions))
(+ (smie-indent-offset 'args)
;; We used to use (smie-indent-calculate :bolp), but that
;; doesn't seem right since it might then indent args less than
;; the function itself.
(current-column)))
((and (null arg) (null positions))
;; We're the function itself. Not sure what to do here yet.
(if virtual (current-column)
(save-excursion
(let* ((pos (point))
(tok (progn (forward-comment (- (point-max)))
(smie-backward-token)))
(toklevels (cdr (assoc tok smie-op-levels))))
(cond
((numberp (car toklevels))
;; We're right after an infix token. Let's skip over the
;; lefthand side.
(goto-char pos)
(let (res)
(while (progn (setq res (smie-backward-sexp 'halfsexp))
(and (not (smie-bolp))
(equal (car res) (car toklevels)))))
;; We should be right after a token of equal or
;; higher precedence.
(cond
((and (consp res) (memq (car res) '(t nil)))
;; The token of higher-precedence is like an open-paren.
;; Sample case for t: foo { bar, \n[TAB] baz }.
;; Sample case for nil: match ... with \n[TAB] | toto ...
;; (goto-char (cadr res))
(smie-indent-calculate :hanging))
((and (consp res) (<= (car res) (car toklevels)))
;; We stopped at a token of equal or higher precedence
;; because we found a place with which to align.
(current-column))
)))
;; For other cases.... hmm... we'll see when we get there.
)))))
((null positions)
(smie-backward-token)
(+ (smie-indent-offset 'args) (smie-indent-calculate :bolp)))
((car (smie-backward-sexp))
;; No arg stands on its own line, but the function does:
(if (cdr positions)
(progn
(goto-char (cadr positions))
(current-column))
(goto-char (car positions))
(+ (current-column) (smie-indent-offset 'args))))
(t
;; We've skipped to a previous arg on its own line: align.
(goto-char (car positions))
(current-column)))))))
(defun smie-indent-line ()
"Indent current line using the SMIE indentation engine."
(interactive)
(let* ((savep (point))
(indent (condition-case nil
(save-excursion
(forward-line 0)
(skip-chars-forward " \t")
(if (>= (point) savep) (setq savep nil))
(or (smie-indent-calculate) 0))
(error 0))))
(if (not (numberp indent))
;; If something funny is used (e.g. `noindent'), return it.
indent
(if (< indent 0) (setq indent 0)) ;Just in case.
(if savep
(save-excursion (indent-line-to indent))
(indent-line-to indent)))))
;;;###autoload
(defun smie-setup (op-levels indent-rules)
(set (make-local-variable 'smie-indent-rules) indent-rules)
(set (make-local-variable 'smie-op-levels) op-levels)
(set (make-local-variable 'indent-line-function) 'smie-indent-line))
(provide 'smie)
;;; smie.el ends here
|