Release commit
[gnus] / lisp / gnus-fun.el
1 ;;; gnus-fun.el --- various frivoluos 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 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
29   "*Directory where X-Face PBM files are stored."
30   :group 'gnus-fun
31   :type 'directory)
32
33 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
34   "Command for converting a PBM to an X-Face."
35   :group 'gnus-fun
36   :type 'string)
37
38 (defcustom gnus-convert-image-to-x-face-command "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface"
39   "Command for converting an image to an X-Face.
40 By default it takes a GIF filename and output the X-Face header data
41 on stdout."
42   :group 'gnus-fun
43   :type 'string)
44
45 (defcustom gnus-convert-image-to-face-command "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng"
46   "Command for converting an image to an Face.
47 By default it takes a JPEG filename and output the Face header data
48 on stdout."
49   :group 'gnus-fun
50   :type 'string)
51
52 (defun gnus-shell-command-to-string (command)
53   "Like `shell-command-to-string' except not mingling ERROR."
54   (with-output-to-string
55     (call-process shell-file-name nil (list standard-output nil)
56                   nil shell-command-switch command)))
57
58 (defun gnus-shell-command-on-region (start end command)
59   "A simplified `shell-command-on-region'.
60 Output to the current buffer, replace text, and don't mingle error."
61   (call-process-region start end shell-file-name t
62                        (list (current-buffer) nil)
63                        nil shell-command-switch command))
64
65 ;;;###autoload
66 (defun gnus-random-x-face ()
67   "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
68   (interactive)
69   (when (file-exists-p gnus-x-face-directory)
70     (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
71            (file (nth (random (length files)) files)))
72       (when file
73         (gnus-shell-command-to-string
74          (format gnus-convert-pbm-to-x-face-command
75                  (shell-quote-argument file)))))))
76
77 ;;;###autoload
78 (defun gnus-insert-random-x-face-header ()
79   "Insert a random X-Face header from `gnus-x-face-directory'."
80   (interactive)
81   (let ((data (gnus-random-x-face)))
82     (save-excursion
83       (message-goto-eoh)
84       (if data
85           (insert "X-Face: " data)
86         (message
87          "No face returned by `gnus-random-x-face'.  Does %s/*.pbm exist?"
88          gnus-x-face-directory)))))