Import Oort Gnus v0.11.
[elisp/gnus.git-] / lisp / gnus-fun.el
1 ;;; gnus-fun.el --- various frivoluos extension functions to Gnus
2 ;; Copyright (C) 2002 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
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 ;;; Code:
27
28 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
29   "*Directory where X-Face PBM files are stored."
30   :group 'gnus-fun
31   :type 'directory)
32
33 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
34   "Command for converting a PBM to an X-Face."
35   :group 'gnus-fun
36   :type 'string)
37
38 (defcustom gnus-convert-image-to-x-face-command "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface"
39   "Command for converting a GIF to an X-Face."
40   :group 'gnus-fun
41   :type 'string)
42
43 (defcustom gnus-convert-image-to-face-command "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng"
44   "Command for converting a GIF to an X-Face."
45   :group 'gnus-fun
46   :type 'string)
47
48 (defun gnus-shell-command-to-string (command)
49   "Like `shell-command-to-string' except not mingling ERROR."
50   (with-output-to-string
51     (call-process shell-file-name nil (list standard-output nil)
52                   nil shell-command-switch command)))
53
54 (defun gnus-shell-command-on-region (start end command)
55   "A simplified `shell-command-on-region'.
56 Output to the current buffer, replace text, and don't mingle error."
57   (call-process-region start end shell-file-name t
58                        (list (current-buffer) nil)
59                        nil shell-command-switch command))
60
61 ;;;###autoload
62 (defun gnus-random-x-face ()
63   "Insert a random X-Face header from `gnus-x-face-directory'."
64   (interactive)
65   (when (file-exists-p gnus-x-face-directory)
66     (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
67            (file (nth (random (length files)) files)))
68       (when file
69         (gnus-shell-command-to-string
70          (format gnus-convert-pbm-to-x-face-command
71                  (shell-quote-argument file)))))))
72
73 ;;;###autoload
74 (defun gnus-x-face-from-file (file)
75   "Insert an X-Face header based on an image file."
76   (interactive "fImage file name:" )
77   (when (file-exists-p file)
78     (gnus-shell-command-to-string
79      (format gnus-convert-image-to-x-face-command
80              (shell-quote-argument file)))))
81
82 (defun gnus-face-from-file (file)
83   "Return an Face header based on an image file."
84   (interactive "fImage file name:" )
85   (when (file-exists-p file)
86     (let ((done nil)
87           (attempt "")
88           (step 72)
89           (quant 16))
90       (while (and (not done)
91                   (> quant 1))
92         (setq attempt
93               (gnus-shell-command-to-string
94                (format gnus-convert-image-to-face-command
95                        (shell-quote-argument file)
96                        quant)))
97         (if (> (length attempt) 740)
98             (progn
99               (setq quant (- quant 2))
100               (message "Length %d; trying quant %d"
101                        (length attempt) quant))
102           (setq done t)))
103       (if done
104           (mm-with-unibyte-buffer       
105             (insert attempt)
106             (base64-encode-region (point-min) (point-max))
107             (goto-char (point-min))
108             (while (search-forward "\n" nil t)
109               (replace-match ""))
110             (goto-char (point-min))
111             (while (> (- (point-max) (point))
112                       step)
113               (forward-char step)
114               (insert "\n ")
115               (setq step 76))
116             (buffer-string))
117         nil))))
118
119 ;;;###autoload
120 (defun gnus-convert-face-to-png (face)
121   (mm-with-unibyte-buffer
122     (insert face)
123     (base64-decode-region (point-min) (point-max))
124     (buffer-string)))
125
126 (defun gnus-convert-image-to-gray-x-face (file depth)
127   (let* ((mapfile (mm-make-temp-file (expand-file-name "gnus." 
128                                                        mm-tmp-directory)))
129          (levels (expt 2 depth))
130          (step (/ 255 (1- levels)))
131          color-alist bits bits-list mask pixel x-faces)
132     (with-temp-file mapfile
133       (insert "P3\n")
134       (insert (format "%d 1\n" levels))
135       (insert "255\n")
136       (dotimes (i levels)
137         (insert (format "%d %d %d\n"
138                         (* step i) (* step i) (* step i)))
139         (push (cons (* step i) i) color-alist)))
140     (when (file-exists-p file)
141       (with-temp-buffer
142         (insert (gnus-shell-command-to-string
143                  (format "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant -fs -map %s | ppmtopgm | pnmnoraw"
144                          (shell-quote-argument file)
145                          mapfile)))
146         (goto-char (point-min))
147         (forward-line 3)
148         (while (setq pixel (ignore-errors (read (current-buffer))))
149           (push (cdr (assq pixel color-alist)) bits-list))
150         (setq bits-list (nreverse bits-list))
151         (dotimes (bit-number depth)
152           (setq mask (expt 2 bit-number))
153           (with-temp-buffer
154             (insert "P1\n48 48\n")
155             (dolist (bits bits-list)
156               (insert (if (zerop (logand bits mask)) "0 " "1 ")))
157             (gnus-shell-command-on-region
158              (point-min) (point-max)
159              ;; the following is taken from xbmtoikon:
160              "pbmtoicon | sed '/^[      ]*[*\\\\/]/d; s/[       ]//g; s/,$//' | tr , '\\012' | sed 's/^0x//; s/^/0x/' | pr -l1 -t -w22 -3 -s, | sed 's/,*$/,/' | compface")
161             (push (buffer-string) x-faces))))
162       (dotimes (i (length x-faces))
163         (insert (if (zerop i) "X-Face:" (format "X-Face-%s:" i))
164                 (nth i x-faces))))
165     (delete-file mapfile)))
166
167 ;;;###autoload
168 (defun gnus-convert-gray-x-face-to-xpm (faces)
169   (let* ((depth (length faces))
170          (scale (/ 255 (1- (expt 2 depth))))
171          (ok-p t)
172          (coding-system-for-read 'binary)
173          (coding-system-for-write 'binary)
174          default-enable-multibyte-characters
175          start bit-array bit-arrays pixel)
176     (with-temp-buffer
177       (dolist (face faces)
178         (erase-buffer)
179         (insert (uncompface face))
180         (gnus-shell-command-on-region
181          (point-min) (point-max)
182          "pnmnoraw")
183         (goto-char (point-min))
184         (forward-line 2)
185         (setq start (point))
186         (insert "[")
187         (while (not (eobp))
188           (forward-char 1)
189           (insert " "))
190         (insert "]")
191         (goto-char start)
192         (setq bit-array (read (current-buffer)))
193         (unless (= (length bit-array) (* 48 48))
194           (setq ok-p nil))
195         (push bit-array bit-arrays))
196       (when ok-p
197         (erase-buffer)
198         (insert "P2\n48 48\n255\n")
199         (dotimes (i (* 48 48))
200           (setq pixel 0)
201           (dotimes (plane depth)
202             (setq pixel (+ (* pixel 2) (aref (nth plane bit-arrays) i))))
203           (insert (number-to-string (* scale pixel)) " "))
204         (gnus-shell-command-on-region
205          (point-min) (point-max)
206          "ppmtoxpm")
207         (buffer-string)))))
208
209 ;;;###autoload
210 (defun gnus-convert-gray-x-face-region (beg end)
211   "Convert the X-Faces in region to a PPM file."
212   (interactive "r")
213   (let ((input (buffer-substring beg end))
214         faces)
215     (with-temp-buffer
216       (insert input)
217       (goto-char (point-min))
218       (while (not (eobp))
219         (save-restriction
220           (mail-header-narrow-to-field)
221           (push (mail-header-field-value) faces)
222           (goto-char (point-max)))))
223     (gnus-convert-gray-x-face-to-xpm faces)))
224
225 (defface gnus-x-face '((t (:foreground "black" :background "white")))
226   "Face to show X-Face.
227 The colors from this face are used as the foreground and background
228 colors of the displayed X-Faces."
229   :group 'gnus-article-headers)
230
231 (defun gnus-display-x-face-in-from (data)
232   "Display the X-Face DATA in the From header."
233   (let ((default-enable-multibyte-characters nil)
234         pbm)
235     (when (or (gnus-image-type-available-p 'xface)
236               (and (gnus-image-type-available-p 'pbm)
237                    (setq pbm (uncompface data))))
238       (save-excursion
239         (save-restriction
240           (article-narrow-to-head)
241           (gnus-article-goto-header "from")
242           (when (bobp)
243             (insert "From: [no `from' set]\n")
244             (forward-char -17))
245           (gnus-add-image
246            'xface
247            (gnus-put-image
248             (if (gnus-image-type-available-p 'xface)
249                 (gnus-create-image
250                  (concat "X-Face: " data)
251                  'xface t :ascent 'center :face 'gnus-x-face)
252               (gnus-create-image
253                pbm 'pbm t :ascent 'center :face 'gnus-x-face))))
254           (gnus-add-wash-type 'xface))))))
255
256 (defun gnus-grab-cam-x-face ()
257   "Grab a picture off the camera and make it into an X-Face."
258   (interactive)
259   (shell-command "xawtv-remote snap ppm")
260   (let ((file nil))
261     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
262                                              t "snap.*ppm")))
263       (sleep-for 1))
264     (setq file (car file))
265     (with-temp-buffer
266       (shell-command
267        (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | ppmnorm 2>/dev/null | pnmscale -width 48 | ppmtopgm | pgmtopbm -threshold -value 0.92 | pbmtoxbm | compface"
268                file)
269        (current-buffer))
270       ;;(sleep-for 3)
271       (delete-file file)
272       (buffer-string))))
273
274 (defun gnus-grab-gray-x-face ()
275   "Grab a picture off the camera and make it into an X-Face."
276   (interactive)
277   (shell-command "xawtv-remote snap ppm")
278   (let ((file nil))
279     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
280                                              t "snap.*ppm")))
281       (sleep-for 1))
282     (setq file (car file))
283     (with-temp-buffer
284       (shell-command
285        (format "pnmcut -left 70 -top 100 -width 144 -height 144 '%s' | ppmquant 256 2>/dev/null | ppmtogif > '%s.gif'"
286                file file)
287        (current-buffer))
288       (delete-file file))
289     (gnus-convert-image-to-gray-x-face (concat file ".gif") 3)
290     (delete-file (concat file ".gif"))))
291
292 (provide 'gnus-fun)
293
294 ;;; gnus-fun.el ends here