Sharing code snippets on IRC with Emacs
I like to use IRC (Internet Relay Chat) because it's a text only protocol and it's relaxing in the age of social networks full of images/videos and all sort of distractions.
But sharing code is not really convenient because IRC is a "line-based" medium: every new line becomes a new message.
So the way people do that is by sharing URL of pastebin or equivalent.
Here are 2 little elisp functions that I use everytime I want to share some code snippets.
(defun ft--upload-file (file)
(let* ((hostname "https://envs.sh")
(upload-command (format "curl -s -F'file=@%s' %s" (expand-file-name file) hostname))
(url (shell-command-to-string upload-command)))
(kill-new url)
(message "%s" url)))
(defun ft-share-region (begin end)
"Take region and upload the text to 0x0.st or equivalent"
(interactive "r")
(when (not (use-region-p))
(user-error "No region selected"))
(let* ((text (buffer-substring-no-properties begin end))
(file-text (make-temp-file "" nil ".txt" text)))
(ft--upload-file file-text)))
(defun ft-share-file (file)
"Take marked file and upload it to 0x0.st. Prompt for file if no marked file"
(interactive (list
(let ((files-marked (dired-get-marked-files)))
(if (= (length files-marked) 1)
(car files-marked)
(read-file-name "File to upload: ")))))
(ft--upload-file file))
I can either directly select a region (ft-share-region
), or I can
directly send the content of a file (ft-share-file
). It will copy in
my kill ring the URL, and will also display it in the echo area, so I
have a little nice feedback it worked.
Obviously, ft-share-file
will work with every kind of files. I also
regularly use it to share screenshots, or even GIFs. Since Emacs web
browser, eww, is totally able to display images (even GIF), providing
you run emacs in graphical mode, this provides a fully integrated experience.
ft-share-file
could be improved, and it could accept multiple
files. It could also take the content of the file and render it in
HTML inside a <pre>
tag, so Emacs browser could apply syntax
highlighting. But honestly, I never need the former, and I'm too lazy
for the latter.