>From d487e2689212d3dab108e043d49a8aa1787a7015 Mon Sep 17 00:00:00 2001 From: Po Lu Date: Tue, 9 Nov 2021 20:11:05 +0800 Subject: [PATCH] Expose xwidget navigation history to Lisp code * doc/lispref/display.texi (Xwidgets): Document changes. * etc/NEWS: Announce new function. * src/xwidget.c (Fxwidget_webkit_goto_history): Remove obsolete test. (Fxwidget_webkit_back_forward_list): New function. (syms_of_xwidget): Define new subr. --- doc/lispref/display.texi | 26 ++++++++++ etc/NEWS | 5 ++ src/xwidget.c | 102 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 130 insertions(+), 3 deletions(-) diff --git a/doc/lispref/display.texi b/doc/lispref/display.texi index 3ccc755391..a953162ae9 100644 --- a/doc/lispref/display.texi +++ b/doc/lispref/display.texi @@ -6953,6 +6953,32 @@ Xwidgets @var{base-uri}, which defaults to @samp{about:blank}. @end defun +@defun xwidget-webkit-goto-history xwidget rel-pos +Make @var{xwidget}, a WebKit widget, load the @code{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 @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 history behind the +current navigation item, and @var{forward} is a list of items in front +of the current navigation item. @var{back}, @var{here} and +@var{forward} can all be @code{nil}. + +Navigation items are themselves lists of the form @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. +@end defun + @node Buttons @section Buttons @cindex buttons in buffers diff --git a/etc/NEWS b/etc/NEWS index 93c161025b..82e8aff1b4 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -797,6 +797,11 @@ On GTK+, only key and function key events are implemented. This function is used to load HTML text into WebKit xwidgets, without having to create a temporary file or URL. ++++ +*** New function 'xwidget-webkit-back-forward-list'. +This function is used to obtain the history of page-loads in a given +WebKit xwidget. + +++ *** New functions for performing searches on WebKit xwidgets. Some new functions, such as 'xwidget-webkit-search', have been added diff --git a/src/xwidget.c b/src/xwidget.c index 22893bfe2e..c731f847c3 100644 --- a/src/xwidget.c +++ b/src/xwidget.c @@ -1617,9 +1617,7 @@ DEFUN ("xwidget-webkit-goto-history", (Lisp_Object xwidget, Lisp_Object rel_pos) { WEBKIT_FN_INIT (); - /* Should be one of -1, 0, 1 */ - if (XFIXNUM (rel_pos) < -1 || XFIXNUM (rel_pos) > 1) - args_out_of_range_3 (rel_pos, make_fixnum (-1), make_fixnum (1)); + CHECK_FIXNUM (rel_pos); #ifdef USE_GTK WebKitWebView *wkwv = WEBKIT_WEB_VIEW (xw->widget_osr); @@ -2176,6 +2174,103 @@ DEFUN ("xwidget-webkit-load-html", Fxwidget_webkit_load_html, 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. + +The history is returned as a list of the form (BACK HERE FORWARD), +where HERE is the current navigation item, while BACK and FORWARD are +lists of navigation items, which are lists 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. The user should normally have no reason to load URI +manually to reach a specific history item. Instead, he should pass +IDX as an index to `xwidget-webkit-goto-history'. + +BACK, HERE, and FORWARD can all be nil if there are no items in the +navigation history. + +BACK and FORWARD will 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; +#ifdef USE_GTK + WebKitWebView *webview; + WebKitBackForwardList *list; + WebKitBackForwardListItem *item; + GList *parent, *tem; + int i; + unsigned int lim; + Lisp_Object title, uri; + const gchar *item_title, *item_uri; +#endif + + back = Qnil; + here = Qnil; + forward = Qnil; + + if (NILP (limit)) + limit = make_fixnum (50); + + CHECK_FIXNAT (limit); + CHECK_XWIDGET (xwidget); + xw = XXWIDGET (xwidget); + +#ifdef USE_GTK + 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 (item_title ? item_title : ""), + build_string (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 (item_title ? item_title : ""); + uri = build_string (item_uri ? item_uri : ""); + back = Fcons (list3 (make_fixnum (-i), title, uri), 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 (item_title ? item_title : ""); + uri = build_string (item_uri ? item_uri : ""); + forward = Fcons (list3 (make_fixnum (i), title, uri), forward); + } + } + + g_list_free (parent); +#endif + + return list3 (back, here, forward); +} + void syms_of_xwidget (void) { @@ -2215,6 +2310,7 @@ syms_of_xwidget (void) defsubr (&Sxwidget_webkit_previous_result); defsubr (&Sset_xwidget_buffer); defsubr (&Sxwidget_webkit_load_html); + defsubr (&Sxwidget_webkit_back_forward_list); DEFSYM (QCxwidget, ":xwidget"); DEFSYM (QCtitle, ":title"); -- 2.31.1