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