diff options
| author | Po Lu | 2021-11-11 09:01:38 +0800 |
|---|---|---|
| committer | Po Lu | 2021-11-14 17:45:23 +0800 |
| commit | c3f53d26043a4e4a91a3f1d140f080b6c8d190d2 (patch) | |
| tree | f10d8e8fe866db61efb07e32f490ebcca1dc344f | |
| parent | 609bc1d33ad81f9f2ffa0ff34522cfdb743d2dbb (diff) | |
| download | emacs-c3f53d26043a4e4a91a3f1d140f080b6c8d190d2.tar.gz emacs-c3f53d26043a4e4a91a3f1d140f080b6c8d190d2.zip | |
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.
| -rw-r--r-- | doc/lispref/display.texi | 33 | ||||
| -rw-r--r-- | etc/NEWS | 5 | ||||
| -rw-r--r-- | src/xwidget.c | 95 |
3 files changed, 133 insertions, 0 deletions
diff --git a/doc/lispref/display.texi b/doc/lispref/display.texi index 3ab29dc5912..dd2c6e003f4 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}, | |||
| 6971 | to be used for resolving relative links in @var{text}. | 6971 | to be used for resolving relative links in @var{text}. |
| 6972 | @end defun | 6972 | @end defun |
| 6973 | 6973 | ||
| 6974 | @defun xwidget-webkit-goto-history xwidget rel-pos | ||
| 6975 | Make @var{xwidget}, a WebKit widget, load the @var{rel-pos}th element | ||
| 6976 | in its navigation history. | ||
| 6977 | |||
| 6978 | If @var{rel-pos} is zero, the current page will be reloaded instead. | ||
| 6979 | @end defun | ||
| 6980 | |||
| 6981 | @defun xwidget-webkit-back-forward-list xwidget &optional limit | ||
| 6982 | Return the navigation history of @var{xwidget}, up to @var{limit} | ||
| 6983 | items in each direction. If not specified, @var{limit} defaults to | ||
| 6984 | 50. | ||
| 6985 | |||
| 6986 | The returned value is a list of the form @w{@code{(@var{back} | ||
| 6987 | @var{here} @var{forward})}}, where @var{here} is the current | ||
| 6988 | navigation item, while @var{back} is a list of items containing the | ||
| 6989 | items recorded by WebKit before the current navigation item, and | ||
| 6990 | @var{forward} is a list of items recorded after the current navigation | ||
| 6991 | item. @var{back}, @var{here} and @var{forward} can all be @code{nil}. | ||
| 6992 | |||
| 6993 | When @var{here} is @code{nil}, it means that no items have been | ||
| 6994 | recorded yet; if @var{back} or @var{forward} are @code{nil}, it means | ||
| 6995 | that there is no history recorded before or after the current item | ||
| 6996 | respectively. | ||
| 6997 | |||
| 6998 | Navigation items are themselves lists of the form @w{@code{(@var{idx} | ||
| 6999 | @var{title} @var{uri})}}. In these lists, @var{idx} is an index that | ||
| 7000 | can be passed to @code{xwidget-webkit-goto-history}, @var{title} is | ||
| 7001 | the human-readable title of the item, and @var{uri} is the URI of the | ||
| 7002 | item. The user should normally have no reason to load @var{uri} | ||
| 7003 | manually to reach a specific history item. Instead, @var{idx} should | ||
| 7004 | be passed as an index to @code{xwidget-webkit-goto-history}. | ||
| 7005 | @end defun | ||
| 7006 | |||
| 6974 | @node Buttons | 7007 | @node Buttons |
| 6975 | @section Buttons | 7008 | @section Buttons |
| 6976 | @cindex buttons in buffers | 7009 | @cindex buttons in buffers |
| @@ -782,6 +782,11 @@ Some new functions, such as 'xwidget-webkit-search', have been added | |||
| 782 | for performing searches on WebKit xwidgets. | 782 | for performing searches on WebKit xwidgets. |
| 783 | 783 | ||
| 784 | +++ | 784 | +++ |
| 785 | *** New function 'xwidget-webkit-back-forward-list'. | ||
| 786 | This function is used to obtain the history of page-loads in a given | ||
| 787 | WebKit xwidget. | ||
| 788 | |||
| 789 | +++ | ||
| 785 | *** 'load-changed' xwidget events are now more detailed. | 790 | *** 'load-changed' xwidget events are now more detailed. |
| 786 | In particular, they can now have different arguments based on the | 791 | In particular, they can now have different arguments based on the |
| 787 | state of the WebKit widget. 'load-finished' is sent when a load has | 792 | state of the WebKit widget. 'load-finished' is sent when a load has |
diff --git a/src/xwidget.c b/src/xwidget.c index 344016ed744..0e8bf13715f 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/>. */ | |||
| 20 | #include <config.h> | 20 | #include <config.h> |
| 21 | 21 | ||
| 22 | #include "buffer.h" | 22 | #include "buffer.h" |
| 23 | #include "coding.h" | ||
| 23 | #include "xwidget.h" | 24 | #include "xwidget.h" |
| 24 | 25 | ||
| 25 | #include "lisp.h" | 26 | #include "lisp.h" |
| @@ -2444,6 +2445,99 @@ to "about:blank". */) | |||
| 2444 | 2445 | ||
| 2445 | return Qnil; | 2446 | return Qnil; |
| 2446 | } | 2447 | } |
| 2448 | |||
| 2449 | DEFUN ("xwidget-webkit-back-forward-list", Fxwidget_webkit_back_forward_list, | ||
| 2450 | Sxwidget_webkit_back_forward_list, 1, 2, 0, | ||
| 2451 | doc: /* Return the navigation history of XWIDGET, a WebKit xwidget. | ||
| 2452 | |||
| 2453 | Return the history as a list of the form (BACK HERE FORWARD), where | ||
| 2454 | HERE is the current navigation item, while BACK and FORWARD are lists | ||
| 2455 | of history items of the form (IDX TITLE URI). Here, IDX is an index | ||
| 2456 | that can be passed to `xwidget-webkit-goto-history', TITLE is a string | ||
| 2457 | containing the human-readable title of the history item, and URI is | ||
| 2458 | the URI of the history item. | ||
| 2459 | |||
| 2460 | BACK, HERE, and FORWARD can all be nil depending on the state of the | ||
| 2461 | navigation history. | ||
| 2462 | |||
| 2463 | BACK and FORWARD will each not contain more elements than LIMIT. If | ||
| 2464 | LIMIT is not specified or nil, it is treated as `50'. */) | ||
| 2465 | (Lisp_Object xwidget, Lisp_Object limit) | ||
| 2466 | { | ||
| 2467 | struct xwidget *xw; | ||
| 2468 | Lisp_Object back, here, forward; | ||
| 2469 | WebKitWebView *webview; | ||
| 2470 | WebKitBackForwardList *list; | ||
| 2471 | WebKitBackForwardListItem *item; | ||
| 2472 | GList *parent, *tem; | ||
| 2473 | int i; | ||
| 2474 | unsigned int lim; | ||
| 2475 | Lisp_Object title, uri; | ||
| 2476 | const gchar *item_title, *item_uri; | ||
| 2477 | |||
| 2478 | back = Qnil; | ||
| 2479 | here = Qnil; | ||
| 2480 | forward = Qnil; | ||
| 2481 | |||
| 2482 | if (NILP (limit)) | ||
| 2483 | limit = make_fixnum (50); | ||
| 2484 | else | ||
| 2485 | CHECK_FIXNAT (limit); | ||
| 2486 | |||
| 2487 | CHECK_LIVE_XWIDGET (xwidget); | ||
| 2488 | xw = XXWIDGET (xwidget); | ||
| 2489 | |||
| 2490 | webview = WEBKIT_WEB_VIEW (xw->widget_osr); | ||
| 2491 | list = webkit_web_view_get_back_forward_list (webview); | ||
| 2492 | item = webkit_back_forward_list_get_current_item (list); | ||
| 2493 | lim = XFIXNAT (limit); | ||
| 2494 | |||
| 2495 | if (item) | ||
| 2496 | { | ||
| 2497 | item_title = webkit_back_forward_list_item_get_title (item); | ||
| 2498 | item_uri = webkit_back_forward_list_item_get_uri (item); | ||
| 2499 | here = list3 (make_fixnum (0), | ||
| 2500 | build_string_from_utf8 (item_title ? item_title : ""), | ||
| 2501 | build_string_from_utf8 (item_uri ? item_uri : "")); | ||
| 2502 | } | ||
| 2503 | parent = webkit_back_forward_list_get_back_list_with_limit (list, lim); | ||
| 2504 | |||
| 2505 | if (parent) | ||
| 2506 | { | ||
| 2507 | for (i = 1, tem = parent; parent; parent = parent->next, ++i) | ||
| 2508 | { | ||
| 2509 | item = tem->data; | ||
| 2510 | item_title = webkit_back_forward_list_item_get_title (item); | ||
| 2511 | item_uri = webkit_back_forward_list_item_get_uri (item); | ||
| 2512 | title = build_string_from_utf8 (item_title ? item_title : ""); | ||
| 2513 | uri = build_string_from_utf8 (item_uri ? item_uri : ""); | ||
| 2514 | back = Fcons (list3 (make_fixnum (-i), title, uri), back); | ||
| 2515 | } | ||
| 2516 | } | ||
| 2517 | |||
| 2518 | back = Fnreverse (back); | ||
| 2519 | g_list_free (parent); | ||
| 2520 | |||
| 2521 | parent = webkit_back_forward_list_get_forward_list_with_limit (list, lim); | ||
| 2522 | |||
| 2523 | if (parent) | ||
| 2524 | { | ||
| 2525 | for (i = 1, tem = parent; parent; parent = parent->next, ++i) | ||
| 2526 | { | ||
| 2527 | item = tem->data; | ||
| 2528 | item_title = webkit_back_forward_list_item_get_title (item); | ||
| 2529 | item_uri = webkit_back_forward_list_item_get_uri (item); | ||
| 2530 | title = build_string_from_utf8 (item_title ? item_title : ""); | ||
| 2531 | uri = build_string_from_utf8 (item_uri ? item_uri : ""); | ||
| 2532 | forward = Fcons (list3 (make_fixnum (i), title, uri), forward); | ||
| 2533 | } | ||
| 2534 | } | ||
| 2535 | |||
| 2536 | forward = Fnreverse (forward); | ||
| 2537 | g_list_free (parent); | ||
| 2538 | |||
| 2539 | return list3 (back, here, forward); | ||
| 2540 | } | ||
| 2447 | #endif | 2541 | #endif |
| 2448 | 2542 | ||
| 2449 | void | 2543 | void |
| @@ -2488,6 +2582,7 @@ syms_of_xwidget (void) | |||
| 2488 | defsubr (&Sset_xwidget_buffer); | 2582 | defsubr (&Sset_xwidget_buffer); |
| 2489 | #ifdef USE_GTK | 2583 | #ifdef USE_GTK |
| 2490 | defsubr (&Sxwidget_webkit_load_html); | 2584 | defsubr (&Sxwidget_webkit_load_html); |
| 2585 | defsubr (&Sxwidget_webkit_back_forward_list); | ||
| 2491 | #endif | 2586 | #endif |
| 2492 | defsubr (&Skill_xwidget); | 2587 | defsubr (&Skill_xwidget); |
| 2493 | 2588 | ||