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