EWW: Fallback to next numerical URL

master
Pierre Neidhardt 2018-03-24 20:03:40 +05:30
parent 8ee705b1de
commit 082656530a
2 changed files with 39 additions and 0 deletions

View File

@ -218,4 +218,11 @@
(evil-define-key '(normal motion) emms-browser-mode-map (kbd "<return>") 'ambrevar/emms-browser-add-tracks-and-maybe-play))))
(add-hook 'evil-collection-setup-hook 'ambrevar/evil-emms)
;; EWW
(defun ambrevar/evil-eww (mode _mode-keymaps &rest _rest)
(when (eq mode 'eww)
(evil-define-key 'normal eww-mode-map "[" 'ambrevar/eww-previous-url)
(evil-define-key 'normal eww-mode-map "]" 'ambrevar/eww-next-url)))
(add-hook 'evil-collection-setup-hook 'ambrevar/evil-eww)
(provide 'init-evil)

View File

@ -3,6 +3,38 @@
(setq eww-bookmarks-directory "~/personal/bookmarks"
eww-download-directory "~/temp")
(defun ambrevar/eww-next-url (&optional backward)
"Like `eww-next-url' but if no next URL is found, go to next URL numerically.
The URL index is the last number after the last '/'."
(interactive)
(condition-case nil
(if backward
(eww-previous-url)
(eww-next-url))
(user-error
(when (eq major-mode 'eww-mode)
(require 'rx)
(let* ((url (plist-get eww-data :url))
(re (rx (group (one-or-more digit))
(zero-or-more (not (any "/")))
line-end)))
(if (and (string-match re url)
(or (not backward)
(> (string-to-number (match-string 1 url)) 0)))
(eww
(replace-regexp-in-string
re
(format (format "%%0.%dd" (length (match-string 1 url))) ; In case matched number is zero-padded.
(funcall (if backward '1- '1+) (string-to-number (match-string 1 url))))
url nil nil 1))
(message "No index in URL.")))))))
(defun ambrevar/eww-previous-url ()
"Like `eww-previous-url' but if no next URL is found, go to next URL numerically.
The URL index is the last number after the last '/'."
(interactive)
(ambrevar/eww-next-url 'backward))
(defun ambrevar/eww-switch-back ()
"Switch to the *eww* buffer."
(interactive)