1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
3 ;; Copyright (C) 1996-2017 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news xpm annotation glyph faces
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;; There are three picon types relevant to Gnus:
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
36 ;; Original implementation by Wes Hardaker <hardaker@ece.ucdavis.edu>.
40 (eval-when-compile (require 'cl
))
47 (defcustom gnus-picon-news-directories
'("news")
48 "List of directories to search for newsgroups faces."
49 :type
'(repeat string
)
52 (defcustom gnus-picon-user-directories
'("users" "usenix" "local" "misc")
53 "List of directories to search for user faces."
54 :type
'(repeat string
)
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
)
63 (defcustom gnus-picon-file-types
64 (let ((types (list "xbm")))
65 (when (gnus-image-type-available-p 'gif
)
67 (when (gnus-image-type-available-p 'xpm
)
70 "List of suffixes on picon file names to try."
71 :type
'(repeat string
)
74 (defcustom gnus-picon-properties
'(:color-symbols
(("None" .
"white")))
75 "List of image properties applied to picons."
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
)
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."
95 ;;; Internal variables:
97 (defvar gnus-picon-glyph-alist nil
99 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
100 (defvar gnus-picon-cache nil
)
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
) "\\."))))
111 (defun gnus-picon-find-face (address directories
&optional exact
)
112 (let* ((address (gnus-picon-split-address address
))
115 database directory result instance base
)
117 (dolist (database gnus-picon-databases
)
118 (dolist (directory directories
)
119 (setq address faddress
120 base
(expand-file-name directory database
))
122 (when (setq result
(gnus-picon-find-image
123 (concat base
"/" (mapconcat 'downcase
126 "/" (downcase user
) "/")))
127 (throw 'found result
))
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
))))))))
137 (defun gnus-picon-find-image (directory)
138 (let ((types gnus-picon-file-types
)
140 (while (and (not found
)
141 (setq type
(pop types
)))
142 (setq found
(file-exists-p (setq file
(concat directory
"face." type
)))))
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
155 (gnus-add-wash-type category
)
156 (gnus-add-image category
(car glyph
))
157 (gnus-put-image (car glyph
) (unless nostring
(cdr glyph
)) category
)))
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
163 gnus-picon-properties
))
164 gnus-picon-glyph-alist
))))
166 ;;; Functions that does picon transformations:
168 (declare-function image-size
"image.c" (spec &optional pixels frame
))
170 (defun gnus-picon-transform-address (header category
)
171 (gnus-with-article-headers
173 (mail-header-parse-addresses
174 ;; mail-header-parse-addresses does not work (reliably) on
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
)))
188 (when (setq file
(or (gnus-picon-find-face
189 address gnus-picon-user-directories
)
190 (gnus-picon-find-face
193 'identity
(cdr spec
) "."))
194 gnus-picon-user-directories
)))
195 (setcar spec
(cons (gnus-picon-create-glyph file
)
198 (dotimes (i (- (length spec
)
199 (if gnus-picon-inhibit-top-level-domains
201 (when (setq file
(gnus-picon-find-face
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
))
212 (gnus-article-goto-header header
)
213 (mail-header-narrow-to-field)
214 (case gnus-picon-style
216 (when (= (length addresses
) 1)
217 (setq len
(apply '+ (mapcar (lambda (x)
219 (car (image-size (car x
)))
222 (goto-char (point-at-eol))
226 (list :align-to
(- (window-width) 1 len
))))))
227 (goto-char (point-at-eol))
228 (setq point
(point-at-eol))
230 (unless (stringp image
)
232 (gnus-picon-insert-glyph image category
'nostring
)))))
234 (when (search-forward address nil t
)
235 (delete-region (match-beginning 0) (match-end 0))
239 (if (> (length spec
) 2)
241 (if (= (length spec
) 2)
243 (gnus-picon-insert-glyph (pop spec
) category
))))))))))
245 (defun gnus-picon-transform-newsgroups (header)
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
)))
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
259 'identity
(nthcdr i spec
) "."))
260 gnus-picon-news-directories t
))
261 (setcar (nthcdr i spec
)
262 (cons (gnus-picon-create-glyph file
)
264 (push (cons group spec
) gnus-picon-cache
))
265 (when (search-forward group nil t
)
266 (delete-region (match-beginning 0) (match-end 0))
268 (narrow-to-region (point) (point))
270 (goto-char (point-min))
271 (if (> (length spec
) 1)
273 (gnus-picon-insert-glyph (pop spec
) 'newsgroups-picon
))
274 (goto-char (point-max))))))))
278 ;; #### NOTE: the test for buffer-read-only is the same as in
279 ;; article-display-[x-]face. See the comment up there.
282 (defun gnus-treat-from-picon ()
283 "Display picons in the From header.
284 If picons are already displayed, remove them."
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
)))))
293 (defun gnus-treat-mail-picon ()
294 "Display picons in the Cc and To headers.
295 If picons are already displayed, remove them."
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
)))))
305 (defun gnus-treat-newsgroups-picon ()
306 "Display picons in the Newsgroups and Followup-To headers.
307 If picons are already displayed, remove them."
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")))))
316 (provide 'gnus-picon
)
318 ;;; gnus-picon.el ends here