* gnus-ems.el (gnus-x-splash): Make it work.
[gnus] / lisp / gnus-ems.el
1 ;;; gnus-ems.el --- functions for making Gnus work under different Emacsen
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;;   2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile
31   (require 'cl)
32   (require 'ring))
33
34 ;;; Function aliases later to be redefined for XEmacs usage.
35
36 (defvar gnus-mouse-2 [mouse-2])
37 (defvar gnus-down-mouse-3 [down-mouse-3])
38 (defvar gnus-down-mouse-2 [down-mouse-2])
39 (defvar gnus-widget-button-keymap nil)
40 (defvar gnus-mode-line-modified
41   (if (featurep 'xemacs)
42       '("--**-" . "-----")
43     '("**" "--")))
44
45 (eval-and-compile
46   (autoload 'gnus-xmas-define "gnus-xmas")
47   (autoload 'gnus-xmas-redefine "gnus-xmas")
48   (autoload 'gnus-get-buffer-create "gnus")
49   (autoload 'nnheader-find-etc-directory "nnheader"))
50
51 (autoload 'smiley-region "smiley")
52
53 (defun gnus-kill-all-overlays ()
54   "Delete all overlays in the current buffer."
55   (let* ((overlayss (overlay-lists))
56          (buffer-read-only nil)
57          (overlays (delq nil (nconc (car overlayss) (cdr overlayss)))))
58     (while overlays
59       (delete-overlay (pop overlays)))))
60
61 ;;; Mule functions.
62
63 (defun gnus-mule-max-width-function (el max-width)
64   `(let* ((val (eval (, el)))
65           (valstr (if (numberp val)
66                       (int-to-string val) val)))
67      (if (> (length valstr) ,max-width)
68          (truncate-string-to-width valstr ,max-width)
69        valstr)))
70
71 (eval-and-compile
72   (if (featurep 'xemacs)
73       (gnus-xmas-define)
74     (defvar gnus-mouse-face-prop 'mouse-face
75       "Property used for highlighting mouse regions.")))
76
77 (eval-when-compile
78   (defvar gnus-tmp-unread)
79   (defvar gnus-tmp-replied)
80   (defvar gnus-tmp-score-char)
81   (defvar gnus-tmp-indentation)
82   (defvar gnus-tmp-opening-bracket)
83   (defvar gnus-tmp-lines)
84   (defvar gnus-tmp-name)
85   (defvar gnus-tmp-closing-bracket)
86   (defvar gnus-tmp-subject-or-nil)
87   (defvar gnus-check-before-posting)
88   (defvar gnus-mouse-face)
89   (defvar gnus-group-buffer))
90
91 (defun gnus-ems-redefine ()
92   (cond
93    ((featurep 'xemacs)
94     (gnus-xmas-redefine))
95
96    ((featurep 'mule)
97     ;; Mule and new Emacs definitions
98
99     ;; [Note] Now there are three kinds of mule implementations,
100     ;; original MULE, XEmacs/mule and Emacs 20+ including
101     ;; MULE features.  Unfortunately these APIs are different.  In
102     ;; particular, Emacs (including original Mule) and XEmacs are
103     ;; quite different.  However, this version of Gnus doesn't support
104     ;; anything other than XEmacs 20+ and Emacs 20.3+.
105
106     ;; Predicates to check are following:
107     ;; (boundp 'MULE) is t only if Mule (original; anything older than
108     ;;                     Mule 2.3) is running.
109     ;; (featurep 'mule) is t when other mule variants are running.
110
111     ;; It is possible to detect XEmacs/mule by (featurep 'mule) and
112     ;; (featurep 'xemacs).  In this case, the implementation for
113     ;; XEmacs/mule may be shareable between XEmacs and XEmacs/mule.
114
115     (defvar gnus-summary-display-table nil
116       "Display table used in summary mode buffers.")
117     (defalias 'gnus-max-width-function 'gnus-mule-max-width-function)
118
119     (when (boundp 'gnus-check-before-posting)
120       (setq gnus-check-before-posting
121             (delq 'long-lines
122                   (delq 'control-chars gnus-check-before-posting))))
123
124     (defun gnus-summary-line-format-spec ()
125       (insert gnus-tmp-unread gnus-tmp-replied
126               gnus-tmp-score-char gnus-tmp-indentation)
127       (put-text-property
128        (point)
129        (progn
130          (insert
131           gnus-tmp-opening-bracket
132           (format "%4d: %-20s"
133                   gnus-tmp-lines
134                   (if (> (length gnus-tmp-name) 20)
135                       (truncate-string-to-width gnus-tmp-name 20)
136                     gnus-tmp-name))
137           gnus-tmp-closing-bracket)
138          (point))
139        gnus-mouse-face-prop gnus-mouse-face)
140       (insert " " gnus-tmp-subject-or-nil "\n")))))
141
142 ;; Clone of `appt-select-lowest-window' in appt.el.
143 (defun gnus-select-lowest-window ()
144 "Select the lowest window on the frame."
145   (let ((lowest-window (selected-window))
146         (bottom-edge (nth 3 (window-edges))))
147     (walk-windows (lambda (w)
148                     (let ((next-bottom-edge (nth 3 (window-edges w))))
149                       (when (< bottom-edge next-bottom-edge)
150                         (setq bottom-edge next-bottom-edge
151                               lowest-window w)))))
152     (select-window lowest-window)))
153
154 (defun gnus-region-active-p ()
155   "Say whether the region is active."
156   (and (boundp 'transient-mark-mode)
157        transient-mark-mode
158        (boundp 'mark-active)
159        mark-active))
160
161 (defun gnus-mark-active-p ()
162   "Non-nil means the mark and region are currently active in this buffer."
163   mark-active) ; aliased to region-exists-p in XEmacs.
164
165 (defun gnus-x-splash ()
166   "Show a splash screen using a pixmap in the current buffer."
167   (interactive)
168   (switch-to-buffer (gnus-get-buffer-create (if (or (gnus-alive-p)
169                                                     (interactive-p))
170                                                 "*gnus-x-splash*"
171                                               gnus-group-buffer)))
172   (let ((inhibit-read-only nil)
173         (file (nnheader-find-etc-directory "images/gnus/x-splash" t))
174         pixmap fcw fch width height fringes sbars left yoffset top)
175     (erase-buffer)
176     (when (and file
177                (ignore-errors
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                      (setq pixmap (read (current-buffer)))))))
184       (setq fcw (float (frame-char-width))
185             fch (float (frame-char-height))
186             width (/ (car pixmap) fcw)
187             height (/ (cadr pixmap) fch)
188             fringes (if (fboundp 'window-fringes)
189                         (eval '(window-fringes))
190                       '(10 11 nil))
191             sbars (/ (or (frame-parameter nil 'scroll-bar-width) 14) fcw))
192       (setq sbars (if (eq (frame-parameter nil 'vertical-scroll-bars) 'right)
193                       (cons 0 sbars)
194                     (cons sbars 0)))
195       (setq left (- (* (round (/ (1- (/ (+ (window-width)
196                                            (car sbars) (cdr sbars)
197                                            (/ (+ (or (car fringes) 0)
198                                                  (or (cadr fringes) 0))
199                                               fcw))
200                                         width))
201                                  2))
202                        width)
203                     (car sbars)
204                     (/ (or (car fringes) 0) fcw))
205             yoffset (cadr (window-edges))
206             top (max 0 (- (* (max (if (and tool-bar-mode
207                                            (not (featurep 'gtk))
208                                            (eq (frame-first-window)
209                                                (selected-window)))
210                                       1 0)
211                                   (round (/ (1- (/ (+ (1- (window-height))
212                                                       (* 2 yoffset))
213                                                    height))
214                                             2)))
215                              height)
216                           yoffset
217                           (/ (* (or line-spacing 0) 2) fch)))
218             height (- height (/ (or line-spacing 0) fch)))
219       (cond ((>= top 1)
220              (insert (propertize
221                       " "
222                       'display `(space :width 0 :ascent 100))
223                      "\n"
224                      (propertize
225                       " "
226                       'display `(space :width 0 :height ,(1- top) :ascent 100))
227                      "\n"))
228             ((> top 0)
229              (insert (propertize
230                       " "
231                       'display `(space :width 0 :height ,top :ascent 100))
232                      "\n")))
233       (if (and (> width 0) (> left 0))
234           (insert (propertize
235                    " "
236                    'display `(space :width ,left :height ,height :ascent 0)))
237         (setq width (+ width left)))
238       (when (> width 0)
239         (insert (propertize
240                  " "
241                  'display `(space :width ,width :height ,height :ascent 0)
242                  'face `(gnus-splash :stipple ,pixmap))))
243       (goto-char (if (zerop top) (1- (point)) (point-min)))
244       (redraw-frame (selected-frame))
245       (sit-for 0))))
246
247 ;;; Image functions.
248
249 (defun gnus-image-type-available-p (type)
250   (and (fboundp 'image-type-available-p)
251        (image-type-available-p type)
252        (if (fboundp 'display-images-p)
253            (display-images-p)
254          t)))
255
256 (defun gnus-create-image (file &optional type data-p &rest props)
257   (let ((face (plist-get props :face)))
258     (when face
259       (setq props (plist-put props :foreground (face-foreground face)))
260       (setq props (plist-put props :background (face-background face))))
261     (apply 'create-image file type data-p props)))
262
263 (defun gnus-put-image (glyph &optional string category)
264   (let ((point (point)))
265     (insert-image glyph (or string " "))
266     (put-text-property point (point) 'gnus-image-category category)
267     (unless string
268       (put-text-property (1- (point)) (point)
269                          'gnus-image-text-deletable t))
270     glyph))
271
272 (defun gnus-remove-image (image &optional category)
273   "Remove the image matching IMAGE and CATEGORY found first."
274   (let ((start (point-min))
275         val end)
276     (while (and (not end)
277                 (or (setq val (get-text-property start 'display))
278                     (and (setq start
279                                (next-single-property-change start 'display))
280                          (setq val (get-text-property start 'display)))))
281       (setq end (or (next-single-property-change start 'display)
282                     (point-max)))
283       (if (and (equal val image)
284                (equal (get-text-property start 'gnus-image-category)
285                       category))
286           (progn
287             (put-text-property start end 'display nil)
288             (when (get-text-property start 'gnus-image-text-deletable)
289               (delete-region start end)))
290         (unless (= end (point-max))
291           (setq start end
292                 end nil))))))
293
294 (provide 'gnus-ems)
295
296 ;;; arch-tag: e7360b45-14b5-4171-aa39-69a44aed3cdb
297 ;;; gnus-ems.el ends here