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