1;;; so-long.el --- Say farewell to performance problems with minified code. -*- lexical-binding:t -*- 2;; 3;; Copyright (C) 2015-2016, 2018-2021 Free Software Foundation, Inc. 4 5;; Author: Phil Sainty <psainty@orcon.net.nz> 6;; Maintainer: Phil Sainty <psainty@orcon.net.nz> 7;; URL: https://savannah.nongnu.org/projects/so-long 8;; Keywords: convenience 9;; Created: 23 Dec 2015 10;; Package-Requires: ((emacs "24.4")) 11;; Version: 1.1.2 12 13;; This file is part of GNU Emacs. 14 15;; GNU Emacs is free software: you can redistribute it and/or modify 16;; it under the terms of the GNU General Public License as published by 17;; the Free Software Foundation, either version 3 of the License, or 18;; (at your option) any later version. 19 20;; GNU Emacs is distributed in the hope that it will be useful, 21;; but WITHOUT ANY WARRANTY; without even the implied warranty of 22;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23;; GNU General Public License for more details. 24 25;; You should have received a copy of the GNU General Public License 26;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 27 28;;; Commentary: 29;; 30;; * Introduction 31;; -------------- 32;; When the lines in a file are so long that performance could suffer to an 33;; unacceptable degree, we say "so long" to the slow modes and options enabled 34;; in that buffer, and invoke something much more basic in their place. 35;; 36;; Many Emacs modes struggle with buffers which contain excessively long lines. 37;; This is commonly on account of 'minified' code (i.e. code that has been 38;; compacted into the smallest file size possible, which often entails removing 39;; newlines should they not be strictly necessary). This can result in lines 40;; which are many thousands of characters long, and most programming modes 41;; simply aren't optimised (remotely) for this scenario, so performance can 42;; suffer significantly. 43;; 44;; When so-long detects such a file, it calls the command `so-long', which 45;; overrides certain minor modes and variables (you can configure the details) 46;; to improve performance in the buffer. 47;; 48;; The default action enables the major mode `so-long-mode' in place of the mode 49;; that Emacs selected. This ensures that the original major mode cannot affect 50;; performance further, as well as making the so-long activity more obvious to 51;; the user. These kinds of minified files are typically not intended to be 52;; edited, so not providing the usual editing mode in such cases will rarely be 53;; an issue; however you can restore the buffer to its original state by calling 54;; `so-long-revert' (the key binding of which is advertised when the major mode 55;; change occurs). If you prefer that the major mode not be changed in the 56;; first place, there is a `so-long-minor-mode' action available, which you can 57;; select by customizing the `so-long-action' user option. 58;; 59;; The user options `so-long-action' and `so-long-action-alist' determine what 60;; `so-long' and `so-long-revert' will do, enabling you to configure alternative 61;; actions (including custom actions). As well as the major and minor mode 62;; actions provided by this library, `longlines-mode' is also supported by 63;; default as an alternative action. 64;; 65;; Note that while the measures taken can improve performance dramatically when 66;; dealing with such files, this library does not have any effect on the 67;; fundamental limitations of the Emacs redisplay code itself; and so if you do 68;; need to edit the file, performance may still degrade as you get deeper into 69;; the long lines. In such circumstances you may find that `longlines-mode' is 70;; the most helpful facility. 71;; 72;; Note also that the mitigations are automatically triggered when visiting a 73;; file. The library does not automatically detect if long lines are inserted 74;; into an existing buffer (although the `so-long' command can be invoked 75;; manually in such situations). 76 77;; * Installation 78;; -------------- 79;; Use M-x global-so-long-mode to enable/toggle the functionality. To enable 80;; the functionality by default, either customize the `global-so-long-mode' user 81;; option, or add the following to your init file: 82;; 83;; ;; Avoid performance issues in files with very long lines. 84;; (global-so-long-mode 1) 85;; 86;; If necessary, ensure that so-long.el is in a directory in your load-path, and 87;; that the library has been loaded. (These steps are not necessary if you are 88;; using Emacs 27+, or have installed the GNU ELPA package.) 89 90;; * Overview of modes and commands 91;; -------------------------------- 92;; - `global-so-long-mode' - A global minor mode which enables the automated 93;; behaviour, causing the user's preferred action to be invoked whenever a 94;; newly-visited file contains excessively long lines. 95;; - `so-long-mode' - A major mode, and the default action. 96;; - `so-long-minor-mode' - A minor mode version of the major mode, and an 97;; alternative action. 98;; - `longlines-mode' - A minor mode provided by the longlines.el library, 99;; and another alternative action. 100;; - `so-long' - Manually invoke the user's preferred action, enabling its 101;; performance improvements for the current buffer. 102;; - `so-long-revert' - Restore the original state of the buffer. 103;; - `so-long-customize' - Configure the user options. 104;; - `so-long-commentary' - Read this documentation in outline-mode. 105 106;; * Usage 107;; ------- 108;; In most cases you will simply enable `global-so-long-mode' and leave it to 109;; act automatically as required, in accordance with your configuration (see 110;; "Basic configuration" below). 111;; 112;; On rare occasions you may choose to manually invoke the `so-long' command, 113;; which invokes your preferred `so-long-action' (exactly as the automatic 114;; behaviour would do if it had detected long lines). You might use this if a 115;; problematic file did not meet your configured criteria, and you wished to 116;; trigger the performance improvements manually. 117;; 118;; It is also possible to directly use `so-long-mode' or `so-long-minor-mode' 119;; (major and minor modes, respectively). Both of these modes are actions 120;; available to `so-long' but, like any other mode, they can be invoked directly 121;; if you have a need to do that (see also "Other ways of using so-long" below). 122;; 123;; If the behaviour ever triggers when you did not want it to, you can use the 124;; `so-long-revert' command to restore the buffer to its original state. 125 126;; * Basic configuration 127;; --------------------- 128;; Use M-x customize-group RET so-long RET 129;; (or M-x so-long-customize RET) 130;; 131;; The user options `so-long-target-modes' and `so-long-threshold' determine 132;; whether action will be taken automatically when visiting a file, and 133;; `so-long-action' determines what will be done. 134 135;; * Actions and menus 136;; ------------------- 137;; The user options `so-long-action' and `so-long-action-alist' determine what 138;; will happen when `so-long' and `so-long-revert' are invoked, and you can add 139;; your own custom actions if you wish. The selected action can be invoked 140;; manually with M-x so-long; and in general M-x so-long-revert will undo the 141;; effects of whichever action was used (which is particularly useful when the 142;; action is triggered automatically, but the detection was a 'false positive'.) 143;; 144;; All defined actions are presented in the "So Long" menu, which is visible 145;; whenever long lines have been detected. Selecting an action from the menu 146;; will firstly cause the current action (if any) to be reverted, before the 147;; newly-selected action is invoked. 148;; 149;; Aside from the menu bar, the menu is also available in the mode line -- 150;; either via the major mode construct (when `so-long-mode' is active), or in 151;; a separate mode line construct when some other major mode is active. 152 153;; * Files with a file-local 'mode' 154;; -------------------------------- 155;; A file-local major mode is likely to be safe even if long lines are detected 156;; (the author of the file would otherwise be unlikely to have set that mode), 157;; and so these files are treated as special cases. When a file-local 'mode' is 158;; present, the function defined by the `so-long-file-local-mode-function' user 159;; option is called. The default value will cause the `so-long-minor-mode' 160;; action to be used instead of the `so-long-mode' action, if the latter was 161;; going to be used for this file. This is still a conservative default, but 162;; this option can also be configured to inhibit so-long entirely in this 163;; scenario, or to not treat a file-local mode as a special case at all. 164 165;; * Buffers which are not displayed in a window 166;; --------------------------------------------- 167;; When a file with long lines is visited and the buffer is not displayed right 168;; away, it may be that it is not intended to be displayed at all, and that it 169;; has instead been visited for behind-the-scenes processing by some library. 170;; Invisible buffers are less likely to cause performance issues, and it also 171;; might be surprising to the other library if such a buffer were manipulated by 172;; `so-long' (which might in turn lead to confusing errors for the user); so in 173;; these situations the `so-long-invisible-buffer-function' value is called 174;; instead. By default this arranges for `so-long' to be invoked on the buffer 175;; if and when it is displayed, but not otherwise. 176;; 177;; This 'deferred call' is actually the most common scenario -- even when a 178;; visited file is displayed "right away", it is normal for the buffer to be 179;; invisible when `global-so-long-mode' processes it, and the gap between 180;; "arranging to call" and "calling" `so-long' is simply extremely brief. 181 182;; * Inhibiting and disabling minor modes 183;; -------------------------------------- 184;; Certain minor modes cause significant performance issues in the presence of 185;; very long lines, and should be disabled automatically in this situation. 186;; 187;; The simple way to disable most buffer-local minor modes is to add the mode 188;; symbol to the `so-long-minor-modes' list. Several modes are targeted by 189;; default, and it is a good idea to customize this variable to add any 190;; additional buffer-local minor modes that you use which you know to have 191;; performance implications. 192;; 193;; These minor modes are disabled if `so-long-action' is set to either 194;; `so-long-mode' or `so-long-minor-mode'. If `so-long-revert' is called, then 195;; the original values are restored. 196;; 197;; In the case of globalized minor modes, be sure to specify the buffer-local 198;; minor mode, and not the global mode which controls it. 199;; 200;; Note that `so-long-minor-modes' is not useful for other global minor modes 201;; (as distinguished from globalized minor modes), but in some cases it will be 202;; possible to inhibit or otherwise counter-act the behaviour of a global mode 203;; by overriding variables, or by employing hooks (see below). You would need 204;; to inspect the code for a given global mode (on a case by case basis) to 205;; determine whether it's possible to inhibit it for a single buffer -- and if 206;; so, how best to do that, as not all modes are alike. 207 208;; * Overriding variables 209;; ---------------------- 210;; `so-long-variable-overrides' is an alist mapping variable symbols to values. 211;; If `so-long-action' is set to either `so-long-mode' or `so-long-minor-mode', 212;; the buffer-local value for each variable in the list is set to the associated 213;; value in the alist. Use this to enforce values which will improve 214;; performance or otherwise avoid undesirable behaviours. If `so-long-revert' 215;; is called, then the original values are restored. 216 217;; * Retaining minor modes and settings when switching to `so-long-mode' 218;; --------------------------------------------------------------------- 219;; A consequence of switching to a new major mode is that many buffer-local 220;; minor modes and variables from the original major mode will be disabled. 221;; For performance purposes this is a desirable trait of `so-long-mode', but 222;; specified modes and variables can also be preserved across the major mode 223;; transition by customizing the `so-long-mode-preserved-minor-modes' and 224;; `so-long-mode-preserved-variables' user options. 225;; 226;; When `so-long-mode' is called, the states of any modes and variables 227;; configured by these options are remembered in the original major mode, and 228;; reinstated after switching to `so-long-mode'. Likewise, if `so-long-revert' 229;; is used to switch back to the original major mode, these modes and variables 230;; are again set to the same states. 231;; 232;; The default values for these options ensure that if `view-mode' was active 233;; in the original mode, then it will also be active in `so-long-mode'. 234 235;; * Hooks 236;; ------- 237;; `so-long-hook' runs at the end of the `so-long' command, after the configured 238;; action has been invoked. 239;; 240;; Likewise, if the `so-long-revert' command is used to restore the original 241;; state then, once that has happened, `so-long-revert-hook' is run. 242;; 243;; The major and minor modes also provide the usual hooks: `so-long-mode-hook' 244;; for the major mode (running between `change-major-mode-after-body-hook' and 245;; `after-change-major-mode-hook'); and `so-long-minor-mode-hook' (when that 246;; mode is enabled or disabled). 247 248;; * Troubleshooting 249;; ----------------- 250;; Any elisp library has the potential to cause performance problems; so while 251;; the default configuration addresses some important common cases, it's 252;; entirely possible that your own config introduces problem cases which are 253;; unknown to this library. 254;; 255;; If visiting a file is still taking a very long time with so-long enabled, 256;; you should test the following command: 257;; 258;; emacs -Q -l so-long -f global-so-long-mode <file> 259;; 260;; For versions of Emacs < 27, use: 261;; emacs -Q -l /path/to/so-long.el -f global-so-long-mode <file> 262;; 263;; If the file loads quickly when that command is used, you'll know that 264;; something in your personal configuration is causing problems. If this 265;; turns out to be a buffer-local minor mode, or a user option, you can 266;; likely alleviate the issue by customizing `so-long-minor-modes' or 267;; `so-long-variable-overrides' accordingly. 268;; 269;; The in-built profiler can be an effective way of discovering the cause 270;; of such problems. Refer to M-: (info "(elisp) Profiling") RET 271;; 272;; In some cases it may be useful to set a file-local `mode' variable to 273;; `so-long-mode', completely bypassing the automated decision process. 274;; Refer to M-: (info "(emacs) Specifying File Variables") RET 275;; 276;; If so-long itself causes problems, disable the automated behaviour with 277;; M-- M-x global-so-long-mode, or M-: (global-so-long-mode 0) 278 279;; * Example configuration 280;; ----------------------- 281;; If you prefer to configure in code rather than via the customize interface, 282;; then you might use something along these lines: 283;; 284;; ;; Enable so-long library. 285;; (when (require 'so-long nil :noerror) 286;; (global-so-long-mode 1) 287;; ;; Basic settings. 288;; (setq so-long-action 'so-long-minor-mode) 289;; (setq so-long-threshold 1000) 290;; (setq so-long-max-lines 100) 291;; ;; Additional target major modes to trigger for. 292;; (mapc (apply-partially #'add-to-list 'so-long-target-modes) 293;; '(sgml-mode nxml-mode)) 294;; ;; Additional buffer-local minor modes to disable. 295;; (mapc (apply-partially #'add-to-list 'so-long-minor-modes) 296;; '(diff-hl-mode diff-hl-amend-mode diff-hl-flydiff-mode)) 297;; ;; Additional variables to override. 298;; (mapc (apply-partially #'add-to-list 'so-long-variable-overrides) 299;; '((show-trailing-whitespace . nil) 300;; (truncate-lines . nil)))) 301 302;; * Mode-specific configuration 303;; ----------------------------- 304;; The `so-long-predicate' function is called in the context of the buffer's 305;; original major mode, and therefore major mode hooks can be used to control 306;; the criteria for calling `so-long' in any given mode (plus its derivatives) 307;; by setting buffer-local values for the variables in question. This includes 308;; `so-long-predicate' itself, as well as any variables used by the predicate 309;; when determining the result. By default this means `so-long-threshold' and 310;; possibly also `so-long-max-lines' and `so-long-skip-leading-comments' (these 311;; latter two are not used by default starting from Emacs 28.1). E.g.: 312;; 313;; (add-hook 'js-mode-hook 'my-js-mode-hook) 314;; 315;; (defun my-js-mode-hook () 316;; "Custom `js-mode' behaviours." 317;; (setq-local so-long-max-lines 100) 318;; (setq-local so-long-threshold 1000)) 319;; 320;; `so-long-variable-overrides' and `so-long-minor-modes' may also be given 321;; buffer-local values in order to apply different settings to different types 322;; of file. For example, the Bidirectional Parentheses Algorithm does not apply 323;; to `<' and `>' characters by default, and therefore one might prefer to not 324;; set `bidi-inhibit-bpa' in XML files, on the basis that XML files with long 325;; lines are less likely to trigger BPA-related performance problems: 326;; 327;; (add-hook 'nxml-mode-hook 'my-nxml-mode-hook) 328;; 329;; (defun my-nxml-mode-hook () 330;; "Custom `nxml-mode' behaviours." 331;; (require 'so-long) 332;; (setq-local so-long-variable-overrides 333;; (remove '(bidi-inhibit-bpa . t) so-long-variable-overrides))) 334;; 335;; Finally, note that setting `so-long-target-modes' to nil buffer-locally in 336;; a major mode hook would prevent that mode from ever being targeted. With 337;; `prog-mode' being targeted by default, specific derivatives of `prog-mode' 338;; could therefore be un-targeted if desired. 339 340;; * Other ways of using so-long 341;; ----------------------------- 342;; It may prove useful to automatically invoke major mode `so-long-mode' for 343;; certain files, irrespective of whether they contain long lines. 344;; 345;; To target specific files and extensions, using `auto-mode-alist' is the 346;; simplest method. To add such an entry, use: 347;; (add-to-list 'auto-mode-alist (cons REGEXP 'so-long-mode)) 348;; Where REGEXP is a regular expression matching the filename. e.g.: 349;; 350;; - Any filename with a particular extension ".foo": 351;; (rx ".foo" eos) 352;; 353;; - Any file in a specific directory: 354;; (rx bos "/path/to/directory/") 355;; 356;; - Only *.c filenames under that directory: 357;; (rx bos "/path/to/directory/" (zero-or-more not-newline) ".c" eos) 358;; 359;; - Match some sub-path anywhere in a filename: 360;; (rx "/sub/path/foo") 361;; 362;; - A specific individual file: 363;; (rx bos "/path/to/file" eos) 364;; 365;; Another way to target individual files is to set a file-local `mode' 366;; variable. Refer to M-: (info "(emacs) Specifying File Variables") RET 367;; 368;; `so-long-minor-mode' can also be called directly if desired. e.g.: 369;; (add-hook 'FOO-mode-hook 'so-long-minor-mode) 370;; 371;; In Emacs 26.1 or later (see "Caveats" below) you also have the option of 372;; using file-local and directory-local variables to determine how `so-long' 373;; behaves. e.g. -*- so-long-action: longlines-mode; -*- 374;; 375;; The buffer-local `so-long-function' and `so-long-revert-function' values may 376;; be set directly (in a major mode hook, for instance), as any existing value 377;; for these variables will be used in preference to the values defined by the 378;; selected action. For file-local or directory-local usage it is preferable to 379;; set only `so-long-action', as all function variables are marked as 'risky', 380;; meaning you would need to add to `safe-local-variable-values' in order to 381;; avoid being queried about them. 382;; 383;; Finally, the `so-long-predicate' user option enables the automated behaviour 384;; to be determined by a custom function, if greater control is needed. 385 386;; * Implementation notes 387;; ---------------------- 388;; This library advises `set-auto-mode' (in order to react after Emacs has 389;; chosen the major mode for a buffer), and `hack-local-variables' (so that we 390;; may behave differently when a file-local mode is set). In earlier versions 391;; of Emacs (< 26.1) we also advise `hack-one-local-variable' (to prevent a 392;; file-local mode from restoring the original major mode if we had changed it). 393;; 394;; Many variables are permanent-local because after the normal major mode has 395;; been set, we potentially change the major mode to `so-long-mode', and it's 396;; important that the values which were established prior to that are retained. 397 398;; * Caveats 399;; --------- 400;; The variables affecting the automated behaviour of this library (such as 401;; `so-long-action') can be used as file- or dir-local values in Emacs 26+, but 402;; not in previous versions of Emacs. This is on account of improvements made 403;; to `normal-mode' in 26.1, which altered the execution order with respect to 404;; when local variables are processed. In earlier versions of Emacs the local 405;; variables are processed too late, and hence have no effect on the decision- 406;; making process for invoking `so-long'. It is unlikely that equivalent 407;; support will be implemented for older versions of Emacs. The exception to 408;; this caveat is the `mode' pseudo-variable, which is processed early in all 409;; versions of Emacs, and can be set to `so-long-mode' if desired. 410 411;; * Change Log: 412;; 413;; 1.1.2 - Use `so-long-mode-line-active' face on `mode-name' in `so-long-mode'. 414;; 1.1.1 - Identical to 1.1, but fixing an incorrect GNU ELPA release. 415;; 1.1 - Utilise `buffer-line-statistics' in Emacs 28+, with the new 416;; `so-long-predicate' function `so-long-statistics-excessive-p'. 417;; - Increase `so-long-threshold' from 250 to 10,000. 418;; - Increase `so-long-max-lines' from 5 to 500. 419;; - Include `fundamental-mode' in `so-long-target-modes'. 420;; - New user option `so-long-mode-preserved-minor-modes'. 421;; - New user option `so-long-mode-preserved-variables'. 422;; 1.0 - Included in Emacs 27.1, and in GNU ELPA for prior versions of Emacs. 423;; - New global mode `global-so-long-mode' to enable/disable the library. 424;; - New user option `so-long-action'. 425;; - New user option `so-long-action-alist' defining alternative actions. 426;; - New user option `so-long-variable-overrides'. 427;; - New user option `so-long-skip-leading-comments'. 428;; - New user option `so-long-file-local-mode-function'. 429;; - New user option `so-long-invisible-buffer-function'. 430;; - New user option `so-long-predicate'. 431;; - New variable and function `so-long-function'. 432;; - New variable and function `so-long-revert-function'. 433;; - New command `so-long' to invoke `so-long-function' interactively. 434;; - New command `so-long-revert' to invoke `so-long-revert-function'. 435;; - New minor mode action `so-long-minor-mode' facilitates retaining the 436;; original major mode, while still disabling minor modes and overriding 437;; variables like the major mode `so-long-mode'. 438;; - Support `longlines-mode' as a `so-long-action' option. 439;; - Added "So Long" menu, including all selectable actions. 440;; - Added mode-line indicator, user option `so-long-mode-line-label', 441;; and faces `so-long-mode-line-active', `so-long-mode-line-inactive'. 442;; - New help commands `so-long-commentary' and `so-long-customize'. 443;; - Refactored the default hook values using variable overrides 444;; (and returning all the hooks to nil default values). 445;; - Performance improvements for `so-long-detected-long-line-p'. 446;; - Converted defadvice to nadvice. 447;; 0.7.6 - Bug fix for `so-long-mode-hook' losing its default value. 448;; 0.7.5 - Documentation. 449;; - Added sgml-mode and nxml-mode to `so-long-target-modes'. 450;; 0.7.4 - Refactored the handling of `whitespace-mode'. 451;; 0.7.3 - Added customization group `so-long' with user options. 452;; - Added `so-long-original-values' to generalise the storage and 453;; restoration of values from the original mode upon `so-long-revert'. 454;; - Added `so-long-revert-hook'. 455;; 0.7.2 - Remember the original major mode even with M-x `so-long-mode'. 456;; 0.7.1 - Clarified interaction with globalized minor modes. 457;; 0.7 - Handle header 'mode' declarations. 458;; - Hack local variables after reverting to the original major mode. 459;; - Reverted `so-long-max-lines' to a default value of 5. 460;; 0.6.5 - Inhibit globalized `hl-line-mode' and `whitespace-mode'. 461;; - Set `buffer-read-only' by default. 462;; 0.6 - Added `so-long-minor-modes' and `so-long-hook'. 463;; 0.5 - Renamed library to "so-long.el". 464;; - Added explicit `so-long-enable' command to activate our advice. 465;; 0.4 - Amended/documented behaviour with file-local 'mode' variables. 466;; 0.3 - Defer to a file-local 'mode' variable. 467;; 0.2 - Initial release to EmacsWiki. 468;; 0.1 - Experimental. 469 470;;; Code: 471 472(require 'cl-lib) 473 474;; Map each :package-version to the associated Emacs version. 475;; (This eliminates the need for explicit :version keywords on the 476;; custom definitions.) 477(add-to-list 'customize-package-emacs-version-alist 478 '(so-long ("1.0" . "27.1") 479 ("1.1" . "28.1"))) 480 481(defconst so-long--latest-version "1.1.2") 482 483(declare-function buffer-line-statistics "fns.c" t t) ;; Emacs 28+ 484(declare-function longlines-mode "longlines") 485(defvar longlines-mode) 486 487(defvar so-long-enabled nil 488 ;; This was initially a renaming of the old `so-long-mode-enabled' and 489 ;; documented as "Set to nil to prevent `so-long' from being triggered 490 ;; automatically."; however `so-long--ensure-enabled' may forcibly re-enable 491 ;; it contrary to the user's expectations, so for the present this should be 492 ;; considered internal-use only (with `global-so-long-mode' the interface 493 ;; for enabling or disabling the automated behaviour). FIXME: Establish a 494 ;; way to support the original use-case, or rename to `so-long--enabled'. 495 "Internal use. Non-nil when any `so-long' functionality has been used.") 496 497(defvar-local so-long--active nil ; internal use 498 "Non-nil when `so-long' mitigations are in effect.") 499 500(defvar so-long--set-auto-mode nil ; internal use 501 "Non-nil while `set-auto-mode' is executing.") 502 503(defvar so-long--hack-local-variables-no-mode nil ; internal use 504 "Non-nil to prevent `hack-local-variables' applying a `mode' variable.") 505 506(defvar-local so-long--inhibited nil ; internal use 507 "When non-nil, prevents the `set-auto-mode' advice from calling `so-long'.") 508(put 'so-long--inhibited 'permanent-local t) 509 510(defvar so-long--calling nil ; internal use 511 ;; This prevents infinite recursion if eval:(so-long) is specified 512 ;; as a file- or dir-local variable, and `so-long-action' is set to 513 ;; `so-long-mode' (as that major mode would once again process the 514 ;; local variables, and hence call itself). 515 "Non-nil while `so-long' or `so-long-revert' is executing.") 516 517(defvar-local so-long-detected-p nil 518 "Non-nil if `so-long' has been invoked (even if subsequently reverted).") 519(put 'so-long-detected-p 'permanent-local t) 520 521(defgroup so-long nil 522 "Prevent unacceptable performance degradation with very long lines." 523 :prefix "so-long" 524 :group 'convenience) 525 526(defcustom so-long-threshold 10000 527 "Maximum line length permitted before invoking `so-long-function'. 528 529Line length is counted in either bytes or characters, depending on 530`so-long-predicate'. 531 532This is the only variable used to determine the presence of long lines if 533the `so-long-predicate' function is `so-long-statistics-excessive-p'." 534 :type 'integer 535 :package-version '(so-long . "1.1")) 536 537(defcustom so-long-max-lines 500 538 "Number of non-blank, non-comment lines to test for excessive length. 539 540This option normally has no effect in Emacs versions >= 28.1, as the default 541`so-long-predicate' sees the entire buffer. Older versions of Emacs still make 542use of this option. 543 544If nil then all lines will be tested, until either a long line is detected, 545or the end of the buffer is reached. 546 547If `so-long-skip-leading-comments' is nil then comments and blank lines will 548be counted. 549 550See `so-long-detected-long-line-p' for details." 551 :type '(choice (integer :tag "Limit") 552 (const :tag "Unlimited" nil)) 553 :package-version '(so-long . "1.1")) 554 555(defcustom so-long-skip-leading-comments t 556 "Non-nil to ignore all leading comments and whitespace. 557 558This option normally has no effect in Emacs versions >= 28.1, as the default 559`so-long-predicate' sees the entire buffer. Older versions of Emacs still make 560use of this option. 561 562If the file begins with a shebang (#!), this option also causes that line to be 563ignored even if it doesn't match the buffer's comment syntax, to ensure that 564comments following the shebang will be ignored. 565 566See `so-long-detected-long-line-p' for details." 567 :type 'boolean 568 :package-version '(so-long . "1.0")) 569 570(defcustom so-long-target-modes 571 '(prog-mode css-mode sgml-mode nxml-mode fundamental-mode) 572 "`global-so-long-mode' affects only these modes and their derivatives. 573 574Our primary use-case is minified programming code, so `prog-mode' covers 575most cases, but there are some exceptions to this. 576 577If t, then all modes are targeted. Note that this is only useful with a 578custom `so-long-predicate', as many file types (archives and binary files, 579for example) can safely contain long lines, and invoking `so-long' on such 580files would prevent Emacs from handling them correctly." 581 ;; Use 'symbol', as 'function' may be unknown => mismatch. 582 :type '(choice (repeat :tag "Specified modes" symbol) 583 (const :tag "All modes" t)) 584 :package-version '(so-long . "1.1")) 585 586(defcustom so-long-invisible-buffer-function #'so-long-deferred 587 "Function called in place of `so-long' when the buffer is not displayed. 588 589This affects the behaviour of `global-so-long-mode'. 590 591We treat invisible buffers differently from displayed buffers because, in 592cases where a library is using a buffer for behind-the-scenes processing, 593it might be surprising if that buffer were unexpectedly manipulated by 594`so-long' (which might in turn lead to confusing errors for the user). 595Invisible buffers are less likely to cause performance issues related to 596long lines, so this differentiation is generally satisfactory. 597 598The default value `so-long-deferred' prevents `global-so-long-mode' from 599triggering `so-long' for any given buffer until such time as the buffer is 600displayed in a window. 601 602\(Note that buffers are normally invisible at this point -- when `find-file' 603is used, the buffer is not displayed in a window until a short time after 604`global-so-long-mode' has seen it.) 605 606The value nil or `so-long' means that `so-long' will be called directly; in 607which case it may be problematic for `so-long-variable-overrides' to enable 608`buffer-read-only', or for `so-long-action' to be set to `so-long-mode'. 609This is because the buffer may not be intended to be displayed at all, and 610the mentioned options might interfere with some intended processing." 611 :type '(radio (const so-long-deferred) 612 (const :tag "nil: Call so-long as normal" nil) 613 (function :tag "Custom function")) 614 :package-version '(so-long . "1.0")) 615 616(defcustom so-long-predicate (if (fboundp 'buffer-line-statistics) 617 'so-long-statistics-excessive-p 618 'so-long-detected-long-line-p) 619 "Function called after `set-auto-mode' to decide whether action is needed. 620 621This affects the behaviour of `global-so-long-mode'. 622 623Only called if the major mode is a member of `so-long-target-modes'. 624 625The specified function will be called with no arguments. If it returns non-nil 626then `so-long' will be invoked. 627 628Defaults to `so-long-statistics-excessive-p' starting from Emacs 28.1, or 629`so-long-detected-long-line-p' in earlier versions. 630 631Note that `so-long-statistics-excessive-p' requires Emacs 28.1 or later." 632 :type '(radio (const so-long-statistics-excessive-p) 633 (const so-long-detected-long-line-p) 634 (function :tag "Custom function")) 635 :package-version '(so-long . "1.1")) 636 637;; Silence byte-compiler warning. `so-long-action-alist' is defined below 638;; as a user option; but the definition sequence required for its setter 639;; function means we also need to declare it beforehand. 640(defvar so-long-action-alist) 641 642(defun so-long--action-type () 643 "Generate a :type for `so-long-action' based on `so-long-action-alist'." 644 ;; :type seemingly cannot be a form to be evaluated on demand, so we 645 ;; endeavour to keep it up-to-date with `so-long-action-alist' by 646 ;; calling this from `so-long--action-alist-setter'. 647 `(radio ,@(mapcar (lambda (x) (list 'const :tag (cadr x) (car x))) 648 (assq-delete-all nil so-long-action-alist)) 649 (const :tag "Do nothing" nil))) 650 651(defun so-long--action-alist-setter (option value) 652 "The customize :set function for `so-long-action-alist'." 653 ;; Set the value as normal. 654 (set-default option value) 655 ;; Update the :type of `so-long-action' to present the updated values. 656 (put 'so-long-action 'custom-type (so-long--action-type))) 657 658(defcustom so-long-action-alist 659 '((so-long-mode 660 "Change major mode to so-long-mode" 661 so-long-mode 662 so-long-mode-revert) 663 (so-long-minor-mode 664 "Enable so-long-minor-mode" 665 turn-on-so-long-minor-mode 666 turn-off-so-long-minor-mode) 667 (longlines-mode 668 "Enable longlines-mode" 669 so-long-function-longlines-mode 670 so-long-revert-function-longlines-mode)) 671 "Options for `so-long-action'. 672 673Each element is a list comprising (KEY LABEL ACTION REVERT) 674 675KEY is a symbol which is a valid value for `so-long-action', and LABEL is a 676string which describes and represents the key in that option's customize 677interface, and in the \"So Long\" menu. ACTION and REVERT are functions: 678 679ACTION will be the `so-long-function' value when `so-long' is called, and 680REVERT will be the `so-long-revert-function' value, if `so-long-revert' is 681subsequently called." 682 :type '(alist :key-type (symbol :tag "Key" :value <action>) 683 :value-type (list (string :tag "Label" :value "<description>") 684 (function :tag "Action") 685 (function :tag "Revert"))) 686 :set #'so-long--action-alist-setter 687 :package-version '(so-long . "1.0")) 688(put 'so-long-action-alist 'risky-local-variable t) 689 690(defcustom so-long-action 'so-long-mode 691 "The action taken by `so-long' when long lines are detected. 692 693\(Long lines are determined by `so-long-predicate' after `set-auto-mode'.) 694 695The value is a key to one of the options defined by `so-long-action-alist'. 696 697The default action is to replace the original major mode with `so-long-mode'. 698Alternatively, the `so-long-minor-mode' action retains the original major mode 699while still disabling minor modes and overriding variables. These are the only 700standard values for which `so-long-minor-modes' and `so-long-variable-overrides' 701will be automatically processed; but custom actions can also do these things. 702 703The value `longlines-mode' causes that minor mode to be enabled. See 704longlines.el for more details. 705 706Each action likewise determines the behaviour of `so-long-revert'. 707 708If the value is nil, or not defined in `so-long-action-alist', then no action 709will be taken." 710 :type (so-long--action-type) 711 :package-version '(so-long . "1.0")) 712 713(defvar-local so-long-function nil 714 "The function called by `so-long'. 715 716This should be set in conjunction with `so-long-revert-function'. This usually 717happens automatically, based on the value of `so-long-action'. 718 719The specified function will be called with no arguments, after which 720`so-long-hook' runs.") 721(put 'so-long-function 'permanent-local t) 722 723(defvar-local so-long-revert-function nil 724 "The function called by `so-long-revert'. 725 726This should be set in conjunction with `so-long-function'. This usually 727happens automatically, based on the value of `so-long-action'. 728 729The specified function will be called with no arguments, after which 730`so-long-revert-hook' runs.") 731(put 'so-long-revert-function 'permanent-local t) 732 733(defun so-long-function (&optional action-arg) 734 "The value of `so-long-function', else derive from `so-long-action'. 735 736If ACTION-ARG is provided, it is used in place of `so-long-action'." 737 (or so-long-function 738 (and (or action-arg 739 (setq action-arg so-long-action)) 740 (let ((action (assq action-arg so-long-action-alist))) 741 (nth 2 action))))) 742 743(defun so-long-revert-function (&optional action-arg) 744 "The value of `so-long-revert-function', else derive from `so-long-action'. 745 746If ACTION-ARG is provided, it is used in place of `so-long-action'." 747 (or so-long-revert-function 748 (and (or action-arg 749 (setq action-arg so-long-action)) 750 (let ((action (assq action-arg so-long-action-alist))) 751 (nth 3 action))))) 752 753(defcustom so-long-file-local-mode-function 'so-long-mode-downgrade 754 "Function to call during `set-auto-mode' when a file-local mode is set. 755 756This affects the behaviour of `global-so-long-mode'. 757 758The specified function will be called with a single argument, being the 759file-local mode which was established. 760 761This happens before `so-long' is called, and so this function can modify the 762subsequent action. 763 764The value `so-long-mode-downgrade' means `so-long-minor-mode' will be used in 765place of `so-long-mode' -- therefore respecting the file-local mode value, yet 766still overriding minor modes and variables (as if `so-long-action' had been set 767to `so-long-minor-mode'). 768 769The value `so-long-inhibit' means that `global-so-long-mode' will not take any 770action at all for this file. 771 772If nil, then do not treat files with file-local modes any differently to other 773files. 774 775Note that this function is called if a file-local mode is set even if `so-long' 776will not be called, and also if the file-local mode is `so-long-mode'. Custom 777functions may need to test for these cases -- see `so-long-mode-downgrade' for 778an example." 779 :type '(radio (const so-long-mode-downgrade) 780 (const so-long-inhibit) 781 (const :tag "nil: Use so-long-function as normal" nil) 782 (function :tag "Custom function")) 783 :package-version '(so-long . "1.0")) 784(make-variable-buffer-local 'so-long-file-local-mode-function) 785 786;; `provided-mode-derived-p' was added in 26.1 787(unless (fboundp 'provided-mode-derived-p) 788 (defun provided-mode-derived-p (mode &rest modes) 789 "Non-nil if MODE is derived from one of MODES. 790Uses the `derived-mode-parent' property of the symbol to trace backwards. 791If you just want to check `major-mode', use `derived-mode-p'." 792 (while (and (not (memq mode modes)) 793 (setq mode (get mode 'derived-mode-parent)))) 794 mode)) 795 796(defun so-long-handle-file-local-mode (mode) 797 "Wrapper for calling `so-long-file-local-mode-function'. 798 799The function is called with one argument, MODE, being the file-local mode which 800was established." 801 ;; Handle the special case whereby the file-local mode was `so-long-mode'. 802 ;; In this instance we set `so-long--inhibited', because the file-local mode 803 ;; is already going to do everything that is wanted. 804 (when (provided-mode-derived-p mode 'so-long-mode) 805 (setq so-long--inhibited t)) 806 ;; Call `so-long-file-local-mode-function'. 807 (when so-long-file-local-mode-function 808 (funcall so-long-file-local-mode-function mode))) 809 810(defcustom so-long-minor-modes 811 ;; In sorted groups. 812 '(font-lock-mode ;; (Generally the most important). 813 ;; Other standard minor modes: 814 display-line-numbers-mode 815 flymake-mode 816 flyspell-mode 817 glasses-mode 818 goto-address-mode 819 goto-address-prog-mode 820 hi-lock-mode 821 highlight-changes-mode 822 hl-line-mode 823 linum-mode 824 nlinum-mode 825 prettify-symbols-mode 826 visual-line-mode 827 whitespace-mode 828 ;; Known third-party modes-of-interest: 829 diff-hl-amend-mode 830 diff-hl-flydiff-mode 831 diff-hl-mode 832 dtrt-indent-mode 833 flycheck-mode 834 hl-sexp-mode 835 idle-highlight-mode 836 rainbow-delimiters-mode 837 smartparens-mode 838 smartparens-strict-mode 839 ) 840 ;; It's not clear to me whether all of these would be problematic, but they 841 ;; seemed like reasonable targets. Some are certainly excessive in smaller 842 ;; buffers of minified code, but we should be aiming to maximise performance 843 ;; by default, so that Emacs is as responsive as we can manage in even very 844 ;; large buffers of minified code. 845 "List of buffer-local minor modes to explicitly disable. 846 847The ones which were originally enabled in the buffer are disabled by calling 848them with the numeric argument 0. Unknown modes, and modes which were not 849enabled, are ignored. 850 851This happens after any globalized minor modes have acted, so that buffer-local 852modes controlled by globalized modes can also be targeted. 853 854By default this happens if `so-long-action' is set to either `so-long-mode' 855or `so-long-minor-mode'. If `so-long-revert' is subsequently invoked, then the 856disabled modes are re-enabled by calling them with the numeric argument 1. 857 858`so-long-hook' can be used where more custom behaviour is desired. 859 860Please submit bug reports to recommend additional modes for this list, whether 861they are in Emacs core, GNU ELPA, or elsewhere." 862 :type '(repeat symbol) ;; not function, as may be unknown => mismatch. 863 :package-version '(so-long . "1.1")) 864 865(defcustom so-long-variable-overrides 866 '((bidi-inhibit-bpa . t) 867 (bidi-paragraph-direction . left-to-right) 868 (buffer-read-only . t) 869 (global-hl-line-mode . nil) 870 (line-move-visual . t) 871 (show-paren-mode . nil) 872 (truncate-lines . nil) 873 (which-func-mode . nil)) 874 "Variables to override, and the values to override them with. 875 876The variables are given buffer-local values. By default this happens if 877`so-long-action' is set to either `so-long-mode' or `so-long-minor-mode'. 878 879If `so-long-revert' is subsequently invoked, then the variables are restored 880to their original states. 881 882The combination of `line-move-visual' (enabled) and `truncate-lines' (disabled) 883is important for maximising responsiveness when moving vertically within an 884extremely long line, as otherwise the full length of the line may need to be 885scanned to find the next position. 886 887Bidirectional text display -- especially handling the large quantities of 888nested parentheses which are liable to occur in minified programming code -- 889can be very expensive for extremely long lines, and so this support is disabled 890by default (insofar as is supported; in particular `bidi-inhibit-bpa' is not 891available in Emacs versions < 27). For more information refer to info node 892`(emacs) Bidirectional Editing' and info node `(elisp) Bidirectional Display'. 893 894Buffers are made read-only by default to prevent potentially-slow editing from 895occurring inadvertently, as buffers with excessively long lines are likely not 896intended to be edited manually." 897 :type '(alist :key-type (variable :tag "Variable") 898 :value-type (sexp :tag "Value")) 899 :options '((bidi-inhibit-bpa boolean) 900 (bidi-paragraph-direction (choice (const left-to-right) 901 (const right-to-left) 902 (const nil))) 903 (buffer-read-only boolean) 904 (global-hl-line-mode boolean) 905 (line-move-visual boolean) 906 (show-paren-mode boolean) 907 (truncate-lines boolean) 908 (which-func-mode boolean)) 909 :package-version '(so-long . "1.0")) 910 911(defcustom so-long-mode-preserved-minor-modes 912 '(view-mode) 913 "List of buffer-local minor modes to preserve in `so-long-mode'. 914 915These will be enabled or disabled after switching to `so-long-mode' (by calling 916them with the numeric argument 1 or 0) in accordance with their state in the 917buffer's original major mode. Unknown modes, and modes which are already in the 918desired state, are ignored. 919 920This happens before `so-long-variable-overrides' and `so-long-minor-modes' 921have been processed. 922 923By default this happens only if `so-long-action' is set to `so-long-mode'. 924If `so-long-revert' is subsequently invoked, then the modes are again set 925to their original state after the original major mode has been called. 926 927See also `so-long-mode-preserved-variables' (processed after this)." 928 :type '(repeat symbol) ;; not function, as may be unknown => mismatch. 929 :package-version '(so-long . "1.1")) 930 931(defcustom so-long-mode-preserved-variables 932 '(view-old-buffer-read-only) 933 "List of buffer-local variables to preserve in `so-long-mode'. 934 935The original value of each variable will be maintained after switching to 936`so-long-mode'. Unknown variables are ignored. 937 938This happens before `so-long-variable-overrides' and `so-long-minor-modes' 939have been processed. 940 941By default this happens only if `so-long-action' is set to `so-long-mode'. 942If `so-long-revert' is subsequently invoked, then the variables are again 943set to their original values after the original major mode has been called. 944 945See also `so-long-mode-preserved-minor-modes' (processed before this)." 946 :type '(repeat variable) 947 :package-version '(so-long . "1.1")) 948 949(defcustom so-long-hook nil 950 "List of functions to call after `so-long' is called. 951 952See also `so-long-revert-hook'." 953 :type 'hook 954 :package-version '(so-long . "1.0")) 955 956(defcustom so-long-revert-hook nil 957 "List of functions to call after `so-long-revert' is called. 958 959See also `so-long-hook'." 960 :type 'hook 961 :package-version '(so-long . "1.0")) 962 963(defcustom so-long-mode-line-label "So Long" 964 "Text label of `so-long-mode-line-info' when long lines are detected. 965 966If nil, no mode line indicator will be displayed." 967 :type '(choice (string :tag "String") 968 (const :tag "None" nil)) 969 :package-version '(so-long . "1.0")) 970 971(defface so-long-mode-line-active 972 '((t :inherit mode-line-emphasis)) 973 "Face for the mode line construct when mitigations are active. 974 975Applied to `mode-name' in the `so-long-mode' major mode, and to 976`so-long-mode-line-label' otherwise (for non-major-mode actions). 977 978See also `so-long-mode-line-info'." 979 :package-version '(so-long . "1.0")) 980 981(defface so-long-mode-line-inactive 982 '((t :inherit mode-line-inactive)) 983 "Face for `so-long-mode-line-info' when mitigations have been reverted." 984 :package-version '(so-long . "1.0")) 985 986;; Modes that go slowly and line lengths excessive 987;; Font-lock performance becoming oppressive 988;; All of my CPU tied up with strings 989;; These are a few of my least-favourite things 990 991(defvar-local so-long-original-values nil 992 "Alist holding the buffer's original `major-mode' value, and other data. 993 994Any values to be restored by `so-long-revert' can be stored here by the 995`so-long-function' or during `so-long-hook'. `so-long' itself stores the 996original states for `so-long-variable-overrides' and `so-long-minor-modes', 997so these values are available to custom actions by default. 998 999See also `so-long-remember' and `so-long-original'.") 1000(put 'so-long-original-values 'permanent-local t) 1001 1002(defun so-long-original (key &optional exists) 1003 "Return the current value for KEY in `so-long-original-values'. 1004 1005If you need to differentiate between a stored value of nil and no stored value 1006at all, make EXISTS non-nil. This then returns the result of `assq' directly: 1007nil if no value was set, and a cons cell otherwise." 1008 (if exists 1009 (assq key so-long-original-values) 1010 (cadr (assq key so-long-original-values)))) 1011 1012(defun so-long-remember (variable) 1013 "Store the value of VARIABLE in `so-long-original-values'. 1014 1015We additionally store a boolean value which indicates whether that value was 1016buffer-local." 1017 (when (boundp variable) 1018 (setq so-long-original-values 1019 (assq-delete-all variable so-long-original-values)) 1020 (push (list variable 1021 (symbol-value variable) 1022 (local-variable-p variable)) 1023 so-long-original-values))) 1024 1025(defun so-long-remember-all (&optional reset) 1026 "Remember the current variable and minor mode values. 1027 1028Stores the existing value for each entry in `so-long-variable-overrides'. 1029Stores the name of each enabled mode from the list `so-long-minor-modes'. 1030 1031The lists themselves are also remembered, so that major mode hooks can 1032provide buffer-local modifications which are still accessible after changing 1033to `so-long-mode'. 1034 1035If RESET is non-nil, remove any existing values before storing the new ones." 1036 (when reset 1037 (setq so-long-original-values nil)) 1038 (so-long-remember 'so-long-variable-overrides) 1039 (so-long-remember 'so-long-minor-modes) 1040 (so-long-remember 'so-long-mode-preserved-variables) 1041 (so-long-remember 'so-long-mode-preserved-minor-modes) 1042 (dolist (ovar so-long-variable-overrides) 1043 (so-long-remember (car ovar))) 1044 (dolist (mode so-long-minor-modes) 1045 (when (and (boundp mode) mode) 1046 (so-long-remember mode))) 1047 (dolist (var so-long-mode-preserved-variables) 1048 (so-long-remember var)) 1049 (dolist (mode so-long-mode-preserved-minor-modes) 1050 (when (and (boundp mode) mode) 1051 (so-long-remember mode)))) 1052 1053(defun so-long-menu () 1054 "Dynamically generate the \"So Long\" menu." 1055 ;; (info "(elisp) Menu Example") 1056 (let ((map (make-sparse-keymap "So Long")) 1057 (help-map (make-sparse-keymap "Help"))) 1058 ;; `so-long-revert'. 1059 (define-key-after map [so-long-revert] 1060 '(menu-item "Revert to normal" so-long-revert 1061 :enable (and so-long-revert-function 1062 so-long--active))) 1063 ;; `so-long-menu-item-replace-action' over `so-long-action-alist'. 1064 (define-key-after map [so-long-actions-separator] 1065 '(menu-item "--")) 1066 (dolist (item so-long-action-alist) 1067 (cl-destructuring-bind (key label actionfunc revertfunc) 1068 item 1069 (define-key-after map (vector key) 1070 `(menu-item 1071 ,label 1072 ,(let ((sym (make-symbol "so-long-menu-item-replace-action"))) 1073 ;; We make a symbol so that `describe-key' on the menu item 1074 ;; produces something more descriptive than byte code. There is 1075 ;; no interned `so-long-menu-item-replace-action' which might 1076 ;; make this slightly confusing -- but only in the rare situation 1077 ;; when someone uses `describe-key' on one of these menu items, 1078 ;; and then wants to find more information. We mitigate this by 1079 ;; making the following docstring very clear. 1080 (defalias sym (lambda () (interactive "@") (so-long key)) 1081 ;; We use "@" as commands in the mode-line menu may be 1082 ;; triggered by mouse when some other window is selected. 1083 "Revert the current action and invoke the chosen replacement. 1084 1085This command calls `so-long' with the selected action as an argument.") 1086 sym) 1087 :enable (not (and so-long--active 1088 (eq ',actionfunc so-long-function) 1089 (eq ',revertfunc so-long-revert-function))))))) 1090 ;; "Help" sub-menu. 1091 (define-key-after map [so-long-help-separator] 1092 '(menu-item "--")) 1093 (define-key-after map [so-long-help] 1094 `(menu-item "Help" ,help-map)) 1095 (define-key-after help-map [so-long-commentary] 1096 '(menu-item "Commentary" so-long-commentary)) 1097 (define-key-after help-map [so-long-customize] 1098 '(menu-item "Customize" so-long-customize)) 1099 map)) 1100 1101;;;###autoload 1102(defun so-long-commentary () 1103 "View the `so-long' library's documentation in `outline-mode'." 1104 (interactive) 1105 (let ((buf "*So Long: Commentary*")) 1106 (when (buffer-live-p (get-buffer buf)) 1107 (kill-buffer buf)) 1108 ;; Use `finder-commentary' to generate the buffer. 1109 (require 'finder) 1110 (cl-letf (((symbol-function 'finder-summary) #'ignore)) 1111 (finder-commentary "so-long")) 1112 (let ((inhibit-read-only t)) 1113 (if (looking-at "^Commentary:\n\n") 1114 (replace-match "so-long.el\n\n") 1115 (insert "so-long.el\n") 1116 (forward-line 1)) 1117 (save-excursion 1118 (while (re-search-forward "^-+$" nil :noerror) 1119 (replace-match "")))) 1120 (rename-buffer buf) 1121 ;; Enable `outline-mode' and `view-mode' for user convenience. 1122 (outline-mode) 1123 (declare-function outline-next-visible-heading "outline") 1124 (declare-function outline-previous-visible-heading "outline") 1125 (declare-function outline-toggle-children "outline") 1126 (declare-function outline-toggle-children "outline") 1127 (view-mode 1) 1128 ;; Add some custom local bindings. 1129 (let ((map (make-sparse-keymap))) 1130 (define-key map (kbd "TAB") #'outline-toggle-children) 1131 (define-key map (kbd "<M-tab>") #'outline-toggle-children) 1132 (define-key map (kbd "M-n") #'outline-next-visible-heading) 1133 (define-key map (kbd "M-p") #'outline-previous-visible-heading) 1134 (set-keymap-parent map (current-local-map)) 1135 (use-local-map map)) 1136 ;; Display the So Long menu. 1137 (so-long--ensure-enabled) 1138 (let ((so-long-action nil)) 1139 (so-long)))) 1140 1141;;;###autoload 1142(defun so-long-customize () 1143 "Open the customization group `so-long'." 1144 (interactive) 1145 (customize-group 'so-long)) 1146 1147(defvar-local so-long-mode-line-info nil 1148 "Mode line construct displayed when `so-long' has been triggered. 1149 1150Displayed as part of `mode-line-misc-info'. 1151 1152`so-long-mode-line-label' defines the text to be displayed (if any). 1153 1154Face `so-long-mode-line-active' is used while mitigations are active, and 1155`so-long-mode-line-inactive' is used if `so-long-revert' is called. 1156 1157Not displayed when `so-long-mode' is enabled, as the major mode construct 1158serves the same purpose.") 1159 1160;; Ensure we can display text properties on this value in the mode line. 1161;; See (info "(elisp) Mode Line Data") or (info "(elisp) Properties in Mode"). 1162(put 'so-long-mode-line-info 'risky-local-variable t) 1163 1164(defun so-long-mode-line-info () 1165 "Return the mode line construct for variable `so-long-mode-line-info'." 1166 (let ((map (make-sparse-keymap))) 1167 (define-key map (kbd "<mode-line> <down-mouse-1>") 1168 `(menu-item "" nil 1169 :filter ,(lambda (_cmd) (so-long-menu)))) 1170 ;; Mode line construct. 1171 ;; n.b. It's necessary for `so-long-mode-line-info' to have a non-nil 1172 ;; risky-local-variable property, as otherwise the text properties won't 1173 ;; be rendered. 1174 `(so-long-mode-line-label 1175 ("" (:eval (propertize so-long-mode-line-label 1176 'mouse-face 'highlight 1177 'keymap ',map 1178 'help-echo t ;; Suppress the mode-line value 1179 'face (if so-long--active 1180 'so-long-mode-line-active 1181 'so-long-mode-line-inactive))) 1182 " ")))) 1183 1184;; When the line's long 1185;; When the mode's slow 1186;; When Emacs is sad 1187;; We change automatically to faster code 1188;; And then I won't feel so mad 1189 1190(defun so-long-statistics-excessive-p () 1191 "Non-nil if the buffer contains a line longer than `so-long-threshold' bytes. 1192 1193This uses `buffer-line-statistics' (available from Emacs 28.1) to establish the 1194longest line in the buffer (counted in bytes rather than characters). 1195 1196This is the default value of `so-long-predicate' in Emacs versions >= 28.1. 1197\(In earlier versions `so-long-detected-long-line-p' is used by default.)" 1198 (> (cadr (buffer-line-statistics)) 1199 so-long-threshold)) 1200 1201(defun so-long-detected-long-line-p () 1202 "Determine whether the current buffer contains long lines. 1203 1204Following any initial comments and blank lines, the next N lines of the buffer 1205will be tested for excessive length (where \"excessive\" means greater than 1206`so-long-threshold' characters, and N is `so-long-max-lines'). 1207 1208Returns non-nil if any such excessive-length line is detected. 1209 1210If `so-long-skip-leading-comments' is nil then the N lines will be counted 1211starting from the first line of the buffer. In this instance you will likely 1212want to increase `so-long-max-lines' to allow for possible comments. 1213 1214This is the default `so-long-predicate' function in Emacs versions < 28.1. 1215\(Starting from 28.1, the default and recommended predicate function is 1216`so-long-statistics-excessive-p', which is faster and sees the entire buffer.)" 1217 (let ((count 0) start) 1218 (save-excursion 1219 (goto-char (point-min)) 1220 (when (and so-long-skip-leading-comments 1221 (or comment-use-syntax ;; Refer to `comment-forward'. 1222 (and comment-start-skip comment-end-skip))) 1223 ;; Skip the shebang line, if any. This is not necessarily comment 1224 ;; syntax, so we need to treat it specially. 1225 (when (looking-at "#!") 1226 (forward-line 1)) 1227 ;; Move past any leading whitespace and/or comments. 1228 ;; We use narrowing to limit the amount of text being processed at any 1229 ;; given time, where possible, as this makes things more efficient. 1230 (setq start (point)) 1231 (while (save-restriction 1232 (narrow-to-region start (min (+ (point) so-long-threshold) 1233 (point-max))) 1234 (goto-char start) 1235 ;; Possibilities for `comment-forward' are: 1236 ;; 0. No comment; no movement; return nil. 1237 ;; 1. Comment is <= point-max; move end of comment; return t. 1238 ;; 2. Comment is truncated; move point-max; return nil. 1239 ;; 3. Only whitespace; move end of WS; return nil. 1240 (prog1 (or (comment-forward 1) ;; Moved past a comment. 1241 (and (eobp) ;; Truncated, or WS up to point-max. 1242 (progn ;; Widen and retry. 1243 (widen) 1244 (goto-char start) 1245 (comment-forward 1)))) 1246 ;; Otherwise there was no comment, and we return nil. 1247 ;; If there was whitespace, we moved past it. 1248 (setq start (point))))) 1249 ;; We're at the first non-comment line, but we may have moved past 1250 ;; indentation whitespace, so move back to the beginning of the line 1251 ;; unless we're at the end of the buffer (in which case there was no 1252 ;; non-comment/whitespace content in the buffer at all). 1253 (unless (eobp) 1254 (forward-line 0))) 1255 ;; Start looking for long lines. 1256 ;; `while' will ultimately return nil if we do not `throw' a result. 1257 (catch 'excessive 1258 (while (and (not (eobp)) 1259 (or (not so-long-max-lines) 1260 (< count so-long-max-lines))) 1261 (setq start (point)) 1262 (save-restriction 1263 (narrow-to-region start (min (+ start 1 so-long-threshold) 1264 (point-max))) 1265 (forward-line 1)) 1266 ;; If point is not now at the beginning of a line, then the previous 1267 ;; line was long -- with the exception of when point is at the end of 1268 ;; the buffer (bearing in mind that we have widened again), in which 1269 ;; case there was a short final line with no newline. There is an 1270 ;; edge case when such a final line is exactly (1+ so-long-threshold) 1271 ;; chars long, so if we're at (eobp) we need to verify the length in 1272 ;; order to be consistent. 1273 (unless (or (bolp) 1274 (and (eobp) (<= (- (point) start) 1275 so-long-threshold))) 1276 (throw 'excessive t)) 1277 (setq count (1+ count))))))) 1278 1279(defun so-long-function-longlines-mode () 1280 "Enable minor mode `longlines-mode'." 1281 (require 'longlines) 1282 (so-long-remember 'longlines-mode) 1283 (longlines-mode 1)) 1284 1285(defun so-long-revert-function-longlines-mode () 1286 "Restore original state of `longlines-mode'." 1287 (require 'longlines) 1288 (let ((state (so-long-original 'longlines-mode :exists))) 1289 (if state 1290 (unless (equal (cadr state) longlines-mode) 1291 (longlines-mode (if (cadr state) 1 0))) 1292 (longlines-mode 0)))) 1293 1294(defun turn-on-so-long-minor-mode () 1295 "Enable minor mode `so-long-minor-mode'." 1296 (so-long-minor-mode 1)) 1297 1298(defun turn-off-so-long-minor-mode () 1299 "Disable minor mode `so-long-minor-mode'." 1300 (so-long-minor-mode 0)) 1301 1302;;;###autoload 1303(define-minor-mode so-long-minor-mode 1304 "This is the minor mode equivalent of `so-long-mode'. 1305 1306Any active minor modes listed in `so-long-minor-modes' are disabled for the 1307current buffer, and buffer-local values are assigned to variables in accordance 1308with `so-long-variable-overrides'. 1309 1310This minor mode is a standard `so-long-action' option." 1311 :lighter nil 1312 (if so-long-minor-mode ;; We are enabling the mode. 1313 (progn 1314 ;; Housekeeping. `so-long-minor-mode' might be invoked directly rather 1315 ;; than via `so-long', so replicate the necessary behaviours. The minor 1316 ;; mode also cares about whether `so-long' was already active, as we do 1317 ;; not want to remember values which were (potentially) overridden 1318 ;; already. 1319 (unless (or so-long--calling so-long--active) 1320 (so-long--ensure-enabled) 1321 (setq so-long--active t 1322 so-long-detected-p t 1323 so-long-function 'turn-on-so-long-minor-mode 1324 so-long-revert-function 'turn-off-so-long-minor-mode) 1325 (so-long-remember-all :reset) 1326 (unless (derived-mode-p 'so-long-mode) 1327 (setq so-long-mode-line-info (so-long-mode-line-info)))) 1328 ;; Now perform the overrides. 1329 (so-long-disable-minor-modes) 1330 (so-long-override-variables)) 1331 ;; We are disabling the mode. 1332 (unless so-long--calling ;; Housekeeping. 1333 (when (eq so-long-function 'turn-on-so-long-minor-mode) 1334 (setq so-long--active nil)) 1335 (unless (derived-mode-p 'so-long-mode) 1336 (setq so-long-mode-line-info (so-long-mode-line-info)))) 1337 ;; Restore the overridden settings. 1338 (so-long-restore-minor-modes) 1339 (so-long-restore-variables))) 1340 1341;; How do you solve a problem like a long line? 1342;; How do you stop a mode from slowing down? 1343;; How do you cope with processing a long line? 1344;; A bit of advice! A mode! A workaround! 1345 1346(defvar so-long-mode-map 1347 (let ((map (make-sparse-keymap))) 1348 (define-key map (kbd "C-c C-c") 'so-long-revert) 1349 ;; Define the major mode menu. We have an awkward issue whereby 1350 ;; [menu-bar so-long] is already defined in the global map and is 1351 ;; :visible so-long-detected-p, but we also want this to be 1352 ;; available via the major mode construct in the mode line. 1353 ;; The following achieves the desired end result, as :visible nil 1354 ;; prevents this from duplicating its contents in the menu bar, 1355 ;; but still includes it in the mode line. 1356 (define-key map [menu-bar so-long] 1357 `(menu-item "" nil 1358 :visible nil 1359 :filter ,(lambda (_cmd) (so-long-menu)))) 1360 map) 1361 "Major mode keymap and menu for `so-long-mode'.") 1362 1363;;;###autoload 1364(define-derived-mode so-long-mode nil 1365 (propertize "So Long" 'face 'so-long-mode-line-active) 1366 "This major mode is the default `so-long-action' option. 1367 1368The normal reason for this mode being active is that `global-so-long-mode' is 1369enabled, and `so-long-predicate' has detected that the file contains long lines. 1370 1371Many Emacs modes struggle with buffers which contain excessively long lines, 1372and may consequently cause unacceptable performance issues. 1373 1374This is commonly on account of \"minified\" code (i.e. code that has been 1375compacted into the smallest file size possible, which often entails removing 1376newlines should they not be strictly necessary). These kinds of files are 1377typically not intended to be edited, so not providing the usual editing mode 1378in these cases will rarely be an issue. 1379 1380This major mode disables any active minor modes listed in `so-long-minor-modes' 1381for the current buffer, and buffer-local values are assigned to variables in 1382accordance with `so-long-variable-overrides'. 1383 1384To restore the original major mode (along with the minor modes and variable 1385values), despite potential performance issues, type \\[so-long-revert]. 1386 1387Use \\[so-long-commentary] for more information. 1388 1389Use \\[so-long-customize] to open the customization group `so-long' to 1390configure the behaviour." 1391 ;; Housekeeping. `so-long-mode' might be invoked directly rather than via 1392 ;; `so-long', so replicate the necessary behaviours. We could use this same 1393 ;; test in `so-long-after-change-major-mode' to run `so-long-hook', but that's 1394 ;; not so obviously the right thing to do, so I've omitted it for now. 1395 (unless so-long--calling 1396 (so-long--ensure-enabled) 1397 (setq so-long--active t 1398 so-long-detected-p t 1399 so-long-function 'so-long-mode 1400 so-long-revert-function 'so-long-mode-revert)) 1401 ;; Use `after-change-major-mode-hook' to disable minor modes and override 1402 ;; variables. Append, to act after any globalized modes have acted. 1403 (add-hook 'after-change-major-mode-hook 1404 'so-long-after-change-major-mode :append :local) 1405 ;; Override variables. This is the first of two instances where we do this 1406 ;; (the other being `so-long-after-change-major-mode'). It is desirable to 1407 ;; set variables here in order to cover cases where the setting of a variable 1408 ;; influences how a global minor mode behaves in this buffer. 1409 (so-long-override-variables) 1410 ;; Hide redundant mode-line information (our major mode info replicates this). 1411 (setq so-long-mode-line-info nil) 1412 ;; Inform the user about our major mode hijacking. 1413 (unless (or so-long--inhibited so-long--set-auto-mode) 1414 (message (concat "Changed to %s (from %s)" 1415 (unless (or (eq this-command 'so-long) 1416 (and (symbolp this-command) 1417 (provided-mode-derived-p this-command 1418 'so-long-mode))) 1419 " on account of line length") 1420 ". %s to revert.") 1421 major-mode 1422 (or (so-long-original 'major-mode) "<unknown>") 1423 (substitute-command-keys "\\[so-long-revert]")))) 1424 1425(defun so-long--change-major-mode () 1426 ;; Advice, enabled with: 1427 ;; (advice-add 'so-long-mode :before #'so-long--change-major-mode) 1428 ;; 1429 ;; n.b. `major-mode-suspend' and `major-mode-restore' are new in Emacs 27, and 1430 ;; related to what we're doing here; but it's not worth going to the effort of 1431 ;; using those (conditionally, only for 27+) when we have our own framework 1432 ;; for remembering and restoring this buffer state (amongst other things). 1433 "Ensure that `so-long-mode' knows the original `major-mode'. 1434 1435This advice acts before `so-long-mode', with the previous mode still active." 1436 (unless (derived-mode-p 'so-long-mode) 1437 ;; Housekeeping. `so-long-mode' might be invoked directly rather than 1438 ;; via `so-long', so replicate the necessary behaviours. 1439 (unless so-long--calling 1440 (so-long-remember-all :reset)) 1441 ;; Remember the original major mode, regardless. 1442 (so-long-remember 'major-mode))) 1443 1444(advice-add 'so-long-mode :before #'so-long--change-major-mode) 1445 1446(defun so-long-after-change-major-mode () 1447 "Run by `so-long-mode' in `after-change-major-mode-hook'. 1448 1449Calls `so-long-disable-minor-modes' and `so-long-override-variables'." 1450 ;; Check/set the state of 'preserved' variables and minor modes. 1451 ;; (See also `so-long-mode-revert'.) 1452 ;; The "modes before variables" sequence is important for the default 1453 ;; preserved mode `view-mode' which remembers the `buffer-read-only' state 1454 ;; (which is also permanent-local). That causes problems unless we restore 1455 ;; the original value of `view-old-buffer-read-only' after; otherwise the 1456 ;; sequence `view-mode' -> `so-long' -> `so-long-revert' -> `view-mode' 1457 ;; results in `view-mode' being disabled but the buffer still read-only. 1458 (so-long-mode-maintain-preserved-minor-modes) 1459 (so-long-mode-maintain-preserved-variables) 1460 ;; Disable minor modes. 1461 (so-long-disable-minor-modes) 1462 ;; Override variables (again). We already did this in `so-long-mode' in 1463 ;; order that variables which affect global/globalized minor modes can have 1464 ;; that effect; however it's feasible that one of the minor modes disabled 1465 ;; above might have reverted one of these variables, so we re-enforce them. 1466 ;; (For example, disabling `visual-line-mode' sets `line-move-visual' to 1467 ;; nil, when for our purposes it is preferable for it to be non-nil). 1468 (so-long-override-variables)) 1469 1470(defun so-long-disable-minor-modes () 1471 "Disable any active minor modes listed in `so-long-minor-modes'." 1472 (dolist (mode (so-long-original 'so-long-minor-modes)) 1473 (when (and (boundp mode) 1474 (symbol-value mode)) 1475 (funcall mode 0)))) 1476 1477(defun so-long-restore-minor-modes () 1478 "Restore the minor modes which were disabled. 1479 1480The modes are enabled in accordance with what was remembered in `so-long'." 1481 (dolist (mode (so-long-original 'so-long-minor-modes)) 1482 (when (and (so-long-original mode) 1483 (boundp mode) 1484 (not (symbol-value mode))) 1485 (funcall mode 1)))) 1486 1487(defun so-long-override-variables () 1488 "Set the buffer-local values defined by `so-long-variable-overrides'." 1489 (dolist (ovar (so-long-original 'so-long-variable-overrides)) 1490 (set (make-local-variable (car ovar)) (cdr ovar)))) 1491 1492(defun so-long-restore-variables () 1493 "Restore the remembered values for the overridden variables. 1494 1495The variables are set in accordance with what was remembered in `so-long'." 1496 (dolist (ovar (so-long-original 'so-long-variable-overrides)) 1497 (so-long-restore-variable (car ovar)))) 1498 1499(defun so-long-restore-variable (variable) 1500 "Restore the remembered value (if any) for VARIABLE." 1501 ;; In the instance where `so-long-mode-revert' has just reverted the major 1502 ;; mode, note that `kill-all-local-variables' was already called by the 1503 ;; original mode function, and so these 'overridden' variables may now have 1504 ;; global rather than buffer-local values (if they are not permanent-local). 1505 (let* ((remembered (so-long-original variable :exists)) 1506 (originally-local (nth 2 remembered))) 1507 (if originally-local 1508 ;; The variable originally existed with a buffer-local value, so we 1509 ;; restore it as such (even if the global value is a match). 1510 (set (make-local-variable variable) (cadr remembered)) 1511 ;; Either this variable did not exist initially, or it did not have a 1512 ;; buffer-local value at that time. In either case we kill the current 1513 ;; buffer-local value (if any) in order to restore the original state. 1514 ;; 1515 ;; It's possible that the global value has *changed* in the interim; but 1516 ;; we can't know whether it's best to use the new global value, or retain 1517 ;; the old value as a buffer-local value, so we keep it simple. 1518 (kill-local-variable variable)))) 1519 1520(defun so-long-mode-maintain-preserved-variables () 1521 "Set any 'preserved' variables. 1522 1523The variables are set in accordance with what was remembered in `so-long'." 1524 (dolist (var (so-long-original 'so-long-mode-preserved-variables)) 1525 (so-long-restore-variable var))) 1526 1527(defun so-long-mode-maintain-preserved-minor-modes () 1528 "Enable or disable 'preserved' minor modes. 1529 1530The modes are set in accordance with what was remembered in `so-long'." 1531 (dolist (mode (so-long-original 'so-long-mode-preserved-minor-modes)) 1532 (when (boundp mode) 1533 (let ((original (so-long-original mode)) 1534 (current (symbol-value mode))) 1535 (unless (equal current original) 1536 (funcall mode (if original 1 0))))))) 1537 1538(defun so-long-mode-revert () 1539 "Call the `major-mode' which was selected before `so-long-mode' replaced it. 1540 1541Re-process local variables, and restore overridden variables and minor modes. 1542 1543This is the `so-long-revert-function' for `so-long-mode'." 1544 (interactive) 1545 (let ((so-long-original-mode (so-long-original 'major-mode))) 1546 (unless so-long-original-mode 1547 (error "Original mode unknown")) 1548 (funcall so-long-original-mode) 1549 ;; Emacs 26+ has already called `hack-local-variables' (during 1550 ;; `run-mode-hooks'; provided there was a `buffer-file-name'), but for older 1551 ;; versions we need to call it here. In Emacs 26+ the revised 'HANDLE-MODE' 1552 ;; argument is set to `no-mode' (being the non-nil-and-non-t behaviour), 1553 ;; which we mimic here by binding `so-long--hack-local-variables-no-mode', 1554 ;; in order to prevent a local 'mode' variable from clobbering the major 1555 ;; mode we have just called. 1556 (when (< emacs-major-version 26) 1557 (let ((so-long--hack-local-variables-no-mode t)) 1558 (hack-local-variables))) 1559 ;; Restore minor modes. 1560 (so-long-restore-minor-modes) 1561 ;; Restore overridden variables. 1562 ;; `kill-all-local-variables' was already called by the original mode 1563 ;; function, so we may be seeing global values. 1564 (so-long-restore-variables) 1565 ;; Check/set the state of 'preserved' variables and minor modes. 1566 ;; (Refer to `so-long-after-change-major-mode' regarding the sequence.) 1567 (so-long-mode-maintain-preserved-minor-modes) 1568 (so-long-mode-maintain-preserved-variables) 1569 ;; Restore the mode line construct. 1570 (unless (derived-mode-p 'so-long-mode) 1571 (setq so-long-mode-line-info (so-long-mode-line-info))))) 1572 1573(defun so-long-mode-downgrade (&optional mode) 1574 "The default value for `so-long-file-local-mode-function'. 1575 1576A buffer-local \"downgrade\" from `so-long-mode' to `so-long-minor-mode'. 1577 1578When `so-long-function' is set to `so-long-mode', then we change it to 1579`turn-on-so-long-minor-mode' instead -- retaining the file-local major 1580mode, but still doing everything else that `so-long-mode' would have done. 1581`so-long-revert-function' is likewise updated. 1582 1583If `so-long-function' has any value other than `so-long-mode', we do nothing, 1584as if `so-long-file-local-mode-function' was nil. 1585 1586We also do nothing if MODE (the file-local mode) has the value `so-long-mode', 1587because we do not want to downgrade the major mode in that scenario." 1588 ;; Do nothing if the file-local mode was `so-long-mode'. 1589 (unless (provided-mode-derived-p mode 'so-long-mode) 1590 ;; Act only if `so-long-mode' would be enabled by the current action. 1591 (when (and (symbolp (so-long-function)) 1592 (provided-mode-derived-p (so-long-function) 'so-long-mode)) 1593 ;; Downgrade from `so-long-mode' to the `so-long-minor-mode' behaviour. 1594 (setq so-long-function 'turn-on-so-long-minor-mode 1595 so-long-revert-function 'turn-off-so-long-minor-mode)))) 1596 1597(defun so-long-inhibit (&optional _mode) 1598 "Prevent `global-so-long-mode' from having any effect. 1599 1600This is a `so-long-file-local-mode-function' option." 1601 (setq so-long--inhibited t)) 1602 1603(defun so-long--check-header-modes () 1604 ;; See also "Files with a file-local 'mode'" in the Commentary. 1605 "Handle the header-comments processing in `set-auto-mode'. 1606 1607`set-auto-mode' has some special-case code to handle the `mode' pseudo-variable 1608when set in the header comment. This runs outside of `hack-local-variables' 1609and cannot be conveniently intercepted, so we are forced to replicate it here. 1610 1611This special-case code will ultimately be removed from Emacs, as it exists to 1612deal with a deprecated feature; but until then we need to replicate it in order 1613to inhibit our own behaviour in the presence of a header comment `mode' 1614declaration. 1615 1616If a file-local mode is detected in the header comment, then we call the 1617function defined by `so-long-file-local-mode-function'." 1618 ;; The following code for processing MODE declarations in the header 1619 ;; comments is copied verbatim from `set-auto-mode', because we have 1620 ;; no way of intercepting it. 1621 ;; 1622 (let ((try-locals (not (inhibit-local-variables-p))) 1623 end _done _mode modes) 1624 ;; Once we drop the deprecated feature where mode: is also allowed to 1625 ;; specify minor-modes (ie, there can be more than one "mode:"), we can 1626 ;; remove this section and just let (hack-local-variables t) handle it. 1627 ;; Find a -*- mode tag. 1628 (save-excursion 1629 (goto-char (point-min)) 1630 (skip-chars-forward " \t\n") 1631 ;; Note by design local-enable-local-variables does not matter here. 1632 (and enable-local-variables 1633 try-locals 1634 (setq end (set-auto-mode-1)) 1635 (if (save-excursion (search-forward ":" end t)) 1636 ;; Find all specifications for the `mode:' variable 1637 ;; and execute them left to right. 1638 (while (let ((case-fold-search t)) 1639 (or (and (looking-at "mode:") 1640 (goto-char (match-end 0))) 1641 (re-search-forward "[ \t;]mode:" end t))) 1642 (skip-chars-forward " \t") 1643 (let ((beg (point))) 1644 (if (search-forward ";" end t) 1645 (forward-char -1) 1646 (goto-char end)) 1647 (skip-chars-backward " \t") 1648 (push (intern (concat (downcase (buffer-substring 1649 beg (point))) 1650 "-mode")) 1651 modes))) 1652 ;; Simple -*-MODE-*- case. 1653 (push (intern (concat (downcase (buffer-substring (point) end)) 1654 "-mode")) 1655 modes)))) 1656 1657 ;; Now process the resulting mode list for `so-long--set-auto-mode'. 1658 ;; If any modes were listed, we assume that one of them is a major mode. 1659 ;; It's possible that this isn't true, but the buffer would remain in 1660 ;; fundamental-mode if that were the case, so it is very unlikely. 1661 ;; For the purposes of passing a value to `so-long-handle-file-local-mode' 1662 ;; we assume the major mode was the first mode specified (in which case it 1663 ;; is the last in the list). 1664 (when modes 1665 (so-long-handle-file-local-mode (car (last modes)))))) 1666 1667;; Lisp advice, Lisp advice 1668;; Every calling you greet me 1669;; Code of mine, redefined 1670;; You look happy to tweak me 1671 1672(defun so-long--hack-local-variables (orig-fun &optional handle-mode &rest args) 1673 ;; Advice, enabled with: 1674 ;; (advice-add 'hack-local-variables :around #'so-long--hack-local-variables) 1675 ;; 1676 ;; See also "Files with a file-local 'mode'" in the Commentary. 1677 "Enable `global-so-long-mode' to defer to file-local mode declarations. 1678 1679If a file-local mode is detected, then we call the function defined by 1680`so-long-file-local-mode-function'. 1681 1682This advice acts after the HANDLE-MODE:t call to `hack-local-variables'. 1683\(MODE-ONLY in Emacs versions < 26). 1684 1685File-local header comments are currently an exception, and are processed by 1686`so-long--check-header-modes' (see which for details)." 1687 ;; The first arg to `hack-local-variables' is HANDLE-MODE since Emacs 26.1, 1688 ;; and MODE-ONLY in earlier versions. In either case we are interested in 1689 ;; whether it has the value t. 1690 (let ((retval (apply orig-fun handle-mode args))) 1691 (and (eq handle-mode t) 1692 retval ; A file-local mode was set. 1693 (so-long-handle-file-local-mode retval)) 1694 retval)) 1695 1696(defun so-long--set-auto-mode (orig-fun &rest args) 1697 ;; Advice, enabled with: 1698 ;; (advice-add 'set-auto-mode :around #'so-long--set-auto-mode) 1699 "Maybe call `so-long' for files with very long lines. 1700 1701This advice acts after `set-auto-mode' has set the buffer's major mode, if 1702`global-so-long-mode' is enabled. 1703 1704We can't act before this point, because some major modes must be exempt 1705\(binary file modes, for example). Instead, we act only when the selected 1706major mode is a member (or derivative of a member) of `so-long-target-modes'. 1707 1708`so-long-predicate' then determines whether the mode change is needed." 1709 (setq so-long--inhibited nil) ; is permanent-local 1710 (when so-long-enabled 1711 (so-long--check-header-modes)) ; may cause `so-long--inhibited' to be set. 1712 (let ((so-long--set-auto-mode t)) 1713 ;; Call `set-auto-mode'. 1714 (apply orig-fun args)) ; may cause `so-long--inhibited' to be set. 1715 ;; Test the new major mode for long lines. 1716 (and so-long-enabled 1717 (not so-long--inhibited) 1718 (not so-long--calling) 1719 (or (eq so-long-target-modes t) 1720 (apply #'derived-mode-p so-long-target-modes)) 1721 (setq so-long-detected-p (funcall so-long-predicate)) 1722 ;; `so-long' should be called; but only if and when the buffer is 1723 ;; displayed in a window. Long lines in invisible buffers are generally 1724 ;; not problematic, whereas it might cause problems if an invisible 1725 ;; buffer being used for behind-the-scenes processing is manipulated 1726 ;; unexpectedly. The default `so-long-invisible-buffer-function' value 1727 ;; is `so-long-deferred', which arranges to call `so-long' as soon as 1728 ;; the buffer is displayed. 1729 (if (or (get-buffer-window (current-buffer) t) 1730 (not so-long-invisible-buffer-function)) 1731 (so-long) 1732 (funcall so-long-invisible-buffer-function)))) 1733 1734(defun so-long--hack-one-local-variable (orig-fun var val) 1735 ;; Advice, enabled with: 1736 ;; (advice-add 'hack-one-local-variable :around 1737 ;; #'so-long--hack-one-local-variable) 1738 "Prevent the original major mode being restored after `so-long-mode'. 1739 1740This advice is needed and enabled only for Emacs versions < 26.1. 1741 1742If the local `mode' pseudo-variable is used, `set-auto-mode-0' will call it 1743firstly, and subsequently `hack-one-local-variable' may call it again. 1744 1745Usually `hack-one-local-variable' tries to avoid processing that second call, 1746by testing the value against `major-mode'; but as we may have changed the 1747major mode to `so-long-mode' by this point, that protection is insufficient 1748and so we need to perform our own test. 1749 1750We likewise need to support an equivalent of the `no-mode' behaviour in 26.1+ 1751to ensure that `so-long-mode-revert' will not restore a file-local mode again 1752after it has already reverted to the original mode. 1753 1754The changes to `normal-mode' in Emacs 26.1 modified the execution order, and 1755makes this advice unnecessary. The relevant NEWS entry is: 1756 1757** File local and directory local variables are now initialized each 1758time the major mode is set, not just when the file is first visited. 1759These local variables will thus not vanish on setting a major mode." 1760 (if (eq var 'mode) 1761 ;; Adapted directly from `hack-one-local-variable' 1762 (let ((mode (intern (concat (downcase (symbol-name val)) 1763 "-mode")))) 1764 (unless (or so-long--hack-local-variables-no-mode 1765 (let ((origmode (so-long-original 'major-mode))) 1766 ;; We bind origmode because (indirect-function nil) is an 1767 ;; error in Emacs versions < 25.1, and so we need to test 1768 ;; it first. 1769 (and origmode 1770 (eq (indirect-function mode) 1771 (indirect-function origmode))))) 1772 (funcall orig-fun var val))) 1773 ;; VAR is not the 'mode' pseudo-variable. 1774 (funcall orig-fun var val))) 1775 1776(defun so-long-deferred () 1777 "Arrange to call `so-long' if the current buffer is displayed in a window." 1778 ;; The first time that a window-configuration change results in the buffer 1779 ;; being displayed in a window, `so-long' will be called (with the window 1780 ;; selected and the buffer set as current). Because `so-long' removes this 1781 ;; buffer-local hook value, it triggers once at most. 1782 (add-hook 'window-configuration-change-hook #'so-long nil :local)) 1783 1784;;;###autoload 1785(defun so-long (&optional action) 1786 "Invoke `so-long-action' and run `so-long-hook'. 1787 1788This command is called automatically when long lines are detected, when 1789`global-so-long-mode' is enabled. 1790 1791The effects of the action can be undone by calling `so-long-revert'. 1792 1793If ACTION is provided, it is used instead of `so-long-action'. 1794 1795With a prefix argument, select the action to use interactively. 1796 1797If an action was already active in the buffer, it will be reverted before 1798invoking the new action." 1799 (interactive 1800 (list (and current-prefix-arg 1801 (intern 1802 (completing-read "Action (none): " 1803 (mapcar #'car so-long-action-alist) 1804 nil :require-match))))) 1805 ;; Ensure that `so-long-deferred' only triggers `so-long' once (at most). 1806 (remove-hook 'window-configuration-change-hook #'so-long :local) 1807 (unless so-long--calling 1808 ;; Revert the existing action, if any. 1809 (when so-long--active 1810 (so-long-revert)) 1811 ;; Invoke the new action. 1812 (let ((so-long--calling t)) 1813 (so-long--ensure-enabled) 1814 ;; ACTION takes precedence if supplied. 1815 (when action 1816 (setq so-long-function nil 1817 so-long-revert-function nil)) 1818 ;; Some of these settings need to be duplicated in `so-long-mode' to cover 1819 ;; the case when that mode is invoked directly. 1820 (setq so-long-detected-p t) ;; ensure menu is present. 1821 (unless so-long-function 1822 (setq so-long-function (so-long-function action))) 1823 (unless so-long-revert-function 1824 (setq so-long-revert-function (so-long-revert-function action))) 1825 ;; Remember original settings. 1826 (so-long-remember-all :reset) 1827 ;; Call the configured `so-long-function'. 1828 (when so-long-function 1829 (funcall so-long-function) 1830 ;; Set `so-long--active' last, as it isn't permanent-local. 1831 (setq so-long--active t)) 1832 ;; Display mode line info, unless we are in `so-long-mode' (which provides 1833 ;; equivalent information in the mode line construct for the major mode). 1834 (unless (derived-mode-p 'so-long-mode) 1835 (setq so-long-mode-line-info (so-long-mode-line-info))) 1836 ;; Run `so-long-hook'. 1837 ;; By default we set `buffer-read-only', which can cause problems if hook 1838 ;; functions need to modify the buffer. We use `inhibit-read-only' to 1839 ;; side-step the issue (and likewise in `so-long-revert'). 1840 (let ((inhibit-read-only t)) 1841 (run-hooks 'so-long-hook))))) 1842 1843(defun so-long-revert () 1844 "Revert the active `so-long-action' and run `so-long-revert-hook'. 1845 1846This undoes the effects of the `so-long' command (which is normally called 1847automatically by `global-so-long-mode'). 1848 1849For the default action, reverting will restore the original major mode, and 1850restore the minor modes and settings which were overridden when `so-long' was 1851invoked." 1852 (interactive "@") 1853 ;; We use "@" as commands in the mode-line menu may be triggered by mouse 1854 ;; when some other window is selected. 1855 (unless so-long--calling 1856 (let ((so-long--calling t)) 1857 (when so-long-revert-function 1858 (funcall so-long-revert-function) 1859 (setq so-long--active nil)) 1860 (let ((inhibit-read-only t)) 1861 (run-hooks 'so-long-revert-hook))))) 1862 1863;;;###autoload 1864(defun so-long-enable () 1865 "Enable the `so-long' library's functionality. 1866 1867Equivalent to calling (global-so-long-mode 1)" 1868 (interactive) 1869 (global-so-long-mode 1)) 1870 1871(defun so-long-disable () 1872 "Disable the `so-long' library's functionality. 1873 1874Equivalent to calling (global-so-long-mode 0)" 1875 (interactive) 1876 (global-so-long-mode 0)) 1877 1878(make-obsolete 'so-long-enable 'global-so-long-mode "so-long 1.0") 1879(make-obsolete 'so-long-disable 'global-so-long-mode "so-long 1.0") 1880 1881;;;###autoload 1882(define-minor-mode global-so-long-mode 1883 "Toggle automated performance mitigations for files with long lines. 1884 1885Many Emacs modes struggle with buffers which contain excessively long lines, 1886and may consequently cause unacceptable performance issues. 1887 1888This is commonly on account of \"minified\" code (i.e. code that has been 1889compacted into the smallest file size possible, which often entails removing 1890newlines should they not be strictly necessary). 1891 1892When such files are detected by `so-long-predicate', we invoke the selected 1893`so-long-action' to mitigate potential performance problems in the buffer. 1894 1895Use \\[so-long-commentary] for more information. 1896 1897Use \\[so-long-customize] to open the customization group `so-long' to 1898configure the behaviour." 1899 :global t 1900 :group 'so-long 1901 (if global-so-long-mode 1902 ;; Enable 1903 (progn 1904 (so-long--enable) 1905 (advice-add 'hack-local-variables :around 1906 #'so-long--hack-local-variables) 1907 (advice-add 'set-auto-mode :around 1908 #'so-long--set-auto-mode) 1909 (when (< emacs-major-version 26) 1910 (advice-add 'hack-one-local-variable :around 1911 #'so-long--hack-one-local-variable))) 1912 ;; Disable 1913 (so-long--disable) 1914 (advice-remove 'hack-local-variables #'so-long--hack-local-variables) 1915 (advice-remove 'set-auto-mode #'so-long--set-auto-mode) 1916 (when (< emacs-major-version 26) 1917 (advice-remove 'hack-one-local-variable 1918 #'so-long--hack-one-local-variable)))) 1919 1920(put 'global-so-long-mode 'variable-documentation 1921 "Non-nil if the so-long library's automated functionality is enabled. 1922 1923Use \\[so-long-commentary] for more information. 1924 1925Setting this variable directly does not take effect; 1926either customize it (see the info node `Easy Customization') 1927or call the function `global-so-long-mode'.") 1928 1929(defun so-long--ensure-enabled () 1930 "Enable essential functionality, if not already enabled." 1931 (unless so-long-enabled 1932 (so-long--enable))) 1933 1934(defun so-long--enable () 1935 "Enable functionality other than `global-so-long-mode'." 1936 (add-to-list 'mode-line-misc-info '("" so-long-mode-line-info)) 1937 (define-key-after (current-global-map) [menu-bar so-long] 1938 `(menu-item "So Long" nil 1939 ;; See also `so-long-mode-map'. 1940 :visible (or so-long--active 1941 so-long-detected-p 1942 (derived-mode-p 'so-long-mode)) 1943 :filter ,(lambda (_cmd) (so-long-menu)))) 1944 (setq so-long-enabled t)) 1945 1946(defun so-long--disable () 1947 "Disable functionality other than `global-so-long-mode'." 1948 (setq mode-line-misc-info 1949 (delete '("" so-long-mode-line-info) mode-line-misc-info)) 1950 (define-key (current-global-map) [menu-bar so-long] nil) 1951 (setq so-long-enabled nil)) 1952 1953(defun so-long-unload-function () 1954 "Handler for `unload-feature'." 1955 (condition-case err 1956 (progn 1957 (global-so-long-mode 0) 1958 ;; Process existing buffers. 1959 (dolist (buf (buffer-list)) 1960 (with-current-buffer buf 1961 ;; Remove buffer-local `window-configuration-change-hook' values set 1962 ;; by `so-long-deferred'. 1963 (remove-hook 'window-configuration-change-hook #'so-long :local) 1964 ;; Call `so-long-revert' in all buffers where so-long is active. 1965 (when (bound-and-true-p so-long--active) 1966 (so-long-revert)))) 1967 ;; Un-define our buffer-local variables, as `unload-feature' will not do 1968 ;; this automatically. We remove them from `unload-function-defs-list' 1969 ;; as well, to prevent them being redefined. n.b.: `so-long--active' is 1970 ;; tested (above) using `bound-and-true-p' because that is one of the 1971 ;; variables which we unbind (below); and if something subsequent to 1972 ;; this handler signals an error, the user may need to call this again. 1973 (defvar unload-function-defs-list) 1974 (dolist (var '(so-long--active 1975 so-long--inhibited 1976 so-long-detected-p 1977 so-long-file-local-mode-function 1978 so-long-function 1979 so-long-minor-mode 1980 so-long-mode-abbrev-table 1981 so-long-mode-line-info 1982 so-long-mode-syntax-table 1983 so-long-original-values 1984 so-long-revert-function)) 1985 (makunbound var) 1986 (setq unload-function-defs-list 1987 (delq var unload-function-defs-list))) 1988 ;; Return nil if unloading was successful. Refer to `unload-feature'. 1989 nil) 1990 ;; If any error occurred, return non-nil. 1991 (error (progn 1992 (message "Error unloading so-long: %S %S" (car err) (cdr err)) 1993 t)))) 1994 1995;; Backwards-compatibility definitions. 1996;; 1997;; The following obsolete functions may exist in the user's customized hook 1998;; values dating from versions < 1.0, so we need to ensure that such saved 1999;; values will not trigger errors. 2000(cl-flet ((ignore () nil)) 2001 (dolist (hookfunc '((so-long-inhibit-whitespace-mode . so-long-hook) 2002 (so-long-make-buffer-read-only . so-long-hook) 2003 (so-long-revert-buffer-read-only . so-long-revert-hook) 2004 (so-long-inhibit-global-hl-line-mode . so-long-mode-hook))) 2005 (defalias (car hookfunc) #'ignore 2006 (format "Obsolete function. It now does nothing. 2007 2008If it appears in `%s', you should remove it." 2009 (cdr hookfunc))) 2010 (make-obsolete (car hookfunc) nil "so-long.el version 1.0"))) 2011 2012;; Live upgrades, for when a newer version is loaded over an older one. 2013;; 2014;; If `so-long-version' was already bound then that tells us which version we 2015;; should upgrade from. If `so-long-version' is unbound then most likely there 2016;; was no older version loaded; however, prior to version 1.0 `so-long-version' 2017;; was not defined at all, and so we also need to detect that scenario, which 2018;; we can do by testing for the presence of a symbol which was removed in 1.0. 2019;; 2020;; The variable `so-long-mode-enabled' covers versions 0.5 - 0.7.6, which is 2021;; every pre-1.0 release using the name "so-long.el". 2022(defvar so-long-version (if (boundp 'so-long-mode-enabled) 2023 "0.5" ;; >= 0.5 and < 1.0 2024 so-long--latest-version) 2025 "The loaded version of so-long.el.") 2026 2027;; Version-specific updates. 2028(when (version< so-long-version so-long--latest-version) 2029 ;; Perform each update in sequence, as necessary. 2030 ;; Update to version 1.0 from earlier versions: 2031 (when (version< so-long-version "1.0") 2032 (remove-hook 'change-major-mode-hook 'so-long-change-major-mode) 2033 (eval-and-compile (require 'advice)) ;; Both macros and functions. 2034 (declare-function ad-find-advice "advice") 2035 (declare-function ad-remove-advice "advice") 2036 (declare-function ad-activate "advice") 2037 (when (ad-find-advice 'hack-local-variables 'after 'so-long--file-local-mode) 2038 (ad-remove-advice 'hack-local-variables 'after 'so-long--file-local-mode) 2039 (ad-activate 'hack-local-variables)) 2040 (when (ad-find-advice 'set-auto-mode 'around 'so-long--set-auto-mode) 2041 (ad-remove-advice 'set-auto-mode 'around 'so-long--set-auto-mode) 2042 (ad-activate 'set-auto-mode)) 2043 (when (boundp 'so-long-mode-map) 2044 (define-key so-long-mode-map [remap so-long-mode-revert] #'so-long-revert)) 2045 (dolist (var '(so-long-mode--inhibited 2046 so-long-original-mode)) 2047 (makunbound var)) 2048 (dolist (func '(so-long-change-major-mode 2049 so-long-check-header-modes 2050 so-long-line-detected-p)) 2051 (fmakunbound func)) 2052 (defvar so-long-mode-enabled) 2053 (when so-long-mode-enabled 2054 (unless global-so-long-mode 2055 (global-so-long-mode 1))) 2056 (makunbound 'so-long-mode-enabled)) 2057 ;; Update to version 1.N from earlier versions: 2058 ;; (when (version< so-long-version "1.N") ...) 2059 ;; 2060 ;; All updates completed. 2061 (setq so-long-version so-long--latest-version)) 2062 2063 2064(provide 'so-long) 2065 2066;; Local Variables: 2067;; emacs-lisp-docstring-fill-column: 80 2068;; fill-column: 80 2069;; indent-tabs-mode: nil 2070;; ispell-check-comments: exclusive 2071;; ispell-local-dictionary: "british" 2072;; End: 2073 2074;; This library is extensively documented in British English, contrary to the 2075;; preference for American English in Emacs. I hope the benefits of the library 2076;; will outweigh any discontent you may experience regarding the spelling (or 2077;; that you find the spelling to be an agreeable bonus). Certain standard Emacs 2078;; terminology, and text quoted from elsewhere in Emacs, retains its original 2079;; spelling. The following LocalWords should result in no misspellings from 2080;; M-x ispell-buffer (using aspell). 2081 2082; LocalWords: LocalWords british ispell aspell hunspell emacs elisp el init dir 2083; LocalWords: customize customized customizing customization Customization prog 2084; LocalWords: initialized profiler boolean minified pre redisplay config keymap 2085; LocalWords: noerror selectable mapc sgml nxml hl flydiff defs arg Phil Sainty 2086; LocalWords: defadvice nadvice whitespace ie bos eos eobp origmode un Un setq 2087; LocalWords: docstring auf Wiedersehen longlines alist autoload Refactored Inc 2088; LocalWords: MERCHANTABILITY RET REGEXP VAR ELPA WS mitigations EmacsWiki eval 2089; LocalWords: rx filename filenames js defun bidi bpa FIXME globalized amongst 2090 2091;; So long, farewell, auf Wiedersehen, goodbye 2092;; You have to go, this code is minified 2093;; Goodbye! 2094 2095;;; so-long.el ends here 2096