emacs-pretest-bug
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tex-mode.el] Subscript in math not fully matched


From: Ralf Angeli
Subject: Re: [tex-mode.el] Subscript in math not fully matched
Date: Mon, 17 Apr 2006 11:28:26 +0200

* Stefan Monnier (2006-04-17) writes:

>>>>>> "Ralf" == Ralf Angeli <address@hidden> writes:
>
>> (defun font-latex-match-script (limit)
>>   "Match subscript and superscript patterns up to LIMIT."
>>   (when (and font-latex-fontify-script
>>           (re-search-forward
>>            "[_^] *\\([^\n\\{}]\\|\\\\\\(address@hidden|[^ \t\n]\\)\\|{\\)"
>>            limit t))
>>     (when (string= (match-string 1) "{")
>
> Better do (eq (char-after (match-beginning 1)) ?\{) so as to avoid consing
> a string.  Alternatively, wrpa the "{" in the regexp inside "\\(...\\)" and
> then just check if (match-end 2) is non-nil.

Okay.

> You could even make it more efficient by handling the "no-nesting" case
> directly in the regexp (as is the case now) and only revert to the elisp
> code for the more complex case.

Matching multiple characters of a complemented character alternative
is really more efficient than a call to stuff like `scan-lists'?  Okay
there is some additional code involved as well, but anyway.

The patch below uses a regexp for the non-nested case.  If you change
your mind, just remove that part.

>>       (let ((beg1 (match-beginning 1))
>>          (end (TeX-find-closing-brace)))
>>      (if end
>>          (store-match-data (list (match-beginning 0) end beg1 end))
>>        (store-match-data (list beg1 beg1 beg1 beg1)))))
>>     t))
>
> Why not merge the two branches of the if:
>
>       (store-match-data (if end
>                               (list (match-beginning 0) end beg1 end))
>                             (store-match-data (list beg1 beg1 beg1 beg1)))

Oh well. (c:

>> I guess I could come up with a similar fix for tex-mode.el if somebody
>> is interested.
>
> Please do,

2006-04-17  Ralf Angeli  <address@hidden>

        * textmodes/tex-mode.el (tex-font-lock-match-suscript): New
        function.
        (tex-font-lock-keywords-3): Use it.

--- tex-mode.el 19 Feb 2006 12:12:41 +0100      1.180
+++ tex-mode.el 17 Apr 2006 11:18:32 +0200      
@@ -594,20 +594,24 @@
        '(face subscript display (raise -0.3))
       '(face superscript display (raise +0.3)))))
 
+(defun tex-font-lock-match-suscript (limit)
+  "Match subscript and superscript patterns up to LIMIT."
+  (when (re-search-forward "[_^] *\\([^\n\\{}]\\|\
+\\\\\\(address@hidden|[^ \t\n]\\)\\|{[^{]*}\\|\\({\\)\\)" limit t)
+    (when (match-end 3)
+      (let ((beg (match-beginning 3))
+           (end (save-restriction
+                  (narrow-to-region (point-min) limit)
+                  (condition-case nil (scan-lists (point) 1 1) (error nil)))))
+       (store-match-data (if end
+                             (list (match-beginning 0) end beg end))
+                         (list beg beg beg beg))))
+    t))
+
 (defconst tex-font-lock-keywords-3
   (append tex-font-lock-keywords-2
-   (eval-when-compile
-     (let ((general "\\(address@hidden|[^ \t\n]\\)")
-          (slash "\\\\")
-          ;; This is not the same regexp as before: it has a `+' removed.
-          ;; The + makes the matching faster in the above cases (where we can
-          ;; exit as soon as the match fails) but would make this matching
-          ;; degenerate to nasty complexity (because we try to match the
-          ;; closing brace, which forces trying all matching combinations).
-          (arg "{\\(?:[^{}\\]\\|\\\\.\\|{[^}]*}\\)*"))
-       `((,(concat "[_^] *\\([^\n\\{}#]\\|" slash general "\\|#[0-9]\\|" arg 
"}\\)")
-         (1 (tex-font-lock-suscript (match-beginning 0))
-            append))))))
+         '((tex-font-lock-match-suscript
+            (1 (tex-font-lock-suscript (match-beginning 0)) append))))
   "Experimental expressions to highlight in TeX modes.")
 
 (defvar tex-font-lock-keywords tex-font-lock-keywords-1
-- 
Ralf

reply via email to

[Prev in Thread] Current Thread [Next in Thread]