comparison lisp/gnus/gnus-fun.el @ 56927:55fd4f77387a after-merge-gnus-5_10

Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523 Merge from emacs--gnus--5.10, gnus--rel--5.10 Patches applied: * miles@gnu.org--gnu-2004/emacs--gnus--5.10--base-0 tag of miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-464 * miles@gnu.org--gnu-2004/emacs--gnus--5.10--patch-1 Import from CVS branch gnus-5_10-branch * miles@gnu.org--gnu-2004/emacs--gnus--5.10--patch-2 Merge from lorentey@elte.hu--2004/emacs--multi-tty--0, emacs--cvs-trunk--0 * miles@gnu.org--gnu-2004/emacs--gnus--5.10--patch-3 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2004/emacs--gnus--5.10--patch-4 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-18 Update from CVS * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-19 Remove autoconf-generated files from archive * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-20 Update from CVS
author Miles Bader <miles@gnu.org>
date Sat, 04 Sep 2004 13:13:48 +0000
parents
children 497f0d2ca551
comparison
equal deleted inserted replaced
56926:f8e248e9a717 56927:55fd4f77387a
1 ;;; gnus-fun.el --- various frivolous extension functions to Gnus
2 ;; Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile
29 (require 'cl)
30 (require 'mm-util))
31
32 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
33 "*Directory where X-Face PBM files are stored."
34 :group 'gnus-fun
35 :type 'directory)
36
37 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
38 "Command for converting a PBM to an X-Face."
39 :group 'gnus-fun
40 :type 'string)
41
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
45 on stdout."
46 :group 'gnus-fun
47 :type 'string)
48
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
52 on stdout."
53 :group 'gnus-fun
54 :type 'string)
55
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)))
61
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))
68
69 ;;;###autoload
70 (defun gnus-random-x-face ()
71 "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
72 (interactive)
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)))
76 (when file
77 (gnus-shell-command-to-string
78 (format gnus-convert-pbm-to-x-face-command
79 (shell-quote-argument file)))))))
80
81 ;;;###autoload
82 (defun gnus-insert-random-x-face-header ()
83 "Insert a random X-Face header from `gnus-x-face-directory'."
84 (interactive)
85 (let ((data (gnus-random-x-face)))
86 (save-excursion
87 (message-goto-eoh)
88 (if data
89 (insert "X-Face: " data)
90 (message
91 "No face returned by `gnus-random-x-face'. Does %s/*.pbm exist?"
92 gnus-x-face-directory)))))
93
94 ;;;###autoload
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))))))
102
103 ;;;###autoload
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)
108 (let ((done nil)
109 (attempt "")
110 (quant 16))
111 (while (and (not done)
112 (> quant 1))
113 (setq attempt
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))
118 quant))))
119 (if (> (length attempt) 726)
120 (progn
121 (setq quant (- quant 2))
122 (gnus-message 9 "Length %d; trying quant %d"
123 (length attempt) quant))
124 (setq done t)))
125 (if done
126 (mm-with-unibyte-buffer
127 (insert attempt)
128 (gnus-face-encode))
129 nil))))
130
131 (defun gnus-face-encode ()
132 (let ((step 72))
133 (base64-encode-region (point-min) (point-max))
134 (goto-char (point-min))
135 (while (search-forward "\n" nil t)
136 (replace-match ""))
137 (goto-char (point-min))
138 (while (> (- (point-max) (point))
139 step)
140 (forward-char step)
141 (insert "\n ")
142 (setq step 76))
143 (buffer-string)))
144
145 ;;;###autoload
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
150 (insert face)
151 (ignore-errors
152 (base64-decode-region (point-min) (point-max)))
153 (buffer-string)))
154
155 ;;;###autoload
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
159 726 bytes."
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"
164 (buffer-size)))
165 (gnus-face-encode)))
166
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)
172
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)
176 pbm)
177 (when (or (gnus-image-type-available-p 'xface)
178 (and (gnus-image-type-available-p 'pbm)
179 (setq pbm (uncompface data))))
180 (save-excursion
181 (save-restriction
182 (article-narrow-to-head)
183 (gnus-article-goto-header "from")
184 (when (bobp)
185 (insert "From: [no `from' set]\n")
186 (forward-char -17))
187 (gnus-add-image
188 'xface
189 (gnus-put-image
190 (if (gnus-image-type-available-p 'xface)
191 (gnus-create-image
192 (concat "X-Face: " data)
193 'xface t :face 'gnus-x-face)
194 (gnus-create-image
195 pbm 'pbm t :face 'gnus-x-face)) nil 'xface))
196 (gnus-add-wash-type 'xface))))))
197
198 (defun gnus-grab-cam-x-face ()
199 "Grab a picture off the camera and make it into an X-Face."
200 (interactive)
201 (shell-command "xawtv-remote snap ppm")
202 (let ((file nil))
203 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
204 t "snap.*ppm")))
205 (sleep-for 1))
206 (setq file (car file))
207 (with-temp-buffer
208 (shell-command
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"
210 file)
211 (current-buffer))
212 ;;(sleep-for 3)
213 (delete-file file)
214 (buffer-string))))
215
216 (defun gnus-grab-cam-face ()
217 "Grab a picture off the camera and make it into an X-Face."
218 (interactive)
219 (shell-command "xawtv-remote snap ppm")
220 (let ((file nil)
221 result)
222 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
223 t "snap.*ppm")))
224 (sleep-for 1))
225 (setq file (car file))
226 (shell-command
227 (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
228 file))
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")))
233 (delete-file file)
234 ;;(delete-file "/tmp/gnus.face.ppm")
235 result))
236
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)
242 "'"))
243 (values nil))
244 (dotimes (i 255)
245 (push (format format i i i i i i)
246 values))
247 (mapconcat 'identity values " ")))
248
249 (provide 'gnus-fun)
250
251 ;;; arch-tag: 9d000a69-15cc-4491-9dc0-4627484f50c1
252 ;;; gnus-fun.el ends here