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