fb4107e657d43f79c020ec053b1272eef238b84f
[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
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
50 (autoload 'smiley-region "smiley")
51
52 (defun gnus-kill-all-overlays ()
53   "Delete all overlays in the current buffer."
54   (let* ((overlayss (overlay-lists))
55          (buffer-read-only nil)
56          (overlays (delq nil (nconc (car overlayss) (cdr overlayss)))))
57     (while overlays
58       (delete-overlay (pop overlays)))))
59
60 ;;; Mule functions.
61
62 (defun gnus-mule-max-width-function (el max-width)
63   `(let* ((val (eval (, el)))
64           (valstr (if (numberp val)
65                       (int-to-string val) val)))
66      (if (> (length valstr) ,max-width)
67          (truncate-string-to-width valstr ,max-width)
68        valstr)))
69
70 (eval-and-compile
71   (defalias 'gnus-char-width
72     (if (fboundp 'char-width)
73         'char-width
74       (lambda (ch) 1)))) ;; A simple hack.
75
76 (eval-and-compile
77   (if (featurep 'xemacs)
78       (gnus-xmas-define)
79     (defvar gnus-mouse-face-prop 'mouse-face
80       "Property used for highlighting mouse regions.")))
81
82 (defvar gnus-tmp-unread)
83 (defvar gnus-tmp-replied)
84 (defvar gnus-tmp-score-char)
85 (defvar gnus-tmp-indentation)
86 (defvar gnus-tmp-opening-bracket)
87 (defvar gnus-tmp-lines)
88 (defvar gnus-tmp-name)
89 (defvar gnus-tmp-closing-bracket)
90 (defvar gnus-tmp-subject-or-nil)
91 (defvar gnus-check-before-posting)
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 three kinds of mule implementations,
102     ;; original MULE, XEmacs/mule and Emacs 20+ including
103     ;; MULE features.  Unfortunately these API are different.  In
104     ;; particular, Emacs (including original MULE) and XEmacs are
105     ;; quite different.  However, this version of Gnus doesn't support
106     ;; anything other than XEmacs 20+ and Emacs 20.3+.
107
108     ;; Predicates to check are following:
109     ;; (boundp 'MULE) is t only if MULE (original; anything older than
110     ;;                     Mule 2.3) is running.
111     ;; (featurep 'mule) is t when every mule variants are running.
112
113     ;; It is possible to detect XEmacs/mule by (featurep 'mule) and
114     ;; checking `emacs-version'.  In this case, the implementation for
115     ;; XEmacs/mule may be shareable between XEmacs and XEmacs/mule.
116
117     (defvar gnus-summary-display-table nil
118       "Display table used in summary mode buffers.")
119     (defalias 'gnus-max-width-function 'gnus-mule-max-width-function)
120
121     (when (boundp 'gnus-check-before-posting)
122       (setq gnus-check-before-posting
123             (delq 'long-lines
124                   (delq 'control-chars gnus-check-before-posting))))
125
126     (defun gnus-summary-line-format-spec ()
127       (insert gnus-tmp-unread gnus-tmp-replied
128               gnus-tmp-score-char gnus-tmp-indentation)
129       (put-text-property
130        (point)
131        (progn
132          (insert
133           gnus-tmp-opening-bracket
134           (format "%4d: %-20s"
135                   gnus-tmp-lines
136                   (if (> (length gnus-tmp-name) 20)
137                       (truncate-string-to-width gnus-tmp-name 20)
138                     gnus-tmp-name))
139           gnus-tmp-closing-bracket)
140          (point))
141        gnus-mouse-face-prop gnus-mouse-face)
142       (insert " " gnus-tmp-subject-or-nil "\n")))))
143
144 (defun gnus-region-active-p ()
145   "Say whether the region is active."
146   (and (boundp 'transient-mark-mode)
147        transient-mark-mode
148        (boundp 'mark-active)
149        mark-active))
150
151 (if (fboundp 'add-minor-mode)
152     (defalias 'gnus-add-minor-mode 'add-minor-mode)
153   (defun gnus-add-minor-mode (mode name map &rest rest)
154     (set (make-local-variable mode) t)
155     (unless (assq mode minor-mode-alist)
156       (push `(,mode ,name) minor-mode-alist))
157     (unless (assq mode minor-mode-map-alist)
158       (push (cons mode map)
159             minor-mode-map-alist))))
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 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)
212   (insert-image glyph (or string " "))
213   (unless string
214     (put-text-property (1- (point)) (point)
215                        'gnus-image-text-deletable t))
216   glyph)
217
218 (defun gnus-remove-image (image)
219   (dolist (position (message-text-with-property 'display))
220     (when (equal (get-text-property position 'display) image)
221       (put-text-property position (1+ position) 'display nil)
222       (when (get-text-property position 'gnus-image-text-deletable)
223         (delete-region position (1+ position))))))
224
225 (provide 'gnus-ems)
226
227 ;;; gnus-ems.el ends here