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."
41 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
42 "Command for converting a PBM to an X-Face."
47 (defcustom gnus-convert-image-to-x-face-command "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface"
48 "Command for converting an image to an X-Face.
49 By default it takes a GIF filename and output the X-Face header data
55 (defcustom gnus-convert-image-to-face-command "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng"
56 "Command for converting an image to an Face.
57 By default it takes a JPEG filename and output the Face header data
63 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
64 '((xface . (:face gnus-x-face)))
65 '((pbm . (:face gnus-x-face))
67 "Alist of image types and properties applied to Face and X-Face images.
70 ;; Specify the altitude of Face images in the From header.
71 \(setq gnus-face-properties-alist
72 '((pbm . (:face gnus-x-face :ascent 80))
73 (png . (:ascent 80))))
75 ;; Show Face images as pressed buttons.
76 \(setq gnus-face-properties-alist
77 '((pbm . (:face gnus-x-face :relief -2))
78 (png . (:relief -2))))
80 See the manual for the valid properties for various image types.
81 Currently, `pbm' is used for X-Face images and `png' is used for Face
82 images in Emacs. Only the `:face' property is effective on the `xface'
83 image type in XEmacs if it is built with the libcompface library."
85 :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
87 (defun gnus-shell-command-to-string (command)
88 "Like `shell-command-to-string' except not mingling ERROR."
89 (with-output-to-string
90 (call-process shell-file-name nil (list standard-output nil)
91 nil shell-command-switch command)))
93 (defun gnus-shell-command-on-region (start end command)
94 "A simplified `shell-command-on-region'.
95 Output to the current buffer, replace text, and don't mingle error."
96 (call-process-region start end shell-file-name t
97 (list (current-buffer) nil)
98 nil shell-command-switch command))
101 (defun gnus-random-x-face ()
102 "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
104 (when (file-exists-p gnus-x-face-directory)
105 (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
106 (file (nth (random (length files)) files)))
108 (gnus-shell-command-to-string
109 (format gnus-convert-pbm-to-x-face-command
110 (shell-quote-argument file)))))))
113 (defun gnus-insert-random-x-face-header ()
114 "Insert a random X-Face header from `gnus-x-face-directory'."
116 (let ((data (gnus-random-x-face)))
120 (insert "X-Face: " data)
122 "No face returned by `gnus-random-x-face'. Does %s/*.pbm exist?"
123 gnus-x-face-directory)))))
126 (defun gnus-x-face-from-file (file)
127 "Insert an X-Face header based on an image file."
128 (interactive "fImage file name (by default GIF): ")
129 (when (file-exists-p file)
130 (gnus-shell-command-to-string
131 (format gnus-convert-image-to-x-face-command
132 (shell-quote-argument (expand-file-name file))))))
135 (defun gnus-face-from-file (file)
136 "Return an Face header based on an image file."
137 (interactive "fImage file name (by default JPEG): ")
138 (when (file-exists-p file)
142 (while (and (not done)
145 (let ((coding-system-for-read 'binary))
146 (gnus-shell-command-to-string
147 (format gnus-convert-image-to-face-command
148 (shell-quote-argument (expand-file-name file))
150 (if (> (length attempt) 726)
152 (setq quant (- quant 2))
153 (gnus-message 9 "Length %d; trying quant %d"
154 (length attempt) quant))
157 (mm-with-unibyte-buffer
162 (defun gnus-face-encode ()
164 (base64-encode-region (point-min) (point-max))
165 (goto-char (point-min))
166 (while (search-forward "\n" nil t)
168 (goto-char (point-min))
169 (while (> (- (point-max) (point))
177 (defun gnus-convert-face-to-png (face)
178 "Convert FACE (which is base64-encoded) to a PNG.
179 The PNG is returned as a string."
180 (mm-with-unibyte-buffer
183 (base64-decode-region (point-min) (point-max)))
187 (defun gnus-convert-png-to-face (file)
188 "Convert FILE to a Face.
189 FILE should be a PNG file that's 48x48 and smaller than or equal to
191 (mm-with-unibyte-buffer
192 (insert-file-contents file)
193 (when (> (buffer-size) 726)
194 (error "The file is %d bytes long, which is too long"
198 (defface gnus-x-face '((t (:foreground "black" :background "white")))
199 "Face to show X-Face.
200 The colors from this face are used as the foreground and background
201 colors of the displayed X-Faces."
202 :group 'gnus-article-headers)
204 (defun gnus-display-x-face-in-from (data)
205 "Display the X-Face DATA in the From header."
206 (let ((default-enable-multibyte-characters nil)
208 (when (or (gnus-image-type-available-p 'xface)
209 (and (gnus-image-type-available-p 'pbm)
210 (setq pbm (uncompface data))))
213 (article-narrow-to-head)
214 (gnus-article-goto-header "from")
216 (insert "From: [no `from' set]\n")
221 (if (gnus-image-type-available-p 'xface)
222 (apply 'gnus-create-image (concat "X-Face: " data) 'xface t
223 (cdr (assq 'xface gnus-face-properties-alist)))
224 (apply 'gnus-create-image pbm 'pbm t
225 (cdr (assq 'pbm gnus-face-properties-alist))))
227 (gnus-add-wash-type 'xface))))))
229 (defun gnus-grab-cam-x-face ()
230 "Grab a picture off the camera and make it into an X-Face."
232 (shell-command "xawtv-remote snap ppm")
234 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
237 (setq file (car file))
240 (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"
247 (defun gnus-grab-cam-face ()
248 "Grab a picture off the camera and make it into an X-Face."
250 (shell-command "xawtv-remote snap ppm")
253 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
256 (setq file (car file))
258 (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
260 (let ((gnus-convert-image-to-face-command
261 (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
262 (gnus-fun-ppm-change-string))))
263 (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
265 ;;(delete-file "/tmp/gnus.face.ppm")
268 (defun gnus-fun-ppm-change-string ()
269 (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
270 "%02x%02x00" "00%02x%02x" "%02x00%02x"))
271 (format (concat "'#%02x%02x%02x' '#"
272 (nth (random 6) possibilites)
276 (push (format format i i i i i i)
278 (mapconcat 'identity values " ")))
282 ;;; gnus-fun.el ends here