Advertisement
mark-naylor-1701

elisp url transformer

May 23rd, 2025 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.05 KB | None | 0 0
  1. (defun mwn/url-transform (&optional to-url)
  2.   "Replace characters in a URL. Some sites block links in posts. This is a
  3. workaround. Only works upon an active region. Default (no prefix)
  4. obscures the URL. With prefix (C-u most likely) restores the URL to a
  5. form that a browser will accept."
  6.   (interactive "P")
  7.   (when (region-active-p)
  8.     (save-excursion
  9.      (let ((beginning (region-beginning))
  10.            (end       (region-end))
  11.            (pairs     '((":" . "(colon)")
  12.                         ("/" . "(slash)")
  13.                         ("." . "(dot)"))))
  14.        (cl-labels ((f (pair)
  15.                      (let* ((from-str (if to-url (cdr pair) (car pair)))
  16.                             (to-str   (if to-url (car pair) (cdr pair)))
  17.                             (adjust   (- (length to-str) (length from-str))))
  18.                        (goto-char beginning)
  19.                        (while (search-forward from-str end t)
  20.                          (replace-match to-str)
  21.                          (setq end (+ end adjust))))))
  22.          (mapc #'f pairs))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement