unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Add command to browse xwidget history
       [not found] <87a6i62e45.fsf.ref@yahoo.com>
@ 2021-11-15  5:15 ` Po Lu
  2021-11-15  9:40   ` Robert Pluim
  2021-11-15 12:56   ` Eli Zaretskii
  0 siblings, 2 replies; 5+ messages in thread
From: Po Lu @ 2021-11-15  5:15 UTC (permalink / raw)
  To: emacs-devel

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

I added a command that lets you browse the history of the current
xwidget, but I'm not sure the documentation for it is detailed enough.

Please let me know if this is OK to push, and if not, how I can improve
it.  Thanks in advance.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-command-to-browse-xwidget-history.patch --]
[-- Type: text/x-patch, Size: 7097 bytes --]

From 3e867ae768292ecb855095d49cd2f35ab6c77ce9 Mon Sep 17 00:00:00 2001
From: Po Lu <luangruo@yahoo.com>
Date: Mon, 15 Nov 2021 13:12:45 +0800
Subject: [PATCH] Add command to browse xwidget history

* doc/emacs/misc.texi (Embedded WebKit Widgets)
* etc/NEWS: Document `xwidget-webkit-browse-history'.

* lisp/xwidget.el (xwidget-webkit-mode-map): Bind "H" to
xwidget-webkit-browse-history.
(xwidget-webkit-import-widget): Set last session buffer correctly.
(xwidget-webkit-browse-history): New command.
(xwidget-webkit-history--session): New variable.

(xwidget-webkit-history--insert-item)
(xwidget-webkit-history-select-item)
(xwidget-webkit-history-reload): New functions.

(xwidget-webkit-history-mode): New major mode.
---
 doc/emacs/misc.texi |  8 ++++++
 etc/NEWS            |  6 +++++
 lisp/xwidget.el     | 64 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 78 insertions(+)

diff --git a/doc/emacs/misc.texi b/doc/emacs/misc.texi
index 3d423d7675..74542d46eb 100644
--- a/doc/emacs/misc.texi
+++ b/doc/emacs/misc.texi
@@ -3011,6 +3011,14 @@ Embedded WebKit Widgets
 
 To leave incremental search, you can type @kbd{C-g}.
 
+@findex xwidget-webkit-browse-history
+@cindex history of webkit buffers
+  The command @code{xwidget-webkit-browse-history} displays a buffer
+containing a list of pages once loaded by the current WebKit buffer,
+and lets you navigate to those pages by hitting @kbd{RET}.
+
+It is bound to @kbd{H}.
+
 @node Browse-URL
 @subsection  Following URLs
 @cindex World Wide Web
diff --git a/etc/NEWS b/etc/NEWS
index 312fc18f4f..cda29650c0 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -486,6 +486,12 @@ This mode acts similarly to incremental search, and allows to search
 the contents of a WebKit widget.  In xwidget-webkit mode, it is bound
 to 'C-s' and 'C-r'.
 
++++
+*** New command 'xwidget-webkit-browse-history'.
+This command displays a buffer containing the page load history of
+the current WebKit widget, and allows you to navigate to various
+points in history.
+
 ---
 *** On X11, the WebKit inspector is now available inside xwidgets.
 To access the inspector, right click on the widget and select "Inspect
diff --git a/lisp/xwidget.el b/lisp/xwidget.el
index 485d995f41..7e784aa0cf 100644
--- a/lisp/xwidget.el
+++ b/lisp/xwidget.el
@@ -55,6 +55,7 @@
 (declare-function delete-xwidget-view "xwidget.c" (xwidget-view))
 (declare-function get-buffer-xwidgets "xwidget.c" (buffer))
 (declare-function xwidget-query-on-exit-flag "xwidget.c" (xwidget))
