summary refs log tree commit diff stats
path: root/lisp/acdw-browse-url.el
blob: 9f8e484ab665698a24101c8a59b06e73ec6441f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
;;; acdw-browse-url.el -*- lexical-binding: t; coding: utf-8-unix -*-
;;
;; Add-ons to `browse-url'.

(defvar browse-url-mpv-arguments nil
  "Arguments to pass to mpv in `browse-url-mpv'.")

(defun browse-url-mpv (url &optional new-window)
  "Play URL in mpv."
  (interactive (browse-url-interactive-arg "Video URL: "))
  (ignore new-window) ;; mpv always opens a new window
  (let* ((url (browse-url-encode-url url))
         (process-environment (browse-url-process-environment)))
    (message "Playing %s in mpv..." url)
    (apply #'start-process
           (concat "mpv " url) nil
           "mpv"
           (append
            browse-url-mpv-arguments
            (list url)))))

(defvar browse-url-feh-arguments '("--auto-zoom"
                                   "--geometry" "800x600")
  "Arguments to pass to feh in `browse-url-feh'.")

(defun browse-url-feh (url &optional new-window)
  "Open `URL' in feh."
  (interactive (browse-url-interactive-arg "Video URL: "))
  (ignore new-window) ;; mpv always opens a new window
  (let* ((url (browse-url-encode-url url))
         (process-environment (browse-url-process-environment)))
    (message "Opening %s in feh..." url)
    (apply #'start-process
           (concat "feh " url) nil
           "feh"
           (append
            browse-url-feh-arguments
            (list url)))))

(defun acdw/browse-url-set-handlers (handlers)
  "Set handlers for `browse-url'.
If Emacs' version is 28 or higher, set `browse-url-handlers'.
Else, set `browse-url-browser-function'; it's deprecated in 28+."
  (set-default (if (version< emacs-version "28")
                   #'browse-url-browser-function
                 #'browse-url-handlers)
               handlers))

;;; URL regexp
;; really, I just want to add gemini:// protocol, but I'm going to do some
;; reverse-engineering here.
(defvar acdw/button-protocols '("http"
                                "https"
                                "shttp"
                                "shttps"
                                "ftp"
                                "file"
                                "gopher"
                                "nntp"
                                "news"
                                "telnet"
                                "wais"
                                "mailto"
                                "info")
  "The list of protocols to splice into `browse-url-button-regexp'.")

(defun acdw/build-button-url-regexp ()
  "Build `browse-url-button-regexp' from `acdw/button-protocols'.
I used `xr' (not included in Emacs) to get the RX form of the
default, so I can easily splice the list into it.  THIS IS
BRITTLE AF!!!"
  (rx-to-string                         ; thanks wgreenhouse!
   `(seq word-boundary
         (group
          (group
           (or "www."
               (seq
                (group (or ,@acdw/button-protocols))
                ":")))
          (opt
           (group "//"
                  (one-or-more
                   (any "0-9a-z" "._-"))
                  ":"
                  (zero-or-more
                   (any "0-9"))))
          (or
           (seq
            (one-or-more
             (any "0-9a-z" "!#$%&*+,./:;=?@\\_~-" word))
            "("
            (one-or-more
             (any "0-9a-z" "!#$%&*+,./:;=?@\\_~-" word))
            (zero-or-more
             (any "0-9a-z" "#$%&*+/=@\\_~-" word))
            ")"
            (opt
             (one-or-more
              (any "0-9a-z" "!#$%&*+,./:;=?@\\_~-" word))
             (any "0-9a-z" "#$%&*+/=@\\_~-" word)))
           (seq
            (one-or-more
             (any "0-9a-z" "!#$%&*+,./:;=?@\\_~-" word))
            (any "0-9a-z" "#$%&*+/=@\\_~-" word)))))))

(defun acdw/add-button-url-regexp-protocol (proto)
  "Add PROTO to `browse-url-button-regexp'
First, add PROTO to `acdw/button-protocols'.
Then, build `browse-url-button-regexp' with the new protocol."
  (add-to-list 'acdw/button-protocols proto)
  (setq-default browse-url-button-regexp (acdw/build-button-url-regexp)))

;;; Browse-URL tweaks

;; convert reddit.com to teddit
(defun acdw/eww-browse-reddit-url (url &rest args)
  "Browse a Reddit.com URL using Teddit."
  (let* ((teddit "teddit.com")
         (url (replace-regexp-in-string "reddit\\.com" teddit url)))
    (eww-browse-url url args)))

;; convert twitter.com to nitter
(defun acdw/eww-browse-twitter-url (url &rest args)
  "Browse a Twitter.com URL using Nitter."
  (let* ((nitter "nitter.snopyta.org")
         (url (replace-regexp-in-string "twitter\\.com" nitter url)))
    (eww-browse-url url args)))

(provide 'acdw-browse-url)