[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/xkcd c2be5c6a08 50/82: Merge pull request #8 from rafoo/ma
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/xkcd c2be5c6a08 50/82: Merge pull request #8 from rafoo/master |
Date: |
Mon, 12 Dec 2022 21:59:53 -0500 (EST) |
branch: elpa/xkcd
commit c2be5c6a08bc0a26199c992667820851bc7e23b0
Merge: f2650dc392 3c13ae29c4
Author: Vibhav Pant <vibhavp@gmail.com>
Commit: Vibhav Pant <vibhavp@gmail.com>
Merge pull request #8 from rafoo/master
Major mode
---
xkcd.el | 113 ++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 56 insertions(+), 57 deletions(-)
diff --git a/xkcd.el b/xkcd.el
index 7c976158b0..0092269864 100644
--- a/xkcd.el
+++ b/xkcd.el
@@ -61,8 +61,8 @@
: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"
+ "File to store the latest cached xkcd number in.
+Should preferably be located in `xkcd-cache-dir'."
:group 'xkcd
:type 'file)
@@ -72,7 +72,7 @@ If the file NUM.json exists, use it instead.
If NUM is 0, always download from URL.
The return value is a string."
(let* ((file (format "%s%d.json" xkcd-cache-dir num))
- (cached (and (file-exists-p file) (not (eq num 0)))))
+ (cached (and (file-exists-p file) (not (eq num 0)))))
(with-current-buffer (if cached
(find-file file)
(url-retrieve-synchronously url))
@@ -83,6 +83,7 @@ The return value is a string."
(kill-buffer (current-buffer))))))
(defun xkcd-get-image-type (url)
+ "Return a symbol (`png', `jpg' or `gif') corresponding to the last
characters of URL."
(let ((substr (substring url (- (length url) 3))))
(cond
((string= substr "png")
@@ -92,9 +93,10 @@ The return value is a string."
(t 'gif))))
(defun xkcd-download (url num)
- "Download the image linked by URL to NUM. If NUM arleady exists, do nothing"
+ "Download the image linked by URL to NUM. If NUM arleady exists, do
nothing."
;;check if the cache directory exists
- (unless (file-exists-p xkcd-cache-dir) (make-directory xkcd-cache-dir))
+ (unless (file-exists-p xkcd-cache-dir)
+ (make-directory xkcd-cache-dir))
(let ((name (format "%s%s.%s" xkcd-cache-dir (number-to-string num)
(substring url (- (length url) 3)))))
(if (file-exists-p name)
@@ -103,8 +105,7 @@ The return value is a string."
name))
(defun xkcd-cache-json (num json-string)
- "Save xkcd NUM's JSON-STRING to cache directory,
-and write xkcd-latest to a file"
+ "Save xkcd NUM's JSON-STRING to cache directory and write xkcd-latest to a
file."
(let ((name (format "%s%d.json" xkcd-cache-dir num)))
(if (> num xkcd-latest)
(with-current-buffer (find-file xkcd-cache-latest)
@@ -113,106 +114,104 @@ and write xkcd-latest to a file"
(insert (number-to-string num))
(save-buffer)
(kill-buffer (current-buffer))))
-
- (unless (file-exists-p (name))
+ (unless (file-exists-p name)
(with-current-buffer (find-file name)
(insert json-string)
(save-buffer)
(kill-buffer (current-buffer))))))
(defun xkcd-insert-image (file num)
- "Insert image FILENAME in buffer with the title-text,
-and animate if FILENAME is a gif"
- (let ((image (create-image (format "%s%s.%s" xkcd-cache-dir
- (number-to-string num)
+ "Insert image described by FILE and NUM in buffer with the title-text.
+If the image is a gif, animate it."
+ (let ((image (create-image (format "%s%d.%s" xkcd-cache-dir
+ num
(substring file (- (length file) 3)))
(xkcd-get-image-type file)))
(start (point)))
(insert-image image)
- (if (image-multi-frame-p image)
+ (if (or
+ (and (fboundp 'image-multi-frame-p)
+ (image-multi-frame-p image))
+ (and (fboundp 'image-animated-p)
+ (image-animated-p image)))
(image-animate image 0 t))
(add-text-properties start (point) '(help-echo xkcd-alt))))
;;;###autoload
(defun xkcd-get (num)
- "Get the xkcd number NUM"
+ "Get the xkcd number NUM."
(interactive "nEnter comic number: ")
(xkcd-update-latest)
(get-buffer-create "*xkcd*")
(switch-to-buffer "*xkcd*")
- (if buffer-read-only
- (toggle-read-only))
- (erase-buffer)
- (if (and (boundp 'xkcd-mode) (not xkcd-mode))
- (xkcd-mode))
- (setq xkcd-cur num)
- (let ((out (if (eq num 0)
- (xkcd-get-json "http://xkcd.com/info.0.json" 0)
- (xkcd-get-json (concat "http://xkcd.com/" (number-to-string num)
- "/info.0.json") num)))
- (img nil)
- (num nil)
- (title nil)
- (file nil))
- (setq num (cdr (assoc 'num (json-read-from-string out))))
- (setq img (cdr (assoc 'img (json-read-from-string out))))
-
- ;; FIXME: This looks pretty ugly.
- (message "Getting comic...")
- (setq file (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)))))
- (insert (concat title "\n"))
- (xkcd-insert-image file num)
- (if (eq xkcd-cur 0)
- (setq xkcd-cur (cdr (assoc 'num (json-read-from-string out)))))
- (xkcd-cache-json num out)
- (setq xkcd-alt (cdr (assoc 'alt (json-read-from-string out))))
- (read-only-mode)
- (message title)))
+ (xkcd-mode)
+ (let (buffer-read-only)
+ (erase-buffer)
+ (setq xkcd-cur num)
+ (let* ((url (if (eq num 0)
+ "http://xkcd.com/info.0.json"
+ (format "http://xkcd.com/%d/info.0.json" num)))
+ (out (xkcd-get-json url num))
+ (json-assoc (json-read-from-string out))
+ (img (cdr (assoc 'img json-assoc)))
+ (num (cdr (assoc 'num json-assoc)))
+ (safe-title (cdr (assoc 'safe_title json-assoc)))
+ title file)
+ (message "Getting comic...")
+ (setq file (xkcd-download img num))
+ (setq title (format "%d: %s" num safe-title))
+ (insert title "\n")
+ (xkcd-insert-image file num)
+ (if (eq xkcd-cur 0)
+ (setq xkcd-cur num))
+ (xkcd-cache-json num out)
+ (setq xkcd-alt (cdr (assoc 'alt json-assoc)))
+ (message title))))
(defun xkcd-next ()
- "Get next xkcd"
+ "Get next xkcd."
(interactive)
(xkcd-get (+ xkcd-cur 1)))
(defun xkcd-prev ()
- "Get previous xkcd"
+ "Get previous xkcd."
(interactive)
(xkcd-get (- xkcd-cur 1)))
(defun xkcd-rand ()
- "Show random xkcd"
+ "Show random xkcd."
(interactive)
- (xkcd-get (random (cdr (assoc 'num (json-read-from-string
- (xkcd-get-json
"http://xkcd.com/info.0.json" 0)))))))
-
+ (let* ((url "http://xkcd.com/info.0.json")
+ (last (cdr (assoc 'num (json-read-from-string
+ (xkcd-get-json url 0))))))
+ (xkcd-get (random last))))
+
(defun xkcd-get-latest ()
- "Get the latest xkcd"
+ "Get the latest xkcd."
(interactive)
(xkcd-get 0))
(defalias 'xkcd 'xkcd-get-latest)
(defun xkcd-get-latest-cached ()
- "Get the latest cached xkcd"
+ "Get the latest cached xkcd."
(interactive)
(xkcd-update-latest)
(xkcd-get xkcd-latest))
(defun xkcd-alt-text ()
- "View the alt text in the buffer"
+ "View the alt text in the buffer."
(interactive)
(message xkcd-alt))
(defun xkcd-kill-buffer ()
- "Kill the xkcd buffer"
+ "Kill the xkcd buffer."
(interactive)
(kill-buffer "*xkcd*"))
(defun xkcd-update-latest ()
- "Update xkcd-latest to point to the last cached comic"
- (let ((file (concat xkcd-cache-latest)))
+ "Update `xkcd-latest' to point to the last cached comic."
+ (let ((file xkcd-cache-latest))
(with-current-buffer (find-file file)
(setq xkcd-latest (string-to-number
(buffer-substring-no-properties (point-min)
(point-max))))
- [nongnu] elpa/xkcd e8bb80c4a0 30/82: Make comments work with Melpa., (continued)
- [nongnu] elpa/xkcd e8bb80c4a0 30/82: Make comments work with Melpa., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd b303669b33 39/82: Merge pull request #7 from rafoo/master, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd ef6e51ade7 43/82: Code simplification, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd f2650dc392 46/82: Retrieve correct substring, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 3304f05265 59/82: Autoload xkcd-handle-links., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 60de549e25 63/82: Merge pull request #10 from heikkil/master, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 0310d572b7 68/82: * images/screenshot.png: Add screenshot., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 74b0dfe926 78/82: Retain the quote style when printing messages, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 2c796bcd0f 81/82: Merge pull request #25 from skangas/prefer-https, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 296aa18259 47/82: Merge branch 'master' of https://github.com/vibhavp/emacs-xkcd, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd c2be5c6a08 50/82: Merge pull request #8 from rafoo/master,
ELPA Syncer <=
- [nongnu] elpa/xkcd 9f77f0422f 64/82: Require browse-url for external-browser functions to work., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 6166a690b5 73/82: Add MELPA badge., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 91be72384e 61/82: Open current xkcd in external browser, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd e0b7938714 54/82: Add docs for xkcd-handle-links., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 7148c2c135 57/82: Add (require 'image) to supress build warnings., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 97235a9fbd 58/82: Add autoloads., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 763b73d6de 69/82: * README.md: Remove feature from customize table which is still to be added., ELPA Syncer, 2022/12/12