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