75501684b4668893117c298f7a55475e9e53e555
[gnus] / lisp / gnus-ems.el
1 ;;; gnus-ems.el --- functions for making Gnus work under different Emacsen
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (defvar gnus-mouse-2 [mouse-2])
28 (defvar gnus-easymenu 'easymenu)
29 (defvar gnus-group-mode-hook ())
30 (defvar gnus-summary-mode-hook ())
31 (defvar gnus-article-mode-hook ())
32
33 ;; We do not byte-compile this file, because error messages are such a
34 ;; bore.  
35
36 (eval
37  '(cond 
38    ((string-match "XEmacs\\|Lucid" emacs-version)
39     ;; XEmacs definitions.
40
41     (setq gnus-mouse-2 [button2])
42     (setq gnus-easymenu 'auc-menu)
43
44     (or (memq 'underline (list-faces))
45         (funcall (intern "make-face") 'underline))
46     ;; Must avoid calling set-face-underline-p directly, because it
47     ;; is a defsubst in emacs19, and will make the .elc files non
48     ;; portable!
49     (or (face-differs-from-default-p 'underline)
50         (funcall 'set-face-underline-p 'underline t))
51     (or (fboundp 'set-text-properties)
52         (defun set-text-properties (start end props &optional buffer)
53           (if props
54               (put-text-property start end (car props) (cdr props) buffer)
55             (remove-text-properties start end ()))))
56     
57     (or (fboundp 'make-overlay) (fset 'make-overlay 'make-extent))
58     (or (fboundp 'overlay-put) (fset 'overlay-put 'set-extent-property))
59     (or (fboundp 'move-overlay) 
60         (defun move-overlay (extent start end &optional buffer)
61           (set-extent-endpoints extent start end)))
62     (or (boundp 'standard-display-table) (setq standard-display-table nil))
63     (or (boundp 'read-event) (fset 'read-event 'next-command-event))
64
65     (if (not gnus-visual)
66         ()
67       (setq gnus-group-mode-hook
68             (cons
69              '(lambda ()
70                (easy-menu-add gnus-group-reading-menu)
71                (easy-menu-add gnus-group-group-menu)
72                (easy-menu-add gnus-group-post-menu)
73                (easy-menu-add gnus-group-misc-menu)
74                (gnus-install-mouse-tracker)) 
75              gnus-group-mode-hook))
76       (setq gnus-summary-mode-hook
77             (cons
78              '(lambda ()
79                (easy-menu-add gnus-summary-mark-menu)
80                (easy-menu-add gnus-summary-move-menu)
81                (easy-menu-add gnus-summary-article-menu)
82                (easy-menu-add gnus-summary-thread-menu)
83                (easy-menu-add gnus-summary-misc-menu)
84                (easy-menu-add gnus-summary-post-menu)
85                (easy-menu-add gnus-summary-kill-menu)
86                (gnus-install-mouse-tracker)) 
87              gnus-summary-mode-hook))
88       (setq gnus-article-mode-hook
89             (cons
90              '(lambda ()
91                (easy-menu-add gnus-article-article-menu)
92                (easy-menu-add gnus-article-treatment-menu))
93              gnus-article-mode-hook)))
94
95     (defun gnus-install-mouse-tracker ()
96       (require 'mode-motion)
97       (setq mode-motion-hook 'mode-motion-highlight-line)))
98
99    ((and (not (string-match "28.9" emacs-version)) 
100          (not (string-match "29" emacs-version)))
101     (setq gnus-hidden-properties '(invisible t))
102     (or (fboundp 'buffer-substring-no-properties)
103         (defun buffer-substring-no-properties (beg end)
104           (format "%s" (buffer-substring beg end)))))
105    
106    ((boundp 'MULE)
107     (provide 'gnusutil))
108    
109    ))
110
111 (eval-and-compile
112   (cond
113    ((not window-system)
114     (defun gnus-dummy-func (&rest args))
115     (let ((funcs '(mouse-set-point set-face-foreground
116                                    set-face-background x-popup-menu)))
117       (while funcs
118         (or (fboundp (car funcs))
119             (fset (car funcs) 'gnus-dummy-func))
120         (setq funcs (cdr funcs))))))
121   (or (fboundp 'file-regular-p)
122       (defun file-regular-p (file)
123         (and (not (file-directory-p file))
124              (not (file-symlink-p file))
125              (file-exists-p file))))
126   (or (fboundp 'face-list)
127       (defun face-list (&rest args)))
128   )
129
130 (defun gnus-ems-redefine ()
131   (cond 
132    ((string-match "XEmacs\\|Lucid" emacs-version)
133     ;; XEmacs definitions.
134     (fset 'gnus-set-mouse-face (lambda (string) string))
135
136     (fset 'gnus-summary-make-display-table (lambda () nil))
137
138     (provide 'gnus)
139     (require 'gnus-vis)
140
141     (defun gnus-highlight-selected-summary ()
142       ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
143       ;; Highlight selected article in summary buffer
144       (if gnus-summary-selected-face
145           (save-excursion
146             (let* ((beg (progn (beginning-of-line) (point)))
147                    (end (progn (end-of-line) (point)))
148                    (to (max 1 (1- (or (previous-single-property-change
149                                        end 'mouse-face nil beg) end))))
150                    (from (1+ (or (next-single-property-change 
151                                   beg 'mouse-face nil end) beg))))
152               (if (< to beg)
153                   (progn
154                     (setq from beg)
155                     (setq to end)))
156               (if gnus-newsgroup-selected-overlay
157                   (delete-extent gnus-newsgroup-selected-overlay))
158               (setq gnus-newsgroup-selected-overlay
159                     (make-extent from to))
160               (set-extent-face gnus-newsgroup-selected-overlay
161                                gnus-summary-selected-face)))))
162
163
164     (defun gnus-summary-recenter ()
165       (let* ((top (cond ((< (window-height) 4) 0)
166                         ((< (window-height) 7) 1)
167                         (t 2)))
168              (height (- (window-height) 2))
169              (bottom (save-excursion (goto-char (point-max))
170                                      (forward-line (- height))
171                                      (point)))
172              (window (get-buffer-window (current-buffer))))
173         (and 
174          ;; The user has to want it,
175          gnus-auto-center-summary 
176          ;; the article buffer must be displayed,
177          (get-buffer-window gnus-article-buffer)
178          ;; Set the window start to either `bottom', which is the biggest
179          ;; possible valid number, or the second line from the top,
180          ;; whichever is the least.
181          (set-window-start
182           window (min bottom (save-excursion (forward-line (- top)) 
183                                              (point)))))))
184
185     (defun gnus-group-insert-group-line-info (group)
186       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
187             (beg (point))
188             active info)
189         (if entry
190             (progn
191               (setq info (nth 2 entry))
192               (gnus-group-insert-group-line 
193                nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
194           (setq active (gnus-gethash group gnus-active-hashtb))
195           
196           (gnus-group-insert-group-line 
197            nil group (if (member group gnus-zombie-list) gnus-level-zombie
198                        gnus-level-killed)
199            nil (if active (- (1+ (cdr active)) (car active)) 0) nil))
200         (save-excursion
201          (goto-char beg)
202          (remove-text-properties 
203           (1+ (gnus-point-at-bol)) (1+ (gnus-point-at-eol))
204           '(gnus-group nil)))))
205
206     (defun gnus-copy-article-buffer (&optional article-buffer)
207       (setq gnus-article-copy (get-buffer-create " *gnus article copy*"))
208       (buffer-disable-undo gnus-article-copy)
209       (or (memq gnus-article-copy gnus-buffer-list)
210           (setq gnus-buffer-list (cons gnus-article-copy gnus-buffer-list)))
211       (let ((article-buffer (or article-buffer gnus-article-buffer))
212             buf)
213         (if (and (get-buffer article-buffer)
214                  (buffer-name (get-buffer article-buffer)))
215             (save-excursion
216               (set-buffer article-buffer)
217               (widen)
218               (setq buf (buffer-substring (point-min) (point-max)))
219               (set-buffer gnus-article-copy)
220               (erase-buffer)
221               (insert (format "%s" buf))))))
222
223     (defun gnus-summary-refer-article (message-id)
224       "Refer article specified by MESSAGE-ID.
225 NOTE: This command only works with newsgroups that use real or simulated NNTP."
226       (interactive "sMessage-ID: ")
227       (if (or (not (stringp message-id))
228               (zerop (length message-id)))
229           ()
230         ;; Construct the correct Message-ID if necessary.
231         ;; Suggested by tale@pawl.rpi.edu.
232         (or (string-match "^<" message-id)
233             (setq message-id (concat "<" message-id)))
234         (or (string-match ">$" message-id)
235             (setq message-id (concat message-id ">")))
236         (let ((header (car (gnus-gethash (downcase message-id)
237                                          gnus-newsgroup-dependencies))))
238           (if header
239               (or (gnus-summary-goto-article (header-number header))
240                   ;; The header has been read, but the article had been
241                   ;; expunged, so we insert it again.
242                   (let ((beg (point)))
243                     (gnus-summary-insert-line
244                      nil header 0 nil gnus-read-mark nil nil
245                      (header-subject header))
246                     (save-excursion
247                       (goto-char beg)
248                       (remove-text-properties
249                        (1+ (gnus-point-at-bol)) (1+ (gnus-point-at-eol))
250                        '(gnus-number nil gnus-mark nil gnus-level nil)))
251                     (forward-line -1)
252                     (header-number header)))
253             (let ((gnus-override-method gnus-refer-article-method)
254                   (gnus-ancient-mark gnus-read-mark)
255                   (tmp-buf (get-buffer-create " *gnus refer"))
256                   (tmp-point (window-start
257                               (get-buffer-window gnus-article-buffer)))
258                   number)
259               (and gnus-refer-article-method
260                    (or (gnus-server-opened gnus-refer-article-method)
261                        (gnus-open-server gnus-refer-article-method)))
262               ;; Save the old article buffer.
263               (save-excursion
264                 (set-buffer tmp-buf)
265                 (buffer-disable-undo (current-buffer))
266                 (insert-buffer-substring gnus-article-buffer))
267               (prog1
268                   (if (gnus-article-prepare 
269                        message-id nil (gnus-read-header message-id))
270                       (progn
271                         (setq number (header-number gnus-current-headers))
272                         (gnus-rebuild-thread message-id)
273                         (gnus-summary-goto-subject number)
274                         (gnus-summary-recenter)
275                         (gnus-article-set-window-start 
276                          (cdr (assq number gnus-newsgroup-bookmarks)))
277                         message-id)
278                     ;; We restore the old article buffer.
279                     (save-excursion
280                       (set-buffer gnus-article-buffer)
281                       (let ((buffer-read-only nil))
282                         (insert-buffer-substring tmp-buf)
283                         (and tmp-point
284                              (set-window-start (get-buffer-window (current-buffer))
285                                                tmp-point))))
286                     nil)
287                 (kill-buffer tmp-buf)))))))
288
289
290
291     )
292
293    ((boundp 'MULE)
294     ;; Mule definitions
295     (if (not (fboundp 'truncate-string))
296         (defun truncate-string (str width)
297           (let ((w (string-width str))
298                 (col 0) (idx 0) (p-idx 0) chr)
299             (if (<= w width)
300                 str
301               (while (< col width)
302                 (setq chr (aref str idx)
303                       col (+ col (char-width chr))
304                       p-idx idx
305                       idx (+ idx (char-bytes chr))
306                       ))
307               (substring str 0 (if (= col width)
308                                    idx
309                                  p-idx))
310               )))
311       )
312     (defalias 'gnus-truncate-string 'truncate-string)
313
314     (fset 
315      'gnus-format-max-width 
316      (lambda (form length)
317        (let* ((val (eval form))
318               (valstr (if (numberp val) (int-to-string val) val)))
319          (if (> (length valstr) length)
320              (truncate-string valstr length)
321            valstr))))
322
323     (fset 'gnus-summary-make-display-table (lambda () nil))
324     )
325    ))
326
327 (provide 'gnus-ems)
328
329 ;; Local Variables:
330 ;; byte-compile-warnings: nil
331 ;; End:
332
333 ;;; gnus-ems.el ends here