(mm-codepage-setup): New helper function.
[gnus] / lisp / gnus-fun.el
1 ;;; gnus-fun.el --- various frivolous extension functions to Gnus
2
3 ;; Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl))
31
32 (require 'mm-util)
33 (require 'gnus-ems)
34 (require 'gnus-util)
35
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)
41
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)
47
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)
55
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)
63
64 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
65                                           '((xface . (:face gnus-x-face)))
66                                         '((pbm . (:face gnus-x-face))
67                                           (png . nil)))
68   "Alist of image types and properties applied to Face and X-Face images.
69 Here are examples:
70
71 ;; Specify the altitude of Face images in the From header.
72 \(setq gnus-face-properties-alist
73       '((pbm . (:face gnus-x-face :ascent 80))
74         (png . (:ascent 80))))
75
76 ;; Show Face images as pressed buttons.
77 \(setq gnus-face-properties-alist
78       '((pbm . (:face gnus-x-face :relief -2))
79         (png . (:relief -2))))
80
81 See the manual for the valid properties for various image types.
82 Currently, `pbm' is used for X-Face images and `png' is used for Face
83 images in Emacs.  Only the `:face' property is effective on the `xface'
84 image type in XEmacs if it is built with the libcompface library."
85   :group 'gnus-fun
86   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
87
88 (defun gnus-shell-command-to-string (command)
89   "Like `shell-command-to-string' except not mingling ERROR."
90   (with-output-to-string
91     (call-process shell-file-name nil (list standard-output nil)
92                   nil shell-command-switch command)))
93
94 (defun gnus-shell-command-on-region (start end command)
95   "A simplified `shell-command-on-region'.
96 Output to the current buffer, replace text, and don't mingle error."
97   (call-process-region start end shell-file-name t
98                        (list (current-buffer) nil)
99                        nil shell-command-switch command))
100
101 ;;;###autoload
102 (defun gnus-random-x-face ()
103   "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
104   (interactive)
105   (when (file-exists-p gnus-x-face-directory)
106     (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
107            (file (nth (random (length files)) files)))
108       (when file
109         (gnus-shell-command-to-string
110          (format gnus-convert-pbm-to-x-face-command
111                  (shell-quote-argument file)))))))
112
113 ;;;###autoload
114 (defun gnus-insert-random-x-face-header ()
115   "Insert a random X-Face header from `gnus-x-face-directory'."
116   (interactive)
117   (let ((data (gnus-random-x-face)))
118     (save-excursion
119       (message-goto-eoh)
120       (if data
121           (insert "X-Face: " data)
122         (message
123          "No face returned by `gnus-random-x-face'.  Does %s/*.pbm exist?"
124          gnus-x-face-directory)))))
125
126 ;;;###autoload
127 (defun gnus-x-face-from-file (file)
128   "Insert an X-Face header based on an image file."
129   (interactive "fImage file name (by default GIF): ")
130   (when (file-exists-p file)
131     (gnus-shell-command-to-string
132      (format gnus-convert-image-to-x-face-command
133              (shell-quote-argument (expand-file-name file))))))
134
135 ;;;###autoload
136 (defun gnus-face-from-file (file)
137   "Return a Face header based on an image file."
138   (interactive "fImage file name (by default JPEG): ")
139   (when (file-exists-p file)
140     (let ((done nil)
141           (attempt "")
142           (quant 16))
143       (while (and (not done)
144                   (> quant 1))
145         (setq attempt
146               (let ((coding-system-for-read 'binary))
147                 (gnus-shell-command-to-string
148                  (format gnus-convert-image-to-face-command
149                          (shell-quote-argument (expand-file-name file))
150                          quant))))
151         (if (> (length attempt) 726)
152             (progn
153               (setq quant (- quant 2))
154               (gnus-message 9 "Length %d; trying quant %d"
155                             (length attempt) quant))
156           (setq done t)))
157       (if done
158           (mm-with-unibyte-buffer
159             (insert attempt)
160             (gnus-face-encode))
161         nil))))
162
163 (defun gnus-face-encode ()
164   (let ((step 72))
165     (base64-encode-region (point-min) (point-max))
166     (goto-char (point-min))
167     (while (search-forward "\n" nil t)
168       (replace-match ""))
169     (goto-char (point-min))
170     (while (> (- (point-max) (point))
171               step)
172       (forward-char step)
173       (insert "\n ")
174       (setq step 76))
175     (buffer-string)))
176
177 ;;;###autoload
178 (defun gnus-convert-face-to-png (face)
179   "Convert FACE (which is base64-encoded) to a PNG.
180 The PNG is returned as a string."
181   (mm-with-unibyte-buffer
182     (insert face)
183     (ignore-errors
184       (base64-decode-region (point-min) (point-max)))
185     (buffer-string)))
186
187 ;;;###autoload
188 (defun gnus-convert-png-to-face (file)
189   "Convert FILE to a Face.
190 FILE should be a PNG file that's 48x48 and smaller than or equal to
191 726 bytes."
192   (mm-with-unibyte-buffer
193     (insert-file-contents file)
194     (when (> (buffer-size) 726)
195       (error "The file is %d bytes long, which is too long"
196              (buffer-size)))
197     (gnus-face-encode)))
198
199 (defface gnus-x-face '((t (:foreground "black" :background "white")))
200   "Face to show X-Face.
201 The colors from this face are used as the foreground and background
202 colors of the displayed X-Faces."
203   :group 'gnus-article-headers)
204
205 (defun gnus-display-x-face-in-from (data)
206   "Display the X-Face DATA in the From header."
207   (let ((default-enable-multibyte-characters nil)
208         pbm)
209     (when (or (gnus-image-type-available-p 'xface)
210               (and (gnus-image-type-available-p 'pbm)
211                    (setq pbm (uncompface data))))
212       (save-excursion
213         (save-restriction
214           (article-narrow-to-head)
215           (gnus-article-goto-header "from")
216           (when (bobp)
217             (insert "From: [no `from' set]\n")
218             (forward-char -17))
219           (gnus-add-image
220            'xface
221            (gnus-put-image
222             (if (gnus-image-type-available-p 'xface)
223                 (apply 'gnus-create-image (concat "X-Face: " data) 'xface t
224                        (cdr (assq 'xface gnus-face-properties-alist)))
225               (apply 'gnus-create-image pbm 'pbm t
226                      (cdr (assq 'pbm gnus-face-properties-alist))))
227             nil 'xface))
228           (gnus-add-wash-type 'xface))))))
229
230 (defun gnus-grab-cam-x-face ()
231   "Grab a picture off the camera and make it into an X-Face."
232   (interactive)
233   (shell-command "xawtv-remote snap ppm")
234   (let ((file nil))
235     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
236                                              t "snap.*ppm")))
237       (sleep-for 1))
238     (setq file (car file))
239     (with-temp-buffer
240       (shell-command
241        (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"
242                file)
243        (current-buffer))
244       ;;(sleep-for 3)
245       (delete-file file)
246       (buffer-string))))
247
248 (defun gnus-grab-cam-face ()
249   "Grab a picture off the camera and make it into an X-Face."
250   (interactive)
251   (shell-command "xawtv-remote snap ppm")
252   (let ((file nil)
253         result)
254     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
255                                              t "snap.*ppm")))
256       (sleep-for 1))
257     (setq file (car file))
258     (shell-command
259      (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
260              file))
261     (let ((gnus-convert-image-to-face-command
262            (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
263                    (gnus-fun-ppm-change-string))))
264       (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
265     (delete-file file)
266     ;;(delete-file "/tmp/gnus.face.ppm")
267     result))
268
269 (defun gnus-fun-ppm-change-string ()
270   (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
271                         "%02x%02x00" "00%02x%02x" "%02x00%02x"))
272          (format (concat "'#%02x%02x%02x' '#"
273                          (nth (random 6) possibilites)
274                          "'"))
275          (values nil))
276   (dotimes (i 255)
277     (push (format format i i i i i i)
278           values))
279   (mapconcat 'identity values " ")))
280
281 (provide 'gnus-fun)
282
283 ;;; arch-tag: 9d000a69-15cc-4491-9dc0-4627484f50c1
284 ;;; gnus-fun.el ends here