>From f1326ea96d656d507c2017080356684523a28cdc Mon Sep 17 00:00:00 2001 From: "Basil L. Contovounesios" Date: Mon, 22 Jul 2019 21:49:47 +0100 Subject: [PATCH 3/5] DRY in gravatar.el For discussion, see the following thread: https://lists.gnu.org/archive/html/emacs-devel/2019-07/msg00528.html * lisp/image/gravatar.el (gravatar-data->image): Remove. (gravatar-retrieve, gravatar-retrieve-synchronously): Reuse url-fetch-from-cache and gravatar-retrieved to reduce duplication. (gravatar-retrieved): Only cache buffer if url-current-object is non-nil and return result of callback. This affords reusing this function in cached URL buffers. --- lisp/image/gravatar.el | 48 +++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/lisp/image/gravatar.el b/lisp/image/gravatar.el index ea746b71d7..fb539bcdbd 100644 --- a/lisp/image/gravatar.el +++ b/lisp/image/gravatar.el @@ -95,14 +95,6 @@ gravatar-get-data (search-forward "\n\n" nil t) (buffer-substring (point) (point-max))))) -(defun gravatar-data->image () - "Get data of current buffer and return an image. -If no image available, return 'error." - (let ((data (gravatar-get-data))) - (if data - (create-image data nil t) - 'error))) - ;;;###autoload (defun gravatar-retrieve (mail-address callback &optional cbargs) "Asynchronously retrieve a gravatar for MAIL-ADDRESS. @@ -112,11 +104,8 @@ gravatar-retrieve (let ((url (gravatar-build-url mail-address))) (if (url-cache-expired url gravatar-cache-ttl) (url-retrieve url #'gravatar-retrieved (list callback cbargs) t) - (apply callback - (with-temp-buffer - (url-cache-extract (url-cache-create-filename url)) - (gravatar-data->image)) - cbargs)))) + (with-current-buffer (url-fetch-from-cache url) + (gravatar-retrieved () callback cbargs))))) ;;;###autoload (defun gravatar-retrieve-synchronously (mail-address) @@ -124,26 +113,23 @@ gravatar-retrieve-synchronously Value is either an image descriptor, or the symbol `error' if the retrieval failed." (let ((url (gravatar-build-url mail-address))) - (if (url-cache-expired url gravatar-cache-ttl) - (with-current-buffer (url-retrieve-synchronously url) - (when gravatar-automatic-caching - (url-store-in-cache (current-buffer))) - (prog1 (gravatar-data->image) - (kill-buffer (current-buffer)))) - (with-temp-buffer - (url-cache-extract (url-cache-create-filename url)) - (gravatar-data->image))))) + (with-current-buffer (if (url-cache-expired url gravatar-cache-ttl) + (url-retrieve-synchronously url) + (url-fetch-from-cache url)) + (gravatar-retrieved () #'identity)))) (defun gravatar-retrieved (status cb &optional cbargs) - "Callback function used by `gravatar-retrieve'." - ;; Store gravatar? - (when gravatar-automatic-caching - (url-store-in-cache (current-buffer))) - (if (plist-get status :error) - ;; Error happened. - (apply cb 'error cbargs) - (apply cb (gravatar-data->image) cbargs)) - (kill-buffer (current-buffer))) + "Handle Gravatar response data in current buffer. +Return the result of (apply CB DATA CBARGS), where DATA is either +an image descriptor, or the symbol `error' on failure. +This function is intended as a callback for `url-retrieve'." + (let ((data (unless (plist-get status :error) + (gravatar-get-data)))) + (and url-current-object ; Only cache if not already cached. + gravatar-automatic-caching + (url-store-in-cache)) + (prog1 (apply cb (if data (create-image data nil t) 'error) cbargs) + (kill-buffer)))) (provide 'gravatar) -- 2.20.1