c898bc9d00ffd98a89c97f8e8aad3cae30672786
[elisp/gnus.git-] / lisp / gnus-ems.el
1 ;;; gnus-ems.el --- functions for making Semi-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 ;;         Tatsuya Ichikawa <t-ichi@niagara.shiojiri.ne.jp>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 ;;; Function aliases later to be redefined for XEmacs usage.
33
34 (eval-and-compile
35   (defvar gnus-xemacs (featurep 'xemacs)
36     "Non-nil if running under XEmacs."))
37
38 (defvar gnus-mouse-2 [mouse-2])
39 (defvar gnus-down-mouse-3 [down-mouse-3])
40 (defvar gnus-down-mouse-2 [down-mouse-2])
41 (defvar gnus-widget-button-keymap nil)
42 (defvar gnus-mode-line-modified
43   (if (or gnus-xemacs
44           (< emacs-major-version 20))
45       '("--**-" . "-----")
46     '("**" "--")))
47
48 (eval-and-compile
49   (autoload 'gnus-xmas-define "gnus-xmas")
50   (autoload 'gnus-xmas-redefine "gnus-xmas")
51   (autoload 'appt-select-lowest-window "appt"))
52
53 (or (fboundp 'mail-file-babyl-p)
54     (fset 'mail-file-babyl-p 'rmail-file-p))
55
56 (when (and (not (featurep 'xemacs)) (>= emacs-major-version 21))
57   (autoload 'gnus-smiley-display "smiley-ems")) ; override XEmacs version
58
59 ;;; Mule functions.
60
61 (eval-and-compile
62   (if gnus-xemacs
63       (gnus-xmas-define)
64     (defvar gnus-mouse-face-prop 'mouse-face
65       "Property used for highlighting mouse regions.")))
66
67 (eval-and-compile
68   (let ((case-fold-search t))
69     (cond
70      ((string-match "windows-nt\\|os/2\\|emx\\|cygwin32"
71                     (symbol-name system-type))
72       (setq nnheader-file-name-translation-alist
73             (append nnheader-file-name-translation-alist
74                     (mapcar (lambda (c) (cons c ?_))
75                             '(?: ?* ?\" ?< ?> ??))
76                     '((?+ . ?-))))))))
77
78 (defvar gnus-tmp-unread)
79 (defvar gnus-tmp-replied)
80 (defvar gnus-tmp-score-char)
81 (defvar gnus-tmp-indentation)
82 (defvar gnus-tmp-opening-bracket)
83 (defvar gnus-tmp-lines)
84 (defvar gnus-tmp-name)
85 (defvar gnus-tmp-closing-bracket)
86 (defvar gnus-tmp-subject-or-nil)
87
88 (defun gnus-ems-redefine ()
89   (cond
90    (gnus-xemacs
91     (gnus-xmas-redefine))
92
93    ((featurep 'mule)
94     ;; Mule and new Emacs definitions
95
96     ;; [Note] Now there are three kinds of mule implementations,
97     ;; original MULE, XEmacs/mule and Emacs 20+ including
98     ;; MULE features.  Unfortunately these API are different.  In
99     ;; particular, Emacs (including original MULE) and XEmacs are
100     ;; quite different.  However, this version of Gnus doesn't support
101     ;; anything other than XEmacs 20+ and Emacs 20.3+.
102
103     ;; Predicates to check are following:
104     ;; (boundp 'MULE) is t only if MULE (original; anything older than
105     ;;                     Mule 2.3) is running.
106     ;; (featurep 'mule) is t when every mule variants are running.
107
108     ;; It is possible to detect XEmacs/mule by (featurep 'mule) and
109     ;; checking `emacs-version'.  In this case, the implementation for
110     ;; XEmacs/mule may be shareable between XEmacs and XEmacs/mule.
111
112     (defvar gnus-summary-display-table nil
113       "Display table used in summary mode buffers.")
114
115     (if (fboundp 'truncate-string-to-width)
116         (fset 'gnus-truncate-string 'truncate-string-to-width)
117       (fset 'gnus-truncate-string 'truncate-string))
118
119     (when (boundp 'gnus-check-before-posting)
120       (setq gnus-check-before-posting
121             (delq 'long-lines
122                   (delq 'control-chars gnus-check-before-posting))))
123     ))
124   (when (featurep 'mule)
125     (defun gnus-tilde-max-form (el max-width)
126       "Return a form that limits EL to MAX-WIDTH."
127       (let ((max (abs max-width)))
128         (if (symbolp el)
129             (if (< max-width 0)
130                 `(let ((width (string-width ,el)))
131                    (gnus-truncate-string ,el width (- width ,max)))
132               `(gnus-truncate-string ,el ,max))
133           (if (< max-width 0)
134               `(let* ((val (eval ,el))
135                       (width (string-width val)))
136                  (gnus-truncate-string val width (- width ,max)))
137             `(let ((val (eval ,el)))
138                (gnus-truncate-string val ,max))))))
139
140     (defun gnus-tilde-cut-form (el cut-width)
141       "Return a form that cuts CUT-WIDTH off of EL."
142       (let ((cut (abs cut-width)))
143         (if (symbolp el)
144             (if (< cut-width 0)
145                 `(gnus-truncate-string ,el (- (string-width ,el) ,cut))
146               `(gnus-truncate-string ,el (string-width ,el) ,cut))
147           (if (< cut-width 0)
148               `(let ((val (eval ,el)))
149                  (gnus-truncate-string val (- (string-width val) ,cut)))
150             `(let ((val (eval ,el)))
151                (gnus-truncate-string val (string-width val) ,cut))))))
152     ))
153
154 (defun gnus-region-active-p ()
155   "Say whether the region is active."
156   (and (boundp 'transient-mark-mode)
157        transient-mark-mode
158        (boundp 'mark-active)
159        mark-active))
160
161 (if (fboundp 'add-minor-mode)
162     (defalias 'gnus-add-minor-mode 'add-minor-mode)
163   (defun gnus-add-minor-mode (mode name map &rest rest)
164     (set (make-local-variable mode) t)
165     (unless (assq mode minor-mode-alist)
166       (push `(,mode ,name) minor-mode-alist))
167     (unless (assq mode minor-mode-map-alist)
168       (push (cons mode map)
169             minor-mode-map-alist))))
170
171 (defun gnus-x-splash ()
172   "Show a splash screen using a pixmap in the current buffer."
173   (let ((dir (nnheader-find-etc-directory "gnus"))
174         pixmap file height beg i)
175     (save-excursion
176       (switch-to-buffer (gnus-get-buffer-create gnus-group-buffer))
177       (let ((buffer-read-only nil)
178             width height)
179         (erase-buffer)
180         (when (and dir
181                    (file-exists-p (setq file
182                                         (expand-file-name "x-splash" dir))))
183           (with-temp-buffer
184             (insert-file-contents-as-binary file)
185             (goto-char (point-min))
186             (ignore-errors
187               (setq pixmap (read (current-buffer))))))
188         (when pixmap
189           (make-face 'gnus-splash)
190           (setq height (/ (car pixmap) (frame-char-height))
191                 width (/ (cadr pixmap) (frame-char-width)))
192           (set-face-foreground 'gnus-splash "Brown")
193           (set-face-stipple 'gnus-splash pixmap)
194           (insert-char ?\n (* (/ (window-height) 2 height) height))
195           (setq i height)
196           (while (> i 0)
197             (insert-char ?\  (* (/ (window-width) 2 width) width))
198             (setq beg (point))
199             (insert-char ?\  width)
200             (set-text-properties beg (point) '(face gnus-splash))
201             (insert ?\n)
202             (decf i))
203           (goto-char (point-min))
204           (sit-for 0))))))
205
206 (defvar gnus-article-xface-ring-internal nil
207   "Cache for face data.")
208
209 ;; Worth customizing?
210 (defvar gnus-article-xface-ring-size 6
211   "Length of the ring used for `gnus-article-xface-ring-internal'.")
212
213 (defvar gnus-article-compface-xbm
214   (when (and (not (featurep 'xemacs)) (>= emacs-major-version 21))
215     (eq 0 (string-match "#define" (shell-command-to-string "uncompface -X"))))
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 (defun-maybe assoc-ignore-case (key alist)
282   "Like `assoc', but assumes KEY is a string and ignores case when comparing."
283   (setq key (downcase key))
284   (let (element)
285     (while (and alist (not element))
286       (if (equal key (downcase (car (car alist))))
287           (setq element (car alist)))
288       (setq alist (cdr alist)))
289     element))
290
291 \f
292 ;;; Language support staffs.
293
294 (defvar-maybe current-language-environment "English"
295   "The language environment.")
296
297 (defvar-maybe language-info-alist nil
298   "Alist of language environment definitions.")
299
300 (defun-maybe get-language-info (lang-env key)
301   "Return information listed under KEY for language environment LANG-ENV."
302   (if (symbolp lang-env)
303       (setq lang-env (symbol-name lang-env)))
304   (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
305     (if lang-slot
306         (cdr (assq key (cdr lang-slot))))))
307
308 (defun-maybe set-language-info (lang-env key info)
309   "Modify part of the definition of language environment LANG-ENV."
310   (if (symbolp lang-env)
311       (setq lang-env (symbol-name lang-env)))
312   (let (lang-slot key-slot)
313     (setq lang-slot (assoc lang-env language-info-alist))
314     (if (null lang-slot)                ; If no slot for the language, add it.
315         (setq lang-slot (list lang-env)
316               language-info-alist (cons lang-slot language-info-alist)))
317     (setq key-slot (assq key lang-slot))
318     (if (null key-slot)                 ; If no slot for the key, add it.
319         (progn
320           (setq key-slot (list key))
321           (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
322     (setcdr key-slot info)))
323
324 (provide 'gnus-ems)
325
326 ;; Local Variables:
327 ;; byte-compile-warnings: '(redefine callargs)
328 ;; End:
329
330 ;;; gnus-ems.el ends here