*** empty log message ***
[gnus] / lisp / gnus-picon.el
1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4 ;; Author: Wes Hardaker <hardaker@ece.ucdavis.edu>
5 ;; Keywords: news xpm annotation glyph faces
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; Usage:
27 ;;     - You must have XEmacs (19.12 or above I think) to use this.
28 ;;     - Read the variable descriptions below.
29 ;;
30 ;;     - chose a setup:
31 ;;
32 ;;       1) display the icons in its own buffer:
33 ;;
34 ;;          (add-hook 'gnus-article-display-hook 'gnus-article-display-picons t)
35 ;;          (add-hook 'gnus-summary-prepare-hook 'gnus-group-display-picons t)
36 ;;          (setq gnus-picons-display-where 'picons)
37 ;;
38 ;;          Then add the picons buffer to your display configuration:
39 ;;          The picons buffer needs to be at least 48 pixels high,
40 ;;          which for me is 5 lines:
41 ;;
42 ;;          (gnus-add-configuration
43 ;;           '(article (vertical 1.0 
44 ;;                             (group 6)
45 ;;                             (picons 5)
46 ;;                             (summary .25 point)
47 ;;                             (article 1.0))))
48 ;;
49 ;;          (gnus-add-configuration
50 ;;           '(summary (vertical 1.0 (group 6)
51 ;;                      (picons 5)
52 ;;                      (summary 1.0 point))))
53 ;;
54 ;;       2) display the icons in the summary buffer
55 ;;
56 ;;          (add-hook 'gnus-article-display-hook 'gnus-article-display-picons t)
57 ;;          (add-hook 'gnus-summary-prepare-hook 'gnus-group-display-picons t)
58 ;;          (setq gnus-picons-display-where 'summary)
59 ;;
60 ;;       3) display the icons in the article buffer
61 ;;
62 ;;          (add-hook 'gnus-article-display-hook 'gnus-article-display-picons t)
63 ;;          (add-hook 'gnus-article-prepare-hook 'gnus-group-display-picons t)
64 ;;          (setq gnus-picons-display-where 'article)
65 ;;
66 ;;
67 ;; Warnings:
68 ;;     - I'm not even close to being a lisp expert.
69 ;;     - The 't' (append) flag MUST be in the add-hook line
70 ;;
71 ;; TODO:
72 ;;     - Remove the TODO section in the headers.
73 ;;
74
75 ;;; Code:
76
77 (require 'xpm)
78 (require 'annotations)
79 (eval-when-compile (require 'cl))
80
81 (defvar gnus-picons-buffer "*Icon Buffer*"
82   "Buffer name to display the icons in if gnus-picons-display-where is 'picons.")
83
84 (defvar gnus-picons-display-where 'picons
85   "Where to display the group and article icons.")
86
87 (defvar gnus-picons-database "/usr/local/faces"
88   "Defines the location of the faces database.  
89 For information on obtaining this database of pretty pictures, please
90 see http://www.cs.indiana.edu/picons/ftp/index.html" )
91
92 (defvar gnus-picons-news-directory "news"
93   "Sub-directory of the faces database containing the icons for newsgroups."
94 )
95
96 (defvar gnus-picons-user-directories '("local" "users" "usenix" "misc/MISC")
97   "List of directories to search for user faces."
98 )
99
100 (defvar gnus-picons-domain-directories '("domains")
101   "List of directories to search for domain faces.  
102 Some people may want to add \"unknown\" to this list."
103 )
104
105 (defvar gnus-picons-x-face-file-name 
106   (format "/tmp/picon-xface.%s.xbm" (user-login-name))
107   "The name of the file in which to store the converted X-face header.")
108
109 (defvar gnus-picons-convert-x-face (format "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pbmtoxbm > %s" gnus-picons-x-face-file-name)
110   "Command to convert the x-face header into a xbm file."
111 )
112
113 (defvar gnus-picons-get-maximum-picons t
114   "*If non-nil, display all picons that apply to the article.
115 If this is nil, use just the \"best\" (or most relevant) picon.")
116        
117 (defvar gnus-group-annotations nil)
118 (defvar gnus-article-annotations nil)
119 (defvar gnus-x-face-annotations nil)
120
121 (defun gnus-picons-remove (plist)
122   (let ((listitem (car plist)))
123     (while (setq listitem (car plist))
124       (if (annotationp listitem)
125           (delete-annotation listitem))
126       (setq plist (cdr plist))))
127   )
128
129 (defun gnus-picons-remove-all ()
130   "Removes all picons from the Gnus display(s)."
131   (interactive)
132   (gnus-picons-remove gnus-article-annotations)
133   (gnus-picons-remove gnus-group-annotations)
134   (gnus-picons-remove gnus-x-face-annotations)
135   (setq gnus-article-annotations nil
136         gnus-group-annotations nil
137         gnus-x-face-annotations nil)
138   (if (bufferp gnus-picons-buffer)
139       (kill-buffer gnus-picons-buffer))
140   )
141
142 (defun gnus-get-buffer-name (variable)
143   "Returns the buffer name associated with the contents of a variable."
144   (cond ((symbolp variable)
145          (let ((newvar (cdr (assq variable gnus-window-to-buffer))))
146            (cond ((symbolp newvar)
147                   (symbol-value newvar))
148                  ((stringp newvar) newvar))))
149         ((stringp variable)
150          variable)))
151
152 (defun gnus-picons-article-display-x-face ()
153   "Display the x-face header bitmap in the 'gnus-picons-display-where buffer."
154   ;; delete any old ones.
155   (gnus-picons-remove gnus-x-face-annotations)
156   (setq gnus-x-face-annotations nil)
157   ;; display the new one.
158   (let ((gnus-article-x-face-command 'gnus-picons-display-x-face))
159     (gnus-article-display-x-face)))
160
161 (defun gnus-picons-display-x-face (beg end)
162   "Function to display the x-face header in the picons window.
163 To use:  (setq gnus-article-x-face-command 'gnus-picons-display-x-face)"
164   (interactive)
165   ;; convert the x-face header to a .xbm file
166   (let ((process-connection-type nil)
167         (process nil))
168     (process-kill-without-query
169      (setq process (start-process
170                     "gnus-x-face" nil shell-file-name shell-command-switch
171                     gnus-picons-convert-x-face)))
172     (process-send-region "gnus-x-face" beg end)
173     (process-send-eof "gnus-x-face")
174     ;; wait for it.
175     (while (not (equal (process-status process) 'exit))
176       (sleep-for .1)))
177   ;; display it
178   (save-excursion
179     (set-buffer (get-buffer-create (gnus-get-buffer-name 
180                                     gnus-picons-display-where)))
181     (gnus-add-current-to-buffer-list)
182     (goto-char (point-min))
183     (let ((iconpoint (point))
184           buffer-read-only)
185       (unless (looking-at "$")
186         (insert "\n")
187         (forward-line -1))
188       ;; append the annotation to gnus-article-annotations for deletion.
189       (setq gnus-x-face-annotations 
190             (append
191              (gnus-picons-try-to-find-face
192               gnus-picons-x-face-file-name iconpoint)
193              gnus-x-face-annotations)))
194     ;; delete the tmp file
195     (delete-file gnus-picons-x-face-file-name)))
196
197 (defun gnus-article-display-picons ()
198   "Display faces for an author and his/her domain in gnus-picons-display-where."
199   (interactive)
200   (if (and (featurep 'xpm) 
201            (or (not (fboundp 'device-type)) (equal (device-type) 'x))
202            (mail-fetch-field "from"))
203       (save-excursion
204         (let* ((from (mail-fetch-field "from"))
205                (username 
206                 (progn
207                   (string-match "\\([-_a-zA-Z0-9]+\\)@" from)
208                   (match-string 1 from)))
209                (hostpath
210                 (concat
211                  (gnus-picons-reverse-domain-path
212                   (replace-in-string
213                    (replace-in-string 
214                     (cadr (mail-extract-address-components from))
215                     ".*@\\(.*\\)\\'" "\\1")
216                    "\\." "/")) "/")))
217           (set-buffer (gnus-get-buffer-name gnus-picons-display-where))
218           (gnus-add-current-to-buffer-list)
219           (goto-char (point-min))
220           (if (eq gnus-picons-display-where 'article)
221               (and (search-forward "\n\n" nil t)
222                    (forward-line -1))
223             (unless (eolp)
224               (open-line 1)))
225
226           (gnus-picons-remove gnus-article-annotations)
227           (setq gnus-article-annotations nil)
228           (if (equal username from)
229               (setq username (progn
230                                (string-match "<\\([_a-zA-Z0-9-.]+\\)>" from)
231                                (match-string 1 from))))
232           (mapcar (lambda (pathpart) 
233                     (setq gnus-article-annotations
234                           (append
235                            (gnus-picons-insert-face-if-exists 
236                             (concat 
237                              (file-name-as-directory 
238                               gnus-picons-database) pathpart)
239                             (concat hostpath username) 
240                             (point))
241                            gnus-article-annotations))) 
242                   gnus-picons-user-directories)
243           (mapcar (lambda (pathpart) 
244                     (setq gnus-article-annotations 
245                           (append
246                            (gnus-picons-insert-face-if-exists 
247                             (concat (file-name-as-directory 
248                                      gnus-picons-database) pathpart)
249                             (concat hostpath "unknown") 
250                             (point))
251                            gnus-article-annotations))) 
252                   gnus-picons-domain-directories)
253           (add-hook 'gnus-summary-exit-hook 'gnus-picons-remove-all)
254           ))))
255
256 (defun gnus-group-display-picons ()
257   "Display icons for the group in the gnus-picons-display-where buffer." 
258   (interactive)
259   (when (and (featurep 'xpm) 
260              (or (not (fboundp 'device-type)) (equal (device-type) 'x)))
261     (save-excursion
262       (set-buffer (get-buffer-create
263                    (gnus-get-buffer-name gnus-picons-display-where)))
264       (gnus-add-current-to-buffer-list)
265       (goto-char (point-min))
266       (if (eq gnus-picons-display-where 'article)
267           (and (search-forward "\n\n" nil t)
268                (forward-line -1)
269                )
270         (unless (eolp)
271           (open-line 1)))
272       (cond 
273        ((listp gnus-group-annotations)
274         (mapcar 'delete-annotation gnus-group-annotations)
275         (setq gnus-group-annotations nil))
276        ((annotationp gnus-group-annotations)
277         (delete-annotation gnus-group-annotations)
278         (setq gnus-group-annotations nil)))
279       (gnus-picons-remove gnus-group-annotations)
280       (setq gnus-group-annotations nil)
281       (setq gnus-group-annotations
282             (gnus-picons-insert-face-if-exists 
283              (concat (file-name-as-directory gnus-picons-database)  
284                      gnus-picons-news-directory)
285              (concat (replace-in-string gnus-newsgroup-name "\\." "/") 
286                      "/unknown")
287              (point) t))
288       (add-hook 'gnus-summary-exit-hook 'gnus-picons-remove-all))))
289
290 (defun gnus-picons-insert-face-if-exists (path filename ipoint &optional rev)
291   "Inserts a face at point if I can find one"
292   (let ((file (concat path "/" filename))
293         (xpm (if (featurep 'xpm) "xpm" "xbm"))
294         picons found)
295     (while (and (or (not found)
296                     gnus-picons-get-maximum-picons)
297                 (>= (length file) (length path)))
298       (or (file-exists-p (setq found (concat file "/face." xpm)))
299           (file-exists-p (setq found (concat file "/unknown/face." xpm)))
300           (setq found nil))
301       (when found
302         (setq picons (nconc (gnus-picons-try-to-find-face found ipoint)
303                             picons)))
304       (setq file (directory-file-name (file-name-directory file))))
305     (nreverse picons)))
306       
307 (defun gnus-picons-try-to-find-face (path ipoint)
308   "If PATH exists, display it as a bitmap.  Returns t if succedded."
309   (when (file-exists-p path)
310     (let ((gl (make-glyph path)))
311       (set-glyph-face gl 'default)
312       (list (make-annotation gl ipoint 'text)))))
313
314 (defun gnus-picons-reverse-domain-path (str)
315   "a/b/c/d -> d/c/b/a"
316   (mapconcat 'identity (nreverse (message-tokenize-header str "/")) "/"))
317
318 (provide 'gnus-picon)
319
320 ;;; gnus-picon.el ends here