2004-01-06 Steve Youngs <sryoungs@bigpond.net.au>
[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 (featurep 'xemacs)
41       '("--**-" . "-----")
42     '("**" "--")))
43
44 (eval-and-compile
45   (autoload 'gnus-xmas-define "gnus-xmas")
46   (autoload 'gnus-xmas-redefine "gnus-xmas")
47   (autoload 'appt-select-lowest-window "appt")
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 (defun gnus-mule-max-width-function (el max-width)
66   `(let* ((val (eval (, el)))
67           (valstr (if (numberp val)
68                       (int-to-string val) val)))
69      (if (> (length valstr) ,max-width)
70          (truncate-string-to-width valstr ,max-width)
71        valstr)))
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 three kinds of mule implementations,
102     ;; original MULE, XEmacs/mule and Emacs 20+ including
103     ;; MULE features.  Unfortunately these APIs 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 other mule variants are running.
112
113     ;; It is possible to detect XEmacs/mule by (featurep 'mule) and
114     ;; (featurep 'xemacs).  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 (defun gnus-mark-active-p ()
152   "Non-nil means the mark and region are currently active in this buffer."
153   mark-active) ; aliased to region-exists-p in XEmacs.
154
155 (defun gnus-x-splash ()
156   "Show a splash screen using a pixmap in the current buffer."
157   (let ((dir (nnheader-find-etc-directory "gnus"))
158         pixmap file height beg i)
159     (save-excursion
160       (switch-to-buffer (gnus-get-buffer-create gnus-group-buffer))
161       (let ((buffer-read-only nil)
162             width height)
163         (erase-buffer)
164         (when (and dir
165                    (file-exists-p (setq file
166                                         (expand-file-name "x-splash" dir))))
167           (let ((coding-system-for-read 'raw-text)
168                 default-enable-multibyte-characters)
169             (with-temp-buffer
170               (insert-file-contents file)
171               (goto-char (point-min))
172               (ignore-errors
173                 (setq pixmap (read (current-buffer)))))))
174         (when pixmap
175           (make-face 'gnus-splash)
176           (setq height (/ (car pixmap) (frame-char-height))
177                 width (/ (cadr pixmap) (frame-char-width)))
178           (set-face-foreground 'gnus-splash "Brown")
179           (set-face-stipple 'gnus-splash pixmap)
180           (insert-char ?\n (* (/ (window-height) 2 height) height))
181           (setq i height)
182           (while (> i 0)
183             (insert-char ?\  (* (/ (window-width) 2 width) width))
184             (setq beg (point))
185             (insert-char ?\  width)
186             (set-text-properties beg (point) '(face gnus-splash))
187             (insert ?\n)
188             (decf i))
189           (goto-char (point-min))
190           (sit-for 0))))))
191
192 ;;; Image functions.
193
194 (defun gnus-image-type-available-p (type)
195   (and (fboundp 'image-type-available-p)
196        (image-type-available-p type)))
197
198 (defun gnus-create-image (file &optional type data-p &rest props)
199   (let ((face (plist-get props :face)))
200     (when face
201       (setq props (plist-put props :foreground (face-foreground face)))
202       (setq props (plist-put props :background (face-background face))))
203     (apply 'create-image file type data-p props)))
204
205 (defun gnus-put-image (glyph &optional string category)
206   (let ((point (point)))
207     (insert-image glyph (or string " "))
208     (put-text-property point (point) 'gnus-image-category category)
209     (unless string
210       (put-text-property (1- (point)) (point)
211                          'gnus-image-text-deletable t))
212     glyph))
213
214 (defun gnus-remove-image (image &optional category)
215   (dolist (position (message-text-with-property 'display))
216     (when (and (equal (get-text-property position 'display) image)
217                (equal (get-text-property position 'gnus-image-category)
218                       category))
219       (put-text-property position (1+ position) 'display nil)
220       (when (get-text-property position 'gnus-image-text-deletable)
221         (delete-region position (1+ position))))))
222
223 (provide 'gnus-ems)
224
225 ;;; gnus-ems.el ends here