--- 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.