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