emacs-detubifier

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 86a403849224aeae9babe468034a0caec22af051
parent c39c60828965ceb085739dc4319d61b4ad31ba67
Author: Yuval Langer <yuval.langer@gmail.com>
Date:   Fri, 27 Sep 2024 12:20:35 +0300

Use better interactive specification for region. Also,

- Convert dolist pcase to pcase-dolist.
- Fix some test cases code (not the code they test).
- Change the default values of custom variables.
- Fix detubifier-regexp-replacement-pairs type.
- Bump version number.
- What are next?

Diffstat:
Mdetubifier.el | 111+++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 55 insertions(+), 56 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.2.0 +;; Version: 0.3.0 ;; Keywords: web, youtube ;; URL: https://codebarg.org/kakafarm/emacs-detubifier-mode/ @@ -42,13 +42,13 @@ (require 'browse-url) (defcustom detubifier-regexp-replacement-pairs - `((,(rx "www.youtube.com") . "invidious.jing.rocks") - (,(rx "youtube.com") . "invidious.jing.rocks") - (,(rx "youtu.be") . "invidious.jing.rocks/watch?v=") - (,(rx "twitter.com") . "nitter.poast.org") - (,(rx "x.com") . "https://farside.link/")) + `((,(rx "https://www.youtube.com/") . "https://invidious.jing.rocks/") + (,(rx "https://youtube.com/") . "https://invidious.jing.rocks/") + (,(rx "https://youtu.be/") . "https://invidious.jing.rocks/watch?v=") + (,(rx "https://twitter.com/") . "https://nitter.poast.org/") + (,(rx "https://x.com/") . "https://farside.link/")) "List of pairs, each with a regexp and its replacement." - :type '(list (cons regexp string)) + :type '(repeat (cons regexp string)) :group 'detubifier ) @@ -69,14 +69,14 @@ (defcustom detubifier-enshittified-urls '( - "https://reddit.com" - "https://twitter.com" - "https://www.reddit.com" - "https://www.twitter.com" - "https://www.youtube.com" - "https://x.com" - "https://youtu.be" - "https://youtube.com" + "https://reddit.com/" + "https://twitter.com/" + "https://www.reddit.com/" + "https://www.twitter.com/" + "https://www.youtube.com/" + "https://x.com/" + "https://youtu.be/" + "https://youtube.com/" ) "URLs of enshittified services." :type '(repeat string) @@ -101,7 +101,7 @@ TODO: What are args in `browse-url'?" ('custom (browse-url (detubifier-custom-detubify-string url) args)))) ;;;###autoload -(defun detubifier-custom-detubify-region (&optional beg end) +(defun detubifier-custom-detubify-region (beg end) "Replace the usual suspect URLs in region. Region is defined either by (region-beginning) and (region-end), @@ -109,20 +109,18 @@ or, if provided, between between BEG and END. Use custom variable `detubifier-regexp-replacement-pairs' to do so." - (interactive) + (interactive (list (region-beginning) + (region-end))) (save-excursion (save-restriction ;; Save narrowing setting and revert to it when finishing. - (let ((beg (if beg beg (region-beginning))) - (end (if end end (region-end)))) - (narrow-to-region beg end) + (narrow-to-region beg end) + (goto-char (point-min)) + (pcase-dolist (`(,current-regexp . ,current-replacement) + detubifier-regexp-replacement-pairs) (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))))))))) + (while (re-search-forward current-regexp nil t) + (replace-match current-replacement nil nil)))))) ;;;###autoload (defun detubifier-custom-detubify-string (str) @@ -131,12 +129,11 @@ Use custom variable `detubifier-regexp-replacement-pairs' to do so." 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))))) + (pcase-dolist (`(,current-regexp . ,current-replacement) + detubifier-regexp-replacement-pairs) + (setq str (replace-regexp-in-string current-regexp + current-replacement + str))) str)) ;;;###autoload @@ -158,7 +155,7 @@ Use the method chosen by the `detubifier-detubifying-method' custom variable." ) ;;;###autoload -(defun detubifier-farsidify-region (&optional beg end) +(defun detubifier-farsidify-region (beg end) "Prefix `detubifier-farside-url' the usual suspect URLs in a region. Region is defined either by (region-beginning) and (region-end), @@ -172,19 +169,18 @@ is replaced with: https://farside.link/example.com/blah/blah" - (interactive) + (interactive (list (region-beginning) + (region-end))) - (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 ((r (rx-to-string '(urls)))) - (replace-regexp-in-region r - (concat detubifier-farside-url "\\1") - beg - 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 ((r (rx-to-string '(urls)))) + (replace-regexp-in-region r + (concat detubifier-farside-url "\\1") + beg + end)))))) ;;;###autoload (defun detubifier-farsidify-string (str) @@ -201,6 +197,7 @@ is replaced with: 'detubifier-detubify-top-kill "2024-09-25") +;;; Testing: (ert-deftest test-detubifier-farsidify-string () "Test `detubifier-farsidify-string'." @@ -216,12 +213,13 @@ https://twitter.com/blah") (ert-deftest test-detubifier-farsidify-region () "Test `detubifier-farsidify-region'." (with-temp-buffer - (insert "sdfljdsf sdfkjl https://youtube.com/moo + (should (equal (let () + (insert "sdfljdsf sdfkjl https://youtube.com/moo https://twitter.com/blah") - (detubifier-farsidify-region (point-min) - (point-max)) - (should (equal (buffer-substring (point-min) - (point-max)) + (detubifier-farsidify-region (point-min) + (point-max)) + (buffer-substring-no-properties (point-min) + (point-max))) (concat "sdfljdsf sdfkjl " detubifier-farside-url "https://youtube.com/moo @@ -239,18 +237,19 @@ https://twitter.com/blah") (concat "sdfljdsf sdfkjl https://invidious.jing.rocks/moo https://nitter.poast.org/blah")))) -(ert-deftest test-detubifier-farsidify-region () +(ert-deftest test-detubifier-custom-detubify-region () "Test `detubifier-custom-detubify-region'. TODO: Test only works with the default `detubifier-regexp-replacement-pairs'. Make it work in general!" (with-temp-buffer - (insert "sdfljdsf sdfkjl https://youtube.com/moo + (should (equal (let () + (insert "sdfljdsf sdfkjl https://youtube.com/moo https://twitter.com/blah") - (detubifier-farsidify-region (point-min) - (point-max)) - (should (equal (buffer-substring (point-min) - (point-max)) + (detubifier-custom-detubify-region (point-min) + (point-max)) + (buffer-substring-no-properties (point-min) + (point-max))) (concat "sdfljdsf sdfkjl " detubifier-farside-url "https://youtube.com/moo