T-gnus 6.15.7 revision 00.
[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, 2001, 2002
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
31   (require 'cl)
32   (require 'ring))
33
34 ;;; Function aliases later to be redefined for XEmacs usage.
35
36 (defvar gnus-mouse-2 [mouse-2])
37 (defvar gnus-down-mouse-3 [down-mouse-3])
38 (defvar gnus-down-mouse-2 [down-mouse-2])
39 (defvar gnus-widget-button-keymap nil)
40 (defvar gnus-mode-line-modified
41   (if (or (featurep 'xemacs)
42           (< emacs-major-version 20))
43       '("--**-" . "-----")
44     '("**" "--")))
45
46 (eval-and-compile
47   (autoload 'gnus-xmas-define "gnus-xmas")
48   (autoload 'gnus-xmas-redefine "gnus-xmas")
49   (autoload 'appt-select-lowest-window "appt"))
50
51 (if (or (featurep 'xemacs)
52         (>= emacs-major-version 21))
53     (autoload 'smiley-region "smiley")
54   (autoload 'smiley-region "smiley-mule"))
55
56 (defun gnus-kill-all-overlays ()
57   "Delete all overlays in the current buffer."
58   (let* ((overlayss (overlay-lists))
59          (buffer-read-only nil)
60          (overlays (delq nil (nconc (car overlayss) (cdr overlayss)))))
61     (while overlays
62       (delete-overlay (pop overlays)))))
63
64 ;;; Mule functions.
65
66 (eval-and-compile
67   (defalias 'gnus-char-width
68     (if (fboundp 'char-width)
69         'char-width
70       (lambda (ch) 1)))) ;; A simple hack.
71
72 (eval-and-compile
73   (if (featurep 'xemacs)
74       (gnus-xmas-define)
75     (defvar gnus-mouse-face-prop 'mouse-face
76       "Property used for highlighting mouse regions.")))
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 (defvar gnus-check-before-posting)
88
89 (defun gnus-ems-redefine ()
90   (cond
91    ((featurep 'xemacs)
92     (gnus-xmas-redefine))
93
94    ((featurep 'mule)
95     ;; Mule and new Emacs definitions
96
97     ;; [Note] Now there are three kinds of mule implementations,
98     ;; original MULE, XEmacs/mule and Emacs 20+ including
99     ;; MULE features.  Unfortunately these API are different.  In
100     ;; particular, Emacs (including original MULE) and XEmacs are
101     ;; quite different.  However, this version of Gnus doesn't support
102     ;; anything other than XEmacs 20+ and Emacs 20.3+.
103
104     ;; Predicates to check are following:
105     ;; (boundp 'MULE) is t only if MULE (original; anything older than
106     ;;                     Mule 2.3) is running.
107     ;; (featurep 'mule) is t when every mule variants are running.
108
109     ;; It is possible to detect XEmacs/mule by (featurep 'mule) and
110     ;; checking `emacs-version'.  In this case, the implementation for
111     ;; XEmacs/mule may be shareable between XEmacs and XEmacs/mule.
112
113     (defvar gnus-summary-display-table nil
114       "Display table used in summary mode buffers.")
115
116     (defalias 'gnus-summary-set-display-table (lambda ()))
117
118     (if (fboundp 'truncate-string-to-width)
119         (fset 'gnus-truncate-string 'truncate-string-to-width)
120       (fset 'gnus-truncate-string 'truncate-string))
121
122     (when (boundp 'gnus-check-before-posting)
123       (setq gnus-check-before-posting
124             (delq 'long-lines
125                   (delq 'control-chars gnus-check-before-posting))))
126     ))
127   (when (featurep 'mule)
128     (defun gnus-tilde-max-form (el max-width)
129       "Return a form that limits EL to MAX-WIDTH."
130       (let ((max (abs max-width)))
131         (if (symbolp el)
132             (if (< max-width 0)
133                 `(let ((width (string-width ,el)))
134                    (gnus-truncate-string ,el width (- width ,max)))
135               `(gnus-truncate-string ,el ,max))
136           (if (< max-width 0)
137               `(let* ((val (eval ,el))
138                       (width (string-width val)))
139                  (gnus-truncate-string val width (- width ,max)))
140             `(let ((val (eval ,el)))
141                (gnus-truncate-string val ,max))))))
142
143     (defun gnus-tilde-cut-form (el cut-width)
144       "Return a form that cuts CUT-WIDTH off of EL."
145       (let ((cut (abs cut-width)))
146         (if (symbolp el)
147             (if (< cut-width 0)
148                 `(gnus-truncate-string ,el (- (string-width ,el) ,cut))
149               `(gnus-truncate-string ,el (string-width ,el) ,cut))
150           (if (< cut-width 0)
151               `(let ((val (eval ,el)))
152                  (gnus-truncate-string val (- (string-width val) ,cut)))
153             `(let ((val (eval ,el)))
154                (gnus-truncate-string val (string-width val) ,cut))))))
155     ))
156
157 (defun gnus-region-active-p ()
158   "Say whether the region is active."
159   (and (boundp 'transient-mark-mode)
160        transient-mark-mode
161        (boundp 'mark-active)
162        mark-active))
163
164 (if (fboundp 'add-minor-mode)
165     (defalias 'gnus-add-minor-mode 'add-minor-mode)
166   (defun gnus-add-minor-mode (mode name map &rest rest)
167     (set (make-local-variable mode) t)
168     (unless (assq mode minor-mode-alist)
169       (push `(,mode ,name) minor-mode-alist))
170     (unless (assq mode minor-mode-map-alist)
171       (push (cons mode map)
172             minor-mode-map-alist))))
173
174 (defun gnus-x-splash ()
175   "Show a splash screen using a pixmap in the current buffer."
176   (let ((dir (nnheader-find-etc-directory "gnus"))
177         pixmap file height beg i)
178     (save-excursion
179       (switch-to-buffer (gnus-get-buffer-create gnus-group-buffer))
180       (let ((buffer-read-only nil)
181             width height)
182         (erase-buffer)
183         (when (and dir
184                    (file-exists-p (setq file
185                                         (expand-file-name "x-splash" dir))))
186           (with-temp-buffer
187             (insert-file-contents-as-binary file)
188             (goto-char (point-min))
189             (ignore-errors
190               (setq pixmap (read (current-buffer))))))
191         (when pixmap
192           (make-face 'gnus-splash)
193           (setq height (/ (car pixmap) (frame-char-height))
194                 width (/ (cadr pixmap) (frame-char-width)))
195           (set-face-foreground 'gnus-splash "Brown")
196           (set-face-stipple 'gnus-splash pixmap)
197           (insert-char ?\n (* (/ (window-height) 2 height) height))
198           (setq i height)
199           (while (> i 0)
200             (insert-char ?\  (* (/ (window-width) 2 width) width))
201             (setq beg (point))
202             (insert-char ?\  width)
203             (set-text-properties beg (point) '(face gnus-splash))
204             (insert ?\n)
205             (decf i))
206           (goto-char (point-min))
207           (sit-for 0))))))
208
209 ;;; Image functions.
210
211 (defun gnus-image-type-available-p (type)
212   (and (fboundp 'image-type-available-p)
213        (image-type-available-p type)))
214
215 (defun gnus-create-image (file &optional type data-p &rest props)
216   (let ((face (plist-get props :face)))
217     (when face
218       (setq props (plist-put props :foreground (face-foreground face)))
219       (setq props (plist-put props :background (face-background face))))
220     (apply 'create-image file type data-p props)))
221
222 (defun gnus-put-image (glyph &optional string)
223   (insert-image glyph (or string " "))
224   (unless string
225     (put-text-property (1- (point)) (point)
226                        'gnus-image-text-deletable t))
227   glyph)
228
229 (defun gnus-remove-image (image)
230   (dolist (position (message-text-with-property 'display))
231     (when (equal (get-text-property position 'display) image)
232       (put-text-property position (1+ position) 'display nil)
233       (when (get-text-property position 'gnus-image-text-deletable)
234         (delete-region position (1+ position))))))
235
236 (defun-maybe assoc-ignore-case (key alist)
237   "Like `assoc', but assumes KEY is a string and ignores case when comparing."
238   (setq key (downcase key))
239   (let (element)
240     (while (and alist (not element))
241       (if (equal key (downcase (car (car alist))))
242           (setq element (car alist)))
243       (setq alist (cdr alist)))
244     element))
245
246 \f
247 ;;; Language support staffs.
248
249 (defvar-maybe current-language-environment "English"
250   "The language environment.")
251
252 (defvar-maybe language-info-alist nil
253   "Alist of language environment definitions.")
254
255 (defun-maybe get-language-info (lang-env key)
256   "Return information listed under KEY for language environment LANG-ENV."
257   (if (symbolp lang-env)
258       (setq lang-env (symbol-name lang-env)))
259   (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
260     (if lang-slot
261         (cdr (assq key (cdr lang-slot))))))
262
263 (defun-maybe set-language-info (lang-env key info)
264   "Modify part of the definition of language environment LANG-ENV."
265   (if (symbolp lang-env)
266       (setq lang-env (symbol-name lang-env)))
267   (let (lang-slot key-slot)
268     (setq lang-slot (assoc lang-env language-info-alist))
269     (if (null lang-slot)                ; If no slot for the language, add it.
270         (setq lang-slot (list lang-env)
271               language-info-alist (cons lang-slot language-info-alist)))
272     (setq key-slot (assq key lang-slot))
273     (if (null key-slot)                 ; If no slot for the key, add it.
274         (progn
275           (setq key-slot (list key))
276           (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
277     (setcdr key-slot info)))
278
279 (provide 'gnus-ems)
280
281 ;;; gnus-ems.el ends here