emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master c3f53d2: Expose xwidget navigation history to Lisp code


From: Po Lu
Subject: master c3f53d2: Expose xwidget navigation history to Lisp code
Date: Sun, 14 Nov 2021 04:46:10 -0500 (EST)

branch: master
commit c3f53d26043a4e4a91a3f1d140f080b6c8d190d2
Author: Po Lu <luangruo@yahoo.com>
Commit: Po Lu <luangruo@yahoo.com>

    Expose xwidget navigation history to Lisp code
    
    * doc/lispref/display.texi (Xwidgets): Document changes.
    * etc/NEWS: Announce new function.
    * src/xwidget.c (Fxwidget_webkit_back_forward_list): New
    function.
    (syms_of_xwidget): Define new subr.
---
 doc/lispref/display.texi | 33 +++++++++++++++++
 etc/NEWS                 |  5 +++
 src/xwidget.c            | 95 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 133 insertions(+)

diff --git a/doc/lispref/display.texi b/doc/lispref/display.texi
index 3ab29dc..dd2c6e0 100644
--- a/doc/lispref/display.texi
+++ b/doc/lispref/display.texi
@@ -6971,6 +6971,39 @@ the absolute location of the web resources referenced by 
@var{text},
 to be used for resolving relative links in @var{text}.
 @end defun
 
+@defun xwidget-webkit-goto-history xwidget rel-pos
+Make @var{xwidget}, a WebKit widget, load the @var{rel-pos}th element
+in its navigation history.
+
+If @var{rel-pos} is zero, the current page will be reloaded instead.
+@end defun
+
+@defun xwidget-webkit-back-forward-list xwidget &optional limit
+Return the navigation history of @var{xwidget}, up to @var{limit}
+items in each direction.  If not specified, @var{limit} defaults to
+50.
+
+The returned value is a list of the form @w{@code{(@var{back}
+@var{here} @var{forward})}}, where @var{here} is the current
+navigation item, while @var{back} is a list of items containing the
+items recorded by WebKit before the current navigation item, and
+@var{forward} is a list of items recorded after the current navigation
+item.  @var{back}, @var{here} and @var{forward} can all be @code{nil}.
+
+When @var{here} is @code{nil}, it means that no items have been
+recorded yet; if @var{back} or @var{forward} are @code{nil}, it means
+that there is no history recorded before or after the current item
+respectively.
+
+Navigation items are themselves lists of the form @w{@code{(@var{idx}
+@var{title} @var{uri})}}.  In these lists, @var{idx} is an index that
+can be passed to @code{xwidget-webkit-goto-history}, @var{title} is
+the human-readable title of the item, and @var{uri} is the URI of the
+item.  The user should normally have no reason to load @var{uri}
+manually to reach a specific history item.  Instead, @var{idx} should
+be passed as an index to @code{xwidget-webkit-goto-history}.
+@end defun
+
 @node Buttons
 @section Buttons
 @cindex buttons in buffers
diff --git a/etc/NEWS b/etc/NEWS
index c362e56..312fc18 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -782,6 +782,11 @@ Some new functions, such as 'xwidget-webkit-search', have 
been added
 for performing searches on WebKit xwidgets.
 
 +++
+*** New function 'xwidget-webkit-back-forward-list'.
+This function is used to obtain the history of page-loads in a given
+WebKit xwidget.
+
++++
 *** 'load-changed' xwidget events are now more detailed.
 In particular, they can now have different arguments based on the
 state of the WebKit widget.  'load-finished' is sent when a load has
diff --git a/src/xwidget.c b/src/xwidget.c
index 344016e..0e8bf13 100644
--- a/src/xwidget.c
+++ b/src/xwidget.c
@@ -20,6 +20,7 @@ along with GNU Emacs.  If not, see 
<https://www.gnu.org/licenses/>.  */
 #include <config.h>
 
 #include "buffer.h"
+#include "coding.h"
 #include "xwidget.h"
 
 #include "lisp.h"
@@ -2444,6 +2445,99 @@ to "about:blank".  */)
 
   return Qnil;
 }
