xwidget: mimic vimium

This commit is contained in:
condy 2022-01-14 04:31:29 +08:00 committed by Youmu
parent a8a12c6290
commit 3f5ff30b0e
1 changed files with 101 additions and 16 deletions

View File

@ -1,6 +1,6 @@
;;; evil-collection-xwidget.el --- Evil bindings for Xwidget -*- lexical-binding: t -*-
;; Copyright (C) 2020 Ruslan Kamashev
;; Copyright (C) 2020, 2022 Ruslan Kamashev
;; Author: Ruslan Kamashev <rynffoll@gmail.com>
;; Maintainer: James Nguyen <james@jojojames.com>
@ -32,28 +32,113 @@
(defvar evil-collection-xwidget-maps '(xwidget-webkit-mode-map))
;; Silence compile errors
(declare-function xwidget-window-inside-pixel-height "xwidget")
;; HACK: elisp byte-opt can't optimize 'format' out, so it's checked at runtime.
(defun evil-collection-xwidget-webkit--scroll-trampoline (f n)
"Compatible wrapper for 'xwidget-webkit-scroll-up' and so on.
F is the name of function, N is the pixel height."
(let ((realf (intern (format "%s" f))))
(if (>= emacs-major-version 28)
(funcall realf n)
(funcall realf))))
(defun evil-collection-xwidget-webkit-scroll-half-page-up ()
"Compatible wrapper for 'xwidget-webkit-scroll-up'."
(interactive)
(evil-collection-xwidget-webkit--scroll-trampoline
"xwidget-webkit-scroll-up"
(/ (xwidget-window-inside-pixel-height (selected-window)) 2)))
(defun evil-collection-xwidget-webkit-scroll-half-page-down ()
"Compatible wrapper for 'xwidget-webkit-scroll-down'."
(interactive)
(evil-collection-xwidget-webkit--scroll-trampoline
"xwidget-webkit-scroll-down"
(/ (xwidget-window-inside-pixel-height (selected-window)) 2)))
(defun evil-collection-xwidget-webkit-new-tab (url)
"New tab (new buffer) of URL."
(interactive "sxwidget-webkit URL: ")
(xwidget-webkit-browse-url url :new-session))
(defun evil-collection-xwidget-webkit-close-tab ()
"Close tab (kill buffer) without confirmation."
(interactive)
(let ((kill-buffer-query-functions nil))
(kill-this-buffer)))
(defun evil-collection-xwidget-webkit-search-tabs ()
"Search tabs (buffers) with 'buffer-name'."
(interactive)
(cl-flet ((is-xwidget-webkit-buffer? (b) (with-current-buffer b
(equal major-mode 'xwidget-webkit-mode))))
(let ((bufs (cl-loop for b in (buffer-list)
when (is-xwidget-webkit-buffer? b)
collect (buffer-name b))))
(switch-to-buffer (completing-read "Go to: " bufs nil t)))))
;;;###autoload
(defun evil-collection-xwidget-setup ()
"Set up `evil' bindings for `xwidget'."
(evil-collection-set-readonly-bindings 'xwidget-webkit-mode-map)
(evil-collection-define-key 'normal 'xwidget-webkit-mode-map
"q" 'quit-window
"k" 'xwidget-webkit-scroll-down-line
;; Mimic vimium (a browser extension)
;;
;; Since xwidget has no tab concept, 'open-url-new-tab' etc are not
;; supported.
;;
;; TODO: edit-mode, visual-mode
;;
;; Navigating the page
"j" 'xwidget-webkit-scroll-up-line
"h" 'xwidget-webkit-scroll-backward
"l" 'xwidget-webkit-scroll-forward
(kbd "C-f") 'xwidget-webkit-scroll-up
(kbd "C-b") 'xwidget-webkit-scroll-down
"+" 'xwidget-webkit-zoom-in
"=" 'xwidget-webkit-zoom-in
"-" 'xwidget-webkit-zoom-out
"R" 'xwidget-webkit-reload
"gr" 'xwidget-webkit-reload
"H" 'xwidget-webkit-back
"L" 'xwidget-webkit-forward
"gu" 'xwidget-webkit-browse-url
"k" 'xwidget-webkit-scroll-down-line
"gg" 'xwidget-webkit-scroll-top
"G" 'xwidget-webkit-scroll-bottom
"y" 'xwidget-webkit-copy-selection-as-kill)
"d" 'evil-collection-xwidget-webkit-scroll-half-page-up
"u" 'evil-collection-xwidget-webkit-scroll-half-page-down
"h" 'xwidget-webkit-scroll-backward
"l" 'xwidget-webkit-scroll-forward
"r" 'xwidget-webkit-reload
"yy" 'xwidget-webkit-current-url
;; TODO 'first-text-input', 'open-link-in-current-tab'
"gi" 'undefined
"f" 'undefined
;; Using the vomnibar
;;
;; For bookmark, use C-x r m, C-x r j.
"o" 'xwidget-webkit-browse-url
"T" 'evil-collection-xwidget-webkit-search-tabs
;; Using find
;;
;; TODO
;; Navigating history
"H" 'xwidget-webkit-back
"L" 'xwidget-webkit-forward
;; Manipulating tabs (actually buffers)
;;
;; Only 'new-tab' and 'close-tab' are supported.
"t" 'evil-collection-xwidget-webkit-new-tab
"x" 'evil-collection-xwidget-webkit-close-tab
;; Miscellaneous
"?" 'describe-mode
;; Evil style
"gr" 'xwidget-webkit-reload
(kbd "C-f") 'xwidget-webkit-scroll-up
(kbd "C-b") 'xwidget-webkit-scroll-down
(kbd "RET") 'xwidget-webkit-insert-string
;; zoom
"+" 'xwidget-webkit-zoom-in
"=" 'xwidget-webkit-zoom-in
"-" 'xwidget-webkit-zoom-out)
(when evil-want-C-d-scroll
(evil-collection-define-key 'normal 'xwidget-webkit-mode-map