diff --git a/doc/lispref/modes.texi b/doc/lispref/modes.texi index ee55f982d02..d55df7db4b6 100644 --- a/doc/lispref/modes.texi +++ b/doc/lispref/modes.texi @@ -266,6 +266,18 @@ This function restores the major mode recorded by function calls @code{normal-mode} (@pxref{Auto Major Mode, normal-mode}), but tries to force it not to choose any modes in @var{avoided-modes}, if that argument is non-@code{nil}. +@end defun + +@defun clean-mode +Changing the major mode clears out most local variables, but it +doesn't remove all artefacts in the buffer (like text properties and +overlays). It's rare to change a buffer from one major mode to +another (except from @code{fundamental-mode} to everything else), so +this is usually not a concern. It can sometimes be convenient (mostly +when debugging a problem in a buffer) to do a ``full reset'' of the +buffer, and that's what the @code{clean-mode} major mode offers. It +will kill all local variables (even the permanently local ones), and +also removes all overlays and text properties. @end defun The easiest way to write a major mode is to use the macro diff --git a/etc/NEWS b/etc/NEWS index 7360bb79852..7b218aaf6da 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -125,6 +125,11 @@ with recent versions of Firefox. * Lisp Changes in Emacs 29.1 ++++ +** New major mode 'clean-mode'. +This is a new major mode meant for debugging. It kills absolutely all +local variables and removes overlays and text properties. + +++ ** 'kill-all-local-variables' can now kill all local variables. If given the new optional KILL-PERMANENT argument, also kill permanent diff --git a/lisp/simple.el b/lisp/simple.el index 5c6adcf4907..042715449b2 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -527,6 +527,18 @@ Other major modes are defined by comparison with this one." (kill-all-local-variables) (run-mode-hooks)) +(define-derived-mode clean-mode fundamental-mode "Clean" + "A mode that removes all overlays and text properties." + (kill-all-local-variables t) + (let ((inhibit-read-only t)) + (dolist (overlay (overlays-in (point-min) (point-max))) + (delete-overlay overlay)) + (set-text-properties (point-min) (point-max) nil) + (setq-local after-change-functions + (list + (lambda (begin end _length) + (set-text-properties begin end nil)))))) + ;; Special major modes to view specially formatted data rather than files. (defvar-keymap special-mode-map