From fa20e443c865db8f2975e1b572654f235e2be91d Mon Sep 17 00:00:00 2001 From: Phil Sainty Date: Sun, 8 Dec 2019 23:28:06 +1300 Subject: [PATCH] lisp/so-long.el: Improve support for major mode hooks * lisp/so-long.el (so-long-remember-all, so-long-disable-minor-modes) (so-long-override-variables): Store and use the `so-long-minor-modes' and `so-long-variable-overrides' values seen by the original major mode, so that buffer-local changes made in the major mode hook will be respected. Add documentation of this and other major mode hook usage. --- lisp/so-long.el | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/lisp/so-long.el b/lisp/so-long.el index 1332ae12633..d4f7140f2ef 100644 --- a/lisp/so-long.el +++ b/lisp/so-long.el @@ -282,6 +282,43 @@ ;; '((show-trailing-whitespace . nil) ;; (truncate-lines . nil)))) +;; * Mode-specific configuration +;; ----------------------------- +;; The `so-long-predicate' function is called in the context of the buffer's +;; original major mode, and therefore major mode hooks can be used to control +;; the criteria for calling `so-long' in any given mode (plus its derivatives) +;; by setting buffer-local values for the variables in question. This includes +;; `so-long-predicate' itself, as well as any variables used by the predicate +;; when determining the result. By default this means `so-long-max-lines', +;; `so-long-skip-leading-comments', and `so-long-threshold'. E.g.: +;; +;; (add-hook 'js-mode-hook 'my-js-mode-hook) +;; +;; (defun my-js-mode-hook () +;; "Custom `js-mode' behaviours." +;; (setq-local so-long-max-lines 100) +;; (setq-local so-long-threshold 1000)) +;; +;; `so-long-variable-overrides' and `so-long-minor-modes' may also be given +;; buffer-local values in order to apply different settings to different types +;; of file. For example, the Bidirectional Parentheses Algorithm does not apply +;; to `<' and `>' characters by default, and therefore one might prefer to not +;; set `bidi-inhibit-bpa' in XML files, on the basis that XML files with long +;; lines are less likely to trigger BPA-related performance problems: +;; +;; (add-hook 'nxml-mode-hook 'my-nxml-mode-hook) +;; +;; (defun my-nxml-mode-hook () +;; "Custom `nxml-mode' behaviours." +;; (require 'so-long) +;; (setq-local so-long-variable-overrides +;; (remove '(bidi-inhibit-bpa . t) so-long-variable-overrides))) +;; +;; Finally, note that setting `so-long-target-modes' to nil buffer-locally in +;; a major mode hook would prevent that mode from ever being targeted. With +;; `prog-mode' being targeted by default, specific derivatives of `prog-mode' +;; could therefore be un-targeted if desired. + ;; * Other ways of using so-long ;; ----------------------------- ;; It may prove useful to automatically invoke major mode `so-long-mode' for @@ -886,9 +923,15 @@ buffer-local." Stores the existing value for each entry in `so-long-variable-overrides'. Stores the name of each enabled mode from the list `so-long-minor-modes'. +The lists themselves are also remembered, so that major mode hooks can +provide buffer-local modifications which are still accessible after changing +to `so-long-mode'. + If RESET is non-nil, remove any existing values before storing the new ones." (when reset (setq so-long-original-values nil)) + (so-long-remember 'so-long-variable-overrides) + (so-long-remember 'so-long-minor-modes) (dolist (ovar so-long-variable-overrides) (so-long-remember (car ovar))) (dolist (mode so-long-minor-modes) @@ -1286,7 +1329,7 @@ Calls `so-long-disable-minor-modes' and `so-long-override-variables'." (defun so-long-disable-minor-modes () "Disable any active minor modes listed in `so-long-minor-modes'." - (dolist (mode so-long-minor-modes) + (dolist (mode (so-long-original 'so-long-minor-modes)) (when (and (boundp mode) mode) (funcall mode 0)))) @@ -1302,7 +1345,7 @@ The modes are enabled in accordance with what was remembered in `so-long'." (defun so-long-override-variables () "Set the buffer-local values defined by `so-long-variable-overrides'." - (dolist (ovar so-long-variable-overrides) + (dolist (ovar (so-long-original 'so-long-variable-overrides)) (set (make-local-variable (car ovar)) (cdr ovar)))) (defun so-long-restore-variables () @@ -1877,7 +1920,7 @@ If it appears in `%s', you should remove it." ; LocalWords: defadvice nadvice whitespace ie bos eos eobp origmode un Un setq ; LocalWords: docstring auf Wiedersehen longlines alist autoload Refactored Inc ; LocalWords: MERCHANTABILITY RET REGEXP VAR ELPA WS mitigations EmacsWiki eval -; LocalWords: rx filename filenames bidi bpa +; LocalWords: rx filename filenames js defun bidi bpa prog ;; So long, farewell, auf Wiedersehen, goodbye ;; You have to go, this code is minified -- 2.39.2