Subject: Re: bug#69069: 14.0.2; table alignment by align-current gives wrong result

From: Ikumi Keita <[email-masked]>
Date: Thu, 15 Feb 2024 16:36:47 +0900

>>>>> Ikumi Keita <[email-masked]> writes:
> The point is that AUCTeX should extend this follow-up on
> `derived-mode-p' to `provided-mode-derived-p'. I'll explain the detail
> in a few days.

Here we go.
1. In my feature branch and current AUCTeX, I added pseudo-parent mode
   facility[1][2] like emacs 30 in order to have compatibility with
   directory local variable entry prepared for former mode names like
   `latex-mode'.

2. That facility only concerns for `derived-mode-p' and doesn't tweak
   `provided-mode-derived-p'. However, the `run-if' property of
   `text-colum' entry in `align-rules-list'[3] uses
   `provided-mode-derived-p'. This discrepancy calls for a spurious
   alignment is in `LaTeX-mode' because it isn't considered as derived
   from modes listed in `align-tex-modes'

3. This spurious alignment doen't occur in emacs 30 since it overhauls
   `provided-mode-derived-p' in depth.

4. The attached patch improves the pseudo-parent mode facility to cover
   `provided-mode-derived-p' as well, and fix this bug.

5. The entry `tex-tabbing-separator' in `align-rules-list'[4] has to be
   fixed because its `run-if' property contains conditional with repect
   to the major mode without `derived-mode-p' nor
   `provided-mode-derived-p'. I'll file a bug report for this.

[1] `TeX-derived-mode-add-parents'
[2] advice for `derived-mode-p' by `TeX--compat-derived-mode-p'
[3]-------------------------------------------------------------------
    ;; Align space delimited text as columns.
    (text-column
     (regexp   . "\\(^\\|\\S-\\)\\([ \t]+\\)\\(\\S-\\|$\\)")
     (group    . 2)
     (modes    . align-text-modes)
     (repeat   . t)
     (run-if   . ,(lambda ()
                    (and (not (eq '- current-prefix-arg))
                         (not (apply #'provided-mode-derived-p
                                     major-mode align-tex-modes))))))
----------------------------------------------------------------------
[4]-------------------------------------------------------------------
    (tex-tabbing-separator
     (regexp   . ,(lambda (end reverse)
                    (align-match-tex-pattern "\\\\[=>]" end reverse)))
     (group    . (1 2))
     (modes    . align-tex-modes)
     (repeat   . t)
     (run-if   . ,(lambda ()
                    (eq major-mode 'latex-mode))))
----------------------------------------------------------------------

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine
#Gaza #StopMassiveKilling #CeasefireNOW

diff --git a/tests/tex/utility.el b/tests/tex/utility.el
index ac0a8aab..68c9a2cb 100644
--- a/tests/tex/utility.el
+++ b/tests/tex/utility.el
@@ -1,6 +1,6 @@
 ;;; utility.el --- tests for AUCTeX utility functions -*- lexical-binding: t; -*-
 
-;; Copyright (C) 2017, 2021 Free Software Foundation, Inc.
+;; Copyright (C) 2017, 2021, 2024 Free Software Foundation, Inc.
 
 ;; This file is part of AUCTeX.
 
@@ -55,4 +55,16 @@
   (TeX-add-to-alist 'TeX-dummy-alist '((a a)))
   (should (equal TeX-dummy-alist '((b 2 3) (a 1 4 a)))))
 
+(ert-deftest TeX-pseudo-parent-mode ()
+  "Check pseudo parent modes are recognized.
+For example, `LaTeX-mode' should be regarded as derived from
+`latex-mode' for compatibility with the former mode names."
+  (require 'plain-tex)
+  (require 'latex)
+  (require 'tex-info)
+  (require 'context)
+  (require 'tex-jp)
+  (dolist (mode-pair TeX-mode-comparison-alist)
+    (should (provided-mode-derived-p (cdr mode-pair) (car mode-pair)))))
+
 ;;; utility.el ends here
diff --git a/tex-info.el b/tex-info.el
index 63242e0d..fc10f018 100644
--- a/tex-info.el
+++ b/tex-info.el
@@ -1,6 +1,6 @@
 ;;; tex-info.el --- Support for editing Texinfo source.  -*- lexical-binding: t; -*-
 
-;; Copyright (C) 1993-2023  Free Software Foundation, Inc.
+;; Copyright (C) 1993-2024  Free Software Foundation, Inc.
 
 ;; Maintainer: [email-masked]
 ;; Keywords: tex
@@ -890,6 +890,11 @@ Run after mode hooks and file local variables application."
 
   (TeX-set-mode-name))
 
+;; Compatibility for former mode name.  Directory local variables
+;; prepared for `texinfo-mode' continue to be valid for
+;; `Texinfo-mode'.
+(TeX-derived-mode-add-parents 'Texinfo-mode '(texinfo-mode))
+
 (defcustom Texinfo-clean-intermediate-suffixes
   '("\\.cps?" "\\.vrs?" "\\.fns?" "\\.tps?" "\\.pgs?" "\\.kys?")
   "List of regexps matching suffixes of files to be deleted.
diff --git a/tex.el b/tex.el
index 17cb1aa9..072fb108 100644
--- a/tex.el
+++ b/tex.el
@@ -3886,15 +3886,14 @@ Run after mode hooks and file local variables application."
 
 ;; COMPATIBILITY for Emacs<30
 (unless (fboundp 'derived-mode-add-parents)
-  (advice-add 'derived-mode-p :after-until
+  (advice-add 'provided-mode-derived-p :after-until
               ;; Don't quote by #'-style to avoid compiler warning.
-              'TeX--compat-derived-mode-p)
-  (defun TeX--compat-derived-mode-p (&rest modes)
-    "Add pseudo-parents facility to `derived-mode-p' like Emacs 30.
-Modes registered in `derived-mode-extra-parents' property of the
-current major mode name symbol are regarded as parent modes as
-long as `derived-mode-p' is concerned."
-    (let ((extra-parents (get major-mode 'derived-mode-extra-parents)))
+              'TeX--compat-provided-mode-derived-p)
+  (defun TeX--compat-provided-mode-derived-p (mode &rest modes)
+    "Add pseudo-parents facility to `provided-mode-derived-p' like Emacs 30.
+Modes registered in `derived-mode-extra-parents' property of MODE
+symbol are regarded as parent modes by `provided-mode-derived-p'."
+    (let ((extra-parents (get mode 'derived-mode-extra-parents)))
       (and extra-parents
            (cl-loop for parent in extra-parents
                     thereis (memq parent modes))))))