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 (defun gnus-shell-command-to-string (command)
60 "Like `shell-command-to-string' except not mingling ERROR."
61 (with-output-to-string
62 (call-process shell-file-name nil (list standard-output nil)
63 nil shell-command-switch command)))
65 (defun gnus-shell-command-on-region (start end command)
66 "A simplified `shell-command-on-region'.
67 Output to the current buffer, replace text, and don't mingle error."
68 (call-process-region start end shell-file-name t
69 (list (current-buffer) nil)
70 nil shell-command-switch command))
73 (defun gnus-random-x-face ()
74 "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
76 (when (file-exists-p gnus-x-face-directory)
77 (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
78 (file (nth (random (length files)) files)))
80 (gnus-shell-command-to-string
81 (format gnus-convert-pbm-to-x-face-command
82 (shell-quote-argument file)))))))
85 (defun gnus-insert-random-x-face-header ()
86 "Insert a random X-Face header from `gnus-x-face-directory'."
88 (let ((data (gnus-random-x-face)))
92 (insert "X-Face: " data)
94 "No face returned by `gnus-random-x-face'. Does %s/*.pbm exist?"
95 gnus-x-face-directory)))))
98 (defun gnus-x-face-from-file (file)
99 "Insert an X-Face header based on an image file."
100 (interactive "fImage file name (by default GIF): ")
101 (when (file-exists-p file)
102 (gnus-shell-command-to-string
103 (format gnus-convert-image-to-x-face-command
104 (shell-quote-argument (expand-file-name file))))))
107 (defun gnus-face-from-file (file)
108 "Return an Face header based on an image file."
109 (interactive "fImage file name (by default JPEG): ")
110 (when (file-exists-p file)
114 (while (and (not done)
117 (let ((coding-system-for-read 'binary))
118 (gnus-shell-command-to-string
119 (format gnus-convert-image-to-face-command
120 (shell-quote-argument (expand-file-name file))
122 (if (> (length attempt) 726)
124 (setq quant (- quant 2))
125 (gnus-message 9 "Length %d; trying quant %d"
126 (length attempt) quant))
129 (mm-with-unibyte-buffer
134 (defun gnus-face-encode ()
136 (base64-encode-region (point-min) (point-max))
137 (goto-char (point-min))
138 (while (search-forward "\n" nil t)
140 (goto-char (point-min))
141 (while (> (- (point-max) (point))
149 (defun gnus-convert-face-to-png (face)
150 "Convert FACE (which is base64-encoded) to a PNG.
151 The PNG is returned as a string."
152 (mm-with-unibyte-buffer
155 (base64-decode-region (point-min) (point-max)))
159 (defun gnus-convert-png-to-face (file)
160 "Convert FILE to a Face.
161 FILE should be a PNG file that's 48x48 and smaller than or equal to
163 (mm-with-unibyte-buffer
164 (insert-file-contents file)
165 (when (> (buffer-size) 726)
166 (error "The file is %d bytes long, which is too long"
170 (defface gnus-x-face '((t (:foreground "black" :background "white")))
171 "Face to show X-Face.
172 The colors from this face are used as the foreground and background
173 colors of the displayed X-Faces."
174 :group 'gnus-article-headers)
176 (defun gnus-display-x-face-in-from (data)
177 "Display the X-Face DATA in the From header."
178 (let ((default-enable-multibyte-characters nil)
180 (when (or (gnus-image-type-available-p 'xface)
181 (and (gnus-image-type-available-p 'pbm)
182 (setq pbm (uncompface data))))
185 (article-narrow-to-head)
186 (gnus-article-goto-header "from")
188 (insert "From: [no `from' set]\n")
193 (if (gnus-image-type-available-p 'xface)
195 (concat "X-Face: " data)
196 'xface t :face 'gnus-x-face)
198 pbm 'pbm t :face 'gnus-x-face)) nil 'xface))
199 (gnus-add-wash-type 'xface))))))
201 (defun gnus-grab-cam-x-face ()
202 "Grab a picture off the camera and make it into an X-Face."
204 (shell-command "xawtv-remote snap ppm")
206 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
209 (setq file (car file))
212 (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"
219 (defun gnus-grab-cam-face ()
220 "Grab a picture off the camera and make it into an X-Face."
222 (shell-command "xawtv-remote snap ppm")
225 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
228 (setq file (car file))
230 (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
232 (let ((gnus-convert-image-to-face-command
233 (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
234 (gnus-fun-ppm-change-string))))
235 (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
237 ;;(delete-file "/tmp/gnus.face.ppm")
240 (defun gnus-fun-ppm-change-string ()
241 (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
242 "%02x%02x00" "00%02x%02x" "%02x00%02x"))
243 (format (concat "'#%02x%02x%02x' '#"
244 (nth (random 6) possibilites)
248 (push (format format i i i i i i)
250 (mapconcat 'identity values " ")))
254 ;;; gnus-fun.el ends here