1999-02-27 Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
[elisp/gnus.git-] / lisp / gnus-ems.el
1 ;;; gnus-ems.el --- functions for making Semi-gnus work under different Emacsen
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;         Tatsuya Ichikawa <t-ichi@niagara.shiojiri.ne.jp>
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 (defvar gnus-xemacs (string-match "XEmacs\\|Lucid" emacs-version)
34   "Non-nil if running under XEmacs.")
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 (or gnus-xemacs
42           (< emacs-major-version 20))
43       '("--**-" . "-----")
44     '("**" "--")))
45
46 (eval-and-compile
47   (autoload 'gnus-xmas-define "gnus-xmas")
48   (autoload 'gnus-xmas-redefine "gnus-xmas")
49   (autoload 'appt-select-lowest-window "appt"))
50
51 (or (fboundp 'mail-file-babyl-p)
52     (fset 'mail-file-babyl-p 'rmail-file-p))
53
54 ;;; Mule functions.
55
56 (defvar gnus-mule-bitmap-image-file nil)
57 (defun gnus-mule-group-startup-message (&optional x y)
58   "Insert startup message in current buffer."
59   ;; Insert the message.
60   (erase-buffer)
61   (insert
62    (if (featurep 'bitmap)
63      (format "              %s
64
65 "
66              "" (if (and (stringp gnus-mule-bitmap-image-file)
67                          (file-exists-p gnus-mule-bitmap-image-file))
68                     (insert-file gnus-mule-bitmap-image-file)))
69      (format "              %s
70           _    ___ _             _
71           _ ___ __ ___  __    _ ___
72           __   _     ___    __  ___
73               _           ___     _
74              _  _ __             _
75              ___   __            _
76                    __           _
77                     _      _   _
78                    _      _    _
79                       _  _    _
80                   __  ___
81                  _   _ _     _
82                 _   _
83               _    _
84              _    _
85             _
86           __
87
88 "
89              "")))
90   ;; And then hack it.
91   (gnus-indent-rigidly (point-min) (point-max)
92                        (/ (max (- (window-width) (or x 46)) 0) 2))
93   (goto-char (point-min))
94   (forward-line 1)
95   (let* ((pheight (count-lines (point-min) (point-max)))
96          (wheight (window-height))
97          (rest (- wheight pheight)))
98     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
99   ;; Fontify some.
100   (put-text-property (point-min) (point-max) 'face 'gnus-splash-face)
101   (goto-char (point-min))
102   (setq mode-line-buffer-identification (concat " " gnus-version))
103   (setq gnus-simple-splash t)
104   (set-buffer-modified-p t))
105
106 (defun gnus-encode-coding-string (string system)
107   string)
108
109 (defun gnus-decode-coding-string (string system)
110   string)
111
112 (eval-and-compile
113   (if (string-match "XEmacs\\|Lucid" emacs-version)
114       nil
115
116     (defvar gnus-mouse-face-prop 'mouse-face
117       "Property used for highlighting mouse regions."))
118
119   (cond
120    ((string-match "XEmacs\\|Lucid" emacs-version)
121     (gnus-xmas-define))
122
123    ((or (not (boundp 'emacs-minor-version))
124         (and (< emacs-major-version 20)
125              (< emacs-minor-version 30)))
126     ;; Remove the `intangible' prop.
127     (let ((props (and (boundp 'gnus-hidden-properties)
128                       gnus-hidden-properties)))
129       (while (and props (not (eq (car (cdr props)) 'intangible)))
130         (setq props (cdr props)))
131       (when props
132         (setcdr props (cdr (cdr (cdr props))))))
133     (unless (fboundp 'buffer-substring-no-properties)
134       (defun buffer-substring-no-properties (beg end)
135         (format "%s" (buffer-substring beg end)))))
136
137    ((boundp 'MULE)
138     (provide 'gnusutil))))
139
140 (eval-and-compile
141   (cond
142    ((not window-system)
143     (defun gnus-dummy-func (&rest args))
144     (let ((funcs '(mouse-set-point set-face-foreground
145                                    set-face-background x-popup-menu)))
146       (while funcs
147         (unless (fboundp (car funcs))
148           (fset (car funcs) 'gnus-dummy-func))
149         (setq funcs (cdr funcs)))))))
150
151 (eval-and-compile
152   (let ((case-fold-search t))
153     (cond
154      ((string-match "windows-nt\\|os/2\\|emx" (symbol-name system-type))
155       (setq nnheader-file-name-translation-alist
156             (append nnheader-file-name-translation-alist
157                     '((?: . ?_)
158                       (?+ . ?-))))))))
159
160 (defvar gnus-tmp-unread)
161 (defvar gnus-tmp-replied)
162 (defvar gnus-tmp-score-char)
163 (defvar gnus-tmp-indentation)
164 (defvar gnus-tmp-opening-bracket)
165 (defvar gnus-tmp-lines)
166 (defvar gnus-tmp-name)
167 (defvar gnus-tmp-closing-bracket)
168 (defvar gnus-tmp-subject-or-nil)
169
170 (defun gnus-ems-redefine ()
171   (cond
172    ((string-match "XEmacs\\|Lucid" emacs-version)
173     (gnus-xmas-redefine))
174
175    ((featurep 'mule)
176     ;; Mule and new Emacs definitions
177
178     ;; [Note] Now there are three kinds of mule implementations,
179     ;; original MULE, XEmacs/mule and beta version of Emacs including
180     ;; some mule features. Unfortunately these API are different. In
181     ;; particular, Emacs (including original MULE) and XEmacs are
182     ;; quite different.
183     ;; Predicates to check are following:
184     ;; (boundp 'MULE) is t only if MULE (original; anything older than
185     ;;                     Mule 2.3) is running.
186     ;; (featurep 'mule) is t when every mule variants are running.
187
188     ;; These implementations may be able to share between original
189     ;; MULE and beta version of new Emacs. In addition, it is able to
190     ;; detect XEmacs/mule by (featurep 'mule) and to check variable
191     ;; `emacs-version'. In this case, implementation for XEmacs/mule
192     ;; may be able to share between XEmacs and XEmacs/mule.
193
194     (defvar gnus-summary-display-table nil
195       "Display table used in summary mode buffers.")
196     (fset 'gnus-summary-set-display-table (lambda ()))
197     (fset 'gnus-encode-coding-string 'encode-coding-string)
198     (fset 'gnus-decode-coding-string 'decode-coding-string)
199
200     (if (fboundp 'truncate-string-to-width)
201         (fset 'gnus-truncate-string 'truncate-string-to-width)
202       (fset 'gnus-truncate-string 'truncate-string))
203
204     (defun gnus-tilde-max-form (el max-width)
205       "Return a form that limits EL to MAX-WIDTH."
206       (let ((max (abs max-width)))
207         (if (symbolp el)
208             `(if (> (string-width ,el) ,max)
209                  ,(if (< max-width 0)
210                       `(gnus-truncate-string
211                         ,el (string-width ,el)
212                         (- (string-width ,el) ,max))
213                     `(gnus-truncate-string ,el ,max))
214                ,el)
215           `(let ((val (eval ,el)))
216              (if (> (string-width val) ,max)
217                  ,(if (< max-width 0)
218                       `(gnus-truncate-string
219                         val (string-width val)
220                         (- (string-width val) ,max))
221                     `(gnus-truncate-string val ,max))
222                val)))))
223
224     (defun gnus-tilde-cut-form (el cut-width)
225       "Return a form that cuts CUT-WIDTH off of EL."
226       (let ((cut (abs cut-width)))
227         (if (symbolp el)
228             `(if (> (string-width ,el) ,cut)
229                  ,(if (< cut-width 0)
230                       `(gnus-truncate-string
231                         ,el (- (string-width ,el) ,cut))
232                     `(gnus-truncate-string
233                       ,el (- (string-width ,el) ,cut) ,cut))
234                ,el)
235           `(let ((val (eval ,el)))
236              (if (> (string-width val) ,cut)
237                  ,(if (< cut-width 0)
238                       `(gnus-truncate-string
239                         val (- (string-width val) ,cut))
240                     `(gnus-truncate-string
241                       val (- (string-width val) ,cut) ,cut))
242                val)))))
243
244     (when window-system
245       (require 'path-util)
246       (if (module-installed-p 'bitmap)
247           (fset 'gnus-group-startup-message 'gnus-mule-group-startup-message)
248         ))
249
250     (when (boundp 'gnus-check-before-posting)
251       (setq gnus-check-before-posting
252             (delq 'long-lines
253                   (delq 'control-chars gnus-check-before-posting))))
254     )))
255
256 (defun gnus-region-active-p ()
257   "Say whether the region is active."
258   (and (boundp 'transient-mark-mode)
259        transient-mark-mode
260        (boundp 'mark-active)
261        mark-active))
262
263 (defun gnus-add-minor-mode (mode name map)
264   (if (fboundp 'add-minor-mode)
265       (add-minor-mode mode name map)
266     (set (make-local-variable mode) t)
267     (unless (assq mode minor-mode-alist)
268       (push `(,mode ,name) minor-mode-alist))
269     (unless (assq mode minor-mode-map-alist)
270       (push (cons mode map)
271             minor-mode-map-alist))))
272
273 (defun gnus-x-splash ()
274   "Show a splash screen using a pixmap in the current buffer."
275   (let ((dir (nnheader-find-etc-directory "gnus"))
276         pixmap file height beg i)
277     (save-excursion
278       (switch-to-buffer (gnus-get-buffer-create gnus-group-buffer))
279       (let ((buffer-read-only nil))
280         (erase-buffer)
281         (when (and dir
282                    (file-exists-p (setq file (concat dir "x-splash"))))
283           (with-temp-file nil
284             (insert-file-contents file)
285             (goto-char (point-min))
286             (ignore-errors
287               (setq pixmap (read (current-buffer))))))
288         (when pixmap
289           (erase-buffer)
290           (unless (facep 'gnus-splash)
291             (make-face 'gnus-splash))
292           (setq height (/ (car pixmap) (frame-char-height))
293                 width (/ (cadr pixmap) (frame-char-width)))
294           (set-face-foreground 'gnus-splash "ForestGreen")
295           (set-face-stipple 'gnus-splash pixmap)
296           (insert-char ?\n (* (/ (window-height) 2 height) height))
297           (setq i height)
298           (while (> i 0)
299             (insert-char ?  (* (+ (/ (window-width) 2 width) 1) width))
300             (setq beg (point))
301             (insert-char ?  width)
302             (set-text-properties beg (point) '(face gnus-splash))
303             (insert "\n")
304             (decf i))
305           (goto-char (point-min))
306           (sit-for 0))))))
307
308 (if (fboundp 'split-string)
309     (fset 'gnus-split-string 'split-string)
310   (defun gnus-split-string (string pattern)
311     "Return a list of substrings of STRING which are separated by PATTERN."
312     (let (parts (start 0))
313       (while (string-match pattern string start)
314         (setq parts (cons (substring string start (match-beginning 0)) parts)
315               start (match-end 0)))
316       (nreverse (cons (substring string start) parts)))))
317
318 (provide 'gnus-ems)
319
320 ;; Local Variables:
321 ;; byte-compile-warnings: '(redefine callargs)
322 ;; End:
323
324 ;;; gnus-ems.el ends here