c172d2c2f330a0f1fdcec6d6a4a8b13998946252
[gnus] / 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   (if (featurep 'xemacs)
76       (gnus-xmas-define)
77     (defvar gnus-mouse-face-prop 'mouse-face
78       "Property used for highlighting mouse regions.")))
79
80 (eval-when-compile
81   (defvar gnus-tmp-unread)
82   (defvar gnus-tmp-replied)
83   (defvar gnus-tmp-score-char)
84   (defvar gnus-tmp-indentation)
85   (defvar gnus-tmp-opening-bracket)
86   (defvar gnus-tmp-lines)
87   (defvar gnus-tmp-name)
88   (defvar gnus-tmp-closing-bracket)
89   (defvar gnus-tmp-subject-or-nil)
90   (defvar gnus-check-before-posting)
91   (defvar gnus-mouse-face)
92   (defvar gnus-group-buffer))
93
94 (defun gnus-ems-redefine ()
95   (cond
96    ((featurep 'xemacs)
97     (gnus-xmas-redefine))
98
99    ((featurep 'mule)
100     ;; Mule and new Emacs definitions
101
102     ;; [Note] Now there are three kinds of mule implementations,
103     ;; original MULE, XEmacs/mule and Emacs 20+ including
104     ;; MULE features.  Unfortunately these APIs are different.  In
105     ;; particular, Emacs (including original Mule) and XEmacs are
106     ;; quite different.  However, this version of Gnus doesn't support
107     ;; anything other than XEmacs 20+ and Emacs 20.3+.
108
109     ;; Predicates to check are following:
110     ;; (boundp 'MULE) is t only if Mule (original; anything older than
111     ;;                     Mule 2.3) is running.
112     ;; (featurep 'mule) is t when other mule variants are running.
113
114     ;; It is possible to detect XEmacs/mule by (featurep 'mule) and
115     ;; (featurep 'xemacs).  In this case, the implementation for
116     ;; XEmacs/mule may be shareable between XEmacs and XEmacs/mule.
117
118     (defvar gnus-summary-display-table nil
119       "Display table used in summary mode buffers.")
120     (defalias 'gnus-max-width-function 'gnus-mule-max-width-function)
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     (defun gnus-summary-line-format-spec ()
128       (insert gnus-tmp-unread gnus-tmp-replied
129               gnus-tmp-score-char gnus-tmp-indentation)
130       (put-text-property
131        (point)
132        (progn
133          (insert
134           gnus-tmp-opening-bracket
135           (format "%4d: %-20s"
136                   gnus-tmp-lines
137                   (if (> (length gnus-tmp-name) 20)
138                       (truncate-string-to-width gnus-tmp-name 20)
139                     gnus-tmp-name))
140           gnus-tmp-closing-bracket)
141          (point))
142        gnus-mouse-face-prop gnus-mouse-face)
143       (insert " " gnus-tmp-subject-or-nil "\n")))))
144
145 (defun gnus-region-active-p ()
146   "Say whether the region is active."
147   (and (boundp 'transient-mark-mode)
148        transient-mark-mode
149        (boundp 'mark-active)
150        mark-active))
151
152 (defun gnus-mark-active-p ()
153   "Non-nil means the mark and region are currently active in this buffer."
154   mark-active) ; aliased to region-exists-p in XEmacs.
155
156 (defun gnus-x-splash ()
157   "Show a splash screen using a pixmap in the current buffer."
158   (let ((dir (nnheader-find-etc-directory "gnus"))
159         pixmap file height beg i)
160     (save-excursion
161       (switch-to-buffer (gnus-get-buffer-create gnus-group-buffer))
162       (let ((buffer-read-only nil)
163             width height)
164         (erase-buffer)
165         (when (and dir
166                    (file-exists-p (setq file
167                                         (expand-file-name "x-splash" dir))))
168           (let ((coding-system-for-read 'raw-text)
169                 default-enable-multibyte-characters)
170             (with-temp-buffer
171               (insert-file-contents file)
172               (goto-char (point-min))
173               (ignore-errors
174                 (setq pixmap (read (current-buffer)))))))
175         (when pixmap
176           (make-face 'gnus-splash)
177           (setq height (/ (car pixmap) (frame-char-height))
178                 width (/ (cadr pixmap) (frame-char-width)))
179           (set-face-foreground 'gnus-splash "Brown")
180           (set-face-stipple 'gnus-splash pixmap)
181           (insert-char ?\n (* (/ (window-height) 2 height) height))
182           (setq i height)
183           (while (> i 0)
184             (insert-char ?\  (* (/ (window-width) 2 width) width))
185             (setq beg (point))
186             (insert-char ?\  width)
187             (set-text-properties beg (point) '(face gnus-splash))
188             (insert ?\n)
189             (decf i))
190           (goto-char (point-min))
191           (sit-for 0))))))
192
193 ;;; Image functions.
194
195 (defun gnus-image-type-available-p (type)
196   (and (fboundp 'image-type-available-p)
197        (image-type-available-p type)))
198
199 (defun gnus-create-image (file &optional type data-p &rest props)
200   (let ((face (plist-get props :face)))
201     (when face
202       (setq props (plist-put props :foreground (face-foreground face)))
203       (setq props (plist-put props :background (face-background face))))
204     (apply 'create-image file type data-p props)))
205
206 (defun gnus-put-image (glyph &optional string category)
207   (let ((point (point)))
208     (insert-image glyph (or string " "))
209     (put-text-property point (point) 'gnus-image-category category)
210     (unless string
211       (put-text-property (1- (point)) (point)
212                          'gnus-image-text-deletable t))
213     glyph))
214
215 (defun gnus-remove-image (image &optional category)
216   (dolist (position (message-text-with-property 'display))
217     (when (and (equal (get-text-property position 'display) image)
218                (equal (get-text-property position 'gnus-image-category)
219                       category))
220       (put-text-property position (1+ position) 'display nil)
221       (when (get-text-property position 'gnus-image-text-deletable)
222         (delete-region position (1+ position))))))
223
224 (provide 'gnus-ems)
225
226 ;;; gnus-ems.el ends here