;;; SOLVED! ;;; ;;; Anyone knows why I'm getting: ;;; ;;; Wrong type argument: char-or-string-p, nil ;;; ;;; after running `detubifier-detubify-region' on a region like: ;;; ;;; [mark]youtube.com[point] ;;; ;;; ? (defcustom detubifier-regexp-replacement-pairs '(("www\\.youtube\\.com" . "invidious.jing.rocks") ("youtube\\.com" . "invidious.jing.rocks") ("youtu\\.be/" . "invidious.jing.rocks/watch?v=") ("twitter.com" . "nitter.poast.org")) "List of pairs, each with a regexp and its replacement." :type '(list (cons regexp string)) :group 'detubifier) (defun detubifier-detubify-region () (interactive) (save-excursion (replace-region-contents (min (point) (mark)) (max (point) (mark)) (lambda () (dolist (regexp-replacement-pairs detubifier-regexp-replacement-pairs) (pcase regexp-replacement-pairs (`(,current-regexp . ,current-replacement) (save-excursion (while (re-search-forward current-regexp nil t) (replace-match current-replacement nil nil)))))))))) ;;; Turns out `replace-region-contents' needs a buffer or a string as ;;; the return value of the provided thunk with which to replace the ;;; marked region. ;;; ;;; This works better: (defun detubifier-detubify-region () "Replace, in region, all occurances of the regular expressions in `detubifier-regexp-replacement-pairs' with their replacement targets." (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))))))))