1 ;;; gnus-fun.el --- various frivolous extension functions to Gnus
2 ;; Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
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)
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.
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.
35 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
36 "*Directory where X-Face PBM files are stored."
40 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
41 "Command for converting a PBM to an X-Face."
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
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
59 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
60 '((xface . (:face gnus-x-face)))
61 '((pbm . (:face gnus-x-face))
63 "Alist of image types and properties applied to Face and X-Face images.
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))))
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))))
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."
81 :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
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)))
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))
97 (defun gnus-random-x-face ()
98 "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
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)))
104 (gnus-shell-command-to-string
105 (format gnus-convert-pbm-to-x-face-command
106 (shell-quote-argument file)))))))
109 (defun gnus-insert-random-x-face-header ()
110 "Insert a random X-Face header from `gnus-x-face-directory'."
112 (let ((data (gnus-random-x-face)))
116 (insert "X-Face: " data)
118 "No face returned by `gnus-random-x-face'. Does %s/*.pbm exist?"
119 gnus-x-face-directory)))))
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))))))
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)
138 (while (and (not done)
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))
146 (if (> (length attempt) 726)
148 (setq quant (- quant 2))
149 (gnus-message 9 "Length %d; trying quant %d"
150 (length attempt) quant))
153 (mm-with-unibyte-buffer
158 (defun gnus-face-encode ()
160 (base64-encode-region (point-min) (point-max))
161 (goto-char (point-min))
162 (while (search-forward "\n" nil t)
164 (goto-char (point-min))
165 (while (> (- (point-max) (point))
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
179 (base64-decode-region (point-min) (point-max)))
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
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"
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)
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)
204 (when (or (gnus-image-type-available-p 'xface)
205 (and (gnus-image-type-available-p 'pbm)
206 (setq pbm (uncompface data))))
209 (article-narrow-to-head)
210 (gnus-article-goto-header "from")
212 (insert "From: [no `from' set]\n")
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))))
223 (gnus-add-wash-type 'xface))))))
225 (defun gnus-grab-cam-x-face ()
226 "Grab a picture off the camera and make it into an X-Face."
228 (shell-command "xawtv-remote snap ppm")
230 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
233 (setq file (car file))
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"
243 (defun gnus-grab-cam-face ()
244 "Grab a picture off the camera and make it into an X-Face."
246 (shell-command "xawtv-remote snap ppm")
249 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
252 (setq file (car file))
254 (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
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")))
261 ;;(delete-file "/tmp/gnus.face.ppm")
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)
272 (push (format format i i i i i i)
274 (mapconcat 'identity values " ")))
278 ;;; gnus-fun.el ends here