Move image files to etc/gnus.
[gnus] / lisp / gnus-ems.el
1 ;;; gnus-ems.el --- functions for making Gnus work under different Emacsen
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
3 ;;        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., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (require 'ring))
32
33 ;;; Function aliases later to be redefined for XEmacs usage.
34
35 (defvar gnus-mouse-2 [mouse-2])
36 (defvar gnus-down-mouse-3 [down-mouse-3])
37 (defvar gnus-down-mouse-2 [down-mouse-2])
38 (defvar gnus-widget-button-keymap nil)
39 (defvar gnus-mode-line-modified
40   (if (or (featurep 'xemacs)
41           (< emacs-major-version 20))
42       '("--**-" . "-----")
43     '("**" "--")))
44
45 (eval-and-compile
46   (autoload 'gnus-xmas-define "gnus-xmas")
47   (autoload 'gnus-xmas-redefine "gnus-xmas")
48   (autoload 'appt-select-lowest-window "appt"))
49
50 (if (featurep 'xemacs)
51     (autoload 'gnus-smiley-display "smiley")
52   (autoload 'gnus-smiley-display "smiley-ems") ; override XEmacs version
53 )
54
55 (defun gnus-kill-all-overlays ()
56   "Delete all overlays in the current buffer."
57   (let* ((overlayss (overlay-lists))
58          (buffer-read-only nil)
59          (overlays (delq nil (nconc (car overlayss) (cdr overlayss)))))
60     (while overlays
61       (delete-overlay (pop overlays)))))
62
63 ;;; Mule functions.
64
65 (defun gnus-mule-max-width-function (el max-width)
66   `(let* ((val (eval (, el)))
67           (valstr (if (numberp val)
68                       (int-to-string val) val)))
69      (if (> (length valstr) ,max-width)
70          (truncate-string-to-width valstr ,max-width)
71        valstr)))
72
73 (eval-and-compile
74   (if (featurep 'xemacs)
75       (gnus-xmas-define)
76     (defvar gnus-mouse-face-prop 'mouse-face
77       "Property used for highlighting mouse regions.")))
78
79 (eval-and-compile
80   (let ((case-fold-search t))
81     (cond
82      ((string-match "windows-nt\\|os/2\\|emx\\|cygwin32"
83                     (symbol-name system-type))
84       (setq nnheader-file-name-translation-alist
85             (append nnheader-file-name-translation-alist
86                     (mapcar (lambda (c) (cons c ?_))
87                             '(?: ?* ?\" ?< ?> ??))
88                     '((?+ . ?-))))))))
89
90 (defvar gnus-tmp-unread)
91 (defvar gnus-tmp-replied)
92 (defvar gnus-tmp-score-char)
93 (defvar gnus-tmp-indentation)
94 (defvar gnus-tmp-opening-bracket)
95 (defvar gnus-tmp-lines)
96 (defvar gnus-tmp-name)
97 (defvar gnus-tmp-closing-bracket)
98 (defvar gnus-tmp-subject-or-nil)
99 (defvar gnus-check-before-posting)
100
101 (defun gnus-ems-redefine ()
102   (cond
103    ((featurep 'xemacs)
104     (gnus-xmas-redefine))
105
106    ((featurep 'mule)
107     ;; Mule and new Emacs definitions
108
109     ;; [Note] Now there are three kinds of mule implementations,
110     ;; original MULE, XEmacs/mule and Emacs 20+ including
111     ;; MULE features.  Unfortunately these API are different.  In
112     ;; particular, Emacs (including original MULE) and XEmacs are
113     ;; quite different.  Howvere, this version of Gnus doesn't support
114     ;; anything other than XEmacs 20+ and Emacs 20.3+.
115
116     ;; Predicates to check are following:
117     ;; (boundp 'MULE) is t only if MULE (original; anything older than
118     ;;                     Mule 2.3) is running.
119     ;; (featurep 'mule) is t when every mule variants are running.
120
121     ;; It is possible to detect XEmacs/mule by (featurep 'mule) and
122     ;; checking `emacs-version'.  In this case, the implementation for
123     ;; XEmacs/mule may be shareable between XEmacs and XEmacs/mule.
124
125     (defvar gnus-summary-display-table nil
126       "Display table used in summary mode buffers.")
127     (defalias 'gnus-max-width-function 'gnus-mule-max-width-function)
128
129     (when (boundp 'gnus-check-before-posting)
130       (setq gnus-check-before-posting
131             (delq 'long-lines
132                   (delq 'control-chars gnus-check-before-posting))))
133
134     (defun gnus-summary-line-format-spec ()
135       (insert gnus-tmp-unread gnus-tmp-replied
136               gnus-tmp-score-char gnus-tmp-indentation)
137       (put-text-property
138        (point)
139        (progn
140          (insert
141           gnus-tmp-opening-bracket
142           (format "%4d: %-20s"
143                   gnus-tmp-lines
144                   (if (> (length gnus-tmp-name) 20)
145                       (truncate-string-to-width gnus-tmp-name 20)
146                     gnus-tmp-name))
147           gnus-tmp-closing-bracket)
148          (point))
149        gnus-mouse-face-prop gnus-mouse-face)
150       (insert " " gnus-tmp-subject-or-nil "\n")))))
151
152 (defun gnus-region-active-p ()
153   "Say whether the region is active."
154   (and (boundp 'transient-mark-mode)
155        transient-mark-mode
156        (boundp 'mark-active)
157        mark-active))
158
159 (if (fboundp 'add-minor-mode)
160     (defalias 'gnus-add-minor-mode 'add-minor-mode)
161   (defun gnus-add-minor-mode (mode name map &rest rest)
162     (set (make-local-variable mode) t)
163     (unless (assq mode minor-mode-alist)
164       (push `(,mode ,name) minor-mode-alist))
165     (unless (assq mode minor-mode-map-alist)
166       (push (cons mode map)
167             minor-mode-map-alist))))
168
169 (defun gnus-x-splash ()
170   "Show a splash screen using a pixmap in the current buffer."
171   (let ((dir (nnheader-find-etc-directory "gnus"))
172         pixmap file height beg i)
173     (save-excursion
174       (switch-to-buffer (gnus-get-buffer-create gnus-group-buffer))
175       (let ((buffer-read-only nil)
176             width height)
177         (erase-buffer)
178         (when (and dir
179                    (file-exists-p (setq file
180                                         (expand-file-name "x-splash" dir))))
181           (with-temp-buffer
182             (insert-file-contents file)
183             (goto-char (point-min))
184             (ignore-errors
185               (setq pixmap (read (current-buffer))))))
186         (when pixmap
187           (make-face 'gnus-splash)
188           (setq height (/ (car pixmap) (frame-char-height))
189                 width (/ (cadr pixmap) (frame-char-width)))
190           (set-face-foreground 'gnus-splash "Brown")
191           (set-face-stipple 'gnus-splash pixmap)
192           (insert-char ?\n (* (/ (window-height) 2 height) height))
193           (setq i height)
194           (while (> i 0)
195             (insert-char ?\  (* (/ (window-width) 2 width) width))
196             (setq beg (point))
197             (insert-char ?\  width)
198             (set-text-properties beg (point) '(face gnus-splash))
199             (insert ?\n)
200             (decf i))
201           (goto-char (point-min))
202           (sit-for 0))))))
203
204 (defvar gnus-article-xface-ring-internal nil
205   "Cache for face data.")
206
207 ;; Worth customizing?
208 (defvar gnus-article-xface-ring-size 6
209   "Length of the ring used for `gnus-article-xface-ring-internal'.")
210
211 (defvar gnus-article-compface-xbm
212   (condition-case ()
213       (eq 0 (string-match "#define" 
214                           (shell-command-to-string "uncompface -X")))
215     (error nil))
216   "Non-nil means the compface program supports the -X option.
217 That produces XBM output.")
218
219 (defun gnus-article-display-xface (beg end)
220   "Display an XFace header from between BEG and END in the current article.
221 Requires support for images in your Emacs and the external programs
222 `uncompface', and `icontopbm'.  On a GNU/Linux system these
223 might be in packages with names like `compface' or `faces-xface' and
224 `netpbm' or `libgr-progs', for instance.  See also
225 `gnus-article-compface-xbm'.
226
227 This function is for Emacs 21+.  See `gnus-xmas-article-display-xface'
228 for XEmacs."
229   ;; It might be worth converting uncompface's output in Lisp.
230
231   (when (if (fboundp 'display-graphic-p)
232             (display-graphic-p))
233     (unless gnus-article-xface-ring-internal ; Only load ring when needed.
234       (setq gnus-article-xface-ring-internal
235             (make-ring gnus-article-xface-ring-size)))
236     (save-excursion
237       (let* ((cur (current-buffer))
238              (data (buffer-substring beg end))
239              (image (cdr-safe (assoc data (ring-elements
240                                            gnus-article-xface-ring-internal))))
241              default-enable-multibyte-characters)
242         (unless image
243           (with-temp-buffer
244             (insert data)
245             (and (eq 0 (apply #'call-process-region (point-min) (point-max)
246                               "uncompface"
247                               'delete '(t nil) nil
248                               (if gnus-article-compface-xbm
249                                   '("-X"))))
250                  (if gnus-article-compface-xbm
251                      t
252                    (goto-char (point-min))
253                    (progn (insert "/* Width=48, Height=48 */\n") t)
254                    (eq 0 (call-process-region (point-min) (point-max)
255                                               "icontopbm"
256                                               'delete '(t nil))))
257                  ;; Miles Bader says that faces don't look right as
258                  ;; light on dark.
259                  (if (eq 'dark (cdr-safe (assq 'background-mode
260                                                (frame-parameters))))
261                      (setq image (create-image (buffer-string)
262                                                (if gnus-article-compface-xbm
263                                                    'xbm
264                                                  'pbm)
265                                                t
266                                                :ascent 'center
267                                                :foreground "black"
268                                                :background "white"))
269                    (setq image (create-image (buffer-string)
270                                              (if gnus-article-compface-xbm
271                                                  'xbm
272                                                'pbm)
273                                              t
274                                              :ascent 'center)))))
275           (ring-insert gnus-article-xface-ring-internal (cons data image)))
276         (when image
277           (goto-char (point-min))
278           (re-search-forward "^From:" nil 'move)
279           (insert-image image))))))
280
281 (provide 'gnus-ems)
282
283 ;;; gnus-ems.el ends here