+
+DEFUN ("xwidget-webkit-back-forward-list", Fxwidget_webkit_back_forward_list,
+       Sxwidget_webkit_back_forward_list, 1, 2, 0,
+       doc: /* Return the navigation history of XWIDGET, a WebKit xwidget.
+
+Return the history as a list of the form (BACK HERE FORWARD), where
+HERE is the current navigation item, while BACK and FORWARD are lists
+of history items of the form (IDX TITLE URI).  Here, IDX is an index
+that can be passed to `xwidget-webkit-goto-history', TITLE is a string
+containing the human-readable title of the history item, and URI is
+the URI of the history item.
+
+BACK, HERE, and FORWARD can all be nil depending on the state of the
+navigation history.
+
+BACK and FORWARD will each not contain more elements than LIMIT.  If
+LIMIT is not specified or nil, it is treated as `50'.  */)
+  (Lisp_Object xwidget, Lisp_Object limit)
+{
+  struct xwidget *xw;
+  Lisp_Object back, here, forward;
+  WebKitWebView *webview;
+  WebKitBackForwardList *list;
+  WebKitBackForwardListItem *item;
+  GList *parent, *tem;
+  int i;
+  unsigned int lim;
+  Lisp_Object title, uri;
+  const gchar *item_title, *item_uri;
+
+  back = Qnil;
+  here = Qnil;
+  forward = Qnil;
+
+  if (NILP (limit))
+    limit = make_fixnum (50);
+  else
+    CHECK_FIXNAT (limit);
+
+  CHECK_LIVE_XWIDGET (xwidget);
+  xw = XXWIDGET (xwidget);
+
+  webview = WEBKIT_WEB_VIEW (xw->widget_osr);
+  list = webkit_web_view_get_back_forward_list (webview);
+  item = webkit_back_forward_list_get_current_item (list);
+  lim = XFIXNAT (limit);
+
+  if (item)
+    {
+      item_title = webkit_back_forward_list_item_get_title (item);
+      item_uri = webkit_back_forward_list_item_get_uri (item);
+      here = list3 (make_fixnum (0),
+                   build_string_from_utf8 (item_title ? item_title : ""),
+                   build_string_from_utf8 (item_uri ? item_uri : ""));
+    }
+  parent = webkit_back_forward_list_get_back_list_with_limit (list, lim);
+
+  if (parent)
+    {
+      for (i = 1, tem = parent; parent; parent = parent->next, ++i)
+       {
+         item = tem->data;
+         item_title = webkit_back_forward_list_item_get_title (item);
+         item_uri = webkit_back_forward_list_item_get_uri (item);
+         title = build_string_from_utf8 (item_title ? item_title : "");
+         uri = build_string_from_utf8 (item_uri ? item_uri : "");
+         back = Fcons (list3 (make_fixnum (-i), title, uri), back);
+       }
+    }
+
+  back = Fnreverse (back);
+  g_list_free (parent);
+
+  parent = webkit_back_forward_list_get_forward_list_with_limit (list, lim);
+
+  if (parent)
+    {
+      for (i = 1, tem = parent; parent; parent = parent->next, ++i)
+       {
+         item = tem->data;
+         item_title = webkit_back_forward_list_item_get_title (item);
+         item_uri = webkit_back_forward_list_item_get_uri (item);
+         title = build_string_from_utf8 (item_title ? item_title : "");
+         uri = build_string_from_utf8 (item_uri ? item_uri : "");
+         forward = Fcons (list3 (make_fixnum (i), title, uri), forward);
+       }
+    }
+
+  forward = Fnreverse (forward);
+  g_list_free (parent);
+
+  return list3 (back, here, forward);
+}
 #endif
 
 void
@@ -2488,6 +2582,7 @@ syms_of_xwidget (void)
   defsubr (&Sset_xwidget_buffer);
 #ifdef USE_GTK
   defsubr (&Sxwidget_webkit_load_html);
+  defsubr (&Sxwidget_webkit_back_forward_list);
 #endif
   defsubr (&Skill_xwidget);
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]