Hi all,

I propose the attched three patches with respect to TikZ and ConTeXt. If
no objection, I'll commit them.

I tried to improve tikz.el to arrive at the attached first patch. As you
can see, it includes preliminary support for ConTeXt and plain TeX as
well.

However, I realized that ConTeXt support doesn't work at all. The reason
is that `ConTeXt-add-environments' is broken as well as context.el
doesn't use `ConTeXt-environment-list' correctly.

I figured out that `TeX-auto-add-type' is responsible for the failure of
`ConTeXt-add-environments'. It adds entries in `TeX-auto-parser' in a
form like
("environment" ConTeXt-auto-environment ConTeXt-add-environments 
ConTeXt-environment-list ConTeXt-environment-changed)
, where "environment" is used as a key to extract each component from
the entry. However, this entry conflicts with another entry
("environment" LaTeX-auto-environment LaTeX-add-environments 
LaTeX-environment-list LaTeX-environment-changed)
, which shares the same "environment" as a key.

Thus I propose to apply the attached second patch to make the key to be
unique.

The third patch fixes the problem that tikz.el inserts empty node name
"()" when the user gave no node name.

Any comments and discussions are welcome.

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine

>From fdcdf202cf59f5650319e337ad65a059b4bc641c Mon Sep 17 00:00:00 2001
From: Ikumi Keita <ik...@ikumi.que.jp>
Date: Tue, 9 Aug 2022 19:37:29 +0900
Subject: [PATCH 1/3] Improve support for TikZ

* style/tikz.el ("tikz"): Add "tikz", "tikzset", "usetikzlibrary" and
"foreach" macros.
Run style hooks for graphicx, keyval and xcolor as well.
Include preliminary support for ConTeXt and plain TeX.
Allow optional argument for "tikzpicture" environment.
Add "scope" environment.
(AUCTeX-TikZ): New customize group.
(TeX-TikZ-point-name-regexp): Use it as group.
(TeX-TikZ-find-named-points): Add comment.
(): Add `declare-function'.
---
 style/tikz.el | 61 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 58 insertions(+), 3 deletions(-)

diff --git a/style/tikz.el b/style/tikz.el
index 565e6b0a..eb7d9281 100644
--- a/style/tikz.el
+++ b/style/tikz.el
@@ -33,11 +33,19 @@
 (require 'tex)
 (require 'latex)
 
+;; Silence compiler
+(declare-function ConTeXt-add-environments "context"
+                  (&rest environments))
+
+(defgroup AUCTeX-TikZ nil
+  "AUCTeX TikZ support"
+  :group 'AUCTeX)
+
 (defcustom TeX-TikZ-point-name-regexp
   "(\\([A-Za-z0-9]+\\))"
   "A regexp that matches TikZ names."
   :type 'regexp
-  :group 'auctex-tikz)
+  :group 'AUCTeX-TikZ)
 
 (defconst TeX-TikZ-point-function-map
   '(("Rect Point" TeX-TikZ-arg-rect-point)
@@ -199,6 +207,10 @@ is finished."
 Begin by finding the span of the current TikZ enviroment and then
 searching within that span to find all named-points and return
 them as a list of strings, dropping the \\='()\\='."
+  ;; FIXME: This function depends on `LaTeX-find-matching-begin' and
+  ;; `LaTeX-find-matching-end', so it doesn't work for ConTeXt and
+  ;; plain TeX.  In addition, it isn't compatible with the TikZ code
+  ;; following \tikz.
   (let* ((env-end (save-excursion
                     (LaTeX-find-matching-end)
                      (point)))
@@ -278,8 +290,51 @@ return \"\"."
    (TeX-add-symbols
     '("draw" (TeX-TikZ-draw-arg))
     '("coordinate" (TeX-TikZ-coordinate-arg))
-    '("node" (TeX-TikZ-node-arg)))
+    '("node" (TeX-TikZ-node-arg))
+    '("tikz" ["TikZ option"])
+    '("tikzset" "TikZ option")
+    ;; FIXME:
+    ;; 1. usetikzlibrary isn't much useful without completion support
+    ;;    for available libraries.
+    ;; 2. ConTeXt users may prefer [...] over {...} as the argument.
+    '("usetikzlibrary" t)
+    ;; XXX: Maybe we should create pgffor.el and factor out this entry
+    ;; into it.
+    '("foreach" (TeX-arg-literal " ") (TeX-arg-free "Variable(s)")
+      (TeX-arg-literal " ") ["Foreach option"]
+      (TeX-arg-literal " in ") "Value list (Use \"...\" for range)"
+      (TeX-arg-literal " ") t))))
+
+;; LaTeX/docTeX specific stuff
+(TeX-add-style-hook
+ "tikz"
+ (lambda ()
    (LaTeX-add-environments
-    '("tikzpicture"))))
+    '("tikzpicture" ["TikZ option"])
+    '("scope" ["TikZ option"]))
+   ;; tikz.sty loads pgfcore.sty, which loads packages graphicx,
+   ;; keyval and xcolor, too.
+   (TeX-run-style-hooks "pgf" "graphicx" "keyval" "xcolor"))
+ :latex)
+
+;; ConTeXt specific stuff
+(TeX-add-style-hook
+ "tikz"
+ (lambda ()
+   (ConTeXt-add-environments
+    '("tikzpicture" ["TikZ option"])
+    '("scope" ["TikZ option"])))
+ :context)
+
+;; plain TeX specific stuff
+(TeX-add-style-hook
+ "tikz"
+ (lambda ()
+   (TeX-add-symbols
+    '("tikzpicture" ["TikZ option"])
+    "endtikzpicture"
+    '("scope" ["TikZ option"])
+    "endscope"))
+ :plain-tex)
 
 ;;; tikz.el ends here
-- 
2.37.3

>From fa7b75f6093a896805d42f9b53c613535fb7daf5 Mon Sep 17 00:00:00 2001
From: Ikumi Keita <ik...@ikumi.que.jp>
Date: Mon, 15 Aug 2022 00:14:13 +0900
Subject: [PATCH 2/3] Fix `ConTeXt-add-environments'

