Subject: bug#33043: [PATCH v1 1/1] Support for the truncation of displayed URLs

From: "Basil L. Contovounesios" <[email-masked]>
Date: Sun, 28 Oct 2018 03:14:39 +0000

>From 76dc7ed5d5b9c6273e99a2659ba304e9e7eb8e03 Mon Sep 17 00:00:00 2001
From: David Edmondson <[email-masked]>
Date: Sun, 28 Oct 2018 03:11:21 +0000
Subject: [PATCH 1/2] Add URL truncation support to rcirc (bug#33043)

* lisp/net/rcirc.el (rcirc-url-max-length): New user option
controlling extent of URL truncation, defaulting to none.
(rcirc-markup-urls): Use it.
---
 lisp/net/rcirc.el | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/lisp/net/rcirc.el b/lisp/net/rcirc.el
index fe9c71a21c..50e4d8ffb2 100644
--- a/lisp/net/rcirc.el
+++ b/lisp/net/rcirc.el
@@ -168,6 +168,14 @@ rcirc-fill-prefix
 		 (string :tag "Prefix text"))
   :group 'rcirc)
 
+(defcustom rcirc-url-max-length nil
+  "Maximum number of characters in displayed URLs.
+If nil, no maximum is applied."
+  :version "27.1"
+  :type '(choice (const :tag "No maximum" nil)
+                 (integer :tag "Number of characters"))
+  :group 'rcirc)
+
 (defvar rcirc-ignore-buffer-activity-flag nil
   "If non-nil, ignore activity in this buffer.")
 (make-variable-buffer-local 'rcirc-ignore-buffer-activity-flag)
@@ -2491,6 +2499,16 @@ rcirc-markup-urls
            (end (match-end 0))
            (url (match-string-no-properties 0))
            (link-text (buffer-substring-no-properties start end)))
+      ;; Truncate the visible part of URLs if required and necessary.
+      (when (and rcirc-url-max-length
+                 (> (- end start) rcirc-url-max-length))
+        (let* ((ellipsis "...")
+               (new-end (- (+ start rcirc-url-max-length)
+                           (length ellipsis))))
+          (delete-region new-end end)
+          (goto-char new-end)
+          (insert ellipsis)
+          (setq end (point))))
       ;; Add a button for the URL.  Note that we use `make-text-button',
       ;; rather than `make-button', as text-buttons are much faster in
       ;; large buffers.
-- 
2.19.1

>From 8d7f03b12ccb07cd9ffe953b99d519e289b61df9 Mon Sep 17 00:00:00 2001
From: "Basil L. Contovounesios" <[email-masked]>
Date: Sun, 28 Oct 2018 02:51:18 +0000
Subject: [PATCH 2/2] Reuse URL truncation function in rcirc (bug#33043)

* lisp/net/rcirc.el (rcirc-markup-urls): Simplify using
url-truncate-url-for-viewing.
---
 lisp/net/rcirc.el | 28 ++++++++++------------------
 1 file changed, 10 insertions(+), 18 deletions(-)

diff --git a/lisp/net/rcirc.el b/lisp/net/rcirc.el
index 50e4d8ffb2..ca707341be 100644
--- a/lisp/net/rcirc.el
+++ b/lisp/net/rcirc.el
@@ -2493,34 +2493,26 @@ rcirc-markup-my-nick
 	(rcirc-record-activity (current-buffer) 'nick)))))
 
 (defun rcirc-markup-urls (_sender _response)
-  (while (and rcirc-url-regexp ;; nil means disable URL catching
+  (while (and rcirc-url-regexp ; nil means disable URL catching.
               (re-search-forward rcirc-url-regexp nil t))
     (let* ((start (match-beginning 0))
-           (end (match-end 0))
-           (url (match-string-no-properties 0))
-           (link-text (buffer-substring-no-properties start end)))
-      ;; Truncate the visible part of URLs if required and necessary.
-      (when (and rcirc-url-max-length
-                 (> (- end start) rcirc-url-max-length))
-        (let* ((ellipsis "...")
-               (new-end (- (+ start rcirc-url-max-length)
-                           (length ellipsis))))
-          (delete-region new-end end)
-          (goto-char new-end)
-          (insert ellipsis)
-          (setq end (point))))
+           (url   (buffer-substring-no-properties start (point))))
+      (when rcirc-url-max-length
+        ;; Replace match with truncated URL.
+        (delete-region start (point))
+        (insert (url-truncate-url-for-viewing url rcirc-url-max-length)))
       ;; Add a button for the URL.  Note that we use `make-text-button',
       ;; rather than `make-button', as text-buttons are much faster in
       ;; large buffers.
-      (make-text-button start end
+      (make-text-button start (point)
 			'face 'rcirc-url
 			'follow-link t
 			'rcirc-url url
 			'action (lambda (button)
 				  (browse-url (button-get button 'rcirc-url))))
-      ;; record the url if it is not already the latest stored url
-      (when (not (string= link-text (caar rcirc-urls)))
-        (push (cons link-text start) rcirc-urls)))))
+      ;; Record the URL if it is not already the latest stored URL.
+      (unless (string= url (caar rcirc-urls))
+        (push (cons url start) rcirc-urls)))))
 
 (defun rcirc-markup-keywords (sender response)
   (when (and (string= response "PRIVMSG")
-- 
2.19.1

Eli Zaretskii <[email-masked]> writes:

>> From: "Basil L. Contovounesios" <[email-masked]>
>> Date: Thu, 18 Oct 2018 11:38:04 +0100
>> Cc: [email-masked]
>> 
>> > I didn't know about url-truncate-url-for-viewing, no other reason.
>> >
>> > I'd be happy with your change, but note that it will always truncate
>> > URLs that are wider than the current frame - there is no way to have no
>> > truncation take place.
>> >
>> > If a user is most familiar with something like ffap to open URLs then
>> > this is unfortunate - ffap doesn't see the full URL in the buffer.
>> 
>> Sorry, that was an oversight.  Here's how the last patch can be patched
>> to support the default of no truncation:
>
> If you two are in agreement wrt what should be installed, I'd need a
> full changeset from Basil to push.

I attach two patches.  The first comprises David's original patch, but
with an added :version tag and reworded commit message.  The second
switches to reusing url-truncate-url-for-viewing.  WDYT?

Thanks,

-- 
Basil