Fix up comment convention on the arch-tag lines.
[bpt/emacs.git] / lisp / gnus / gnus-picon.el
1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news xpm annotation glyph faces
8
9 ;; This file is part of GNU Emacs.
10
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, or (at your option)
14 ;; any later version.
15
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.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; There are three picon types relevant to Gnus:
29 ;;
30 ;; Persons: person@subdomain.dom
31 ;; users/dom/subdomain/person/face.gif
32 ;; usenix/dom/subdomain/person/face.gif
33 ;; misc/MISC/person/face.gif
34 ;; Domains: subdomain.dom
35 ;; domain/dom/subdomain/unknown/face.gif
36 ;; Groups: comp.lang.lisp
37 ;; news/comp/lang/lisp/unknown/face.gif
38 ;;
39 ;; Original implementation by Wes Hardaker <hardaker@ece.ucdavis.edu>.
40 ;;
41 ;;; Code:
42
43 (eval-when-compile (require 'cl))
44
45 (require 'gnus)
46 (require 'gnus-art)
47
48 ;;; User variables:
49
50 (defcustom gnus-picon-news-directories '("news")
51 "*List of directories to search for newsgroups faces."
52 :type '(repeat string)
53 :group 'gnus-picon)
54
55 (defcustom gnus-picon-user-directories '("users" "usenix" "local" "misc")
56 "*List of directories to search for user faces."
57 :type '(repeat string)
58 :group 'gnus-picon)
59
60 (defcustom gnus-picon-domain-directories '("domains")
61 "*List of directories to search for domain faces.
62 Some people may want to add \"unknown\" to this list."
63 :type '(repeat string)
64 :group 'gnus-picon)
65
66 (defcustom gnus-picon-file-types
67 (let ((types (list "xbm")))
68 (when (gnus-image-type-available-p 'gif)
69 (push "gif" types))
70 (when (gnus-image-type-available-p 'xpm)
71 (push "xpm" types))
72 types)
73 "*List of suffixes on picon file names to try."
74 :type '(repeat string)
75 :group 'gnus-picon)
76
77 (defcustom gnus-picon-style 'inline
78 "How should picons be displayed.
79 If `inline', the textual representation is replaced. If `right', picons are
80 added right to the textual representation."
81 ;; FIXME: `right' needs improvement for XEmacs.
82 :type '(choice (const inline)
83 (const right))
84 :group 'gnus-picon)
85
86 (defface gnus-picon-xbm '((t (:foreground "black" :background "white")))
87 "Face to show xbm picon in."
88 :group 'gnus-picon)
89 ;; backward-compatibility alias
90 (put 'gnus-picon-xbm-face 'face-alias 'gnus-picon-xbm)
91
92 (defface gnus-picon '((t (:foreground "black" :background "white")))
93 "Face to show picon in."
94 :group 'gnus-picon)
95 ;; backward-compatibility alias
96 (put 'gnus-picon-face 'face-alias 'gnus-picon)
97
98 ;;; Internal variables:
99
100 (defvar gnus-picon-setup-p nil)
101 (defvar gnus-picon-glyph-alist nil
102 "Picon glyphs cache.
103 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
104 (defvar gnus-picon-cache nil)
105
106 ;;; Functions:
107
108 (defsubst gnus-picon-split-address (address)
109 (setq address (split-string address "@"))
110 (if (stringp (cadr address))
111 (cons (car address) (split-string (cadr address) "\\."))
112 (if (stringp (car address))
113 (split-string (car address) "\\."))))
114
115 (defun gnus-picon-find-face (address directories &optional exact)
116 (let* ((address (gnus-picon-split-address address))
117 (user (pop address))
118 (faddress address)
119 database directory result instance base)
120 (catch 'found
121 (dolist (database gnus-picon-databases)
122 (dolist (directory directories)
123 (setq address faddress
124 base (expand-file-name directory database))
125 (while address
126 (when (setq result (gnus-picon-find-image
127 (concat base "/" (mapconcat 'downcase
128 (reverse address)
129 "/")
130 "/" (downcase user) "/")))
131 (throw 'found result))
132 (if exact
133 (setq address nil)
134 (pop address)))
135 ;; Kludge to search MISC as well. But not in "news".
136 (unless (string= directory "news")
137 (when (setq result (gnus-picon-find-image
138 (concat base "/MISC/" user "/")))
139 (throw 'found result))))))))
140
141 (defun gnus-picon-find-image (directory)
142 (let ((types gnus-picon-file-types)
143 found type file)
144 (while (and (not found)
145 (setq type (pop types)))
146 (setq found (file-exists-p (setq file (concat directory "face." type)))))
147 (if found
148 file
149 nil)))
150
151 (defun gnus-picon-insert-glyph (glyph category &optional nostring)
152 "Insert GLYPH into the buffer.
153 GLYPH can be either a glyph or a string. When NOSTRING, no textual
154 replacement is added."
155 ;; Using NOSTRING prevents wrong BBDB entries with `gnus-picon-style' set to
156 ;; 'right.
157 (if (stringp glyph)
158 (insert glyph)
159 (gnus-add-wash-type category)
160 (gnus-add-image category (car glyph))
161 (gnus-put-image (car glyph) (unless nostring (cdr glyph)) category)))
162
163 (defun gnus-picon-create-glyph (file)
164 (or (cdr (assoc file gnus-picon-glyph-alist))
165 (cdar (push (cons file (gnus-create-image file))
166 gnus-picon-glyph-alist))))
167
168 ;;; Functions that does picon transformations:
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 (1- (length spec)))
199 (when (setq file (gnus-picon-find-face
200 (concat "unknown@"
201 (mapconcat
202 'identity (nthcdr (1+ i) spec) "."))
203 gnus-picon-domain-directories t))
204 (setcar (nthcdr (1+ i) spec)
205 (cons (gnus-picon-create-glyph file)
206 (nth (1+ i) spec)))))
207 (setq spec (nreverse spec))
208 (push (cons address spec) gnus-picon-cache))
209
210 (gnus-article-goto-header header)
211 (mail-header-narrow-to-field)
212 (case gnus-picon-style
213 (right
214 (when (= (length addresses) 1)
215 (setq len (apply '+ (mapcar (lambda (x)
216 (condition-case nil
217 (car (image-size (car x)))
218 (error 0))) spec)))
219 (when (> len 0)
220 (goto-char (point-at-eol))
221 (insert (propertize
222 " " 'display
223 (cons 'space
224 (list :align-to (- (window-width) 1 len))))))
225 (goto-char (point-at-eol))
226 (setq point (point-at-eol))
227 (dolist (image spec)
228 (unless (stringp image)
229 (goto-char point)
230 (gnus-picon-insert-glyph image category 'nostring)))))
231 (inline
232 (when (search-forward address nil t)
233 (delete-region (match-beginning 0) (match-end 0))
234 (setq point (point))
235 (while spec
236 (goto-char point)
237 (if (> (length spec) 2)
238 (insert ".")
239 (if (= (length spec) 2)
240 (insert "@")))
241 (gnus-picon-insert-glyph (pop spec) category))))))))))
242
243 (defun gnus-picon-transform-newsgroups (header)
244 (interactive)
245 (gnus-with-article-headers
246 (gnus-article-goto-header header)
247 (mail-header-narrow-to-field)
248 (let ((groups (message-tokenize-header (mail-fetch-field header)))
249 spec file point)
250 (dolist (group groups)
251 (unless (setq spec (cdr (assoc group gnus-picon-cache)))
252 (setq spec (nreverse (split-string group "[.]")))
253 (dotimes (i (length spec))
254 (when (setq file (gnus-picon-find-face
255 (concat "unknown@"
256 (mapconcat
257 'identity (nthcdr i spec) "."))
258 gnus-picon-news-directories t))
259 (setcar (nthcdr i spec)
260 (cons (gnus-picon-create-glyph file)
261 (nth i spec)))))
262 (push (cons group spec) gnus-picon-cache))
263 (when (search-forward group nil t)
264 (delete-region (match-beginning 0) (match-end 0))
265 (save-restriction
266 (narrow-to-region (point) (point))
267 (while spec
268 (goto-char (point-min))
269 (if (> (length spec) 1)
270 (insert "."))
271 (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon))
272 (goto-char (point-max))))))))
273
274 ;;; Commands:
275
276 ;; #### NOTE: the test for buffer-read-only is the same as in
277 ;; article-display-[x-]face. See the comment up there.
278
279 ;;;###autoload
280 (defun gnus-treat-from-picon ()
281 "Display picons in the From header.
282 If picons are already displayed, remove them."
283 (interactive)
284 (let ((wash-picon-p buffer-read-only))
285 (gnus-with-article-buffer
286 (if (and wash-picon-p (memq 'from-picon gnus-article-wash-types))
287 (gnus-delete-images 'from-picon)
288 (gnus-picon-transform-address "from" 'from-picon)))))
289
290 ;;;###autoload
291 (defun gnus-treat-mail-picon ()
292 "Display picons in the Cc and To headers.
293 If picons are already displayed, remove them."
294 (interactive)
295 (let ((wash-picon-p buffer-read-only))
296 (gnus-with-article-buffer
297 (if (and wash-picon-p (memq 'mail-picon gnus-article-wash-types))
298 (gnus-delete-images 'mail-picon)
299 (gnus-picon-transform-address "cc" 'mail-picon)
300 (gnus-picon-transform-address "to" 'mail-picon)))))
301
302 ;;;###autoload
303 (defun gnus-treat-newsgroups-picon ()
304 "Display picons in the Newsgroups and Followup-To headers.
305 If picons are already displayed, remove them."
306 (interactive)
307 (let ((wash-picon-p buffer-read-only))
308 (gnus-with-article-buffer
309 (if (and wash-picon-p (memq 'newsgroups-picon gnus-article-wash-types))
310 (gnus-delete-images 'newsgroups-picon)
311 (gnus-picon-transform-newsgroups "newsgroups")
312 (gnus-picon-transform-newsgroups "followup-to")))))
313
314 (provide 'gnus-picon)
315
316 ;; arch-tag: fe9aede0-1b1b-463a-b4ab-807f98bcb31f
317 ;;; gnus-picon.el ends here