unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#51176: eww switch buffer by title
@ 2021-10-13  4:56 Yuchen Pei
  2021-10-13 11:52 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 29+ messages in thread
From: Yuchen Pei @ 2021-10-13  4:56 UTC (permalink / raw)
  To: 51176

[-- Attachment #1: Type: text/plain, Size: 875 bytes --]

Hello,

How about switching to an eww buffer by title?

I have a hacky way to do it (see below).  I'll be happy to prepare 
a patch if people think this is a sensible feature to add.

(defun eww-switch-by-title (title-and-buffer)
  "Switches to an eww buffer with selected title."
  (interactive
   (list
    (let ((com-table))
      (dolist (buffer (buffer-list))
	(with-current-buffer buffer
	  (when (equal major-mode 'eww-mode)
	    (add-to-list
	     'com-table
	     (concat (plist-get eww-data :title)
		     (propertize (concat " " (buffer-name))
				 'invisible t))))))
      (completing-read "Eww buffer title: " com-table))))
  (string-match "^.* \\(.*\\)$" title-and-buffer)
  (switch-to-buffer (match-string 1 title-and-buffer)))


-- 
Best,
Yuchen

PGP Key: 47F9 D050 1E11 8879 9040  4941 2126 7E93 EF86 DFD0
           <https://ypei.me/assets/ypei-pubkey.txt>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 243 bytes --]

^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-13  4:56 bug#51176: eww switch buffer by title Yuchen Pei
@ 2021-10-13 11:52 ` Lars Ingebrigtsen
  2021-10-13 12:15   ` Yuchen Pei
  0 siblings, 1 reply; 29+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-13 11:52 UTC (permalink / raw)
  To: Yuchen Pei; +Cc: 51176

Yuchen Pei <hi@ypei.me> writes:

> How about switching to an eww buffer by title?

So the use case is that you have a large number of eww buffers, but you
want to find a particular based on the title of the web page being
displayed in that buffer?

I'm not sure that's generally useful enough to include -- do people use
`C-u M-x eww' a lot?

Some code comments:

> (defun eww-switch-by-title (title-and-buffer)
>  "Switches to an eww buffer with selected title."
>  (interactive
>   (list
>    (let ((com-table))
>      (dolist (buffer (buffer-list))
> 	(with-current-buffer buffer
> 	  (when (equal major-mode 'eww-mode)
> 	    (add-to-list
> 	     'com-table

Use `push'.

> 	     (concat (plist-get eww-data :title)
> 		     (propertize (concat " " (buffer-name))
> 				 'invisible t))))))
>      (completing-read "Eww buffer title: " com-table))))

Use an alist instead of propertising a string.

There may also be several buffers with the same title, I guess, in which
case this wouldn't allow you to choose either?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-13 11:52 ` Lars Ingebrigtsen
@ 2021-10-13 12:15   ` Yuchen Pei
  2021-10-13 12:54     ` Lars Ingebrigtsen
  0 siblings, 1 reply; 29+ messages in thread
From: Yuchen Pei @ 2021-10-13 12:15 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: 51176

[-- Attachment #1: Type: text/plain, Size: 1473 bytes --]


Lars Ingebrigtsen <larsi@gnus.org> writes:

> Yuchen Pei <hi@ypei.me> writes:
>
>> How about switching to an eww buffer by title?
>
> So the use case is that you have a large number of eww buffers, 
> but you
> want to find a particular based on the title of the web page 
> being
> displayed in that buffer?

Yes.  For me I'm just trying to use eww as my main browser.  For 
that to happen, I'd need to duplicate the tabs functionality in 
firefox, and tabs are identified by titles.

>
> I'm not sure that's generally useful enough to include -- do 
> people use
> `C-u M-x eww' a lot?

I don't know.  I assume people who use a graphical browser 
generally have more tabs open than I do (I have an addon in my 
firefox limiting the number of tabs to 15 by killing LRU tabs), 
but I don't know how many eww users use eww as their main browser 
and how many have migrated from graphical browsers to eww.

BTW I have a few functions with keybindings spawning new eww 
buffers in my init file.

>
> There may also be several buffers with the same title, I guess, 
> in which
> case this wouldn't allow you to choose either?

I think with icomplete it will show all of them in the minibuffer 
and you can choose (you can't tell which one is which though, same 
as graphical browser tabs).  But I haven't tested it without 
icomplete - will do.

-- 
Best,
Yuchen

PGP Key: 47F9 D050 1E11 8879 9040  4941 2126 7E93 EF86 DFD0
           <https://ypei.me/assets/ypei-pubkey.txt>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 243 bytes --]

^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-13 12:15   ` Yuchen Pei
@ 2021-10-13 12:54     ` Lars Ingebrigtsen
  2021-10-13 16:28       ` Juri Linkov
  0 siblings, 1 reply; 29+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-13 12:54 UTC (permalink / raw)
  To: Yuchen Pei; +Cc: 51176

Yuchen Pei <hi@ypei.me> writes:

>> I'm not sure that's generally useful enough to include -- do people
>> use
>> `C-u M-x eww' a lot?
>
> I don't know.  I assume people who use a graphical browser generally
> have more tabs open than I do (I have an addon in my firefox limiting
> the number of tabs to 15 by killing LRU tabs), but I don't know how
> many eww users use eww as their main browser and how many have
> migrated from graphical browsers to eww.

I think in that case, perhaps it would make more sense to just rename
the eww buffer to have the title in the buffer name?  Then the normal
buffer switching commands would work automatically.

So the change would be to offer an option that renames the buffer at the
end of...  `eww-display-html', perhaps?  

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-13 12:54     ` Lars Ingebrigtsen
@ 2021-10-13 16:28       ` Juri Linkov
  2021-10-13 16:47         ` Lars Ingebrigtsen
  0 siblings, 1 reply; 29+ messages in thread
From: Juri Linkov @ 2021-10-13 16:28 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: 51176, Yuchen Pei

>> I don't know.  I assume people who use a graphical browser generally
>> have more tabs open than I do (I have an addon in my firefox limiting
>> the number of tabs to 15 by killing LRU tabs), but I don't know how
>> many eww users use eww as their main browser and how many have
>> migrated from graphical browsers to eww.
>
> I think in that case, perhaps it would make more sense to just rename
> the eww buffer to have the title in the buffer name?  Then the normal
> buffer switching commands would work automatically.

In Info, forking commands (such as 'Info-follow-nearest-node FORK')
create a new buffer with the Info node name in its buffer name, e.g.
*info-TITLE*.  So instead of current *eww*<2>, eww could use the same
format *eww-TITLE*, or uniquify-like angle brackets *eww<TITLE>*.





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-13 16:28       ` Juri Linkov
@ 2021-10-13 16:47         ` Lars Ingebrigtsen
  2021-10-13 17:31           ` Juri Linkov
  0 siblings, 1 reply; 29+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-13 16:47 UTC (permalink / raw)
  To: Juri Linkov; +Cc: 51176, Yuchen Pei

Juri Linkov <juri@linkov.net> writes:

>> I think in that case, perhaps it would make more sense to just rename
>> the eww buffer to have the title in the buffer name?  Then the normal
>> buffer switching commands would work automatically.
>
> In Info, forking commands (such as 'Info-follow-nearest-node FORK')
> create a new buffer with the Info node name in its buffer name, e.g.
> *info-TITLE*.  So instead of current *eww*<2>, eww could use the same
> format *eww-TITLE*, or uniquify-like angle brackets *eww<TITLE>*.

Yup, that's what I meant.  The wrinkle here is that we don't know the
title until we've fetched the HTML, so the buffer has to be renamed
after rendering.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-13 16:47         ` Lars Ingebrigtsen
@ 2021-10-13 17:31           ` Juri Linkov
  2021-10-13 18:14             ` Lars Ingebrigtsen
  2021-10-13 23:48             ` Yuchen Pei
  0 siblings, 2 replies; 29+ messages in thread
From: Juri Linkov @ 2021-10-13 17:31 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: 51176, Yuchen Pei

>>> I think in that case, perhaps it would make more sense to just rename
>>> the eww buffer to have the title in the buffer name?  Then the normal
>>> buffer switching commands would work automatically.
>>
>> In Info, forking commands (such as 'Info-follow-nearest-node FORK')
>> create a new buffer with the Info node name in its buffer name, e.g.
>> *info-TITLE*.  So instead of current *eww*<2>, eww could use the same
>> format *eww-TITLE*, or uniquify-like angle brackets *eww<TITLE>*.
>
> Yup, that's what I meant.  The wrinkle here is that we don't know the
> title until we've fetched the HTML, so the buffer has to be renamed
> after rendering.

I see.  Then maybe in the existing hook `eww-after-render-hook'?  This is
the standard way to customize buffer renaming, e.g. I have a lot of such
(add-hook 'occur-hook 'occur-rename-buffer) for customizing different modes.





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-13 17:31           ` Juri Linkov
@ 2021-10-13 18:14             ` Lars Ingebrigtsen
  2021-10-13 20:31               ` Protesilaos Stavrou
  2021-10-13 23:48             ` Yuchen Pei
  1 sibling, 1 reply; 29+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-13 18:14 UTC (permalink / raw)
  To: Juri Linkov; +Cc: 51176, Yuchen Pei

Juri Linkov <juri@linkov.net> writes:

> I see.  Then maybe in the existing hook `eww-after-render-hook'?

Yup; I misread the code -- that looks like the correct place to put it.
But I can also see many people wanting this, so perhaps a user option to
rename the buffers this way would be nice.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-13 18:14             ` Lars Ingebrigtsen
@ 2021-10-13 20:31               ` Protesilaos Stavrou
  2021-10-13 20:33                 ` Lars Ingebrigtsen
                                   ` (2 more replies)
  0 siblings, 3 replies; 29+ messages in thread
From: Protesilaos Stavrou @ 2021-10-13 20:31 UTC (permalink / raw)
  To: Lars Ingebrigtsen, Juri Linkov; +Cc: 51176, Yuchen Pei

On 2021-10-13, 20:14 +0200, Lars Ingebrigtsen <larsi@gnus.org> wrote:

> Juri Linkov <juri@linkov.net> writes:
>
>> I see.  Then maybe in the existing hook `eww-after-render-hook'?
>
> Yup; I misread the code -- that looks like the correct place to put it.
> But I can also see many people wanting this, so perhaps a user option to
> rename the buffers this way would be nice.

That is right.  I am preparing together with another person a patch that
auto-renames eww buffers after they are rendered.  The idea is to use
either the page title or, if that is not available, the page's URL.

In terms of user options, there would be a toggle to enable the
behaviour (off by default to not upset existing users) as well as a
defcustom with ":type 'function", so users can specify exactly how the
renaming scheme should work.

I expect the code to be finalised within the coming days, in case you
have not done the work already.

-- 
Protesilaos Stavrou
https://protesilaos.com





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-13 20:31               ` Protesilaos Stavrou
@ 2021-10-13 20:33                 ` Lars Ingebrigtsen
  2021-10-15  6:31                   ` Protesilaos Stavrou
  2021-10-13 21:26                 ` Yuchen Pei
  2021-10-14  0:16                 ` bug#51176: [External] : " Drew Adams
  2 siblings, 1 reply; 29+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-13 20:33 UTC (permalink / raw)
  To: Protesilaos Stavrou; +Cc: 51176, Yuchen Pei, Juri Linkov

Protesilaos Stavrou <info@protesilaos.com> writes:

> In terms of user options, there would be a toggle to enable the
> behaviour (off by default to not upset existing users) as well as a
> defcustom with ":type 'function", so users can specify exactly how the
> renaming scheme should work.

Sounds good to me.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-13 20:31               ` Protesilaos Stavrou
  2021-10-13 20:33                 ` Lars Ingebrigtsen
@ 2021-10-13 21:26                 ` Yuchen Pei
  2021-10-14  0:16                 ` bug#51176: [External] : " Drew Adams
  2 siblings, 0 replies; 29+ messages in thread
From: Yuchen Pei @ 2021-10-13 21:26 UTC (permalink / raw)
  To: Protesilaos Stavrou, Lars Ingebrigtsen, Juri Linkov; +Cc: 51176

On 14 October 2021 07:31:22 GMT+11:00, Protesilaos Stavrou <info@protesilaos.com> wrote:
>On 2021-10-13, 20:14 +0200, Lars Ingebrigtsen <larsi@gnus.org> wrote:
>
>> Juri Linkov <juri@linkov.net> writes:
>>
>>> I see.  Then maybe in the existing hook `eww-after-render-hook'?
>>
>> Yup; I misread the code -- that looks like the correct place to put it.
>> But I can also see many people wanting this, so perhaps a user option to
>> rename the buffers this way would be nice.
>
>That is right.  I am preparing together with another person a patch that
>auto-renames eww buffers after they are rendered.  The idea is to use
>either the page title or, if that is not available, the page's URL.
>
>In terms of user options, there would be a toggle to enable the
>behaviour (off by default to not upset existing users) as well as a
>defcustom with ":type 'function", so users can specify exactly how the
>renaming scheme should work.
>
>I expect the code to be finalised within the coming days, in case you
>have not done the work already.
>

Sgtm and looking forward :)






^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-13 17:31           ` Juri Linkov
  2021-10-13 18:14             ` Lars Ingebrigtsen
@ 2021-10-13 23:48             ` Yuchen Pei
  2021-10-14 11:06               ` Lars Ingebrigtsen
  2021-10-14 16:05               ` Juri Linkov
  1 sibling, 2 replies; 29+ messages in thread
From: Yuchen Pei @ 2021-10-13 23:48 UTC (permalink / raw)
  To: Juri Linkov; +Cc: Lars Ingebrigtsen, 51176

[-- Attachment #1: Type: text/plain, Size: 2252 bytes --]


Juri Linkov <juri@linkov.net> writes:

>>>> I think in that case, perhaps it would make more sense to 
>>>> just rename
>>>> the eww buffer to have the title in the buffer name?  Then 
>>>> the normal
>>>> buffer switching commands would work automatically.
>>>
>>> In Info, forking commands (such as 'Info-follow-nearest-node 
>>> FORK')
>>> create a new buffer with the Info node name in its buffer 
>>> name, e.g.
>>> *info-TITLE*.  So instead of current *eww*<2>, eww could use 
>>> the same
>>> format *eww-TITLE*, or uniquify-like angle brackets 
>>> *eww<TITLE>*.
>>
>> Yup, that's what I meant.  The wrinkle here is that we don't 
>> know the
>> title until we've fetched the HTML, so the buffer has to be 
>> renamed
>> after rendering.
>
> I see.  Then maybe in the existing hook `eww-after-render-hook'? 
> This is
> the standard way to customize buffer renaming, e.g. I have a lot 
> of such
> (add-hook 'occur-hook 'occur-rename-buffer) for customizing 
> different modes.

On a related note, I also have a function defined in a similar 
fashion switching to a shell buffer by prompts.  Are people 
interested in having this as well?  I don't see a shell hook like 
eww-after-render-hook that can achieve this though.

(defun get-comint-prompt ()
  (save-excursion
    ;; more useful than (beginning-of-line) to move past sticky 
    part
    (forward-line 0)
    (let ((end
	   ;; font-lock-face should be comint-highlight-prompt
	   (next-single-char-property-change (point) 
	   'font-lock-face)))
      (buffer-substring-no-properties (point) end))))

(defun shell-switch-by-prompt (prompt-and-buffer)
  "Switches to a shell with selected prompt."
  (interactive
   (list
    (let ((com-table))
      (dolist (buffer (buffer-list))
	(with-current-buffer buffer
	  (when (equal major-mode 'shell-mode)
	    (add-to-list
	     'com-table
	     (concat (get-comint-prompt)
		     (propertize (concat " " (buffer-name))
				 'invisible t))))))
      (completing-read "Shell prompt: " com-table))))
  (string-match "^.* \\(.*\\)$" prompt-and-buffer)
  (switch-to-buffer (match-string 1 prompt-and-buffer)))


-- 
Best,
Yuchen

PGP Key: 47F9 D050 1E11 8879 9040  4941 2126 7E93 EF86 DFD0
           <https://ypei.me/assets/ypei-pubkey.txt>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 243 bytes --]

^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: [External] : bug#51176: eww switch buffer by title
  2021-10-13 20:31               ` Protesilaos Stavrou
  2021-10-13 20:33                 ` Lars Ingebrigtsen
  2021-10-13 21:26                 ` Yuchen Pei
@ 2021-10-14  0:16                 ` Drew Adams
  2 siblings, 0 replies; 29+ messages in thread
From: Drew Adams @ 2021-10-14  0:16 UTC (permalink / raw)
  To: Protesilaos Stavrou, Lars Ingebrigtsen, Juri Linkov
  Cc: 51176@debbugs.gnu.org, Yuchen Pei

> >> I see.  Then maybe in the existing hook `eww-after-render-hook'?
> >
> > Yup; I misread the code -- that looks like the correct place to put it.
> > But I can also see many people wanting this, so perhaps a user option to
> > rename the buffers this way would be nice.
> 
> That is right.  I am preparing together with another person a patch
> that auto-renames eww buffers after they are rendered.  The idea is to use
> either the page title or, if that is not available, the page's URL.
> 
> In terms of user options, there would be a toggle to enable the
> behaviour (off by default to not upset existing users) as well as a
> defcustom with ":type 'function", so users can specify exactly how the
> renaming scheme should work.
> 
> I expect the code to be finalised within the coming days, in case you
> have not done the work already.

FWIW, Bookmark+ allows these kinds of automatic renaming:

(defcustom bmkp-eww-buffer-renaming nil
  "Whether and how an EWW buffer is renamed.
Non-nil values affect EWW behavior even when bookmarks are not used.

* nil:    Do not rename buffer - use `*eww*' (vanilla EWW behavior).
* `url':  Rename buffer to web-page title plus last 20 chars of URL.
* `page': Rename buffer to web-page title (only)."
  :type '(choice
          (const :tag "Do not rename buffer (use name `*eww*')"                    nil)
          (const :tag "Rename buffer to web-page title plus last 20 chars of URL"  url)
          ;; Any symbol other than `page' and nil is treated the same as `page'.
          (const :tag "Rename buffer to web-page title"                            page))
  :group 'bookmark-plus)

The code (e.g. for the after-render hook function)
is in file `bookmark+-1.el'.

https://www.emacswiki.org/emacs/BookmarkPlus





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-13 23:48             ` Yuchen Pei
@ 2021-10-14 11:06               ` Lars Ingebrigtsen
  2021-10-14 16:05               ` Juri Linkov
  1 sibling, 0 replies; 29+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-14 11:06 UTC (permalink / raw)
  To: Yuchen Pei; +Cc: 51176, Juri Linkov

Yuchen Pei <hi@ypei.me> writes:

> On a related note, I also have a function defined in a similar fashion
> switching to a shell buffer by prompts.  Are people interested in
> having this as well?  I don't see a shell hook like
> eww-after-render-hook that can achieve this though.

Perhaps you can do something with after-change-functions in comint
buffers -- comint-last-prompt has the location of the prompt.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-13 23:48             ` Yuchen Pei
  2021-10-14 11:06               ` Lars Ingebrigtsen
@ 2021-10-14 16:05               ` Juri Linkov
  1 sibling, 0 replies; 29+ messages in thread
From: Juri Linkov @ 2021-10-14 16:05 UTC (permalink / raw)
  To: Yuchen Pei; +Cc: Lars Ingebrigtsen, 51176

> On a related note, I also have a function defined in a similar fashion
> switching to a shell buffer by prompts.  Are people interested in having
> this as well?  I don't see a shell hook like eww-after-render-hook that can
> achieve this though.

project-shell uses project-prefixed-buffer-name to generate
project-specific buffer names, but not with shell prompts.





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-13 20:33                 ` Lars Ingebrigtsen
@ 2021-10-15  6:31                   ` Protesilaos Stavrou
  2021-10-15  8:32                     ` miha--- via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2021-10-15 10:35                     ` Lars Ingebrigtsen
  0 siblings, 2 replies; 29+ messages in thread
From: Protesilaos Stavrou @ 2021-10-15  6:31 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: 51176, Yuchen Pei, Abhiseck Paira, Juri Linkov

[-- Attachment #1: Type: text/plain, Size: 764 bytes --]

On 2021-10-13, 22:33 +0200, Lars Ingebrigtsen <larsi@gnus.org> wrote:

> Protesilaos Stavrou <info@protesilaos.com> writes:
>
>> In terms of user options, there would be a toggle to enable the
>> behaviour (off by default to not upset existing users) as well as a
>> defcustom with ":type 'function", so users can specify exactly how the
>> renaming scheme should work.
>
> Sounds good to me.

Hello again!

Please find attached the patch which implements a renaming scheme for
EWW buffers.  I include Abhiseck, the co-author of this patch, in Cc.

What do you think?

We had entertained the notion of a single defcustom with more options,
but ultimately decided against it on the grounds that it was more
complex.

-- 
Protesilaos Stavrou
https://protesilaos.com

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Implement-auto-renaming-scheme-for-eww-buffers.patch --]
[-- Type: text/x-patch, Size: 3580 bytes --]

From add8dabc4f3f33707f069865794ec53b2e0e4037 Mon Sep 17 00:00:00 2001
Message-Id: <add8dabc4f3f33707f069865794ec53b2e0e4037.1634279302.git.info@protesilaos.com>
From: Protesilaos Stavrou <info@protesilaos.com>
Date: Fri, 15 Oct 2021 09:24:04 +0300
Subject: [PATCH] Implement auto-renaming scheme for eww buffers

* etc/NEWS: Document the new feature.

* lisp/net/eww.el (eww-auto-rename-buffer-function): Introduce user
option to specify the function which performs the renaming.
(eww-auto-rename-buffer-flag): Add toggle to control the behaviour,
keeping it off by default in order not to surprise existing users.
(eww--rename-buffer): Write function to rename buffers.
(eww-render): Call 'eww--rename-buffer' at the post-render phase, if
appropriate.

Fix bug#51176.

Co-authored-by: Abhiseck Paira <abhiseckpaira@disroot.org>
Co-authored-by: Protesilaos Stavrou <info@protesilaos.com>
---
 etc/NEWS        | 11 +++++++++++
 lisp/net/eww.el | 26 ++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index 7dd4d14274..54d554ee0b 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -89,6 +89,17 @@ Customize this option to limit the amount of entries in the menu
 \f
 * Changes in Specialized Modes and Packages in Emacs 29.1
 
+** eww
+
+---
+*** New user option to automatically rename EWW buffers
+When 'eww-auto-rename-buffer-flag' is set to a non-nil value, all
+rendered web pages will have their buffer renamed from the generic
+"*eww*" to a name that includes the web page's title or, if that is
+absent, its URL.  The renaming scheme is controlled by the user option
+'eww-auto-rename-buffer-function'.  By default, no automatic renaming
+is performed.
+
 ** image-dired
 
 ---
diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 24c6335210..2104d09696 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -178,6 +178,21 @@ (defcustom eww-after-render-hook nil
   :group 'eww
   :type 'hook)
 
+(defcustom eww-auto-rename-buffer-function #'eww--rename-buffer
+  "Function to rename EWW buffer.
+The function is called just before the `eww-after-render-hook' if
+`eww-auto-rename-buffer-flag' is non-nil."
+  :version "29.1"
+  :group 'eww
+  :type 'function)
+
+(defcustom eww-auto-rename-buffer-flag nil
+  "When non-nil rename EWW buffers after they are rendered.
+Renaming is controlled by `eww-auto-rename-buffer-function'."
+  :version "29.1"
+  :group 'eww
+  :type 'boolean)
+
 (defcustom eww-form-checkbox-selected-symbol "[X]"
   "Symbol used to represent a selected checkbox.
 See also `eww-form-checkbox-symbol'."
@@ -502,6 +517,15 @@ (defun eww-html-p (content-type)
   (member content-type '("text/html"
 			 "application/xhtml+xml")))
 
+(defun eww--rename-buffer ()
+  "Rename the current EWW buffer.
+Use the page's title or URL as an identifier."
+  (when (eq major-mode 'eww-mode)
+    (when-let ((title (or (plist-get eww-data :title)
+                          (plist-get eww-data :url))))
+      (rename-buffer
+       (format "*%s # eww*" (truncate-string-to-width title 40)) t))))
+
 (defun eww-render (status url &optional point buffer encode)
   (let* ((headers (eww-parse-headers))
 	 (content-type
@@ -556,6 +580,8 @@ (defun eww-render (status url &optional point buffer encode)
 	    (setq eww-history-position 0)
 	    (and last-coding-system-used
 		 (set-buffer-file-coding-system last-coding-system-used))
+            (and eww-auto-rename-buffer-flag
+                 (funcall eww-auto-rename-buffer-function))
 	    (run-hooks 'eww-after-render-hook)))
       (kill-buffer data-buffer))))
 
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-15  6:31                   ` Protesilaos Stavrou
@ 2021-10-15  8:32                     ` miha--- via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2021-10-15 10:35                     ` Lars Ingebrigtsen
  1 sibling, 0 replies; 29+ messages in thread
From: miha--- via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2021-10-15  8:32 UTC (permalink / raw)
  To: Protesilaos Stavrou, Lars Ingebrigtsen
  Cc: 51176, Yuchen Pei, Abhiseck Paira, Juri Linkov

[-- Attachment #1: Type: text/plain, Size: 939 bytes --]

Protesilaos Stavrou <info@protesilaos.com> writes:

> Hello again!
>
> Please find attached the patch which implements a renaming scheme for
> EWW buffers.

Thanks.

> I include Abhiseck, the co-author of this patch, in Cc.
>
> What do you think?
>
> We had entertained the notion of a single defcustom with more options,
> but ultimately decided against it on the grounds that it was more
> complex.
>
> -- 
> Protesilaos Stavrou
> https://protesilaos.com
> @@ -556,6 +580,8 @@ (defun eww-render (status url &optional point buffer encode)
> +            (and eww-auto-rename-buffer-flag
> +                 (funcall eww-auto-rename-buffer-function))
>  	    (run-hooks 'eww-after-render-hook)))

With your patch, the buffer isn't renamed if the user presses "l" or "r"
to go backward and forward in history. I suggest calling your rename
function from 'eww-update-header-line-format' instead.

Best regards.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 861 bytes --]

^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-15  6:31                   ` Protesilaos Stavrou
  2021-10-15  8:32                     ` miha--- via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2021-10-15 10:35                     ` Lars Ingebrigtsen
  2021-10-15 11:22                       ` Protesilaos Stavrou
  1 sibling, 1 reply; 29+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-15 10:35 UTC (permalink / raw)
  To: Protesilaos Stavrou; +Cc: 51176, Yuchen Pei, Abhiseck Paira, Juri Linkov

Protesilaos Stavrou <info@protesilaos.com> writes:

> Please find attached the patch which implements a renaming scheme for
> EWW buffers.  I include Abhiseck, the co-author of this patch, in Cc.
>
> What do you think?
>
> We had entertained the notion of a single defcustom with more options,
> but ultimately decided against it on the grounds that it was more
> complex.

Yeah, looks good to me, but:

miha@kamnitnik.top writes:

> With your patch, the buffer isn't renamed if the user presses "l" or "r"
> to go backward and forward in history. I suggest calling your rename
> function from 'eww-update-header-line-format' instead.

Yes, it has to be performed upon all updates.  But putting it in that
function would be misleading, so what about creating a new function
`eww--after-page-change' that would call 'eww-update-header-line-format'
and

            (when eww-auto-rename-buffer-flag
              (funcall eww-auto-rename-buffer-function))

and call that where 'eww-update-header-line-format' is called now.

And we don't really call user options -flag, so rename it to just
`eww-auto-rename-buffer'. 

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-15 10:35                     ` Lars Ingebrigtsen
@ 2021-10-15 11:22                       ` Protesilaos Stavrou
  2021-10-15 12:13                         ` Lars Ingebrigtsen
  0 siblings, 1 reply; 29+ messages in thread
From: Protesilaos Stavrou @ 2021-10-15 11:22 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: 51176, Yuchen Pei, Abhiseck Paira, Juri Linkov

[-- Attachment #1: Type: text/plain, Size: 1735 bytes --]

On 2021-10-15, 12:35 +0200, Lars Ingebrigtsen <larsi@gnus.org> wrote:

> Protesilaos Stavrou <info@protesilaos.com> writes:
>
>> Please find attached the patch which implements a renaming scheme for
>> EWW buffers.  I include Abhiseck, the co-author of this patch, in Cc.
>>
>> What do you think?
>>
>> We had entertained the notion of a single defcustom with more options,
>> but ultimately decided against it on the grounds that it was more
>> complex.
>
> Yeah, looks good to me, but:
>
> miha@kamnitnik.top writes:
>
>> With your patch, the buffer isn't renamed if the user presses "l" or "r"
>> to go backward and forward in history. I suggest calling your rename
>> function from 'eww-update-header-line-format' instead.
>
> Yes, it has to be performed upon all updates.  But putting it in that
> function would be misleading, so what about creating a new function
> `eww--after-page-change' that would call 'eww-update-header-line-format'
> and
>
>             (when eww-auto-rename-buffer-flag
>               (funcall eww-auto-rename-buffer-function))
>
> and call that where 'eww-update-header-line-format' is called now.
>
> And we don't really call user options -flag, so rename it to just
> `eww-auto-rename-buffer'. 

Thanks for the feedback!  Please find attached the revised patch, based
on what you suggested:

    0001-Implement-auto-renaming-scheme-for-EWW-buffers-simple.patch

Because it may be useful for your consideration, there also exists the
more complex approach that I alluded to earlier ("complex" may be an
exaggeration, but still).  Attached as well and also based on your
feedback.

    0001-Implement-auto-renaming-scheme-for-EWW-buffers-complex.patch

-- 
Protesilaos Stavrou
https://protesilaos.com

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Implement-auto-renaming-scheme-for-EWW-buffers-complex.patch --]
[-- Type: text/x-patch, Size: 6321 bytes --]

From 9b1c46c18c113952f2274fab9c334140d5c071e3 Mon Sep 17 00:00:00 2001
Message-Id: <9b1c46c18c113952f2274fab9c334140d5c071e3.1634295931.git.info@protesilaos.com>
From: Protesilaos Stavrou <info@protesilaos.com>
Date: Fri, 15 Oct 2021 14:05:01 +0300
Subject: [PATCH] Implement auto-renaming scheme for EWW buffers

* etc/NEWS: Document the new user options.

* lisp/net/eww.el (eww-auto-rename-buffer, eww-buffer-name-length):
Add new user options.
(eww--rename-buffer): Introduce new function that performs the
renaming of buffers.
(eww--after-page-change): Add new wrapper function which calls
'eww-update-header-line-format' and 'eww--rename-buffer'.
(eww, eww-render, eww-tag-title, eww-readable, eww-restore-history):
Include eww--after-page-change.

Fix bug#51176.

Co-authored-by: Abhiseck Paira <abhiseckpaira@disroot.org>
Co-authored-by: Protesilaos Stavrou <info@protesilaos.com>
---
 etc/NEWS        | 11 +++++++++
 lisp/net/eww.el | 65 +++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 71 insertions(+), 5 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 7dd4d14274..3300ac157d 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -89,6 +89,17 @@ Customize this option to limit the amount of entries in the menu
 \f
 * Changes in Specialized Modes and Packages in Emacs 29.1
 
+** eww
+
+---
+*** New user option to automatically rename EWW buffers
+The 'eww-auto-rename-buffer' can be configured to rename rendered web
+pages by using their title, URL, or a user-defined function which
+returns a string.  For the first two cases, the length of the
+resulting name is controlled by 'eww-buffer-name-length'.  By default,
+no automatic renaming is performed.
+
+
 ** image-dired
 
 ---
diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 24c6335210..dc20c1745c 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -178,6 +178,33 @@ (defcustom eww-after-render-hook nil
   :group 'eww
   :type 'hook)
 
+(defcustom eww-auto-rename-buffer nil
+  "Automatically rename EWW buffers once the page is rendered.
+
+When nil, do not rename the buffer.  With a non-nil value
+determine the renaming scheme, as follows:
+
+- `title': Use the web page's title.
+- `url': Use the web page's URL.
+- a function's symbol: Run a user-defined function that returns a
+  string with which to rename the buffer.
+
+The string of `title' and `url' is always truncated to the value
+of `eww-buffer-name-length'."
+  :version "29.1"
+  :type '(choice
+          (const :tag "Do not rename buffers (default)" nil)
+          (const :tag "Rename buffer to web page title" title)
+          (const :tag "Rename buffer to web page URL" url)
+          (function :tag "A user-defined function to rename the buffer"))
+  :group 'eww)
+
+(defcustom eww-buffer-name-length 40
+  "Length of renamed buffer name, per `eww-auto-rename-buffer'."
+  :type 'natnum
+  :version "29.1"
+  :group 'eww)
+
 (defcustom eww-form-checkbox-selected-symbol "[X]"
   "Symbol used to represent a selected checkbox.
 See also `eww-form-checkbox-symbol'."
@@ -353,7 +380,7 @@ (defun eww (url &optional arg buffer)
     (setq url (url-recreate-url parsed)))
   (plist-put eww-data :url url)
   (plist-put eww-data :title "")
-  (eww-update-header-line-format)
+  (eww--after-page-change)
   (let ((inhibit-read-only t))
     (insert (format "Loading %s..." url))
     (goto-char (point-min)))
@@ -502,6 +529,30 @@ (defun eww-html-p (content-type)
   (member content-type '("text/html"
 			 "application/xhtml+xml")))
 
+(defun eww--rename-buffer ()
+  "Rename the current EWW buffer.
+The renaming scheme is performed in accordance with
+`eww-auto-rename-buffer'."
+  (let ((rename-string)
+        (formater
+         (lambda (string)
+           (format "*%s # eww*" (truncate-string-to-width
+                                 string eww-buffer-name-length))))
+        (site-title (plist-get eww-data :title))
+        (site-url (plist-get eww-data :url)))
+    (cond ((null eww-auto-rename-buffer))
+          ((eq eww-auto-rename-buffer 'url)
+           (setq rename-string (funcall formater site-url)))
+          ((functionp eww-auto-rename-buffer)
+           (setq rename-string (funcall eww-auto-rename-buffer)))
+          (t (setq rename-string
+                   (funcall formater (if (or (equal site-title "")
+                                             (null site-title))
+                                         "Untitled"
+                                       site-title)))))
+    (when rename-string
+      (rename-buffer rename-string t))))
+
 (defun eww-render (status url &optional point buffer encode)
   (let* ((headers (eww-parse-headers))
 	 (content-type
@@ -552,7 +603,7 @@ (defun eww-render (status url &optional point buffer encode)
 	    (eww-display-raw buffer (or encode charset 'utf-8))))
 	  (with-current-buffer buffer
 	    (plist-put eww-data :url url)
-	    (eww-update-header-line-format)
+	    (eww--after-page-change)
 	    (setq eww-history-position 0)
 	    (and last-coding-system-used
 		 (set-buffer-file-coding-system last-coding-system-used))
@@ -796,12 +847,16 @@ (defun eww-update-header-line-format ()
 		 `((?u . ,(or url ""))
 		   (?t . ,title))))))))
 
+(defun eww--after-page-change ()
+  (eww-update-header-line-format)
+  (eww--rename-buffer))
+
 (defun eww-tag-title (dom)
   (plist-put eww-data :title
 	     (replace-regexp-in-string
 	      "^ \\| $" ""
 	      (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom))))
-  (eww-update-header-line-format))
+  (eww--after-page-change))
 
 (defun eww-display-raw (buffer &optional encode)
   (let ((data (buffer-substring (point) (point-max))))
@@ -929,7 +984,7 @@ (defun eww-readable ()
 		      nil (current-buffer))
     (dolist (elem '(:source :url :title :next :previous :up))
       (plist-put eww-data elem (plist-get old-data elem)))
-    (eww-update-header-line-format)))
+    (eww--after-page-change)))
 
 (defun eww-score-readability (node)
   (let ((score -1))
@@ -1161,7 +1216,7 @@ (defun eww-restore-history (elem)
       (goto-char (plist-get elem :point))
       ;; Make buffer listings more informative.
       (setq list-buffers-directory (plist-get elem :url))
-      (eww-update-header-line-format))))
+      (eww--after-page-change))))
 
 (defun eww-next-url ()
   "Go to the page marked `next'.
-- 
2.33.0


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0001-Implement-auto-renaming-scheme-for-EWW-buffers-simple.patch --]
[-- Type: text/x-patch, Size: 4775 bytes --]

From 6bd3f685b4602aa478b5e987899e7215af20f25e Mon Sep 17 00:00:00 2001
Message-Id: <6bd3f685b4602aa478b5e987899e7215af20f25e.1634295379.git.info@protesilaos.com>
From: Protesilaos Stavrou <info@protesilaos.com>
Date: Fri, 15 Oct 2021 13:55:44 +0300
Subject: [PATCH] Implement auto-renaming scheme for EWW buffers

* etc/NEWS: Document the new user options.

* lisp/net/eww.el (eww-auto-rename-buffer-function)
(eww-auto-rename-buffer): Add user options.
(eww--rename-buffer): Introduce new function that performs the
renaming of buffers.
(eww--after-page-change): Add new wrapper function which calls
'eww-update-header-line-format' and 'eww-auto-rename-buffer-function'.
(eww, eww-render, eww-tag-title, eww-readable, eww-restore-history):
Include eww--after-page-change.

Fix bug#51176.

Co-authored-by: Abhiseck Paira <abhiseckpaira@disroot.org>
Co-authored-by: Protesilaos Stavrou <info@protesilaos.com>
---
 etc/NEWS        |  8 ++++++++
 lisp/net/eww.el | 39 ++++++++++++++++++++++++++++++++++-----
 2 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 7dd4d14274..906ff4e6ae 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -89,6 +89,14 @@
 \f
 * Changes in Specialized Modes and Packages in Emacs 29.1
 
+** eww
+
+---
+*** New user option to automatically rename EWW buffers
+When 'eww-auto-rename-buffer' is set to a non-nil value, it will make
+all buffers use either the title or the URL of the rendered web page.
+The rendering scheme is handled by 'eww-auto-rename-buffer-function'.
+
 ** image-dired
 
 ---
diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 24c6335210..14cf207e59 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -178,6 +178,21 @@
   :group 'eww
   :type 'hook)
 
+(defcustom eww-auto-rename-buffer-function #'eww--rename-buffer
+  "Function to rename EWW buffer.
+The function is called just before the `eww-after-render-hook' if
+`eww-auto-rename-buffer' is non-nil."
+  :version "29.1"
+  :group 'eww
+  :type 'function)
+
+(defcustom eww-auto-rename-buffer nil
+  "When non-nil rename EWW buffers after they are rendered.
+Renaming is controlled by `eww-auto-rename-buffer-function'."
+  :version "29.1"
+  :group 'eww
+  :type 'boolean)
+
 (defcustom eww-form-checkbox-selected-symbol "[X]"
   "Symbol used to represent a selected checkbox.
 See also `eww-form-checkbox-symbol'."
@@ -353,7 +368,7 @@
     (setq url (url-recreate-url parsed)))
   (plist-put eww-data :url url)
   (plist-put eww-data :title "")
-  (eww-update-header-line-format)
+  (eww--after-page-change)
   (let ((inhibit-read-only t))
     (insert (format "Loading %s..." url))
     (goto-char (point-min)))
@@ -502,6 +517,15 @@
   (member content-type '("text/html"
 			 "application/xhtml+xml")))
 
+(defun eww--rename-buffer ()
+  "Rename the current EWW buffer.
+Use the page's title or URL as an identifier."
+  (when (eq major-mode 'eww-mode)
+    (when-let ((title (or (plist-get eww-data :title)
+                          (plist-get eww-data :url))))
+      (rename-buffer
+       (format "*%s # eww*" (truncate-string-to-width title 40)) t))))
+
 (defun eww-render (status url &optional point buffer encode)
   (let* ((headers (eww-parse-headers))
 	 (content-type
@@ -552,7 +576,7 @@
 	    (eww-display-raw buffer (or encode charset 'utf-8))))
 	  (with-current-buffer buffer
 	    (plist-put eww-data :url url)
-	    (eww-update-header-line-format)
+	    (eww--after-page-change)
 	    (setq eww-history-position 0)
 	    (and last-coding-system-used
 		 (set-buffer-file-coding-system last-coding-system-used))
@@ -796,12 +820,17 @@
 		 `((?u . ,(or url ""))
 		   (?t . ,title))))))))
 
+(defun eww--after-page-change ()
+  (eww-update-header-line-format)
+  (when eww-auto-rename-buffer
+    (funcall eww-auto-rename-buffer-function)))
+
 (defun eww-tag-title (dom)
   (plist-put eww-data :title
 	     (replace-regexp-in-string
 	      "^ \\| $" ""
 	      (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom))))
-  (eww-update-header-line-format))
+  (eww--after-page-change))
 
 (defun eww-display-raw (buffer &optional encode)
   (let ((data (buffer-substring (point) (point-max))))
@@ -929,7 +958,7 @@
 		      nil (current-buffer))
     (dolist (elem '(:source :url :title :next :previous :up))
       (plist-put eww-data elem (plist-get old-data elem)))
-    (eww-update-header-line-format)))
+    (eww--after-page-change)))
 
 (defun eww-score-readability (node)
   (let ((score -1))
@@ -1161,7 +1190,7 @@
       (goto-char (plist-get elem :point))
       ;; Make buffer listings more informative.
       (setq list-buffers-directory (plist-get elem :url))
-      (eww-update-header-line-format))))
+      (eww--after-page-change))))
 
 (defun eww-next-url ()
   "Go to the page marked `next'.
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-15 11:22                       ` Protesilaos Stavrou
@ 2021-10-15 12:13                         ` Lars Ingebrigtsen
  2021-10-15 13:25                           ` Jose Antonio Ortega Ruiz
  0 siblings, 1 reply; 29+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-15 12:13 UTC (permalink / raw)
  To: Protesilaos Stavrou; +Cc: 51176, Yuchen Pei, Abhiseck Paira, Juri Linkov

Protesilaos Stavrou <info@protesilaos.com> writes:

> Because it may be useful for your consideration, there also exists the
> more complex approach that I alluded to earlier ("complex" may be an
> exaggeration, but still).  Attached as well and also based on your
> feedback.
>
>     0001-Implement-auto-renaming-scheme-for-EWW-buffers-complex.patch

Thanks; I thought the "complex" one was simpler, so I pushed that to
Emacs 29 (with some trivial changes and a manual entry).

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-15 12:13                         ` Lars Ingebrigtsen
@ 2021-10-15 13:25                           ` Jose Antonio Ortega Ruiz
  2021-10-15 14:00                             ` Lars Ingebrigtsen
  0 siblings, 1 reply; 29+ messages in thread
From: Jose Antonio Ortega Ruiz @ 2021-10-15 13:25 UTC (permalink / raw)
  To: Lars Ingebrigtsen, Protesilaos Stavrou
  Cc: 51176, Yuchen Pei, Abhiseck Paira, Juri Linkov

On Fri, Oct 15 2021, Lars Ingebrigtsen wrote:

> Protesilaos Stavrou <info@protesilaos.com> writes:
>
>> Because it may be useful for your consideration, there also exists the
>> more complex approach that I alluded to earlier ("complex" may be an
>> exaggeration, but still).  Attached as well and also based on your
>> feedback.
>>
>>     0001-Implement-auto-renaming-scheme-for-EWW-buffers-complex.patch
>
> Thanks; I thought the "complex" one was simpler, so I pushed that to
> Emacs 29 (with some trivial changes and a manual entry).

Since you went for the complex config, and additional option i'd find
useful for the ewww rename "flag" would be a string that specifies the
format to use once the title/url part has been computed.  So one can set
it to, say, "eww: %s", or whatever. I know that that use case is already
covered by the possibility of specifying a function, but having to
duplicate the default function just to change the final format string
feels a bit overkill (and i suspect changing the format is going to be a
very common reason to opt for a function call).


Cheers,
jao
-- 
Dealing with failure is easy: Work hard to improve. Success is also
easy to handle: You've solved the wrong problem. Work hard to improve.
  - Alan Perlis, Epigrams on Programming





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-15 13:25                           ` Jose Antonio Ortega Ruiz
@ 2021-10-15 14:00                             ` Lars Ingebrigtsen
  2021-10-15 19:59                               ` Jose Antonio Ortega Ruiz
  0 siblings, 1 reply; 29+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-15 14:00 UTC (permalink / raw)
  To: Jose Antonio Ortega Ruiz
  Cc: Protesilaos Stavrou, 51176, Yuchen Pei, Abhiseck Paira,
	Juri Linkov

Jose Antonio Ortega Ruiz <jao@gnu.org> writes:

> Since you went for the complex config, and additional option i'd find
> useful for the ewww rename "flag" would be a string that specifies the
> format to use once the title/url part has been computed.  So one can set
> it to, say, "eww: %s", or whatever. I know that that use case is already
> covered by the possibility of specifying a function, but having to
> duplicate the default function just to change the final format string
> feels a bit overkill (and i suspect changing the format is going to be a
> very common reason to opt for a function call).

I think it's best to leave that sort of flexibility to the function
option -- there's too many ways users may want to customise this.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-15 14:00                             ` Lars Ingebrigtsen
@ 2021-10-15 19:59                               ` Jose Antonio Ortega Ruiz
  2021-10-16  4:16                                 ` Protesilaos Stavrou
  0 siblings, 1 reply; 29+ messages in thread
From: Jose Antonio Ortega Ruiz @ 2021-10-15 19:59 UTC (permalink / raw)
  To: Lars Ingebrigtsen
  Cc: Protesilaos Stavrou, 51176, Yuchen Pei, Abhiseck Paira,
	Juri Linkov

On Fri, Oct 15 2021, Lars Ingebrigtsen wrote:

> Jose Antonio Ortega Ruiz <jao@gnu.org> writes:
>
>> Since you went for the complex config, and additional option i'd find
>> useful for the ewww rename "flag" would be a string that specifies the
>> format to use once the title/url part has been computed.  So one can set
>> it to, say, "eww: %s", or whatever. I know that that use case is already
>> covered by the possibility of specifying a function, but having to
>> duplicate the default function just to change the final format string
>> feels a bit overkill (and i suspect changing the format is going to be a
>> very common reason to opt for a function call).
>
> I think it's best to leave that sort of flexibility to the function
> option -- there's too many ways users may want to customise this.

but, 43.45% of users will simply want to change the format and get rid
of "# eww" at the end to see more of the title! :)

specifying a format string is extremely common in many packages out
there for things like this, and is, in my opinion, much more
user-friendly: to change that format with a function, one has to study
eww's api for obtaining title and url, and write a non-trivial piece of
elisp, probably (although admittedly not necessarily) outside the
customization interface.

in other words, i find the format string option as natural and common as
the nil and t ones (which, in a way, are also redundant and could be
removed, since it's trivial to write a function for them).

but i'm probably exaggerating: people will simply copy and paste the
relevant piece of elisp in their init files and call it a day.  so i'll
stop the bike-shedding now :)

cheers,
jao
-- 
Love, friendship, respect, do not unite people as much as a common
hatred for something. -Anton Chekhov (1860-1904)





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-15 19:59                               ` Jose Antonio Ortega Ruiz
@ 2021-10-16  4:16                                 ` Protesilaos Stavrou
  2021-10-16  4:20                                   ` Protesilaos Stavrou
                                                     ` (2 more replies)
  0 siblings, 3 replies; 29+ messages in thread
From: Protesilaos Stavrou @ 2021-10-16  4:16 UTC (permalink / raw)
  To: Jose Antonio Ortega Ruiz, Lars Ingebrigtsen
  Cc: 51176, Yuchen Pei, Abhiseck Paira, Juri Linkov

On 2021-10-15, 20:59 +0100, Jose Antonio Ortega Ruiz <jao@gnu.org> wrote:

> On Fri, Oct 15 2021, Lars Ingebrigtsen wrote:
>
>> Jose Antonio Ortega Ruiz <jao@gnu.org> writes:
>>
>>> Since you went for the complex config, and additional option i'd find
>>> useful for the ewww rename "flag" would be a string that specifies the
>>> format to use once the title/url part has been computed.  So one can set
>>> it to, say, "eww: %s", or whatever. I know that that use case is already
>>> covered by the possibility of specifying a function, but having to
>>> duplicate the default function just to change the final format string
>>> feels a bit overkill (and i suspect changing the format is going to be a
>>> very common reason to opt for a function call).
>>
>> I think it's best to leave that sort of flexibility to the function
>> option -- there's too many ways users may want to customise this.
>
> but, 43.45% of users will simply want to change the format and get rid
> of "# eww" at the end to see more of the title! :)
>
> specifying a format string is extremely common in many packages out
> there for things like this, and is, in my opinion, much more
> user-friendly: to change that format with a function, one has to study
> eww's api for obtaining title and url, and write a non-trivial piece of
> elisp, probably (although admittedly not necessarily) outside the
> customization interface.
>
> in other words, i find the format string option as natural and common as
> the nil and t ones (which, in a way, are also redundant and could be
> removed, since it's trivial to write a function for them).
>
> but i'm probably exaggerating: people will simply copy and paste the
> relevant piece of elisp in their init files and call it a day.  so i'll
> stop the bike-shedding now :)

How about we add a sample function in the doc string?  That would give
users all the information they need to get started, no?

Like this function:

    (defun my-eww-rename-buffer ()
      (when (eq major-mode 'eww-mode)
        (when-let ((string (or (plist-get eww-data :title)
                               (plist-get eww-data :url))))
          (rename-buffer (format "*%s*" string) t))))

And we add it here:

    (defcustom eww-auto-rename-buffer nil
      "Automatically rename EWW buffers once the page is rendered.

    When nil, do not rename the buffer.  With a non-nil value
    determine the renaming scheme, as follows:

    - `title': Use the web page's title.
    - `url': Use the web page's URL.
    - a function's symbol: Run a user-defined function that returns a
      string with which to rename the buffer.  Sample of a
      user-defined function:

        (defun my-eww-rename-buffer ()
          (when (eq major-mode 'eww-mode)
            (when-let ((string (or (plist-get eww-data :title)
                                   (plist-get eww-data :url))))
              (rename-buffer (format \"*%s*\" string) t))))

    The string of `title' and `url' is always truncated to the value
    of `eww-buffer-name-length'."
      :version "29.1"
      :type '(choice
              (const :tag "Do not rename buffers (default)" nil)
              (const :tag "Rename buffer to web page title" title)
              (const :tag "Rename buffer to web page URL" url)
              (function :tag "A user-defined function to rename the buffer"))
      :group 'eww)

I can prepare a patch if you think this is worth it.

-- 
Protesilaos Stavrou
https://protesilaos.com





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-16  4:16                                 ` Protesilaos Stavrou
@ 2021-10-16  4:20                                   ` Protesilaos Stavrou
  2021-10-16 14:31                                   ` Jose Antonio Ortega Ruiz
  2021-10-18  6:43                                   ` Lars Ingebrigtsen
  2 siblings, 0 replies; 29+ messages in thread
From: Protesilaos Stavrou @ 2021-10-16  4:20 UTC (permalink / raw)
  To: Jose Antonio Ortega Ruiz, Lars Ingebrigtsen
  Cc: 51176, Yuchen Pei, Abhiseck Paira, Juri Linkov

On 2021-10-16, 07:16 +0300, Protesilaos Stavrou <info@protesilaos.com> wrote:

> On 2021-10-15, 20:59 +0100, Jose Antonio Ortega Ruiz <jao@gnu.org> wrote:
>
>> On Fri, Oct 15 2021, Lars Ingebrigtsen wrote:
>>
>>> Jose Antonio Ortega Ruiz <jao@gnu.org> writes:
>>>
>>>> Since you went for the complex config, and additional option i'd find
>>>> useful for the ewww rename "flag" would be a string that specifies the
>>>> format to use once the title/url part has been computed.  So one can set
>>>> it to, say, "eww: %s", or whatever. I know that that use case is already
>>>> covered by the possibility of specifying a function, but having to
>>>> duplicate the default function just to change the final format string
>>>> feels a bit overkill (and i suspect changing the format is going to be a
>>>> very common reason to opt for a function call).
>>>
>>> I think it's best to leave that sort of flexibility to the function
>>> option -- there's too many ways users may want to customise this.
>>
>> but, 43.45% of users will simply want to change the format and get rid
>> of "# eww" at the end to see more of the title! :)
>>
>> specifying a format string is extremely common in many packages out
>> there for things like this, and is, in my opinion, much more
>> user-friendly: to change that format with a function, one has to study
>> eww's api for obtaining title and url, and write a non-trivial piece of
>> elisp, probably (although admittedly not necessarily) outside the
>> customization interface.
>>
>> in other words, i find the format string option as natural and common as
>> the nil and t ones (which, in a way, are also redundant and could be
>> removed, since it's trivial to write a function for them).
>>
>> but i'm probably exaggerating: people will simply copy and paste the
>> relevant piece of elisp in their init files and call it a day.  so i'll
>> stop the bike-shedding now :)
>
> How about we add a sample function in the doc string?  That would give
> users all the information they need to get started, no?

Sorry, I sent the wrong version...  I meant this function:

    (defun my-eww-rename-buffer ()
      (when (eq major-mode 'eww-mode)
        (when-let ((string (or (plist-get eww-data :title)
                               (plist-get eww-data :url))))
          (format "*%s*" string))))

And we edit this doc string:

    (defcustom eww-auto-rename-buffer nil
      "Automatically rename EWW buffers once the page is rendered.

    When nil, do not rename the buffer.  With a non-nil value
    determine the renaming scheme, as follows:

    - `title': Use the web page's title.
    - `url': Use the web page's URL.
    - a function's symbol: Run a user-defined function that returns a
      string with which to rename the buffer.  Sample of a
      user-defined function:

        (defun my-eww-rename-buffer ()
          (when (eq major-mode 'eww-mode)
            (when-let ((string (or (plist-get eww-data :title)
                                   (plist-get eww-data :url))))
              (format \"*%s*\" string))))

    The string of `title' and `url' is always truncated to the value
    of `eww-buffer-name-length'."
      :version "29.1"
      :type '(choice
              (const :tag "Do not rename buffers (default)" nil)
              (const :tag "Rename buffer to web page title" title)
              (const :tag "Rename buffer to web page URL" url)
              (function :tag "A user-defined function to rename the buffer"))
      :group 'eww)

-- 
Protesilaos Stavrou
https://protesilaos.com





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-16  4:16                                 ` Protesilaos Stavrou
  2021-10-16  4:20                                   ` Protesilaos Stavrou
@ 2021-10-16 14:31                                   ` Jose Antonio Ortega Ruiz
  2021-10-18  6:43                                   ` Lars Ingebrigtsen
  2 siblings, 0 replies; 29+ messages in thread
From: Jose Antonio Ortega Ruiz @ 2021-10-16 14:31 UTC (permalink / raw)
  To: Protesilaos Stavrou, Lars Ingebrigtsen
  Cc: 51176, Yuchen Pei, Abhiseck Paira, Juri Linkov

On Sat, Oct 16 2021, Protesilaos Stavrou wrote:

[...]

> How about we add a sample function in the doc string?  That would give
> users all the information they need to get started, no?

imho, that'd be excellent, even if we also had the string option.

cheers,
jao
-- 
More people would learn from their mistakes if they weren't so busy
denying them - Harlod J. Smith





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-16  4:16                                 ` Protesilaos Stavrou
  2021-10-16  4:20                                   ` Protesilaos Stavrou
  2021-10-16 14:31                                   ` Jose Antonio Ortega Ruiz
@ 2021-10-18  6:43                                   ` Lars Ingebrigtsen
  2021-10-18  7:15                                     ` Protesilaos Stavrou
  2 siblings, 1 reply; 29+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-18  6:43 UTC (permalink / raw)
  To: Protesilaos Stavrou
  Cc: 51176, Yuchen Pei, Jose Antonio Ortega Ruiz, Abhiseck Paira,
	Juri Linkov

Protesilaos Stavrou <info@protesilaos.com> writes:

> How about we add a sample function in the doc string?  That would give
> users all the information they need to get started, no?

Makes sense to me.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-18  6:43                                   ` Lars Ingebrigtsen
@ 2021-10-18  7:15                                     ` Protesilaos Stavrou
  2021-10-18  7:16                                       ` Lars Ingebrigtsen
  0 siblings, 1 reply; 29+ messages in thread
From: Protesilaos Stavrou @ 2021-10-18  7:15 UTC (permalink / raw)
  To: Lars Ingebrigtsen
  Cc: 51176, Yuchen Pei, Jose Antonio Ortega Ruiz, Abhiseck Paira,
	Juri Linkov

[-- Attachment #1: Type: text/plain, Size: 447 bytes --]

On 2021-10-18, 08:43 +0200, Lars Ingebrigtsen <larsi@gnus.org> wrote:

> Protesilaos Stavrou <info@protesilaos.com> writes:
>
>> How about we add a sample function in the doc string?  That would give
>> users all the information they need to get started, no?
>
> Makes sense to me.

Patch attached.  I only updated the doc string, thinking that this
addition does not need to be added to the NEWS.

-- 
Protesilaos Stavrou
https://protesilaos.com

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-sample-user-function-in-eww-auto-rename-buffer.patch --]
[-- Type: text/x-patch, Size: 1379 bytes --]

From 85762fa3b88410675cbf89854f3a1c29bbac7a9d Mon Sep 17 00:00:00 2001
Message-Id: <85762fa3b88410675cbf89854f3a1c29bbac7a9d.1634540834.git.info@protesilaos.com>
From: Protesilaos Stavrou <info@protesilaos.com>
Date: Mon, 18 Oct 2021 10:07:03 +0300
Subject: [PATCH] Add sample user function in eww-auto-rename-buffer

* eww.el (eww-auto-rename-buffer): Update doc string.

Fix bug#51176.

Co-authored-by: Abhiseck Paira <abhiseckpaira@disroot.org>
Co-authored-by: Protesilaos Stavrou <info@protesilaos.com>
---
 lisp/net/eww.el | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index bed458ed8a..178a25e4be 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -187,7 +187,14 @@ (defcustom eww-auto-rename-buffer nil
 - `title': Use the web page's title.
 - `url': Use the web page's URL.
 - a function's symbol: Run a user-defined function that returns a
-  string with which to rename the buffer.
+  string with which to rename the buffer.  Sample of a
+  user-defined function:
+
+  (defun my-eww-rename-buffer ()
+    (when (eq major-mode 'eww-mode)
+      (when-let ((string (or (plist-get eww-data :title)
+                             (plist-get eww-data :url))))
+        (format \"*%s*\" string))))
 
 The string of `title' and `url' is always truncated to the value
 of `eww-buffer-name-length'."
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* bug#51176: eww switch buffer by title
  2021-10-18  7:15                                     ` Protesilaos Stavrou
@ 2021-10-18  7:16                                       ` Lars Ingebrigtsen
  0 siblings, 0 replies; 29+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-18  7:16 UTC (permalink / raw)
  To: Protesilaos Stavrou
  Cc: 51176, Yuchen Pei, Jose Antonio Ortega Ruiz, Abhiseck Paira,
	Juri Linkov

Protesilaos Stavrou <info@protesilaos.com> writes:

> Patch attached.  I only updated the doc string, thinking that this
> addition does not need to be added to the NEWS.

Thanks; pushed to Emacs 29.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2021-10-18  7:16 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-13  4:56 bug#51176: eww switch buffer by title Yuchen Pei
2021-10-13 11:52 ` Lars Ingebrigtsen
2021-10-13 12:15   ` Yuchen Pei
2021-10-13 12:54     ` Lars Ingebrigtsen
2021-10-13 16:28       ` Juri Linkov
2021-10-13 16:47         ` Lars Ingebrigtsen
2021-10-13 17:31           ` Juri Linkov
2021-10-13 18:14             ` Lars Ingebrigtsen
2021-10-13 20:31               ` Protesilaos Stavrou
2021-10-13 20:33                 ` Lars Ingebrigtsen
2021-10-15  6:31                   ` Protesilaos Stavrou
2021-10-15  8:32                     ` miha--- via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-10-15 10:35                     ` Lars Ingebrigtsen
2021-10-15 11:22                       ` Protesilaos Stavrou
2021-10-15 12:13                         ` Lars Ingebrigtsen
2021-10-15 13:25                           ` Jose Antonio Ortega Ruiz
2021-10-15 14:00                             ` Lars Ingebrigtsen
2021-10-15 19:59                               ` Jose Antonio Ortega Ruiz
2021-10-16  4:16                                 ` Protesilaos Stavrou
2021-10-16  4:20                                   ` Protesilaos Stavrou
2021-10-16 14:31                                   ` Jose Antonio Ortega Ruiz
2021-10-18  6:43                                   ` Lars Ingebrigtsen
2021-10-18  7:15                                     ` Protesilaos Stavrou
2021-10-18  7:16                                       ` Lars Ingebrigtsen
2021-10-13 21:26                 ` Yuchen Pei
2021-10-14  0:16                 ` bug#51176: [External] : " Drew Adams
2021-10-13 23:48             ` Yuchen Pei
2021-10-14 11:06               ` Lars Ingebrigtsen
2021-10-14 16:05               ` Juri Linkov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).