From: Joseph Turner <joseph@breatheoutbreathe.in>
To: Philip Kaludercic <philipk@posteo.net>
Cc: Emacs Devel Mailing List <emacs-devel@gnu.org>,
mail+gh@daniel-mendler.de
Subject: Re: [IDEA] Add function clean-buffer
Date: Tue, 05 Sep 2023 12:37:30 -0700 [thread overview]
Message-ID: <87a5u0tmwd.fsf@breatheoutbreathe.in> (raw)
In-Reply-To: <87ledm6i68.fsf@posteo.net>
[-- Attachment #1: Type: text/plain, Size: 1867 bytes --]
Philip Kaludercic <philipk@posteo.net> writes:
> Joseph Turner <joseph@breatheoutbreathe.in> writes:
>
>> Prompted by Daniel Mendler's comment here:
>>
>> https://github.com/emacs-compat/compat/issues/27#issuecomment-1704381157
>>
>> IIUC, clean-mode is intended for interactive, debugging usage. I am
>> interested in a function that performs some of the internal behavior of
>> the Emacs 29 clean-mode in non-interactive use. Note that
>> yank-excluded-properties is not set in clean-buffer.
>
> Perhaps you could explain what the concrete example is where you need
> the functionality?
In hyperdrive.el, when writing an existing buffer to a hyperdrive file,
we want to first remove overlays, text properties, and local variables,
and then use set-auto-mode to set the major mode (along with its
overlays, text properties, and local variable). The reason for doing
this is so that after writing a buffer to a file, users will see the
buffer as it will appear on another peer's machine.
For example, if you call hyperdrive-write-buffer on a magit log buffer,
we want the overlays to disappear so that users don't expect others to
be able to see the overlays when they load the file.
>> (defun clean-buffer (&optional buffer)
>> "Remove all local variables, overlays, and text properties in BUFFER.
>> When BUFFER is nil, act on current buffer."
>> (with-current-buffer (or buffer (current-buffer))
>> (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))))
>>
>> It could also be used internally in clean-mode.
>
> Could you prepare this as a patch?
See attached patches. I'm not sure if subr.el is the right place for
this change, and I haven't added added to the NEWS file.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-new-function-clean-buffer.patch --]
[-- Type: text/x-diff, Size: 1289 bytes --]
From 6253633af98240f1f3ad2d00888cec945de5d708 Mon Sep 17 00:00:00 2001
From: Joseph Turner <joseph@breatheoutbreathe.in>
Date: Tue, 5 Sep 2023 12:28:49 -0700
Subject: [PATCH 1/2] Add new function clean-buffer
* lisp/subr.el (clean-buffer):
Removes all local variables, overlays, and text properties.
---
lisp/subr.el | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/lisp/subr.el b/lisp/subr.el
index 6cedaffa806..cd4149c0a79 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -4694,6 +4694,16 @@ Normally, mouse motion is ignored."
(declare (debug (def-body)) (indent 0))
`(internal--track-mouse (lambda () ,@body)))
+(defun clean-buffer (&optional buffer)
+ "Remove all local variables, overlays, and text properties in BUFFER.
+ When BUFFER is nil, act on current buffer."
+ (with-current-buffer (or buffer (current-buffer))
+ (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))))
+
(defmacro with-current-buffer (buffer-or-name &rest body)
"Execute the forms in BODY with BUFFER-OR-NAME temporarily current.
BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
--
2.41.0
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-Refactor-clean-mode-to-use-clean-buffer.patch --]
[-- Type: text/x-diff, Size: 1310 bytes --]
From dfb0ac3d7ae03d8e9dffd1d447afe0a604a49960 Mon Sep 17 00:00:00 2001
From: Joseph Turner <joseph@breatheoutbreathe.in>
Date: Tue, 5 Sep 2023 12:34:23 -0700
Subject: [PATCH 2/2] Refactor clean-mode to use clean-buffer
* lisp/simple.el (clean-mode):
Use clean-mode internally. Clarify docstring.
---
lisp/simple.el | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/lisp/simple.el b/lisp/simple.el
index 05a3c4b93d6..37d76c174dc 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -546,13 +546,11 @@ Other major modes are defined by comparison with this one."
(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 yank-excluded-properties t)))
+ "Mode removing all local variables, overlays, and text properties.
+This mode is intended for debugging purposes. For non-interactive
+use, see `clean-buffer'."
+ (clean-buffer)
+ (setq-local yank-excluded-properties t))
;; Special major modes to view specially formatted data rather than files.
--
2.41.0
next prev parent reply other threads:[~2023-09-05 19:37 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-03 22:18 [IDEA] Add function clean-buffer Joseph Turner
2023-09-04 16:02 ` Philip Kaludercic
2023-09-05 19:37 ` Joseph Turner [this message]
2023-09-06 11:59 ` Eli Zaretskii
2023-09-08 4:55 ` Joseph Turner
2023-09-08 9:13 ` David Ponce
2023-09-08 17:21 ` Joseph Turner
2023-09-09 0:38 ` Richard Stallman
2023-09-09 0:59 ` Joseph Turner
2023-09-09 7:00 ` Eli Zaretskii
2023-09-10 8:19 ` Stefan Kangas
2023-09-12 0:29 ` Richard Stallman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87a5u0tmwd.fsf@breatheoutbreathe.in \
--to=joseph@breatheoutbreathe.in \
--cc=emacs-devel@gnu.org \
--cc=mail+gh@daniel-mendler.de \
--cc=philipk@posteo.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.