a3d41c6ce5c679fbb2cc8b1f8403a13149265bc1
[gnus] / 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 an image 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 an image 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   "Return X-Face header data chosen randomly 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-insert-random-x-face-header ()
75   "Insert a random X-Face header from `gnus-x-face-directory'."
76   (interactive)
77   (let ((data (gnus-random-x-face)))
78     (save-excursion
79       (message-goto-eoh)
80       (if data
81           (insert "X-Face: " data)
82         (message
83          "No face returned by `gnus-random-x-face'.  Does %s/*.pbm exist?"
84          gnus-x-face-directory)))))
85
86 ;;;###autoload
87 (defun gnus-x-face-from-file (file)
88   "Insert an X-Face header based on an image file."
89   (interactive "fImage file name: ")
90   (when (file-exists-p file)
91     (gnus-shell-command-to-string
92      (format gnus-convert-image-to-x-face-command
93              (shell-quote-argument (expand-file-name file))))))
94
95 ;;;###autoload
96 (defun gnus-face-from-file (file)
97   "Return an Face header based on an image file."
98   (interactive "fImage file name: ")
99   (when (file-exists-p file)
100     (let ((done nil)
101           (attempt "")
102           (step 72)
103           (quant 16))
104       (while (and (not done)
105                   (> quant 1))
106         (setq attempt
107               (gnus-shell-command-to-string
108                (format gnus-convert-image-to-face-command
109                        (shell-quote-argument (expand-file-name file))
110                        quant)))
111         (if (> (length attempt) 740)
112             (progn
113               (setq quant (- quant 2))
114               (message "Length %d; trying quant %d"
115                        (length attempt) quant))
116           (setq done t)))
117       (if done
118           (mm-with-unibyte-buffer       
119             (insert attempt)
120             (base64-encode-region (point-min) (point-max))
121             (goto-char (point-min))
122             (while (search-forward "\n" nil t)
123               (replace-match ""))
124             (goto-char (point-min))
125             (while (> (- (point-max) (point))
126                       step)
127               (forward-char step)
128               (insert "\n ")
129               (setq step 76))
130             (buffer-string))
131         nil))))
132
133 ;;;###autoload
134 (defun gnus-convert-face-to-png (face)
135   (mm-with-unibyte-buffer
136     (insert face)
137     (ignore-errors
138       (base64-decode-region (point-min) (point-max)))
139     (buffer-string)))
140
141 (defun gnus-convert-image-to-gray-x-face (file depth)
142   (let* ((mapfile (mm-make-temp-file (expand-file-name "gnus." 
143                                                        mm-tmp-directory)))
144          (levels (expt 2 depth))
145          (step (/ 255 (1- levels)))
146          color-alist bits bits-list mask pixel x-faces)
147     (with-temp-file mapfile
148       (insert "P3\n")
149       (insert (format "%d 1\n" levels))
150       (insert "255\n")
151       (dotimes (i levels)
152         (insert (format "%d %d %d\n"
153                         (* step i) (* step i) (* step i)))
154         (push (cons (* step i) i) color-alist)))
155     (when (file-exists-p file)
156       (with-temp-buffer
157         (insert (gnus-shell-command-to-string
158                  (format "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant -fs -map %s | ppmtopgm | pnmnoraw"
159                          (shell-quote-argument file)
160                          mapfile)))
161         (goto-char (point-min))
162         (forward-line 3)
163         (while (setq pixel (ignore-errors (read (current-buffer))))
164           (push (cdr (assq pixel color-alist)) bits-list))
165         (setq bits-list (nreverse bits-list))
166         (dotimes (bit-number depth)
167           (setq mask (expt 2 bit-number))
168           (with-temp-buffer
169             (insert "P1\n48 48\n")
170             (dolist (bits bits-list)
171               (insert (if (zerop (logand bits mask)) "0 " "1 ")))
172             (gnus-shell-command-on-region
173              (point-min) (point-max)
174              ;; the following is taken from xbmtoikon:
175              "pbmtoicon | sed '/^[      ]*[*\\\\/]/d; s/[       ]//g; s/,$//' | tr , '\\012' | sed 's/^0x//; s/^/0x/' | pr -l1 -t -w22 -3 -s, | sed 's/,*$/,/' | compface")
176             (push (buffer-string) x-faces))))
177       (dotimes (i (length x-faces))
178         (insert (if (zerop i) "X-Face:" (format "X-Face-%s:" i))
179                 (nth i x-faces))))
180     (delete-file mapfile)))
181
182 ;;;###autoload
183 (defun gnus-convert-gray-x-face-to-xpm (faces)
184   (let* ((depth (length faces))
185          (scale (/ 255 (1- (expt 2 depth))))
186          (ok-p t)
187          (coding-system-for-read 'binary)
188          (coding-system-for-write 'binary)
189          default-enable-multibyte-characters
190          start bit-array bit-arrays pixel)
191     (with-temp-buffer
192       (dolist (face faces)
193         (erase-buffer)
194         (insert (uncompface face))
195         (gnus-shell-command-on-region
196          (point-min) (point-max)
197          "pnmnoraw")
198         (goto-char (point-min))
199         (forward-line 2)
200         (setq start (point))
201         (insert "[")
202         (while (not (eobp))
203           (forward-char 1)
204           (insert " "))
205         (insert "]")
206         (goto-char start)
207         (setq bit-array (read (current-buffer)))
208         (unless (= (length bit-array) (* 48 48))
209           (setq ok-p nil))
210         (push bit-array bit-arrays))
211       (when ok-p
212         (erase-buffer)
213         (insert "P2\n48 48\n255\n")
214         (dotimes (i (* 48 48))
215           (setq pixel 0)
216           (dotimes (plane depth)
217             (setq pixel (+ (* pixel 2) (aref (nth plane bit-arrays) i))))
218           (insert (number-to-string (* scale pixel)) " "))
219         (gnus-shell-command-on-region
220          (point-min) (point-max)
221          "ppmtoxpm")
222         (buffer-string)))))
223
224 ;;;###autoload
225 (defun gnus-convert-gray-x-face-region (beg end)
226   "Convert the X-Faces in region to a PPM file."
227   (interactive "r")
228   (let ((input (buffer-substring beg end))
229         faces)
230     (with-temp-buffer
231       (insert input)
232       (goto-char (point-min))
233       (while (not (eobp))
234         (save-restriction
235           (mail-header-narrow-to-field)
236           (push (mail-header-field-value) faces)
237           (goto-char (point-max)))))
238     (gnus-convert-gray-x-face-to-xpm faces)))
239
240 (defface gnus-x-face '((t (:foreground "black" :background "white")))
241   "Face to show X-Face.
242 The colors from this face are used as the foreground and background
243 colors of the displayed X-Faces."
244   :group 'gnus-article-headers)
245
246 (defun gnus-display-x-face-in-from (data)
247   "Display the X-Face DATA in the From header."
248   (let ((default-enable-multibyte-characters nil)
249         pbm)
250     (when (or (gnus-image-type-available-p 'xface)
251               (and (gnus-image-type-available-p 'pbm)
252                    (setq pbm (uncompface data))))
253       (save-excursion
254         (save-restriction
255           (article-narrow-to-head)
256           (gnus-article-goto-header "from")
257           (when (bobp)
258             (insert "From: [no `from' set]\n")
259             (forward-char -17))
260           (gnus-add-image
261            'xface
262            (gnus-put-image
263             (if (gnus-image-type-available-p 'xface)
264                 (gnus-create-image
265                  (concat "X-Face: " data)
266                  'xface t :ascent 'center :face 'gnus-x-face)
267               (gnus-create-image
268                pbm 'pbm t :ascent 'center :face 'gnus-x-face))))
269           (gnus-add-wash-type 'xface))))))
270
271 (defun gnus-grab-cam-x-face ()
272   "Grab a picture off the camera and make it into an X-Face."
273   (interactive)
274   (shell-command "xawtv-remote snap ppm")
275   (let ((file nil))
276     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
277                                              t "snap.*ppm")))
278       (sleep-for 1))
279     (setq file (car file))
280     (with-temp-buffer
281       (shell-command
282        (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"
283                file)
284        (current-buffer))
285       ;;(sleep-for 3)
286       (delete-file file)
287       (buffer-string))))
288
289 (defun gnus-grab-gray-x-face ()
290   "Grab a picture off the camera and make it into an X-Face."
291   (interactive)
292   (shell-command "xawtv-remote snap ppm")
293   (let ((file nil))
294     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
295                                              t "snap.*ppm")))
296       (sleep-for 1))
297     (setq file (car file))
298     (with-temp-buffer
299       (shell-command
300        (format "pnmcut -left 70 -top 100 -width 144 -height 144 '%s' | ppmquant 256 2>/dev/null | ppmtogif > '%s.gif'"
301                file file)
302        (current-buffer))
303       (delete-file file))
304     (gnus-convert-image-to-gray-x-face (concat file ".gif") 3)
305     (delete-file (concat file ".gif"))))
306
307 (provide 'gnus-fun)
308
309 ;;; gnus-fun.el ends here