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-setup-p nil
)
105 (defvar gnus-picon-glyph-alist nil
107 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
108 (defvar gnus-picon-cache nil
)
112 (defsubst gnus-picon-split-address
(address)
113 (setq address
(split-string address
"@"))
114 (if (stringp (cadr address
))
115 (cons (car address
) (split-string (cadr address
) "\\."))
116 (if (stringp (car address
))
117 (split-string (car address
) "\\."))))
119 (defun gnus-picon-find-face (address directories
&optional exact
)
120 (let* ((address (gnus-picon-split-address address
))
123 database directory result instance base
)
125 (dolist (database gnus-picon-databases
)
126 (dolist (directory directories
)
127 (setq address faddress
128 base
(expand-file-name directory database
))
130 (when (setq result
(gnus-picon-find-image
131 (concat base
"/" (mapconcat 'downcase
134 "/" (downcase user
) "/")))
135 (throw 'found result
))
139 ;; Kludge to search MISC as well. But not in "news".
140 (unless (string= directory
"news")
141 (when (setq result
(gnus-picon-find-image
142 (concat base
"/MISC/" user
"/")))
143 (throw 'found result
))))))))
145 (defun gnus-picon-find-image (directory)
146 (let ((types gnus-picon-file-types
)
148 (while (and (not found
)
149 (setq type
(pop types
)))
150 (setq found
(file-exists-p (setq file
(concat directory
"face." type
)))))
155 (defun gnus-picon-insert-glyph (glyph category
&optional nostring
)
156 "Insert GLYPH into the buffer.
157 GLYPH can be either a glyph or a string. When NOSTRING, no textual
158 replacement is added."
159 ;; Using NOSTRING prevents wrong BBDB entries with `gnus-picon-style' set to
163 (gnus-add-wash-type category
)
164 (gnus-add-image category
(car glyph
))
165 (gnus-put-image (car glyph
) (unless nostring
(cdr glyph
)) category
)))
167 (defun gnus-picon-create-glyph (file)
168 (or (cdr (assoc file gnus-picon-glyph-alist
))
169 (cdar (push (cons file
(gnus-create-image file
))
170 gnus-picon-glyph-alist
))))
172 ;;; Functions that does picon transformations:
174 (declare-function image-size
"image.c" (spec &optional pixels frame
))
176 (defun gnus-picon-transform-address (header category
)
177 (gnus-with-article-headers
179 (mail-header-parse-addresses
180 ;; mail-header-parse-addresses does not work (reliably) on
184 (mail-encode-encoded-word-string
185 (or (mail-fetch-field header
) "")))
186 (mail-fetch-field header
))))
187 spec file point cache len
)
188 (dolist (address addresses
)
189 (setq address
(car address
))
190 (when (and (stringp address
)
191 (setq spec
(gnus-picon-split-address address
)))
192 (if (setq cache
(cdr (assoc address gnus-picon-cache
)))
194 (when (setq file
(or (gnus-picon-find-face
195 address gnus-picon-user-directories
)
196 (gnus-picon-find-face
199 'identity
(cdr spec
) "."))
200 gnus-picon-user-directories
)))
201 (setcar spec
(cons (gnus-picon-create-glyph file
)
204 (dotimes (i (1- (length spec
)))
205 (when (setq file
(gnus-picon-find-face
208 'identity
(nthcdr (1+ i
) spec
) "."))
209 gnus-picon-domain-directories t
))
210 (setcar (nthcdr (1+ i
) spec
)
211 (cons (gnus-picon-create-glyph file
)
212 (nth (1+ i
) spec
)))))
213 (setq spec
(nreverse spec
))
214 (push (cons address spec
) gnus-picon-cache
))
216 (gnus-article-goto-header header
)
217 (mail-header-narrow-to-field)
218 (case gnus-picon-style
220 (when (= (length addresses
) 1)
221 (setq len
(apply '+ (mapcar (lambda (x)
223 (car (image-size (car x
)))
226 (goto-char (point-at-eol))
230 (list :align-to
(- (window-width) 1 len
))))))
231 (goto-char (point-at-eol))
232 (setq point
(point-at-eol))
234 (unless (stringp image
)
236 (gnus-picon-insert-glyph image category
'nostring
)))))
238 (when (search-forward address nil t
)
239 (delete-region (match-beginning 0) (match-end 0))
243 (if (> (length spec
) 2)
245 (if (= (length spec
) 2)
247 (gnus-picon-insert-glyph (pop spec
) category
))))))))))
249 (defun gnus-picon-transform-newsgroups (header)
251 (gnus-with-article-headers
252 (gnus-article-goto-header header
)
253 (mail-header-narrow-to-field)
254 (let ((groups (message-tokenize-header (mail-fetch-field header
)))
256 (dolist (group groups
)
257 (unless (setq spec
(cdr (assoc group gnus-picon-cache
)))
258 (setq spec
(nreverse (split-string group
"[.]")))
259 (dotimes (i (length spec
))
260 (when (setq file
(gnus-picon-find-face
263 'identity
(nthcdr i spec
) "."))
264 gnus-picon-news-directories t
))
265 (setcar (nthcdr i spec
)
266 (cons (gnus-picon-create-glyph file
)
268 (push (cons group spec
) gnus-picon-cache
))
269 (when (search-forward group nil t
)
270 (delete-region (match-beginning 0) (match-end 0))
272 (narrow-to-region (point) (point))
274 (goto-char (point-min))
275 (if (> (length spec
) 1)
277 (gnus-picon-insert-glyph (pop spec
) 'newsgroups-picon
))
278 (goto-char (point-max))))))))
282 ;; #### NOTE: the test for buffer-read-only is the same as in
283 ;; article-display-[x-]face. See the comment up there.
286 (defun gnus-treat-from-picon ()
287 "Display picons in the From header.
288 If picons are already displayed, remove them."
290 (let ((wash-picon-p buffer-read-only
))
291 (gnus-with-article-buffer
292 (if (and wash-picon-p
(memq 'from-picon gnus-article-wash-types
))
293 (gnus-delete-images 'from-picon
)
294 (gnus-picon-transform-address "from" 'from-picon
)))))
297 (defun gnus-treat-mail-picon ()
298 "Display picons in the Cc and To headers.
299 If picons are already displayed, remove them."
301 (let ((wash-picon-p buffer-read-only
))
302 (gnus-with-article-buffer
303 (if (and wash-picon-p
(memq 'mail-picon gnus-article-wash-types
))
304 (gnus-delete-images 'mail-picon
)
305 (gnus-picon-transform-address "cc" 'mail-picon
)
306 (gnus-picon-transform-address "to" 'mail-picon
)))))
309 (defun gnus-treat-newsgroups-picon ()
310 "Display picons in the Newsgroups and Followup-To headers.
311 If picons are already displayed, remove them."
313 (let ((wash-picon-p buffer-read-only
))
314 (gnus-with-article-buffer
315 (if (and wash-picon-p
(memq 'newsgroups-picon gnus-article-wash-types
))
316 (gnus-delete-images 'newsgroups-picon
)
317 (gnus-picon-transform-newsgroups "newsgroups")
318 (gnus-picon-transform-newsgroups "followup-to")))))
320 (provide 'gnus-picon
)
322 ;; arch-tag: fe9aede0-1b1b-463a-b4ab-807f98bcb31f
323 ;;; gnus-picon.el ends here