Merge from emacs--devo--0
[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 3, 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 (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 (autoload 'gnus-alive-p "gnus-util")
165
166 (defun gnus-x-splash ()
167   "Show a splash screen using a pixmap in the current buffer."
168   (interactive)
169   (unless window-system
170     (error "`gnus-x-splash' requires running on the window system"))
171   (switch-to-buffer (gnus-get-buffer-create (if (or (gnus-alive-p)
172                                                     (interactive-p))
173                                                 "*gnus-x-splash*"
174                                               gnus-group-buffer)))
175   (let ((inhibit-read-only t)
176         (file (nnheader-find-etc-directory "images/gnus/x-splash" t))
177         pixmap fcw fch width height fringes sbars left yoffset top ls)
178     (erase-buffer)
179     (sit-for 0) ;; Necessary for measuring the window size correctly.
180     (when (and file
181                (ignore-errors
182                 (let ((coding-system-for-read 'raw-text)
183                       default-enable-multibyte-characters)
184                   (with-temp-buffer
185                     (insert-file-contents file)
186                     (goto-char (point-min))
187                     (setq pixmap (read (current-buffer)))))))
188       (setq fcw (float (frame-char-width))
189             fch (float (frame-char-height))
190             width (/ (car pixmap) fcw)
191             height (/ (cadr pixmap) fch)
192             fringes (if (fboundp 'window-fringes)
193                         (eval '(window-fringes))
194                       '(10 11 nil))
195             sbars (frame-parameter nil 'vertical-scroll-bars))
196       (cond ((eq sbars 'right)
197              (setq sbars
198                    (cons 0 (/ (or (frame-parameter nil 'scroll-bar-width) 14)
199                               fcw))))
200             (sbars
201              (setq sbars
202                    (cons (/ (or (frame-parameter nil 'scroll-bar-width) 14)
203                             fcw)
204                          0)))
205             (t
206              (setq sbars '(0 . 0))))
207       (setq left (- (* (round (/ (1- (/ (+ (window-width)
208                                            (car sbars) (cdr sbars)
209                                            (/ (+ (or (car fringes) 0)
210                                                  (or (cadr fringes) 0))
211                                               fcw))
212                                         width))
213                                  2))
214                        width)
215                     (car sbars)
216                     (/ (or (car fringes) 0) fcw))
217             yoffset (cadr (window-edges))
218             top (max 0 (- (* (max (if (and tool-bar-mode
219                                            (not (featurep 'gtk))
220                                            (eq (frame-first-window)
221                                                (selected-window)))
222                                       1 0)
223                                   (round (/ (1- (/ (+ (1- (window-height))
224                                                       (* 2 yoffset))
225                                                    height))
226                                             2)))
227                              height)
228                           yoffset))
229             ls (/ (or line-spacing 0) fch)
230             height (max 0 (- height ls)))
231       (cond ((>= (- top ls) 1)
232              (insert
233               (propertize
234                " "
235                'display `(space :width 0 :ascent 100))
236               "\n"
237               (propertize
238                " "
239                'display `(space :width 0 :height ,(- top ls 1) :ascent 100))
240               "\n"))
241             ((> (- top ls) 0)
242              (insert
243               (propertize
244                " "
245                'display `(space :width 0 :height ,(- top ls) :ascent 100))
246               "\n")))
247       (if (and (> width 0) (> left 0))
248           (insert (propertize
249                    " "
250                    'display `(space :width ,left :height ,height :ascent 0)))
251         (setq width (+ width left)))
252       (when (> width 0)
253         (insert (propertize
254                  " "
255                  'display `(space :width ,width :height ,height :ascent 0)
256                  'face `(gnus-splash :stipple ,pixmap))))
257       (goto-char (if (<= (- top ls) 0) (1- (point)) (point-min)))
258       (redraw-frame (selected-frame))
259       (sit-for 0))))
260
261 ;;; Image functions.
262
263 (defun gnus-image-type-available-p (type)
264   (and (fboundp 'image-type-available-p)
265        (image-type-available-p type)
266        (if (fboundp 'display-images-p)
267            (display-images-p)
268          t)))
269
270 (defun gnus-create-image (file &optional type data-p &rest props)
271   (let ((face (plist-get props :face)))
272     (when face
273       (setq props (plist-put props :foreground (face-foreground face)))
274       (setq props (plist-put props :background (face-background face))))
275     (apply 'create-image file type data-p props)))
276
277 (defun gnus-put-image (glyph &optional string category)
278   (let ((point (point)))
279     (insert-image glyph (or string " "))
280     (put-text-property point (point) 'gnus-image-category category)
281     (unless string
282       (put-text-property (1- (point)) (point)
283                          'gnus-image-text-deletable t))
284     glyph))
285
286 (defun gnus-remove-image (image &optional category)
287   "Remove the image matching IMAGE and CATEGORY found first."
288   (let ((start (point-min))
289         val end)
290     (while (and (not end)
291                 (or (setq val (get-text-property start 'display))
292                     (and (setq start
293                                (next-single-property-change start 'display))
294                          (setq val (get-text-property start 'display)))))
295       (setq end (or (next-single-property-change start 'display)
296                     (point-max)))
297       (if (and (equal val image)
298                (equal (get-text-property start 'gnus-image-category)
299                       category))
300           (progn
301             (put-text-property start end 'display nil)
302             (when (get-text-property start 'gnus-image-text-deletable)
303               (delete-region start end)))
304         (unless (= end (point-max))
305           (setq start end
306                 end nil))))))
307
308 (provide 'gnus-ems)
309
310 ;;; arch-tag: e7360b45-14b5-4171-aa39-69a44aed3cdb
311 ;;; gnus-ems.el ends here