-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathr-indent.el
1121 lines (975 loc) · 38.5 KB
/
r-indent.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
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; r-indent.el --- Indentation engine for r-mode
;;
;; Copyright (C) 2016 Lionel Henry, Vitalie Spinu
;;
;; Author: Lionel Henry <[email protected]>,
;; Created: 3 Jul 2016
;;
;; 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 2, 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.
;;
;; A copy of the GNU General Public License is available at
;; http://www.r-project.org/Licenses/
;;
;;; Commentary:
;;
;; API is not yet stable.
;;
;;; Code:
(require 'r-syntax)
(defgroup r-indent nil
"r-mode indentation."
:group 'r
:prefix "r-indent-"
:prefix "r-offset-")
(defcustom r-indent-offset 2
"Main indentation offset that is commonly inherited by other offsets.
See `r-style-alist' for all available offsets."
:group 'r-indent
:type 'integer)
(defcustom r-offset-arguments 'open-delim
"Indent for arguments of function calls or indexing brackets.
This variables has an effect only when the ( or [ are not
directly followed by a new line. See
`r-offset-arguments-newline' for indentation after closing
newline.
When set to `open-delim', arguments are indented relative to the
opening parenthesis of the closest function call:
object <- call(argument, other_call(argument,
other_argument))
When set to `prev-call', arguments are indented relative to the
closest function call:
object <- call(argument, other_call(argument,
other_argument))
When set to `prev-line', arguments are indented relative to the
preceding line:
object <- call(argument, other_call(argument,
other_argument))
This setting can also be set to a list containing the the offset
type and the offset size, such as `'(prev-call 2)'. Otherwise,
`r-indent-offset' is used as a default. See `r-style-alist'
for other offsets controlling indentation."
:group 'r-indent
:type '(choice (const open-delim)
(const prev-call)
(const prev-line)))
(defcustom r-offset-arguments-newline 'prev-call
"Indent of arguments when ( or [ is followed by a new line.
When set to `open-delim', arguments on a new line are indented
relative to the opening parenthesis of the closest function call:
object <- call(argument, other_call(
argument,
other_argument
))
When set to `prev-call', arguments on a new line are indented relative to
the closest function call:
object <- call(argument, other_call(
argument,
other_argument
))
You can control the details of indentation at `prev-call' with
`r-indent-from-lhs' and `r-indent-from-chain-start'.
When set to `prev-line', arguments on a new line are indented
relative to the preceding line:
object <- call(argument, other_call(
argument,
other_argument
))
This setting can also be set to a list containing the the offset
type and the offset size, such as `'(prev-call 2)'. Otherwise,
`r-indent-offset' is used as a default. See `r-style-alist'
for other offsets controlling indentation."
:group 'r-indent
:type '(choice (const open-delim)
(const prev-call)
(const prev-line)))
(defcustom r-offset-block 'prev-line
"Indentation for blocks. A block is usually declared with
braces but a statement wrapped in anonymous parentheses is also
considered a block. This offset can be either `prev-call',
`prev-line' or `open-delim'.
When set to `open-delim', blocks are indented relative to the
opening parenthesis of the closest function call:
call(argument, other_call(parameter = {
stuff
}, {
stuff
}))
call(argument, lapply(data, function(x) {
body
}))
When set to `prev-call', blocks are indented relative to the
closest function call:
call(argument, other_call(parameter = {
stuff
}, {
stuff
}))
call(argument, lapply(data, function(x) {
body
}))
You can control the details of indentation at `prev-call' with
`r-indent-from-lhs' and `r-indent-from-chain-start'.
When set to `prev-line', blocks are indented relative to the
preceding line:
call(argument, other_call(parameter = {
stuff
}, {
stuff
}))
call(argument, lapply(data, function(x) {
body
}))
This setting can also be set to a list containing the the offset
type and the offset size, such as `'(prev-call 2)'. Otherwise,
`r-indent-offset' is used as a default. See `r-style-alist'
for other offsets controlling indentation."
:group 'r-indent
:type '(choice (const open-delim)
(const prev-call)
(const prev-line)))
(defcustom r-offset-continued 'straight
"This setting controls indentation of continued statements, that is,
consecutive statements separated by operators.
When set to 'straight, continued statements are indented as follows:
object %>%
some_function() %>%
other_function()
When set to 'cascade:
object %>%
some_function() %>%
other_function()
The 'straight and 'cascade settings are actually equivalent to
'(straight . t) and '(cascade . t), where `t' represents the
base indent size. More generally, you can supply '(straight . N)
to control the size of indentation.
See `r-style-alist' for for an overview of ESS indentation."
:group 'r-indent
:type '(choice (const straight)
(const cascade)))
(defcustom r-align-nested-calls '("ifelse")
"List of strings declaring function calls for which
`r-offset-arguments-newline' should be ignored. These calls
will be vertically aligned instead. The default is `ifelse',
resulting in the following indentation for nested ifelse calls:
object <- ifelse(condition1, out1,
ifelse(condition2, out2, out3))
See `r-style-alist' for for an overview of ESS indentation."
:group 'r-indent
:type '(repeat string))
(defcustom r-align-arguments-in-calls '("function[ \t]*(")
"List of regexes specifying the calls where
`r-offset-arguments' should have no effect on function
declarations. The arguments of those calls will be aligned from
the opening parenthesis.
By default, function declarations are overridden. If for example
`r-offset-arguments' is set to `prev-line', then function calls
are normally indented as in:
some_function(argument1,
argument2, argument3
)
However, the parameters of function declarations will be
vertically aligned:
fun <- function(argument1,
argument2
argument3) {
body
}
See `r-style-alist' for further details."
:group 'r-indent
:type '(reapeat string))
(defcustom r-align-continuations-in-calls t
"If non-nil, indent code inside calls from the opening delimiter.
This produces the following indentation:
10 + (1 + 2 +
3 + 4)
object[variable1 +
variable2]
if (test1 || test2 ||
test3 || test4) {
any(test5 &
test6)
}
instead of
10 + (1 + 2 +
3 + 4)
object[variable1 +
variable2]
if (test1 || test2 ||
test3 || test4) {
any(test5 &
test6)
}
Definition operators (`<-', `=', `:=' and `~') still trigger an
indentation in all cases. Also, operators at top level and in
curly brackets are not affected by this setting and always induce
an offset:
{
var1 +
var2
}
See `r-style-alist' for for an overview of ESS indentation."
:group 'r-indent
:type 'boolean)
(defcustom r-align-blocks '(control-flow)
"List of block types for which `r-offset-blocks' should be
ignored. The overridden blocks are vertically aligned. The list
can contain either or both of the symbols `control-flow' and
`fun-decl'.
With `control-flow', if, else for and while blocks will always be
aligned vertically. With `fun-decl', the body of a function
declaration will always be aligned with the call to
`function'."
:group 'r-indent
:type '(repeat symbol))
(defcustom r-indent-from-lhs '(arguments fun-decl-opening)
"List of syntactic elements that should be indented from the
left-hand side of an assignment. The list accepts the symbol
`arguments' and `fun-decl-opening'.
For arguments, this setting only has an effect for offsets set to
`prev-call'. When set, this indentation is produced:
some_function(parameter = other_function(
argument
))
object <- some_function(
argument1,
argument2
)
instead of:
some_function(parameter = other_function(
argument
))
object <- some_function(
argument1,
argument2
)
`fun-decl-opening' refers to the opening curly following a function
declaration. Setting it produces:
object <-
function(argument)
{
body
}
instead of:
object <-
function(argument)
{
body
}
This is useful when (a) you have a long function name and want to
break a line after `<-' so that you have room to lay out the
arguments within `fill-column' characters; (b) you still want to
align the function body from the LHS to save horizontal space.
See `r-style-alist' for for an overview of ESS indentation."
:group 'r-indent
:type '(repeat symbol))
(defcustom r-indent-from-chain-start t
"When non-nil, chained calls will be treated as if they were
one call and indentation will start from the first one. This
setting only has an effect for offsets set to `prev-call' or
block offsets set to `opening-delim'.
If `nil':
some_function(other_function(
argument
))
If `t':
some_function(other_function(
argument
))
See `r-style-alist' for for an overview of ESS indentation."
:group 'r-indent
:type 'boolean)
(defcustom r-indent-with-fancy-comments t
"Non-nil means distiguish between #, ##, and ### for indentation.
See `r-style-alist' for for an overview of ESS indentation."
:group 'r-indent
:type 'boolean)
;;; Styles
(defcustom r-indent-style 'DEFAULT
"The default value of `ess-indent-style'.
See the variable `r-style-alist' for how these groups map onto
different settings for variables. DEFAULT style picks
default (aka global) values from the indentation variables."
:group 'r-indent
:type '(choice (const DEFAULT :tag "DEFAULT: Pick from global variables.")
(const ESS)
(const ESS> :tag "ESS with some settings shifted to the right.")
(const RStudio)
(const RStudio< :tag "RStudio with some settings shifted to the left.")))
(defvar r-indent-style-alist
'((DEFAULT
(r-indent-offset . (default-value 'r-indent-offset))
(r-offset-arguments . (default-value 'r-offset-arguments))
(r-offset-arguments-newline . (default-value 'r-offset-arguments-newline))
(r-offset-block . (default-value 'r-offset-block))
(r-offset-continued . (default-value 'r-offset-continued))
(r-align-nested-calls . (default-value 'r-align-nested-calls))
(r-align-arguments-in-calls . (default-value 'r-align-arguments-in-calls))
(r-align-continuations-in-calls . (default-value 'r-align-continuations-in-calls))
(r-align-blocks . (default-value 'r-align-blocks))
(r-indent-from-lhs . (default-value 'r-indent-from-lhs))
(r-indent-from-chain-start . (default-value 'r-indent-from-chain-start))
(r-indent-with-fancy-comments . (default-value 'r-indent-with-fancy-comments)))
(ESS
(r-indent-offset . (default-value 'r-indent-offset))
(r-offset-arguments . 'open-delim)
(r-offset-arguments-newline . 'prev-call)
(r-offset-block . 'prev-line)
(r-offset-continued . 'straight)
(r-align-nested-calls . '("ifelse"))
(r-align-arguments-in-calls . (default-value 'r-align-arguments-in-calls))
(r-align-continuations-in-calls . t)
(r-align-blocks . '(control-flow))
(r-indent-from-lhs . '(arguments fun-decl-opening))
(r-indent-from-chain-start . t)
(r-indent-with-fancy-comments . t))
(ESS>
(r-indent-offset . (default-value 'r-indent-offset))
(r-offset-arguments . 'open-delim)
(r-offset-arguments-newline . 'prev-call)
(r-offset-block . 'open-delim) ; <- differs from ESS
(r-offset-continued . 'straight)
(r-align-nested-calls . '("ifelse"))
(r-align-arguments-in-calls . (default-value 'r-align-arguments-in-calls))
(r-align-continuations-in-calls . t)
(r-align-blocks . '(control-flow))
(r-indent-from-lhs . '(arguments)) ; <- differs from ESS
(r-indent-from-chain-start . nil) ; <- differs from ESS
(r-indent-with-fancy-comments . t))
(RStudio
(r-indent-offset . (default-value 'r-indent-offset))
(r-offset-arguments . 'open-delim)
(r-offset-arguments-newline . 'prev-line)
(r-offset-block . 'prev-line)
(r-offset-continued . 'straight)
(r-align-nested-calls . nil)
(r-align-arguments-in-calls . (default-value 'r-align-arguments-in-calls))
(r-align-continuations-in-calls . nil)
(r-align-blocks . nil)
(r-indent-from-lhs . '(arguments))
(r-indent-from-chain-start . t)
(r-indent-with-fancy-comments . nil))
(RStudio<
(r-indent-offset . (default-value 'r-indent-offset))
(r-offset-arguments . 'prev-line) ; <- differs from RStudio
(r-offset-arguments-newline . 'prev-line)
(r-offset-block . 'prev-line)
(r-offset-continued . 'straight)
(r-align-nested-calls . nil) ; <- differs from RStudio
(r-align-arguments-in-calls . (default-value 'r-align-arguments-in-calls))
(r-align-continuations-in-calls . nil)
(r-align-blocks . nil)
(r-indent-from-lhs . '(arguments))
(r-indent-from-chain-start . t)
(r-indent-with-fancy-comments . nil)))
"Predefined formatting styles for ESS code. Use
`r-default-style' to apply a style in all R buffers. The values
of all styles except OWN are fixed. To change the value of
variables in the OWN group, customize the variable
`r-own-style-list'. DEFAULT style picks default (aka global)
values from ESS indentation variables. In addition, ESS provides
many indentation styles, the most important being the RRR and the
RStudio variants.
RRR is the common R style that adheres closely to R internal
standards. RRR+ is the same except it also aligns blocks in
function calls with the opening delimiter, producing more
indentation. The C++ style (named like this for historical
reasons rather than any resemblance to existing C++ indentation
schemes) is halfway between these two styles and indent block
arguments from the start of the surrounding function's name.
The RStudio style closely mimics the indentation of the RStudio
editor. RStudio- is the same except it does not align arguments
in function calls, which corresponds to the settings of some
RStudio users.
ESS indentation is fully specified by the following offsets and
variables. See the documentation of these variables for examples.
Offsets:
- `r-indent-offset': main offset inherited by other settings
- `r-offset-arguments': offset type for function and bracket
arguments
- `r-offset-arguments-newline': offset type of arguments
when ( or [ is followed by a new line.
- `r-offset-block': offset type for brace and anonymous
parenthesis blocks
- `r-offset-continued': offset type for continuation lines in
multiline statements
Overrides (implies vertical alignment):
- `r-align-nested-calls': functions whose nested calls
should be aligned.
- `r-align-arguments-in-calls': calls where
`r-offset-arguments' should be ignored
- `r-align-continuations-in-calls': whether to ignore
`r-offset-continued' in calls.
- `r-align-blocks': whether to ignore `r-offset-blocks' for
function declarations or control flow statements.
Control variables:
- `r-indent-from-lhs': whether to indent arguments from
left-hand side of an assignment or parameter declaration.
- `r-indent-from-chain-start': whether to indent arguments from
the first of several consecutive calls.
- `r-indent-with-fancy-comments': whether to indent #, ## and
### comments distinctly.")
(defvar r-set-indent-style--history nil)
(defun r-set-indent-style (&optional style-name globalp)
"Set R indentation style in current buffer to STYLE.
If GLOBALP is non-nil, set default values of the variables."
(interactive)
(let* ((styles (mapcar 'car r-indent-style-alist))
(style-name (or style-name
(intern (completing-read "Indentation style: "
styles nil t nil
'r-set-indent-style--history
(car r-set-indent-style--history)))))
(style (cdr (assoc style-name r-indent-style-alist))))
(unless style
(error (format "Style `%s' is not defined in `r-indent-style-alist'"
style-name)))
(r-set-variables style globalp)
style-name))
;;; Engine
(defun r-indent-line ()
"Indent current line as ESS R code.
Return the amount the indentation changed by."
(let ((indent (r-calculate-indent nil))
beg shift-amt
(case-fold-search nil)
(pos (- (point-max) (point))))
(beginning-of-line)
(setq beg (point))
(skip-chars-forward " \t")
(setq shift-amt (- indent (current-column)))
(if (zerop shift-amt)
(if (> (- (point-max) pos) (point))
(goto-char (- (point-max) pos)))
(delete-region beg (point))
(indent-to indent)
;; If initial point was within line's indentation,
;; position after the indentation.
;; Else stay at same point in text.
(when (> (- (point-max) pos) (point))
(goto-char (- (point-max) pos))))
shift-amt))
(defun r-indent-exp ()
(save-excursion
(when current-prefix-arg
(r-climb-to-top-level))
(let* ((bounds (r-continuations-bounds))
(end (cadr bounds))
(beg (if current-prefix-arg
(car bounds)
(forward-line)
(point))))
(indent-region beg end))))
(defun r-indent-call (&optional start)
(save-excursion
(when (r-escape-calls)
(setq start (or start (point)))
(skip-chars-forward "^[(")
(forward-char)
(r-up-list)
(indent-region start (point)))))
(defun r-offset (offset)
(setq offset (eval (intern (concat "r-offset-" (symbol-name offset)))))
(when (and (not (eq offset nil))
(listp offset)
(or (numberp (cadr offset))
(eq (cadr offset) t)
(error "Malformed offset")))
(setq offset (cadr offset)))
(cond ((numberp offset)
offset)
((null offset)
0)
(t
r-indent-offset)))
(defun r-offset-type (offset)
(setq offset (eval (intern (concat "r-offset-" (symbol-name offset)))))
(if (listp offset)
(car offset)
offset))
(defun r-overridden-blocks ()
(append (when (memq 'fun-decl r-align-blocks)
(list (car r-prefixed-block-patterns)))
(when (memq 'control-flow r-align-blocks)
(append (cdr r-prefixed-block-patterns)
'("}?[ \t]*else")))))
(defun r-calculate-indent (&optional parse-start)
"Return appropriate indentation for current line as ESS code.
In usual case returns an integer: the column to indent to.
Returns nil if line starts inside a string, t if in a comment."
(save-excursion
(beginning-of-line)
(let* ((indent-point (point))
(state (syntax-ppss))
(containing-sexp (cadr state))
(prev-containing-sexp (car (last (butlast (nth 9 state))))))
(r-back-to-indentation)
(cond
;; Strings
((r-within-string-p state)
(current-indentation))
;; Comments
((r-calculate-indent--comments))
;; Indentation of commas
((looking-at ",")
(r-calculate-indent--comma))
;; Arguments: Closing
((r-call-closing-p)
(r-calculate-indent--call-closing-delim))
;; Block: Contents (easy cases)
((r-calculate-indent--block-relatively))
;; Block: Prefixed block
((r-calculate-indent--prefixed-block-curly))
;; Continuations
((r-calculate-indent--continued))
;; Block: Overridden contents
((r-calculate-indent--aligned-block))
;; Block: Opening
((r-block-opening-p)
(r-calculate-indent--block-opening))
;; Bare line
((and (null containing-sexp)
(not (r-unbraced-block-p)))
0)
;; Block: Closing
((r-block-closing-p)
(r-calculate-indent--block 0))
;; Block: Contents
((r-block-p)
(r-calculate-indent--block))
;; Arguments: Nested calls override
((r-calculate-indent--nested-calls))
;; Arguments: Contents
(t
(r-calculate-indent--args))))))
(defun r-calculate-indent--comments ()
(when r-indent-with-fancy-comments
(cond
;; ### or #!
((or (looking-at "###")
(and (looking-at "#!")
(= 1 (line-number-at-pos))))
0)
;; Single # comment
((looking-at "#[^#']")
comment-column))))
(defun r-calculate-indent--comma ()
(when (r-within-call-p)
(let ((indent (save-excursion
(r-calculate-indent--args)))
(unindent (progn (skip-chars-forward " \t")
;; return number of skiped chars
(skip-chars-forward ", \t"))))
(- indent unindent))))
(defun r-calculate-indent--call-closing-delim ()
(cond ((save-excursion
(r-skip-blanks-backward t)
(eq (char-before) ?,))
(r-calculate-indent--args nil))
((save-excursion
(and (r-ahead-operator-p)
(or (r-ahead-definition-op-p)
(not r-align-continuations-in-calls))))
(r-calculate-indent--continued))
(t
(r-calculate-indent--args 0))))
(defun r-calculate-indent--block-opening ()
(cond
;; Block is an argument in a function call
((when containing-sexp
(r-at-containing-sexp
(r-behind-call-opening "[[(]")))
(r-calculate-indent--block 0))
;; Top-level block
((null containing-sexp) 0)
;; Block is embedded in another block
((r-at-containing-sexp
(equal (char-after) ?\{)
(+ (current-indentation)
(r-offset 'block))))))
(defun r-calculate-indent--aligned-block ()
;; Check for `else' opening
(if (and (memq 'control-flow r-align-blocks)
(looking-at "else\\b")
(r-climb-if-else))
(progn
(when (looking-at "else\\b")
(r-skip-curly-backward))
(current-column))
;; Check for braced and unbraced blocks
(r-save-excursion-when-nil
(let ((offset (if (looking-at "[{})]")
0 (r-offset 'block))))
(when (and (cond
;; Unbraced blocks
((r-climb-block-prefix))
;; Braced blocks
(containing-sexp
(when (r-at-containing-sexp
(looking-at "{"))
(r-escape-prefixed-block))))
(some 'looking-at (r-overridden-blocks)))
(+ (current-column) offset))))))
(defun r-calculate-indent--block-relatively ()
(r-save-excursion-when-nil
(let ((offset (if (looking-at "[})]") 0 (r-offset 'block)))
(start-line (line-number-at-pos)))
(cond
;; Braceless block continuations: only when not in a call
((r-save-excursion-when-nil
(and (not (looking-at "{"))
(r-goto-char (r-unbraced-block-p))
(not (looking-at "function\\b"))
(or (null containing-sexp)
(r-at-containing-sexp
(not (looking-at "("))))))
(r-maybe-climb-broken-else 'same-line)
(r-skip-curly-backward)
(+ (current-column)
(r-offset 'block)))
;; Don't indent relatively other continuations
((r-ahead-continuation-p)
nil)
;; If a block already contains an indented line, we can indent
;; relatively from that first line
((r-save-excursion-when-nil
(and (not (looking-at "}"))
containing-sexp
(goto-char containing-sexp)
(looking-at "{")
(progn
(forward-line)
(r-back-to-indentation)
(/= (line-number-at-pos) start-line))
(not (looking-at "[ \t]*\\(#\\|$\\)"))
(save-excursion
(or (r-jump-expression)
(r-jump-continuations))
(< (line-number-at-pos) start-line))))
(current-column))
;; If a block is not part of a call, we can indent relatively
;; from the opening {. First check that enclosing { is first
;; thing on line
((and containing-sexp
(not (r-unbraced-block-p))
(goto-char containing-sexp)
(r-block-opening-p)
(equal (point) (save-excursion
(r-back-to-indentation)
(point))))
(+ (current-column) offset))))))
(defun r-arg-block-p ()
(unless (or (null containing-sexp)
;; Unbraced blocks in a { block are not arg blocks
(and (r-unbraced-block-p)
(r-at-containing-sexp
(looking-at "{"))))
(cond
;; Unbraced body
((r-at-indent-point
(and (r-unbraced-block-p)
(goto-char containing-sexp)
(r-behind-call-opening "[[(]")))
'body)
;; Indentation of opening brace as argument
((r-at-containing-sexp
(r-behind-call-opening "[[(]"))
'opening)
;; Indentation of body or closing brace as argument
((r-at-containing-sexp
(and (or (looking-at "{")
(r-behind-block-paren-p))
prev-containing-sexp
(goto-char prev-containing-sexp)
(r-behind-call-opening "[[(]")))
'body))))
(defun r-calculate-indent--block (&optional offset)
(let ((arg-block (r-arg-block-p)))
(cond (arg-block
(r-calculate-indent--arg-block offset arg-block))
(t
;; Block is not part of an arguments list. Climb over any
;; block opening (function declaration, etc) to indent from
;; starting indentation.
(or (r-climb-block-prefix)
(and (goto-char containing-sexp)
(r-climb-block-prefix)))
(+ (current-indentation) (or offset (r-offset 'block)))))))
(defun r-calculate-indent--arg-block (offset arg-block)
(let* ((block-type (cond ((or (r-at-containing-sexp
(and (eq arg-block 'body)
(r-climb-block-prefix "function")))
(r-at-indent-point
(and (eq arg-block 'opening)
(r-backward-sexp 2)
(looking-at "function\\b"))))
'fun-decl)
((r-at-indent-point
(r-unbraced-block-p))
'unbraced)
((r-at-containing-sexp
(not (r-ahead-attached-name-p)))
'bare-block)
(t)))
(call-pos (if (and (not (eq block-type 'unbraced))
(not (eq arg-block 'opening)))
(goto-char prev-containing-sexp)
(prog1 containing-sexp
(goto-char indent-point)))))
(r-calculate-indent--args offset (r-offset-type 'block)
call-pos indent-point block-type)))
;; This function is currently the speed bottleneck of the indentation
;; engine. This is due to the need to call (r-maximum-args-indent)
;; to check if some previous arguments have been pushed off from their
;; natural indentation: we need to check the whole call. This is very
;; inefficient especially when indenting a region containing a large
;; function call (e.g. some dplyr's data cleaning code). Should be
;; solved by implementing a cache as in (syntax-ppss), though it's
;; probably not worth the work.
(defun r-calculate-indent--args (&optional offset type call-pos to block)
(let* ((call-pos (or call-pos containing-sexp))
(max-col (prog1 (unless (eq type 'prev-line)
(r-maximum-args-indent call-pos to))
(goto-char call-pos)))
(override (and r-align-arguments-in-calls
(save-excursion
(r-climb-object)
(some 'looking-at r-align-arguments-in-calls))))
(type-sym (cond (block 'block)
((looking-at "[[:blank:]]*[([][[:blank:]]*\\($\\|#\\)")
'arguments-newline)
(t 'arguments)))
(type (or type
(and override 'open-delim)
(r-offset-type type-sym)))
(offset (or offset
(and (not block) (eq type 'open-delim) 0)
(r-offset type-sym)))
(indent
(cond
;; Indent from opening delimiter
((eq type 'open-delim)
(r-calculate-indent--args-open-delim))
;; Indent from attached name
((eq type 'prev-call)
(r-calculate-indent--args-prev-call))
;; Indent from previous line indentation
((eq type 'prev-line)
(r-calculate-indent--args-prev-line))
(t
(error "Malformed offset")))))
(if max-col
(r-adjust-argument-indent indent offset max-col block)
(+ indent offset))))
(defun r-calculate-indent--args-open-delim ()
(forward-char)
(current-column))
(defun r-calculate-indent--args-prev-call ()
;; Handle brackets chains such as ][ (cf data.table)
(r-climb-chained-delims)
;; Handle call chains
(if r-indent-from-chain-start
(while (and (r-backward-sexp)
(when (looking-back "[[(][ \t,]*" (line-beginning-position))
(goto-char (match-beginning 0)))))
(r-backward-sexp))
(when r-indent-from-lhs
(r-climb-lhs))
(if (and nil
(eq block 'fun-decl)
(not (eq arg-block 'opening))
(not (eq (r-offset-type type-sym) 'open-delim)))
(+ (r-offset 'block) (current-column))
(current-column)))
(defun r-calculate-indent--args-prev-line ()
(r-at-indent-point
(cond
;; Closing delimiters are actually not indented at
;; prev-line, but at opening-line
((looking-at "[]})]")
(r-up-list -1)
(when (looking-at "{")
(r-climb-block-prefix))
(current-indentation))
;; Function blocks need special treatment
((and (eq type 'prev-line)
(eq block 'fun-decl))
(goto-char containing-sexp)
(r-climb-block-prefix)
(current-indentation))
;; Regular case
(t
;; Find next non-empty line to indent from
(while (and (= (forward-line -1) 0)
(looking-at "[ \t]*\\($\\|#\\)")))
(goto-char (r-line-end-position))
;; Climb relevant structures
(unless (r-climb-block-prefix)
(when (eq (char-before) ?,)
(forward-char -1))
(r-climb-expression)
(r-climb-continuations))
;; The following ensures that only the first line
;; counts. Otherwise consecutive statements would get
;; increasingly more indented.
(when (and block
containing-sexp
(not (eq block 'unbraced))
(save-excursion
(/= (line-number-at-pos)
(progn (goto-char containing-sexp)
(line-number-at-pos)))))
(setq offset 0))
(current-indentation)))))
;; Indentation of arguments needs to keep track of how previous
;; arguments are indented. If one of those has a smaller indentation,
;; we push off the current line from its natural indentation. For
;; block arguments, we still need to push off this column so we ignore
;; it.
(defun r-adjust-argument-indent (base offset max-col push)
(if push
(+ (min base max-col) offset)
(min (+ base offset) max-col)))
;; When previous arguments are shifted to the left (can happen in
;; several situations) compared to their natural indentation, the
;; following lines should not get indented past them. The following
;; function checks the minimum indentation for all arguments of the
;; current function call or bracket indexing.
(defun r-maximum-args-indent (&optional from to)
(let* ((to (or to (point)))
(to-line (line-number-at-pos to))
(from-line (progn
(goto-char (1+ (or from containing-sexp)))
(line-number-at-pos)))
(prev-pos (1- (point)))
max-col)
(while (< (line-number-at-pos) to-line)
(forward-line)
(r-back-to-indentation)
;; Ignore the line with the function call, the line to be
;; indented, and empty lines.
(unless (or (>= (line-number-at-pos) to-line)
(looking-at "[ \t]*\\($\\|#\\)"))
(let ((indent (cond