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.
32 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
33 "*Directory where X-Face PBM files are stored."
37 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
38 "Command for converting a PBM to an X-Face."
42 (defcustom gnus-convert-image-to-x-face-command "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface"
43 "Command for converting an image to an X-Face.
44 By default it takes a GIF filename and output the X-Face header data
49 (defcustom gnus-convert-image-to-face-command "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng"
50 "Command for converting an image to an Face.
51 By default it takes a JPEG filename and output the Face header data
56 (defun gnus-shell-command-to-string (command)
57 "Like `shell-command-to-string' except not mingling ERROR."
58 (with-output-to-string
59 (call-process shell-file-name nil (list standard-output nil)
60 nil shell-command-switch command)))
62 (defun gnus-shell-command-on-region (start end command)
63 "A simplified `shell-command-on-region'.
64 Output to the current buffer, replace text, and don't mingle error."
65 (call-process-region start end shell-file-name t
66 (list (current-buffer) nil)
67 nil shell-command-switch command))
70 (defun gnus-random-x-face ()
71 "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
73 (when (file-exists-p gnus-x-face-directory)
74 (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
75 (file (nth (random (length files)) files)))
77 (gnus-shell-command-to-string
78 (format gnus-convert-pbm-to-x-face-command
79 (shell-quote-argument file)))))))
82 (defun gnus-insert-random-x-face-header ()
83 "Insert a random X-Face header from `gnus-x-face-directory'."
85 (let ((data (gnus-random-x-face)))
89 (insert "X-Face: " data)
91 "No face returned by `gnus-random-x-face'. Does %s/*.pbm exist?"
92 gnus-x-face-directory)))))
95 (defun gnus-x-face-from-file (file)
96 "Insert an X-Face header based on an image file."
97 (interactive "fImage file name (by default GIF): ")
98 (when (file-exists-p file)
99 (gnus-shell-command-to-string
100 (format gnus-convert-image-to-x-face-command
101 (shell-quote-argument (expand-file-name file))))))
104 (defun gnus-face-from-file (file)
105 "Return an Face header based on an image file."
106 (interactive "fImage file name (by default JPEG): ")
107 (when (file-exists-p file)
111 (while (and (not done)
114 (let ((coding-system-for-read 'binary))
115 (gnus-shell-command-to-string
116 (format gnus-convert-image-to-face-command
117 (shell-quote-argument (expand-file-name file))
119 (if (> (length attempt) 726)
121 (setq quant (- quant 2))
122 (gnus-message 9 "Length %d; trying quant %d"
123 (length attempt) quant))
126 (mm-with-unibyte-buffer
131 (defun gnus-face-encode ()
133 (base64-encode-region (point-min) (point-max))
134 (goto-char (point-min))
135 (while (search-forward "\n" nil t)
137 (goto-char (point-min))
138 (while (> (- (point-max) (point))
146 (defun gnus-convert-face-to-png (face)
147 "Convert FACE (which is base64-encoded) to a PNG.
148 The PNG is returned as a string."
149 (mm-with-unibyte-buffer
152 (base64-decode-region (point-min) (point-max)))
156 (defun gnus-convert-png-to-face (file)
157 "Convert FILE to a Face.
158 FILE should be a PNG file that's 48x48 and smaller than or equal to
160 (mm-with-unibyte-buffer
161 (insert-file-contents file)
162 (when (> (buffer-size) 726)
163 (error "The file is %d bytes long, which is too long"
167 (defface gnus-x-face '((t (:foreground "black" :background "white")))
168 "Face to show X-Face.
169 The colors from this face are used as the foreground and background
170 colors of the displayed X-Faces."
171 :group 'gnus-article-headers)
173 (defun gnus-display-x-face-in-from (data)
174 "Display the X-Face DATA in the From header."
175 (let ((default-enable-multibyte-characters nil)
177 (when (or (gnus-image-type-available-p 'xface)
178 (and (gnus-image-type-available-p 'pbm)
179 (setq pbm (uncompface data))))
182 (article-narrow-to-head)
183 (gnus-article-goto-header "from")
185 (insert "From: [no `from' set]\n")
190 (if (gnus-image-type-available-p 'xface)
192 (concat "X-Face: " data)
193 'xface t :face 'gnus-x-face)
195 pbm 'pbm t :face 'gnus-x-face)) nil 'xface))
196 (gnus-add-wash-type 'xface))))))
198 (defun gnus-grab-cam-x-face ()
199 "Grab a picture off the camera and make it into an X-Face."
201 (shell-command "xawtv-remote snap ppm")
203 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
206 (setq file (car file))
209 (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"
216 (defun gnus-grab-cam-face ()
217 "Grab a picture off the camera and make it into an X-Face."
219 (shell-command "xawtv-remote snap ppm")
222 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
225 (setq file (car file))
227 (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
229 (let ((gnus-convert-image-to-face-command
230 (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
231 (gnus-fun-ppm-change-string))))
232 (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
234 ;;(delete-file "/tmp/gnus.face.ppm")
237 (defun gnus-fun-ppm-change-string ()
238 (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
239 "%02x%02x00" "00%02x%02x" "%02x00%02x"))
240 (format (concat "'#%02x%02x%02x' '#"
241 (nth (random 6) possibilites)
245 (push (format format i i i i i i)
247 (mapconcat 'identity values " ")))
251 ;;; gnus-fun.el ends here