Hi all,

AUCTeX currently doesn't support fontification of macros when you have
an asterisk between the arguments.  I admit, it is rare, but still,
there are some packages which have this kind of macros, e.g.,
paracol.sty with \switchcolumn which goes like:

  \switchcolumn[col]*[text]

Now, take this small .tex file:

--8<---------------cut here---------------start------------->8---
\documentclass{article}
\usepackage{graphicx}
\begin{document}

Eval these forms,
\begin{verbatim}
(font-latex-add-keywords '(("TestMacro" "*[*{"))
                         'reference)
(font-latex-add-keywords '(("switchcolumn" "[*["))
                         'reference)
\end{verbatim}
%
and hit \verb|C-x x f| for \verb|M-x font-lock-update RET|.  Emacs
will say \verb|Char not supported|.

\TestMacro*[optional]*{mandatory}
\TestMacro*[optional]{mandatory}

\switchcolumn[text]*[text]
\switchcolumn[text][text]

\includegraphics*[width=3cm,keepaspectratio]{somefig}

\end{document}

%%% Local Variables:
%%% mode: LaTeX
%%% TeX-master: t
%%% End:
--8<---------------cut here---------------end--------------->8---

We can teach font-latex.el TDRT with this small change:

--8<---------------cut here---------------start------------->8---
diff --git a/font-latex.el b/font-latex.el
index 3da68ba4..7873a4a5 100644
--- a/font-latex.el
+++ b/font-latex.el
@@ -1617,8 +1617,7 @@ Returns nil if none of KEYWORDS is found."
           ;; Check for starred macro if first spec is an asterisk or a
           ;; plus sign in case of \defaultfontfeatures+ provided by
           ;; fontspec.sty
-          (when (or (eq (car spec-list) ?*)
-                    (eq (car spec-list) ?+))
+          (when (memql (car spec-list) '(?* ?+))
             (setq spec-list (cdr spec-list))
             (skip-chars-forward "*+" (1+ (point))))
           ;; Add current point to match data and use keyword face for
@@ -1669,6 +1668,21 @@ Returns nil if none of KEYWORDS is found."
                     (when (and match-beg (= match-beg (point)))
                       (setq error-indicator-pos match-beg))
                     (throw 'break nil))))
+               ;; Asterisk or plus sign between arguments (sigh!):
+               ((and (memql spec '(?* ?+))
+                     (= (char-after) spec))
+                (setq match-beg (point))
+                (if (= (char-after) spec)
+                    (progn
+                      (nconc match-data
+                             (list (point)
+                                   (progn
+                                     (skip-chars-forward "*+")
+                                     (point))))
+                      (nconc font-latex-matched-faces
+                             (list 'font-lock-keyword-face))
+                      (setq end (max end (point))))
+                  (throw 'break nil)))
                ;; Optional arguments: [...] and others
                ((eq (char-after) spec)
                 (setq match-beg (point))
--8<---------------cut here---------------end--------------->8---

If you don't want to apply the patch, this is the complete function:

--8<---------------cut here---------------start------------->8---
(defun font-latex-match-command-with-arguments (regexp keywords face limit)
  "Search for regexp command KEYWORDS[opt]{arg} before LIMIT.
Returns nil if none of KEYWORDS is found."
  (setq font-latex-matched-faces nil)
  (catch 'match
    (while (re-search-forward regexp limit t)
      (unless (font-latex-faces-present-p '(font-lock-comment-face
                                            font-latex-verbatim-face)
                                          (match-beginning 0))
        (let* ((beg (match-beginning 0))
               end                 ; Used for multiline text property.
               (match-data (list beg))
               match-beg syntax-error alternative spec
               error-indicator-pos
               (spec-list (string-to-list
                           (or (cadr (assoc (match-string 1) keywords))
                               font-latex-command-with-args-default-spec)))
               (parse-sexp-ignore-comments t)) ; scan-sexps ignores comments
          (goto-char (match-end 0))
          ;; Check for starred macro if first spec is an asterisk or a
          ;; plus sign in case of \defaultfontfeatures+ provided by
          ;; fontspec.sty
          (when (memql (car spec-list) '(?* ?+))
            (setq spec-list (cdr spec-list))
            (skip-chars-forward "*+" (1+ (point))))
          ;; Add current point to match data and use keyword face for
          ;; region from start to point.
          (nconc match-data (list (point)))
          (add-to-list 'font-latex-matched-faces 'font-lock-keyword-face)
          (setq end (point))
          (catch 'break
            ;; Walk the list of specs.
            (while spec-list
              (setq spec (pop spec-list)
                    error-indicator-pos beg)
              (while (and (not (eobp)) (font-latex-forward-comment)))
              ;; Alternative
              (when (eq spec ?|)
                (setq alternative t)
                (setq spec (pop spec-list)))
              (cond
               ;; Macros: \foo
               ((eq spec ?\\)
                (if (eq (char-after) spec)
                    (progn
                      (nconc match-data
                             (list (point)
                                   (progn
                                     (forward-char)
                                     (if (zerop (skip-syntax-forward "_w"))
                                         (forward-char) ; Single-char macro.
                                       (skip-chars-forward "*+"))
                                     (point))))
                      (nconc font-latex-matched-faces (list face))
                      (setq end (max end (point)))
                      (when alternative (pop spec-list)))
                  (setq syntax-error t)
                  (throw 'break nil)))
               ;; Mandatory arguments: {...}
               ((eq spec ?{)
                (if (and (eq (char-after) spec)
                         (setq match-beg (point))
                         (font-latex-find-matching-close ?{ ?}))
                    (progn
                      (nconc match-data (list (1+ match-beg) (1- (point))))
                      (nconc font-latex-matched-faces (list face))
                      (setq end (max end (1- (point))))
                      (when alternative (pop spec-list)))
                  (unless alternative
                    (setq syntax-error t)
                    (when (and match-beg (= match-beg (point)))
                      (setq error-indicator-pos match-beg))
                    (throw 'break nil))))
               ;; Asterisk or plus sign between arguments (sigh!):
               ((and (memql spec '(?* ?+))
                     (= (char-after) spec))
                (setq match-beg (point))
                (if (= (char-after) spec)
                    (progn
                      (nconc match-data
                             (list (point)
                                   (progn
                                     (skip-chars-forward "*+")
                                     (point))))
                      (nconc font-latex-matched-faces
                             (list 'font-lock-keyword-face))
                      (setq end (max end (point))))
                  (throw 'break nil)))
               ;; Optional arguments: [...] and others
               ((eq (char-after) spec)
                (setq match-beg (point))
                (if (font-latex-find-matching-close
                     spec (cdr (assq
                                spec
                                font-latex-command-with-args-opt-arg-delims)))
                    (progn
                      (nconc match-data (list (1+ match-beg) (1- (point))))
                      (nconc font-latex-matched-faces
                             (list 'font-lock-variable-name-face))
                      (setq end (max end (1- (point)))))
                  (setq syntax-error t
                        error-indicator-pos match-beg)
                  (throw 'break nil))))
              (setq alternative nil)))
          (when (and syntax-error (memq major-mode
                                        font-latex-syntax-error-modes))
            ;; Add the warning face at the front of the list because
            ;; the matcher uses 'append and the face would otherwise
            ;; be overridden by the keyword face.
            (setq match-data (append (list error-indicator-pos
                                           (1+ error-indicator-pos))
                                     match-data))
            (push 'font-latex-warning-face font-latex-matched-faces))
          (store-match-data match-data)
          (throw 'match t))))))
--8<---------------cut here---------------end--------------->8---

This is part of font-latex.el is a mystery to me, so I'd appreciate any
comments.

Best, Arash



_______________________________________________
bug-auctex mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/bug-auctex

Reply via email to