aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorProtesilaos Stavrou2021-10-15 14:12:32 +0200
committerLars Ingebrigtsen2021-10-15 14:13:58 +0200
commit171de3eee459ed64388a8ced7d07fa031ea025a6 (patch)
treeadaed5bc528f080fba7145fd440fdb177e8e509b
parent7dedba1cc02055befa097f8782cda108f4af08c6 (diff)
downloademacs-171de3eee459ed64388a8ced7d07fa031ea025a6.tar.gz
emacs-171de3eee459ed64388a8ced7d07fa031ea025a6.zip
Add new option to rename 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>
-rw-r--r--doc/misc/eww.texi8
-rw-r--r--etc/NEWS10
-rw-r--r--lisp/net/eww.el65
3 files changed, 78 insertions, 5 deletions
diff --git a/doc/misc/eww.texi b/doc/misc/eww.texi
index 2543dc2ff5a..7635685e56f 100644
--- a/doc/misc/eww.texi
+++ b/doc/misc/eww.texi
@@ -380,6 +380,14 @@ thus allowing for the use of the usual substitutions, such as
380@code{\[eww-reload]} for the current key binding of the 380@code{\[eww-reload]} for the current key binding of the
381@code{eww-reload} command. 381@code{eww-reload} command.
382 382
383@vindex eww-auto-rename-buffer
384 If the @code{eww-auto-rename-buffer} user option is non-@code{nil},
385EWW buffers will be renamed after rendering a document. If this is
386@code{title}, rename based on the title of the document. If this is
387@code{url}, rename based on the @acronym{URL} of the document. This
388can also be a user-defined function, which is called with no
389parameters in the EWW buffer, and should return a string.
390
383@node Command Line 391@node Command Line
384@chapter Command Line Usage 392@chapter Command Line Usage
385 393
diff --git a/etc/NEWS b/etc/NEWS
index 7dd4d14274f..2c09d24dde3 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -89,6 +89,16 @@ Customize this option to limit the amount of entries in the menu
89 89
90* Changes in Specialized Modes and Packages in Emacs 29.1 90* Changes in Specialized Modes and Packages in Emacs 29.1
91 91
92** eww
93
94+++
95*** New user option to automatically rename EWW buffers.
96The 'eww-auto-rename-buffer' user option can be configured to rename
97rendered web pages by using their title, URL, or a user-defined
98function which returns a string. For the first two cases, the length
99of the resulting name is controlled by 'eww-buffer-name-length'. By
100default, no automatic renaming is performed.
101
92** image-dired 102** image-dired
93 103
94--- 104---
diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 24c63352105..bed458ed8a5 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -178,6 +178,33 @@ the tab bar is enabled."
178 :group 'eww 178 :group 'eww
179 :type 'hook) 179 :type 'hook)
180 180
181(defcustom eww-auto-rename-buffer nil
182 "Automatically rename EWW buffers once the page is rendered.
183
184When nil, do not rename the buffer. With a non-nil value
185determine the renaming scheme, as follows:
186
187- `title': Use the web page's title.
188- `url': Use the web page's URL.
189- a function's symbol: Run a user-defined function that returns a
190 string with which to rename the buffer.
191
192The string of `title' and `url' is always truncated to the value
193of `eww-buffer-name-length'."
194 :version "29.1"
195 :type '(choice
196 (const :tag "Do not rename buffers (default)" nil)
197 (const :tag "Rename buffer to web page title" title)
198 (const :tag "Rename buffer to web page URL" url)
199 (function :tag "A user-defined function to rename the buffer"))
200 :group 'eww)
201
202(defcustom eww-buffer-name-length 40
203 "Length of renamed buffer name, per `eww-auto-rename-buffer'."
204 :type 'natnum
205 :version "29.1"
206 :group 'eww)
207
181(defcustom eww-form-checkbox-selected-symbol "[X]" 208(defcustom eww-form-checkbox-selected-symbol "[X]"
182 "Symbol used to represent a selected checkbox. 209 "Symbol used to represent a selected checkbox.
183See also `eww-form-checkbox-symbol'." 210See also `eww-form-checkbox-symbol'."
@@ -353,7 +380,7 @@ killed after rendering."
353 (setq url (url-recreate-url parsed))) 380 (setq url (url-recreate-url parsed)))
354 (plist-put eww-data :url url) 381 (plist-put eww-data :url url)
355 (plist-put eww-data :title "") 382 (plist-put eww-data :title "")
356 (eww-update-header-line-format) 383 (eww--after-page-change)
357 (let ((inhibit-read-only t)) 384 (let ((inhibit-read-only t))
358 (insert (format "Loading %s..." url)) 385 (insert (format "Loading %s..." url))
359 (goto-char (point-min))) 386 (goto-char (point-min)))
@@ -502,6 +529,30 @@ Currently this means either text/html or application/xhtml+xml."
502 (member content-type '("text/html" 529 (member content-type '("text/html"
503 "application/xhtml+xml"))) 530 "application/xhtml+xml")))
504 531
532(defun eww--rename-buffer ()
533 "Rename the current EWW buffer.
534The renaming scheme is performed in accordance with
535`eww-auto-rename-buffer'."
536 (let ((rename-string)
537 (formatter
538 (lambda (string)
539 (format "*%s # eww*" (truncate-string-to-width
540 string eww-buffer-name-length))))
541 (site-title (plist-get eww-data :title))
542 (site-url (plist-get eww-data :url)))
543 (cond ((null eww-auto-rename-buffer))
544 ((eq eww-auto-rename-buffer 'url)
545 (setq rename-string (funcall formatter site-url)))
546 ((functionp eww-auto-rename-buffer)
547 (setq rename-string (funcall eww-auto-rename-buffer)))
548 (t (setq rename-string
549 (funcall formatter (if (or (equal site-title "")
550 (null site-title))
551 "Untitled"
552 site-title)))))
553 (when rename-string
554 (rename-buffer rename-string t))))
555
505(defun eww-render (status url &optional point buffer encode) 556(defun eww-render (status url &optional point buffer encode)
506 (let* ((headers (eww-parse-headers)) 557 (let* ((headers (eww-parse-headers))
507 (content-type 558 (content-type
@@ -552,7 +603,7 @@ Currently this means either text/html or application/xhtml+xml."
552 (eww-display-raw buffer (or encode charset 'utf-8)))) 603 (eww-display-raw buffer (or encode charset 'utf-8))))
553 (with-current-buffer buffer 604 (with-current-buffer buffer
554 (plist-put eww-data :url url) 605 (plist-put eww-data :url url)
555 (eww-update-header-line-format) 606 (eww--after-page-change)
556 (setq eww-history-position 0) 607 (setq eww-history-position 0)
557 (and last-coding-system-used 608 (and last-coding-system-used
558 (set-buffer-file-coding-system last-coding-system-used)) 609 (set-buffer-file-coding-system last-coding-system-used))
@@ -796,12 +847,16 @@ Currently this means either text/html or application/xhtml+xml."
796 `((?u . ,(or url "")) 847 `((?u . ,(or url ""))
797 (?t . ,title)))))))) 848 (?t . ,title))))))))
798 849
850(defun eww--after-page-change ()
851 (eww-update-header-line-format)
852 (eww--rename-buffer))
853
799(defun eww-tag-title (dom) 854(defun eww-tag-title (dom)
800 (plist-put eww-data :title 855 (plist-put eww-data :title
801 (replace-regexp-in-string 856 (replace-regexp-in-string
802 "^ \\| $" "" 857 "^ \\| $" ""
803 (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom)))) 858 (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom))))
804 (eww-update-header-line-format)) 859 (eww--after-page-change))
805 860
806(defun eww-display-raw (buffer &optional encode) 861(defun eww-display-raw (buffer &optional encode)
807 (let ((data (buffer-substring (point) (point-max)))) 862 (let ((data (buffer-substring (point) (point-max))))
@@ -929,7 +984,7 @@ the like."
929 nil (current-buffer)) 984 nil (current-buffer))
930 (dolist (elem '(:source :url :title :next :previous :up)) 985 (dolist (elem '(:source :url :title :next :previous :up))
931 (plist-put eww-data elem (plist-get old-data elem))) 986 (plist-put eww-data elem (plist-get old-data elem)))
932 (eww-update-header-line-format))) 987 (eww--after-page-change)))
933 988
934(defun eww-score-readability (node) 989(defun eww-score-readability (node)
935 (let ((score -1)) 990 (let ((score -1))
@@ -1161,7 +1216,7 @@ instead of `browse-url-new-window-flag'."
1161 (goto-char (plist-get elem :point)) 1216 (goto-char (plist-get elem :point))
1162 ;; Make buffer listings more informative. 1217 ;; Make buffer listings more informative.
1163 (setq list-buffers-directory (plist-get elem :url)) 1218 (setq list-buffers-directory (plist-get elem :url))
1164 (eww-update-header-line-format)))) 1219 (eww--after-page-change))))
1165 1220
1166(defun eww-next-url () 1221(defun eww-next-url ()
1167 "Go to the page marked `next'. 1222 "Go to the page marked `next'.