-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathso-long.el
1874 lines (1638 loc) · 83.3 KB
/
so-long.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
;;; so-long.el --- Say farewell to performance problems with minified code. -*- lexical-binding:t -*-
;;
;; Copyright (C) 2015-2016, 2018-2020 Free Software Foundation, Inc.
;; Author: Phil Sainty <[email protected]>
;; Maintainer: Phil Sainty <[email protected]>
;; URL: https://savannah.nongnu.org/projects/so-long
;; Keywords: convenience
;; Created: 23 Dec 2015
;; Package-Requires: ((emacs "24.4"))
;; Version: 1.0
;; 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; * Introduction
;; --------------
;; When the lines in a file are so long that performance could suffer to an
;; unacceptable degree, we say "so long" to the slow modes and options enabled
;; in that buffer, and invoke something much more basic in their place.
;;
;; Many Emacs modes struggle with buffers which contain excessively long lines.
;; This is commonly on account of 'minified' code (i.e. code that has been
;; compacted into the smallest file size possible, which often entails removing
;; newlines should they not be strictly necessary). This can result in lines
;; which are many thousands of characters long, and most programming modes
;; simply aren't optimized (remotely) for this scenario, so performance can
;; suffer significantly.
;;
;; When such files are detected, the command `so-long' is automatically called,
;; overriding certain minor modes and variables with performance implications
;; (all configurable), in order to enhance performance in the buffer.
;;
;; The default action enables the major mode `so-long-mode' in place of the mode
;; that Emacs selected. This ensures that the original major mode cannot affect
;; performance further, as well as making the so-long activity more obvious to
;; the user. These kinds of minified files are typically not intended to be
;; edited, so not providing the usual editing mode in such cases will rarely be
;; an issue. However, should the user wish to do so, the original state of the
;; buffer may be reinstated by calling `so-long-revert' (the key binding for
;; which is advertised when the major mode change occurs). If you prefer that
;; the major mode not be changed, the `so-long-minor-mode' action can be
;; configured.
;;
;; The user options `so-long-action' and `so-long-action-alist' determine what
;; will happen when `so-long' and `so-long-revert' are invoked, allowing
;; alternative actions (including custom actions) to be configured. As well as
;; the major and minor mode actions provided by this library, `longlines-mode'
;; is also supported by default as an alternative action.
;;
;; Note that while the measures taken can improve performance dramatically when
;; dealing with such files, this library does not have any effect on the
;; fundamental limitations of the Emacs redisplay code itself; and so if you do
;; need to edit the file, performance may still degrade as you get deeper into
;; the long lines. In such circumstances you may find that `longlines-mode' is
;; the most helpful facility.
;;
;; Note also that the mitigation is automatically triggered when visiting a
;; file. The library does not automatically detect if long lines are inserted
;; into an existing buffer (although the `so-long' command can be invoked
;; manually in such situations).
;; * Installation
;; --------------
;; Use M-x global-so-long-mode to enable/toggle the functionality. To enable
;; the functionality by default, either customize the `global-so-long-mode' user
;; option, or add the following to your init file:
;;
;; ;; Avoid performance issues in files with very long lines.
;; (global-so-long-mode 1)
;;
;; If necessary, ensure that so-long.el is in a directory in your load-path, and
;; that the library has been loaded. (These steps are not necessary if you are
;; using Emacs 27+, or have installed the GNU ELPA package.)
;; * Overview of modes and commands
;; --------------------------------
;; - `global-so-long-mode' - A global minor mode which enables the automated
;; behavior, causing the user's preferred action to be invoked whenever a
;; newly-visited file contains excessively long lines.
;; - `so-long-mode' - A major mode, and the default action.
;; - `so-long-minor-mode' - A minor mode version of the major mode, and an
;; alternative action.
;; - `longlines-mode' - A minor mode provided by the longlines.el library,
;; and another alternative action.
;; - `so-long' - Manually invoke the user's preferred action, enabling its
;; performance improvements for the current buffer.
;; - `so-long-revert' - Restore the original state of the buffer.
;; - `so-long-customize' - Configure the user options.
;; - `so-long-commentary' - Read this documentation in outline-mode.
;; * Usage
;; -------
;; In most cases you will simply enable `global-so-long-mode' and leave it to
;; act automatically as required, in accordance with your configuration (see
;; "Basic configuration" below).
;;
;; On rare occasions you may choose to manually invoke the `so-long' command,
;; which invokes your preferred `so-long-action' (exactly as the automatic
;; behavior would do if it had detected long lines). You might use this if a
;; problematic file did not meet your configured criteria, and you wished to
;; trigger the performance improvements manually.
;;
;; It is also possible to directly use `so-long-mode' or `so-long-minor-mode'
;; (major and minor modes, respectively). Both of these modes are actions
;; available to `so-long' but, like any other mode, they can be invoked directly
;; if you have a need to do that (see also "Other ways of using so-long" below).
;;
;; If the behavior ever triggers when you did not want it to, you can use the
;; `so-long-revert' command to restore the buffer to its original state.
;; * Basic configuration
;; ---------------------
;; Use M-x customize-group RET so-long RET
;; (or M-x so-long-customize RET)
;;
;; The user options `so-long-target-modes', `so-long-threshold', and
;; `so-long-max-lines' determine whether action will be taken automatically when
;; visiting a file, and `so-long-action' determines what will be done.
;; * Actions and menus
;; -------------------
;; The user options `so-long-action' and `so-long-action-alist' determine what
;; will happen when `so-long' and `so-long-revert' are invoked, and you can add
;; your own custom actions if you wish. The selected action can be invoked
;; manually with M-x so-long; and in general M-x so-long-revert will undo the
;; effects of whichever action was used (which is particularly useful when the
;; action is triggered automatically, but the detection was a 'false positive'.)
;;
;; All defined actions are presented in the "So Long" menu, which is visible
;; whenever long lines have been detected. Selecting an action from the menu
;; will firstly cause the current action (if any) to be reverted, before the
;; newly-selected action is invoked.
;;
;; Aside from the menu bar, the menu is also available in the mode line --
;; either via the major mode construct (when `so-long-mode' is active), or in
;; a separate mode line construct when some other major mode is active.
;; * Files with a file-local 'mode'
;; --------------------------------
;; A file-local major mode is likely to be safe even if long lines are detected
;; (as the author of the file would otherwise be unlikely to have set that mode),
;; and so these files are treated as special cases. When a file-local 'mode' is
;; present, the function defined by the `so-long-file-local-mode-function' user
;; option is called. The default value will cause the `so-long-minor-mode'
;; action to be used instead of the `so-long-mode' action, if the latter was
;; going to be used for this file. This is still a conservative default, but
;; this option can also be configured to inhibit so-long entirely in this
;; scenario, or to not treat a file-local mode as a special case at all.
;; * Buffers which are not displayed in a window
;; ---------------------------------------------
;; When a file with long lines is visited and the buffer is not displayed right
;; away, it may be that it is not intended to be displayed at all, and that it
;; has instead been visited for behind-the-scenes processing by some library.
;; Invisible buffers are less likely to cause performance issues, and it also
;; might be surprising to the other library if such a buffer were manipulated by
;; `so-long' (which might in turn lead to confusing errors for the user); so in
;; these situations the `so-long-invisible-buffer-function' value is called
;; instead. By default this arranges for `so-long' to be invoked on the buffer
;; if and when it is displayed, but not otherwise.
;;
;; This 'deferred call' is actually the most common scenario -- even when a
;; visited file is displayed "right away", it is normal for the buffer to be
;; invisible when `global-so-long-mode' processes it, and the gap between
;; "arranging to call" and "calling" `so-long' is simply extremely brief.
;; * Inhibiting and disabling minor modes
;; --------------------------------------
;; Certain minor modes cause significant performance issues in the presence of
;; very long lines, and should be disabled automatically in this situation.
;;
;; The simple way to disable most buffer-local minor modes is to add the mode
;; symbol to the `so-long-minor-modes' list. Several modes are targeted by
;; default, and it is a good idea to customize this variable to add any
;; additional buffer-local minor modes that you use which you know to have
;; performance implications.
;;
;; These minor modes are disabled if `so-long-action' is set to either
;; `so-long-mode' or `so-long-minor-mode'. If `so-long-revert' is called, then
;; the original values are restored.
;;
;; In the case of globalized minor modes, be sure to specify the buffer-local
;; minor mode, and not the global mode which controls it.
;;
;; Note that `so-long-minor-modes' is not useful for other global minor modes
;; (as distinguished from globalized minor modes), but in some cases it will be
;; possible to inhibit or otherwise counter-act the behavior of a global mode
;; by overriding variables, or by employing hooks (see below). You would need
;; to inspect the code for a given global mode (on a case by case basis) to
;; determine whether it's possible to inhibit it for a single buffer -- and if
;; so, how best to do that, as not all modes are alike.
;; * Overriding variables
;; ----------------------
;; `so-long-variable-overrides' is an alist mapping variable symbols to values.
;; If `so-long-action' is set to either `so-long-mode' or `so-long-minor-mode',
;; the buffer-local value for each variable in the list is set to the associated
;; value in the alist. Use this to enforce values which will improve
;; performance or otherwise avoid undesirable behaviors. If `so-long-revert'
;; is called, then the original values are restored.
;; * Hooks
;; -------
;; `so-long-hook' runs at the end of the `so-long' command, after the configured
;; action has been invoked.
;;
;; Likewise, if the `so-long-revert' command is used to restore the original
;; state then, once that has happened, `so-long-revert-hook' is run.
;;
;; The major and minor modes also provide the usual hooks: `so-long-mode-hook'
;; for the major mode (running between `change-major-mode-after-body-hook' and
;; `after-change-major-mode-hook'); and `so-long-minor-mode-hook' (when that
;; mode is enabled or disabled).
;; * Troubleshooting
;; -----------------
;; Any elisp library has the potential to cause performance problems; so while
;; the default configuration addresses some important common cases, it's
;; entirely possible that your own config introduces problem cases which are
;; unknown to this library.
;;
;; If visiting a file is still taking a very long time with so-long enabled,
;; you should test the following command:
;;
;; emacs -Q -l so-long -f global-so-long-mode <file>
;;
;; For versions of Emacs < 27, use:
;; emacs -Q -l /path/to/so-long.el -f global-so-long-mode <file>
;;
;; If the file loads quickly when that command is used, you'll know that
;; something in your personal configuration is causing problems. If this
;; turns out to be a buffer-local minor mode, or a user option, you can
;; likely alleviate the issue by customizing `so-long-minor-modes' or
;; `so-long-variable-overrides' accordingly.
;;
;; The in-built profiler can be an effective way of discovering the cause
;; of such problems. Refer to M-: (info "(elisp) Profiling") RET
;;
;; In some cases it may be useful to set a file-local `mode' variable to
;; `so-long-mode', completely bypassing the automated decision process.
;; Refer to M-: (info "(emacs) Specifying File Variables") RET
;;
;; If so-long itself is causing problems, it can be inhibited by setting the
;; `so-long-enabled' variable to nil, or by disabling the global mode with
;; M-- M-x global-so-long-mode, or M-: (global-so-long-mode 0)
;; * Example configuration
;; -----------------------
;; If you prefer to configure in code rather than via the customize interface,
;; then you might use something along these lines:
;;
;; ;; Enable so-long library.
;; (when (require 'so-long nil :noerror)
;; (global-so-long-mode 1)
;; ;; Basic settings.
;; (setq so-long-action 'so-long-minor-mode)
;; (setq so-long-threshold 1000)
;; (setq so-long-max-lines 100)
;; ;; Additional target major modes to trigger for.
;; (mapc (apply-partially #'add-to-list 'so-long-target-modes)
;; '(sgml-mode nxml-mode))
;; ;; Additional buffer-local minor modes to disable.
;; (mapc (apply-partially #'add-to-list 'so-long-minor-modes)
;; '(diff-hl-mode diff-hl-amend-mode diff-hl-flydiff-mode))
;; ;; Additional variables to override.
;; (mapc (apply-partially #'add-to-list 'so-long-variable-overrides)
;; '((show-trailing-whitespace . nil)
;; (truncate-lines . nil))))
;; * Other ways of using so-long
;; -----------------------------
;; It may prove useful to automatically invoke major mode `so-long-mode' for
;; certain files, irrespective of whether they contain long lines.
;;
;; To target specific files and extensions, using `auto-mode-alist' is the
;; simplest method. To add such an entry, use:
;; (add-to-list 'auto-mode-alist (cons REGEXP 'so-long-mode))
;; Where REGEXP is a regular expression matching the filename. e.g.:
;;
;; - Any filename with a particular extension ".foo":
;; (rx ".foo" eos)
;;
;; - Any file in a specific directory:
;; (rx bos "/path/to/directory/")
;;
;; - Only *.c filenames under that directory:
;; (rx bos "/path/to/directory/" (zero-or-more not-newline) ".c" eos)
;;
;; - Match some sub-path anywhere in a filename:
;; (rx "/sub/path/foo")
;;
;; - A specific individual file:
;; (rx bos "/path/to/file" eos)
;;
;; Another way to target individual files is to set a file-local `mode'
;; variable. Refer to M-: (info "(emacs) Specifying File Variables") RET
;;
;; `so-long-minor-mode' can also be called directly if desired. e.g.:
;; (add-hook 'FOO-mode-hook 'so-long-minor-mode)
;;
;; In Emacs 26.1 or later (see "Caveats" below) you also have the option of
;; using file-local and directory-local variables to determine how `so-long'
;; behaves. e.g. -*- so-long-action: longlines-mode; -*-
;;
;; The buffer-local `so-long-function' and `so-long-revert-function' values may
;; be set directly (in a major mode hook, for instance), as any existing value
;; for these variables will be used in preference to the values defined by the
;; selected action. For file-local or directory-local usage it is preferable to
;; set only `so-long-action', as all function variables are marked as 'risky',
;; meaning you would need to add to `safe-local-variable-values' in order to
;; avoid being queried about them.
;;
;; Finally, the `so-long-predicate' user option enables the automated behavior
;; to be determined by a custom function, if greater control is needed.
;; * Implementation notes
;; ----------------------
;; This library advises `set-auto-mode' (in order to react after Emacs has
;; chosen the major mode for a buffer), and `hack-local-variables' (so that we
;; may behave differently when a file-local mode is set). In earlier versions
;; of Emacs (< 26.1) we also advise `hack-one-local-variable' (to prevent a
;; file-local mode from restoring the original major mode if we had changed it).
;;
;; Many variables are permanent-local because after the normal major mode has
;; been set, we potentially change the major mode to `so-long-mode', and it's
;; important that the values which were established prior to that are retained.
;; * Caveats
;; ---------
;; The variables affecting the automated behavior of this library (such as
;; `so-long-action') can be used as file- or dir-local values in Emacs 26+, but
;; not in previous versions of Emacs. This is on account of improvements made
;; to `normal-mode' in 26.1, which altered the execution order with respect to
;; when local variables are processed. In earlier versions of Emacs the local
;; variables are processed too late, and hence have no effect on the decision-
;; making process for invoking `so-long'. It is unlikely that equivalent
;; support will be implemented for older versions of Emacs. The exception to
;; this caveat is the `mode' pseudo-variable, which is processed early in all
;; versions of Emacs, and can be set to `so-long-mode' if desired.
;; * Change Log:
;;
;; 1.0 - Included in Emacs 27.1, and in GNU ELPA for prior versions of Emacs.
;; - New global mode `global-so-long-mode' to enable/disable the library.
;; - New user option `so-long-action'.
;; - New user option `so-long-action-alist' defining alternative actions.
;; - New user option `so-long-variable-overrides'.
;; - New user option `so-long-skip-leading-comments'.
;; - New user option `so-long-file-local-mode-function'.
;; - New user option `so-long-invisible-buffer-function'.
;; - New user option `so-long-predicate'.
;; - New variable and function `so-long-function'.
;; - New variable and function `so-long-revert-function'.
;; - New command `so-long' to invoke `so-long-function' interactively.
;; - New command `so-long-revert' to invoke `so-long-revert-function'.
;; - New minor mode action `so-long-minor-mode' facilitates retaining the
;; original major mode, while still disabling minor modes and overriding
;; variables like the major mode `so-long-mode'.
;; - Support `longlines-mode' as a `so-long-action' option.
;; - Added "So Long" menu, including all selectable actions.
;; - Added mode-line indicator, user option `so-long-mode-line-label',
;; and faces `so-long-mode-line-active', `so-long-mode-line-inactive'.
;; - New help commands `so-long-commentary' and `so-long-customize'.
;; - Renamed `so-long-mode-enabled' to `so-long-enabled'.
;; - Refactored the default hook values using variable overrides
;; (and returning all the hooks to nil default values).
;; - Performance improvements for `so-long-detected-long-line-p'.
;; - Converted defadvice to nadvice.
;; 0.7.6 - Bug fix for `so-long-mode-hook' losing its default value.
;; 0.7.5 - Documentation.
;; - Added sgml-mode and nxml-mode to `so-long-target-modes'.
;; 0.7.4 - Refactored the handling of `whitespace-mode'.
;; 0.7.3 - Added customize group `so-long' with user options.
;; - Added `so-long-original-values' to generalize the storage and
;; restoration of values from the original mode upon `so-long-revert'.
;; - Added `so-long-revert-hook'.
;; 0.7.2 - Remember the original major mode even with M-x `so-long-mode'.
;; 0.7.1 - Clarified interaction with globalized minor modes.
;; 0.7 - Handle header 'mode' declarations.
;; - Hack local variables after reverting to the original major mode.
;; - Reverted `so-long-max-lines' to a default value of 5.
;; 0.6.5 - Inhibit globalized `hl-line-mode' and `whitespace-mode'.
;; - Set `buffer-read-only' by default.
;; 0.6 - Added `so-long-minor-modes' and `so-long-hook'.
;; 0.5 - Renamed library to "so-long.el".
;; - Added explicit `so-long-enable' command to activate our advice.
;; 0.4 - Amended/documented behavior with file-local 'mode' variables.
;; 0.3 - Defer to a file-local 'mode' variable.
;; 0.2 - Initial release to EmacsWiki.
;; 0.1 - Experimental.
;;; Code:
(require 'cl-lib)
(add-to-list 'customize-package-emacs-version-alist
'(so-long ("1.0" . "27.1")))
(defconst so-long--latest-version "1.0")
(declare-function longlines-mode "longlines")
(defvar longlines-mode)
(defvar so-long-enabled nil
"Set to nil to prevent `so-long' from being triggered automatically.
Has no effect if `global-so-long-mode' is not enabled.")
(defvar-local so-long--active nil ; internal use
"Non-nil when `so-long' mitigation is in effect.")
(defvar so-long--set-auto-mode nil ; internal use
"Non-nil while `set-auto-mode' is executing.")
(defvar so-long--hack-local-variables-no-mode nil ; internal use
"Non-nil to prevent `hack-local-variables' applying a `mode' variable.")
(defvar-local so-long--inhibited nil ; internal use
"When non-nil, prevents the `set-auto-mode' advice from calling `so-long'.")
(put 'so-long--inhibited 'permanent-local t)
(defvar so-long--calling nil ; internal use
;; This prevents infinite recursion if eval:(so-long) is specified
;; as a file- or dir-local variable, and `so-long-action' is set to
;; `so-long-mode' (as that major mode would once again process the
;; local variables, and hence call itself).
"Non-nil while `so-long' or `so-long-revert' is executing.")
(defvar-local so-long-detected-p nil
"Non-nil if `so-long' has been invoked (even if subsequently reverted).")
(put 'so-long-detected-p 'permanent-local t)
(defgroup so-long nil
"Prevent unacceptable performance degradation with very long lines."
:prefix "so-long"
:group 'convenience)
(defcustom so-long-threshold 250
"Maximum line length permitted before invoking `so-long-function'.
See `so-long-detected-long-line-p' for details."
:type 'integer
:package-version '(so-long . "1.0"))
(defcustom so-long-max-lines 5
"Number of non-blank, non-comment lines to test for excessive length.
If nil then all lines will be tested, until either a long line is detected,
or the end of the buffer is reached.
If `so-long-skip-leading-comments' is nil then comments and blank lines will
be counted.
See `so-long-detected-long-line-p' for details."
:type '(choice (integer :tag "Limit")
(const :tag "Unlimited" nil))
:package-version '(so-long . "1.0"))
(defcustom so-long-skip-leading-comments t
"Non-nil to ignore all leading comments and whitespace.
If the file begins with a shebang (#!), this option also causes that line to be
ignored even if it doesn't match the buffer's comment syntax, to ensure that
comments following the shebang will be ignored.
See `so-long-detected-long-line-p' for details."
:type 'boolean
:package-version '(so-long . "1.0"))
(defcustom so-long-target-modes
'(prog-mode css-mode sgml-mode nxml-mode)
"`so-long' affects only these modes and their derivatives.
Our primary use-case is minified programming code, so `prog-mode' covers
most cases, but there are some exceptions to this.
If t, then all modes are targeted. Note that this is only useful with a
custom `so-long-predicate', as many file types (archives and binary files,
for example) can safely contain long lines, and invoking `so-long' on such
files would prevent Emacs from handling them correctly."
;; Use 'symbol', as 'function' may be unknown => mismatch.
:type '(choice (repeat :tag "Specified modes" symbol)
(const :tag "All modes" t))
:package-version '(so-long . "1.0"))
(defcustom so-long-invisible-buffer-function #'so-long-deferred
"Function called in place of `so-long' when the buffer is not displayed.
This affects the behavior of `global-so-long-mode'.
We treat invisible buffers differently from displayed buffers because, in
cases where a library is using a buffer for behind-the-scenes processing,
it might be surprising if that buffer were unexpectedly manipulated by
`so-long' (which might in turn lead to confusing errors for the user).
Invisible buffers are less likely to cause performance issues related to
long lines, so this differentiation is generally satisfactory.
The default value `so-long-deferred' prevents `global-so-long-mode' from
triggering `so-long' for any given buffer until such time as the buffer is
displayed in a window.
\(Note that buffers are normally invisible at this point -- when `find-file'
is used, the buffer is not displayed in a window until a short time after
`global-so-long-mode' has seen it.)
The value nil or `so-long' means that `so-long' will be called directly; in
which case it may be problematic for `so-long-variable-overrides' to enable
`buffer-read-only', or for `so-long-action' to be set to `so-long-mode'.
This is because the buffer may not be intended to be displayed at all, and
the mentioned options might interfere with some intended processing."
:type '(radio (const so-long-deferred)
(const :tag "nil: Call so-long as normal" nil)
(function :tag "Custom function"))
:package-version '(so-long . "1.0"))
(defcustom so-long-predicate 'so-long-detected-long-line-p
"Function, called after `set-auto-mode' to decide whether action is needed.
Only called if the major mode is a member of `so-long-target-modes'.
The specified function will be called with no arguments. If it returns non-nil
then `so-long' will be invoked.
Defaults to `so-long-detected-long-line-p'."
:type '(radio (const so-long-detected-long-line-p)
(function :tag "Custom function"))
:package-version '(so-long . "1.0"))
;; Silence byte-compiler warning. `so-long-action-alist' is defined below
;; as a user option; but the definition sequence required for its setter
;; function means we also need to declare it beforehand.
(defvar so-long-action-alist)
(defun so-long--action-type ()
"Generate a :type for `so-long-action' based on `so-long-action-alist'."
;; :type seemingly cannot be a form to be evaluated on demand, so we
;; endeavor to keep it up-to-date with `so-long-action-alist' by
;; calling this from `so-long--action-alist-setter'.
`(radio ,@(mapcar (lambda (x) (list 'const :tag (cadr x) (car x)))
(assq-delete-all nil so-long-action-alist))
(const :tag "Do nothing" nil)))
(defun so-long--action-alist-setter (option value)
"The customize :set function for `so-long-action-alist'."
;; Set the value as normal.
(set-default option value)
;; Update the :type of `so-long-action' to present the updated values.
(put 'so-long-action 'custom-type (so-long--action-type)))
(defcustom so-long-action-alist
'((so-long-mode
"Change major mode to so-long-mode"
so-long-mode
so-long-mode-revert)
(so-long-minor-mode
"Enable so-long-minor-mode"
turn-on-so-long-minor-mode
turn-off-so-long-minor-mode)
(longlines-mode
"Enable longlines-mode"
so-long-function-longlines-mode
so-long-revert-function-longlines-mode))
"Options for `so-long-action'.
Each element is a list comprising (KEY LABEL ACTION REVERT)
KEY is a symbol which is a valid value for `so-long-action', and LABEL is a
string which describes and represents the key in that option's customize
interface, and in the \"So Long\" menu. ACTION and REVERT are functions:
ACTION will be the `so-long-function' value when `so-long' is called, and
REVERT will be the `so-long-revert-function' value, if `so-long-revert' is
subsequently called."
:type '(alist :key-type (symbol :tag "Key" :value <action>)
:value-type (list (string :tag "Label" :value "<description>")
(function :tag "Action")
(function :tag "Revert")))
:set #'so-long--action-alist-setter
:package-version '(so-long . "1.0"))
(put 'so-long-action-alist 'risky-local-variable t)
(defcustom so-long-action 'so-long-mode
"The action taken by `so-long' when long lines are detected.
\(Long lines are determined by `so-long-predicate' after `set-auto-mode'.)
The value is a key to one of the options defined by `so-long-action-alist'.
The default action is to replace the original major mode with `so-long-mode'.
Alternatively, the `so-long-minor-mode' action retains the original major mode
while still disabling minor modes and overriding variables. These are the only
standard values for which `so-long-minor-modes' and `so-long-variable-overrides'
will be automatically processed; but custom actions can also do these things.
The value `longlines-mode' causes that minor mode to be enabled. See
longlines.el for more details.
Each action likewise determines the behavior of `so-long-revert'.
If the value is nil, or not defined in `so-long-action-alist', then no action
will be taken."
:type (so-long--action-type)
:package-version '(so-long . "1.0"))
(defvar-local so-long-function nil
"The function called by `so-long'.
This should be set in conjunction with `so-long-revert-function'. This usually
happens automatically, based on the value of `so-long-action'.
The specified function will be called with no arguments, after which
`so-long-hook' runs.")
(put 'so-long-function 'permanent-local t)
(defvar-local so-long-revert-function nil
"The function called by `so-long-revert'.
This should be set in conjunction with `so-long-function'. This usually
happens automatically, based on the value of `so-long-action'.
The specified function will be called with no arguments, after which
`so-long-revert-hook' runs.")
(put 'so-long-revert-function 'permanent-local t)
(defun so-long-function (&optional action-arg)
"The value of `so-long-function', else derive from `so-long-action'.
If ACTION-ARG is provided, it is used in place of `so-long-action'."
(or so-long-function
(and (or action-arg
(setq action-arg so-long-action))
(let ((action (assq action-arg so-long-action-alist)))
(nth 2 action)))))
(defun so-long-revert-function (&optional action-arg)
"The value of `so-long-revert-function', else derive from `so-long-action'.
If ACTION-ARG is provided, it is used in place of `so-long-action'."
(or so-long-revert-function
(and (or action-arg
(setq action-arg so-long-action))
(let ((action (assq action-arg so-long-action-alist)))
(nth 3 action)))))
(defcustom so-long-file-local-mode-function 'so-long-mode-downgrade
"Function to call during `set-auto-mode' when a file-local mode is set.
The specified function will be called with a single argument, being the
file-local mode which was established.
This happens before `so-long' is called, and so this function can modify the
subsequent action.
The value `so-long-mode-downgrade' means `so-long-minor-mode' will be used in
place of `so-long-mode' -- therefore respecting the file-local mode value, yet
still overriding minor modes and variables (as if `so-long-action' had been set
to `so-long-minor-mode').
The value `so-long-inhibit' means that so-long will not take any action at all
for this file.
If nil, then do not treat files with file-local modes any differently to other
files.
Note that this function is called if a file-local mode is set even if `so-long'
will not be called, and also if the file-local mode is `so-long-mode'. Custom
functions may need to test for these cases -- see `so-long-mode-downgrade' for
an example."
:type '(radio (const so-long-mode-downgrade)
(const so-long-inhibit)
(const :tag "nil: Use so-long-function as normal" nil)
(function :tag "Custom function"))
:package-version '(so-long . "1.0"))
(make-variable-buffer-local 'so-long-file-local-mode-function)
;; `provided-mode-derived-p' was added in 26.1
(unless (fboundp 'provided-mode-derived-p)
(defun provided-mode-derived-p (mode &rest modes)
"Non-nil if MODE is derived from one of MODES.
Uses the `derived-mode-parent' property of the symbol to trace backwards.
If you just want to check `major-mode', use `derived-mode-p'."
(while (and (not (memq mode modes))
(setq mode (get mode 'derived-mode-parent))))
mode))
(defun so-long-handle-file-local-mode (mode)
"Wrapper for calling `so-long-file-local-mode-function'.
The function is called with one argument, MODE, being the file-local mode which
was established."
;; Handle the special case whereby the file-local mode was `so-long-mode'.
;; In this instance we set `so-long--inhibited', because the file-local mode
;; is already going to do everything that is wanted.
(when (provided-mode-derived-p mode 'so-long-mode)
(setq so-long--inhibited t))
;; Call `so-long-file-local-mode-function'.
(when so-long-file-local-mode-function
(funcall so-long-file-local-mode-function mode)))
(defcustom so-long-minor-modes
;; In sorted groups.
'(font-lock-mode ;; (Generally the most important).
;; Other standard minor modes:
display-line-numbers-mode
flymake-mode
flyspell-mode
goto-address-mode
goto-address-prog-mode
hi-lock-mode
highlight-changes-mode
hl-line-mode
linum-mode
nlinum-mode
prettify-symbols-mode
visual-line-mode
whitespace-mode
;; Known third-party modes-of-interest:
diff-hl-amend-mode
diff-hl-flydiff-mode
diff-hl-mode
dtrt-indent-mode
flycheck-mode
hl-sexp-mode
idle-highlight-mode
rainbow-delimiters-mode
)
;; It's not clear to me whether all of these would be problematic, but they
;; seemed like reasonable targets. Some are certainly excessive in smaller
;; buffers of minified code, but we should be aiming to maximize performance
;; by default, so that Emacs is as responsive as we can manage in even very
;; large buffers of minified code.
"List of buffer-local minor modes to explicitly disable.
The ones which were originally enabled in the buffer are disabled by calling
them with the numeric argument 0. Unknown modes, and modes which were not
enabled, are ignored.
This happens after any globalized minor modes have acted, so that buffer-local
modes controlled by globalized modes can also be targeted.
By default this happens if `so-long-action' is set to either `so-long-mode'
or `so-long-minor-mode'. If `so-long-revert' is subsequently invoked, then the
disabled modes are re-enabled by calling them with the numeric argument 1.
`so-long-hook' can be used where more custom behavior is desired.
Please submit bug reports to recommend additional modes for this list, whether
they are in Emacs core, GNU ELPA, or elsewhere."
:type '(repeat symbol) ;; not function, as may be unknown => mismatch.
:package-version '(so-long . "1.0"))
(defcustom so-long-variable-overrides
'((bidi-inhibit-bpa . t)
(bidi-paragraph-direction . left-to-right)
(buffer-read-only . t)
(global-hl-line-mode . nil)
(line-move-visual . t)
(show-paren-mode . nil)
(truncate-lines . nil)
(which-func-mode . nil))
"Variables to override, and the values to override them with.
The variables are given buffer-local values. By default this happens if
`so-long-action' is set to either `so-long-mode' or `so-long-minor-mode'.
If `so-long-revert' is subsequently invoked, then the variables are restored
to their original states.
The combination of `line-move-visual' (enabled) and `truncate-lines' (disabled)
is important for maximizing responsiveness when moving vertically within an
extremely long line, as otherwise the full length of the line may need to be
scanned to find the next position."
:type '(alist :key-type (variable :tag "Variable")
:value-type (sexp :tag "Value"))
:options '((bidi-inhibit-bpa boolean)
(bidi-paragraph-direction (choice (const left-to-right)
(const right-to-left)
(const nil)))
(buffer-read-only boolean)
(global-hl-line-mode boolean)
(line-move-visual boolean)
(show-paren-mode boolean)
(truncate-lines boolean)
(which-func-mode boolean))
:package-version '(so-long . "1.0"))
(defcustom so-long-hook nil
"List of functions to call after `so-long' is called.
See also `so-long-revert-hook'."
:type 'hook
:package-version '(so-long . "1.0"))
(defcustom so-long-revert-hook nil
"List of functions to call after `so-long-revert' is called.
See also `so-long-hook'."
:type 'hook
:package-version '(so-long . "1.0"))
(defcustom so-long-mode-line-label "So Long"
"Text label of `so-long-mode-line-info' when long lines are detected.
If nil, no mode line indicator will be displayed."
:type '(choice (string :tag "String")
(const :tag "None" nil))
:package-version '(so-long . "1.0"))
(defface so-long-mode-line-active
'((t :inherit mode-line-emphasis))
"Face for `so-long-mode-line-info' when mitigation is active."
:package-version '(so-long . "1.0"))
(defface so-long-mode-line-inactive
'((t :inherit mode-line-inactive))
"Face for `so-long-mode-line-info' when mitigation has been reverted."
:package-version '(so-long . "1.0"))
;; Modes that go slowly and line lengths excessive
;; Font-lock performance becoming oppressive
;; All of my CPU tied up with strings
;; These are a few of my least-favorite things
(defvar-local so-long-original-values nil
"Alist holding the buffer's original `major-mode' value, and other data.
Any values to be restored by `so-long-revert' can be stored here by the
`so-long-function' or during `so-long-hook'. `so-long' itself stores the
original states for `so-long-variable-overrides' and `so-long-minor-modes',
so these values are available to custom actions by default.
See also `so-long-remember' and `so-long-original'.")
(put 'so-long-original-values 'permanent-local t)
(defun so-long-original (key &optional exists)
"Return the current value for KEY in `so-long-original-values'.
If you need to differentiate between a stored value of nil and no stored value
at all, make EXISTS non-nil. This then returns the result of `assq' directly:
nil if no value was set, and a cons cell otherwise."
(if exists
(assq key so-long-original-values)
(cadr (assq key so-long-original-values))))
(defun so-long-remember (variable)
"Store the value of VARIABLE in `so-long-original-values'.
We additionally store a boolean value which indicates whether that value was
buffer-local."
(when (boundp variable)
(setq so-long-original-values
(assq-delete-all variable so-long-original-values))
(push (list variable
(symbol-value variable)
(local-variable-p variable))
so-long-original-values)))
(defun so-long-remember-all (&optional reset)
"Remember the current variable and minor mode values.
Stores the existing value for each entry in `so-long-variable-overrides'.
Stores the name of each enabled mode from the list `so-long-minor-modes'.
If RESET is non-nil, remove any existing values before storing the new ones."
(when reset
(setq so-long-original-values nil))
(dolist (ovar so-long-variable-overrides)
(so-long-remember (car ovar)))
(dolist (mode so-long-minor-modes)
(when (and (boundp mode) mode)
(so-long-remember mode))))
(defun so-long-menu ()
"Dynamically generate the \"So Long\" menu."
;; (info "(elisp) Menu Example")
(let ((map (make-sparse-keymap "So Long"))
(help-map (make-sparse-keymap "Help")))
;; `so-long-revert'.
(define-key-after map [so-long-revert]
'(menu-item "Revert to normal" so-long-revert
:enable (and so-long-revert-function
so-long--active)))
;; `so-long-menu-item-replace-action' over `so-long-action-alist'.
(define-key-after map [so-long-actions-separator]
'(menu-item "--"))
(dolist (item so-long-action-alist)
(cl-destructuring-bind (key label actionfunc revertfunc)
item
(define-key-after map (vector key)
`(menu-item
,label
,(let ((sym (make-symbol "so-long-menu-item-replace-action")))
;; We make a symbol so that `describe-key' on the menu item
;; produces something more descriptive than byte code. There is
;; no interned `so-long-menu-item-replace-action' which might
;; make this slightly confusing -- but only in the rare situation
;; when someone uses `describe-key' on one of these menu items,
;; and then wants to find more information. We mitigate this by
;; making the following docstring very clear.
(defalias sym (lambda () (interactive "@") (so-long key))
;; We use "@" as commands in the mode-line menu may be
;; triggered by mouse when some other window is selected.
"Revert the current action and invoke the chosen replacement.
This command calls `so-long' with the selected action as an argument.")
sym)
:enable (not (and so-long--active
(eq ',actionfunc so-long-function)
(eq ',revertfunc so-long-revert-function)))))))
;; "Help" sub-menu.
(define-key-after map [so-long-help-separator]
'(menu-item "--"))
(define-key-after map [so-long-help]
`(menu-item "Help" ,help-map))
(define-key-after help-map [so-long-commentary]
'(menu-item "Commentary" so-long-commentary))
(define-key-after help-map [so-long-customize]
'(menu-item "Customize" so-long-customize))
map))
;;;###autoload
(defun so-long-commentary ()
"View the so-long documentation in `outline-mode'."
(interactive)
(let ((buf "*So Long: Commentary*"))
(when (buffer-live-p (get-buffer buf))
(kill-buffer buf))
;; Use `finder-commentary' to generate the buffer.
(require 'finder)
(cl-letf (((symbol-function 'finder-summary) #'ignore))
(finder-commentary "so-long"))
(let ((inhibit-read-only t))
(if (looking-at "^Commentary:\n\n")
(replace-match "so-long.el\n\n")
(insert "so-long.el\n")
(forward-line 1))
(save-excursion
(while (re-search-forward "^-+$" nil :noerror)
(replace-match ""))))
(rename-buffer buf)
;; Enable `outline-mode' and `view-mode' for user convenience.
(outline-mode)
(declare-function outline-next-visible-heading "outline")
(declare-function outline-previous-visible-heading "outline")
(declare-function outline-toggle-children "outline")
(declare-function outline-toggle-children "outline")
(view-mode 1)
;; Add some custom local bindings.
(let ((map (make-sparse-keymap)))
(define-key map (kbd "TAB") #'outline-toggle-children)
(define-key map (kbd "<M-tab>") #'outline-toggle-children)
(define-key map (kbd "M-n") #'outline-next-visible-heading)
(define-key map (kbd "M-p") #'outline-previous-visible-heading)
(set-keymap-parent map (current-local-map))
(use-local-map map))
;; Display the So Long menu.
(so-long--ensure-enabled)
(let ((so-long-action nil))
(so-long))))
;;;###autoload
(defun so-long-customize ()
"Open the so-long `customize' group."
(interactive)
(customize-group 'so-long))
(defvar-local so-long-mode-line-info nil
"Mode line construct displayed when `so-long' has been triggered.
Displayed as part of `mode-line-misc-info'.
`so-long-mode-line-label' defines the text to be displayed (if any).
Face `so-long-mode-line-active' is used while mitigation is active, and
`so-long-mode-line-inactive' is used if `so-long-revert' is called.
Not displayed when `so-long-mode' is enabled, as the major mode construct
serves the same purpose.")
;; Ensure we can display text properties on this value in the mode line.
;; See (info "(elisp) Mode Line Data") or (info "(elisp) Properties in Mode").
(put 'so-long-mode-line-info 'risky-local-variable t)
(defun so-long-mode-line-info ()
"Return the mode line construct for variable `so-long-mode-line-info'."
(let ((map (make-sparse-keymap)))