Hi Stefan and all,

>>>>> Stefan Monnier <monn...@iro.umontreal.ca> writes:
> Looks good to me (I mean: it doesn't literally look good, but given what
> we have, this seems about as clean as it gets).

Thanks for your comments. Then I'll take this approach.

> Only detail, I'd move the

>     (if (eq (symbol-function 'plain-TeX-mode) #'plain-tex-mode)
>         (defalias 'plain-TeX-mode nil))
>     (if (eq (symbol-function 'LaTeX-mode) #'latex-mode)
>         (defalias 'LaTeX-mode nil))
>     (if (eq (symbol-function 'TeX-mode) #'tex-mode)
>         (defalias 'TeX-mode nil))

> into a function and merge it with the similar code which uses
> `auctex-function-definition`.

All right.

>> 2. ConTeXt modes
> In the case of `ConTeXt-mode`, maybe the better option is to make the
> submodes be minor modes?

Hmm. Some difficulties for that idea come up to my mind:
1. It isn't clear what should be done when the mode is turned off.
2. As they are minor mode, en-mode and nl-mode can be turned on at the
   same time.
3. An entry of the form
mode: ConTeXt-en
   in file local variable no longer works.
Summing these up, I think it would be better to just turn them into
ordinary functions. (patch 1)
Actually, I don't think that the item 3 is a fundamental problem because
context-nl-mode didn't work with error until my very recent commit[1] :-(.
This means that there are practically no ConTeXt users who specify sub
modes explicitly, I think.

[1] 
https://git.savannah.gnu.org/cgit/auctex.git/commit/?id=f69211af6239da6d96864733c5b86e2ee3a5b6eb

> That's not very pretty, indeed.  The fundamental problem you're trying
> to solve is the same as the one we have with `tex-mode` since that
> function is used both as "parent mode" and as "dispatch to the
> appropriate child-mode".

Yes, my initial attempt was just an adaptation of the framework of
`tex-mode' and its child modes. :-) It did use :around advice.

> In `tex-mode` we've tried various approaches over the years, none of
> which are elegant :-(

Now I arrived at another approach without advice which makes use of
`major-mode-remap-alist'. How about this idea?
1. Separete the role of "parent mode" and "dispatch function".
2. Add an entry (tex-mode . DISPATCH-FUNCTION) to
   `major-mode-remap-alist'.
In other words, this is the basic idea:
--- tex-mode.el ------------------------------------------------------
(defun tex--dispatch ()
  (funcall (tex--guess-mode)))

;;;###autoload (add-to-list 'major-mode-remap-alist '(tex-mode . tex--dispatch))
----------------------------------------------------------------------
This is parallel to what "feature/fix-mode-names-overlap" branch does
for `TeX-tex-mode' now. I expect `tex--redirect-to-submode' is no longer
necessary.

This approach is incompatible with the current AUCTeX redirection
facility relying on
(advice-add 'tex-mode :override 'TeX-tex-mode '((depth . -10)))
. However, we can incorporate mimimal modification in the master branch
of AUCTeX so that tex-site.el uses `major-mode-remap-alist' instead of
advice for Emacs 29 or higher. Then the above treatment to tex-mode.el
will become compatible with AUCTeX even before
"feature/fix-mode-names-overlap" branch is merged into master branch.

And we can use the same approach for ConTeXt modes for Emacs 29 or
higher. If we redirect context mode to a suitable dispatch function by
`major-mode-remap-alist', we can have ConTeXt-xx-mode as derived major
mode of ConTeXt-mode in a clean way. (patch 2)
(If we actually take this approach, we need to have codes for Emacs<29
and Emacs>28 separately.)

>> 3. Additional menu item
> Maybe `nil` (a.k.a. `fundamental-mode`) is a better parent, which more
> closely matches the historical behavior of AUCTeX?

> TeX is one of those formats that sits halfway between `text-mode` and
> `prog-mode` (like (SG|X)ML, as well).

That's a possible option, of course.

Hmm, I realized that
(define-key TeX-mode-map [menu-bar text] #'undefined)
hides the "Text" menu. This would be another option.

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine

>From 07b0b7b428139a712650beee5050bbb76d826f69 Mon Sep 17 00:00:00 2001
From: Ikumi Keita <ik...@ikumi.que.jp>
Date: Mon, 28 Aug 2023 18:22:45 +0900
Subject: [PATCH] Simplify ConTeXt modes

This branch no longer offers language-specific major modes for
ConTeXt.

* context-en.el (ConTeXt-en-mode): Rename to ConTeXt--mode-en and
stop being major mode.
(context-en-mode): Delete alias.
* context-nl.el (ConTeXt-nl-mode): Rename to ConTeXt--mode-nl and
stop being major mode.
(context-nl-mode): Delete alias.
* context.el (ConTeXt-mode): Revert to a form similar to the one
before introducing `define-derived-mode'.  Call ConTeXt--mode-xx
instead of ConTeXt-xx-mode.
(ConTeXt--auto-mode): Delete.
---
 context-en.el | 21 +++++----------------
 context-nl.el | 21 +++++----------------
 context.el    | 23 ++++-------------------
 3 files changed, 14 insertions(+), 51 deletions(-)

diff --git a/context-en.el b/context-en.el
index 26f84564..c63a4fd1 100644
--- a/context-en.el
+++ b/context-en.el
@@ -200,29 +200,18 @@ That is, besides the section(-block) commands.")
    '("sub" ConTeXt-arg-define-ref (TeX-arg-literal " "))
    '("sym" (TeX-arg-string "Symbol") (TeX-arg-literal " "))))
 
-;;;###autoload
-(define-derived-mode ConTeXt-en-mode ConTeXt-mode "ConTeXt-en"
-  "Major mode for editing files for ConTeXt using its english interface.
-
-Entering `ConTeXt-mode' calls the value of `text-mode-hook',
-then the value of `TeX-mode-hook', and then the value
-of `ConTeXt-mode-hook'.
-
-Don't use `ConTeXt-en-mode-hook' and `ConTeXt-en-mode-map', both
-specific to this mode.  They are ignored unless `ConTeXt-en-mode'
-is called directly."
-  :syntax-table nil
-  :abbrev-table nil
+(defun ConTeXt--mode-en ()
+  "Set up ConTeXt mode for editing files under english interface.
+Helper function of `ConTeXt-mode'.  Don't use."
 
   ;; set the ConTeXt interface
   (setq ConTeXt-current-interface "en")
 
   ;; initialization
   (ConTeXt-mode-common-initialization)
-  (ConTeXt-en-mode-initialization))
+  (ConTeXt-en-mode-initialization)
 
-;;;###autoload
-(defalias 'context-en-mode #'ConTeXt-en-mode)
+  (setq mode-name "ConTeXt-en"))
 
 (provide 'context-en)
 
diff --git a/context-nl.el b/context-nl.el
index dfbcc3ac..721b127f 100644
--- a/context-nl.el
+++ b/context-nl.el
@@ -178,29 +178,18 @@ That is, besides the section(-block) commands.")
    '("sub" ConTeXt-arg-define-ref (TeX-arg-literal " "))
    '("sym" (TeX-arg-string "Symbol") (TeX-arg-literal " "))))
 
-;;;###autoload
-(define-derived-mode ConTeXt-nl-mode ConTeXt-mode "ConTeXt-nl"
-  "Major mode for editing files for ConTeXt using its dutch interface.
-
-Entering `ConTeXt-mode' calls the value of `text-mode-hook',
-then the value of `TeX-mode-hook', and then the value
-of `ConTeXt-mode-hook'.
-
-Don't use `ConTeXt-nl-mode-hook' and `ConTeXt-nl-mode-map', both
-specific to this mode.  They are ignored unless `ConTeXt-nl-mode'
-is called directly."
-  :syntax-table nil
-  :abbrev-table nil
+(defun ConTeXt--mode-nl ()
+  "Set up ConTeXt mode for editing files under dutch interface.
+Helper function of `ConTeXt-mode'.  Don't use."
 
   ;; set the ConTeXt interface
   (setq ConTeXt-current-interface "nl")
 
   ;; initialization
   (ConTeXt-mode-common-initialization)
-  (ConTeXt-nl-mode-initialization))
+  (ConTeXt-nl-mode-initialization)
 
-;;;###autoload
-(defalias 'context-nl-mode #'ConTeXt-nl-mode)
+  (setq mode-name "ConTeXt-nl"))
 
 (provide 'context-nl)
 
diff --git a/context.el b/context.el
index fc448e9a..4e84b135 100644
--- a/context.el
+++ b/context.el
@@ -1919,25 +1919,10 @@ that is, you do _not_ have to cater for this yourself by adding \\\\\\=' or $."
 Entering `ConTeXt-mode' calls the value of `text-mode-hook',
 then the value of `TeX-mode-hook', and then the value
 of `ConTeXt-mode-hook'."
-  ;; When called as parent of ConTeXt-{en,nl}-mode, do nothing to
-  ;; avoid `TeX-add-symbols' and `ConTeXt-add-environments' for wrong
-  ;; language interface.
-  (add-hook 'change-major-mode-after-body-hook #'ConTeXt--auto-mode nil t))
-
-(defun ConTeXt--auto-mode ()
-  ;; When called as not parent of ConTeXt-{en,nl}-mode, guess ConTeXt
-  ;; interface and use it.
-  (remove-hook 'change-major-mode-after-body-hook #'ConTeXt--auto-mode t)
-  (when (string-equal mode-name "ConTeXt")
-    (context-guess-current-interface)
-    (ConTeXt-mode-common-initialization)
-    (require (intern (concat "context-" ConTeXt-current-interface)))
-    ;; This bypasses call to ConTeXt-{en,nl}-mode.  Consequently,
-    ;; their mode-specific hook/keymap are ignored.
-    (funcall (intern (concat "ConTeXt-" ConTeXt-current-interface "-mode-initialization")))
-
-    (setq mode-name
-          (concat "ConTeXt-" ConTeXt-current-interface)))
+  (context-guess-current-interface)
+  (require (intern (concat "context-" ConTeXt-current-interface)))
+  (funcall (intern (concat "ConTeXt--mode-" ConTeXt-current-interface)))
+
   ;; set mode line
   (setq TeX-base-mode-name mode-name))
 
-- 
2.41.0

>From 174a890ffa1a4556acdd2217959da37cc46cf7af Mon Sep 17 00:00:00 2001
From: Ikumi Keita <ik...@ikumi.que.jp>
Date: Mon, 28 Aug 2023 21:07:27 +0900
Subject: [PATCH] Use `major-mode-remap-alist' for ConTeXt mode

---
 context-en.el  |  5 ++++-
 context-nl.el  |  5 ++++-
 context.el     | 25 ++++++-------------------
 tex-site.el.in |  5 +++++
 4 files changed, 19 insertions(+), 21 deletions(-)

diff --git a/context-en.el b/context-en.el
index 26f84564..3af4fcb3 100644
--- a/context-en.el
+++ b/context-en.el
@@ -219,7 +219,10 @@ is called directly."
 
   ;; initialization
   (ConTeXt-mode-common-initialization)
-  (ConTeXt-en-mode-initialization))
+  (ConTeXt-en-mode-initialization)
+
+  ;; set mode line
+  (setq TeX-base-mode-name mode-name))
 
 ;;;###autoload
 (defalias 'context-en-mode #'ConTeXt-en-mode)
diff --git a/context-nl.el b/context-nl.el
index dfbcc3ac..28bb45c3 100644
--- a/context-nl.el
+++ b/context-nl.el
@@ -197,7 +197,10 @@ is called directly."
 
   ;; initialization
   (ConTeXt-mode-common-initialization)
-  (ConTeXt-nl-mode-initialization))
+  (ConTeXt-nl-mode-initialization)
+
+  ;; set mode line
+  (setq TeX-base-mode-name mode-name))
 
 ;;;###autoload
 (defalias 'context-nl-mode #'ConTeXt-nl-mode)
diff --git a/context.el b/context.el
index fc448e9a..577beeaa 100644
--- a/context.el
+++ b/context.el
@@ -1919,27 +1919,14 @@ that is, you do _not_ have to cater for this yourself by adding \\\\\\=' or $."
 Entering `ConTeXt-mode' calls the value of `text-mode-hook',
 then the value of `TeX-mode-hook', and then the value
 of `ConTeXt-mode-hook'."
-  ;; When called as parent of ConTeXt-{en,nl}-mode, do nothing to
-  ;; avoid `TeX-add-symbols' and `ConTeXt-add-environments' for wrong
-  ;; language interface.
-  (add-hook 'change-major-mode-after-body-hook #'ConTeXt--auto-mode nil t))
+  )
 
+;;;###autoload
 (defun ConTeXt--auto-mode ()
-  ;; When called as not parent of ConTeXt-{en,nl}-mode, guess ConTeXt
-  ;; interface and use it.
-  (remove-hook 'change-major-mode-after-body-hook #'ConTeXt--auto-mode t)
-  (when (string-equal mode-name "ConTeXt")
-    (context-guess-current-interface)
-    (ConTeXt-mode-common-initialization)
-    (require (intern (concat "context-" ConTeXt-current-interface)))
-    ;; This bypasses call to ConTeXt-{en,nl}-mode.  Consequently,
-    ;; their mode-specific hook/keymap are ignored.
-    (funcall (intern (concat "ConTeXt-" ConTeXt-current-interface "-mode-initialization")))
-
-    (setq mode-name
-          (concat "ConTeXt-" ConTeXt-current-interface)))
-  ;; set mode line
-  (setq TeX-base-mode-name mode-name))
+  (context-guess-current-interface)
+  (ConTeXt-mode-common-initialization)
+  (require (intern (concat "context-" ConTeXt-current-interface)))
+  (funcall (intern (concat "ConTeXt-" ConTeXt-current-interface "-mode"))))
 
 (provide 'context)
 
diff --git a/tex-site.el.in b/tex-site.el.in
index 59318bd9..db708954 100644
--- a/tex-site.el.in
+++ b/tex-site.el.in
@@ -140,6 +140,11 @@ set it with `TeX-modes-set'."
   :set #'TeX-modes-set
   :initialize #'custom-initialize-reset)
 
+(if (boundp 'major-mode-remap-alist)
+    (setq major-mode-remap-alist
+          (append '((ConTeXt-mode . ConTeXt--auto-mode)
+                    (context-mode . ConTeXt--auto-mode)))))
+
 ;; COMPATIBILITY for Emacs<29, which executes
 ;; (defalias 'LaTeX-mode #'latex-mode) etc. in tex-mode.el.
 ;; This `with-eval-after-load' should be removed when the supported
-- 
2.41.0

Reply via email to