Commit | Line | Data |
---|---|---|
23f87bed MB |
1 | ;;; gnus-picon.el --- displaying pretty icons in Gnus |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 1996-2011 Free Software Foundation, Inc. |
23f87bed MB |
4 | |
5 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> | |
6 | ;; Keywords: news xpm annotation glyph faces | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
5e809f55 | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
23f87bed | 11 | ;; it under the terms of the GNU General Public License as published by |
5e809f55 GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
23f87bed MB |
14 | |
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
5e809f55 | 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23f87bed MB |
18 | ;; GNU General Public License for more details. |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
5e809f55 | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
23f87bed MB |
22 | |
23 | ;;; Commentary: | |
24 | ||
25 | ;; There are three picon types relevant to Gnus: | |
26 | ;; | |
27 | ;; Persons: person@subdomain.dom | |
28 | ;; users/dom/subdomain/person/face.gif | |
29 | ;; usenix/dom/subdomain/person/face.gif | |
30 | ;; misc/MISC/person/face.gif | |
31 | ;; Domains: subdomain.dom | |
32 | ;; domain/dom/subdomain/unknown/face.gif | |
33 | ;; Groups: comp.lang.lisp | |
34 | ;; news/comp/lang/lisp/unknown/face.gif | |
35 | ;; | |
36 | ;; Original implementation by Wes Hardaker <hardaker@ece.ucdavis.edu>. | |
37 | ;; | |
38 | ;;; Code: | |
39 | ||
f0b7f5a8 | 40 | ;; For Emacs <22.2 and XEmacs. |
b882f02c GM |
41 | (eval-and-compile |
42 | (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))) | |
43 | ||
c1d7d285 MB |
44 | (eval-when-compile (require 'cl)) |
45 | ||
23f87bed | 46 | (require 'gnus) |
23f87bed MB |
47 | (require 'gnus-art) |
48 | ||
49 | ;;; User variables: | |
50 | ||
51 | (defcustom gnus-picon-news-directories '("news") | |
52 | "*List of directories to search for newsgroups faces." | |
53 | :type '(repeat string) | |
54 | :group 'gnus-picon) | |
55 | ||
56 | (defcustom gnus-picon-user-directories '("users" "usenix" "local" "misc") | |
57 | "*List of directories to search for user faces." | |
58 | :type '(repeat string) | |
59 | :group 'gnus-picon) | |
60 | ||
61 | (defcustom gnus-picon-domain-directories '("domains") | |
62 | "*List of directories to search for domain faces. | |
63 | Some people may want to add \"unknown\" to this list." | |
64 | :type '(repeat string) | |
65 | :group 'gnus-picon) | |
66 | ||
67 | (defcustom gnus-picon-file-types | |
68 | (let ((types (list "xbm"))) | |
69 | (when (gnus-image-type-available-p 'gif) | |
70 | (push "gif" types)) | |
71 | (when (gnus-image-type-available-p 'xpm) | |
72 | (push "xpm" types)) | |
73 | types) | |
74 | "*List of suffixes on picon file names to try." | |
75 | :type '(repeat string) | |
76 | :group 'gnus-picon) | |
77 | ||
01c52d31 MB |
78 | (defcustom gnus-picon-style 'inline |
79 | "How should picons be displayed. | |
80 | If `inline', the textual representation is replaced. If `right', picons are | |
81 | added right to the textual representation." | |
82 | ;; FIXME: `right' needs improvement for XEmacs. | |
83 | :type '(choice (const inline) | |
84 | (const right)) | |
85 | :group 'gnus-picon) | |
86 | ||
4478e074 G |
87 | (defcustom gnus-picon-inhibit-top-level-domains t |
88 | "If non-nil, don't piconify top-level domains. | |
89 | These are often not very interesting." | |
90 | :type 'boolean | |
91 | :group 'gnus-picon) | |
92 | ||
23f87bed MB |
93 | ;;; Internal variables: |
94 | ||
23f87bed MB |
95 | (defvar gnus-picon-glyph-alist nil |
96 | "Picon glyphs cache. | |
97 | List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.") | |
98 | (defvar gnus-picon-cache nil) | |
99 | ||
100 | ;;; Functions: | |
101 | ||
102 | (defsubst gnus-picon-split-address (address) | |
103 | (setq address (split-string address "@")) | |
104 | (if (stringp (cadr address)) | |
105 | (cons (car address) (split-string (cadr address) "\\.")) | |
106 | (if (stringp (car address)) | |
107 | (split-string (car address) "\\.")))) | |
108 | ||
109 | (defun gnus-picon-find-face (address directories &optional exact) | |
110 | (let* ((address (gnus-picon-split-address address)) | |
111 | (user (pop address)) | |
112 | (faddress address) | |
113 | database directory result instance base) | |
114 | (catch 'found | |
115 | (dolist (database gnus-picon-databases) | |
116 | (dolist (directory directories) | |
117 | (setq address faddress | |
118 | base (expand-file-name directory database)) | |
119 | (while address | |
120 | (when (setq result (gnus-picon-find-image | |
121 | (concat base "/" (mapconcat 'downcase | |
122 | (reverse address) | |
123 | "/") | |
124 | "/" (downcase user) "/"))) | |
125 | (throw 'found result)) | |
126 | (if exact | |
127 | (setq address nil) | |
128 | (pop address))) | |
129 | ;; Kludge to search MISC as well. But not in "news". | |
130 | (unless (string= directory "news") | |
131 | (when (setq result (gnus-picon-find-image | |
132 | (concat base "/MISC/" user "/"))) | |
133 | (throw 'found result)))))))) | |
134 | ||
135 | (defun gnus-picon-find-image (directory) | |
136 | (let ((types gnus-picon-file-types) | |
137 | found type file) | |
138 | (while (and (not found) | |
139 | (setq type (pop types))) | |
140 | (setq found (file-exists-p (setq file (concat directory "face." type))))) | |
141 | (if found | |
142 | file | |
143 | nil))) | |
144 | ||
01c52d31 | 145 | (defun gnus-picon-insert-glyph (glyph category &optional nostring) |
23f87bed | 146 | "Insert GLYPH into the buffer. |
01c52d31 MB |
147 | GLYPH can be either a glyph or a string. When NOSTRING, no textual |
148 | replacement is added." | |
149 | ;; Using NOSTRING prevents wrong BBDB entries with `gnus-picon-style' set to | |
150 | ;; 'right. | |
23f87bed MB |
151 | (if (stringp glyph) |
152 | (insert glyph) | |
153 | (gnus-add-wash-type category) | |
154 | (gnus-add-image category (car glyph)) | |
2696d88f | 155 | (gnus-put-image (car glyph) (unless nostring (cdr glyph)) category))) |
23f87bed MB |
156 | |
157 | (defun gnus-picon-create-glyph (file) | |
158 | (or (cdr (assoc file gnus-picon-glyph-alist)) | |
2696d88f G |
159 | (cdar (push (cons file (gnus-create-image |
160 | file nil nil | |
161 | :color-symbols '(("None" . "white")))) | |
23f87bed MB |
162 | gnus-picon-glyph-alist)))) |
163 | ||
164 | ;;; Functions that does picon transformations: | |
165 | ||
b882f02c GM |
166 | (declare-function image-size "image.c" (spec &optional pixels frame)) |
167 | ||
23f87bed MB |
168 | (defun gnus-picon-transform-address (header category) |
169 | (gnus-with-article-headers | |
01c52d31 MB |
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 | ||
4478e074 G |
196 | (dotimes (i (- (length spec) |
197 | (if gnus-picon-inhibit-top-level-domains | |
198 | 2 1))) | |
01c52d31 MB |
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)))))))))) | |
23f87bed MB |
242 | |
243 | (defun gnus-picon-transform-newsgroups (header) | |
244 | (interactive) | |
245 | (gnus-with-article-headers | |
01c52d31 MB |
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)))))))) | |
23f87bed MB |
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 | |
01c52d31 MB |
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))))) | |
23f87bed MB |
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 | |
01c52d31 MB |
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))))) | |
23f87bed MB |
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 | |
01c52d31 MB |
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"))))) | |
23f87bed MB |
313 | |
314 | (provide 'gnus-picon) | |
315 | ||
23f87bed | 316 | ;;; gnus-picon.el ends here |