Subject: Re: bug#77228: [PATCH] ; * lisp/cus-edit.el (custom-dirlocals-with-buffer): Use Custom-mode

From: Elijah Gabe Pérez <[email-masked]>
Date: Wed, 02 Apr 2025 18:26:08 -0600

Mauro Aranda <[email-masked]> writes:

> Elijah Gabe Pérez <[email-masked]> writes:
>
>> Tags: patch
>>
>> Little patch for make customize-dirlocals use Custom-mode instead
>> fundamental-mode, Mostly intended for make this inherit and use
>> Custom-mode-hook.
>>
>> I have tested it and at the moment it does not cause problems with the
>> configuration made.
>
> Yes, I don't anticipate any real problems, but just using Custom-mode is
> almost entirely redundant since some of the settings that it performs
> are not relevant, and others get clobbered right away.
>
> Overriden:
> custom-mode-map, tool-bar-map, revert-buffer-function.
>
> Not relevant:
> custom--invocation-options, custom--hidden-state, custom-options,
> custom-local-buffer, widget-edit-functions.
>
> Redundant:
> custom--initialize-widget-variables.
>
>
> That just leaves us with the text-conversion-style and
> touch-screen-keyboard-function settings, which my guess is that they
> should've been added to custom-dirlocals-with-buffer too, and of course,
> running Custom-mode-hook.
>
> So I think it'd be better either to derive from Custom-mode, or find a
> way to avoid the redundant settings.

But making it derived won't include Custom-mode options anyway?

I made it a major mode but not derived from it.
I've included the Custom-mode options that are useful (as you pointed)
and run Custom-mode-hook.

>From 6510e3f7f68289c99e80426e3c76b8268661fa8a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?El=C3=ADas=20Gabriel=20P=C3=A9rez?= <[email-masked]>
Date: Sun, 23 Mar 2025 21:26:26 -0600
Subject: [PATCH] Create major mode for `customize-dirlocals` (bug#77228)

* lisp/cus-edit.el (Custom-dirlocals-mode): New major mode.
(custom-dirlocals-with-buffer): Move stuffs to
`Custom-dirlocals-mode'.
---
 lisp/cus-edit.el | 39 ++++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/lisp/cus-edit.el b/lisp/cus-edit.el
index ecfce03e490..a0d21fee571 100644
--- a/lisp/cus-edit.el
+++ b/lisp/cus-edit.el
@@ -6060,31 +6060,40 @@ custom-dirlocals--set-widget-vars
   (custom--initialize-widget-variables)
   (add-hook 'widget-forward-hook #'custom-dirlocals-maybe-update-cons nil t))
 
+(define-derived-mode Custom-dirlocals-mode nil "Custom dirlocals"
+  "Major mode for Customize Directory Local Variables in the current directory."
+  (kill-all-local-variables)
+  (run-hooks 'Custom-mode-hook)
+  (custom-dirlocals--set-widget-vars)
+  (setq-local text-conversion-style 'action)
+  (setq-local touch-screen-keyboard-function
+              #'Custom-display-on-screen-keyboard-p)
+  (setq-local revert-buffer-function #'Custom-dirlocals-revert-buffer)
+  (setq-local tool-bar-map
+              (or custom-dirlocals-tool-bar-map
+                  ;; Set up `custom-dirlocals-tool-bar-map'.
+                  (let ((map (make-sparse-keymap)))
+                    (mapc
+                     (lambda (arg)
+                       (tool-bar-local-item-from-menu
+                        (nth 1 arg) (nth 4 arg) map custom-dirlocals-map
+                        :label (nth 5 arg)))
+                     custom-dirlocals-commands)
+                    (setq custom-dirlocals-tool-bar-map map))))
+  (use-local-map custom-dirlocals-map))
+
 (defmacro custom-dirlocals-with-buffer (&rest body)
   "Arrange to execute BODY in a \"*Customize Dirlocals*\" buffer."
   ;; We don't use `custom-buffer-create' because the settings here
   ;; don't go into the `custom-file'.
   `(progn
      (switch-to-buffer "*Customize Dirlocals*")
-     (kill-all-local-variables)
+
      (let ((inhibit-read-only t))
        (erase-buffer))
      (remove-overlays)
-     (custom-dirlocals--set-widget-vars)
+     (Custom-dirlocals-mode)
      ,@body
-     (setq-local tool-bar-map
-                 (or custom-dirlocals-tool-bar-map
-                     ;; Set up `custom-dirlocals-tool-bar-map'.
-                     (let ((map (make-sparse-keymap)))
-                       (mapc
-                        (lambda (arg)
-                          (tool-bar-local-item-from-menu
-                           (nth 1 arg) (nth 4 arg) map custom-dirlocals-map
-                           :label (nth 5 arg)))
-                        custom-dirlocals-commands)
-                       (setq custom-dirlocals-tool-bar-map map))))
-     (setq-local revert-buffer-function #'Custom-dirlocals-revert-buffer)
-     (use-local-map custom-dirlocals-map)
      (widget-setup)))
 
 (defun custom-dirlocals-get-options ()
-- 
2.49.0

-- 
                                          - E.G via GNU Emacs and Org.