-
Notifications
You must be signed in to change notification settings - Fork 31
/
dtrt-indent.el
1141 lines (1032 loc) · 47.3 KB
/
dtrt-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
;;; dtrt-indent.el --- Adapt to foreign indentation offsets
;; Copyright (C) 2003, 2007, 2008 Julian Scheid
;; Copyright (C) 2014-2024 Reuben Thomas
;; Author: Julian Scheid <[email protected]>
;; Maintainer: Reuben Thomas <[email protected]>
;; Version: 1.19
;; Keywords: convenience files languages c
;; 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.
;; You should have received a copy of the GNU General Public License
;; along with this software; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301 USA
;;; Commentary:
;; A minor mode that guesses the indentation offset and
;; `indent-tabs-mode' originally used for creating source code files and
;; transparently adjusts the corresponding settings in Emacs, making it
;; more convenient to edit others' files.
;;
;; This hooks into many major modes - c-mode, java-mode and ruby-mode, to
;; name but a few - and makes an educated guess on which offset is
;; appropriate by analyzing indentation levels in the file. Modes that have
;; their own indentation offset guessing, such as python-mode, are not dealt
;; with. In modes based on SMIE, dtrt-indent delegates to smie-config-guess.
;;
;; Heuristics are used to estimate the proper indentation offset and
;; therefore this system is not infallible, however adjustments will
;; only be made if the guess is considered reliable. This way it
;; should leave you off no worse than before.
;;
;; To install, M-x customize-variable dtrt-indent-global-mode, and turn on
;; "Dtrt Indent Global Mode".
;;
;; The default settings have been carefully chosen and tested to work
;; reliably on a wide range of source files. However, if it doesn't work
;; for you they can be fine tuned using M-x customize-group dtrt-indent.
;; You can use `dtrt-indent-diagnosis' to see dtrt-indent's
;; measurements, `dtrt-indent-highlight' to show indentation that was
;; considered,and `dtrt-indent-undo' to undo any changes it makes.
;;
;;
;; Heuristics
;;
;; We now describe the inner workings of dtrt-indent and how it arrives
;; at a conclusion on whether or not to change the indentation settings,
;; and to which value.
;;
;; Lines Analyzed
;;
;; In order to limit performance degradation due to the analysis, only a
;; fixed number of lines will be analyzed. If the size of the file is
;; less than this number of lines, the whole file will be analyzed;
;; otherwise, the given number of lines at the beginning of the file are
;; analyzed.
;;
;; Certain lines are ignored during analysis:
;;
;; * Empty lines.
;; * Lines that are not indented (indentation offset 0).
;; * Lines that are the continuation of a multi-line comment or a
;; multi-line statement or expression.
;; * Lines that only contain a single character can be ignored; by
;; default, however, they are included.
;;
;; If, after ignoring any lines that are not eligible, the number of
;; relevant lines is smaller than a given threshold then the file is
;; treated as not fit for analysis and no guess will be made.
;;
;; Configuration settings used at this stage:
;; `dtrt-indent-min-relevant-lines', `dtrt-indent-max-lines',
;; `dtrt-indent-ignore-single-chars-flag'
;;
;; Histogram Generation
;;
;; For the remaining lines - those eligible within the fixed range - a
;; histogram is generated. The histogram informs dtrt-indent about how
;; many lines are indented with one space, how many with two spaces, how
;; many with three spaces, etc.
;;
;; Offset Assessment
;;
;; Using the histogram, dtrt-indent determines for each of the potential
;; indentation offsets (by default, 2 through 8) how many lines are
;; indented with a multiple of that offset.
;;
;; Offsets for which the histogram doesn't contain enough distinct
;; indentations might be ignored; by default, however, a single
;; indentation per offset is accepted.
;;
;; After this step, dtrt-indent has a map of probabilities for each of
;; the potential offsets.
;;
;; Configuration settings used at this stage: `dtrt-indent-min-offset',
;; `dtrt-indent-max-offset', `dtrt-indent-min-matching-indentations'
;;
;; Offset Merging
;;
;; As a next step, offsets that are a factor of another offset with
;; similar probability are discarded; this is necessary because in a file
;; that has been indented with, say, 4 spaces per level, 2 spaces per
;; level could otherwise be wrongly guessed.
;;
;; Configuration settings used at this stage:
;; `dtrt-indent-max-merge-deviation'
;;
;; Final Evaluation
;;
;; Finally, dtrt-indent looks at the highest probability of all
;; potential offsets; if that probablity is below a given threshold, the
;; guess is deemed unreliable and no settings are changed.
;;
;; If the analysis yielded a best guess that exceeds the absolute
;; threshold, that guess is deemed reliable and the indentation setting
;; will be modified.
;;
;; Configuration settings used at this stage: `dtrt-indent-min-quality'.
;;
;; `indent-tabs-mode' Setting
;;
;; For determining hard vs. soft tabs, dtrt-indent counts the number of
;; lines out of the eligible lines in the fixed segment that are
;; indented using hard tabs, and the number of lines indented using
;; spaces. If either count is significantly higher than the other count,
;; `indent-tabs-mode' will be modified.
;;
;; Configuration settings used at this stage:
;; `dtrt-indent-min-soft-tab-superiority',
;; `dtrt-indent-min-hard-tab-superiority'
;;
;; Files not touched by dtrt-indent:
;;
;; - Files that specify the corresponding variable
;; (e.g. c-basic-offset) as a File Variable.
;;
;; - Files that specify dtrt-indent-mode: 0 as a File Variable.
;;
;; - Files with a major mode that dtrt-indent doesn't hook into.
;;
;; - Files for which the indentation offset cannot be guessed
;; reliably.
;;
;; - Files for which `dtrt-indent-explicit-offset' is true; this can be
;; - used in `.dir-locals.el' files, for example.
;;
;; Limitations:
;;
;; - dtrt-indent can't deal well with files that use variable
;; indentation offsets, e.g. files that use varying indentation
;; based on the outer construct.
;;
;; - dtrt-indent currently only supports a limited number of languages
;; (major-modes).
;;
;; - dtrt-indent only guesses the indendation offset, not the
;; indentation style. For instance, it does not detect whether a
;; C-like file uses hanging braces or not.
;;
;; - dtrt-indent can't deal well with files that mix hard tabs with
;; - spaces for indentation.
;;
;; TODO:
;;
;; - verbose and diagnostics messages
;; - make sure variable documentation match their function
;; - make sure defaults are sensible
;; - bulk (real world) tests
;; - functional tests
;; - unit tests
;;; Code:
;;;###autoload
(define-minor-mode dtrt-indent-mode
"Toggle dtrt-indent mode.
With no argument, this command toggles the mode. Non-null prefix
argument turns on the mode. Null prefix argument turns off the
mode.
When dtrt-indent mode is enabled, the proper indentation offset
and `indent-tabs-mode' will be guessed for newly opened files and
adjusted transparently."
:lighter dtrt-indent-lighter
:group 'dtrt-indent
(if dtrt-indent-mode
(if (and (featurep 'smie) (not (null smie-grammar)) (not (eq smie-grammar 'unset)))
(progn
(when (null smie-config--buffer-local) (smie-config-guess))
(when dtrt-indent-run-after-smie
(dtrt-indent-try-set-offset)))
(dtrt-indent-try-set-offset))
(dtrt-indent-undo)))
;;;###autoload
(define-globalized-minor-mode dtrt-indent-global-mode dtrt-indent-mode
(lambda ()
;; javascript-mode is an alias for js-mode, so derived-mode-p does not
;; detect it is derived from 'prog-mode (Emacs bug #46331: remove once
;; Emacs >= 28.1 can be assumed)
(when (derived-mode-p 'prog-mode 'text-mode 'javascript-mode)
(dtrt-indent-mode))))
(defvar dtrt-indent-language-syntax-table
'((c/c++/java ("\"" 0 "\"" nil "\\\\.")
("'" 0 "'" nil "\\\\.")
("/\\*" 0 "\\*/" nil)
("//" 0 "$" nil)
("(" 0 ")" t)
("\\[" 0 "\\]" t))
;; Same as c/c++/java but ignore function call arguments, to cope with
;; modules defined entirely within a function call, e.g. AMD style
(javascript ("\"" 0 "\"" nil "\\\\.")
("'" 0 "'" nil "\\\\.")
("/\\*" 0 "\\*/" nil)
("//" 0 "$" nil)
("/\\(.*\\)" 1 "\\1/" nil)
("\\[" 0 "\\]" t))
(perl ("\"" 0 "\"" nil "\\\\.")
("'" 0 "'" nil "\\\\.")
("/" 0 "/" nil "\\\\.")
("#" 0 "$" nil)
("(" 0 ")" t)
("\\[" 0 "\\]" t))
(lua ("\"" 0 "\"" nil "\\\\.")
("'" 0 "'" nil "\\\\.")
("--" 0 "$" nil)
("(" 0 ")" t)
("\\[\\(=+\\)\\[" 1 "\\]\\1\\]" nil)
("{" 0 "}" t))
(ruby ("\"" 0 "\"" nil "\\\\.")
("'" 0 "'" nil "\\\\.")
("/" 0 "/" nil "\\\\.")
("#" 0 "$" nil)
("(" 0 ")" t)
("\\[" 0 "\\]" t)
("{" 0 "}" t))
(ada ("\"" 0 "\"" nil "\\\\.")
("--" 0 "$" nil)
("(" 0 ")" t)
("\\[" 0 "\\]" t)
("{" 0 "}" t))
;; The standard Erlang style is to indent code inside a block
;; relative to the token that opened the block. For example:
;;
;; bar(X) ->
;; {A, B} = case X of
;; true ->
;; {alpha, [beta,
;; gamma]}
;; end.
;;
;; Thus it is best to ignore the code inside these block
;; constructs when determining the indent offset.
(erlang ("\"" 0 "\"" nil "\\\\.")
;; next pattern avoids error on git merge conflict lines
("[<][<][<]" 0 "$" nil)
("[<][<]" 0 "[>][>]" nil)
("%" 0 "$" nil)
("^-" 0 "\\." nil)
("{" 0 "}" t)
("\\[" 0 "\\]" t)
("(" 0 ")" t)
("\\_<\\(?:begin\\|case\\|fun\\|if\\|receive\\|try\\)\\_>"
0 "\\_<end\\_>" t))
(css ("\"" 0 "\"" nil "\\\\.")
("'" 0 "'" nil "\\\\.")
("/\\*" 0 "\\*/" nil))
(sgml ("[<]!\\[(CDATA|IGNORE|RCDATA)\\["
0 "\\]\\][>]" nil)
("[<]!--" 0 "[^-]--[>]" nil))
(cmake ("\"" 0 "\"" nil "\\\\.")
("#\\[\\(=*\\)\\[" 1 "\\]\\1\\]" nil)
("#" 0 "$" nil))
(default ("\"" 0 "\"" nil "\\\\.")))
"A list of syntax tables for supported languages.
Each entry in this list is of the form
(SYMBOL SYNTAX-ENTRY [SYNTAX-ENTRY [...]])
where SYMBOL is used to identify the language in
question and SYNTAX-ENTRY is in format
(BEGIN-REGEXP NUM-GROUPS END-REGEXP RECURSIVE-P SKIP-REGEXP)
BEGIN-REGEXP is a regular expression matching the beginning of
the syntax construct in question. NUM-GROUPS indicates how many
groups there are in BEGIN-REGEXP to be substituted in END-REGEXP.
END-REGEXP is a regular expression matching the end of the syntax
construct in question. It can refer back to one group in
BEGIN-REGEXP using \1. Currently only one group is supported (\2
cannot be used.)
RECURSIVE-P indicates whether other syntax constructs can be
nested within this construct. RECURSIVE-P is usually false for
comment and constant constructs, such as strings, and usually
true for bracketing constructs.
SKIP-REGEXP is a regular expression that, if matches, means that
END-REGEXP is ignored for that location. This can be used to
prevent an escaped quote from being interpreted as the closing
quote, for example.")
(defvar dtrt-indent-hook-mapping-list
;; Mode Syntax Variable
'((c-mode c/c++/java c-basic-offset) ; C
(c++-mode c/c++/java c-basic-offset) ; C++
(d-mode c/c++/java c-basic-offset) ; D
(java-mode c/c++/java c-basic-offset) ; Java
(jde-mode c/c++/java c-basic-offset) ; Java (JDE)
(js-mode javascript js-indent-level) ; JavaScript
(js-json-mode javascript js-indent-level) ; JSON
(js2-mode javascript js2-basic-offset) ; JavaScript-IDE
(js3-mode javascript js3-indent-level) ; JavaScript-IDE
(json-mode javascript js-indent-level) ; JSON
(lua-mode lua lua-indent-level) ; Lua
(objc-mode c/c++/java c-basic-offset) ; Objective C
(php-mode c/c++/java c-basic-offset) ; PHP
(perl-mode perl perl-indent-level) ; Perl
(cperl-mode perl cperl-indent-level) ; Perl
(raku-mode perl raku-indent-offset) ; Perl6/Raku
(erlang-mode erlang erlang-indent-level) ; Erlang
(ada-mode ada ada-indent) ; Ada
(sgml-mode sgml sgml-basic-offset) ; SGML
(nxml-mode sgml nxml-child-indent) ; XML
(web-mode sgml (web-mode-markup-indent-offset
web-mode-code-indent-offset
web-mode-sql-indent-offset
web-mode-css-indent-offset)) ; HTML
(pascal-mode pascal pascal-indent-level) ; Pascal
(typescript-mode javascript typescript-indent-level) ; Typescript
(protobuf-mode c/c++/java c-basic-offset) ; Protobuf
(plantuml-mode default plantuml-indent-level) ; PlantUML
(pug-mode default pug-tab-width) ; Pug
(cmake-mode cmake cmake-tab-width) ; CMake
(xquery-mode sgml xquery-mode-indent-width) ; XQuery
(vhdl-mode default vhdl-basic-offset) ; VHDL
(groovy-mode c/c++/java (groovy-indent-offset
tab-width)) ; Groovy
(yaml-mode default (yaml-indent-offset
tab-width)) ; YAML
(swift-mode c/c++/java swift-mode:basic-offset) ; Swift
;; Modes that use SMIE if available
(sh-mode default sh-basic-offset) ; Shell Script
(ruby-mode ruby ruby-indent-level) ; Ruby
(enh-ruby-mode ruby enh-ruby-indent-level); Ruby
(crystal-mode ruby crystal-indent-level) ; Crystal (Ruby)
(css-mode css css-indent-offset) ; CSS
(rust-mode c/c++/java rust-indent-offset) ; Rust
(rustic-mode c/c++/java rustic-indent-offset) ; Rust
(scala-mode c/c++/java scala-indent:step) ; Scala
;; modes with treesitter enabled
(ada-ts-mode ada ada-ts-mode-indent-offset)
(c-ts-mode c/c++/java c-ts-mode-indent-offset)
(c++-ts-mode c/c++/java c-ts-mode-indent-offset)
(go-ts-mode c/c++/java go-ts-mode-indent-offset)
(gpr-ts-mode ada gpr-ts-mode-indent-offset)
(java-ts-mode c/c++/java java-ts-mode-indent-offset)
(rust-ts-mode c/c++/java rust-ts-mode-indent-offset)
(json-ts-mode javascript json-ts-mode-indent-offset)
(cmake-ts-mode cmake cmake-ts-mode-indent-offset)
(typescript-ts-base-mode javascript typescript-ts-mode-indent-offset)
(ursa-ts-mode c/c++/java ursa-ts-mode-indent-offset)
(default default standard-indent)) ; default fallback
"A mapping from hook variables to language types.")
;;-----------------------------------------------------------------
;; Customization Definitions:
(defgroup dtrt-indent nil
"Transparently adapt to foreign indentation offsets."
:version "22.0"
:group 'files
:group 'convenience)
;;;###autoload
(defcustom dtrt-indent-mode nil
"Toggle adaptive indentation mode.
Setting this variable directly does not take effect;
use either \\[customize] or the function `dtrt-indent-mode'."
:set #'(lambda (symbol value) (funcall symbol (or value 0)))
:initialize 'custom-initialize-default
:version "22.0"
:type 'boolean
:group 'dtrt-indent
:require 'dtrt-indent)
(defcustom dtrt-indent-lighter " dtrt-indent"
"Mode line lighter for the dtrt-indent minor mode.
If the value is nil, no lighter is displayed."
:type '(choice string
(const :tag "None" nil)))
(defcustom dtrt-indent-verbosity 1
"*Verbosity level.
How much dtrt-indent should tell you about what it's doing. If
this is 1, the default, dtrt-indent will only let you know when
it adjusts the indentation offset but will be silent otherwise.
By setting this to 2 it will also tell you why it didn't adjust
the offset. You might want to try this as a first measure if
you're unhappy with dtrt-indent's actions. A setting of 3 will
output lots of diagnostic information. Finally, a setting of 0
keeps dtrt-indent of ever outputting anything."
:type '(choice (const :tag "Silent" 0)
(const :tag "Normal" 1)
(const :tag "Verbose" 2)
(const :tag "Diagnostics" 3))
:tag "Verbosity"
:group 'dtrt-indent)
(defcustom dtrt-indent-require-confirmation-flag nil
"*Non-nil means to ask for confirmation before making adjustments.
Whether dtrt-indent asks for confirmation whenever it is about to
make any adjustments. By default, adjustments are made without
your explicit consent because dtrt-indent is already quite
conservative and tries to 'do the right thing', adjustments can
be undone easily, and they aren't harmful in the first place.
However, if you feel like it's doing things behind your back
you should enable this setting."
:type 'boolean
:tag "Require Confirmation"
:group 'dtrt-indent)
(defcustom dtrt-indent-hook-generic-mapping-list
;; Key variable Value variable
'((evil-mode evil-shift-width)) ; evil
"A mapping from hook variables to indentation variables.
For each true key variable, its value variable is set to the same
indentation offset as the variable in `dtrt-indent-hook-mapping-list'
(e.g., `c-basic-offset'). Every pair in the list is processed. To
disable processing of any one pair, remove the pair from the list.
Processing the list obeys `dtrt-indent-require-confirmation-flag'.
The key can be any variable. This list is used for cases such as when
a minor-mode defines a variable to control its own indentation
functionality (e.g. `evil-mode' using `evil-shift-width'), so the
value variable must updated in addition to the syntax indentation
variable."
:type '(alist :key-type variable
:value-type (group variable))
:group 'dtrt-indent)
(defcustom dtrt-indent-run-after-smie nil
"*Non-nil means to run dtrt-indent even in modes using SMIE.
Normally, dtrt-indent will detect SMIE-based modes and delegate
to `smie-config-guess'. However, dtrt-indent configures some
variables that SMIE does not (e.g. the contents of
`dtrt-indent-hook-generic-mapping-list'), so you may want to run
dtrt-indent even in SMIE-based modes. You can do so by enabling
this setting."
:type 'boolean
:tag "Run dtrt-indent After SMIE"
:group 'dtrt-indent)
(defcustom dtrt-indent-min-relevant-lines 2
"*Minimum number of relevant lines required for a guess to be made.
This check is in place because with a very low number of lines
that can be analyzed the chances of a wrong guess are rather
high because the sample size is so small. If you are getting
false positives with small files - i.e. the wrong offset is guessed
- you might want to increase this setting. On the other hand, if
you are getting false negatives on small files - i.e. no guess is
made on a small file - you might want to decrease it."
:type 'integer
:tag "Minimum Number Of Relevant Lines"
:group 'dtrt-indent)
(defcustom dtrt-indent-max-lines 5000
"*Maximum number of lines to be considered in analysis.
This setting is meant to prevent dtrt-indent from spending large
amounts of time on analyzing large source files. In general, the
higher this setting, the more accurate the guess will be but the
more time dtrt-indent will consume when opening files."
:type 'integer
:tag "Maximum Number Of Lines"
:group 'dtrt-indent)
(defcustom dtrt-indent-min-quality 80.0
"*Minimum quality for an indentation offset to be accepted.
Percentage (0-100) of lines that are indented by a non-zero
amount of spaces divisible by a given offset required for that
offset to be eligible for guessing. A value of 80 means that for
instance, an indentation level of 4 will only be guessed if at
least 80% of all lines are indented by an offset divisible by 4.
This setting essentially controls how lenient or conservative
dtrt-indent operates. If you are getting false positives -
i.e. guess-offset guesses the wrong offset - you might want to
increase this setting. On the other hand, if you are getting
false negatives - i.e. guess-offset refuses to adjust the offset
- you might want to decrease it."
:type 'float
:tag "Minimum Number Of Matching Lines"
:group 'dtrt-indent)
(defcustom dtrt-indent-min-soft-tab-superiority 300.0
"*Minimum percentage soft-tab lines need to outnumber hard-tab ones.
TBD"
:type 'float
:tag "Minimum Superiority Of Soft Tabs"
:group 'dtrt-indent)
(defcustom dtrt-indent-min-hard-tab-superiority 300.0
"*Minimum percentage hard-tab lines need to outnumber soft-tab ones.
TBD"
:type 'float
:tag "Minimum Superiority Of Hard Tabs"
:group 'dtrt-indent)
(defcustom dtrt-indent-max-merge-deviation 20.0
"*Minimum difference between offsets divisible without remainder.
The percentage of difference in the number of lines that are
matched by two guessed offsets where the larger offset is
divisible by the smaller without remainder for the smaller offset
to be discarded.
This setting is in place because without it, in a file with 1000
lines matching an offset of 4, all of those 1000 lines are also
matching an offset of 2 and a number of stray lines whose offset
is divisible by 2 but not by 4 would confuse dtrt-indent to treat
the smaller offset as the better guess. By discarding the
smaller offset with some tolerance, this problem is avoided.
If you notice that often, sub-offsets are wrongly guessed (e.g. 8
would be the proper offset but 4 is guessed, or 6 would be
correct but 3 is guessed) you might want to decrease this
setting. On the other hand, if super-offsets are guessed (e.g. 4
would be appropriate, but 8 is guessed) you might want to
increase it."
:type 'float
:tag "Maximum Deviation For Sub-Offset Merging"
:group 'dtrt-indent)
(defcustom dtrt-indent-ignore-single-chars-flag nil
"*Non-nil means ignore lines containing only a single character.
Whether to treat lines that contain only a single non-whitespace
character as irrelevant for the analysis. Set this to t in to
prevent hanging braces etc. from skewing the results. Set it to
nil if you are dealing with source files whose indentation level
isn't reliably guessed without those lines."
:type 'boolean
:tag "Ignore Single-Character Lines"
:group 'dtrt-indent)
(defcustom dtrt-indent-min-matching-indentations 1
"*Minimum number of distinct levels for an offset to be eligible.
The minimum number of distinct, non-zero indentation levels
matching a given offset required to be present in a file for that
offset to be eligible for guessing. A value of 2 means that for
instance, an indentation level of 4 will only be guessed if some
lines are indented at 4 spaces and some at 8; or if some lines
are indented at 12 spaces and some at 20; but not if some lines
are indented at 4 spaces but there are no other lines indented at
an offset divisible by 4.
The default value of 1 effectively disables any such requirement.
If you are getting false positives, you might want to set this to
a higher value such as 2. However, a value of 2 means that the
offset won't be guessed for files containing only 'flat'
constructs"
:type 'integer
:tag "Minimum Depth"
:group 'dtrt-indent)
(defcustom dtrt-indent-min-offset 2
"*Minimum indentation offset that can be guessed.
You usually don't want to tinker with this - setting it to a
higher value than 2 means that the rather common offset of 2 will
no longer be guessed. Setting it to 1 will likely screw up
dtrt-indent algorithms because every line in every source code
matches that indentation level."
:type 'integer
:tag "Minimum Guessed Indentation Offset"
:group 'dtrt-indent)
(defcustom dtrt-indent-max-offset 8
"*Maximum indentation offset that can be guessed.
You usually don't want to tinker with this - setting it to a
lower value than 8 means that the (unfortunately) rather common
indentation offset of 8 will no longer be guessed. Setting it to
a higher value than 8 should not be harmful, but source files
using more than 8 spaces per indentation level are very rare."
:type 'integer
:tag "Maximum Guessed Indentation Offset"
:group 'dtrt-indent)
(defvar dtrt-indent-original-indent)
(make-variable-buffer-local
'dtrt-indent-original-indent)
(defvar dtrt-indent-explicit-offset)
(make-variable-buffer-local
'dtrt-indent-explicit-offset)
(defvar dtrt-indent-explicit-tab-mode)
(make-variable-buffer-local
'dtrt-indent-explicit-tab-mode)
(defun dtrt-indent--replace-in-string (haystack
needle-regexp
replacement)
"Replace every match in string by constant replacement.
Returns HAYSTACK with every match of NEEDLE-REGEXP replaced by
REPLACEMENT."
(if (string-match needle-regexp haystack)
(concat (substring haystack 0 (match-beginning 0))
replacement
(substring haystack (match-end 0)))
haystack))
(defun dtrt-indent--skip-to-end-of-match (end-regex
skip-regex
syntax-regex-pairs
multi-line)
"Place point at the end of the current match.
END-REGEX is a regular expression matching the end. If
SKIP-REGEX matches though, END-REGEX is ignored.
SYNTAX-REGEX-PAIRS is a list of syntax entries as described for
`dtrt-indent-language-syntax-table'. MULTI-LINE, if false,
constrains the search to the current line."
(let* ((index-offset 1)
end-index
skip-index
(regexp
(mapconcat
(lambda (el) (concat "\\(" el "\\)"))
(append (when end-regex
(setq end-index index-offset)
(setq index-offset (1+ index-offset))
(list end-regex))
(when skip-regex
(setq skip-index index-offset)
(setq index-offset (1+ index-offset))
(list skip-regex))
(mapcar (lambda (x) (car x))
syntax-regex-pairs))
"\\|")))
(while
(and
(re-search-forward regexp
(unless multi-line
(save-excursion (end-of-line) (point))) t)
(let ((match-index 1)
(match-count (/ (length (match-data)) 2)))
(while (and (<= match-index match-count)
(null (match-beginning match-index)))
(setq match-index (1+ match-index)))
(cond
((eq match-index end-index) nil)
((eq match-index skip-index) t)
(t
(let ((matching-syntax-entry
(let ((match-count (- match-index index-offset))
(syntax-regex-iterator syntax-regex-pairs))
(while (> match-count
(nth 1 (car syntax-regex-iterator)))
(setq match-count
(- match-count
(nth 1 (car syntax-regex-iterator)) 1))
(setq syntax-regex-iterator
(cdr syntax-regex-iterator)))
(car syntax-regex-iterator))))
(dtrt-indent--skip-to-end-of-match
(if (> (nth 1 matching-syntax-entry) 0)
(dtrt-indent--replace-in-string
(nth 2 matching-syntax-entry)
"[\\][1]" (regexp-quote
(match-string-no-properties
(1+ match-index))))
(nth 2 matching-syntax-entry))
(nth 4 matching-syntax-entry)
(when (nth 3 matching-syntax-entry) syntax-regex-pairs)
t)
t))))))))
(defun dtrt-indent--for-each-indentation (language func user-data)
"Call a function for each indentation found.
LANGUAGE is used to lookup a syntax table for excluding lines
from the process. For each line not excluded, FUNC is called
with USER-DATA as its argument and with point on the first
non-whitespace character of the line."
(save-excursion
(let ((case-fold-search nil))
(goto-char (point-min))
(while (and (re-search-forward "^[ \t]*" nil t)
(funcall func user-data)
(progn
(dtrt-indent--skip-to-end-of-match
nil
nil
(cdr
(assoc language
dtrt-indent-language-syntax-table))
nil)
(beginning-of-line)
(let ((here (point)))
(forward-line)
(not (eq here (point))))))))))
(defun dtrt-indent--calc-histogram (language)
"Calculate an indendation histogram.
The histogram is calculated for the current buffer using LANGUAGE
to determine which lines to exclude from the histogram."
(let ((histogram (make-hash-table))
(hard-tab-line-count 0)
(soft-tab-line-count 0)
(line-count 0))
(dtrt-indent--for-each-indentation
language
(lambda (histogram-and-count)
(setq line-count (1+ line-count))
(when (and (> (current-column) 0)
(not (looking-at "$"))
(or (not dtrt-indent-ignore-single-chars-flag)
(save-excursion
(forward-char)
(not (looking-at "[ \t]*$")))))
(puthash (current-column)
(1+ (gethash (current-column)
(car histogram-and-count) 0))
(car histogram-and-count))
(beginning-of-line)
(if (looking-at "[\t]+")
(setq hard-tab-line-count (1+ hard-tab-line-count))
(setq soft-tab-line-count (1+ soft-tab-line-count)))
(setcdr histogram-and-count (1+ (cdr histogram-and-count))))
(< line-count dtrt-indent-max-lines))
(cons histogram 0))
(let ((histogram-list '()) (total-lines 0))
(maphash (lambda (key value)
(setq histogram-list (append histogram-list
(list (list key value))))
(setq total-lines (+ total-lines value)))
histogram)
(list histogram-list
total-lines
hard-tab-line-count
soft-tab-line-count))))
(defun dtrt-indent--analyze-histogram-try-offset (try-offset
histogram
total-lines)
"Return match information for the given offset.
TRY-OFFSET is the offset to try, HISTOGRAM is the previously
calculated indentation histogram, TOTAL-LINES is the total number
of lines for which the histogram was calculated.
Returns a list in the format (TRY-OFFSET, PERCENTAGE,
MATCHING-INDENTATIONS, REJECT-REASON) where TRY-OFFSET is the
offset that was passed in as the first argument, PERCENTAGE is
the percentage of lines (0..1) with indentation levels that are a
multiple of TRY-OFFSET, MATCHING-INDENTATIONS is the number of
distinct indentation levels found that are a multiple of
TRY-OFFSET, and REJECT-REASON, if non-nil, is a string explaining
why TRY-OFFSET should be rejected."
(let ((total-matching-lines 0)
(matching-indentations 0))
(dolist (histogram-entry histogram)
(when (eq 0 (mod (nth 0 histogram-entry) try-offset))
(setq total-matching-lines (+ total-matching-lines
(nth 1 histogram-entry)))
(setq matching-indentations (1+ matching-indentations))))
(list try-offset
(/ (float total-matching-lines) total-lines)
matching-indentations
(cond
((< matching-indentations
dtrt-indent-min-matching-indentations)
(format "\
rejected: too few distinct matching offsets (%d required)"
dtrt-indent-min-matching-indentations))
(t
nil)))))
(defun dtrt-indent--search-hook-mapping (mode)
"Search hook-mapping for MODE or its derived-mode-parent."
(if mode
(or (assoc mode dtrt-indent-hook-mapping-list)
(dtrt-indent--search-hook-mapping (get mode 'derived-mode-parent))
(assoc 'default dtrt-indent-hook-mapping-list))))
(defun dtrt-indent--analyze (histogram-and-total-lines)
"Analyze the histogram.
HISTOGRAM-AND-TOTAL-LINES is a tuple with the first item being
the histogram, the second item being the total number of lines
considered in the histogram.
Returns a map with the following entries:
TBD"
(let* ((analysis
(let ((try-offset dtrt-indent-min-offset)
unsorted-analysis)
(while (<= try-offset dtrt-indent-max-offset)
(setq
unsorted-analysis
(append unsorted-analysis
(list (dtrt-indent--analyze-histogram-try-offset
try-offset
(nth 0 histogram-and-total-lines)
(nth 1 histogram-and-total-lines)))))
(setq try-offset (1+ try-offset)))
(sort unsorted-analysis (lambda (x y) (> (nth 1 x)
(nth 1 y))))))
(analysis-iterator analysis))
(while analysis-iterator
(let ((analysis-entry (car analysis-iterator)))
(dolist (other-analysis-entry (cdr analysis-iterator))
(let ((deviation (abs (- (nth 1 other-analysis-entry)
(nth 1 analysis-entry)))))
(when (and (not (nth 3 analysis-entry))
(eq 0 (mod (car other-analysis-entry)
(car analysis-entry)))
(> dtrt-indent-max-merge-deviation
(* 100.0 deviation)))
(setcdr
(cddr analysis-entry)
(list
(format "\
merged with offset %s (%.2f%% deviation, limit %.2f%%)"
(nth 0 other-analysis-entry)
(* 100.0 deviation)
dtrt-indent-max-merge-deviation)))))))
(setq analysis-iterator (cdr analysis-iterator)))
(let (best-guess)
(dolist (guess analysis)
(cond
((and (null best-guess)
(null (nth 3 guess)))
(setq best-guess guess))))
(let* ((confidence
(if best-guess
(nth 1 best-guess)
0))
(total-lines (nth 1 histogram-and-total-lines))
(enough-relevant-lines
(>= total-lines dtrt-indent-min-relevant-lines))
(hard-tab-percentage (if (> total-lines 0)
(/ (float (nth 2 histogram-and-total-lines))
total-lines)
0))
(soft-tab-percentage (if (> total-lines 0)
(/ (float (nth 3 histogram-and-total-lines))
total-lines)
0))
(change-indent-tabs-mode)
(indent-tabs-mode-setting)
(rejected
(cond
((null best-guess)
"no best guess")
((< (* 100.0 (nth 1 best-guess))
dtrt-indent-min-quality)
(format "best guess below minimum quality (%f < %f)"
(* 100.0 (nth 1 best-guess))
dtrt-indent-min-quality))
((not enough-relevant-lines)
(format "not enough relevant lines (%d required)"
dtrt-indent-min-relevant-lines)))))
(cond
((not enough-relevant-lines))
((or (= 0 hard-tab-percentage)
(>= (/ soft-tab-percentage
hard-tab-percentage)
(+ 1.0 (/ dtrt-indent-min-soft-tab-superiority 100.0))))
(setq change-indent-tabs-mode t)
(setq indent-tabs-mode-setting nil))
((or (= 0 soft-tab-percentage)
(>= (/ hard-tab-percentage
soft-tab-percentage)
(+ 1.0 (/ dtrt-indent-min-hard-tab-superiority 100.0))))
(setq change-indent-tabs-mode t)
(setq indent-tabs-mode-setting t)))
(list (cons :histogram (car histogram-and-total-lines))
(cons :total-lines total-lines)
(cons :analysis analysis)
(cons :best-guess best-guess)
(cons :hard-tab-lines (nth 2 histogram-and-total-lines) )
(cons :hard-tab-percentage hard-tab-percentage)
(cons :soft-tab-lines (nth 3 histogram-and-total-lines) )
(cons :soft-tab-percentage soft-tab-percentage)
(cons :change-indent-tabs-mode change-indent-tabs-mode)
(cons :indent-tabs-mode-setting indent-tabs-mode-setting)
(cons :rejected rejected)
(cons :confidence confidence))))))
(defun dtrt-indent-try-set-offset ()
"Try adjusting the current buffer's indentation offset."
(let ((language-and-variable (cdr (dtrt-indent--search-hook-mapping major-mode))))
(when language-and-variable
(let* ((result
(dtrt-indent--analyze
(dtrt-indent--calc-histogram
(car language-and-variable))))
(best-guess
(cdr (assoc :best-guess result)))
(rejected
(cdr (assoc :rejected result)))
(confidence
(cdr (assoc :confidence result)))
(change-indent-tabs-mode
(cdr (assoc :change-indent-tabs-mode result)))
(indent-tabs-mode-setting
(cdr (assoc :indent-tabs-mode-setting result)))
(best-indent-offset
(nth 0 best-guess))
(indent-offset-mode-variables
(let ((v (nth 1 language-and-variable)))
(if (listp v) v (list v))))
(indent-offset-variables
(append
indent-offset-mode-variables
(remove nil
(mapcar
(lambda (x)
(let ((mode (car x))
(variable (cadr x)))
(when (and (boundp mode)
(symbol-value mode))
variable)))
dtrt-indent-hook-generic-mapping-list))))
(indent-offset-names
(mapconcat (lambda (x) (format "%s" x))
indent-offset-variables ", ")))
;; update indent-offset-variable?
(cond
((and best-guess
(not rejected))
(if dtrt-indent-explicit-offset
(message "\
Indentation offset set with file variable; not adjusted")
(when (or (not dtrt-indent-require-confirmation-flag)
(yes-or-no-p
(format "Do you want to adjust %s to %s for buffer %s? "
indent-offset-names
best-indent-offset
(buffer-name))))
(setq dtrt-indent-original-indent
(mapcar
(lambda (x)
(list x (symbol-value x) (local-variable-p x)))
indent-offset-variables))
(when (>= dtrt-indent-verbosity 1)
(let ((offset-info
(format "%s adjusted to %s%s"
indent-offset-names
best-indent-offset