(save-completions-file-name): Fix typo in last change.
[emacs.git] / lisp / gnus / gnus-fun.el
blob43cd02c1b5e879637950f64b1bfbf3a85c7a04d2
1 ;;; gnus-fun.el --- various frivolous extension functions to Gnus
3 ;; Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
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)
13 ;; any later version.
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.
25 ;;; Commentary:
27 ;;; Code:
29 (eval-when-compile
30 (require 'cl))
32 (require 'mm-util)
33 (require 'gnus-ems)
34 (require 'gnus-util)
36 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
37 "*Directory where X-Face PBM files are stored."
38 :version "22.1"
39 :group 'gnus-fun
40 :type 'directory)
42 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
43 "Command for converting a PBM to an X-Face."
44 :version "22.1"
45 :group 'gnus-fun
46 :type 'string)
48 (defcustom gnus-convert-image-to-x-face-command "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface"
49 "Command for converting an image to an X-Face.
50 By default it takes a GIF filename and output the X-Face header data
51 on stdout."
52 :version "22.1"
53 :group 'gnus-fun
54 :type 'string)
56 (defcustom gnus-convert-image-to-face-command "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng"
57 "Command for converting an image to a Face.
58 By default it takes a JPEG filename and output the Face header data
59 on stdout."
60 :version "22.1"
61 :group 'gnus-fun
62 :type 'string)
64 (defun gnus-shell-command-to-string (command)
65 "Like `shell-command-to-string' except not mingling ERROR."
66 (with-output-to-string
67 (call-process shell-file-name nil (list standard-output nil)
68 nil shell-command-switch command)))
70 (defun gnus-shell-command-on-region (start end command)
71 "A simplified `shell-command-on-region'.
72 Output to the current buffer, replace text, and don't mingle error."
73 (call-process-region start end shell-file-name t
74 (list (current-buffer) nil)
75 nil shell-command-switch command))
77 ;;;###autoload
78 (defun gnus-random-x-face ()
79 "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
80 (interactive)
81 (when (file-exists-p gnus-x-face-directory)
82 (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
83 (file (nth (random (length files)) files)))
84 (when file
85 (gnus-shell-command-to-string
86 (format gnus-convert-pbm-to-x-face-command
87 (shell-quote-argument file)))))))
89 ;;;###autoload
90 (defun gnus-insert-random-x-face-header ()
91 "Insert a random X-Face header from `gnus-x-face-directory'."
92 (interactive)
93 (let ((data (gnus-random-x-face)))
94 (save-excursion
95 (message-goto-eoh)
96 (if data
97 (insert "X-Face: " data)
98 (message
99 "No face returned by `gnus-random-x-face'. Does %s/*.pbm exist?"
100 gnus-x-face-directory)))))
102 ;;;###autoload
103 (defun gnus-x-face-from-file (file)
104 "Insert an X-Face header based on an image file."
105 (interactive "fImage file name (by default GIF): ")
106 (when (file-exists-p file)
107 (gnus-shell-command-to-string
108 (format gnus-convert-image-to-x-face-command
109 (shell-quote-argument (expand-file-name file))))))
111 ;;;###autoload
112 (defun gnus-face-from-file (file)
113 "Return a Face header based on an image file."
114 (interactive "fImage file name (by default JPEG): ")
115 (when (file-exists-p file)
116 (let ((done nil)
117 (attempt "")
118 (quant 16))
119 (while (and (not done)
120 (> quant 1))
121 (setq attempt
122 (let ((coding-system-for-read 'binary))
123 (gnus-shell-command-to-string
124 (format gnus-convert-image-to-face-command
125 (shell-quote-argument (expand-file-name file))
126 quant))))
127 (if (> (length attempt) 726)
128 (progn
129 (setq quant (- quant 2))
130 (gnus-message 9 "Length %d; trying quant %d"
131 (length attempt) quant))
132 (setq done t)))
133 (if done
134 (mm-with-unibyte-buffer
135 (insert attempt)
136 (gnus-face-encode))
137 nil))))
139 (defun gnus-face-encode ()
140 (let ((step 72))
141 (base64-encode-region (point-min) (point-max))
142 (goto-char (point-min))
143 (while (search-forward "\n" nil t)
144 (replace-match ""))
145 (goto-char (point-min))
146 (while (> (- (point-max) (point))
147 step)
148 (forward-char step)
149 (insert "\n ")
150 (setq step 76))
151 (buffer-string)))
153 ;;;###autoload
154 (defun gnus-convert-face-to-png (face)
155 "Convert FACE (which is base64-encoded) to a PNG.
156 The PNG is returned as a string."
157 (mm-with-unibyte-buffer
158 (insert face)
159 (ignore-errors
160 (base64-decode-region (point-min) (point-max)))
161 (buffer-string)))
163 ;;;###autoload
164 (defun gnus-convert-png-to-face (file)
165 "Convert FILE to a Face.
166 FILE should be a PNG file that's 48x48 and smaller than or equal to
167 726 bytes."
168 (mm-with-unibyte-buffer
169 (insert-file-contents file)
170 (when (> (buffer-size) 726)
171 (error "The file is %d bytes long, which is too long"
172 (buffer-size)))
173 (gnus-face-encode)))
175 (defface gnus-x-face '((t (:foreground "black" :background "white")))
176 "Face to show X-Face.
177 The colors from this face are used as the foreground and background
178 colors of the displayed X-Faces."
179 :group 'gnus-article-headers)
181 (defun gnus-display-x-face-in-from (data)
182 "Display the X-Face DATA in the From header."
183 (let ((default-enable-multibyte-characters nil)
184 pbm)
185 (when (or (gnus-image-type-available-p 'xface)
186 (and (gnus-image-type-available-p 'pbm)
187 (setq pbm (uncompface data))))
188 (save-excursion
189 (save-restriction
190 (article-narrow-to-head)
191 (gnus-article-goto-header "from")
192 (when (bobp)
193 (insert "From: [no `from' set]\n")
194 (forward-char -17))
195 (gnus-add-image
196 'xface
197 (gnus-put-image
198 (if (gnus-image-type-available-p 'xface)
199 (gnus-create-image
200 (concat "X-Face: " data)
201 'xface t :face 'gnus-x-face)
202 (gnus-create-image
203 pbm 'pbm t :face 'gnus-x-face)) nil 'xface))
204 (gnus-add-wash-type 'xface))))))
206 (defun gnus-grab-cam-x-face ()
207 "Grab a picture off the camera and make it into an X-Face."
208 (interactive)
209 (shell-command "xawtv-remote snap ppm")
210 (let ((file nil))
211 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
212 t "snap.*ppm")))
213 (sleep-for 1))
214 (setq file (car file))
215 (with-temp-buffer
216 (shell-command
217 (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"
218 file)
219 (current-buffer))
220 ;;(sleep-for 3)
221 (delete-file file)
222 (buffer-string))))
224 (defun gnus-grab-cam-face ()
225 "Grab a picture off the camera and make it into an X-Face."
226 (interactive)
227 (shell-command "xawtv-remote snap ppm")
228 (let ((file nil)
229 result)
230 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
231 t "snap.*ppm")))
232 (sleep-for 1))
233 (setq file (car file))
234 (shell-command
235 (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
236 file))
237 (let ((gnus-convert-image-to-face-command
238 (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
239 (gnus-fun-ppm-change-string))))
240 (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
241 (delete-file file)
242 ;;(delete-file "/tmp/gnus.face.ppm")
243 result))
245 (defun gnus-fun-ppm-change-string ()
246 (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
247 "%02x%02x00" "00%02x%02x" "%02x00%02x"))
248 (format (concat "'#%02x%02x%02x' '#"
249 (nth (random 6) possibilites)
250 "'"))
251 (values nil))
252 (dotimes (i 255)
253 (push (format format i i i i i i)
254 values))
255 (mapconcat 'identity values " ")))
257 (provide 'gnus-fun)
259 ;;; arch-tag: 9d000a69-15cc-4491-9dc0-4627484f50c1
260 ;;; gnus-fun.el ends here