*** empty log message ***
[gnus] / lisp / gnus-xmas.el
1 ;;; gnus-xmas.el --- Gnus functions for XEmacs
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 (require 'text-props)
28 (defvar menu-bar-mode t)
29
30 (defvar gnus-xmas-glyph-directory nil
31   "*Directory where Gnus logos and icons are located.
32 If this variable is nil, Gnus will try to locate the directory
33 automatically.")
34
35 ;;; Internal variables.
36
37 (defvar gnus-xmas-logo (make-glyph (make-specifier 'image)))
38
39 ;; Don't warn about these undefined variables.
40
41 (defvar gnus-group-mode-hook)
42 (defvar gnus-summary-mode-hook)
43 (defvar gnus-article-mode-hook)
44
45 ;;defined in gnus.el
46 (defvar gnus-active-hashtb)
47 (defvar gnus-article-buffer)
48 (defvar gnus-auto-center-summary)
49 (defvar gnus-buffer-list)
50 (defvar gnus-current-headers)
51 (defvar gnus-level-killed)
52 (defvar gnus-level-zombie)
53 (defvar gnus-newsgroup-bookmarks)
54 (defvar gnus-newsgroup-dependencies)
55 (defvar gnus-newsgroup-selected-overlay)
56 (defvar gnus-newsrc-hashtb)
57 (defvar gnus-read-mark)
58 (defvar gnus-refer-article-method)
59 (defvar gnus-reffed-article-number)
60 (defvar gnus-unread-mark)
61 (defvar gnus-version)
62 (defvar gnus-view-pseudos)
63 (defvar gnus-view-pseudos-separately)
64 (defvar gnus-visual)
65 (defvar gnus-zombie-list)
66 ;;defined in gnus-msg.el
67 (defvar gnus-article-copy)
68 (defvar gnus-check-before-posting)
69 ;;defined in gnus-vis.el
70 (defvar gnus-article-button-face)
71 (defvar gnus-article-mouse-face)
72 (defvar gnus-summary-selected-face)
73 (defvar gnus-group-reading-menu)
74 (defvar gnus-group-group-menu)
75 (defvar gnus-group-misc-menu)
76 (defvar gnus-summary-article-menu)
77 (defvar gnus-summary-thread-menu)
78 (defvar gnus-summary-misc-menu)
79 (defvar gnus-summary-post-menu)
80 (defvar gnus-summary-kill-menu)
81 (defvar gnus-article-article-menu)
82 (defvar gnus-article-treatment-menu)
83 (defvar gnus-mouse-2)
84 (defvar standard-display-table)
85
86 (defun gnus-xmas-set-text-properties (start end props &optional buffer)
87   "You should NEVER use this function.  It is ideologically blasphemous.
88 It is provided only to ease porting of broken FSF Emacs programs."
89   (if (and (stringp buffer) (not (setq buffer (get-buffer buffer))))
90       nil
91     (map-extents (lambda (extent ignored)
92                    (remove-text-properties 
93                     start end
94                     (list (extent-property extent 'text-prop) nil)
95                     buffer))
96                  buffer start end nil nil 'text-prop)
97     (add-text-properties start end props buffer)))
98
99 (defun gnus-xmas-highlight-selected-summary ()
100   ;; Highlight selected article in summary buffer
101   (if gnus-summary-selected-face
102       (progn
103         (if gnus-newsgroup-selected-overlay
104             (delete-extent gnus-newsgroup-selected-overlay))
105         (setq gnus-newsgroup-selected-overlay 
106               (make-extent (gnus-point-at-bol) (gnus-point-at-eol)))
107         (set-extent-face gnus-newsgroup-selected-overlay
108                          gnus-summary-selected-face))))
109
110 (defun gnus-xmas-summary-recenter ()
111   (let* ((top (cond ((< (window-height) 4) 0)
112                     ((< (window-height) 7) 1)
113                     (t 2)))
114          (height (- (window-height) 2))
115          (bottom (save-excursion (goto-char (point-max))
116                                  (forward-line (- height))
117                                  (point)))
118          (window (get-buffer-window (current-buffer))))
119     (and 
120      ;; The user has to want it,
121      gnus-auto-center-summary 
122      ;; the article buffer must be displayed,
123      (get-buffer-window gnus-article-buffer)
124      ;; Set the window start to either `bottom', which is the biggest
125      ;; possible valid number, or the second line from the top,
126      ;; whichever is the least.
127      (set-window-start
128       window (min bottom (save-excursion
129                            (forward-line (- top)) (point)))))))
130
131 (defun gnus-xmas-group-remove-excess-properties ()
132   (let ((end (point))
133         (beg (progn (forward-line -1) (point))))
134     (remove-text-properties (1+ beg) end '(gnus-group nil))
135     (remove-text-properties 
136      beg end 
137      '(gnus-topic nil gnus-topic-level nil gnus-topic-visible nil))
138     (goto-char end)))
139                   
140 (defun gnus-xmas-copy-article-buffer (&optional article-buffer)
141   (setq gnus-article-copy (get-buffer-create " *gnus article copy*"))
142   (buffer-disable-undo gnus-article-copy)
143   (or (memq gnus-article-copy gnus-buffer-list)
144       (setq gnus-buffer-list (cons gnus-article-copy gnus-buffer-list)))
145   (let ((article-buffer (or article-buffer gnus-article-buffer))
146         buf)
147     (if (and (get-buffer article-buffer)
148              (buffer-name (get-buffer article-buffer)))
149         (save-excursion
150           (set-buffer article-buffer)
151           (widen)
152           (setq buf (buffer-substring (point-min) (point-max)))
153           (set-buffer gnus-article-copy)
154           (erase-buffer)
155           (insert (format "%s" buf))))))
156
157 (defun gnus-xmas-article-push-button (event)
158   "Check text under the mouse pointer for a callback function.
159 If the text under the mouse pointer has a `gnus-callback' property,
160 call it with the value of the `gnus-data' text property."
161   (interactive "e")
162   (set-buffer (window-buffer (event-window event)))
163   (let* ((pos (event-closest-point event))
164          (data (get-text-property pos 'gnus-data))
165          (fun (get-text-property pos 'gnus-callback)))
166     (if fun (funcall fun data))))
167
168 (defun gnus-xmas-move-overlay (extent start end &optional buffer)
169   (set-extent-endpoints extent start end))
170
171 ;; Fixed by Christopher Davis <ckd@loiosh.kei.com>.
172 (defun gnus-xmas-article-add-button (from to fun &optional data)
173   "Create a button between FROM and TO with callback FUN and data DATA."
174   (and gnus-article-button-face
175        (gnus-overlay-put (gnus-make-overlay from to) 
176                          'face gnus-article-button-face))
177   (add-text-properties 
178    from to
179    (nconc
180     (and gnus-article-mouse-face
181          (list 'mouse-face gnus-article-mouse-face))
182     (list 'gnus-callback fun)
183     (and data (list 'gnus-data data))
184     (list 'highlight t))))
185
186 (defun gnus-xmas-window-top-edge (&optional window)
187   (nth 1 (window-pixel-edges window)))
188
189 ;; Select the lowest window on the frame.
190 (defun gnus-xmas-appt-select-lowest-window ()
191   (let* ((lowest-window (selected-window))
192          (bottom-edge (car (cdr (cdr (cdr (window-pixel-edges))))))
193          (last-window (previous-window))
194          (window-search t))
195     (while window-search
196       (let* ((this-window (next-window))
197              (next-bottom-edge (car (cdr (cdr (cdr 
198                                                (window-pixel-edges 
199                                                 this-window)))))))
200         (if (< bottom-edge next-bottom-edge)
201             (progn
202               (setq bottom-edge next-bottom-edge)
203               (setq lowest-window this-window)))
204
205         (select-window this-window)
206         (if (eq last-window this-window)
207             (progn
208               (select-window lowest-window)
209               (setq window-search nil)))))))
210
211 (defun gnus-xmas-group-menu-add ()
212   (easy-menu-add gnus-group-reading-menu)
213   (easy-menu-add gnus-group-group-menu)
214   (easy-menu-add gnus-group-misc-menu))
215
216 (defun gnus-xmas-summary-menu-add ()
217   (easy-menu-add gnus-summary-article-menu)
218   (easy-menu-add gnus-summary-thread-menu)
219   (easy-menu-add gnus-summary-misc-menu)
220   (easy-menu-add gnus-summary-post-menu)
221   (easy-menu-add gnus-summary-kill-menu)) 
222
223 (defun gnus-xmas-article-menu-add ()
224   (easy-menu-add gnus-article-article-menu)
225   (easy-menu-add gnus-article-treatment-menu))
226
227 (defun gnus-xmas-read-event-char ()
228   "Get the next event."
229   (let ((event (next-event)))
230     (while (timeout-event-p event)
231       (setq event (next-event)))
232     (cons (and (key-press-event-p event) 
233                (numberp (event-key event))
234                (event-to-character event)) 
235           event)))
236
237 (defun gnus-xmas-define ()
238   (setq gnus-mouse-2 [button2])
239
240   (or (memq 'underline (list-faces))
241       (and (fboundp 'make-face)
242            (funcall (intern "make-face") 'underline)))
243   ;; Must avoid calling set-face-underline-p directly, because it
244   ;; is a defsubst in emacs19, and will make the .elc files non
245   ;; portable!
246   (or (face-differs-from-default-p 'underline)
247       (funcall (intern "set-face-underline-p") 'underline t))
248
249   (fset 'gnus-make-overlay 'make-extent)
250   (fset 'gnus-overlay-put 'set-extent-property)
251   (fset 'gnus-move-overlay 'gnus-xmas-move-overlay)
252       
253   (fset 'set-text-properties 'gnus-xmas-set-text-properties)
254
255   (or (boundp 'standard-display-table) (setq standard-display-table nil))
256
257   (defvar gnus-mouse-face-prop 'highlight)
258
259   (defun gnus-byte-code (func)
260     "Return a form that can be `eval'ed based on FUNC."
261     (let ((fval (symbol-function func)))
262       (if (byte-code-function-p fval)
263           (list 'funcall fval)
264         (cons 'progn (cdr (cdr fval))))))
265       
266   ;; Fix by "jeff (j.d.) sparkes" <jsparkes@bnr.ca>.
267   (defvar gnus-display-type (device-class)
268     "A symbol indicating the display Emacs is running under.
269 The symbol should be one of `color', `grayscale' or `mono'. If Emacs
270 guesses this display attribute wrongly, either set this variable in
271 your `~/.emacs' or set the resource `Emacs.displayType' in your
272 `~/.Xdefaults'. See also `gnus-background-mode'.
273
274 This is a meta-variable that will affect what default values other
275 variables get.  You would normally not change this variable, but
276 pounce directly on the real variables themselves.")
277
278
279   (or (fboundp 'x-color-values)
280       (fset 'x-color-values 
281             (lambda (color)
282               (color-instance-rgb-components
283                (make-color-instance color)))))
284     
285   (defvar gnus-background-mode 
286     (let ((bg-resource 
287            (condition-case ()
288                (x-get-resource ".backgroundMode" "BackgroundMode" 'string)
289              (error nil)))
290           (params (frame-parameters)))
291       (cond (bg-resource (intern (downcase bg-resource)))
292             ((and (assq 'background-color params)
293                   (< (apply '+ (x-color-values
294                                 (cdr (assq 'background-color params))))
295                      (/ (apply '+ (x-color-values "white")) 3)))
296              'dark)
297             (t 'light)))
298     "A symbol indicating the Emacs background brightness.
299 The symbol should be one of `light' or `dark'.
300 If Emacs guesses this frame attribute wrongly, either set this variable in
301 your `~/.emacs' or set the resource `Emacs.backgroundMode' in your
302 `~/.Xdefaults'.
303 See also `gnus-display-type'.
304
305 This is a meta-variable that will affect what default values other
306 variables get.  You would normally not change this variable, but
307 pounce directly on the real variables themselves.")
308   )
309
310
311
312 (defun gnus-xmas-redefine ()
313   "Redefine lots of Gnus functions for XEmacs."
314   (fset 'gnus-summary-make-display-table (lambda () nil))
315   (fset 'gnus-visual-turn-off-edit-menu 'identity)
316   (fset 'gnus-highlight-selected-summary
317         'gnus-xmas-highlight-selected-summary)
318   (fset 'gnus-summary-recenter 'gnus-xmas-summary-recenter)
319   (fset 'gnus-group-remove-excess-properties
320         'gnus-xmas-group-remove-excess-properties)
321   (fset 'gnus-copy-article-buffer 'gnus-xmas-copy-article-buffer)
322   (fset 'gnus-article-push-button 'gnus-xmas-article-push-button)
323   (fset 'gnus-article-add-button 'gnus-xmas-article-add-button)
324   (fset 'gnus-window-top-edge 'gnus-xmas-window-top-edge)
325   (fset 'set-text-properties 'gnus-xmas-set-text-properties)
326   (fset 'gnus-read-event-char 'gnus-xmas-read-event-char)
327   (fset 'gnus-group-startup-message 'gnus-xmas-group-startup-message)
328
329   (or (fboundp 'appt-select-lowest-window)
330       (fset 'appt-select-lowest-window 
331             'gnus-xmas-appt-select-lowest-window))
332
333   (add-hook 'gnus-group-mode-hook 'gnus-xmas-group-menu-add)
334   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
335   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add)
336
337   (add-hook 'gnus-group-mode-hook 'gnus-xmas-setup-group-toolbar)
338   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar))
339
340
341 ;;; XEmacs logo and toolbar.
342
343 (defun gnus-xmas-find-glyph-directory ()
344   (or gnus-xmas-glyph-directory ; We have a dir already...
345       (let ((path load-path)
346             dir)
347         ;; We try to find the dir by looking at the load path,
348         ;; stripping away the last component and adding "etc/".
349         (while path
350           (setq dir (concat
351                      (file-name-directory (directory-file-name (car path)))
352                      "etc/"))
353           (if (and (file-exists-p dir)
354                    (file-directory-p dir)
355                    (file-exists-p (concat dir "gnus-group-exit-icon-up.xpm")))
356               (setq gnus-xmas-glyph-directory dir
357                     path nil)
358             (setq path (cdr path))))
359         gnus-xmas-glyph-directory)))
360
361 (defun gnus-xmas-group-startup-message (&optional x y)
362   "Insert startup message in current buffer."
363   ;; Insert the message.
364   (gnus-xmas-find-glyph-directory)
365   (erase-buffer)
366   (let ((file (and gnus-xmas-glyph-directory
367                    (concat 
368                     (file-name-as-directory gnus-xmas-glyph-directory)
369                     "gnus.xpm"))))
370     (if (and (featurep 'xpm)
371              file (file-exists-p file))
372         (progn
373           (set-glyph-property gnus-xmas-logo 'image file)
374           (set-glyph-image gnus-xmas-logo file 'global 'x)
375
376           (insert " ")
377           (set-extent-begin-glyph (make-extent (point) (point)) gnus-xmas-logo)
378           (goto-char (point-min))
379           (while (not (eobp))
380             (insert (make-string (/ (max (- (window-width) (or x 35)) 0) 2)
381                                  ? ))
382             (forward-line 1))
383           (goto-char (point-min))
384           (let* ((pheight (+ 20 (count-lines (point-min) (point-max))))
385                  (wheight (window-height))
386                  (rest (- wheight pheight)))
387             (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n))))
388
389       (insert
390        (format "              %s
391           _    ___ _             _      
392           _ ___ __ ___  __    _ ___     
393           __   _     ___    __  ___     
394               _           ___     _     
395              _  _ __             _      
396              ___   __            _      
397                    __           _       
398                     _      _   _        
399                    _      _    _        
400                       _  _    _         
401                   __  ___               
402                  _   _ _     _          
403                 _   _                   
404               _    _                    
405              _    _                     
406             _                         
407           __                             
408
409
410                ""))
411       ;; And then hack it.
412       (gnus-indent-rigidly (point-min) (point-max) 
413                            (/ (max (- (window-width) (or x 46)) 0) 2))
414       (goto-char (point-min))
415       (forward-line 1)
416       (let* ((pheight (count-lines (point-min) (point-max)))
417              (wheight (window-height))
418              (rest (- wheight pheight)))
419         (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n))))
420     ;; Fontify some.
421     (goto-char (point-min))
422     (and (search-forward "Praxis" nil t)
423          (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
424     (goto-char (point-min))
425     (let* ((mode-string (gnus-group-set-mode-line)))
426       (setq mode-line-buffer-identification 
427             (list (concat gnus-version (substring (car mode-string) 4))))
428       (set-buffer-modified-p t))))
429
430
431 ;;; The toolbar.
432
433 (defvar gnus-use-toolbar 'default-toolbar
434   "*If nil, do not use a toolbar.
435 If it is non-nil, it must be a toolbar.  The five legal values are
436 `default-toolbar', `top-toolbar', `bottom-toolbar',
437 `right-toolbar', and `left-toolbar'.")
438
439 (defvar gnus-group-toolbar 
440   '([gnus-group-exit-icon gnus-group-exit t "Exit Gnus"]
441     [gnus-group-kill-group-icon gnus-group-kill-group t "Kill group"]
442     [gnus-group-get-new-news-icon gnus-group-get-new-news t "Get new news"]
443     [gnus-group-get-new-news-this-group-icon 
444      gnus-group-get-new-news-this-group t "Get new news in this group"]
445     [gnus-group-catchup-current-icon 
446      gnus-group-catchup-current t "Catchup group"]
447     [gnus-group-describe-group-icon 
448      gnus-group-describe-group t "Describe group"])
449   "The group buffer toolbar.")
450
451 (defvar gnus-summary-toolbar 
452   '([gnus-summary-post-news-icon 
453      gnus-summary-post-news t "Post an article"]
454     [gnus-summary-save-article-file-icon
455      gnus-summary-save-article-file t "Save article in file"]
456     [gnus-summary-save-article-icon
457      gnus-summary-save-article t "Save article"]
458     [gnus-summary-reply-icon 
459      gnus-summary-reply t "Mail a reply"]
460     [gnus-summary-reply-with-original-icon
461      gnus-summary-reply-with-original t "Mail a reply and yank the original"]
462     [gnus-summary-followup-icon 
463      gnus-summary-followup t "Post a followup"]
464     [gnus-summary-followup-with-original-icon
465      gnus-summary-followup-with-original t 
466      "Post a followup and yank the original"]
467     [gnus-uu-decode-uu-icon
468      gnus-uu-decode-uu t "Decode uuencoded articles"]
469     [gnus-uu-post-news-icon 
470      gnus-uu-post-news t "Post an uuencoded article"]
471     [gnus-summary-caesar-message-icon
472      gnus-summary-caesar-message t "Rot 13"]
473     [gnus-summary-cancel-article-icon
474      gnus-summary-cancel-article t "Cancel article"])
475   "The summary buffer toolbar.")
476
477 (defun gnus-xmas-setup-toolbar (bar &optional force)
478   (let ((dir (gnus-xmas-find-glyph-directory))
479         icon up down disabled name)
480     (if (not dir)
481         ()
482       (if (and (not force)
483                (boundp (aref (car bar) 0)))
484           dir
485         (while bar
486           (setq icon (aref (car bar) 0)
487                 name (symbol-name icon)
488                 bar (cdr bar))
489           (setq up (concat dir name "-up.xpm"))
490           (setq down (concat dir name "-down.xpm"))
491           (setq disabled (concat dir name "-disabled.xpm"))
492           (if (not (file-exists-p up))
493               (set icon nil)
494             (set icon (toolbar-make-button-list
495                        up (and (file-exists-p down) down)
496                        (and (file-exists-p disabled) disabled)))))
497         dir))))
498
499 (defun gnus-xmas-setup-group-toolbar ()
500   (and gnus-use-toolbar
501        (gnus-xmas-setup-toolbar gnus-group-toolbar)
502        (set-specifier (symbol-value gnus-use-toolbar)
503                       (cons (current-buffer) gnus-group-toolbar))))
504
505 (defun gnus-xmas-setup-summary-toolbar ()
506   (and gnus-use-toolbar
507        (gnus-xmas-setup-toolbar gnus-summary-toolbar)
508        (set-specifier (symbol-value gnus-use-toolbar)
509                       (cons (current-buffer) gnus-summary-toolbar))))
510
511 ;;; gnus-xmas.el ends here