e32f4ee7ae9add836e9db8748f568d7d6f1c3ae4
[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
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 (require 'cl))
30
31 ;;; Function aliases later to be redefined for XEmacs usage.
32
33 (eval-and-compile
34   (defvar gnus-xemacs (featurep 'xemacs)
35     "Non-nil if running under XEmacs."))
36
37 (defvar gnus-mouse-2 [mouse-2])
38 (defvar gnus-down-mouse-3 [down-mouse-3])
39 (defvar gnus-down-mouse-2 [down-mouse-2])
40 (defvar gnus-widget-button-keymap nil)
41 (defvar gnus-mode-line-modified
42   (if (or gnus-xemacs
43           (< emacs-major-version 20))
44       '("--**-" . "-----")
45     '("**" "--")))
46
47 (eval-and-compile
48   (autoload 'gnus-xmas-define "gnus-xmas")
49   (autoload 'gnus-xmas-redefine "gnus-xmas")
50   (autoload 'appt-select-lowest-window "appt"))
51
52 ;;; Mule functions.
53
54 (defun gnus-mule-max-width-function (el max-width)
55   `(let* ((val (eval (, el)))
56           (valstr (if (numberp val)
57                       (int-to-string val) val)))
58      (if (> (length valstr) ,max-width)
59          (truncate-string-to-width valstr ,max-width)
60        valstr)))
61
62 (eval-and-compile
63   (if gnus-xemacs
64       (gnus-xmas-define)
65     (defvar gnus-mouse-face-prop 'mouse-face
66       "Property used for highlighting mouse regions.")))
67
68 (eval-and-compile
69   (cond
70    ((not window-system)
71     (let ((funcs '(mouse-set-point set-face-foreground
72                                    set-face-background x-popup-menu)))
73       (while funcs
74         (unless (fboundp (car funcs))
75           (defalias (car funcs) 'ignore))
76         (setq funcs (cdr funcs)))))))
77
78 (eval-and-compile
79   (let ((case-fold-search t))
80     (cond
81      ((string-match "windows-nt\\|os/2\\|emx\\|cygwin32"
82                     (symbol-name system-type))
83       (setq nnheader-file-name-translation-alist
84             (append nnheader-file-name-translation-alist
85                     (mapcar (lambda (c) (cons c ?_))
86                             '(?: ?* ?\" ?< ?> ??))
87                     '((?+ . ?-))))))))
88
89 (defvar gnus-tmp-unread)
90 (defvar gnus-tmp-replied)
91 (defvar gnus-tmp-score-char)
92 (defvar gnus-tmp-indentation)
93 (defvar gnus-tmp-opening-bracket)
94 (defvar gnus-tmp-lines)
95 (defvar gnus-tmp-name)
96 (defvar gnus-tmp-closing-bracket)
97 (defvar gnus-tmp-subject-or-nil)
98
99 (defun gnus-ems-redefine ()
100   (cond
101    (gnus-xemacs
102     (gnus-xmas-redefine))
103
104    ((featurep 'mule)
105     ;; Mule and new Emacs definitions
106
107     ;; [Note] Now there are three kinds of mule implementations,
108     ;; original MULE, XEmacs/mule and Emacs 20+ including
109     ;; MULE features.  Unfortunately these API are different.  In
110     ;; particular, Emacs (including original MULE) and XEmacs are
111     ;; quite different.  Howvere, this version of Gnus doesn't support
112     ;; anything other than XEmacs 20+ and Emacs 20.3+.
113
114     ;; Predicates to check are following:
115     ;; (boundp 'MULE) is t only if MULE (original; anything older than
116     ;;                     Mule 2.3) is running.
117     ;; (featurep 'mule) is t when every mule variants are running.
118
119     ;; It is possible to detect XEmacs/mule by (featurep 'mule) and
120     ;; checking `emacs-version'.  In this case, the implementation for
121     ;; XEmacs/mule may be shareable between XEmacs and XEmacs/mule.
122
123     (defvar gnus-summary-display-table nil
124       "Display table used in summary mode buffers.")
125     (defalias 'gnus-max-width-function 'gnus-mule-max-width-function)
126     (defalias 'gnus-summary-set-display-table 'ignore)
127
128     (when (boundp 'gnus-check-before-posting)
129       (setq gnus-check-before-posting
130             (delq 'long-lines
131                   (delq 'control-chars gnus-check-before-posting))))
132
133     (defun gnus-summary-line-format-spec ()
134       (insert gnus-tmp-unread gnus-tmp-replied
135               gnus-tmp-score-char gnus-tmp-indentation)
136       (put-text-property
137        (point)
138        (progn
139          (insert
140           gnus-tmp-opening-bracket
141           (format "%4d: %-20s"
142                   gnus-tmp-lines
143                   (if (> (length gnus-tmp-name) 20)
144                       (truncate-string-to-width gnus-tmp-name 20)
145                     gnus-tmp-name))
146           gnus-tmp-closing-bracket)
147          (point))
148        gnus-mouse-face-prop gnus-mouse-face)
149       (insert " " gnus-tmp-subject-or-nil "\n")))))
150
151 (defun gnus-region-active-p ()
152   "Say whether the region is active."
153   (and (boundp 'transient-mark-mode)
154        transient-mark-mode
155        (boundp 'mark-active)
156        mark-active))
157
158 (if (fboundp 'add-minor-mode)
159     (defalias 'gnus-add-minor-mode 'add-minor-mode)
160   (defun gnus-add-minor-mode (mode name map &rest rest)
161     (set (make-local-variable mode) t)
162     (unless (assq mode minor-mode-alist)
163       (push `(,mode ,name) minor-mode-alist))
164     (unless (assq mode minor-mode-map-alist)
165       (push (cons mode map)
166             minor-mode-map-alist))))
167
168 (defun gnus-x-splash ()
169   "Show a splash screen using a pixmap in the current buffer."
170   (let ((dir (nnheader-find-etc-directory "gnus"))
171         pixmap file height beg i)
172     (save-excursion
173       (switch-to-buffer (gnus-get-buffer-create gnus-group-buffer))
174       (let ((buffer-read-only nil))
175         (erase-buffer)
176         (when (and dir
177                    (file-exists-p (setq file
178                                         (expand-file-name "x-splash" dir))))
179           (with-temp-buffer
180             (insert-file-contents file)
181             (goto-char (point-min))
182             (ignore-errors
183               (setq pixmap (read (current-buffer))))))
184         (when pixmap
185           (make-face 'gnus-splash)
186           (setq height (/ (car pixmap) (frame-char-height))
187                 width (/ (cadr pixmap) (frame-char-width)))
188           (set-face-foreground 'gnus-splash "Brown")
189           (set-face-stipple 'gnus-splash pixmap)
190           (insert-char ?\n (* (/ (window-height) 2 height) height))
191           (setq i height)
192           (while (> i 0)
193             (insert-char ?\  (* (/ (window-width) 2 width) width))
194             (setq beg (point))
195             (insert-char ?\  width)
196             (set-text-properties beg (point) '(face gnus-splash))
197             (insert ?\n)
198             (decf i))
199           (goto-char (point-min))
200           (sit-for 0))))))
201
202 (defvar gnus-article-xface-ring-internal nil
203   "Cache for face data.")
204
205 ;; Worth customizing?
206 (defvar gnus-article-xface-ring-size 6
207   "Length of the ring used for `gnus-article-xface-ring-internal'.")
208
209 (defun gnus-article-display-xface (beg end)
210   "Display an XFace header from between BEG and END in the current article.
211 Requires support for images in your Emacs and the external programs
212 `uncompface', `icontopbm' and `ppmtoxbm'.  On a GNU/Linux system these
213 might be in packages with names like `compface' or `faces-xface' and
214 `netpbm' or `libgr-progs', for instance.
215
216 This function is for Emacs 21+.  See `gnus-xmas-article-display-xface'
217 for XEmacs."
218   ;; It might be worth converting uncompface's output in Lisp.
219
220   (unless gnus-article-xface-ring-internal ; Only load ring when needed.
221     (setq gnus-article-xface-ring-internal
222           (make-ring gnus-article-xface-ring-size)))
223   (save-excursion
224     (let* ((cur (current-buffer))
225            (data (buffer-substring beg end))
226            (image (cdr-safe (assoc data (ring-elements
227                                          gnus-article-xface-ring-internal)))))
228       (when (if (fboundp 'display-graphic-p)
229                 (display-graphic-p))
230         (unless image
231           (let ((coding-system-for-read 'binary)
232                 (coding-system-for-write 'binary))
233             (with-temp-buffer
234               (insert data)
235               (and (eq 0 (call-process-region (point-min) (point-max)
236                                               "uncompface"
237                                               'delete '(t nil)))
238                    (goto-char (point-min))
239                    (progn (insert "/* Width=48, Height=48 */\n") t)
240                    (eq 0 (call-process-region (point-min) (point-max)
241                                               "icontopbm"
242                                               'delete '(t nil)))
243                    (eq 0 (call-process-region (point-min) (point-max)
244                                               "pbmtoxbm"
245                                               'delete '(t nil)))
246                    ;; Miles Bader says that faces don't look right as
247                    ;; light on dark.
248                    (if (eq 'dark (cdr-safe (assq 'background-mode
249                                                  (frame-parameters))))
250                        (setq image (create-image (buffer-string) 'xbm t
251                                                  :ascent 'center
252                                                  :foreground "black"
253                                                  :background "white"))
254                      (setq image (create-image (buffer-string) 'xbm t
255                                                :ascent 'center))))))
256           (ring-insert gnus-article-xface-ring-internal (cons data image))))
257       (when image
258         (goto-char (point-min))
259         (re-search-forward "^From:" nil 'move)
260         (insert-image image)))))
261
262 (provide 'gnus-ems)
263
264 ;; Local Variables:
265 ;; byte-compile-warnings: '(redefine callargs)
266 ;; End:
267
268 ;;; gnus-ems.el ends here