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