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