aboutsummaryrefslogtreecommitdiffstats
path: root/oni-browse-url.el
blob: 33260d2faff38abb4f755d8893704c2b5101049a (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
;;; oni-browse-url.el --- Browse URL configuration   -*- lexical-binding: t; -*-

;; Copyright (C) 2019  Tom Willemse

;; Author: Tom Willemse <tom@ryuslash.org>
;; Keywords: local
;; Version: 2022.1213.070851

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Browse URL configuration.

;;; Code:

(require 'browse-url)

(autoload 'mastodon-url-lookup "mastodon")

;; Ideas:
;; - Split out to different functions for Medium, Twitter, Youtube, etc.
;; - Split the medium one into *.medium.com and also allow arbitrary sites to be added.
(defun browse-url-redirect-alternative (args)
  "Check the url given to ‘browse-url’ and redirect to a free alternative if available.
ARGS is the list of arguments that was passed to ‘browse-url’
before this filter."
  (save-match-data
    (cond
     ((string-prefix-p "https://blog.bitsrc.io" (car args))
      (cons (string-replace "https://blog.bitsrc.io" "https://scribe.rip/bitsrc" (car args))
            (cdr args)))
     ((string-prefix-p "https://levelup.gitconnected.com" (car args))
      (cons (string-replace "https://levelup.gitconnected.com" "https://scribe.rip/gitconnected" (car args))
            (cdr args)))
     ((string-prefix-p "https://twitter.com" (car args))
      (cons (string-replace "https://twitter.com" "https://nitter.net" (car args))
            (cdr args)))
     ((string-prefix-p "https://medium.marqeta.com" (car args))
      (cons (string-replace "https://medium.marqeta.com" "https://scribe.rip/marqeta" (car args))
            (cdr args)))
     ((string-prefix-p "https://itnext.io" (car args))
      (cons (string-replace "https://itnext.io" "https://scribe.rip" (car args))
            (cdr args)))
     ((string-match (rx "https://" (optional (one-or-more (not ".")) ".") "medium.com") (car args))
      (cons (replace-match "https://scribe.rip" nil nil (car args))
            (cdr args)))
     ((string-prefix-p "https://www.youtube.com" (car args))
      (cons (string-replace "https://www.youtube.com" "https://yewtu.be" (car args))
            (cdr args)))
     ((string-prefix-p "https://m.youtube.com" (car args))
      (cons (string-replace "https://m.youtube.com" "https://yewtu.be" (car args))
            (cdr args)))
     ((string-prefix-p "https://youtu.be" (car args))
      (cons (string-replace "https://youtu.be/" "https://yewtu.be/watch?v=" (car args))
            (cdr args)))
     ((string-prefix-p "https://www.instagram.com" (car args))
      (cons (string-replace "https://www.instagram.com" "https://bibliogram.1d4.us/u" (car args))
            (cdr args)))
     (t args))))

(setq browse-url-browser-function
      (if (eql system-type 'windows-nt)
          'browse-url-default-windows-browser
        'browse-url-firefox))

;; Taken from https://mas.to/@ParetoOptimalDev/109378647927115065
(defun oni-browse-url-browse-mastodon (url &rest _args)
  "Open URL in mastodon."
  (mastodon-url-lookup url))

(when (package-installed-p 'mastodon)
  (add-to-list 'browse-url-handlers
               (cons (rx "http" (optional "s") "://"
                         (minimal-match (one-or-more (not "/")))
                         "/@"
                         (minimal-match (one-or-more (not "/")))
                         "/")
                     #'oni-browse-url-browse-mastodon)))

(advice-add 'browse-url :filter-args #'browse-url-redirect-alternative)

(provide 'oni-browse-url)
;;; oni-browse-url.el ends here