* context.el (ConTeXt-environment, ConTeXt-environment-menu): Use
function `ConTeXt-environment-list' to refer to the current
environments list.
(ConTeXt-environment-menu): Follow `LaTeX-environment-menu' to support
optional argument for environments.
* tex.el (TeX-auto-add-type): Use unique key for `TeX-auto-parser' in
order to discriminate ConTeXt environments from LaTeX environments.
* context-en.el (ConTeXt-en-mode-initialization):
* context-nl.el (ConTeXt-nl-mode-initialization):
Add comments.
---
 context-en.el |  5 ++++-
 context-nl.el |  5 ++++-
 context.el    | 25 ++++++++++++++-----------
 tex.el        | 12 ++++++++----
 4 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/context-en.el b/context-en.el
index f4b8fdf8..54f30da3 100644
--- a/context-en.el
+++ b/context-en.el
@@ -1,7 +1,7 @@
 ;;; context-en.el --- Support for the ConTeXt english interface. -*- lexical-binding: t; -*-
 
 ;; Copyright (C) 2003-2004, 2006, 2008
-;;               2010, 2014, 2020, 2021 Free Software Foundation, Inc.
+;;               2010, 2014, 2020-2022 Free Software Foundation, Inc.
 
 ;; Maintainer: Berend de Boer <ber...@pobox.com>
 ;; Keywords: tex
@@ -188,6 +188,9 @@ That is, besides the section(-block) commands.")
 
 (defun ConTeXt-en-mode-initialization ()
   "ConTeXt english interface specific initialization."
+  ;; FIXME: This `mapc' seems spurious because
+  ;; `ConTeXt-language-variable-list' includes
+  ;; `ConTeXt-environment-list'.
   (mapc #'ConTeXt-add-environments (reverse ConTeXt-environment-list-en))
 
   (TeX-add-symbols
diff --git a/context-nl.el b/context-nl.el
index 9b49cce2..143a18a3 100644
--- a/context-nl.el
+++ b/context-nl.el
@@ -1,7 +1,7 @@
 ;;; context-nl.el --- Support for the ConTeXt dutch interface. -*- lexical-binding: t; -*-
 
 ;; Copyright (C) 2003, 2004, 2006, 2010,
-;;               2015, 2020, 2021 Free Software Foundation, Inc.
+;;               2015, 2020-2022 Free Software Foundation, Inc.
 
 ;; Maintainer: Berend de Boer <ber...@pobox.com>
 ;; Keywords: tex
@@ -163,6 +163,9 @@ That is, besides the section(-block) commands.")
 
 (defun ConTeXt-nl-mode-initialization ()
   "ConTeXt dutch interface specific initialization."
+  ;; FIXME: This `mapc' seems spurious because
+  ;; `ConTeXt-language-variable-list' includes
+  ;; `ConTeXt-environment-list'.
   (mapc #'ConTeXt-add-environments (reverse ConTeXt-environment-list-nl))
 
   (TeX-add-symbols
diff --git a/context.el b/context.el
index 50fb1adf..40eacda8 100644
--- a/context.el
+++ b/context.el
@@ -680,14 +680,14 @@ With optional ARG, modify current environment."
                    (t ConTeXt-default-environment)))
          (environment
           (completing-read (concat "Environment type (default " default "): ")
-                           ConTeXt-environment-list nil nil nil
+                           (ConTeXt-environment-list) nil nil nil
                            'ConTeXt-environment-history default)))
     ;; Use `environment' as default for the next time only if it is different
     ;; from the current default.
     (unless (equal environment default)
       (setq ConTeXt-default-environment environment))
 
-    (let ((entry (assoc environment ConTeXt-environment-list)))
+    (let ((entry (assoc environment (ConTeXt-environment-list))))
       (if (null entry)
           (ConTeXt-add-environments (list environment)))
 
@@ -717,7 +717,7 @@ With optional ARG, modify current environment."
 
 (defun ConTeXt-environment-menu (environment)
   "Insert ENVIRONMENT around point or region."
-  (let ((entry (assoc environment ConTeXt-environment-list)))
+  (let ((entry (assoc environment (ConTeXt-environment-list))))
     (cond ((not (and entry (nth 1 entry)))
            (ConTeXt-insert-environment environment))
           ((numberp (nth 1 entry))
@@ -727,16 +727,19 @@ With optional ARG, modify current environment."
                (setq args (concat args TeX-grop TeX-grcl))
                (setq count (- count 1)))
              (ConTeXt-insert-environment environment args)))
-          ((stringp (nth 1 entry))
+          ((or (stringp (nth 1 entry)) (vectorp (nth 1 entry)))
            (let ((prompts (cdr entry))
                  (args ""))
-             (while prompts
-               (setq args (concat args
-                                  TeX-grop
-                                  (read-from-minibuffer
-                                   (concat (car prompts) ": "))
-                                  TeX-grcl))
-               (setq prompts (cdr prompts)))
+             (dolist (elt prompts)
+               (let* ((optional (vectorp elt))
+                      (elt (if optional (elt elt 0) elt))
+                      (arg (TeX-read-string
+                            (TeX-argument-prompt optional elt nil))))
+                 (setq args (concat args
+                                    (cond ((and optional (> (length arg) 0))
+                                           (concat ConTeXt-optop arg ConTeXt-optcl))
+                                          ((not optional)
+                                           (concat TeX-grop arg TeX-grcl)))))))
              (ConTeXt-insert-environment environment args)))
           (t
            (apply (nth 1 entry) environment (nthcdr 2 entry))))))
diff --git a/tex.el b/tex.el
index 976fafba..f6c55ed8 100644
--- a/tex.el
+++ b/tex.el
@@ -3919,7 +3919,10 @@ separate type of information in the parser."
          (change (intern (concat prefix "-" name "-changed")))
          (vardoc (concat "Information about " names
                           " in the current buffer.
-Generated by `TeX-auto-add-type'.")))
+Generated by `TeX-auto-add-type'."))
+         ;; Avoid clash between LaTeX environments and ConTeXt
+         ;; environments in keys of `TeX-auto-parser'.
+         (unique-key (concat prefix "-" name)))
     `(progn
        (defvar ,tmp nil ,vardoc)
        (defvar ,local nil ,vardoc)
@@ -3930,15 +3933,16 @@ Generated by `TeX-auto-add-type'.")))
          ,(concat "Add information about " (upcase names)
                   " to the current buffer.
 Generated by `TeX-auto-add-type'.")
-         (TeX-auto-add-information ,name ,(intern names)))
+         (TeX-auto-add-information ,unique-key ,(intern names)))
        (defun ,local ()
          ,(concat "List of " names
                   " active in the current buffer.
 Generated by `TeX-auto-add-type'.")
-         (TeX-auto-list-information ,name))
+         (TeX-auto-list-information ,unique-key))
        ;; Append new type to `TeX-auto-parser' in order to make `style' type
        ;; always the first.
-       (add-to-list 'TeX-auto-parser ',(list name tmp add local change) t)
+       (add-to-list 'TeX-auto-parser
+                    ',(list unique-key tmp add local change) t)
        (add-hook 'TeX-remove-style-hook
                  (lambda ()
                    (setq ,local nil))))))
-- 
2.37.3

>From 22efb8ed0023757ee02ee97a073e79287db468df Mon Sep 17 00:00:00 2001
From: Ikumi Keita <ik...@ikumi.que.jp>
Date: Sat, 8 Oct 2022 23:16:25 +0900
Subject: [PATCH 3/3] Don't insert empty node name

* style/tikz.el (TeX-TikZ-node-arg): Treat node name as optional to
avoid the insertion of empty node name.
(TeX-TikZ-arg-options, TeX-TikZ-arg-name,TeX-TikZ-arg-label):
Fix doc strings.
---
 style/tikz.el | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/style/tikz.el b/style/tikz.el
index eb7d9281..a0edfd46 100644
--- a/style/tikz.el
+++ b/style/tikz.el
@@ -112,7 +112,7 @@ Ask the user for r and theta values, and return the string
 
 (defun TeX-TikZ-arg-options (optional)
   "Prompt the user for options to a TikZ macro.
-If OPTIONAL is non-nil, always return `LaTeX-optop' and
+If OPTIONAL is nil, always return `LaTeX-optop' and
 `LaTeX-optcl', even if the user doesn't provide any input."
   (let ((options (TeX-read-string (TeX-argument-prompt optional nil "Options" ))))
     (if optional
@@ -121,7 +121,7 @@ If OPTIONAL is non-nil, always return `LaTeX-optop' and
 
 (defun TeX-TikZ-arg-name (optional)
   "Prompt the user for a TikZ name.
-If OPTIONAL is non-nil, always return \"()\", even if the user
+If OPTIONAL is nil, always return \"()\", even if the user
 doesn't provide any input."
   (let ((name (TeX-read-string (TeX-argument-prompt optional nil "Name" ))))
     (if optional
@@ -130,8 +130,8 @@ doesn't provide any input."
 
 (defun TeX-TikZ-arg-label (optional)
   "Prompt the user for TikZ label.
-If OPTIONAL is non-nil always return `TeX-grop' and `TeX-grcl',
-even if the user doesn't provide any input."
+If OPTIONAL is nil always return `TeX-grop' and `TeX-grcl', even
+if the user doesn't provide any input."
   (let ((label (TeX-read-string (TeX-argument-prompt optional nil "Label" ))))
     (if optional
         (TeX-TikZ-get-opt-arg-string label TeX-grop TeX-grcl)
@@ -278,7 +278,7 @@ return \"\"."
 (defun TeX-TikZ-node-arg (_ignored)
   "Prompt the user for the arguments to a TikZ node macro."
   (let ((options (TeX-TikZ-arg-options t))
-        (name (TeX-TikZ-arg-name nil))
+        (name (TeX-TikZ-arg-name t))
         (point (TeX-TikZ-single-macro-arg TeX-TikZ-point-function-map
                                           "Node point type: "))
         (label (TeX-TikZ-arg-label nil)))
-- 
2.37.3

Reply via email to