Ralf Angeli <[EMAIL PROTECTED]> writes:

> * David Kastrup (2005-06-07) writes:
>
>> Ralf Angeli <[EMAIL PROTECTED]> writes:
>>
>> Well, I thought about doing this "right" in every case.  And then I
>> thought about "[\\(]" and decided that I had no real chance.
>>
>> Maybe something that will work in more cases is reasonably easy to
>> do and not too inefficient.  I am not sure it will be worth the
>> trouble, but if somebody else is of a different opinion, I don't
>> mind if he comes up with something.
>
> What about the following ol' trick?
>
> (string-match "\\(^\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\(" "\\\\(")
>
> As you haven't come up with this I am probably missing
> something. (c;

An anchored search will not match either ^ nor [^\\] at the anchor
position.  But this can probably be cured by backstepping into the
last ( before resuming the search.  It still does not deal with
"[\\(]" though, but we probably can live with that.

So the revised patch would be something like

--- tex.el	05 Jun 2005 19:07:23 +0200	5.524
+++ tex.el	07 Jun 2005 13:58:22 +0200	
@@ -2669,28 +2669,72 @@
   :group 'TeX-parse)
   (make-variable-buffer-local 'TeX-auto-x-regexp-list)
 
+(defun TeX-regexp-group-count (regexp)
+  "Return number of groups in a regexp.  This is not foolproof:
+you should not use something like `[\\(]' for a character range."
+  (let (start (n 0))
+    (while (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\([^?]"
+			 regexp start)
+      (setq start (- (match-end 0) 2)
+	    n (1+ n)))
+    n))
+
 (defun TeX-auto-parse-region (regexp-list beg end)
   "Parse TeX information according to REGEXP-LIST between BEG and END."
   (if (symbolp regexp-list)
       (setq regexp-list (and (boundp regexp-list) (symbol-value regexp-list))))
    (if regexp-list
        ;; Extract the information.
-       (let ((regexp (concat "\\("
-			     (mapconcat 'car regexp-list "\\)\\|\\(")
-			     "\\)")))
+       (let* (groups
+	      (count 1)
+	      (regexp (concat "\\("
+			      (mapconcat
+			       #'(lambda(x)
+				   (push (cons count x) groups)
+				   (setq count
+					 (+ 1 count
+					    (TeX-regexp-group-count (car x))))
+				   (car x))
+			       regexp-list "\\)\\|\\(")
+			      "\\)"))
+	      (hash (make-hash-table :test 'equal))
+	      syms
+	      lst)
+	 (setq count 0)
 	 (goto-char (if end (min end (point-max)) (point-max)))
 	 (while (re-search-backward regexp beg t)
-	   (unless (TeX-in-comment)
-	     (let* ((entry (TeX-member nil regexp-list
-				       (lambda (a b)
-					 (looking-at (nth 0 b)))))
-		    (symbol (nth 2 entry))
-		    (match (nth 1 entry)))
+	   (let* ((entry (cdr (TeX-member nil groups
+					  #'(lambda (a b)
+					      (match-beginning (car b))))))
+		  (symbol (nth 2 entry))
+		  (match (nth 1 entry)))
+	     (unless (TeX-in-comment)
+	       (looking-at (nth 0 entry))
 	       (if (fboundp symbol)
 		   (funcall symbol match)
-		 (add-to-list symbol (if (listp match)
-					 (mapcar 'TeX-match-buffer match)
-				       (TeX-match-buffer match))))))))))
+		 (add-to-list 'syms symbol)
+		 (puthash (cons symbol (if (listp match)
+					   (mapcar 'TeX-match-buffer match)
+					 (TeX-match-buffer match)))
+			  (setq count (1- count))
+			  hash)))))
+	 (setq count 0)
+	 (dolist (symbol syms)
+	   (setq lst (symbol-value symbol))
+	   (set symbol nil)
+	   (while lst
+	     (puthash (cons symbol
+			    (pop lst))
+		      (setq count (1+ count))
+		      hash)))
+	 (maphash (lambda (key value)
+		    (set (car key) (cons (cons value (cdr key))
+					 (symbol-value (car key)))))
+		  hash)
+	 (clrhash hash)
+	 (dolist (symbol syms)
+	   (set symbol (mapcar #'cdr (sort (symbol-value symbol)
+					   #'car-less-than-car)))))))
 
 (defun TeX-auto-parse ()
   "Parse TeX information in current buffer.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum
_______________________________________________
auctex-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/auctex-devel

Reply via email to