diff options
| author | Basil L. Contovounesios | 2019-07-22 22:06:22 +0100 |
|---|---|---|
| committer | Basil L. Contovounesios | 2019-08-02 16:33:30 +0300 |
| commit | 60eb0a4834305e1c2b31b1e817875f3d8d0be5f5 (patch) | |
| tree | d3f0655a672ebaad1325aaffc937c9bd3ff7c390 | |
| parent | 87ec668e95084af45bec010de36493fb90a26461 (diff) | |
| download | emacs-60eb0a4834305e1c2b31b1e817875f3d8d0be5f5.tar.gz emacs-60eb0a4834305e1c2b31b1e817875f3d8d0be5f5.zip | |
Use lexical-binding for Gravatar support
For discussion, see the following thread:
https://lists.gnu.org/archive/html/emacs-devel/2019-07/msg00528.html
* lisp/gnus/gnus-gravatar.el: Use lexical-binding. Link custom
group 'gnus-gravatar' to 'gravatar'.
(gnus-gravatar-size, gnus-gravatar-too-ugly): Doc fix.
(gnus-gravatar-insert): Check liveness of article buffer sooner.
(gnus-treat-from-gravatar, gnus-treat-mail-gravatar): Use
interactive spec "p" instead of emulating it.
* lisp/image/gravatar.el: Use lexical-binding.
(gravatar-cache-expired): Remove. Change all callers to use
url-cache-expired instead.
(gravatar-get-data, gravatar-retrieve)
(gravatar-retrieve-synchronously): Simplify.
| -rw-r--r-- | lisp/gnus/gnus-gravatar.el | 101 | ||||
| -rw-r--r-- | lisp/image/gravatar.el | 60 |
2 files changed, 71 insertions, 90 deletions
diff --git a/lisp/gnus/gnus-gravatar.el b/lisp/gnus/gnus-gravatar.el index 19cbf529c65..ec3f909161f 100644 --- a/lisp/gnus/gnus-gravatar.el +++ b/lisp/gnus/gnus-gravatar.el | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | ;;; gnus-gravatar.el --- Gnus Gravatar support | 1 | ;;; gnus-gravatar.el --- Gnus Gravatar support -*- lexical-binding: t -*- |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 2010-2019 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 2010-2019 Free Software Foundation, Inc. |
| 4 | 4 | ||
| 5 | ;; Author: Julien Danjou <julien@danjou.info> | 5 | ;; Author: Julien Danjou <julien@danjou.info> |
| 6 | ;; Keywords: news | 6 | ;; Keywords: multimedia, news |
| 7 | 7 | ||
| 8 | ;; This file is part of GNU Emacs. | 8 | ;; This file is part of GNU Emacs. |
| 9 | 9 | ||
| @@ -29,13 +29,15 @@ | |||
| 29 | (require 'mail-extr) ;; Because of binding `mail-extr-disable-voodoo'. | 29 | (require 'mail-extr) ;; Because of binding `mail-extr-disable-voodoo'. |
| 30 | 30 | ||
| 31 | (defgroup gnus-gravatar nil | 31 | (defgroup gnus-gravatar nil |
| 32 | "Gnus Gravatar." | 32 | "Gravatars in Gnus." |
| 33 | :link '(custom-group-link gravatar) | ||
| 33 | :group 'gnus-visual) | 34 | :group 'gnus-visual) |
| 34 | 35 | ||
| 35 | (defcustom gnus-gravatar-size nil | 36 | (defcustom gnus-gravatar-size nil |
| 36 | "How big should gravatars be displayed. | 37 | "Size in pixels at which gravatars should be displayed. |
| 37 | If nil, default to `gravatar-size'." | 38 | If nil, default to `gravatar-size'." |
| 38 | :type '(choice (const nil) integer) | 39 | :type '(choice (const :tag "Default" nil) |
| 40 | (integer :tag "Pixels")) | ||
| 39 | :version "24.1" | 41 | :version "24.1" |
| 40 | :group 'gnus-gravatar) | 42 | :group 'gnus-gravatar) |
| 41 | 43 | ||
| @@ -48,7 +50,7 @@ If nil, default to `gravatar-size'." | |||
| 48 | (defcustom gnus-gravatar-too-ugly gnus-article-x-face-too-ugly | 50 | (defcustom gnus-gravatar-too-ugly gnus-article-x-face-too-ugly |
| 49 | "Regexp matching posters whose avatar shouldn't be shown automatically. | 51 | "Regexp matching posters whose avatar shouldn't be shown automatically. |
| 50 | If nil, show all avatars." | 52 | If nil, show all avatars." |
| 51 | :type '(choice regexp (const nil)) | 53 | :type '(choice regexp (const :tag "Allow all" nil)) |
| 52 | :version "24.1" | 54 | :version "24.1" |
| 53 | :group 'gnus-gravatar) | 55 | :group 'gnus-gravatar) |
| 54 | 56 | ||
| @@ -74,56 +76,57 @@ If nil, show all avatars." | |||
| 74 | (ignore-errors | 76 | (ignore-errors |
| 75 | (gravatar-retrieve | 77 | (gravatar-retrieve |
| 76 | (cadr address) | 78 | (cadr address) |
| 77 | 'gnus-gravatar-insert | 79 | #'gnus-gravatar-insert |
| 78 | (list header address category)))))))) | 80 | (list header address category)))))))) |
| 79 | 81 | ||
| 80 | (defun gnus-gravatar-insert (gravatar header address category) | 82 | (defun gnus-gravatar-insert (gravatar header address category) |
| 81 | "Insert GRAVATAR for ADDRESS in HEADER in current article buffer. | 83 | "Insert GRAVATAR for ADDRESS in HEADER in current article buffer. |
| 82 | Set image category to CATEGORY." | 84 | Set image category to CATEGORY. This function is intended as a |
| 85 | callback for `gravatar-retrieve'." | ||
| 83 | (unless (eq gravatar 'error) | 86 | (unless (eq gravatar 'error) |
| 84 | (gnus-with-article-buffer | 87 | (gnus-with-article-buffer |
| 85 | (let ((mark (point-marker)) | 88 | ;; The buffer can be gone at this time. |
| 86 | (inhibit-point-motion-hooks t) | 89 | (when (buffer-live-p (current-buffer)) |
| 87 | (case-fold-search t)) | 90 | (let ((real-name (car address)) |
| 88 | (save-restriction | 91 | (mail-address (cadr address)) |
| 89 | (article-narrow-to-head) | 92 | (mark (point-marker)) |
| 90 | ;; The buffer can be gone at this time | 93 | (inhibit-point-motion-hooks t) |
| 91 | (when (buffer-live-p (current-buffer)) | 94 | (case-fold-search t)) |
| 95 | (save-restriction | ||
| 96 | (article-narrow-to-head) | ||
| 92 | (gnus-article-goto-header header) | 97 | (gnus-article-goto-header header) |
| 93 | (mail-header-narrow-to-field) | 98 | (mail-header-narrow-to-field) |
| 94 | (let ((real-name (car address)) | 99 | (when (if real-name |
| 95 | (mail-address (cadr address))) | 100 | (re-search-forward |
| 96 | (when (if real-name | 101 | (concat (replace-regexp-in-string |
| 97 | (re-search-forward | 102 | "[\t ]+" "[\t\n ]+" |
| 98 | (concat (replace-regexp-in-string | 103 | (regexp-quote real-name)) |
| 99 | "[\t ]+" "[\t\n ]+" | 104 | "\\|" |
| 100 | (regexp-quote real-name)) | 105 | (regexp-quote mail-address)) |
| 101 | "\\|" | 106 | nil t) |
| 102 | (regexp-quote mail-address)) | 107 | (search-forward mail-address nil t)) |
| 103 | nil t) | 108 | (goto-char (1- (match-beginning 0))) |
| 104 | (search-forward mail-address nil t)) | 109 | ;; If we're on the " quoting the name, go backward. |
| 105 | (goto-char (1- (match-beginning 0))) | 110 | (when (looking-at-p "[\"<]") |
| 106 | ;; If we're on the " quoting the name, go backward | 111 | (goto-char (1- (point)))) |
| 107 | (when (looking-at "[\"<]") | 112 | ;; Do not do anything if there's already a gravatar. This can |
| 108 | (goto-char (1- (point)))) | 113 | ;; happen if the buffer has been regenerated in the mean time, for |
| 109 | ;; Do not do anything if there's already a gravatar. This can | 114 | ;; example we were fetching someaddress, and then we change to |
| 110 | ;; happens if the buffer has been regenerated in the mean time, for | 115 | ;; another mail with the same someaddress. |
| 111 | ;; example we were fetching someaddress, and then we change to | 116 | (unless (get-text-property (point) 'gnus-gravatar) |
| 112 | ;; another mail with the same someaddress. | 117 | (let ((pos (point))) |
| 113 | (unless (memq 'gnus-gravatar (text-properties-at (point))) | 118 | (setq gravatar (append gravatar gnus-gravatar-properties)) |
| 114 | (let ((point (point))) | 119 | (gnus-put-image gravatar (buffer-substring pos (1+ pos)) category) |
| 115 | (setq gravatar (append gravatar gnus-gravatar-properties)) | 120 | (put-text-property pos (point) 'gnus-gravatar address) |
| 116 | (gnus-put-image gravatar (buffer-substring (point) (1+ point)) category) | 121 | (gnus-add-wash-type category) |
| 117 | (put-text-property point (point) 'gnus-gravatar address) | 122 | (gnus-add-image category gravatar))))) |
| 118 | (gnus-add-wash-type category) | 123 | (goto-char mark)))))) |
| 119 | (gnus-add-image category gravatar))))))) | ||
| 120 | (goto-char (marker-position mark)))))) | ||
| 121 | 124 | ||
| 122 | ;;;###autoload | 125 | ;;;###autoload |
| 123 | (defun gnus-treat-from-gravatar (&optional force) | 126 | (defun gnus-treat-from-gravatar (&optional force) |
| 124 | "Display gravatar in the From header. | 127 | "Display gravatar in the From header. |
| 125 | If gravatar is already displayed, remove it." | 128 | If gravatar is already displayed, remove it." |
| 126 | (interactive (list t)) ;; When type `W D g' | 129 | (interactive "p") |
| 127 | (gnus-with-article-buffer | 130 | (gnus-with-article-buffer |
| 128 | (if (memq 'from-gravatar gnus-article-wash-types) | 131 | (if (memq 'from-gravatar gnus-article-wash-types) |
| 129 | (gnus-delete-images 'from-gravatar) | 132 | (gnus-delete-images 'from-gravatar) |
| @@ -133,12 +136,12 @@ If gravatar is already displayed, remove it." | |||
| 133 | (defun gnus-treat-mail-gravatar (&optional force) | 136 | (defun gnus-treat-mail-gravatar (&optional force) |
| 134 | "Display gravatars in the Cc and To headers. | 137 | "Display gravatars in the Cc and To headers. |
| 135 | If gravatars are already displayed, remove them." | 138 | If gravatars are already displayed, remove them." |
| 136 | (interactive (list t)) ;; When type `W D h' | 139 | (interactive "p") |
| 137 | (gnus-with-article-buffer | 140 | (gnus-with-article-buffer |
| 138 | (if (memq 'mail-gravatar gnus-article-wash-types) | 141 | (if (memq 'mail-gravatar gnus-article-wash-types) |
| 139 | (gnus-delete-images 'mail-gravatar) | 142 | (gnus-delete-images 'mail-gravatar) |
| 140 | (gnus-gravatar-transform-address "cc" 'mail-gravatar force) | 143 | (gnus-gravatar-transform-address "cc" 'mail-gravatar force) |
| 141 | (gnus-gravatar-transform-address "to" 'mail-gravatar force)))) | 144 | (gnus-gravatar-transform-address "to" 'mail-gravatar force)))) |
| 142 | 145 | ||
| 143 | (provide 'gnus-gravatar) | 146 | (provide 'gnus-gravatar) |
| 144 | 147 | ||
diff --git a/lisp/image/gravatar.el b/lisp/image/gravatar.el index 9a1ec3b556b..ea746b71d7b 100644 --- a/lisp/image/gravatar.el +++ b/lisp/image/gravatar.el | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | ;;; gravatar.el --- Get Gravatars | 1 | ;;; gravatar.el --- Get Gravatars -*- lexical-binding: t -*- |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 2010-2019 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 2010-2019 Free Software Foundation, Inc. |
| 4 | 4 | ||
| 5 | ;; Author: Julien Danjou <julien@danjou.info> | 5 | ;; Author: Julien Danjou <julien@danjou.info> |
| 6 | ;; Keywords: news | 6 | ;; Keywords: comm, multimedia |
| 7 | 7 | ||
| 8 | ;; This file is part of GNU Emacs. | 8 | ;; This file is part of GNU Emacs. |
| 9 | 9 | ||
| @@ -26,10 +26,9 @@ | |||
| 26 | 26 | ||
| 27 | (require 'url) | 27 | (require 'url) |
| 28 | (require 'url-cache) | 28 | (require 'url-cache) |
| 29 | (require 'image) | ||
| 30 | 29 | ||
| 31 | (defgroup gravatar nil | 30 | (defgroup gravatar nil |
| 32 | "Gravatar." | 31 | "Gravatars." |
| 33 | :version "24.1" | 32 | :version "24.1" |
| 34 | :group 'comm) | 33 | :group 'comm) |
| 35 | 34 | ||
| @@ -88,22 +87,13 @@ Valid sizes range from 1 to 2048 inclusive." | |||
| 88 | gravatar-rating | 87 | gravatar-rating |
| 89 | gravatar-size)) | 88 | gravatar-size)) |
| 90 | 89 | ||
| 91 | (defun gravatar-cache-expired (url) | ||
| 92 | "Check if URL is cached for more than `gravatar-cache-ttl'." | ||
| 93 | (cond (url-standalone-mode | ||
| 94 | (not (file-exists-p (url-cache-create-filename url)))) | ||
| 95 | (t (let ((cache-time (url-is-cached url))) | ||
| 96 | (if cache-time | ||
| 97 | (time-less-p (time-add cache-time gravatar-cache-ttl) nil) | ||
| 98 | t))))) | ||
| 99 | |||
| 100 | (defun gravatar-get-data () | 90 | (defun gravatar-get-data () |
| 101 | "Get data from current buffer." | 91 | "Return body of current URL buffer, or nil on failure." |
| 102 | (save-excursion | 92 | (save-excursion |
| 103 | (goto-char (point-min)) | 93 | (goto-char (point-min)) |
| 104 | (when (re-search-forward "^HTTP/.+ 200 OK$" nil (line-end-position)) | 94 | (and (re-search-forward "^HTTP/.+ 200 OK$" nil (line-end-position)) |
| 105 | (when (search-forward "\n\n" nil t) | 95 | (search-forward "\n\n" nil t) |
| 106 | (buffer-substring (point) (point-max)))))) | 96 | (buffer-substring (point) (point-max))))) |
| 107 | 97 | ||
| 108 | (defun gravatar-data->image () | 98 | (defun gravatar-data->image () |
| 109 | "Get data of current buffer and return an image. | 99 | "Get data of current buffer and return an image. |
| @@ -113,29 +103,20 @@ If no image available, return 'error." | |||
| 113 | (create-image data nil t) | 103 | (create-image data nil t) |
| 114 | 'error))) | 104 | 'error))) |
| 115 | 105 | ||
| 116 | (autoload 'help-function-arglist "help-fns") | ||
| 117 | |||
| 118 | ;;;###autoload | 106 | ;;;###autoload |
| 119 | (defun gravatar-retrieve (mail-address cb &optional cbargs) | 107 | (defun gravatar-retrieve (mail-address callback &optional cbargs) |
| 120 | "Asynchronously retrieve a gravatar for MAIL-ADDRESS. | 108 | "Asynchronously retrieve a gravatar for MAIL-ADDRESS. |
| 121 | When finished, call CB as (apply CB GRAVATAR CBARGS), | 109 | When finished, call CALLBACK as (apply CALLBACK GRAVATAR CBARGS), |
| 122 | where GRAVATAR is either an image descriptor, or the symbol | 110 | where GRAVATAR is either an image descriptor, or the symbol |
| 123 | `error' if the retrieval failed." | 111 | `error' if the retrieval failed." |
| 124 | (let ((url (gravatar-build-url mail-address))) | 112 | (let ((url (gravatar-build-url mail-address))) |
| 125 | (if (gravatar-cache-expired url) | 113 | (if (url-cache-expired url gravatar-cache-ttl) |
| 126 | (let ((args (list url | 114 | (url-retrieve url #'gravatar-retrieved (list callback cbargs) t) |
| 127 | 'gravatar-retrieved | 115 | (apply callback |
| 128 | (list cb (when cbargs cbargs))))) | 116 | (with-temp-buffer |
| 129 | (when (> (length (help-function-arglist 'url-retrieve)) | 117 | (url-cache-extract (url-cache-create-filename url)) |
| 130 | 4) | 118 | (gravatar-data->image)) |
| 131 | (setq args (nconc args (list t)))) | 119 | cbargs)))) |
| 132 | (apply #'url-retrieve args)) | ||
| 133 | (apply cb | ||
| 134 | (with-temp-buffer | ||
| 135 | (set-buffer-multibyte nil) | ||
| 136 | (url-cache-extract (url-cache-create-filename url)) | ||
| 137 | (gravatar-data->image)) | ||
| 138 | cbargs)))) | ||
| 139 | 120 | ||
| 140 | ;;;###autoload | 121 | ;;;###autoload |
| 141 | (defun gravatar-retrieve-synchronously (mail-address) | 122 | (defun gravatar-retrieve-synchronously (mail-address) |
| @@ -143,19 +124,16 @@ where GRAVATAR is either an image descriptor, or the symbol | |||
| 143 | Value is either an image descriptor, or the symbol `error' if the | 124 | Value is either an image descriptor, or the symbol `error' if the |
| 144 | retrieval failed." | 125 | retrieval failed." |
| 145 | (let ((url (gravatar-build-url mail-address))) | 126 | (let ((url (gravatar-build-url mail-address))) |
| 146 | (if (gravatar-cache-expired url) | 127 | (if (url-cache-expired url gravatar-cache-ttl) |
| 147 | (with-current-buffer (url-retrieve-synchronously url) | 128 | (with-current-buffer (url-retrieve-synchronously url) |
| 148 | (when gravatar-automatic-caching | 129 | (when gravatar-automatic-caching |
| 149 | (url-store-in-cache (current-buffer))) | 130 | (url-store-in-cache (current-buffer))) |
| 150 | (let ((data (gravatar-data->image))) | 131 | (prog1 (gravatar-data->image) |
| 151 | (kill-buffer (current-buffer)) | 132 | (kill-buffer (current-buffer)))) |
| 152 | data)) | ||
| 153 | (with-temp-buffer | 133 | (with-temp-buffer |
| 154 | (set-buffer-multibyte nil) | ||
| 155 | (url-cache-extract (url-cache-create-filename url)) | 134 | (url-cache-extract (url-cache-create-filename url)) |
| 156 | (gravatar-data->image))))) | 135 | (gravatar-data->image))))) |
| 157 | 136 | ||
| 158 | |||
| 159 | (defun gravatar-retrieved (status cb &optional cbargs) | 137 | (defun gravatar-retrieved (status cb &optional cbargs) |
| 160 | "Callback function used by `gravatar-retrieve'." | 138 | "Callback function used by `gravatar-retrieve'." |
| 161 | ;; Store gravatar? | 139 | ;; Store gravatar? |