T-gnus 6.15.18 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, 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 (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 (defun gnus-mark-active-p ()
165   "Non-nil means the mark and region are currently active in this buffer."
166   mark-active) ; aliased to region-exists-p in XEmacs.
167
168 (if (fboundp 'add-minor-mode)
169     (defalias 'gnus-add-minor-mode 'add-minor-mode)
170   (defun gnus-add-minor-mode (mode name map &rest rest)
171     (set (make-local-variable mode) t)
172     (unless (assq mode minor-mode-alist)
173       (push `(,mode ,name) minor-mode-alist))
174     (unless (assq mode minor-mode-map-alist)
175       (push (cons mode map)
176             minor-mode-map-alist))))
177
178 (defun gnus-x-splash ()
179   "Show a splash screen using a pixmap in the current buffer."
180   (let ((dir (nnheader-find-etc-directory "gnus"))
181         pixmap file height beg i)
182     (save-excursion
183       (switch-to-buffer (gnus-get-buffer-create gnus-group-buffer))
184       (let ((buffer-read-only nil)
185             width height)
186         (erase-buffer)
187         (when (and dir
188                    (file-exists-p (setq file
189                                         (expand-file-name "x-splash" dir))))
190           (let ((coding-system-for-read 'raw-text)
191                 default-enable-multibyte-characters)
192             (with-temp-buffer
193               (insert-file-contents-as-binary file)
194               (goto-char (point-min))
195               (ignore-errors
196                 (setq pixmap (read (current-buffer)))))))
197         (when pixmap
198           (make-face 'gnus-splash)
199           (setq height (/ (car pixmap) (frame-char-height))
200                 width (/ (cadr pixmap) (frame-char-width)))
201           (set-face-foreground 'gnus-splash "Brown")
202           (set-face-stipple 'gnus-splash pixmap)
203           (insert-char ?\n (* (/ (window-height) 2 height) height))
204           (setq i height)
205           (while (> i 0)
206             (insert-char ?\  (* (/ (window-width) 2 width) width))
207             (setq beg (point))
208             (insert-char ?\  width)
209             (set-text-properties beg (point) '(face gnus-splash))
210             (insert ?\n)
211             (decf i))
212           (goto-char (point-min))
213           (sit-for 0))))))
214
215 ;;; Image functions.
216
217 (defun gnus-image-type-available-p (type)
218   (and (fboundp 'image-type-available-p)
219        (image-type-available-p type)))
220
221 (defun gnus-create-image (file &optional type data-p &rest props)
222   (let ((face (plist-get props :face)))
223     (when face
224       (setq props (plist-put props :foreground (face-foreground face)))
225       (setq props (plist-put props :background (face-background face))))
226     (apply 'create-image file type data-p props)))
227
228 (defun gnus-put-image (glyph &optional string)
229   (insert-image glyph (or string " "))
230   (unless string
231     (put-text-property (1- (point)) (point)
232                        'gnus-image-text-deletable t))
233   glyph)
234
235 (defun gnus-remove-image (image)
236   (dolist (position (message-text-with-property 'display))
237     (when (equal (get-text-property position 'display) image)
238       (put-text-property position (1+ position) 'display nil)
239       (when (get-text-property position 'gnus-image-text-deletable)
240         (delete-region position (1+ position))))))
241
242 (defun-maybe assoc-ignore-case (key alist)
243   "Like `assoc', but assumes KEY is a string and ignores case when comparing."
244   (setq key (downcase key))
245   (let (element)
246     (while (and alist (not element))
247       (if (equal key (downcase (car (car alist))))
248           (setq element (car alist)))
249       (setq alist (cdr alist)))
250     element))
251
252 \f
253 ;;; Language support staffs.
254
255 (defvar-maybe current-language-environment "English"
256   "The language environment.")
257
258 (defvar-maybe language-info-alist nil
259   "Alist of language environment definitions.")
260
261 (defun-maybe get-language-info (lang-env key)
262   "Return information listed under KEY for language environment LANG-ENV."
263   (if (symbolp lang-env)
264       (setq lang-env (symbol-name lang-env)))
265   (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
266     (if lang-slot
267         (cdr (assq key (cdr lang-slot))))))
268
269 (defun-maybe set-language-info (lang-env key info)
270   "Modify part of the definition of language environment LANG-ENV."
271   (if (symbolp lang-env)
272       (setq lang-env (symbol-name lang-env)))
273   (let (lang-slot key-slot)
274     (setq lang-slot (assoc lang-env language-info-alist))
275     (if (null lang-slot)                ; If no slot for the language, add it.
276         (setq lang-slot (list lang-env)
277               language-info-alist (cons lang-slot language-info-alist)))
278     (setq key-slot (assq key lang-slot))
279     (if (null key-slot)                 ; If no slot for the key, add it.
280         (progn
281           (setq key-slot (list key))
282           (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
283     (setcdr key-slot info)))
284
285 (provide 'gnus-ems)
286
287 ;;; gnus-ems.el ends here