04f63cbd4e784273db9b03e729a769f35c3b0301
[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         (erase-buffer)
179         (when (and dir
180                    (file-exists-p (setq file
181                                         (expand-file-name "x-splash" dir))))
182           (with-temp-buffer
183             (insert-file-contents-as-binary file)
184             (goto-char (point-min))
185             (ignore-errors
186               (setq pixmap (read (current-buffer))))))
187         (when pixmap
188           (make-face 'gnus-splash)
189           (setq height (/ (car pixmap) (frame-char-height))
190                 width (/ (cadr pixmap) (frame-char-width)))
191           (set-face-foreground 'gnus-splash "Brown")
192           (set-face-stipple 'gnus-splash pixmap)
193           (insert-char ?\n (* (/ (window-height) 2 height) height))
194           (setq i height)
195           (while (> i 0)
196             (insert-char ?\  (* (/ (window-width) 2 width) width))
197             (setq beg (point))
198             (insert-char ?\  width)
199             (set-text-properties beg (point) '(face gnus-splash))
200             (insert ?\n)
201             (decf i))
202           (goto-char (point-min))
203           (sit-for 0))))))
204
205 (defvar gnus-article-xface-ring-internal nil
206   "Cache for face data.")
207
208 ;; Worth customizing?
209 (defvar gnus-article-xface-ring-size 6
210   "Length of the ring used for `gnus-article-xface-ring-internal'.")
211
212 (defvar gnus-article-compface-xbm
213   (when (and (not (featurep 'xemacs)) (>= emacs-major-version 21))
214     (eq 0 (string-match "#define" (shell-command-to-string "uncompface -X"))))
215   "Non-nil means the compface program supports the -X option.
216 That produces XBM output.")
217
218 (defun gnus-article-display-xface (beg end)
219   "Display an XFace header from between BEG and END in the current article.
220 Requires support for images in your Emacs and the external programs
221 `uncompface', and `icontopbm'.  On a GNU/Linux system these
222 might be in packages with names like `compface' or `faces-xface' and
223 `netpbm' or `libgr-progs', for instance.  See also
224 `gnus-article-compface-xbm'.
225
226 This function is for Emacs 21+.  See `gnus-xmas-article-display-xface'
227 for XEmacs."
228   ;; It might be worth converting uncompface's output in Lisp.
229
230   (when (if (fboundp 'display-graphic-p)
231             (display-graphic-p))
232     (unless gnus-article-xface-ring-internal ; Only load ring when needed.
233       (setq gnus-article-xface-ring-internal
234             (make-ring gnus-article-xface-ring-size)))
235     (save-excursion
236       (let* ((cur (current-buffer))
237              (data (buffer-substring beg end))
238              (image (cdr-safe (assoc data (ring-elements
239                                            gnus-article-xface-ring-internal))))
240              default-enable-multibyte-characters)
241         (unless image
242           (with-temp-buffer
243             (insert data)
244             (and (eq 0 (apply #'call-process-region (point-min) (point-max)
245                               "uncompface"
246                               'delete '(t nil) nil
247                               (if gnus-article-compface-xbm
248                                   '("-X"))))
249                  (unless gnus-article-compface-xbm
250                    (goto-char (point-min))
251                    (progn (insert "/* Width=48, Height=48 */\n") t)
252                    (eq 0 (call-process-region (point-min) (point-max)
253                                               "icontopbm"
254                                               'delete '(t nil))))
255                  ;; Miles Bader says that faces don't look right as
256                  ;; light on dark.
257                  (if (eq 'dark (cdr-safe (assq 'background-mode
258                                                (frame-parameters))))
259                      (setq image (create-image (buffer-string) 'pbm t
260                                                :ascent 'center
261                                                :foreground "black"
262                                                :background "white"))
263                    (setq image (create-image (buffer-string) 'pbm t
264                                              :ascent 'center)))))
265           (ring-insert gnus-article-xface-ring-internal (cons data image)))
266         (when image
267           (goto-char (point-min))
268           (re-search-forward "^From:" nil 'move)
269           (insert-image image))))))
270
271 (defun-maybe assoc-ignore-case (key alist)
272   "Like `assoc', but assumes KEY is a string and ignores case when comparing."
273   (setq key (downcase key))
274   (let (element)
275     (while (and alist (not element))
276       (if (equal key (downcase (car (car alist))))
277           (setq element (car alist)))
278       (setq alist (cdr alist)))
279     element))
280
281 \f
282 ;;; Language support staffs.
283
284 (defvar-maybe current-language-environment "English"
285   "The language environment.")
286
287 (defvar-maybe language-info-alist nil
288   "Alist of language environment definitions.")
289
290 (defun-maybe get-language-info (lang-env key)
291   "Return information listed under KEY for language environment LANG-ENV."
292   (if (symbolp lang-env)
293       (setq lang-env (symbol-name lang-env)))
294   (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
295     (if lang-slot
296         (cdr (assq key (cdr lang-slot))))))
297
298 (defun-maybe set-language-info (lang-env key info)
299   "Modify part of the definition of language environment LANG-ENV."
300   (if (symbolp lang-env)
301       (setq lang-env (symbol-name lang-env)))
302   (let (lang-slot key-slot)
303     (setq lang-slot (assoc lang-env language-info-alist))
304     (if (null lang-slot)                ; If no slot for the language, add it.
305         (setq lang-slot (list lang-env)
306               language-info-alist (cons lang-slot language-info-alist)))
307     (setq key-slot (assq key lang-slot))
308     (if (null key-slot)                 ; If no slot for the key, add it.
309         (progn
310           (setq key-slot (list key))
311           (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
312     (setcdr key-slot info)))
313
314 (provide 'gnus-ems)
315
316 ;; Local Variables:
317 ;; byte-compile-warnings: '(redefine callargs)
318 ;; End:
319
320 ;;; gnus-ems.el ends here