summary refs log tree commit diff stats
path: root/lisp/+browse-url.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/+browse-url.el')
-rw-r--r--lisp/+browse-url.el156
1 files changed, 0 insertions, 156 deletions
diff --git a/lisp/+browse-url.el b/lisp/+browse-url.el deleted file mode 100644 index fc479e4..0000000 --- a/lisp/+browse-url.el +++ /dev/null
@@ -1,156 +0,0 @@
1;;; +browse-url.el -*- lexical-binding: t; -*-
2
3;;; Code:
4
5(require 'browse-url)
6(require 'cl-lib)
7
8(defgroup +browse-url nil
9 "Group for my `browse-url' extras."
10 :group 'browse-url)
11
12;;; URL Handlers
13
14(defun +browse-url-set-handlers (&optional handlers)
15 "Set HANDLERS for `browse-url'.
16Set `browse-url-handlers', if they exist; else
17`browse-url-browser-function'. The reason for this switch is
18that the latter is deprecated in Emacs 28+.
19
20If HANDLERS is absent or nil, recompute handlers. This can be
21useful when changing the default browser."
22 (let ((h (if (boundp 'browse-url-handlers)
23 'browse-url-handlers
24 'browse-url-browser-function)))
25 (set-default h (or handlers (symbol-value h)))))
26
27(cl-defmacro +browse-url-make-external-viewer-handler
28 (viewer default-args &optional (prompt "URL: ")
29 &key
30 (custom-group '+browse-url)
31 (name (format "+browse-url-with-%s" viewer))
32 (fallback #'browse-url-generic))
33 "Create a `browse-url' handler function that calls VIEWER on the url.
34Also create a `customize' setting in CUSTOM-GROUP for VIEWER's
35arguments. DEFAULT-ARGS specifies the default arguments that
36setting should have. PROMPT will be shown to user in the
37function's `interactive' spec, as an argument to
38`browse-url-interactive-arg'. The resulting function will be
39named NAME, defaulting to \"+browse-url-with-VIEWER\", and the variable
40\"NAME-args\".
41
42If FALLBACK is non-nil, it's a function to fallback on if the
43`start-process' call fails in anyway."
44 (declare (indent 1))
45 `(progn
46 (defcustom ,(intern (format "%s-args" name))
47 ,default-args
48 ,(format "Arguments to pass to %s in `%s'." viewer name)
49 :type '(repeat :tag "Command-line argument" string)
50 :group ',custom-group)
51 (defun ,(intern name) (url &optional new-window)
52 ,(format "Open URL in %s." viewer)
53 (interactive (browse-url-interactive-arg ,prompt))
54 (let* ((url (browse-url-encode-url url))
55 (process-environment (browse-url-process-environment)))
56 (message ,(format "Opening %%s in %s..." viewer) url)
57 (unless (ignore-errors
58 (apply #'start-process
59 (concat ,viewer " " url) nil
60 ,viewer
61 (append ,(intern (format "%s-args" name))
62 (list url))))
63 (funcall fallback url new-window))))))
64
65;; Reference implementation: mpv
66(+browse-url-make-external-viewer-handler "mpv" '("--cache-pause-wait=30"
67 "--cache-pause-initial=yes")
68 "Video URL: ")
69;; And feh too
70(+browse-url-make-external-viewer-handler "feh" '("--auto-zoom"
71 "--geometry" "800x600"))
72;; And ... mpv, but for images
73(+browse-url-make-external-viewer-handler "mpv"
74 '("--image-display-duration=inf")
75 "Image URL: "
76 :name "+browse-image-with-mpv")
77
78;;; Easily add extra domains to open in `browse-url-secondary-browser-function'
79;; I like to open most websites in eww, but a lot of website on the modern web
80;; just make that hard to do. Right now I have a list in `browse-url-handlers'
81;; with domains in an (rx (or ...)) form, but that's not super easy to config.
82;; With this custom setting, I'm making it a list that'll be way easier to
83;; customize.
84
85(defcustom +browse-url-secondary-browser-regexps nil
86 "List of URL regexps to open with `browse-url-secondary-browser-function'."
87 :type '(repeat regexp))
88
89;; Because `browse-url-browser-function', when set to an alist, must be of the
90;; form (REGEXP . FUNCTION), I need to convert
91;; `+browse-url-secondary-browser-regexps' into a regexp.
92
93(defun +browse-url-secondary-browser-regexps-combine ()
94 "Combine `+browse-url-secondary-browser-regexps'.
95This combines a list of regexps into one regexp."
96 (mapconcat #'identity +browse-url-secondary-browser-regexps "\\\|"))
97
98;;; URL Transformation Functions
99;; There's a lot of bad websites out there. Luckily we can easily redirect
100;; requests to more privacy-respecting, or just less javascript-ridden, sites
101;; using some basic regex magic. Inspired by add-ons like
102;; https://einaregilsson.com/redirector/.
103
104(defcustom +browse-url-transformations nil
105 "Transformation rules for various URLs.
106This is an alist, the keys of which are regexen to match URLs
107against, and the values are how to transform them. Match capture
108data will be used in the transformations."
109 :type
110 '(alist :key-type (string :tag "URL regex match")
111 :value-type (string :tag "URL regex transformation"))
112 :group '+browse-url)
113
114(defun +browse-url-transform-advice (url &rest args)
115 "ADVICE to transform URL for later opening by `browse-url'.
116ARGS are ignored here, but passed on for later processing."
117 ;; Basically, loop through `+browse-url-transformations' until finding a CAR
118 ;; that matches the URL. If one is found, transform it using `replace-match'
119 ;; with the CDR of that cell, or if one isn't, just pass the URL unchanged,
120 ;; along with the rest of the args, in a list to the original caller (probably
121 ;; `browse-url'.)
122 (apply 'list
123 (cl-loop with url = (substring-no-properties
124 (if (consp url) (car url) url))
125 for (regex . transformation) in +browse-url-transformations
126 if (string-match regex url)
127 return (replace-match transformation nil nil url)
128 ;; else
129 finally return url)
130 args))
131
132(define-minor-mode +browse-url-transform-url-mode
133 "Minor mode to transform a URL before passing it to `browse-url'.
134This can be used to \"redirect\" URLs, for example from an
135information silo to a more privacy-respecting one (e.g.,
136\"twitter.com\" -> \"nitter.com\"), by adding advice to `browse-url'.
137
138When using this mode, ensure that the transformed URL is also in
139`browse-url-handlers', since that's what `browse-url' will see."
140 :lighter " Xurl"
141 :keymap nil
142 (if +browse-url-transform-url-mode
143 (advice-add 'browse-url :filter-args '+browse-url-transform-advice)
144 (advice-remove 'browse-url '+browse-url-transform-advice)))
145
146(define-global-minor-mode +browse-url-transform-url-global-mode
147 +browse-url-transform-url-mode +browse-url-transform-url-mode)
148
149(defun +browse-url-other-window (&rest args)
150 "Browse URL in the other window."
151 (let ((browsed (apply #'browse-url args)))
152 (when (bufferp browsed)
153 (switch-to-buffer-other-window browsed))))
154
155(provide '+browse-url)
156;;; +browse-url.el ends here