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