aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/reddio.el
diff options
context:
space:
mode:
Diffstat (limited to 'emacs/.emacs.d/lisp/my/reddio.el')
-rw-r--r--emacs/.emacs.d/lisp/my/reddio.el80
1 files changed, 80 insertions, 0 deletions
diff --git a/emacs/.emacs.d/lisp/my/reddio.el b/emacs/.emacs.d/lisp/my/reddio.el
new file mode 100644
index 0000000..f8bc77f
--- /dev/null
+++ b/emacs/.emacs.d/lisp/my/reddio.el
@@ -0,0 +1,80 @@
+;;; reddio.el -- reddit client through reddio -*- lexical-binding: t -*-
+
+;; Copyright (C) 2024 Free Software Foundation, Inc.
+
+;; Author: Yuchen Pei <id@ypei.org>
+;; Package-Requires: ((emacs "29.4"))
+
+;; This file is part of dotted.
+
+;; dotted is free software: you can redistribute it and/or modify it under
+;; the terms of the GNU Affero General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; dotted 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 Affero General
+;; Public License for more details.
+
+;; You should have received a copy of the GNU Affero General Public
+;; License along with dotted. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; reddit client through reddio.
+
+;;; Code:
+
+(defvar reddio-buffer "*reddio*")
+
+(defvar reddio-dir (locate-user-emacs-file "reddio")
+ "Path to local directory of saved threads.")
+
+(defun reddio-make-filename (url)
+ (string-match "/r/\\([^/]+\\)/comments/\\([^/]+\\)/\\([^/]+\\)" url)
+ (file-name-concat
+ reddio-dir
+ (format "%s.%s.%s.txt"
+ (match-string 1 url)
+ (match-string 3 url)
+ (match-string 2 url))))
+
+(defun reddio-save-text-and-switch-to-buffer (text file-name)
+ "Save TEXT to FILE-NAME and switch to buffer."
+ (let ((buffer (find-file-noselect file-name))
+ (coding-system-for-write 'utf-8))
+ (with-current-buffer buffer
+ (let ((inhibit-read-only t))
+ (erase-buffer)
+ (insert text))
+ (goto-char (point-min))
+ (save-buffer)
+ (revert-buffer t t))
+ (switch-to-buffer buffer)))
+
+(defun reddio-open-url (url)
+ (interactive "sReddit link: ")
+ (let ((text
+ (when (string-match "/\\(comments/[^/]+\\)/" url)
+ (with-temp-buffer
+ (if (= 0 (call-process "reddio" nil (current-buffer) nil
+ "print" "-l" "500"
+ (match-string 1 url)))
+ (goto-char (point-min))
+ (error "reddio process failed: %s" (buffer-string)))
+ (delete-trailing-whitespace)
+ (buffer-string)))))
+ (reddio-save-text-and-switch-to-buffer
+ text
+ (reddio-make-filename url))))
+
+(defun reddio-reddit-url-p (url)
+ "e.g.
+https://www.reddit.com/r/linux/comments/cs3os6/introducing_reddio_a_commandline_interface_for/"
+ (let ((urlobj (url-generic-parse-url url)))
+ (and (string-match-p "^.*\\<reddit.com$" (url-host urlobj))
+ (string-match-p "^/r/[^/]+/comments/[^/]+/.+$" (url-filename urlobj)))))
+
+(provide 'reddio)
+;;; reddio.el ends here