Import Gnus v5.10.2.
[elisp/gnus.git-] / lisp / gnus-ems.el
1 ;;; gnus-ems.el --- functions for making 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 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (require 'ring))
32
33 ;;; Function aliases later to be redefined for XEmacs usage.
34
35 (defvar gnus-mouse-2 [mouse-2])
36 (defvar gnus-down-mouse-3 [down-mouse-3])
37 (defvar gnus-down-mouse-2 [down-mouse-2])
38 (defvar gnus-widget-button-keymap nil)
39 (defvar gnus-mode-line-modified
40   (if (or (featurep 'xemacs)
41           (< emacs-major-version 20))
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 (defun gnus-mule-max-width-function (el max-width)
67   `(let* ((val (eval (, el)))
68           (valstr (if (numberp val)
69                       (int-to-string val) val)))
70      (if (> (length valstr) ,max-width)
71          (truncate-string-to-width valstr ,max-width)
72        valstr)))
73
74 (eval-and-compile
75   (defalias 'gnus-char-width
76     (if (fboundp 'char-width)
77         'char-width
78       (lambda (ch) 1)))) ;; A simple hack.
79
80 (eval-and-compile
81   (if (featurep 'xemacs)
82       (gnus-xmas-define)
83     (defvar gnus-mouse-face-prop 'mouse-face
84       "Property used for highlighting mouse regions.")))
85
86 (eval-when-compile
87   (defvar gnus-tmp-unread)
88   (defvar gnus-tmp-replied)
89   (defvar gnus-tmp-score-char)
90   (defvar gnus-tmp-indentation)
91   (defvar gnus-tmp-opening-bracket)
92   (defvar gnus-tmp-lines)
93   (defvar gnus-tmp-name)
94   (defvar gnus-tmp-closing-bracket)
95   (defvar gnus-tmp-subject-or-nil)
96   (defvar gnus-check-before-posting)
97   (defvar gnus-mouse-face)
98   (defvar gnus-group-buffer))
99
100 (defun gnus-ems-redefine ()
101   (cond
102    ((featurep 'xemacs)
103     (gnus-xmas-redefine))
104
105    ((featurep 'mule)
106     ;; Mule and new Emacs definitions
107
108     ;; [Note] Now there are three kinds of mule implementations,
109     ;; original MULE, XEmacs/mule and Emacs 20+ including
110     ;; MULE features.  Unfortunately these APIs are different.  In
111     ;; particular, Emacs (including original Mule) and XEmacs are
112     ;; quite different.  However, this version of Gnus doesn't support
113     ;; anything other than XEmacs 20+ and Emacs 20.3+.
114
115     ;; Predicates to check are following:
116     ;; (boundp 'MULE) is t only if Mule (original; anything older than
117     ;;                     Mule 2.3) is running.
118     ;; (featurep 'mule) is t when other mule variants are running.
119
120     ;; It is possible to detect XEmacs/mule by (featurep 'mule) and
121     ;; (featurep 'xemacs).  In this case, the implementation for
122     ;; XEmacs/mule may be shareable between XEmacs and XEmacs/mule.
123
124     (defvar gnus-summary-display-table nil
125       "Display table used in summary mode buffers.")
126     (defalias 'gnus-max-width-function 'gnus-mule-max-width-function)
127
128     (when (boundp 'gnus-check-before-posting)
129       (setq gnus-check-before-posting
130             (delq 'long-lines
131                   (delq 'control-chars gnus-check-before-posting))))
132
133     (defun gnus-summary-line-format-spec ()
134       (insert gnus-tmp-unread gnus-tmp-replied
135               gnus-tmp-score-char gnus-tmp-indentation)
136       (put-text-property
137        (point)
138        (progn
139          (insert
140           gnus-tmp-opening-bracket
141           (format "%4d: %-20s"
142                   gnus-tmp-lines
143                   (if (> (length gnus-tmp-name) 20)
144                       (truncate-string-to-width gnus-tmp-name 20)
145                     gnus-tmp-name))
146           gnus-tmp-closing-bracket)
147          (point))
148        gnus-mouse-face-prop gnus-mouse-face)
149       (insert " " gnus-tmp-subject-or-nil "\n")))))
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 (if (fboundp 'add-minor-mode)
163     (defalias 'gnus-add-minor-mode 'add-minor-mode)
164   (defun gnus-add-minor-mode (mode name map &rest rest)
165     (set (make-local-variable mode) t)
166     (unless (assq mode minor-mode-alist)
167       (push `(,mode ,name) minor-mode-alist))
168     (unless (assq mode minor-mode-map-alist)
169       (push (cons mode map)
170             minor-mode-map-alist))))
171
172 (defun gnus-x-splash ()
173   "Show a splash screen using a pixmap in the current buffer."
174   (let ((dir (nnheader-find-etc-directory "gnus"))
175         pixmap file height beg i)
176     (save-excursion
177       (switch-to-buffer (gnus-get-buffer-create gnus-group-buffer))
178       (let ((buffer-read-only nil)
179             width height)
180         (erase-buffer)
181         (when (and dir
182                    (file-exists-p (setq file
183                                         (expand-file-name "x-splash" dir))))
184           (let ((coding-system-for-read 'raw-text)
185                 default-enable-multibyte-characters)
186             (with-temp-buffer
187               (insert-file-contents 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 (provide 'gnus-ems)
237
238 ;;; gnus-ems.el ends here