Subject: Re: bug#74132: 31.0.50; thing-at-pt, ffap and Github markdown

From: Madhu <[email-masked]>
Date: Thu, 20 Feb 2025 14:50:28 +0530

I'm including the fixed patch (hand edited with a test, the test
severely needs review, thanks

>From c12eba4ae2572049e8b3f7b0048a9c54f6c6c057 Mon Sep 17 00:00:00 2001
From: Madhu <[email-masked]>
Date: Thu, 31 Oct 2024 15:40:42 +0530
Subject: [PATCH] lisp/thingatpt.el: recognize urls better in markdown text

* lisp/thingatpt.el: (thing-at-point-bounds-of-url-at-point): supply a
LIM when calling (skip-chars-backward allowed-chars), which is the
position where `thing-at-point-beginning-of-url-regexp' matches
backwards

problematic url e.g.
```
[![GitHub Releases Downloads](https://img.shields.io/github/downloads/raysan5/raylib/total)](https://github.com/raysan5/raylib/releases)
```
If the point is in the the second url, skip-chars-backwards goes to the
space (between s and D) and `ffap-url-at-point' eventually fails.
but if we supply a limit with a left anchor, we work around it.

fix text to lisp/org/org-capture.el
---
 lisp/thingatpt.el | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/lisp/thingatpt.el b/lisp/thingatpt.el
index f2d917c6fc0..f813e91617c 100644
--- a/lisp/thingatpt.el
+++ b/lisp/thingatpt.el
@@ -502,9 +502,15 @@ thing-at-point-bounds-of-url-at-point
       (let* ((allowed-chars "--:=&?$+@-Z_[:alpha:]~#,%;*()!'[]")
 	     (skip-before "^[0-9a-zA-Z]")
 	     (skip-after  ":;.,!?'")
+             (hard-beg (and thing-at-point-beginning-of-url-regexp
+                            (save-excursion
+                              (and
+                               (re-search-backward
+                                thing-at-point-beginning-of-url-regexp nil t)
+                               (point)))))
 	     (pt (point))
 	     (beg (save-excursion
-		    (skip-chars-backward allowed-chars)
+		    (skip-chars-backward allowed-chars hard-beg)
 		    (skip-chars-forward skip-before pt)
 		    (point)))
 	     (end (save-excursion
diff --git a/test/lisp/thingatpt-tests.el b/test/lisp/thingatpt-tests.el
index e873141aa4e..5ee66c05738 100644
--- a/test/lisp/thingatpt-tests.el
+++ b/test/lisp/thingatpt-tests.el
@@ -347,4 +347,19 @@ consecutive-things-at-point
     (should (equal (thing-at-point 'url) "bar.com"))
     (should (equal (bounds-of-thing-at-point 'url) '(4 . 7)))))
 
+(ert-deftest thing-at-point-ffap-on-github-markdown-urls-tests ()
+  "Test ffap on GITHUB markdown url tests."
+  (let* ((thing-at-point-beginning-of-url-regexp "https?://")
+         (thing-at-point-default-mail-uri-scheme nil)
+         (link-text "GitHub Releases Downloads")
+         (url1 "https://img.shields.io/github/downloads/raysan5/raylib/total")
+         (url2 "https://github.com/raysan5/raylib/releases")
+         (url (format "[![%s](%s)](%s)" link-text url1 url2)))
+    (dolist (test (list (list url  85 url url1)
+                        (list url 113 url url2)))
+      (with-temp-buffer
+        (insert (nth 0 test))
+        (goto-char (nth 1 test))
+        (should (equal (thing-at-point-url-at-point) (nth 3 test)))))))
+
 ;;; thingatpt-tests.el ends here
-- 
2.46.0.27.gfa3b914457