Skip to content

Instantly share code, notes, and snippets.

@MasWag
Created February 23, 2024 04:14
Show Gist options
  • Save MasWag/a5c2ca90b87f8ba6edc2983cfc2f681b to your computer and use it in GitHub Desktop.
Save MasWag/a5c2ca90b87f8ba6edc2983cfc2f681b to your computer and use it in GitHub Desktop.
Extract and copy Mastodon URIs to kill-ring
;;; copy-mastodon-uris-as-kill.el --- Extract and copy Mastodon URIs to kill-ring -*- lexical-binding:t -*-
;; Copyright (C) 2024 Masaki Waga
;; Maintainer: Masaki Waga
;; Keywords: Mastodon, social, kill-ring
;; This file is NOT a part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This Emacs Lisp tool provides a convenient way to extract Mastodon instance URIs
;; from text within Emacs. It supports extracting from either a selected region or
;; the entire buffer, and copies the extracted URIs to the kill-ring for easy pasting.
;; This can be particularly useful for users frequently interacting with Mastodon
;; instances or needing to collect and share multiple URIs.
;;; Code:
(defun copy-mastodon-uris-as-kill (&optional buffer)
"Extract and copy Mastodon instance URIs to the `kill-ring'.
Operates on the selected region, or if none, on a specified BUFFER or
the current buffer. Extracts URIs matching a pattern, omitting the '@'."
(interactive)
(let ((regexp "@[^()@]+\\.[^()@]+")
(matches '())
(search-end (if (use-region-p)
(region-end)
(point-max))))
(save-excursion
(goto-char (if (use-region-p)
(region-beginning)
(point-min)))
(while (re-search-forward regexp search-end t)
(let ((match (substring (match-string-no-properties 0) 1))) ; Drop the first @
(push match matches))))
(if buffer
(with-current-buffer buffer
(kill-new (mapconcat 'identity matches "\n")))
(kill-new (mapconcat 'identity matches "\n")))
(message "Mastodon URIs added to kill-ring.")))
(provide 'copy-mastodon-uris-as-kill)
;;; copy-mastodon-uris-as-kill.el ends here
@MasWag
Copy link
Author

MasWag commented Feb 23, 2024

If you like to do it on shell one-liner, it can be grep -o '@[^()@]\+\.[^()@]\+' | sed 's/^.//;'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment