1 ;;; gnus-fun.el --- various frivolous extension functions to Gnus
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
36 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
37 "*Directory where X-Face PBM files are stored."
42 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
43 "Command for converting a PBM to an X-Face."
48 (defcustom gnus-convert-image-to-x-face-command
49 "convert -scale 48x48! %s xbm:- | xbm2xface.pl"
50 "Command for converting an image to an X-Face.
51 The command must take a image filename (use \"%s\") as input.
52 The output must be the Face header data on stdout in PNG format.
54 By default it takes a GIF filename and output the X-Face header data
58 :type '(choice (const :tag "giftopnm, netpbm (GIF input only)"
59 "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface")
61 "convert -scale 48x48! %s xbm:- | xbm2xface.pl")
64 (defcustom gnus-convert-image-to-face-command
65 "convert -scale 48x48! %s -colors %d png:-"
66 "Command for converting an image to a Face.
68 The command must take an image filename (first format argument
69 \"%s\") and the number of colors (second format argument: \"%d\")
70 as input. The output must be the Face header data on stdout in
74 :type '(choice (const :tag "djpeg, netpbm (JPG input only)"
75 "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng")
77 "convert -scale 48x48! %s -colors %d png:-")
80 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
81 '((xface . (:face gnus-x-face)))
82 '((pbm . (:face gnus-x-face))
84 "Alist of image types and properties applied to Face and X-Face images.
87 ;; Specify the altitude of Face images in the From header.
88 \(setq gnus-face-properties-alist
89 '((pbm . (:face gnus-x-face :ascent 80))
90 (png . (:ascent 80))))
92 ;; Show Face images as pressed buttons.
93 \(setq gnus-face-properties-alist
94 '((pbm . (:face gnus-x-face :relief -2))
95 (png . (:relief -2))))
97 See the manual for the valid properties for various image types.
98 Currently, `pbm' is used for X-Face images and `png' is used for Face
99 images in Emacs. Only the `:face' property is effective on the `xface'
100 image type in XEmacs if it is built with the libcompface library."
101 :version "23.0" ;; No Gnus
103 :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
105 (defun gnus-shell-command-to-string (command)
106 "Like `shell-command-to-string' except not mingling ERROR."
107 (with-output-to-string
108 (call-process shell-file-name nil (list standard-output nil)
109 nil shell-command-switch command)))
111 (defun gnus-shell-command-on-region (start end command)
112 "A simplified `shell-command-on-region'.
113 Output to the current buffer, replace text, and don't mingle error."
114 (call-process-region start end shell-file-name t
115 (list (current-buffer) nil)
116 nil shell-command-switch command))
119 (defun gnus-random-x-face ()
120 "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
122 (when (file-exists-p gnus-x-face-directory)
123 (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
124 (file (nth (random (length files)) files)))
126 (gnus-shell-command-to-string
127 (format gnus-convert-pbm-to-x-face-command
128 (shell-quote-argument file)))))))
131 (defun gnus-insert-random-x-face-header ()
132 "Insert a random X-Face header from `gnus-x-face-directory'."
134 (let ((data (gnus-random-x-face)))
138 (insert "X-Face: " data)
140 "No face returned by `gnus-random-x-face'. Does %s/*.pbm exist?"
141 gnus-x-face-directory)))))
144 (defun gnus-x-face-from-file (file)
145 "Insert an X-Face header based on an image file.
147 Depending on `gnus-convert-image-to-x-face-command' it may accept
148 different input formats."
149 (interactive "fImage file name: ")
150 (when (file-exists-p file)
151 (gnus-shell-command-to-string
152 (format gnus-convert-image-to-x-face-command
153 (shell-quote-argument (expand-file-name file))))))
156 (defun gnus-face-from-file (file)
157 "Return a Face header based on an image file.
159 Depending on `gnus-convert-image-to-face-command' it may accept
160 different input formats."
161 (interactive "fImage file name: ")
162 (when (file-exists-p file)
166 (while (and (not done)
169 (let ((coding-system-for-read 'binary))
170 (gnus-shell-command-to-string
171 (format gnus-convert-image-to-face-command
172 (shell-quote-argument (expand-file-name file))
174 (if (> (length attempt) 726)
176 (setq quant (- quant (if (< quant 10) 1 2)))
177 (gnus-message 9 "Length %d; trying quant %d"
178 (length attempt) quant))
181 (mm-with-unibyte-buffer
186 (defun gnus-face-encode ()
188 (base64-encode-region (point-min) (point-max))
189 (goto-char (point-min))
190 (while (search-forward "\n" nil t)
192 (goto-char (point-min))
193 (while (> (- (point-max) (point))
201 (defun gnus-convert-face-to-png (face)
202 "Convert FACE (which is base64-encoded) to a PNG.
203 The PNG is returned as a string."
204 (mm-with-unibyte-buffer
207 (base64-decode-region (point-min) (point-max)))
211 (defun gnus-convert-png-to-face (file)
212 "Convert FILE to a Face.
213 FILE should be a PNG file that's 48x48 and smaller than or equal to
215 (mm-with-unibyte-buffer
216 (insert-file-contents file)
217 (when (> (buffer-size) 726)
218 (error "The file is %d bytes long, which is too long"
222 (defface gnus-x-face '((t (:foreground "black" :background "white")))
223 "Face to show X-Face.
224 The colors from this face are used as the foreground and background
225 colors of the displayed X-Faces."
226 :group 'gnus-article-headers)
228 (defun gnus-display-x-face-in-from (data)
229 "Display the X-Face DATA in the From header."
230 (let ((default-enable-multibyte-characters nil)
232 (when (or (gnus-image-type-available-p 'xface)
233 (and (gnus-image-type-available-p 'pbm)
234 (setq pbm (uncompface data))))
237 (article-narrow-to-head)
238 (gnus-article-goto-header "from")
240 (insert "From: [no `from' set]\n")
245 (if (gnus-image-type-available-p 'xface)
246 (apply 'gnus-create-image (concat "X-Face: " data) 'xface t
247 (cdr (assq 'xface gnus-face-properties-alist)))
248 (apply 'gnus-create-image pbm 'pbm t
249 (cdr (assq 'pbm gnus-face-properties-alist))))
251 (gnus-add-wash-type 'xface))))))
253 (defun gnus-grab-cam-x-face ()
254 "Grab a picture off the camera and make it into an X-Face."
256 (shell-command "xawtv-remote snap ppm")
258 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
261 (setq file (car file))
264 (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"
271 (defun gnus-grab-cam-face ()
272 "Grab a picture off the camera and make it into an X-Face."
274 (shell-command "xawtv-remote snap ppm")
277 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
280 (setq file (car file))
282 (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
284 (let ((gnus-convert-image-to-face-command
285 (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
286 (gnus-fun-ppm-change-string))))
287 (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
289 ;;(delete-file "/tmp/gnus.face.ppm")
292 (defun gnus-fun-ppm-change-string ()
293 (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
294 "%02x%02x00" "00%02x%02x" "%02x00%02x"))
295 (format (concat "'#%02x%02x%02x' '#"
296 (nth (random 6) possibilites)
300 (push (format format i i i i i i)
302 (mapconcat 'identity values " ")))
306 ;;; arch-tag: 9d000a69-15cc-4491-9dc0-4627484f50c1
307 ;;; gnus-fun.el ends here