[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/xkcd ef6e51ade7 43/82: Code simplification
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/xkcd ef6e51ade7 43/82: Code simplification |
Date: |
Mon, 12 Dec 2022 21:59:52 -0500 (EST) |
branch: elpa/xkcd
commit ef6e51ade7eddfa6751da19a71fa149b6697e02a
Author: Raphaël Cauderlier <cauderlier@crans.org>
Commit: Raphaël Cauderlier <cauderlier@crans.org>
Code simplification
---
xkcd.el | 85 +++++++++++++++++++++++++++++------------------------------------
1 file changed, 38 insertions(+), 47 deletions(-)
diff --git a/xkcd.el b/xkcd.el
index 3cb49781ec..86827c7163 100644
--- a/xkcd.el
+++ b/xkcd.el
@@ -39,8 +39,7 @@
;;;###autoload
(define-derived-mode xkcd-mode special-mode "xkcd"
"Major mode for viewing xkcd (http://xkcd.com/) comics."
- :group 'xkcd
- )
+ :group 'xkcd)
(define-key xkcd-mode-map (kbd "<right>") 'xkcd-next)
(define-key xkcd-mode-map (kbd "<left>") 'xkcd-prev)
@@ -72,34 +71,32 @@ Should preferably be located in `xkcd-cache-dir'."
If the file NUM.json exists, use it instead.
If NUM is 0, always download from URL.
The return value is a string."
- (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)))
+ (let* ((file (format "%s%d.json" xkcd-cache-dir num))
+ (cached (and (file-exists-p file) (not (eq num 0)))))
+ (with-current-buffer (if cached
(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))
+ (unless cached (re-search-forward "^$"))
+ (prog1
+ (buffer-substring-no-properties (point) (point-max))
+ (kill-buffer (current-buffer))))))
+
(defun xkcd-download (url num)
"Download the image linked by URL to NUM.png.
If the file NUM.png arleady exists, do nothing"
;;check if the cache directory exists
- (if (not (file-exists-p xkcd-cache-dir))
- (make-directory xkcd-cache-dir))
- (let ((name (concat xkcd-cache-dir (number-to-string num) ".png")))
- (if (file-exists-p name)
- nil
+ (unless (file-exists-p xkcd-cache-dir)
+ (make-directory xkcd-cache-dir))
+ (let ((name (format "%s%d.png" xkcd-cache-dir num)))
+ (unless (file-exists-p name)
(url-copy-file url name))))
(defun xkcd-cache-json (num json-string)
"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)))
+ (let ((name (format "%s%d.json" xkcd-cache-dir num))
+ (file xkcd-cache-latest))
(if (> num xkcd-latest)
(with-current-buffer (find-file file)
(setq xkcd-latest num)
@@ -108,8 +105,7 @@ If the file NUM.png arleady exists, do nothing"
(save-buffer)
(kill-buffer (current-buffer))))
- (if (file-exists-p name)
- nil
+ (unless (file-exists-p name)
(with-current-buffer (find-file name)
(insert json-string)
(save-buffer)
@@ -122,39 +118,32 @@ If the file NUM.png arleady exists, do nothing"
(xkcd-update-latest)
(get-buffer-create "*xkcd*")
(switch-to-buffer "*xkcd*")
- (if (and (boundp 'xkcd-mode) (not xkcd-mode))
- (xkcd-mode))
+ (xkcd-mode)
(let (buffer-read-only)
(erase-buffer)
(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))
- (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.
+ (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)
(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)))))
- (insert (concat title "\n"))
+ (setq title (format "%d: %s" num safe-title))
+ (insert title "\n")
(let ((start (point)))
(insert-image (create-image
- (concat xkcd-cache-dir
- (number-to-string
- (cdr
- (assoc 'num (json-read-from-string out))))
".png") 'png))
- (add-text-properties start (point) '(help-echo xkcd-alt))
- )
+ (format "%s%d.png" xkcd-cache-dir num)
+ 'png))
+ (add-text-properties start (point) '(help-echo xkcd-alt)))
(if (eq xkcd-cur 0)
- (setq xkcd-cur (cdr (assoc 'num (json-read-from-string out)))))
+ (setq xkcd-cur num))
(xkcd-cache-json num out)
- (setq xkcd-alt (cdr (assoc 'alt (json-read-from-string out))))
+ (setq xkcd-alt (cdr (assoc 'alt json-assoc)))
(message title))))
(defun xkcd-next ()
@@ -170,8 +159,10 @@ If the file NUM.png arleady exists, do nothing"
(defun xkcd-rand ()
"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."
@@ -198,7 +189,7 @@ If the file NUM.png arleady exists, do nothing"
(defun xkcd-update-latest ()
"Update `xkcd-latest' to point to the last cached comic."
- (let ((file (concat xkcd-cache-latest)))
+ (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 e61b7e6ed6 51/82: xkcd-get-json: Get substring between (point) and (point-max), instead of, (continued)
- [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
- [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 <=
- [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, 2022/12/12
- [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