From 6ed7454f17c2af7c489d18bfe82563ebf5ff7a5a Mon Sep 17 00:00:00 2001 From: Po Lu Date: Fri, 5 Nov 2021 14:47:26 +0800 Subject: [PATCH] Add xwidget-webkit-edit-mode to make using the WebKit browser easier * doc/emacs/misc.texi: Document changes. * etc/NEWS: Document recent changes. * lisp/xwidget.el (xwidget-webkit-pass-command-event): New variable. * lisp/xwidget.el (xwidget-perform-lispy-event): New function declaration. * lisp/xwidget.el (xwidget-webkit-edit-mode): New minor mode. --- doc/emacs/misc.texi | 8 ++++++++ etc/NEWS | 15 +++++++++++++++ lisp/xwidget.el | 46 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/doc/emacs/misc.texi b/doc/emacs/misc.texi index 7f91e1c188e..daed242da12 100644 --- a/doc/emacs/misc.texi +++ b/doc/emacs/misc.texi @@ -2953,6 +2953,14 @@ one-key commands for scrolling the widget, changing its size, and reloading it. Type @w{@kbd{C-h b}} in that buffer to see the key bindings. +@findex xwidget-webkit-edit-mode +@cindex xwidget-webkit-edit-mode + By default, typing a self-inserting character inside an xwidget +webkit buffer will do nothing, or trigger some special action. To +make those characters and other common editing keys insert themselves +when pressed, you can enable @code{xwidget-webkit-edit-mode}, which +redefines them to be passed through to the WebKit xwidget. + @node Browse-URL @subsection Following URLs @cindex World Wide Web diff --git a/etc/NEWS b/etc/NEWS index a50229916fd..6654e54552f 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -495,6 +495,13 @@ the buffer will take you to that directory. This is a convenience function to extract the field data from 'exif-parse-file' and 'exif-parse-buffer'. +** Xwidgets + +*** New minor mode `xwidget-webkit-edit-mode'. +When this mode is enabled, self-inserting characters and other common +web browser shotcut keys are redefined to send themselves to the +WebKit widget. + * New Modes and Packages in Emacs 29.1 @@ -719,6 +726,14 @@ an exact match, then the lowercased '[menu-bar foo\ bar]' and finally '[menu-bar foo-bar]'. This further improves backwards-compatibility when converting menus to use 'easy-menu-define'. ++++ +** New function `xwidget-perform-lispy-event'. +This function allows you to send events to xwidgets. Usually, some +equivalent of the event will be sent, but there is no guarantee of +what the widget will actually receive. + +On GTK+, only key and function key events are implemented. + * Changes in Emacs 29.1 on Non-Free Operating Systems diff --git a/lisp/xwidget.el b/lisp/xwidget.el index 8c593abea88..babab63d74f 100644 --- a/lisp/xwidget.el +++ b/lisp/xwidget.el @@ -124,6 +124,14 @@ in `split-window-right' with a new xwidget webkit session." (with-selected-window (split-window-right) (xwidget-webkit-new-session url)))) +(declare-function xwidget-perform-lispy-event "xwidget.c") + +(defun xwidget-webkit-pass-command-event () + "Pass `last-command-event' to the current buffer's WebKit widget." + (interactive) + (xwidget-perform-lispy-event (xwidget-webkit-current-session) + last-command-event)) + ;;todo. ;; - check that the webkit support is compiled in (defvar xwidget-webkit-mode-map @@ -684,6 +692,44 @@ You can retrieve the value with `xwidget-get'." (set-xwidget-plist xwidget (plist-put (xwidget-plist xwidget) propname value))) +(defvar xwidget-webkit-edit-mode-map (make-keymap)) + +(define-key xwidget-webkit-edit-mode-map [backspace] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [tab] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [left] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [right] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [up] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [down] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [return] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [C-left] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [C-right] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [C-up] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [C-down] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [C-return] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [S-left] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [S-right] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [S-up] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [S-down] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [S-return] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [M-left] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [M-right] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [M-up] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [M-down] 'xwidget-webkit-pass-command-event) +(define-key xwidget-webkit-edit-mode-map [M-return] 'xwidget-webkit-pass-command-event) + +(define-minor-mode xwidget-webkit-edit-mode + "Minor mode for editing the content of WebKit buffers. + +This defines most self-inserting characters and some common +keyboard shortcuts to `xwidget-webkit-pass-command-event', which +will pass the key events corresponding to these characters to the +WebKit widget." + :keymap xwidget-webkit-edit-mode-map) + +(substitute-key-definition 'self-insert-command + 'xwidget-webkit-pass-command-event + xwidget-webkit-edit-mode-map + global-map) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -- 2.39.5