summaryrefslogtreecommitdiff
path: root/guix/kolwynia/home/bdunahu/files/.config/emacs/modules/bd--browse.el
diff options
context:
space:
mode:
Diffstat (limited to 'guix/kolwynia/home/bdunahu/files/.config/emacs/modules/bd--browse.el')
-rw-r--r--guix/kolwynia/home/bdunahu/files/.config/emacs/modules/bd--browse.el146
1 files changed, 146 insertions, 0 deletions
diff --git a/guix/kolwynia/home/bdunahu/files/.config/emacs/modules/bd--browse.el b/guix/kolwynia/home/bdunahu/files/.config/emacs/modules/bd--browse.el
new file mode 100644
index 0000000..a1b185f
--- /dev/null
+++ b/guix/kolwynia/home/bdunahu/files/.config/emacs/modules/bd--browse.el
@@ -0,0 +1,146 @@
+;;; -*- lexical-binding: t; -*-
+;;; Commentary:
+;;; Code:
+
+(require 'selector)
+(require 'dash)
+(require 'fill-column)
+
+(defun bd/browse (url &optional pref &rest _)
+ "Given PREF, launches URL in an external browser or eww."
+ (interactive)
+ (pcase pref
+ ('eww (eww url))
+ ('tor (start-process "torbrowser" nil "torbrowser" "--new-window" url))
+ ('chromium (start-process "chromium" nil "chromium" "--new-window" url))
+ (_ (start-process "browser" nil (getenv "BROWSER") "--new-window" url))))
+
+(defun bd/selector-bookmarks ()
+ "Selector source for all bookmarks."
+ (selector-source-create
+ "Bookmarks"
+ :candidates
+ (-map
+ (lambda (b) (selector-candidate-create (car b) :value (cdr b)))
+ bd/bookmarks)
+ :actions
+ (list (lambda (x) (apply #'bd/browse x)))))
+
+(defmacro bd/search-candidate (name url pref)
+ "Syntax for a search candidate given NAME, URL, and PREF."
+ `(selector-candidate-create
+ ,(concat "Search " name)
+ :type 'dummy
+ :action (lambda (_) (browse-url (concat ,url (selector-input)) ,pref))))
+
+(defun rip-video (url)
+ "Play URL (or search string) in mpv."
+ (message "Ludu %s" url)
+ (start-process "rip" nil
+ "mpv" "--force-window=yes"
+ (concat (if (string-match "https://.*" url)
+ "ytdl://"
+ "ytdl://ytsearch:") url)))
+
+(defun rip-html (url)
+ "Open an HTML document in an emacs org buffer."
+ (with-current-buffer-window url
+ '((display-buffer-same-window))
+ nil
+ (princ
+ (shell-command-to-string
+ (concat "curl --silent " url " | pandoc --from=html --to=org --standalone")))
+ (org-mode)))
+
+(defun bd/selector-rip-video ()
+ "Selector source for streaming a video off of youtube."
+ (selector-candidate-create
+ "Search Immediate"
+ :type 'dummy
+ :action (lambda (_) (rip-video (selector-input)))))
+
+(defun bd/selector-search ()
+ "Selector source for all search engines."
+ (selector-source-create
+ "Browser"
+ :candidates
+ (list (bd/search-candidate "DuckDuckGo" "https://duckduckgo.com/html/?q=" 'wolf)
+ (bd/search-candidate "SearXNG" "https://searx.operationnull.com/searxng/search?q=" 'wolf)
+ (bd/search-candidate "SearXNG-E" "https://searx.operationnull.com/searxng/search?q=" 'eww)
+ (bd/search-candidate "Wikipedia" "https://en.wikipedia.org/w/index.php?search=" 'eww)
+ (bd/search-candidate "Invidious" "https://inv.nadeko.net/search?q=" 'eww)
+ (bd/search-candidate "Urban Dictionary" "https://www.urbandictionary.com/define.php?term=" 'wolf)
+ (bd/search-candidate "Nethack Wiki" "https://nethackwiki.com/w/index.php?search=" 'eww)
+ (bd/search-candidate "Archive of Our Own" "https://archiveofourown.org/works/search?work_search%5Bquery%5D=" 'eww)
+ (bd/search-candidate "Archwiki" "https://wiki.archlinux.org/index.php?title=Special%3ASearch&search=" 'eww)
+
+ (bd/selector-rip-video)
+ (bd/search-candidate "Torbrowser" "" 'tor)
+ (bd/search-candidate "Librewolf" "" 'wolf))))
+
+(defun bd/browse-dispatcher ()
+ "Select and `browse-url' a bookmark or search feature."
+ (interactive)
+ (unwind-protect
+ (selector
+ (list (bd/selector-bookmarks)
+ (bd/selector-search)))))
+
+(setopt browse-url-handlers
+ `((,(regexp-opt '("youtube.com" "youtu.be" "vid.puffyan.us" "deezer.page" "deezer.com")) .
+ (lambda (url &rest _) (rip-video url))))
+ url-privacy-level '(email os emacs lastloc cookies))
+
+(use-package elpher
+ :bind
+ (:map elpher-mode-map
+ ("l" . #'elpher-back)
+ ("d" . #'elpher-download)
+ ("w" . #'elpher-copy-current-url)
+ ("A" . #'elpher-copy-link-url)
+ ("E" . #'elpher-bookmark-current)
+ ("TAB" . #'elpher-next-link)
+ ("g" . #'elpher-reload)
+ ("G" . #'elpher-go))
+ :config
+ (defun bd/elpher (original url &optional new-window)
+ "Handle gemini links."
+ (cond ((string-match-p "\\`\\(gemini\\|gopher\\)://" url)
+ (elpher-go url))
+ (t (funcall original url new-window))))
+ (advice-add 'eww :around 'bd/elpher)
+ (setopt elpher-default-url-type "gemini"
+ elpher-connection-timeout 120
+ elpher-gemini-max-fill-width 85
+ elpher-use-emacs-bookmark-menu t))
+
+(use-package eww
+ :defer 1
+ :bind
+ (:map eww-mode-map
+ ("i" . eww-toggle-images)
+ ("o" . (lambda () (interactive) (rip-html (eww-current-url)))))
+ :hook
+ ;; eww-mode by default sets this as local var to eww-browse-url
+ ((eww-mode .
+ (lambda ()
+ (setq-local browse-url-browser-function #'bd/browse)))
+ (eww-after-render . eww-readable))
+ :config
+ (setopt eww-search-prefix "https://searx.operationnull.com/searxng/search?q="
+ eww-auto-rename-buffer 'title
+ eww-browse-url-new-window-is-tab nil
+ browse-url-browser-function 'bd/browse
+ browse-url-secondary-browser-function #'browse-url-default-browser
+ eww-header-line-format ""
+ eww-use-browse-url (regexp-opt '("mailto:"
+ "youtube.com"
+ "youtu.be"))
+ shr-use-fonts nil
+ shr-inhibit-images t
+ shr-cookie-policy nil
+ shr-max-width 90))
+
+
+(provide 'bd--browse)
+;;; bd--browse.el ends here