Add arch taglines
[gnus] / lisp / gnus-fun.el
1 ;;; gnus-fun.el --- various frivolous extension functions to Gnus
2 ;; Copyright (C) 2002, 2003 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 (eval-when-compile
29   (require 'cl))
30
31 (require 'mm-util)
32 (require 'gnus-ems)
33 (require 'gnus-util)
34
35 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
36   "*Directory where X-Face PBM files are stored."
37   :group 'gnus-fun
38   :type 'directory)
39
40 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
41   "Command for converting a PBM to an X-Face."
42   :group 'gnus-fun
43   :type 'string)
44
45 (defcustom gnus-convert-image-to-x-face-command "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface"
46   "Command for converting an image to an X-Face.
47 By default it takes a GIF filename and output the X-Face header data
48 on stdout."
49   :group 'gnus-fun
50   :type 'string)
51
52 (defcustom gnus-convert-image-to-face-command "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng"
53   "Command for converting an image to an Face.
54 By default it takes a JPEG filename and output the Face header data
55 on stdout."
56   :group 'gnus-fun
57   :type 'string)
58
59 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
60                                           '((xface . (:face gnus-x-face)))
61                                         '((pbm . (:face gnus-x-face))
62                                           (png . nil)))
63   "Alist of image types and properties applied to Face and X-Face images.
64 Here are examples:
65
66 ;; Specify the altitude of Face images in the From header.
67 \(setq gnus-face-properties-alist
68       '((pbm . (:face gnus-x-face :ascent 80))
69         (png . (:ascent 80))))
70
71 ;; Show Face images as pressed buttons.
72 \(setq gnus-face-properties-alist
73       '((pbm . (:face gnus-x-face :relief -2))
74         (png . (:relief -2))))
75
76 See the manual for the valid properties for various image types.
77 Currently, `pbm' is used for X-Face images and `png' is used for Face
78 images in Emacs.  Only the `:face' property is effective on the `xface'
79 image type in XEmacs if it is built with the libcompface library."
80   :group 'gnus-fun
81   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
82
83 (defun gnus-shell-command-to-string (command)
84   "Like `shell-command-to-string' except not mingling ERROR."
85   (with-output-to-string
86     (call-process shell-file-name nil (list standard-output nil)
87                   nil shell-command-switch command)))
88
89 (defun gnus-shell-command-on-region (start end command)
90   "A simplified `shell-command-on-region'.
91 Output to the current buffer, replace text, and don't mingle error."
92   (call-process-region start end shell-file-name t
93                        (list (current-buffer) nil)
94                        nil shell-command-switch command))
95
96 ;;;###autoload
97 (defun gnus-random-x-face ()
98   "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
99   (interactive)
100   (when (file-exists-p gnus-x-face-directory)
101     (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
102            (file (nth (random (length files)) files)))
103       (when file
104         (gnus-shell-command-to-string
105          (format gnus-convert-pbm-to-x-face-command
106                  (shell-quote-argument file)))))))
107
108 ;;;###autoload
109 (defun gnus-insert-random-x-face-header ()
110   "Insert a random X-Face header from `gnus-x-face-directory'."
111   (interactive)
112   (let ((data (gnus-random-x-face)))
113     (save-excursion
114       (message-goto-eoh)
115       (if data
116           (insert "X-Face: " data)
117         (message
118          "No face returned by `gnus-random-x-face'.  Does %s/*.pbm exist?"
119          gnus-x-face-directory)))))
120
121 ;;;###autoload
122 (defun gnus-x-face-from-file (file)
123   "Insert an X-Face header based on an image file."
124   (interactive "fImage file name (by default GIF): ")
125   (when (file-exists-p file)
126     (gnus-shell-command-to-string
127      (format gnus-convert-image-to-x-face-command
128              (shell-quote-argument (expand-file-name file))))))
129
130 ;;;###autoload
131 (defun gnus-face-from-file (file)
132   "Return an Face header based on an image file."
133   (interactive "fImage file name (by default JPEG): ")
134   (when (file-exists-p file)
135     (let ((done nil)
136           (attempt "")
137           (quant 16))
138       (while (and (not done)
139                   (> quant 1))
140         (setq attempt
141               (let ((coding-system-for-read 'binary))
142                 (gnus-shell-command-to-string
143                  (format gnus-convert-image-to-face-command
144                          (shell-quote-argument (expand-file-name file))
145                          quant))))
146         (if (> (length attempt) 726)
147             (progn
148               (setq quant (- quant 2))
149               (gnus-message 9 "Length %d; trying quant %d"
150                             (length attempt) quant))
151           (setq done t)))
152       (if done
153           (mm-with-unibyte-buffer
154             (insert attempt)
155             (gnus-face-encode))
156         nil))))
157
158 (defun gnus-face-encode ()
159   (let ((step 72))
160     (base64-encode-region (point-min) (point-max))
161     (goto-char (point-min))
162     (while (search-forward "\n" nil t)
163       (replace-match ""))
164     (goto-char (point-min))
165     (while (> (- (point-max) (point))
166               step)
167       (forward-char step)
168       (insert "\n ")
169       (setq step 76))
170     (buffer-string)))
171
172 ;;;###autoload
173 (defun gnus-convert-face-to-png (face)
174   "Convert FACE (which is base64-encoded) to a PNG.
175 The PNG is returned as a string."
176   (mm-with-unibyte-buffer
177     (insert face)
178     (ignore-errors
179       (base64-decode-region (point-min) (point-max)))
180     (buffer-string)))
181
182 ;;;###autoload
183 (defun gnus-convert-png-to-face (file)
184   "Convert FILE to a Face.
185 FILE should be a PNG file that's 48x48 and smaller than or equal to
186 726 bytes."
187   (mm-with-unibyte-buffer
188     (insert-file-contents file)
189     (when (> (buffer-size) 726)
190       (error "The file is %d bytes long, which is too long"
191              (buffer-size)))
192     (gnus-face-encode)))
193
194 (defface gnus-x-face '((t (:foreground "black" :background "white")))
195   "Face to show X-Face.
196 The colors from this face are used as the foreground and background
197 colors of the displayed X-Faces."
198   :group 'gnus-article-headers)
199
200 (defun gnus-display-x-face-in-from (data)
201   "Display the X-Face DATA in the From header."
202   (let ((default-enable-multibyte-characters nil)
203         pbm)
204     (when (or (gnus-image-type-available-p 'xface)
205               (and (gnus-image-type-available-p 'pbm)
206                    (setq pbm (uncompface data))))
207       (save-excursion
208         (save-restriction
209           (article-narrow-to-head)
210           (gnus-article-goto-header "from")
211           (when (bobp)
212             (insert "From: [no `from' set]\n")
213             (forward-char -17))
214           (gnus-add-image
215            'xface
216            (gnus-put-image
217             (if (gnus-image-type-available-p 'xface)
218                 (apply 'gnus-create-image (concat "X-Face: " data) 'xface t
219                        (cdr (assq 'xface gnus-face-properties-alist)))
220               (apply 'gnus-create-image pbm 'pbm t
221                      (cdr (assq 'pbm gnus-face-properties-alist))))
222             nil 'xface))
223           (gnus-add-wash-type 'xface))))))
224
225 (defun gnus-grab-cam-x-face ()
226   "Grab a picture off the camera and make it into an X-Face."
227   (interactive)
228   (shell-command "xawtv-remote snap ppm")
229   (let ((file nil))
230     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
231                                              t "snap.*ppm")))
232       (sleep-for 1))
233     (setq file (car file))
234     (with-temp-buffer
235       (shell-command
236        (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"
237                file)
238        (current-buffer))
239       ;;(sleep-for 3)
240       (delete-file file)
241       (buffer-string))))
242
243 (defun gnus-grab-cam-face ()
244   "Grab a picture off the camera and make it into an X-Face."
245   (interactive)
246   (shell-command "xawtv-remote snap ppm")
247   (let ((file nil)
248         result)
249     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
250                                              t "snap.*ppm")))
251       (sleep-for 1))
252     (setq file (car file))
253     (shell-command
254      (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
255              file))
256     (let ((gnus-convert-image-to-face-command
257            (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
258                    (gnus-fun-ppm-change-string))))
259       (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
260     (delete-file file)
261     ;;(delete-file "/tmp/gnus.face.ppm")
262     result))
263
264 (defun gnus-fun-ppm-change-string ()
265   (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
266                         "%02x%02x00" "00%02x%02x" "%02x00%02x"))
267          (format (concat "'#%02x%02x%02x' '#"
268                          (nth (random 6) possibilites)
269                          "'"))
270          (values nil))
271   (dotimes (i 255)
272     (push (format format i i i i i i)
273           values))
274   (mapconcat 'identity values " ")))
275
276 (provide 'gnus-fun)
277
278 ;;; arch-tag: 9d000a69-15cc-4491-9dc0-4627484f50c1
279 ;;; gnus-fun.el ends here