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