[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/xkcd d1bc89dee2 34/82: Add variable xkcd-latest: Stores th
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/xkcd d1bc89dee2 34/82: Add variable xkcd-latest: Stores the latest cached xkcd number. |
Date: |
Mon, 12 Dec 2022 21:59:51 -0500 (EST) |
branch: elpa/xkcd
commit d1bc89dee2951b19242cc5b226e78af2e83ec5dc
Author: Vibhav Pant <vibhavp@gmail.com>
Commit: Vibhav Pant <vibhavp@gmail.com>
Add variable xkcd-latest: Stores the latest cached xkcd number.
Add defcustom xkcd-cache-latest.
Rewrite xkcd-get-json to work offline.
Rewrite xkcd-cache json to update xkcd-latest.
Add functions xkcd-get-latest-cached, xkcd-update-latest.
---
xkcd.el | 76 +++++++++++++++++++++++++++++++++++++++++++----------------------
1 file changed, 51 insertions(+), 25 deletions(-)
diff --git a/xkcd.el b/xkcd.el
index fdc46931ee..fbad11fd59 100644
--- a/xkcd.el
+++ b/xkcd.el
@@ -51,6 +51,7 @@
(defvar xkcd-alt nil)
(defvar xkcd-cur nil)
+(defvar xkcd-latest 0)
(defgroup xkcd nil
"A xkcd reader for Emacs")
@@ -60,21 +61,25 @@
:group 'xkcd
:type 'directory)
+(defcustom xkcd-cache-latest (concat xkcd-cache-dir "latest")
+ "File to store the latest cached xkcd number in. Should preferably
+be located in xkcd-cache-dir"
+ :group 'xkcd
+ :type 'file)
+
(defun xkcd-get-json (url &optional num)
- (let ((json nil))
- (let ((file (concat xkcd-cache-dir (number-to-string num) ".json")))
- (if (and (file-exists-p file) (not (eq num 0)))
- (with-current-buffer (find-file-literally file) ;; File already
exists in the cache
- (setq json (buffer-substring-no-properties (point-min) (point-max)))
- (kill-buffer (current-buffer))
- json)
- (let ((buffer (url-retrieve-synchronously url)))
- (with-current-buffer buffer
- (goto-char (point-min))
- (re-search-forward "^$")
- (setq json (buffer-substring-no-properties (+ (point) 1)
(point-max)))
- (kill-buffer (current-buffer)))
- json)))))
+ (let ((json-string nil)
+ (file (concat xkcd-cache-dir (number-to-string num) ".json")))
+ (with-current-buffer (if (and (file-exists-p file) (not (eq num 0)))
+ (find-file file)
+ (url-retrieve-synchronously url))
+ (goto-char (point-min))
+ (if (not (and (file-exists-p file) (not (eq num 0))))
+ (re-search-forward "^$")
+ (goto-char (point-min)))
+ (setq json-string (buffer-substring-no-properties (point) (point-max)))
+ (kill-buffer (current-buffer)))
+ json-string))
(defun xkcd-download (url num)
"Download the image linked by URL. If the file arleady exists, do nothing"
@@ -87,14 +92,23 @@
(url-copy-file url name))))
(defun xkcd-cache-json (num json-string)
- "Save comic json to cache directory"
- (let ((name (concat xkcd-cache-dir (number-to-string num) ".json")))
- (if (file-exists-p name)
- nil
- (with-current-buffer (find-file name)
- (insert json-string)
- (save-buffer)
- (kill-buffer (current-buffer))))))
+ "Save xkcd NUM's JSON-STRING to cache directory, and write xkcd-latest to a
file"
+ (let ((name (concat xkcd-cache-dir (number-to-string num) ".json"))
+ (file (concat xkcd-cache-latest)))
+ (if (> num xkcd-latest)
+ (with-current-buffer (find-file file)
+ (setq xkcd-latest num)
+ (erase-buffer)
+ (insert (number-to-string num))
+ (save-buffer)
+ (kill-buffer (current-buffer))))
+
+ (if (file-exists-p name)
+ nil
+ (with-current-buffer (find-file name)
+ (insert json-string)
+ (save-buffer)
+ (kill-buffer (current-buffer))))))
;;;###autoload
(defun xkcd-get (num)
@@ -119,7 +133,7 @@
(setq img (cdr (assoc 'img (json-read-from-string out))))
;; FIXME: This looks pretty ugly.
- (message "Downloading comic...")
+ (message "Getting comic...")
(xkcd-download img num)
(setq title (format "%d: %s" (cdr (assoc 'num (json-read-from-string out)))
(cdr (assoc 'safe_title (json-read-from-string out)))))
@@ -155,10 +169,14 @@
(defun xkcd-get-latest ()
"Get the latest xkcd"
(interactive)
- (if (and (boundp 'xkcd-mode) (not xkcd-mode))
- (xkcd-mode))
(xkcd-get 0))
+(defun xkcd-get-latest-cached ()
+ "Get the latest cached xkcd"
+ (interactive)
+ (xkcd-update-latest)
+ (xkcd-get xkcd-latest))
+
(defun xkcd-alt-text ()
"View the alt text in the buffer"
(interactive)
@@ -169,5 +187,13 @@
(interactive)
(kill-buffer "*xkcd*"))
+(defun xkcd-update-latest ()
+ "Update xkcd-latest to point to the last cached comic"
+ (let ((file (concat xkcd-cache-latest)))
+ (with-current-buffer (find-file file)
+ (setq xkcd-latest (string-to-number
+ (buffer-substring-no-properties (point-min)
(point-max))))
+ (kill-buffer (current-buffer)))))
+
(provide 'xkcd)
;;; xkcd.el ends here
- [nongnu] elpa/xkcd 80011da2e7 82/82: Update copyright years., (continued)
- [nongnu] elpa/xkcd 80011da2e7 82/82: Update copyright years., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd ca3eed5603 80/82: Prefer HTTPS to HTTP, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 5c10f6a679 21/82: Merge pull request #4 from purcell/patch-1, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 25594f98c7 31/82: Add info about Melpa in README., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 52c0d270de 36/82: Use one-letter keybindings, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 20e0b48044 42/82: Correct flycheck warnings, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd f5942003bf 45/82: Fix xkcd-get-json, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 3c13ae29c4 49/82: Docstrings, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd e2daa08012 25/82: Add a better screenshot., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd df47be622f 28/82: The package isn't that hackish now., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd d1bc89dee2 34/82: Add variable xkcd-latest: Stores the latest cached xkcd number.,
ELPA Syncer <=
- [nongnu] elpa/xkcd 7b1a312239 37/82: Add an help-echo text property to display the alt-text, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd e61b7e6ed6 51/82: xkcd-get-json: Get substring between (point) and (point-max), instead of, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 5693718f71 52/82: Add support for browse-url., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 0db98c2ca2 72/82: Add instructions for el-get., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 046ad2c582 67/82: Add README.org, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 7987d64a7e 71/82: * README.md: Remove. Replaced with README.org., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd b637a2aa84 13/82: Update README.md, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd b9095c5aae 16/82: Make xkcd-get-latest work., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 2da77a12e2 29/82: Provide xkcd instead of emacs-xkcd, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd e8bb80c4a0 30/82: Make comments work with Melpa., ELPA Syncer, 2022/12/12