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