Add new major mode 'clean-mode'

* doc/lispref/modes.texi (Major Modes): Document it.

* lisp/simple.el (clean-mode): New major mode.
This commit is contained in:
Lars Ingebrigtsen 2021-10-06 12:55:17 +02:00
parent c0f7396588
commit 597b6deb29
3 changed files with 29 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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