Subject: bug#31772: 26.1; (thing-at-point 'list) regression

From: Leo Liu <[email-masked]>
Date: Thu, 06 Sep 2018 18:37:11 +0800

Hi there,

I have been using 26.1 as my main editor for the last few months and
this breakage remains a pain point in my day-to-day editing. For example
whenever I rewrite a function, I normally comment out the old one (to
keep the linter, pretty-printer or whatnot happy) and write the new one
from scratch, occasionally copy things from the old one to save typing
and this bug gets in the way many times a day. I propose a patch that
doesn't divert too much from the old and tried behaviour.

The idea that is currently in thing-at-point-bounds-of-list-at-point is
fine for a higher level function such as list-at-point but doing it
there affects all functions that build on it including some from
thingatpt.el itself.

I hope you can find time to review the patch and come to a solution for
26.2 which I very much look forward to.

Thanks,
Leo

diff --git a/lisp/thingatpt.el b/lisp/thingatpt.el
index 6a978fe9..8da31a03 100644
--- a/lisp/thingatpt.el
+++ b/lisp/thingatpt.el
@@ -221,15 +221,12 @@ The bounds of THING are determined by `bounds-of-thing-at-point'."
   "Return the bounds of the list at point.
 \[Internal function used by `bounds-of-thing-at-point'.]"
   (save-excursion
-    (let* ((st (parse-partial-sexp (point-min) (point)))
-           (beg (or (and (eq 4 (car (syntax-after (point))))
-                         (not (nth 8 st))
-                         (point))
-                    (nth 1 st))))
-      (when beg
-        (goto-char beg)
-        (forward-sexp)
-        (cons beg (point))))))
+    (if (ignore-errors (up-list -1))
+	(ignore-errors (cons (point) (progn (forward-sexp) (point))))
+      (let ((bound (bounds-of-thing-at-point 'sexp)))
+	(and bound
+	     (<= (car bound) (point)) (< (point) (cdr bound))
+	     bound)))))
 
 ;; Defuns
 
diff --git a/test/lisp/thingatpt-tests.el b/test/lisp/thingatpt-tests.el
index cfb57de6..6093c209 100644
--- a/test/lisp/thingatpt-tests.el
+++ b/test/lisp/thingatpt-tests.el
@@ -84,20 +84,18 @@ position to retrieve THING.")
       (goto-char (nth 1 test))
       (should (equal (thing-at-point (nth 2 test)) (nth 3 test))))))
 
-;; These tests reflect the actual behavior of
-;; `thing-at-point-bounds-of-list-at-point'.
-(ert-deftest thing-at-point-bug24627 ()
-  "Test for https://debbugs.gnu.org/24627 ."
+;; See bug#24627 and bug#31772.
+(ert-deftest thing-at-point-bounds-of-list-at-point ()
   (let ((string-result '(("(a \"b\" c)" . (a "b" c))
-                         (";(a \"b\" c)")
+                         (";(a \"b\" c)" . (a "b" c))
                          ("(a \"b\" c\n)" . (a "b" c))
-                         ("\"(a b c)\"")
+                         ("\"(a b c)\"" . (a b c))
                          ("(a ;(b c d)\ne)" . (a e))
-                         ("(foo\n(a ;(b c d)\ne) bar)" . (a e))
+                         ("(foo\n(a ;(b c d)\ne) bar)" . (foo (a e) bar))
                          ("(foo\na ;(b c d)\ne bar)" . (foo a e bar))
-                         ("(foo\n(a \"(b c d)\"\ne) bar)" . (a "(b c d)" e))
-                         ("(b\n(a ;(foo c d)\ne) bar)" . (a e))
-                         ("(princ \"(a b c)\")" . (princ "(a b c)"))
+                         ("(foo\n(a \"(b c d)\"\ne) bar)" . (foo (a "(b c d)" e) bar))
+                         ("(b\n(a ;(foo c d)\ne) bar)" . (b (a e) bar))
+                         ("(princ \"(a b c)\")" . (a b c))
                          ("(defun foo ()\n  \"Test function.\"\n  ;;(a b)\n  nil)" . (defun foo nil "Test function." nil))))
         (file
          (expand-file-name "lisp/thingatpt.el" source-directory))