Subject: Re: bug#70811: ConTeXt-mode-cleanup clobbers my paragraph-start

From: Ikumi Keita <[email-masked]>
Date: Thu, 09 May 2024 15:14:00 +0900

Hi Jim,

>>>>> Jim <[email-masked]> writes:
>> I think I can implement `ConTeXt-paragraph-commands' like LaTeX mode and
>> then you can set it instead of `paragraph-start'. What do you think
>> about this idea?

> I spent a few minutes looking at the code; I now have a glimmer of
> understanding, thanks.

> I still am unclear about why the setting of paragraph-start is done in
> the after-hook, rather than before the user's hook is called. However,
> it may well be that I don't need to understand this.

Suppose that there is a file local variable entry for
`LaTeX-paragraph-commands'. LaTeX mode uses after-hook so that its value
is reflected to `paragraph-start'. In addition, AUCTeX style files
casually overwrite `paragraph-start' to support various paragraph
commands; in that procedure, they never care what the user set to
`paragraph-start' in the mode hook and just overwrite. In other words,
AUCTeX doesn't consider `paragraph-start' as a variable which is to be
customized in the mode hook. The users are expected to use other
variables to control `paragraph-start'.

> I think your solution would work for me. My current paragraph-start
> does not have "[ \t]*" and yet does what I want, but perhaps having "[
> \t]*" won't hurt. Maybe it will make things better in some way I don't
> see now. Time will tell.

> In summary, thanks very much for the response, and if you are able to
> implement that for ConTeXt-mode I would very much appreciate it.

Thanks. Can you try the attached patch? If you customize the new option
`ConTeXt-paragraph-commands', its value will be reflected to
`paragraph-start' (for new buffers created after the customization).

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

>From b6bbde5c5678f23fdf6031d7f5dd29e811a792cd Mon Sep 17 00:00:00 2001
From: Ikumi Keita <[email-masked]>
Date: Thu, 9 May 2024 14:47:06 +0900
Subject: [PATCH] Improve paragraph commands management for ConTeXt mode
 (bug#70811)

* context.el (ConTeXt-paragraph-commands): New customize option similar
to LaTeX counterpart.
(ConTeXt-extra-paragraph-commands): Provide proper defvar.
(ConTeXt-paragraph-commands-regexp-make): Rename from
`ConTeXt-paragraph-commands-regexp' for consistency with latex.el.
Make efficient regexp using `regexp-opt'.
(ConTeXt-paragraph-commands-regexp): Add alias for backward
compatibility.
(ConTeXt-set-paragraph-start): Factor out following latex.el.
(ConTeXt-paragraph-commands-add-locally): New convinience function
similar to LaTeX counterpart.
(ConTeXt-mode-cleanup): Use `ConTeXt-set-paragraph-start' so that the
value of `ConTeXt-paragraph-commands' is refected to `paragraph-start'.
---
 context.el | 79 +++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 57 insertions(+), 22 deletions(-)

