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

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

bug#24627: 24.5; (thing-at-point 'list) may return a non-empty string wi


From: Tino Calancha
Subject: bug#24627: 24.5; (thing-at-point 'list) may return a non-empty string without a list
Date: Sat, 15 Oct 2016 18:44:33 +0900
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux)

Andreas Röhler <andreas.roehler@easy-emacs.de> writes:

> On 13.10.2016 10:50, Tino Calancha wrote:
>> Andreas Röhler <andreas.roehler@easy-emacs.de> writes:
>>
>>> beg still needs a check like
>>>
>>> (not (nth 8 (parse-partial-sexp (point-min) (point))))
>>>
>>> otherwise it could match inside a string or comment
>> I have the feeling that this should return the local list
>> at point, even if inside a string or comment.
>
> Yes, but that would be reported by pps. However, when point is at
> opening delimiter, this is not recognised by pps. Then we must be sure
> not being inside a string or comment, where an opening delimiter is
> meaningless, i.e. just a literal.
>
> IMO all needed is  something like
>
> (beg (or (nth 1 (parse-partial-sexp...))
>
>          (and (eq 4 (car (syntax-after (point))))
>               (not (nth 8 (parse-partial-sexp...))
>               (point)))))
>      
>
> Should both fail, there is not list at point.
Thank you.  I think i got what you mean.
I need to invert the order of the above `or':
(nth 1 (parse-partial-sexp...))
need to appear the second.  Otherwise,
(with-temp-buffer
  (insert "(foo (a b) bar)")
  (goto-char 6)
  (list-at-point))

will return:
(foo (a b) bar)
instead of:
(a b)

The new patch pass following test:
(We might want to add this test into test/lisp/thingatpt-tests.el)

(ert-deftest list-at-point-tests ()
  "Test `list-at-point'."
  (let ((string-result '(("(a \"b\" c)" . (a "b" c))
                         (";(a \"b\" c)")
                         ("(a \"b\" c\n)" . (a "b" c))
                         ("\"(a b c)\"")
                         ("(a ;(b c d)\ne)" . (a e))
                         ("(foo\n(a ;(b c d)\ne) bar)" . (a e))
                         ("(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)"))
                         ("(defun foo ()\n  \"Test function.\"\n  ;;(a b)\n  
nil)" . (defun foo nil "Test function." nil)))))
    (dolist (str-res string-result)
      (with-temp-buffer
        (emacs-lisp-mode)
        (insert (car str-res))
        (re-search-backward "\\((a\\|^a\\)")
        (should (equal (list-at-point)
                       (cdr str-res)))))))


This is the new patch:
please, let me know if it's OK for you and feel free to suggest
additional tests.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>From 693aeed2a7251d23885ee53db9bf7026c7c1af3f Mon Sep 17 00:00:00 2001
From: Tino Calancha <tino.calancha@gmail.com>
Date: Sat, 15 Oct 2016 18:21:36 +0900
Subject: [PATCH] (thing-at-point 'list) return nil if no list at point

* lisp/thingatpt.el (thing-at-point-bounds-of-list-at-point):
Check first if we are at the beginning of a top-level sexp (Bug#24627).
If point is inside a comment or string, look for a list out of the
comment/string.
Escape '[' in doc string.
---
 lisp/thingatpt.el | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)

diff --git a/lisp/thingatpt.el b/lisp/thingatpt.el
index 6d1014b..e423630 100644
--- a/lisp/thingatpt.el
+++ b/lisp/thingatpt.el
@@ -219,22 +219,17 @@ 'beginning-of-sexp
 
 (defun thing-at-point-bounds-of-list-at-point ()
   "Return the bounds of the list at point.
-[Internal function used by `bounds-of-thing-at-point'.]"
+\[Internal function used by `bounds-of-thing-at-point'.]"
   (save-excursion
-    (let ((opoint (point))
-         (beg (ignore-errors
-                (up-list -1)
-                (point))))
-      (ignore-errors
-       (if beg
-           (progn (forward-sexp)
-                  (cons beg (point)))
-         ;; Are we are at the beginning of a top-level sexp?
-         (forward-sexp)
-         (let ((end (point)))
-           (backward-sexp)
-           (if (>= opoint (point))
-               (cons opoint end))))))))
+    (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))))))
 
 ;; Defuns
 
-- 
2.9.3

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

In GNU Emacs 26.0.50.4 (x86_64-pc-linux-gnu, GTK+ Version 3.22.1)
 of 2016-10-15
Repository revision: b0f1d23ec482aa71a0ae0251f6f44f4b8d261259





reply via email to

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