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