Commit | Line | Data |
---|---|---|
23f87bed MB |
1 | ;;; gnus-picon.el --- displaying pretty icons in Gnus |
2 | ||
acaf905b | 3 | ;; Copyright (C) 1996-2012 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." | |
2bed3f04 | 90 | :version "24.1" |
4478e074 G |
91 | :type 'boolean |
92 | :group 'gnus-picon) | |
93 | ||
23f87bed MB |
94 | ;;; Internal variables: |
95 | ||
23f87bed MB |
96 | (defvar gnus-picon-glyph-alist nil |
97 | "Picon glyphs cache. | |
98 | List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.") | |
99 | (defvar gnus-picon-cache nil) | |
100 | ||
101 | ;;; Functions: | |
102 | ||
103 | (defsubst gnus-picon-split-address (address) | |
104 | (setq address (split-string address "@")) | |
105 | (if (stringp (cadr address)) | |
106 | (cons (car address) (split-string (cadr address) "\\.")) | |
107 | (if (stringp (car address)) | |
108 | (split-string (car address) "\\.")))) | |
109 | ||
110 | (defun gnus-picon-find-face (address directories &optional exact) | |
111 | (let* ((address (gnus-picon-split-address address)) | |
112 | (user (pop address)) | |
113 | (faddress address) | |
114 | database directory result instance base) | |
115 | (catch 'found | |
116 | (dolist (database gnus-picon-databases) | |
117 | (dolist (directory directories) | |
118 | (setq address faddress | |
119 | base (expand-file-name directory database)) | |
120 | (while address | |
121 | (when (setq result (gnus-picon-find-image | |
122 | (concat base "/" (mapconcat 'downcase | |
123 | (reverse address) | |
124 | "/") | |
125 | "/" (downcase user) "/"))) | |
126 | (throw 'found result)) | |
127 | (if exact | |
128 | (setq address nil) | |
129 | (pop address))) | |
130 | ;; Kludge to search MISC as well. But not in "news". | |
131 | (unless (string= directory "news") | |
132 | (when (setq result (gnus-picon-find-image | |
133 | (concat base "/MISC/" user "/"))) | |
134 | (throw 'found result)))))))) | |
135 | ||
136 | (defun gnus-picon-find-image (directory) | |
137 | (let ((types gnus-picon-file-types) | |
138 | found type file) | |
139 | (while (and (not found) | |
140 | (setq type (pop types))) | |
141 | (setq found (file-exists-p (setq file (concat directory "face." type))))) | |
142 | (if found | |
143 | file | |
144 | nil))) | |
145 | ||
01c52d31 | 146 | (defun gnus-picon-insert-glyph (glyph category &optional nostring) |
23f87bed | 147 | "Insert GLYPH into the buffer. |
01c52d31 MB |
148 | GLYPH can be either a glyph or a string. When NOSTRING, no textual |
149 | replacement is added." | |
150 | ;; Using NOSTRING prevents wrong BBDB entries with `gnus-picon-style' set to | |
151 | ;; 'right. | |
23f87bed MB |
152 | (if (stringp glyph) |
153 | (insert glyph) | |
154 | (gnus-add-wash-type category) | |
155 | (gnus-add-image category (car glyph)) | |
2696d88f | 156 | (gnus-put-image (car glyph) (unless nostring (cdr glyph)) category))) |
23f87bed MB |
157 | |
158 | (defun gnus-picon-create-glyph (file) | |
159 | (or (cdr (assoc file gnus-picon-glyph-alist)) | |
2696d88f G |
160 | (cdar (push (cons file (gnus-create-image |
161 | file nil nil | |
162 | :color-symbols '(("None" . "white")))) | |
23f87bed MB |
163 | gnus-picon-glyph-alist)))) |
164 | ||
165 | ;;; Functions that does picon transformations: | |
166 | ||
b882f02c GM |
167 | (declare-function image-size "image.c" (spec &optional pixels frame)) |
168 | ||
23f87bed MB |
169 | (defun gnus-picon-transform-address (header category) |
170 | (gnus-with-article-headers | |
01c52d31 MB |
171 | (let ((addresses |
172 | (mail-header-parse-addresses | |
173 | ;; mail-header-parse-addresses does not work (reliably) on | |
174 | ;; decoded headers. | |
175 | (or | |
176 | (ignore-errors | |
177 | (mail-encode-encoded-word-string | |
178 | (or (mail-fetch-field header) ""))) | |
179 | (mail-fetch-field header)))) | |
180 | spec file point cache len) | |
181 | (dolist (address addresses) | |
182 | (setq address (car address)) | |
183 | (when (and (stringp address) | |
184 | (setq spec (gnus-picon-split-address address))) | |
185 | (if (setq cache (cdr (assoc address gnus-picon-cache))) | |
186 | (setq spec cache) | |
187 | (when (setq file (or (gnus-picon-find-face | |
188 | address gnus-picon-user-directories) | |
189 | (gnus-picon-find-face | |
190 | (concat "unknown@" | |
191 | (mapconcat | |
192 | 'identity (cdr spec) ".")) | |
193 | gnus-picon-user-directories))) | |
194 | (setcar spec (cons (gnus-picon-create-glyph file) | |
195 | (car spec)))) | |
196 | ||
4478e074 G |
197 | (dotimes (i (- (length spec) |
198 | (if gnus-picon-inhibit-top-level-domains | |
199 | 2 1))) | |
01c52d31 MB |
200 | (when (setq file (gnus-picon-find-face |
201 | (concat "unknown@" | |
202 | (mapconcat | |
203 | 'identity (nthcdr (1+ i) spec) ".")) | |
204 | gnus-picon-domain-directories t)) | |
205 | (setcar (nthcdr (1+ i) spec) | |
206 | (cons (gnus-picon-create-glyph file) | |
207 | (nth (1+ i) spec))))) | |
208 | (setq spec (nreverse spec)) | |
209 | (push (cons address spec) gnus-picon-cache)) | |
210 | ||
211 | (gnus-article-goto-header header) | |
212 | (mail-header-narrow-to-field) | |
213 | (case gnus-picon-style | |
214 | (right | |
215 | (when (= (length addresses) 1) | |
216 | (setq len (apply '+ (mapcar (lambda (x) | |
217 | (condition-case nil | |
218 | (car (image-size (car x))) | |
219 | (error 0))) spec))) | |
220 | (when (> len 0) | |
221 | (goto-char (point-at-eol)) | |
222 | (insert (propertize | |
223 | " " 'display | |
224 | (cons 'space | |
225 | (list :align-to (- (window-width) 1 len)))))) | |
226 | (goto-char (point-at-eol)) | |
227 | (setq point (point-at-eol)) | |
228 | (dolist (image spec) | |
229 | (unless (stringp image) | |
230 | (goto-char point) | |
231 | (gnus-picon-insert-glyph image category 'nostring))))) | |
232 | (inline | |
233 | (when (search-forward address nil t) | |
234 | (delete-region (match-beginning 0) (match-end 0)) | |
235 | (setq point (point)) | |
236 | (while spec | |
237 | (goto-char point) | |
238 | (if (> (length spec) 2) | |
239 | (insert ".") | |
240 | (if (= (length spec) 2) | |
241 | (insert "@"))) | |
242 | (gnus-picon-insert-glyph (pop spec) category)))))))))) | |
23f87bed MB |
243 | |
244 | (defun gnus-picon-transform-newsgroups (header) | |
245 | (interactive) | |
246 | (gnus-with-article-headers | |
01c52d31 MB |
247 | (gnus-article-goto-header header) |
248 | (mail-header-narrow-to-field) | |
249 | (let ((groups (message-tokenize-header (mail-fetch-field header))) | |
250 | spec file point) | |
251 | (dolist (group groups) | |
252 | (unless (setq spec (cdr (assoc group gnus-picon-cache))) | |
253 | (setq spec (nreverse (split-string group "[.]"))) | |
254 | (dotimes (i (length spec)) | |
255 | (when (setq file (gnus-picon-find-face | |
256 | (concat "unknown@" | |
257 | (mapconcat | |
258 | 'identity (nthcdr i spec) ".")) | |
259 | gnus-picon-news-directories t)) | |
260 | (setcar (nthcdr i spec) | |
261 | (cons (gnus-picon-create-glyph file) | |
262 | (nth i spec))))) | |
263 | (push (cons group spec) gnus-picon-cache)) | |
264 | (when (search-forward group nil t) | |
265 | (delete-region (match-beginning 0) (match-end 0)) | |
266 | (save-restriction | |
267 | (narrow-to-region (point) (point)) | |
268 | (while spec | |
269 | (goto-char (point-min)) | |
270 | (if (> (length spec) 1) | |
271 | (insert ".")) | |
272 | (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon)) | |
273 | (goto-char (point-max)))))))) | |
23f87bed MB |
274 | |
275 | ;;; Commands: | |
276 | ||
277 | ;; #### NOTE: the test for buffer-read-only is the same as in | |
278 | ;; article-display-[x-]face. See the comment up there. | |
279 | ||
280 | ;;;###autoload | |
281 | (defun gnus-treat-from-picon () | |
282 | "Display picons in the From header. | |
283 | If picons are already displayed, remove them." | |
284 | (interactive) | |
285 | (let ((wash-picon-p buffer-read-only)) | |
286 | (gnus-with-article-buffer | |
01c52d31 MB |
287 | (if (and wash-picon-p (memq 'from-picon gnus-article-wash-types)) |
288 | (gnus-delete-images 'from-picon) | |
289 | (gnus-picon-transform-address "from" 'from-picon))))) | |
23f87bed MB |
290 | |
291 | ;;;###autoload | |
292 | (defun gnus-treat-mail-picon () | |
293 | "Display picons in the Cc and To headers. | |
294 | If picons are already displayed, remove them." | |
295 | (interactive) | |
296 | (let ((wash-picon-p buffer-read-only)) | |
297 | (gnus-with-article-buffer | |
01c52d31 MB |
298 | (if (and wash-picon-p (memq 'mail-picon gnus-article-wash-types)) |
299 | (gnus-delete-images 'mail-picon) | |
300 | (gnus-picon-transform-address "cc" 'mail-picon) | |
301 | (gnus-picon-transform-address "to" 'mail-picon))))) | |
23f87bed MB |
302 | |
303 | ;;;###autoload | |
304 | (defun gnus-treat-newsgroups-picon () | |
305 | "Display picons in the Newsgroups and Followup-To headers. | |
306 | If picons are already displayed, remove them." | |
307 | (interactive) | |
308 | (let ((wash-picon-p buffer-read-only)) | |
309 | (gnus-with-article-buffer | |
01c52d31 MB |
310 | (if (and wash-picon-p (memq 'newsgroups-picon gnus-article-wash-types)) |
311 | (gnus-delete-images 'newsgroups-picon) | |
312 | (gnus-picon-transform-newsgroups "newsgroups") | |
313 | (gnus-picon-transform-newsgroups "followup-to"))))) | |
23f87bed MB |
314 | |
315 | (provide 'gnus-picon) | |
316 | ||
23f87bed | 317 | ;;; gnus-picon.el ends here |