* smiley-ems.el (smiley-update-cache): Check for valid types.
[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
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 (or (featurep 'xemacs)
41           (< emacs-major-version 20))
42       '("--**-" . "-----")
43     '("**" "--")))
44
45 (eval-and-compile
46   (autoload 'gnus-xmas-define "gnus-xmas")
47   (autoload 'gnus-xmas-redefine "gnus-xmas")
48   (autoload 'appt-select-lowest-window "appt"))
49
50 (if (featurep 'xemacs)
51     (autoload 'gnus-smiley-display "smiley")
52   (autoload 'gnus-smiley-display "smiley-ems")) ; override XEmacs version
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   (defalias 'gnus-char-width
74     (if (fboundp 'char-width)
75         'char-width
76       (lambda (ch) 1)))) ;; A simple hack.
77
78 (eval-and-compile
79   (if (featurep 'xemacs)
80       (gnus-xmas-define)
81     (defvar gnus-mouse-face-prop 'mouse-face
82       "Property used for highlighting mouse regions.")))
83
84 (eval-and-compile
85   (let ((case-fold-search t))
86     (cond
87      ((string-match "windows-nt\\|os/2\\|emx\\|cygwin32"
88                     (symbol-name system-type))
89       (setq nnheader-file-name-translation-alist
90             (append nnheader-file-name-translation-alist
91                     (mapcar (lambda (c) (cons c ?_))
92                             '(?: ?* ?\" ?< ?> ??))
93                     (if (string-match "windows-nt\\|cygwin32"
94                                       (symbol-name system-type))
95                         nil
96                       '((?+ . ?-)))))))))
97
98 (defvar gnus-tmp-unread)
99 (defvar gnus-tmp-replied)
100 (defvar gnus-tmp-score-char)
101 (defvar gnus-tmp-indentation)
102 (defvar gnus-tmp-opening-bracket)
103 (defvar gnus-tmp-lines)
104 (defvar gnus-tmp-name)
105 (defvar gnus-tmp-closing-bracket)
106 (defvar gnus-tmp-subject-or-nil)
107 (defvar gnus-check-before-posting)
108
109 (defun gnus-ems-redefine ()
110   (cond
111    ((featurep 'xemacs)
112     (gnus-xmas-redefine))
113
114    ((featurep 'mule)
115     ;; Mule and new Emacs definitions
116
117     ;; [Note] Now there are three kinds of mule implementations,
118     ;; original MULE, XEmacs/mule and Emacs 20+ including
119     ;; MULE features.  Unfortunately these API are different.  In
120     ;; particular, Emacs (including original MULE) and XEmacs are
121     ;; quite different.  Howvere, this version of Gnus doesn't support
122     ;; anything other than XEmacs 20+ and Emacs 20.3+.
123
124     ;; Predicates to check are following:
125     ;; (boundp 'MULE) is t only if MULE (original; anything older than
126     ;;                     Mule 2.3) is running.
127     ;; (featurep 'mule) is t when every mule variants are running.
128
129     ;; It is possible to detect XEmacs/mule by (featurep 'mule) and
130     ;; checking `emacs-version'.  In this case, the implementation for
131     ;; XEmacs/mule may be shareable between XEmacs and XEmacs/mule.
132
133     (defvar gnus-summary-display-table nil
134       "Display table used in summary mode buffers.")
135     (defalias 'gnus-max-width-function 'gnus-mule-max-width-function)
136
137     (when (boundp 'gnus-check-before-posting)
138       (setq gnus-check-before-posting
139             (delq 'long-lines
140                   (delq 'control-chars gnus-check-before-posting))))
141
142     (defun gnus-summary-line-format-spec ()
143       (insert gnus-tmp-unread gnus-tmp-replied
144               gnus-tmp-score-char gnus-tmp-indentation)
145       (put-text-property
146        (point)
147        (progn
148          (insert
149           gnus-tmp-opening-bracket
150           (format "%4d: %-20s"
151                   gnus-tmp-lines
152                   (if (> (length gnus-tmp-name) 20)
153                       (truncate-string-to-width gnus-tmp-name 20)
154                     gnus-tmp-name))
155           gnus-tmp-closing-bracket)
156          (point))
157        gnus-mouse-face-prop gnus-mouse-face)
158       (insert " " gnus-tmp-subject-or-nil "\n")))))
159
160 (defun gnus-region-active-p ()
161   "Say whether the region is active."
162   (and (boundp 'transient-mark-mode)
163        transient-mark-mode
164        (boundp 'mark-active)
165        mark-active))
166
167 (if (fboundp 'add-minor-mode)
168     (defalias 'gnus-add-minor-mode 'add-minor-mode)
169   (defun gnus-add-minor-mode (mode name map &rest rest)
170     (set (make-local-variable mode) t)
171     (unless (assq mode minor-mode-alist)
172       (push `(,mode ,name) minor-mode-alist))
173     (unless (assq mode minor-mode-map-alist)
174       (push (cons mode map)
175             minor-mode-map-alist))))
176
177 (defun gnus-x-splash ()
178   "Show a splash screen using a pixmap in the current buffer."
179   (let ((dir (nnheader-find-etc-directory "gnus"))
180         pixmap file height beg i)
181     (save-excursion
182       (switch-to-buffer (gnus-get-buffer-create gnus-group-buffer))
183       (let ((buffer-read-only nil)
184             width height)
185         (erase-buffer)
186         (when (and dir
187                    (file-exists-p (setq file
188                                         (expand-file-name "x-splash" dir))))
189           (with-temp-buffer
190             (insert-file-contents file)
191             (goto-char (point-min))
192             (ignore-errors
193               (setq pixmap (read (current-buffer))))))
194         (when pixmap
195           (make-face 'gnus-splash)
196           (setq height (/ (car pixmap) (frame-char-height))
197                 width (/ (cadr pixmap) (frame-char-width)))
198           (set-face-foreground 'gnus-splash "Brown")
199           (set-face-stipple 'gnus-splash pixmap)
200           (insert-char ?\n (* (/ (window-height) 2 height) height))
201           (setq i height)
202           (while (> i 0)
203             (insert-char ?\  (* (/ (window-width) 2 width) width))
204             (setq beg (point))
205             (insert-char ?\  width)
206             (set-text-properties beg (point) '(face gnus-splash))
207             (insert ?\n)
208             (decf i))
209           (goto-char (point-min))
210           (sit-for 0))))))
211
212 (defvar gnus-article-xface-ring-internal nil
213   "Cache for face data.")
214
215 ;; Worth customizing?
216 (defvar gnus-article-xface-ring-size 6
217   "Length of the ring used for `gnus-article-xface-ring-internal'.")
218
219 (defvar gnus-article-compface-xbm
220   (condition-case ()
221       (eq 0 (string-match "#define"
222                           (shell-command-to-string "uncompface -X")))
223     (error nil))
224   "Non-nil means the compface program supports the -X option.
225 That produces XBM output.")
226
227 (defun gnus-article-display-xface (data)
228   "Display the XFace header FACE in the current buffer.
229 Requires support for images in your Emacs and the external programs
230 `uncompface', and `icontopbm'.  On a GNU/Linux system these
231 might be in packages with names like `compface' or `faces-xface' and
232 `netpbm' or `libgr-progs', for instance.  See also
233 `gnus-article-compface-xbm'.
234
235 This function is for Emacs 21+.  See `gnus-xmas-article-display-xface'
236 for XEmacs."
237   ;; It might be worth converting uncompface's output in Lisp.
238
239   (when (if (fboundp 'display-graphic-p)
240             (display-graphic-p))
241     (unless gnus-article-xface-ring-internal ; Only load ring when needed.
242       (setq gnus-article-xface-ring-internal
243             (make-ring gnus-article-xface-ring-size)))
244     (save-excursion
245       (let* ((cur (current-buffer))
246              (image (cdr-safe (assoc data (ring-elements
247                                            gnus-article-xface-ring-internal))))
248              default-enable-multibyte-characters)
249         (unless image
250           (with-temp-buffer
251             (insert data)
252             (and (eq 0 (apply #'call-process-region (point-min) (point-max)
253                               "uncompface"
254                               'delete '(t nil) nil
255                               (if gnus-article-compface-xbm
256                                   '("-X"))))
257                  (if gnus-article-compface-xbm
258                      t
259                    (goto-char (point-min))
260                    (progn (insert "/* Width=48, Height=48 */\n") t)
261                    (eq 0 (call-process-region (point-min) (point-max)
262                                               "icontopbm"
263                                               'delete '(t nil))))
264                  ;; Miles Bader says that faces don't look right as
265                  ;; light on dark.
266                  (if (eq 'dark (cdr-safe (assq 'background-mode
267                                                (frame-parameters))))
268                      (setq image (create-image (buffer-string)
269                                                (if gnus-article-compface-xbm
270                                                    'xbm
271                                                  'pbm)
272                                                t
273                                                :ascent 'center
274                                                :foreground "black"
275                                                :background "white"))
276                    (setq image (create-image (buffer-string)
277                                              (if gnus-article-compface-xbm
278                                                  'xbm
279                                                'pbm)
280                                              t
281                                              :ascent 'center)))))
282           (ring-insert gnus-article-xface-ring-internal (cons data image)))
283         (when image
284           (goto-char (point-min))
285           (re-search-forward "^From:" nil 'move)
286           (while (get-text-property (point) 'display)
287             (goto-char (next-single-property-change (point) 'display)))
288           (gnus-add-wash-type 'xface)
289           (gnus-add-image 'xface image)
290           (insert-image image))))))
291
292 ;;; Image functions.
293
294 (defun gnus-image-type-available-p (type)
295   (and (fboundp 'image-type-available-p)
296        (image-type-available-p type)))
297
298 (defun gnus-create-image (file)
299   (create-image file))
300
301 (defun gnus-put-image (glyph &optional string)
302   (insert-image glyph string))
303
304 (defun gnus-remove-image (image)
305   (dolist (position (gnus-text-with-property 'display))
306     (when (equal (get-text-property position 'display) image)
307       (put-text-property position (1+ position) 'display nil))))
308
309 (provide 'gnus-ems)
310
311 ;;; gnus-ems.el ends here