branch: externals/auctex commit a27850faedf2a9c0871a558353a8f8f42597794c Author: Ikumi Keita <ik...@ikumi.que.jp> Commit: Ikumi Keita <ik...@ikumi.que.jp>
Fix previous commit and add new test * font-latex.el (font-latex-extend-region-backwards-quotation): Move the point back to the correct position when the inner loop search fails. * tests/latex/font-latex-test.el (font-latex-extend-region-backwards-quotation): New test. --- font-latex.el | 67 ++++++++++++++++++++++++------------------ tests/latex/font-latex-test.el | 40 +++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 29 deletions(-) diff --git a/font-latex.el b/font-latex.el index 09a6feb..6ce901f 100644 --- a/font-latex.el +++ b/font-latex.el @@ -2050,12 +2050,13 @@ set to french, and >>german<< (and 8-bit) are used if set to german." "Extend region backwards for quotations." (when font-latex-quotes (font-latex-update-quote-list) - (let ((regexp-end (regexp-opt (mapcar 'cadr font-latex-quote-list) t))) + (let ((regexp-end (regexp-opt (mapcar #'cadr font-latex-quote-list) t))) (save-excursion (goto-char font-lock-end) (catch 'extend (while (re-search-backward regexp-end font-lock-beg t) - (let ((closing-quote (match-string 0)) + (let ((found-end (match-beginning 0)) + (closing-quote (match-string-no-properties 0)) (nest-count 0) (point-of-surrender (- font-lock-beg font-latex-multiline-boundary)) @@ -2066,33 +2067,41 @@ set to french, and >>german<< (and 8-bit) are used if set to german." (setq opening-quote (car elt)) (throw 'found nil)))) ;; Find opening quote taking nested quotes into account. - (while (and (re-search-backward (concat opening-quote "\\|" - closing-quote) - point-of-surrender t) - ;; Found quotes before point-of-surrender. - (cond ((string= (match-string 0) closing-quote) - ;; Encountered another closing quote. - ;; Increase nest-count and continue - ;; the inner loop. - (setq nest-count (1+ nest-count))) - ;; Found an opening quote. - ((/= nest-count 0) - ;; If in nest, decrease nest-count - ;; and continue the inner loop. - (setq nest-count (1- nest-count))) - ;; Else we arrived at the opening quote - ;; matching with the closing quote found - ;; in the outer loop. - ((< (point) font-lock-beg) - ;; If that opening quote locates - ;; before `font-lock-beg', break the - ;; outer loop and extend the region. - (setq font-lock-beg (point)) - (throw 'extend t)) - (t - ;; Else terminate the inner loop and - ;; continue the outer loop. - nil))))))))))) + (while (if (re-search-backward (concat opening-quote "\\|" + closing-quote) + point-of-surrender 'move) + ;; Found quotes before point-of-surrender. + (cond ((string= (match-string-no-properties 0) + closing-quote) + ;; Encountered another closing quote. + ;; Increase nest-count and continue + ;; the inner loop. + (setq nest-count (1+ nest-count))) + ;; Found an opening quote. + ((/= nest-count 0) + ;; If in nest, decrease nest-count + ;; and continue the inner loop. + (setq nest-count (1- nest-count))) + ;; Else we arrived at the opening quote + ;; matching with the closing quote found + ;; in the outer loop. + ((< (point) font-lock-beg) + ;; If that opening quote locates + ;; before `font-lock-beg', break the + ;; outer loop and extend the region. + (setq font-lock-beg (point)) + (throw 'extend t)) + (t + ;; Else terminate the inner loop and + ;; continue the outer loop. + nil)) + ;; Didn't find quotes before + ;; point-of-surrender. + ;; Go back just before the closing quote, + ;; terminate the inner loop and + ;; continue the outer loop. + (goto-char found-end) + nil))))))))) (defun font-latex-match-script (limit) "Match subscript and superscript patterns up to LIMIT." diff --git a/tests/latex/font-latex-test.el b/tests/latex/font-latex-test.el index c45a7b5..a869fe4 100644 --- a/tests/latex/font-latex-test.el +++ b/tests/latex/font-latex-test.el @@ -41,4 +41,44 @@ $a$") (setq font-latex--updated-region-end (point-max)) (font-latex-match-dollar-math (point-max)))))) +(ert-deftest font-latex-extend-region-backwards-quotation () + "Test f-l-e-r-b-q doesn't extend region too eagerly." + (with-temp-buffer + (let ((TeX-install-font-lock 'font-latex-setup) + (font-latex-quotes 'french) + font-lock-beg font-lock-end) + (LaTeX-mode) + + ;; Test 1: Double prime in math expression doesn't cause region + ;; extension. + (setq font-lock-beg (point)) + (insert "$f''(x)=x^{3}$") + (setq font-lock-end (point)) + (should-not (font-latex-extend-region-backwards-quotation)) + + (erase-buffer) + (insert "abc ``def ghi'' jkl ") + (setq font-lock-beg (point)) + (insert "$f''(x)=x^{3}$") + (setq font-lock-end (point)) + (should-not (font-latex-extend-region-backwards-quotation)) + + ;; Test 2: open-close pair before '' in math expression is + ;; picked up. + (erase-buffer) + (insert "abc ``def ") + (setq font-lock-beg (point)) + (insert "ghi'' jkl $f''(x)=x^{3}$") + (setq font-lock-end (point)) + (should (font-latex-extend-region-backwards-quotation)) + (should (= font-lock-beg 5)) + + (erase-buffer) + (insert "abc <<def ") + (setq font-lock-beg (point)) + (insert "ghi>> jkl $f''(x)=x^{3}$") + (setq font-lock-end (point)) + (should (font-latex-extend-region-backwards-quotation)) + (should (= font-lock-beg 5))))) + ;;; font-latex-test.el ends here