From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Joseph Turner Newsgroups: gmane.emacs.devel Subject: Re: [IDEA] Add function clean-buffer Date: Tue, 05 Sep 2023 12:37:30 -0700 Message-ID: <87a5u0tmwd.fsf@breatheoutbreathe.in> References: <87il8qx50p.fsf@breatheoutbreathe.in> <87ledm6i68.fsf@posteo.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="14325"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Emacs Devel Mailing List , mail+gh@daniel-mendler.de To: Philip Kaludercic Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Sep 06 04:20:39 2023 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qdi9i-0003QL-Rv for ged-emacs-devel@m.gmane-mx.org; Wed, 06 Sep 2023 04:20:35 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qdi93-00087Q-R0; Tue, 05 Sep 2023 22:19:53 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qdc9k-00078g-8B for emacs-devel@gnu.org; Tue, 05 Sep 2023 15:56:12 -0400 Original-Received: from out-210.mta0.migadu.com ([91.218.175.210]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qdc9h-0008SV-0v for emacs-devel@gnu.org; Tue, 05 Sep 2023 15:56:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=breatheoutbreathe.in; s=key1; t=1693943766; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=+DwRtP1DvIgHQdfSUvO19wLgZ+l3veZeTSKptvccGrU=; b=hbpadCVbAy/D6EgX9T1WSjahckoYp1dJMc4Qe/zMMgsndaQGr7iKcwRHUL9t5VHrOJXb2b KfsX5NRrfQCF79pYaZxJ8ib2ySodgPyPsBA+SmooXKJLrl5DxhP6WFbwqvGo8HVbzKuFTZ 6df5WUY0i5+QnvuL0HOKLN84pdWoDW4= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. In-reply-to: <87ledm6i68.fsf@posteo.net> X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=91.218.175.210; envelope-from=joseph@breatheoutbreathe.in; helo=out-210.mta0.migadu.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Tue, 05 Sep 2023 22:19:52 -0400 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:310167 Archived-At: --=-=-= Content-Type: text/plain Philip Kaludercic writes: > Joseph Turner 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. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Add-new-function-clean-buffer.patch >From 6253633af98240f1f3ad2d00888cec945de5d708 Mon Sep 17 00:00:00 2001 From: Joseph Turner 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 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0002-Refactor-clean-mode-to-use-clean-buffer.patch >From dfb0ac3d7ae03d8e9dffd1d447afe0a604a49960 Mon Sep 17 00:00:00 2001 From: Joseph Turner 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 --=-=-=--