diff --git a/context.el b/context.el
index 0d5b9adb..3ccd0637 100644
--- a/context.el
+++ b/context.el
@@ -53,9 +53,6 @@
 (require 'latex) ; for functions like `TeX-look-at' and `LaTeX-split-long-menu'
 (require 'plain-tex) ; for `plain-TeX-common-initialization'
 
-;; Silence the compiler:
-(defvar ConTeXt-extra-paragraph-commands)
-
 (defgroup ConTeXt-macro nil
   "Special support for ConTeXt macros in AUCTeX."
   :prefix "TeX-"
@@ -1121,18 +1118,61 @@ If OPTIONAL, only insert it if not empty, and then use square brackets."
 (defvar ConTeXt-item-list ()
   "List of macro's considered items.")
 
-(defun ConTeXt-paragraph-commands-regexp ()
-  "Return a regexp matching macros that should have their own line."
+(defvar ConTeXt-extra-paragraph-commands nil
+  "List of default ConTeXt macros that should begin their own line.
+Updated in language-specific initialization.")
+
+(defcustom ConTeXt-paragraph-commands nil
+  "List of user ConTeXt macros that should begin their own line.
+The list should contain macro names without the leading backslash.
+
+If you change this variable, its value is reflected to only new buffers
+created afterwards; existing ConTeXt mode buffers aren't affected."
+  :group 'ConTeXt-macro
+  :type '(repeat (string)))
+
+(defun ConTeXt-paragraph-commands-regexp-make ()
+  "Return a regexp matching macros that should begin their own line."
   (concat
-   (regexp-quote TeX-esc) "\\("
+   (regexp-quote TeX-esc) "\\(?:"
    "[][]\\|"  ; display math delimitors (is this applicable to ConTeXt??)
-   (ConTeXt-environment-start-name) "\\|"
-   (ConTeXt-environment-stop-name) "\\|"
-   (mapconcat #'car ConTeXt-numbered-section-list "\\b\\|") "\\b\\|"
-   (mapconcat #'car ConTeXt-unnumbered-section-list "\\b\\|") "\\b\\|"
-   (mapconcat #'identity ConTeXt-extra-paragraph-commands "\\b\\|")
-   "\\b\\|"
-   (mapconcat #'identity ConTeXt-item-list "\\b\\|") "\\b\\)"))
+   (regexp-opt
+    (append
+     (list
+      (ConTeXt-environment-start-name)
+      (ConTeXt-environment-stop-name))
+     (mapcar #'car ConTeXt-numbered-section-list)
+     (mapcar #'car ConTeXt-unnumbered-section-list)
+     ConTeXt-paragraph-commands
+     ConTeXt-extra-paragraph-commands
+     ConTeXt-item-list)
+    'words)
+   "\\)"))
+;; Backward compatibility.
+(defalias 'ConTeXt-paragraph-commands-regexp
+  #'ConTeXt-paragraph-commands-regexp-make)
+
+(defun ConTeXt-set-paragraph-start ()
+  "Set `paragraph-start'."
+  (setq paragraph-start
+        (concat
+         "[ \t]*\\(?:"
+         LaTeX-paragraph-commands-regexp "\\|"
+         "\\$\\$\\|" ; Plain TeX display math
+         "$\\)")))
+
+(defun ConTeXt-paragraph-commands-add-locally (commands)
+  "Make COMMANDS be recognized as paragraph commands.
+COMMANDS can be a single string or a list of strings which will be added
+to `ConTeXt-extra-paragraph-commands'.  Additionally
+`LaTeX-paragraph-commands-regexp' will be updated.
+This is mainly a convenience function which can be used in style files."
+  (unless (listp commands) (setq commands (list commands)))
+  (dolist (elt commands)
+    (add-to-list 'ConTeXt-extra-paragraph-commands elt))
+  (setq-local LaTeX-paragraph-commands-regexp
+              (ConTeXt-paragraph-commands-regexp-make))
+  (ConTeXt-set-paragraph-start))

 ;; Outline support
@@ -1908,16 +1948,11 @@ Run after mode hooks and file local variables application."
                    (mapconcat #'identity ConTeXt-item-list "\\|")
                    "\\)\\>")))
 
-  ;; Don't do locally-bound test for `LaTeX-paragraph-commands-regexp'
-  ;; and `paragraph-start'.  See comments in similar part in latex.el.
   (setq-local LaTeX-paragraph-commands-regexp
-              (ConTeXt-paragraph-commands-regexp))
-  (setq paragraph-start
-        (concat
-         "[ \t]*\\("
-         (ConTeXt-paragraph-commands-regexp) "\\|"
-         "\\$\\$\\|" ; Plain TeX display math
-         "$\\)")))
+              (ConTeXt-paragraph-commands-regexp-make))
+  ;; Don't do locally-bound test for `paragraph-start'.  See comments in
+  ;; similar part in latex.el.
+  (ConTeXt-set-paragraph-start))
 
 (defun context-guess-current-interface ()
   "Guess what ConTeXt interface the current buffer is using."
-- 
2.44.0