;;; altsubsup.el --- AUCTeX style for `altsubsup.sty'  -*- lexical-binding: t; -*-

;; Copyright (C) 2022-2026 by Julien Labbé <Julien.Labbe@univ-grenoble-alpes.fr>

;; This file may be distributed and/or modified under the conditions of the LaTeX
;; Project Public License, either version 1.3 of this license or (at your option)
;; any later version. The latest version of this license is in:
;;      http://www.latex-project.org/lppl.txt
;; and version 1.3c or later is part of all distributions of LaTeX version
;; 2008/05/04 or later.

;;; Commentary:

;; This file adds support for `altsubsup.sty'. It was written with the
;; assistance of Google Gemini.

;;; Code:

(require 'tex)
(require 'latex)

;; Silence the compiler:
(declare-function font-latex-add-keywords
                  "font-latex"
                  (keywords class))

(defvar LaTeX-altsubsup-key-val-options
  '(;; Alternative format setting
    ("command")
    ("subscript-command")
    ("superscript-command")
    ("put-left")
    ("subscript-put-left")
    ("superscript-put-left")
    ;; Script selection
    ("only-subscripts")
    ("subscript")
    ("only-superscripts")
    ("superscript")
    ("subscripts-and-superscripts")
    ("both")
    ("disable")
    ;; active-symbols mode specific keys
    ("no-catcode-warnings" ("true" "false"))
    ("patch-macro")
    ;; active-delimiters mode specific keys
    ("enable")
    ("new-delimiters")
    ("define-letter-command")
    ;; spbmark interface keys
    ("spbmark")
    ("spbmark-subscript-style")
    ("spbmark-superscript-style")
    ("spbmark-style"))
  "Key=value options for \\altsubsupset from `altsubsup' package.")

(defun LaTeX-altsubsup-fontify ()
  "Add font-lock keywords for `_[...]' and `^[...]'."
  (font-lock-add-keywords
   nil
   '(("\\^\\(\\[[^]]*\\]\\)" . 'font-latex-superscript-face)
     ("_\\(\\[[^]]*\\]\\)"   . 'font-latex-subscript-face))))

(TeX-add-style-hook
 "altsubsup"
 (lambda ()

   ;; Load styles

   (TeX-run-style-hooks "amstext")

   (when (LaTeX-provided-package-options-member "altsubsup" "spbmark")
     (TeX-run-style-hooks "spbmark"))

   ;; Add symbols

   (TeX-add-symbols
    ;; Main key-val configuration macro
    '("altsubsupset"
      (TeX-arg-key-val LaTeX-altsubsup-key-val-options))

    ;; Active-delimiters mode specific macros
    '("leftbracket" 0)
    '("lbrack" 0)

    ;; Deprecated macros
    '("SetAltSubscriptCommand" TeX-arg-macro)
    '("SetAltSubscriptCommand*" t)
    '("SetAltSuperscriptCommand" TeX-arg-macro)
    '("SetAltSuperscriptCommand*" t)
    '("SetAltSubSupCommands" TeX-arg-macro)
    '("SetAltSubSupCommands*" t))

   ;; Fontify

   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))

     (font-latex-add-keywords '(("altsubsupset" "{")
                                ("SetAltSubscriptCommand" "*{")
                                ("SetAltSuperscriptCommand" "*{")
                                ("SetAltSubSupCommands" "*{"))
                              'function)

     (font-latex-add-keywords '(("leftbracket" "")
                                ("lbrack" ""))
                              'math-command)

     (add-hook 'TeX-update-style-hook #'LaTeX-altsubsup-fontify nil t)))

 TeX-dialect)

(defvar LaTeX-altsubsup-package-options-list
  '(("mode" ("lua" "active-symbols" "active-delimiters" "nolua"))
    ("nolua")
    ("only-subscripts")
    ("subscript")
    ("only-superscripts")
    ("superscript")
    ("subscripts-and-superscripts")
    ("both")
    ("spbmark"))
  "Package options for the `altsubsup' package.")

(defun LaTeX-altsubsup-package-options ()
  "Prompt for package options for the `altsubsup' package."
  (TeX-read-key-val t LaTeX-altsubsup-package-options-list))

;;; altsubsup.el ends here