+(declare-function xwidget-webkit-back-forward-list "xwidget.c" (xwidget &optional limit))
 
 (defgroup xwidget nil
   "Displaying native widgets in Emacs buffers."
@@ -194,6 +195,7 @@ xwidget-webkit-mode-map
     (define-key map "e" 'xwidget-webkit-edit-mode)
     (define-key map "\C-r" 'xwidget-webkit-isearch-mode)
     (define-key map "\C-s" 'xwidget-webkit-isearch-mode)
+    (define-key map "H" 'xwidget-webkit-browse-history)
 
     ;;similar to image mode bindings
     (define-key map (kbd "SPC")                 'xwidget-webkit-scroll-up)
@@ -228,6 +230,7 @@ nil
         ["Back" xwidget-webkit-back t]
         ["Forward" xwidget-webkit-forward t]
         ["Reload" xwidget-webkit-reload t]
+        ["History" xwidget-webkit-browse-history t]
         ["Insert String" xwidget-webkit-insert-string
          :active t
          :help "Insert a string into the currently active field"]
@@ -396,6 +399,9 @@ xwidget-webkit-callback
              (when (or (string-equal (nth 3 last-input-event)
                                      "load-finished")
                        (> (length title) 0))
+               (when (get-buffer "*Xwidget WebKit History*")
+                 (with-current-buffer "*Xwidget WebKit History*"
+                   (revert-buffer)))
                (with-current-buffer (xwidget-buffer xwidget)
                  (setq xwidget-webkit--title title)
                  (force-mode-line-update)
@@ -775,6 +781,7 @@ xwidget-webkit-import-widget
          (callback #'xwidget-webkit-callback)
          (buffer (get-buffer-create bufname)))
     (with-current-buffer buffer
+      (setq xwidget-webkit-last-session-buffer buffer)
       (save-excursion
         (erase-buffer)
         (insert ".")
@@ -821,6 +828,15 @@ xwidget-webkit-current-url
   (let ((url (xwidget-webkit-uri (xwidget-webkit-current-session))))
     (message "URL: %s" (kill-new (or url "")))))
 
+(defun xwidget-webkit-browse-history ()
+  "Display a buffer containing the history of page loads."
+  (interactive)
+  (setq xwidget-webkit-last-session-buffer (current-buffer))
+  (let ((buffer (get-buffer-create "*Xwidget WebKit History*")))
+    (with-current-buffer buffer
+      (xwidget-webkit-history-mode))
+    (display-buffer buffer)))
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 (defun xwidget-webkit-get-selection (proc)
   "Get the webkit selection and pass it to PROC."
@@ -1059,6 +1075,54 @@ xwidget-webkit-isearch-yank-kill
         (concat xwidget-webkit-isearch--string
                 (current-kill 0)))
   (xwidget-webkit-isearch--update))
+
+(defvar-local xwidget-webkit-history--session nil
+  "The xwidget this history buffer controls.")
+
+(defun xwidget-webkit-history--insert-item (item)
+  "Insert specified ITEM into the current buffer."
+  (let ((idx (car item))
+        (title (cadr item))
+        (uri (caddr item)))
+    (push (list idx (vector title uri))
+          tabulated-list-entries)))
+
+(defun xwidget-webkit-history-select-item ()
+  "Navigate to this line's history item."
+  (interactive)
+  (let ((id (tabulated-list-get-id)))
+    (xwidget-webkit-goto-history xwidget-webkit-history--session id))
+  (xwidget-webkit-history-reload))
+
+(defun xwidget-webkit-history-reload (&rest ignored)
+  "Reload the current history buffer."
+  (interactive)
+  (setq tabulated-list-entries nil)
+  (let* ((back-forward-list
+          (xwidget-webkit-back-forward-list xwidget-webkit-history--session))
+         (back-list (car back-forward-list))
+         (here (cadr back-forward-list))
+         (forward-list (caddr back-forward-list)))
+    (mapc #'xwidget-webkit-history--insert-item back-list)
+    (xwidget-webkit-history--insert-item here)
+    (mapc #'xwidget-webkit-history--insert-item forward-list))
+  (tabulated-list-print t nil))
+
+(define-derived-mode xwidget-webkit-history-mode tabulated-list-mode
+  "Xwidget Webkit History"
+  "Major mode for browsing the history of an Xwidget Webkit buffer.
+Each line describes an entry in history."
+  (setq truncate-lines t)
+  (setq buffer-read-only t)
+  (setq tabulated-list-format [("Title" 50 nil) ("URL" 100 nil)])
+  (setq tabulated-list-entries nil)
+  (setq xwidget-webkit-history--session (xwidget-webkit-current-session))
+  (xwidget-webkit-history-reload)
+  (setq-local revert-buffer-function #'xwidget-webkit-history-reload)
+  (tabulated-list-init-header))
+
+(define-key xwidget-webkit-history-mode-map (kbd "RET")
+  #'xwidget-webkit-history-select-item)
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 (defvar xwidget-view-list)              ; xwidget.c
-- 
2.31.1


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

* Re: Add command to browse xwidget history
  2021-11-15  5:15 ` Add command to browse xwidget history Po Lu
@ 2021-11-15  9:40   ` Robert Pluim
  2021-11-15  9:58     ` Po Lu
  2021-11-15 12:56   ` Eli Zaretskii
  1 sibling, 1 reply; 5+ messages in thread
From: Robert Pluim @ 2021-11-15  9:40 UTC (permalink / raw)
  To: Po Lu; +Cc: emacs-devel

>>>>> On Mon, 15 Nov 2021 13:15:54 +0800, Po Lu <luangruo@yahoo.com> said:

    Po> +@findex xwidget-webkit-browse-history
    Po> +@cindex history of webkit buffers
    Po> +  The command @code{xwidget-webkit-browse-history} displays a buffer
    Po> +containing a list of pages once loaded by the current WebKit buffer,

'previously loaded'

    Po> +and lets you navigate to those pages by hitting @kbd{RET}.
    Po> +
    Po> +It is bound to @kbd{H}.
    Po> +
    Po>  @node Browse-URL
    Po>  @subsection  Following URLs
    Po>  @cindex World Wide Web
    Po> diff --git a/etc/NEWS b/etc/NEWS
    Po> index 312fc18f4f..cda29650c0 100644
    Po> --- a/etc/NEWS
    Po> +++ b/etc/NEWS
    Po> @@ -486,6 +486,12 @@ This mode acts similarly to incremental search, and allows to search
    Po>  the contents of a WebKit widget.  In xwidget-webkit mode, it is bound
    Po>  to 'C-s' and 'C-r'.
 
    Po> ++++
    Po> +*** New command 'xwidget-webkit-browse-history'.
    Po> +This command displays a buffer containing the page load history of
    Po> +the current WebKit widget, and allows you to navigate to various
    Po> +points in history.
    Po> +

Short phrases are better: 'allows you to navigate it'

    Po> +               (when (get-buffer "*Xwidget WebKit History*")
    Po> +                 (with-current-buffer "*Xwidget WebKit History*"
    Po> +                   (revert-buffer)))

Minor optimisation: use `when-let'. Avoid repetition. DRY 😄

Robert
-- 



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

* Re: Add command to browse xwidget history
  2021-11-15  9:40   ` Robert Pluim
@ 2021-11-15  9:58     ` Po Lu
  0 siblings, 0 replies; 5+ messages in thread
From: Po Lu @ 2021-11-15  9:58 UTC (permalink / raw)
  To: Robert Pluim; +Cc: emacs-devel

Robert Pluim <rpluim@gmail.com> writes:

> 'previously loaded'
> Short phrases are better: 'allows you to navigate it'
> Minor optimisation: use `when-let'. Avoid repetition. DRY 😄

Thanks.




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

* Re: Add command to browse xwidget history
  2021-11-15  5:15 ` Add command to browse xwidget history Po Lu
  2021-11-15  9:40   ` Robert Pluim
@ 2021-11-15 12:56   ` Eli Zaretskii
  2021-11-15 12:58     ` Po Lu
  1 sibling, 1 reply; 5+ messages in thread
From: Eli Zaretskii @ 2021-11-15 12:56 UTC (permalink / raw)
  To: Po Lu; +Cc: emacs-devel

> From: Po Lu <luangruo@yahoo.com>
> Date: Mon, 15 Nov 2021 13:15:54 +0800
> 
> I added a command that lets you browse the history of the current
> xwidget, but I'm not sure the documentation for it is detailed enough.
> 
> Please let me know if this is OK to push, and if not, how I can improve
> it.  Thanks in advance.

The documentation LGTM, but it is customary in Emacs to have mouse
support in such list buffers, and also show tooltips when the mouse
hovers over an item.  Compare with "M-x proced", for example.



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

* Re: Add command to browse xwidget history
  2021-11-15 12:56   ` Eli Zaretskii
@ 2021-11-15 12:58     ` Po Lu
  0 siblings, 0 replies; 5+ messages in thread
From: Po Lu @ 2021-11-15 12:58 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

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

Eli Zaretskii <eliz@gnu.org> writes:

> The documentation LGTM, but it is customary in Emacs to have mouse
> support in such list buffers, and also show tooltips when the mouse
> hovers over an item.  Compare with "M-x proced", for example.

Yes, I added that support on my side, but forgot to send a patch.

Here it is, thanks.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-command-to-browse-xwidget-history.patch --]
[-- Type: text/x-patch, Size: 7806 bytes --]

From b52851272fdbb17a9ed59b0a03dfdf9d3725e97b Mon Sep 17 00:00:00 2001
From: Po Lu <luangruo@yahoo.com>
Date: Mon, 15 Nov 2021 13:12:45 +0800
Subject: [PATCH] Add command to browse xwidget history

* doc/emacs/misc.texi (Embedded WebKit Widgets)
* etc/NEWS: Document `xwidget-webkit-browse-history'.

* lisp/xwidget.el (xwidget-webkit-mode-map): Bind "H" to
xwidget-webkit-browse-history.
(xwidget-webkit-import-widget): Set last session buffer correctly.
(xwidget-webkit-browse-history): New command.
(xwidget-webkit-history--session): New variable.

(xwidget-webkit-history--insert-item)
(xwidget-webkit-history-select-item)
(xwidget-webkit-history-reload): New functions.

(xwidget-webkit-history-mode): New major mode.
---
 doc/emacs/misc.texi |  8 +++++
 etc/NEWS            |  5 +++
 lisp/xwidget.el     | 77 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 90 insertions(+)

diff --git a/doc/emacs/misc.texi b/doc/emacs/misc.texi
index 3d423d7675..1f2c852fac 100644
--- a/doc/emacs/misc.texi
+++ b/doc/emacs/misc.texi
@@ -3011,6 +3011,14 @@ Embedded WebKit Widgets
 
 To leave incremental search, you can type @kbd{C-g}.
 
+@findex xwidget-webkit-browse-history
+@cindex history of webkit buffers
+  The command @code{xwidget-webkit-browse-history} displays a buffer
+containing a list of pages previously loaded by the current WebKit
+buffer, and lets you navigate to those pages by hitting @kbd{RET}.
+
+It is bound to @kbd{H}.
+
 @node Browse-URL
 @subsection  Following URLs
 @cindex World Wide Web
diff --git a/etc/NEWS b/etc/NEWS
index 312fc18f4f..d52461909f 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -486,6 +486,11 @@ This mode acts similarly to incremental search, and allows to search
 the contents of a WebKit widget.  In xwidget-webkit mode, it is bound
 to 'C-s' and 'C-r'.
 
++++
+*** New command 'xwidget-webkit-browse-history'.
+This command displays a buffer containing the page load history of
+the current WebKit widget, and allows you to navigate it.
+
 ---
 *** On X11, the WebKit inspector is now available inside xwidgets.
 To access the inspector, right click on the widget and select "Inspect
diff --git a/lisp/xwidget.el b/lisp/xwidget.el
index 485d995f41..7031769c26 100644
--- a/lisp/xwidget.el
+++ b/lisp/xwidget.el
@@ -55,6 +55,7 @@
 (declare-function delete-xwidget-view "xwidget.c" (xwidget-view))
 (declare-function get-buffer-xwidgets "xwidget.c" (buffer))
 (declare-function xwidget-query-on-exit-flag "xwidget.c" (xwidget))
+(declare-function xwidget-webkit-back-forward-list "xwidget.c" (xwidget &optional limit))
 
 (defgroup xwidget nil
   "Displaying native widgets in Emacs buffers."
@@ -194,6 +195,7 @@ xwidget-webkit-mode-map
     (define-key map "e" 'xwidget-webkit-edit-mode)
     (define-key map "\C-r" 'xwidget-webkit-isearch-mode)
     (define-key map "\C-s" 'xwidget-webkit-isearch-mode)
+    (define-key map "H" 'xwidget-webkit-browse-history)
 
     ;;similar to image mode bindings
     (define-key map (kbd "SPC")                 'xwidget-webkit-scroll-up)
@@ -228,6 +230,7 @@ nil
         ["Back" xwidget-webkit-back t]
         ["Forward" xwidget-webkit-forward t]
         ["Reload" xwidget-webkit-reload t]
+        ["History" xwidget-webkit-browse-history t]
         ["Insert String" xwidget-webkit-insert-string
          :active t
          :help "Insert a string into the currently active field"]
@@ -396,6 +399,9 @@ xwidget-webkit-callback
              (when (or (string-equal (nth 3 last-input-event)
                                      "load-finished")
                        (> (length title) 0))
+               (when-let ((buffer (get-buffer "*Xwidget WebKit History*")))
+                 (with-current-buffer buffer
+                   (revert-buffer)))
                (with-current-buffer (xwidget-buffer xwidget)
                  (setq xwidget-webkit--title title)
                  (force-mode-line-update)
@@ -775,6 +781,7 @@ xwidget-webkit-import-widget
          (callback #'xwidget-webkit-callback)
          (buffer (get-buffer-create bufname)))
     (with-current-buffer buffer
+      (setq xwidget-webkit-last-session-buffer buffer)
       (save-excursion
         (erase-buffer)
         (insert ".")
@@ -821,6 +828,15 @@ xwidget-webkit-current-url
   (let ((url (xwidget-webkit-uri (xwidget-webkit-current-session))))
     (message "URL: %s" (kill-new (or url "")))))
 
+(defun xwidget-webkit-browse-history ()
+  "Display a buffer containing the history of page loads."
+  (interactive)
+  (setq xwidget-webkit-last-session-buffer (current-buffer))
+  (let ((buffer (get-buffer-create "*Xwidget WebKit History*")))
+    (with-current-buffer buffer
+      (xwidget-webkit-history-mode))
+    (display-buffer buffer)))
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 (defun xwidget-webkit-get-selection (proc)
   "Get the webkit selection and pass it to PROC."
@@ -1059,6 +1075,67 @@ xwidget-webkit-isearch-yank-kill
         (concat xwidget-webkit-isearch--string
                 (current-kill 0)))
   (xwidget-webkit-isearch--update))
+
+(defvar-local xwidget-webkit-history--session nil
+  "The xwidget this history buffer controls.")
+
+(define-button-type 'xwidget-webkit-history 'action #'xwidget-webkit-history-select-item)
+
+(defun xwidget-webkit-history--insert-item (item)
+  "Insert specified ITEM into the current buffer."
+  (let ((idx (car item))
+        (title (cadr item))
+        (uri (caddr item)))
+    (setq tabulated-list-entries
+          (push (list idx (vector (list (number-to-string idx)
+                                        :type 'xwidget-webkit-history)
+                                  (list title :type 'xwidget-webkit-history)
+                                  (list uri :type 'xwidget-webkit-history)))
+                 tabulated-list-entries))))
+
+(defun xwidget-webkit-history-select-item (pos)
+  "Navigate to the history item underneath POS."
+  (interactive "P")
+  (let ((id (tabulated-list-get-id pos)))
+    (xwidget-webkit-goto-history xwidget-webkit-history--session id))
+  (xwidget-webkit-history-reload))
+
+(defun xwidget-webkit-history-reload (&rest ignored)
+  "Reload the current history buffer."
+  (interactive)
+  (setq tabulated-list-entries nil)
+  (let* ((back-forward-list
+          (xwidget-webkit-back-forward-list xwidget-webkit-history--session))
+         (back-list (car back-forward-list))
+         (here (cadr back-forward-list))
+         (forward-list (caddr back-forward-list)))
+    (mapc #'xwidget-webkit-history--insert-item (nreverse forward-list))
+    (xwidget-webkit-history--insert-item here)
+    (mapc #'xwidget-webkit-history--insert-item back-list)
+    (tabulated-list-print t nil)
+    (goto-char (point-min))
+    (let ((position (line-beginning-position (1+ (length back-list)))))
+      (goto-char position)
+      (setq-local overlay-arrow-position (make-marker))
+      (set-marker overlay-arrow-position position))))
+
+(define-derived-mode xwidget-webkit-history-mode tabulated-list-mode
+  "Xwidget Webkit History"
+  "Major mode for browsing the history of an Xwidget Webkit buffer.
+Each line describes an entry in history."
+  (setq truncate-lines t)
+  (setq buffer-read-only t)
+  (setq tabulated-list-format [("Index" 10 nil)
+                               ("Title" 50 nil)
+                               ("URL" 100 nil)])
+  (setq tabulated-list-entries nil)
+  (setq xwidget-webkit-history--session (xwidget-webkit-current-session))
+  (xwidget-webkit-history-reload)
+  (setq-local revert-buffer-function #'xwidget-webkit-history-reload)
+  (tabulated-list-init-header))
+
+(define-key xwidget-webkit-history-mode-map (kbd "RET")
+  #'xwidget-webkit-history-select-item)
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 (defvar xwidget-view-list)              ; xwidget.c
-- 
2.31.1


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

end of thread, other threads:[~2021-11-15 12:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <87a6i62e45.fsf.ref@yahoo.com>
2021-11-15  5:15 ` Add command to browse xwidget history Po Lu
2021-11-15  9:40   ` Robert Pluim
2021-11-15  9:58     ` Po Lu
2021-11-15 12:56   ` Eli Zaretskii
2021-11-15 12:58     ` Po Lu

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).