Sync with `t-gnus-6_14'.
[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 ;;; Mule functions.
57
58 (eval-and-compile
59   (if gnus-xemacs
60       (gnus-xmas-define)
61     (defvar gnus-mouse-face-prop 'mouse-face
62       "Property used for highlighting mouse regions.")))
63
64 (eval-and-compile
65   (cond
66    ((not window-system)
67     (let ((funcs '(mouse-set-point set-face-foreground
68                                    set-face-background x-popup-menu)))
69       (while funcs
70         (unless (fboundp (car funcs))
71           (defalias (car funcs) 'ignore))
72         (setq funcs (cdr funcs)))))))
73
74 (eval-and-compile
75   (let ((case-fold-search t))
76     (cond
77      ((string-match "windows-nt\\|os/2\\|emx\\|cygwin32"
78                     (symbol-name system-type))
79       (setq nnheader-file-name-translation-alist
80             (append nnheader-file-name-translation-alist
81                     (mapcar (lambda (c) (cons c ?_))
82                             '(?: ?* ?\" ?< ?> ??))
83                     '((?+ . ?-))))))))
84
85 (defvar gnus-tmp-unread)
86 (defvar gnus-tmp-replied)
87 (defvar gnus-tmp-score-char)
88 (defvar gnus-tmp-indentation)
89 (defvar gnus-tmp-opening-bracket)
90 (defvar gnus-tmp-lines)
91 (defvar gnus-tmp-name)
92 (defvar gnus-tmp-closing-bracket)
93 (defvar gnus-tmp-subject-or-nil)
94
95 (defun gnus-ems-redefine ()
96   (cond
97    (gnus-xemacs
98     (gnus-xmas-redefine))
99
100    ((featurep 'mule)
101     ;; Mule and new Emacs definitions
102
103     ;; [Note] Now there are three kinds of mule implementations,
104     ;; original MULE, XEmacs/mule and Emacs 20+ including
105     ;; MULE features.  Unfortunately these API are different.  In
106     ;; particular, Emacs (including original MULE) and XEmacs are
107     ;; quite different.  However, this version of Gnus doesn't support
108     ;; anything other than XEmacs 20+ and Emacs 20.3+.
109
110     ;; Predicates to check are following:
111     ;; (boundp 'MULE) is t only if MULE (original; anything older than
112     ;;                     Mule 2.3) is running.
113     ;; (featurep 'mule) is t when every mule variants are running.
114
115     ;; It is possible to detect XEmacs/mule by (featurep 'mule) and
116     ;; checking `emacs-version'.  In this case, the implementation for
117     ;; XEmacs/mule may be shareable between XEmacs and XEmacs/mule.
118
119     (defvar gnus-summary-display-table nil
120       "Display table used in summary mode buffers.")
121     (defalias 'gnus-summary-set-display-table (lambda ()))
122
123     (if (fboundp 'truncate-string-to-width)
124         (fset 'gnus-truncate-string 'truncate-string-to-width)
125       (fset 'gnus-truncate-string 'truncate-string))
126
127     (when (boundp 'gnus-check-before-posting)
128       (setq gnus-check-before-posting
129             (delq 'long-lines
130                   (delq 'control-chars gnus-check-before-posting))))
131     ))
132   (when (featurep 'mule)
133     (defun gnus-tilde-max-form (el max-width)
134       "Return a form that limits EL to MAX-WIDTH."
135       (let ((max (abs max-width)))
136         (if (symbolp el)
137             (if (< max-width 0)
138                 `(let ((width (string-width ,el)))
139                    (gnus-truncate-string ,el width (- width ,max)))
140               `(gnus-truncate-string ,el ,max))
141           (if (< max-width 0)
142               `(let* ((val (eval ,el))
143                       (width (string-width val)))
144                  (gnus-truncate-string val width (- width ,max)))
145             `(let ((val (eval ,el)))
146                (gnus-truncate-string val ,max))))))
147
148     (defun gnus-tilde-cut-form (el cut-width)
149       "Return a form that cuts CUT-WIDTH off of EL."
150       (let ((cut (abs cut-width)))
151         (if (symbolp el)
152             (if (< cut-width 0)
153                 `(gnus-truncate-string ,el (- (string-width ,el) ,cut))
154               `(gnus-truncate-string ,el (string-width ,el) ,cut))
155           (if (< cut-width 0)
156               `(let ((val (eval ,el)))
157                  (gnus-truncate-string val (- (string-width val) ,cut)))
158             `(let ((val (eval ,el)))
159                (gnus-truncate-string val (string-width val) ,cut))))))
160     ))
161
162 (defun gnus-region-active-p ()
163   "Say whether the region is active."
164   (and (boundp 'transient-mark-mode)
165        transient-mark-mode
166        (boundp 'mark-active)
167        mark-active))
168
169 (if (fboundp 'add-minor-mode)
170     (defalias 'gnus-add-minor-mode 'add-minor-mode)
171   (defun gnus-add-minor-mode (mode name map &rest rest)
172     (set (make-local-variable mode) t)
173     (unless (assq mode minor-mode-alist)
174       (push `(,mode ,name) minor-mode-alist))
175     (unless (assq mode minor-mode-map-alist)
176       (push (cons mode map)
177             minor-mode-map-alist))))
178
179 (defun gnus-x-splash ()
180   "Show a splash screen using a pixmap in the current buffer."
181   (let ((dir (nnheader-find-etc-directory "gnus"))
182         pixmap file height beg i)
183     (save-excursion
184       (switch-to-buffer (gnus-get-buffer-create gnus-group-buffer))
185       (let ((buffer-read-only nil))
186         (erase-buffer)
187         (when (and dir
188                    (file-exists-p (setq file
189                                         (expand-file-name "x-splash" dir))))
190           (with-temp-buffer
191             (insert-file-contents-as-binary file)
192             (goto-char (point-min))
193             (ignore-errors
194               (setq pixmap (read (current-buffer))))))
195         (when pixmap
196           (make-face 'gnus-splash)
197           (setq height (/ (car pixmap) (frame-char-height))
198                 width (/ (cadr pixmap) (frame-char-width)))
199           (set-face-foreground 'gnus-splash "Brown")
200           (set-face-stipple 'gnus-splash pixmap)
201           (insert-char ?\n (* (/ (window-height) 2 height) height))
202           (setq i height)
203           (while (> i 0)
204             (insert-char ?\  (* (/ (window-width) 2 width) width))
205             (setq beg (point))
206             (insert-char ?\  width)
207             (set-text-properties beg (point) '(face gnus-splash))
208             (insert ?\n)
209             (decf i))
210           (goto-char (point-min))
211           (sit-for 0))))))
212
213 (defvar gnus-article-xface-ring-internal nil
214   "Cache for face data.")
215
216 ;; Worth customizing?
217 (defvar gnus-article-xface-ring-size 6
218   "Length of the ring used for `gnus-article-xface-ring-internal'.")
219
220 (defun gnus-article-display-xface (beg end)
221   "Display an XFace header from between BEG and END in the current article.
222 Requires support for images in your Emacs and the external programs
223 `uncompface', `icontopbm' and `ppmtoxbm'.  On a GNU/Linux system these
224 might be in packages with names like `compface' or `faces-xface' and
225 `netpbm' or `libgr-progs', for instance.
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   (unless gnus-article-xface-ring-internal ; Only load ring when needed.
232     (setq gnus-article-xface-ring-internal
233           (make-ring gnus-article-xface-ring-size)))
234   (save-excursion
235     (let* ((cur (current-buffer))
236            (data (buffer-substring beg end))
237            (image (cdr-safe (assoc data (ring-elements
238                                          gnus-article-xface-ring-internal)))))
239       (when (if (fboundp 'display-graphic-p)
240                 (display-graphic-p))
241         (unless image
242           (let ((coding-system-for-read 'binary)
243                 (coding-system-for-write 'binary))
244             (with-temp-buffer
245               (insert data)
246               (and (eq 0 (call-process-region (point-min) (point-max)
247                                               "uncompface"
248                                               'delete '(t nil)))
249                    (goto-char (point-min))
250                    (progn (insert "/* Width=48, Height=48 */\n") t)
251                    (eq 0 (call-process-region (point-min) (point-max)
252                                               "icontopbm"
253                                               'delete '(t nil)))
254                    (eq 0 (call-process-region (point-min) (point-max)
255                                               "pbmtoxbm"
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) 'xbm t
262                                                  :ascent 'center
263                                                  :foreground "black"
264                                                  :background "white"))
265                      (setq image (create-image (buffer-string) 'xbm t
266                                                :ascent 'center))))))
267           (ring-insert gnus-article-xface-ring-internal (cons data image))))
268       (when image
269         (goto-char (point-min))
270         (re-search-forward "^From:" nil 'move)
271         (insert-image image)))))
272
273 (defun-maybe assoc-ignore-case (key alist)
274   "Like `assoc', but assumes KEY is a string and ignores case when comparing."
275   (setq key (downcase key))
276   (let (element)
277     (while (and alist (not element))
278       (if (equal key (downcase (car (car alist))))
279           (setq element (car alist)))
280       (setq alist (cdr alist)))
281     element))
282
283 \f
284 ;;; Language support staffs.
285
286 (defvar-maybe current-language-environment "English"
287   "The language environment.")
288
289 (defvar-maybe language-info-alist nil
290   "Alist of language environment definitions.")
291
292 (defun-maybe get-language-info (lang-env key)
293   "Return information listed under KEY for language environment LANG-ENV."
294   (if (symbolp lang-env)
295       (setq lang-env (symbol-name lang-env)))
296   (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
297     (if lang-slot
298         (cdr (assq key (cdr lang-slot))))))
299
300 (defun-maybe set-language-info (lang-env key info)
301   "Modify part of the definition of language environment LANG-ENV."
302   (if (symbolp lang-env)
303       (setq lang-env (symbol-name lang-env)))
304   (let (lang-slot key-slot)
305     (setq lang-slot (assoc lang-env language-info-alist))
306     (if (null lang-slot)                ; If no slot for the language, add it.
307         (setq lang-slot (list lang-env)
308               language-info-alist (cons lang-slot language-info-alist)))
309     (setq key-slot (assq key lang-slot))
310     (if (null key-slot)                 ; If no slot for the key, add it.
311         (progn
312           (setq key-slot (list key))
313           (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
314     (setcdr key-slot info)))
315
316 (provide 'gnus-ems)
317
318 ;; Local Variables:
319 ;; byte-compile-warnings: '(redefine callargs)
320 ;; End:
321
322 ;;; gnus-ems.el ends here