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

From: Ikumi Keita <[email-masked]>
Date: Tue, 14 May 2024 00:34:11 +0900

Hi Jim,

>>>>> Jim <[email-masked]> writes:
> Thank you very much for spending some time on this.  I tried it on this
> section of code

> \startitemize[yadda]
> \item sslkdslkd lskdlskdls slkd slkdslk
> sldk sldkl
> lkslks d sldsldk s sldksldk slkdsldksd sldsldk s sldksldk slkdsldksd
> sldsldk s sldksldk slkdsldksd
> \item sldsldk s sldksldk slkdsldksd
> \item fill this paragraph right here
> fill this paragraph right here fill this paragraph right here
> fill this paragraph right here
> \stopitemize

> and if I run fill-paragraph on the last item, I get

> ...
> \item fill this paragraph right here fill this paragraph right here fill
>   this paragraph right here fill this paragraph right here \stopitemize

> (i.e., the \stopitemize has become "part of" the "\item" paragraph).  I
> won't try to imagine what other ConTeXt users want, but I don't like this.

Thanks for your report. I'm sorry, I was wrong at altering regexp
generation code. Can you try the attached patch? (Undo the last patch
and apply the attached patch to the fresh source, please.)

> I wonder if the default definition should (could?) somehow capture all
> of them.

I expect the revised patch fills that bill.

> Given that the (interactive) customization doesn't want the leading
> '\',

Do you mean that `ConTeXt-paragraph-commands' doesn't include the
leading backslash? Then it isn't specific to interactive customization.
It is desgined after LaTeX counterpart, which also doesn't include the
leading backslash.

> is there any way to add that to paragraph-start with the code you
> recently added?

You can use
(add-hook 'LaTeX-mode-hook
  (lambda ()
     (add-hook 'find-file-hook
        (lambda ()
           (setq paragraph-start "abc")) <-- Set whatever you like
        nil t)))
in your init file if you really need, but I'd like you to try the patch
below first.

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

>From edc178926f57eca69c1676715657486f2e6ac952 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.
Include `ConTeXt-paragraph-commands' to generate the regexp.
(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 new functions.
---
 context.el | 63 +++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 48 insertions(+), 15 deletions(-)

diff --git a/context.el b/context.el
index 0d5b9adb..8b0c88ee 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,59 @@ 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.
+Unlike `ConTeXt-paragraph-commands', each entry should be a regular
+expression without leading backslash.
+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-paragraph-commands "\\b\\|") "\\b\\|"
    (mapconcat #'identity ConTeXt-extra-paragraph-commands "\\b\\|")
    "\\b\\|"
    (mapconcat #'identity ConTeXt-item-list "\\b\\|") "\\b\\)"))
+;; 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 +1946,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