commit f16608913a26aad63ed006634dadd818291c0b98
parent 2146cd76df48c8a9212486060e81c4654958c976
Author: Yuval Langer <yuval.langer@gmail.com>
Date: Fri, 27 Sep 2024 07:51:13 +0300
Define a clearer API and impove docstring.
Diffstat:
M | detubifier.el | | | 148 | ++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------- |
1 file changed, 97 insertions(+), 51 deletions(-)
diff --git a/detubifier.el b/detubifier.el
@@ -3,7 +3,7 @@
;; Copyright (C) 2024 Yuval Langer
;; Author: Yuval Langer <yuval.langer@gmail.com>
-;; Version: 0.1.0
+;; Version: 0.2.0
;; Keywords: web, youtube
;; URL: https://codebarg.org/kakafarm/emacs-detubifier-mode/
@@ -39,6 +39,8 @@
;;; Code:
+(require 'browse-url)
+
(defcustom detubifier-regexp-replacement-pairs
`((,(rx "www.youtube.com") . "invidious.jing.rocks")
(,(rx "youtube.com") . "invidious.jing.rocks")
@@ -53,14 +55,15 @@
(defcustom detubifier-farside-url
"https://farside.link/"
"Base address to the farside service."
- :type 'url
+ :type 'string
:group 'detubifier
)
(defcustom detubifier-detubifying-method
'farside
"Method of detubifying."
- :type '(radio farside custom)
+ :type '(radio (symbol farside)
+ (symbol custom))
:group 'detubifier
)
@@ -71,80 +74,123 @@
"youtu.be"
"twitter.com"
"x.com"
+ "reddit.com"
)
"URLs of enshittified services."
:type '(repeat string)
:group 'detubifier
)
-(defun detubifier-farsidify-string (str)
- "Replace enshittified URLs in string `STR' with nonenshittified farside.link."
- (rx-let-eval `((urls () (group (or ,@detubifier-enshittified-urls))))
- (replace-regexp-in-string (rx-to-string '(urls))
- (concat detubifier-farside-url "\\1")
- str))
- )
+;;;###autoload
+(defun detubifier-browse-url (url &rest args)
+ "Open URL using `browse-url' with the usual suspect URLs replaced.
-(defun detubifier-farsidify-region ()
- "Replace enshittified URLs with "
- (interactive)
+Replace the usual suspect URLs with the method chosen by the
+`detubifier-detubifying-method' custom variable.
- (save-excursion
- (save-restriction ;; Save narrowing setting and revert to it when finishing.
- (narrow-to-region (mark) (point))
- (rx-let-eval `((urls () (group (or ,@detubifier-enshittified-urls))))
- (message (rx-to-string '(urls)))
- (replace-regexp-in-region (rx-to-string '(urls))
- (concat detubifier-farside-url "\\1")
- (mark)
- (point)))))
- )
+ARGS is passed as the ARGS of `browse-url'.
-(defun detubifier-browse-url (url &rest args)
- "Open using `browse-url', but with the usual suspects replaced as defined in `detubifier-regexp-replacement-pairs'."
+TODO: What are args in `browse-url'?"
- (let ((url url))
- (dolist (regexp-replacement-pairs detubifier-regexp-replacement-pairs)
- (pcase regexp-replacement-pairs
- (`(,current-regexp . ,current-replacement)
- (setq url (replace-regexp-in-string current-regexp
- current-replacement
- url)))))
- (browse-url url args)))
+ (interactive (browse-url-interactive-arg "URL: "))
-(defun detubifier-detubify-region ()
- "Replace, in region, all occurances of the regular expressions in `detubifier-regexp-replacement-pairs' with their replacement targets."
+ (pcase detubifier-detubifying-method
+ ('farside (browse-url (detubifier-farsidify-string url) args))
+ ('custom (browse-url (detubifier-custom-detubify-string url) args))))
+
+;;;###autoload
+(defun detubifier-custom-detubify-region (&optional beg end)
+ "Replace the usual suspect URLs in string STR.
+
+Use custom variable `detubifier-regexp-replacement-pairs' to do so."
(interactive)
(save-excursion
- (save-restriction ;; Save narrowing setting and revert to it when finishing.
- (narrow-to-region (mark) (point))
- (dolist (regexp-replacement-pairs detubifier-regexp-replacement-pairs)
- (pcase regexp-replacement-pairs
- (`(,current-regexp . ,current-replacement)
- (goto-char (point-min))
- (while (re-search-forward current-regexp nil t)
- (replace-match current-replacement nil nil))))))))
+ (save-restriction
+ (let ((beg (if beg beg (region-beginning)))
+ (end (if end end (region-end))))
+ (dolist (regexp-replacement-pairs detubifier-regexp-replacement-pairs)
+ (pcase regexp-replacement-pairs
+ (`(,current-regexp . ,current-replacement)
+ (goto-char (point-min))
+ (while (re-search-forward current-regexp nil t)
+ (replace-match current-replacement nil nil)))))))))
+
+;;;###autoload
+(defun detubifier-custom-detubify-string (str)
+ "Replace the usual suspect URLs in string STR.
+
+Use custom variable `detubifier-regexp-replacement-pairs' to do so."
+
+ (let ((str str))
+ (dolist (regexp-replacement-pairs detubifier-regexp-replacement-pairs)
+ (pcase regexp-replacement-pairs
+ (`(,current-regexp . ,current-replacement)
+ (setq str (replace-regexp-in-string current-regexp
+ current-replacement
+ str)))))
+ str))
+;;;###autoload
(defun detubifier-detubify-top-kill ()
- "Replace, in the top of the kill ring, all occurances of the regular expressions in `detubifier-regexp-replacement-pairs' with their replacement targets."
-
+ "Replace all occurances of the usual suspects in the top of the kill ring.
+
+Use the method chosen by the `detubifier-detubifying-method' custom variable."
+
(interactive)
(save-excursion
(with-temp-buffer
(yank)
(goto-char (point-min))
- (dolist (regexp-replacement-pairs detubifier-regexp-replacement-pairs)
- (pcase regexp-replacement-pairs
- (`(,current-regexp . ,current-replacement)
- (goto-char (point-min))
- (while (re-search-forward current-regexp nil t)
- (replace-match current-replacement nil nil)))))
+ (pcase detubifier-detubifying-method
+ ('farside (detubifier-farsidify-region (point-min) (point-max)))
+ ('custom (detubifier-custom-detubify-region (point-min) (point-max))))
(kill-region (point-min) (point-max))))
)
+;;;###autoload
+(defun detubifier-farsidify-region (&optional beg end)
+ "Prefix `detubifier-farside-url' the usual suspect URLs in a region.
+
+Region is defined either by (region-beginning) and (region-end),
+or, if provided, between between BEG and END.
+
+The usual suspects are replaced in this way:
+
+ example.com/blah/blah
+
+is replaced with:
+
+ https://farside.link/example.com/blah/blah"
+
+ (interactive)
+
+ (let ((beg (if beg beg (region-beginning)))
+ (end (if end end (region-end))))
+
+ (save-excursion
+ (save-restriction ;; Save narrowing setting and revert to it when finishing.
+ (narrow-to-region beg end)
+ (rx-let-eval `((urls () (group (or ,@detubifier-enshittified-urls))))
+ (let ((result (replace-regexp-in-region (rx-to-string '(urls))
+ (concat detubifier-farside-url "\\1")
+ beg
+ end)))
+ result)))))
+ )
+
+;;;###autoload
+(defun detubifier-farsidify-string (str)
+ "Prefix `detubifier-farside-url' the usual suspects URLs in string STR."
+
+ (rx-let-eval `((urls () (group (or ,@detubifier-enshittified-urls))))
+ (replace-regexp-in-string (rx-to-string '(urls))
+ (concat detubifier-farside-url "\\1")
+ str))
+ )
+
(define-obsolete-function-alias
'detubifier-replace-contents
'detubifier-detubify-top-kill