1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
3 ;; Copyright (C) 1996-2014 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 ;; FIXME: `right' needs improvement for XEmacs.
85 :type
'(choice (const inline
)
89 (defcustom gnus-picon-inhibit-top-level-domains t
90 "If non-nil, don't piconify top-level domains.
91 These are often not very interesting."
96 ;;; Internal variables:
98 (defvar gnus-picon-glyph-alist nil
100 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
101 (defvar gnus-picon-cache nil
)
105 (defsubst gnus-picon-split-address
(address)
106 (setq address
(split-string address
"@"))
107 (if (stringp (cadr address
))
108 (cons (car address
) (split-string (cadr address
) "\\."))
109 (if (stringp (car address
))
110 (split-string (car address
) "\\."))))
112 (defun gnus-picon-find-face (address directories
&optional exact
)
113 (let* ((address (gnus-picon-split-address address
))
116 database directory result instance base
)
118 (dolist (database gnus-picon-databases
)
119 (dolist (directory directories
)
120 (setq address faddress
121 base
(expand-file-name directory database
))
123 (when (setq result
(gnus-picon-find-image
124 (concat base
"/" (mapconcat 'downcase
127 "/" (downcase user
) "/")))
128 (throw 'found result
))
132 ;; Kludge to search MISC as well. But not in "news".
133 (unless (string= directory
"news")
134 (when (setq result
(gnus-picon-find-image
135 (concat base
"/MISC/" user
"/")))
136 (throw 'found result
))))))))
138 (defun gnus-picon-find-image (directory)
139 (let ((types gnus-picon-file-types
)
141 (while (and (not found
)
142 (setq type
(pop types
)))
143 (setq found
(file-exists-p (setq file
(concat directory
"face." type
)))))
148 (defun gnus-picon-insert-glyph (glyph category
&optional nostring
)
149 "Insert GLYPH into the buffer.
150 GLYPH can be either a glyph or a string. When NOSTRING, no textual
151 replacement is added."
152 ;; Using NOSTRING prevents wrong BBDB entries with `gnus-picon-style' set to
156 (gnus-add-wash-type category
)
157 (gnus-add-image category
(car glyph
))
158 (gnus-put-image (car glyph
) (unless nostring
(cdr glyph
)) category
)))
160 (defun gnus-picon-create-glyph (file)
161 (or (cdr (assoc file gnus-picon-glyph-alist
))
162 (cdar (push (cons file
(apply 'gnus-create-image
164 gnus-picon-properties
))
165 gnus-picon-glyph-alist
))))
167 ;;; Functions that does picon transformations:
169 (declare-function image-size
"image.c" (spec &optional pixels frame
))
171 (defun gnus-picon-transform-address (header category
)
172 (gnus-with-article-headers
174 (mail-header-parse-addresses
175 ;; mail-header-parse-addresses does not work (reliably) on
179 (mail-encode-encoded-word-string
180 (or (mail-fetch-field header
) "")))
181 (mail-fetch-field header
))))
182 spec file point cache len
)
183 (dolist (address addresses
)
184 (setq address
(car address
))
185 (when (and (stringp address
)
186 (setq spec
(gnus-picon-split-address address
)))
187 (if (setq cache
(cdr (assoc address gnus-picon-cache
)))
189 (when (setq file
(or (gnus-picon-find-face
190 address gnus-picon-user-directories
)
191 (gnus-picon-find-face
194 'identity
(cdr spec
) "."))
195 gnus-picon-user-directories
)))
196 (setcar spec
(cons (gnus-picon-create-glyph file
)
199 (dotimes (i (- (length spec
)
200 (if gnus-picon-inhibit-top-level-domains
202 (when (setq file
(gnus-picon-find-face
205 'identity
(nthcdr (1+ i
) spec
) "."))
206 gnus-picon-domain-directories t
))
207 (setcar (nthcdr (1+ i
) spec
)
208 (cons (gnus-picon-create-glyph file
)
209 (nth (1+ i
) spec
)))))
210 (setq spec
(nreverse spec
))
211 (push (cons address spec
) gnus-picon-cache
))
213 (gnus-article-goto-header header
)
214 (mail-header-narrow-to-field)
215 (case gnus-picon-style
217 (when (= (length addresses
) 1)
218 (setq len
(apply '+ (mapcar (lambda (x)
220 (car (image-size (car x
)))
223 (goto-char (point-at-eol))
227 (list :align-to
(- (window-width) 1 len
))))))
228 (goto-char (point-at-eol))
229 (setq point
(point-at-eol))
231 (unless (stringp image
)
233 (gnus-picon-insert-glyph image category
'nostring
)))))
235 (when (search-forward address nil t
)
236 (delete-region (match-beginning 0) (match-end 0))
240 (if (> (length spec
) 2)
242 (if (= (length spec
) 2)
244 (gnus-picon-insert-glyph (pop spec
) category
))))))))))
246 (defun gnus-picon-transform-newsgroups (header)
248 (gnus-with-article-headers
249 (gnus-article-goto-header header
)
250 (mail-header-narrow-to-field)
251 (let ((groups (message-tokenize-header (mail-fetch-field header
)))
253 (dolist (group groups
)
254 (unless (setq spec
(cdr (assoc group gnus-picon-cache
)))
255 (setq spec
(nreverse (split-string group
"[.]")))
256 (dotimes (i (length spec
))
257 (when (setq file
(gnus-picon-find-face
260 'identity
(nthcdr i spec
) "."))
261 gnus-picon-news-directories t
))
262 (setcar (nthcdr i spec
)
263 (cons (gnus-picon-create-glyph file
)
265 (push (cons group spec
) gnus-picon-cache
))
266 (when (search-forward group nil t
)
267 (delete-region (match-beginning 0) (match-end 0))
269 (narrow-to-region (point) (point))
271 (goto-char (point-min))
272 (if (> (length spec
) 1)
274 (gnus-picon-insert-glyph (pop spec
) 'newsgroups-picon
))
275 (goto-char (point-max))))))))
279 ;; #### NOTE: the test for buffer-read-only is the same as in
280 ;; article-display-[x-]face. See the comment up there.
283 (defun gnus-treat-from-picon ()
284 "Display picons in the From header.
285 If picons are already displayed, remove them."
287 (let ((wash-picon-p buffer-read-only
))
288 (gnus-with-article-buffer
289 (if (and wash-picon-p
(memq 'from-picon gnus-article-wash-types
))
290 (gnus-delete-images 'from-picon
)
291 (gnus-picon-transform-address "from" 'from-picon
)))))
294 (defun gnus-treat-mail-picon ()
295 "Display picons in the Cc and To headers.
296 If picons are already displayed, remove them."
298 (let ((wash-picon-p buffer-read-only
))
299 (gnus-with-article-buffer
300 (if (and wash-picon-p
(memq 'mail-picon gnus-article-wash-types
))
301 (gnus-delete-images 'mail-picon
)
302 (gnus-picon-transform-address "cc" 'mail-picon
)
303 (gnus-picon-transform-address "to" 'mail-picon
)))))
306 (defun gnus-treat-newsgroups-picon ()
307 "Display picons in the Newsgroups and Followup-To headers.
308 If picons are already displayed, remove them."
310 (let ((wash-picon-p buffer-read-only
))
311 (gnus-with-article-buffer
312 (if (and wash-picon-p
(memq 'newsgroups-picon gnus-article-wash-types
))
313 (gnus-delete-images 'newsgroups-picon
)
314 (gnus-picon-transform-newsgroups "newsgroups")
315 (gnus-picon-transform-newsgroups "followup-to")))))
317 (provide 'gnus-picon
)
319 ;;; gnus-picon.el ends here