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