]> code.delx.au - gnu-emacs/blob - lisp/gnus/gnus-picon.el
Merge from origin/emacs-25
[gnu-emacs] / lisp / gnus / gnus-picon.el
1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
2
3 ;; Copyright (C) 1996-2016 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news xpm annotation glyph faces
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; There are three picon types relevant to Gnus:
26 ;;
27 ;; Persons: person@subdomain.dom
28 ;; users/dom/subdomain/person/face.gif
29 ;; usenix/dom/subdomain/person/face.gif
30 ;; misc/MISC/person/face.gif
31 ;; Domains: subdomain.dom
32 ;; domain/dom/subdomain/unknown/face.gif
33 ;; Groups: comp.lang.lisp
34 ;; news/comp/lang/lisp/unknown/face.gif
35 ;;
36 ;; Original implementation by Wes Hardaker <hardaker@ece.ucdavis.edu>.
37 ;;
38 ;;; Code:
39
40 (eval-when-compile (require 'cl))
41
42 (require 'gnus)
43 (require 'gnus-art)
44
45 ;;; User variables:
46
47 (defcustom gnus-picon-news-directories '("news")
48 "*List of directories to search for newsgroups faces."
49 :type '(repeat string)
50 :group 'gnus-picon)
51
52 (defcustom gnus-picon-user-directories '("users" "usenix" "local" "misc")
53 "*List of directories to search for user faces."
54 :type '(repeat string)
55 :group 'gnus-picon)
56
57 (defcustom gnus-picon-domain-directories '("domains")
58 "*List of directories to search for domain faces.
59 Some people may want to add \"unknown\" to this list."
60 :type '(repeat string)
61 :group 'gnus-picon)
62
63 (defcustom gnus-picon-file-types
64 (let ((types (list "xbm")))
65 (when (gnus-image-type-available-p 'gif)
66 (push "gif" types))
67 (when (gnus-image-type-available-p 'xpm)
68 (push "xpm" types))
69 types)
70 "*List of suffixes on picon file names to try."
71 :type '(repeat string)
72 :group 'gnus-picon)
73
74 (defcustom gnus-picon-properties '(:color-symbols (("None" . "white")))
75 "List of image properties applied to picons."
76 :type 'plist
77 :version "24.3"
78 :group 'gnus-picon)
79
80 (defcustom gnus-picon-style 'inline
81 "How should picons be displayed.
82 If `inline', the textual representation is replaced. If `right', picons are
83 added right to the textual representation."
84 :type '(choice (const inline)
85 (const right))
86 :group 'gnus-picon)
87
88 (defcustom gnus-picon-inhibit-top-level-domains t
89 "If non-nil, don't piconify top-level domains.
90 These are often not very interesting."
91 :version "24.1"
92 :type 'boolean
93 :group 'gnus-picon)
94
95 ;;; Internal variables:
96
97 (defvar gnus-picon-glyph-alist nil
98 "Picon glyphs cache.
99 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
100 (defvar gnus-picon-cache nil)
101
102 ;;; Functions:
103
104 (defsubst gnus-picon-split-address (address)
105 (setq address (split-string address "@"))
106 (if (stringp (cadr address))
107 (cons (car address) (split-string (cadr address) "\\."))
108 (if (stringp (car address))
109 (split-string (car address) "\\."))))
110
111 (defun gnus-picon-find-face (address directories &optional exact)
112 (let* ((address (gnus-picon-split-address address))
113 (user (pop address))
114 (faddress address)
115 database directory result instance base)
116 (catch 'found
117 (dolist (database gnus-picon-databases)
118 (dolist (directory directories)
119 (setq address faddress
120 base (expand-file-name directory database))
121 (while address
122 (when (setq result (gnus-picon-find-image
123 (concat base "/" (mapconcat 'downcase
124 (reverse address)
125 "/")
126 "/" (downcase user) "/")))
127 (throw 'found result))
128 (if exact
129 (setq address nil)
130 (pop address)))
131 ;; Kludge to search MISC as well. But not in "news".
132 (unless (string= directory "news")
133 (when (setq result (gnus-picon-find-image
134 (concat base "/MISC/" user "/")))
135 (throw 'found result))))))))
136
137 (defun gnus-picon-find-image (directory)
138 (let ((types gnus-picon-file-types)
139 found type file)
140 (while (and (not found)
141 (setq type (pop types)))
142 (setq found (file-exists-p (setq file (concat directory "face." type)))))
143 (if found
144 file
145 nil)))
146
147 (defun gnus-picon-insert-glyph (glyph category &optional nostring)
148 "Insert GLYPH into the buffer.
149 GLYPH can be either a glyph or a string. When NOSTRING, no textual
150 replacement is added."
151 ;; Using NOSTRING prevents wrong BBDB entries with `gnus-picon-style' set to
152 ;; 'right.
153 (if (stringp glyph)
154 (insert glyph)
155 (gnus-add-wash-type category)
156 (gnus-add-image category (car glyph))
157 (gnus-put-image (car glyph) (unless nostring (cdr glyph)) category)))
158
159 (defun gnus-picon-create-glyph (file)
160 (or (cdr (assoc file gnus-picon-glyph-alist))
161 (cdar (push (cons file (apply 'gnus-create-image
162 file nil nil
163 gnus-picon-properties))
164 gnus-picon-glyph-alist))))
165
166 ;;; Functions that does picon transformations:
167
168 (declare-function image-size "image.c" (spec &optional pixels frame))
169
170 (defun gnus-picon-transform-address (header category)
171 (gnus-with-article-headers
172 (let ((addresses
173 (mail-header-parse-addresses
174 ;; mail-header-parse-addresses does not work (reliably) on
175 ;; decoded headers.
176 (or
177 (ignore-errors
178 (mail-encode-encoded-word-string
179 (or (mail-fetch-field header) "")))
180 (mail-fetch-field header))))
181 spec file point cache len)
182 (dolist (address addresses)
183 (setq address (car address))
184 (when (and (stringp address)
185 (setq spec (gnus-picon-split-address address)))
186 (if (setq cache (cdr (assoc address gnus-picon-cache)))
187 (setq spec cache)
188 (when (setq file (or (gnus-picon-find-face
189 address gnus-picon-user-directories)
190 (gnus-picon-find-face
191 (concat "unknown@"
192 (mapconcat
193 'identity (cdr spec) "."))
194 gnus-picon-user-directories)))
195 (setcar spec (cons (gnus-picon-create-glyph file)
196 (car spec))))
197
198 (dotimes (i (- (length spec)
199 (if gnus-picon-inhibit-top-level-domains
200 2 1)))
201 (when (setq file (gnus-picon-find-face
202 (concat "unknown@"
203 (mapconcat
204 'identity (nthcdr (1+ i) spec) "."))
205 gnus-picon-domain-directories t))
206 (setcar (nthcdr (1+ i) spec)
207 (cons (gnus-picon-create-glyph file)
208 (nth (1+ i) spec)))))
209 (setq spec (nreverse spec))
210 (push (cons address spec) gnus-picon-cache))
211
212 (gnus-article-goto-header header)
213 (mail-header-narrow-to-field)
214 (case gnus-picon-style
215 (right
216 (when (= (length addresses) 1)
217 (setq len (apply '+ (mapcar (lambda (x)
218 (condition-case nil
219 (car (image-size (car x)))
220 (error 0))) spec)))
221 (when (> len 0)
222 (goto-char (point-at-eol))
223 (insert (propertize
224 " " 'display
225 (cons 'space
226 (list :align-to (- (window-width) 1 len))))))
227 (goto-char (point-at-eol))
228 (setq point (point-at-eol))
229 (dolist (image spec)
230 (unless (stringp image)
231 (goto-char point)
232 (gnus-picon-insert-glyph image category 'nostring)))))
233 (inline
234 (when (search-forward address nil t)
235 (delete-region (match-beginning 0) (match-end 0))
236 (setq point (point))
237 (while spec
238 (goto-char point)
239 (if (> (length spec) 2)
240 (insert ".")
241 (if (= (length spec) 2)
242 (insert "@")))
243 (gnus-picon-insert-glyph (pop spec) category))))))))))
244
245 (defun gnus-picon-transform-newsgroups (header)
246 (interactive)
247 (gnus-with-article-headers
248 (gnus-article-goto-header header)
249 (mail-header-narrow-to-field)
250 (let ((groups (message-tokenize-header (mail-fetch-field header)))
251 spec file point)
252 (dolist (group groups)
253 (unless (setq spec (cdr (assoc group gnus-picon-cache)))
254 (setq spec (nreverse (split-string group "[.]")))
255 (dotimes (i (length spec))
256 (when (setq file (gnus-picon-find-face
257 (concat "unknown@"
258 (mapconcat
259 'identity (nthcdr i spec) "."))
260 gnus-picon-news-directories t))
261 (setcar (nthcdr i spec)
262 (cons (gnus-picon-create-glyph file)
263 (nth i spec)))))
264 (push (cons group spec) gnus-picon-cache))
265 (when (search-forward group nil t)
266 (delete-region (match-beginning 0) (match-end 0))
267 (save-restriction
268 (narrow-to-region (point) (point))
269 (while spec
270 (goto-char (point-min))
271 (if (> (length spec) 1)
272 (insert "."))
273 (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon))
274 (goto-char (point-max))))))))
275
276 ;;; Commands:
277
278 ;; #### NOTE: the test for buffer-read-only is the same as in
279 ;; article-display-[x-]face. See the comment up there.
280
281 ;;;###autoload
282 (defun gnus-treat-from-picon ()
283 "Display picons in the From header.
284 If picons are already displayed, remove them."
285 (interactive)
286 (let ((wash-picon-p buffer-read-only))
287 (gnus-with-article-buffer
288 (if (and wash-picon-p (memq 'from-picon gnus-article-wash-types))
289 (gnus-delete-images 'from-picon)
290 (gnus-picon-transform-address "from" 'from-picon)))))
291
292 ;;;###autoload
293 (defun gnus-treat-mail-picon ()
294 "Display picons in the Cc and To headers.
295 If picons are already displayed, remove them."
296 (interactive)
297 (let ((wash-picon-p buffer-read-only))
298 (gnus-with-article-buffer
299 (if (and wash-picon-p (memq 'mail-picon gnus-article-wash-types))
300 (gnus-delete-images 'mail-picon)
301 (gnus-picon-transform-address "cc" 'mail-picon)
302 (gnus-picon-transform-address "to" 'mail-picon)))))
303
304 ;;;###autoload
305 (defun gnus-treat-newsgroups-picon ()
306 "Display picons in the Newsgroups and Followup-To headers.
307 If picons are already displayed, remove them."
308 (interactive)
309 (let ((wash-picon-p buffer-read-only))
310 (gnus-with-article-buffer
311 (if (and wash-picon-p (memq 'newsgroups-picon gnus-article-wash-types))
312 (gnus-delete-images 'newsgroups-picon)
313 (gnus-picon-transform-newsgroups "newsgroups")
314 (gnus-picon-transform-newsgroups "followup-to")))))
315
316 (provide 'gnus-picon)
317
318 ;;; gnus-picon.el ends here