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