1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news xpm annotation glyph faces
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; There are three picon types relevant to Gnus:
28 ;; Persons: person@subdomain.dom
29 ;; users/dom/subdomain/person/face.gif
30 ;; usenix/dom/subdomain/person/face.gif
31 ;; misc/MISC/person/face.gif
32 ;; Domains: subdomain.dom
33 ;; domain/dom/subdomain/unknown/face.gif
34 ;; Groups: comp.lang.lisp
35 ;; news/comp/lang/lisp/unknown/face.gif
37 ;; Original implementation by Wes Hardaker <hardaker@ece.ucdavis.edu>.
43 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
45 (eval-when-compile (require 'cl
))
52 (defcustom gnus-picon-news-directories
'("news")
53 "*List of directories to search for newsgroups faces."
54 :type
'(repeat string
)
57 (defcustom gnus-picon-user-directories
'("users" "usenix" "local" "misc")
58 "*List of directories to search for user faces."
59 :type
'(repeat string
)
62 (defcustom gnus-picon-domain-directories
'("domains")
63 "*List of directories to search for domain faces.
64 Some people may want to add \"unknown\" to this list."
65 :type
'(repeat string
)
68 (defcustom gnus-picon-file-types
69 (let ((types (list "xbm")))
70 (when (gnus-image-type-available-p 'gif
)
72 (when (gnus-image-type-available-p 'xpm
)
75 "*List of suffixes on picon file names to try."
76 :type
'(repeat string
)
79 (defcustom gnus-picon-style
'inline
80 "How should picons be displayed.
81 If `inline', the textual representation is replaced. If `right', picons are
82 added right to the textual representation."
83 ;; FIXME: `right' needs improvement for XEmacs.
84 :type
'(choice (const inline
)
88 (defface gnus-picon-xbm
'((t (:foreground
"black" :background
"white")))
89 "Face to show xbm picon in."
91 ;; backward-compatibility alias
92 (put 'gnus-picon-xbm-face
'face-alias
'gnus-picon-xbm
)
93 (put 'gnus-picon-xbm-face
'obsolete-face
"22.1")
95 (defface gnus-picon
'((t (:foreground
"black" :background
"white")))
96 "Face to show picon in."
98 ;; backward-compatibility alias
99 (put 'gnus-picon-face
'face-alias
'gnus-picon
)
100 (put 'gnus-picon-face
'obsolete-face
"22.1")
102 ;;; Internal variables:
104 (defvar gnus-picon-glyph-alist nil
106 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
107 (defvar gnus-picon-cache nil
)
111 (defsubst gnus-picon-split-address
(address)
112 (setq address
(split-string address
"@"))
113 (if (stringp (cadr address
))
114 (cons (car address
) (split-string (cadr address
) "\\."))
115 (if (stringp (car address
))
116 (split-string (car address
) "\\."))))
118 (defun gnus-picon-find-face (address directories
&optional exact
)
119 (let* ((address (gnus-picon-split-address address
))
122 database directory result instance base
)
124 (dolist (database gnus-picon-databases
)
125 (dolist (directory directories
)
126 (setq address faddress
127 base
(expand-file-name directory database
))
129 (when (setq result
(gnus-picon-find-image
130 (concat base
"/" (mapconcat 'downcase
133 "/" (downcase user
) "/")))
134 (throw 'found result
))
138 ;; Kludge to search MISC as well. But not in "news".
139 (unless (string= directory
"news")
140 (when (setq result
(gnus-picon-find-image
141 (concat base
"/MISC/" user
"/")))
142 (throw 'found result
))))))))
144 (defun gnus-picon-find-image (directory)
145 (let ((types gnus-picon-file-types
)
147 (while (and (not found
)
148 (setq type
(pop types
)))
149 (setq found
(file-exists-p (setq file
(concat directory
"face." type
)))))
154 (defun gnus-picon-insert-glyph (glyph category
&optional nostring
)
155 "Insert GLYPH into the buffer.
156 GLYPH can be either a glyph or a string. When NOSTRING, no textual
157 replacement is added."
158 ;; Using NOSTRING prevents wrong BBDB entries with `gnus-picon-style' set to
162 (gnus-add-wash-type category
)
163 (gnus-add-image category
(car glyph
))
164 (gnus-put-image (car glyph
) (unless nostring
(cdr glyph
)) category
)))
166 (defun gnus-picon-create-glyph (file)
167 (or (cdr (assoc file gnus-picon-glyph-alist
))
168 (cdar (push (cons file
(gnus-create-image file
))
169 gnus-picon-glyph-alist
))))
171 ;;; Functions that does picon transformations:
173 (declare-function image-size
"image.c" (spec &optional pixels frame
))
175 (defun gnus-picon-transform-address (header category
)
176 (gnus-with-article-headers
178 (mail-header-parse-addresses
179 ;; mail-header-parse-addresses does not work (reliably) on
183 (mail-encode-encoded-word-string
184 (or (mail-fetch-field header
) "")))
185 (mail-fetch-field header
))))
186 spec file point cache len
)
187 (dolist (address addresses
)
188 (setq address
(car address
))
189 (when (and (stringp address
)
190 (setq spec
(gnus-picon-split-address address
)))
191 (if (setq cache
(cdr (assoc address gnus-picon-cache
)))
193 (when (setq file
(or (gnus-picon-find-face
194 address gnus-picon-user-directories
)
195 (gnus-picon-find-face
198 'identity
(cdr spec
) "."))
199 gnus-picon-user-directories
)))
200 (setcar spec
(cons (gnus-picon-create-glyph file
)
203 (dotimes (i (1- (length spec
)))
204 (when (setq file
(gnus-picon-find-face
207 'identity
(nthcdr (1+ i
) spec
) "."))
208 gnus-picon-domain-directories t
))
209 (setcar (nthcdr (1+ i
) spec
)
210 (cons (gnus-picon-create-glyph file
)
211 (nth (1+ i
) spec
)))))
212 (setq spec
(nreverse spec
))
213 (push (cons address spec
) gnus-picon-cache
))
215 (gnus-article-goto-header header
)
216 (mail-header-narrow-to-field)
217 (case gnus-picon-style
219 (when (= (length addresses
) 1)
220 (setq len
(apply '+ (mapcar (lambda (x)
222 (car (image-size (car x
)))
225 (goto-char (point-at-eol))
229 (list :align-to
(- (window-width) 1 len
))))))
230 (goto-char (point-at-eol))
231 (setq point
(point-at-eol))
233 (unless (stringp image
)
235 (gnus-picon-insert-glyph image category
'nostring
)))))
237 (when (search-forward address nil t
)
238 (delete-region (match-beginning 0) (match-end 0))
242 (if (> (length spec
) 2)
244 (if (= (length spec
) 2)
246 (gnus-picon-insert-glyph (pop spec
) category
))))))))))
248 (defun gnus-picon-transform-newsgroups (header)
250 (gnus-with-article-headers
251 (gnus-article-goto-header header
)
252 (mail-header-narrow-to-field)
253 (let ((groups (message-tokenize-header (mail-fetch-field header
)))
255 (dolist (group groups
)
256 (unless (setq spec
(cdr (assoc group gnus-picon-cache
)))
257 (setq spec
(nreverse (split-string group
"[.]")))
258 (dotimes (i (length spec
))
259 (when (setq file
(gnus-picon-find-face
262 'identity
(nthcdr i spec
) "."))
263 gnus-picon-news-directories t
))
264 (setcar (nthcdr i spec
)
265 (cons (gnus-picon-create-glyph file
)
267 (push (cons group spec
) gnus-picon-cache
))
268 (when (search-forward group nil t
)
269 (delete-region (match-beginning 0) (match-end 0))
271 (narrow-to-region (point) (point))
273 (goto-char (point-min))
274 (if (> (length spec
) 1)
276 (gnus-picon-insert-glyph (pop spec
) 'newsgroups-picon
))
277 (goto-char (point-max))))))))
281 ;; #### NOTE: the test for buffer-read-only is the same as in
282 ;; article-display-[x-]face. See the comment up there.
285 (defun gnus-treat-from-picon ()
286 "Display picons in the From header.
287 If picons are already displayed, remove them."
289 (let ((wash-picon-p buffer-read-only
))
290 (gnus-with-article-buffer
291 (if (and wash-picon-p
(memq 'from-picon gnus-article-wash-types
))
292 (gnus-delete-images 'from-picon
)
293 (gnus-picon-transform-address "from" 'from-picon
)))))
296 (defun gnus-treat-mail-picon ()
297 "Display picons in the Cc and To headers.
298 If picons are already displayed, remove them."
300 (let ((wash-picon-p buffer-read-only
))
301 (gnus-with-article-buffer
302 (if (and wash-picon-p
(memq 'mail-picon gnus-article-wash-types
))
303 (gnus-delete-images 'mail-picon
)
304 (gnus-picon-transform-address "cc" 'mail-picon
)
305 (gnus-picon-transform-address "to" 'mail-picon
)))))
308 (defun gnus-treat-newsgroups-picon ()
309 "Display picons in the Newsgroups and Followup-To headers.
310 If picons are already displayed, remove them."
312 (let ((wash-picon-p buffer-read-only
))
313 (gnus-with-article-buffer
314 (if (and wash-picon-p
(memq 'newsgroups-picon gnus-article-wash-types
))
315 (gnus-delete-images 'newsgroups-picon
)
316 (gnus-picon-transform-newsgroups "newsgroups")
317 (gnus-picon-transform-newsgroups "followup-to")))))
319 (provide 'gnus-picon
)
321 ;;; gnus-picon.el ends here