Last active
December 11, 2015 10:38
-
-
Save ScriptDevil/4587724 to your computer and use it in GitHub Desktop.
A function to collect links in an email and group them together at the end
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun collect-links () | |
(interactive) | |
(save-excursion | |
(goto-char (point-min)) | |
(let ((urls '())) | |
;; Find unique URLs | |
(while (re-search-forward "[a-zA-Z]+://[^\n\s-]+" nil t) | |
(add-to-list 'urls (match-string-no-properties 0))) | |
;; Add to list adds to the front. So we need to reverse | |
(setq urls (reverse urls)) | |
;; Replace them with a number | |
(let ((counter 1)) | |
(dolist (url urls) | |
(goto-char (point-min)) | |
(while (search-forward url nil t) | |
(replace-match | |
(concat "[" (number-to-string counter) "]"))) | |
(setq counter (+ 1 counter)))) | |
(goto-char (point-max)) | |
(newline 2) | |
;; Print the numbered list | |
(let ((counter 1)) | |
(dolist (url urls) | |
(insert (concat "[" (number-to-string counter) "] " url)) | |
(incf counter) | |
(newline)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment