T-gnus 6.14.3.
[elisp/gnus.git-] / lisp / gnus-xmas.el
1 ;;; gnus-xmas.el --- Gnus functions for XEmacs
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
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 (require 'text-props)
30 (defvar menu-bar-mode (featurep 'menubar))
31 (require 'messagexmas)
32 (require 'wid-edit)
33
34 (defgroup gnus-xmas nil
35   "XEmacsoid support for Gnus"
36   :group 'gnus)
37
38 (defcustom gnus-xmas-glyph-directory nil
39   "Directory where Gnus logos and icons are located.
40 If this variable is nil, Gnus will try to locate the directory
41 automatically."
42   :type '(choice (const :tag "autodetect" nil)
43                  directory)
44   :group 'gnus-xmas)
45
46 ;;(format "%02x%02x%02x" 114 66 20) "724214"
47
48 (defvar gnus-xmas-logo-color-alist
49   '((flame "#cc3300" "#ff2200")
50     (pine "#c0cc93" "#f8ffb8")
51     (moss "#a1cc93" "#d2ffb8")
52     (irish "#04cc90" "#05ff97")
53     (sky "#049acc" "#05deff")
54     (tin "#6886cc" "#82b6ff")
55     (velvet "#7c68cc" "#8c82ff")
56     (grape "#b264cc" "#cf7df")
57     (labia "#cc64c2" "#fd7dff")
58     (berry "#cc6485" "#ff7db5")
59     (dino "#724214" "#1e3f03")
60     (neutral "#b4b4b4" "#878787")
61     (september "#bf9900" "#ffcc00"))
62   "Color alist used for the Gnus logo.")
63
64 (defcustom gnus-xmas-logo-color-style 'dino
65   "*Color styles used for the Gnus logo."
66   :type '(choice (const flame) (const pine) (const moss)
67                  (const irish) (const sky) (const tin)
68                  (const velvet) (const grape) (const labia)
69                  (const berry) (const neutral) (const september)
70                  (const dino))
71   :group 'gnus-xmas)
72
73 (defvar gnus-xmas-logo-colors
74   (cdr (assq gnus-xmas-logo-color-style gnus-xmas-logo-color-alist))
75   "Colors used for the Gnus logo.")
76
77 (defcustom gnus-article-x-face-command
78   (if (or (featurep 'xface)
79           (featurep 'xpm))
80       'gnus-xmas-article-display-xface
81     "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
82   "*String or function to be executed to display an X-Face header.
83 If it is a string, the command will be executed in a sub-shell
84 asynchronously.  The compressed face will be piped to this command."
85   :type '(choice string function))
86
87 ;;; Internal variables.
88
89 ;; Don't warn about these undefined variables.
90
91 (defvar gnus-group-mode-hook)
92 (defvar gnus-summary-mode-hook)
93 (defvar gnus-article-mode-hook)
94
95 ;;defined in gnus.el
96 (defvar gnus-active-hashtb)
97 (defvar gnus-article-buffer)
98 (defvar gnus-auto-center-summary)
99 (defvar gnus-current-headers)
100 (defvar gnus-level-killed)
101 (defvar gnus-level-zombie)
102 (defvar gnus-newsgroup-bookmarks)
103 (defvar gnus-newsgroup-dependencies)
104 (defvar gnus-newsgroup-selected-overlay)
105 (defvar gnus-newsrc-hashtb)
106 (defvar gnus-read-mark)
107 (defvar gnus-refer-article-method)
108 (defvar gnus-reffed-article-number)
109 (defvar gnus-unread-mark)
110 (defvar gnus-version)
111 (defvar gnus-view-pseudos)
112 (defvar gnus-view-pseudos-separately)
113 (defvar gnus-visual)
114 (defvar gnus-zombie-list)
115 ;;defined in gnus-msg.el
116 (defvar gnus-article-copy)
117 (defvar gnus-check-before-posting)
118 ;;defined in gnus-vis.el
119 (defvar gnus-article-button-face)
120 (defvar gnus-article-mouse-face)
121 (defvar gnus-summary-selected-face)
122 (defvar gnus-group-reading-menu)
123 (defvar gnus-group-group-menu)
124 (defvar gnus-group-misc-menu)
125 (defvar gnus-summary-article-menu)
126 (defvar gnus-summary-thread-menu)
127 (defvar gnus-summary-misc-menu)
128 (defvar gnus-summary-post-menu)
129 (defvar gnus-summary-kill-menu)
130 (defvar gnus-article-article-menu)
131 (defvar gnus-article-treatment-menu)
132 (defvar gnus-mouse-2)
133 (defvar standard-display-table)
134 (defvar gnus-tree-minimize-window)
135
136 (defun gnus-xmas-set-text-properties (start end props &optional buffer)
137   "You should NEVER use this function.  It is ideologically blasphemous.
138 It is provided only to ease porting of broken FSF Emacs programs."
139   (if (stringp buffer)
140       nil
141     (map-extents (lambda (extent ignored)
142                    (remove-text-properties
143                     start end
144                     (list (extent-property extent 'text-prop) nil)
145                     buffer)
146                    nil)
147                  buffer start end nil nil 'text-prop)
148     (gnus-add-text-properties start end props buffer)))
149
150 (defun gnus-xmas-highlight-selected-summary ()
151   ;; Highlight selected article in summary buffer
152   (when gnus-summary-selected-face
153     (when gnus-newsgroup-selected-overlay
154       (delete-extent gnus-newsgroup-selected-overlay))
155     (setq gnus-newsgroup-selected-overlay
156           (make-extent (gnus-point-at-bol) (gnus-point-at-eol)))
157     (set-extent-face gnus-newsgroup-selected-overlay
158                      gnus-summary-selected-face)))
159
160 (defcustom gnus-xmas-force-redisplay nil
161   "*If non-nil, force a redisplay before recentering the summary buffer.
162 This is ugly, but it works around a bug in `window-displayed-height'."
163   :type 'boolean
164   :group 'gnus-xmas)
165
166 (defun gnus-xmas-switch-horizontal-scrollbar-off ()
167   (when (featurep 'scrollbar)
168     (set-specifier scrollbar-height (cons (current-buffer) 0))))
169
170 (defun gnus-xmas-summary-recenter ()
171   "\"Center\" point in the summary window.
172 If `gnus-auto-center-summary' is nil, or the article buffer isn't
173 displayed, no centering will be performed."
174   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
175   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
176   ;; Force redisplay to get properly computed window height.
177   (when gnus-xmas-force-redisplay
178     (sit-for 0))
179   (when gnus-auto-center-summary
180     (let* ((height (if (fboundp 'window-displayed-height)
181                        (window-displayed-height)
182                      (- (window-height) 2)))
183            (top (cond ((< height 4) 0)
184                       ((< height 7) 1)
185                       (t (if (numberp gnus-auto-center-summary)
186                              gnus-auto-center-summary
187                            2))))
188            (bottom (save-excursion (goto-char (point-max))
189                                    (forward-line (- height))
190                                    (point)))
191            (window (get-buffer-window (current-buffer))))
192       (when (get-buffer-window gnus-article-buffer)
193         ;; Only do recentering when the article buffer is displayed,
194         ;; Set the window start to either `bottom', which is the biggest
195         ;; possible valid number, or the second line from the top,
196         ;; whichever is the least.
197         ;; NOFORCE parameter suggested by Daniel Pittman <daniel@danann.net>.
198         (set-window-start
199          window (min bottom (save-excursion (forward-line (- top)) (point))) 
200          t))
201       ;; Do horizontal recentering while we're at it.
202       (when (and (get-buffer-window (current-buffer) t)
203                  (not (eq gnus-auto-center-summary 'vertical)))
204         (let ((selected (selected-window)))
205           (select-window (get-buffer-window (current-buffer) t))
206           (gnus-summary-position-point)
207           (gnus-horizontal-recenter)
208           (select-window selected))))))
209
210 (defun gnus-xmas-summary-set-display-table ()
211   ;; Setup the display table -- like `gnus-summary-setup-display-table',
212   ;; but done in an XEmacsish way.
213   (let ((table (make-display-table))
214         (i 32))
215     ;; Nix out all the control chars...
216     (while (>= (setq i (1- i)) 0)
217       (aset table i [??]))
218     ;; ... but not newline and cr, of course.  (cr is necessary for the
219     ;; selective display).
220     (aset table ?\n nil)
221     (aset table ?\r nil)
222     ;; We keep TAB as well.
223     (aset table ?\t nil)
224     ;; We nix out any glyphs over 126 below ctl-arrow.
225     (let ((i (if (integerp ctl-arrow) ctl-arrow 160)))
226       (while (>= (setq i (1- i)) 127)
227         (unless (aref table i)
228           (aset table i [??]))))
229     ;; Can't use `set-specifier' because of a bug in 19.14 and earlier
230     (add-spec-to-specifier current-display-table table (current-buffer) nil)))
231
232 (defun gnus-xmas-add-text-properties (start end props &optional object)
233   (add-text-properties start end props object)
234   (put-text-property start end 'start-closed nil object))
235
236 (defun gnus-xmas-put-text-property (start end prop value &optional object)
237   (put-text-property start end prop value object)
238   (put-text-property start end 'start-closed nil object))
239
240 (defun gnus-xmas-extent-start-open (point)
241   (map-extents (lambda (extent arg)
242                  (set-extent-property extent 'start-open t))
243                nil point (min (1+ (point)) (point-max))))
244
245 (defun gnus-xmas-article-push-button (event)
246   "Check text under the mouse pointer for a callback function.
247 If the text under the mouse pointer has a `gnus-callback' property,
248 call it with the value of the `gnus-data' text property."
249   (interactive "e")
250   (set-buffer (window-buffer (event-window event)))
251   (let* ((pos (event-closest-point event))
252          (data (get-text-property pos 'gnus-data))
253          (fun (get-text-property pos 'gnus-callback)))
254     (goto-char pos)
255     (when fun
256       (funcall fun data))))
257
258 (defun gnus-xmas-move-overlay (extent start end &optional buffer)
259   (set-extent-endpoints extent start end buffer))
260
261 (defun gnus-xmas-kill-all-overlays ()
262   "Delete all extents in the current buffer."
263   (map-extents (lambda (extent ignore)
264                  (delete-extent extent)
265                  nil)))
266
267 (defun gnus-xmas-window-top-edge (&optional window)
268   (nth 1 (window-pixel-edges window)))
269
270 (defun gnus-xmas-tree-minimize ()
271   (when (and gnus-tree-minimize-window
272              (not (one-window-p)))
273     (let* ((window-min-height 2)
274            (height (1+ (count-lines (point-min) (point-max))))
275            (min (max (1- window-min-height) height))
276            (tot (if (numberp gnus-tree-minimize-window)
277                     (min gnus-tree-minimize-window min)
278                   min))
279            (win (get-buffer-window (current-buffer)))
280            (wh (and win (1- (window-height win)))))
281       (when (and win
282                  (not (eq tot wh)))
283         (let ((selected (selected-window)))
284           (select-window win)
285           (enlarge-window (- tot wh))
286           (select-window selected))))))
287
288 ;; Select the lowest window on the frame.
289 (defun gnus-xmas-appt-select-lowest-window ()
290   (let* ((lowest-window (selected-window))
291          (bottom-edge (car (cdr (cdr (cdr (window-pixel-edges))))))
292          (last-window (previous-window))
293          (window-search t))
294     (while window-search
295       (let* ((this-window (next-window))
296              (next-bottom-edge (car (cdr (cdr (cdr
297                                                (window-pixel-edges
298                                                 this-window)))))))
299         (when (< bottom-edge next-bottom-edge)
300           (setq bottom-edge next-bottom-edge)
301           (setq lowest-window this-window))
302
303         (select-window this-window)
304         (when (eq last-window this-window)
305           (select-window lowest-window)
306           (setq window-search nil))))))
307
308 (defmacro gnus-xmas-menu-add (type &rest menus)
309   `(gnus-xmas-menu-add-1 ',type ',menus))
310 (put 'gnus-xmas-menu-add 'lisp-indent-function 1)
311
312 (defun gnus-xmas-menu-add-1 (type menus)
313   (when (and menu-bar-mode
314              (gnus-visual-p (intern (format "%s-menu" type)) 'menu))
315     (while menus
316       (easy-menu-add (symbol-value (pop menus))))))
317
318 (defun gnus-xmas-group-menu-add ()
319   (gnus-xmas-menu-add group
320     gnus-group-reading-menu gnus-group-group-menu gnus-group-misc-menu))
321
322 (defun gnus-xmas-summary-menu-add ()
323   (gnus-xmas-menu-add summary
324     gnus-summary-misc-menu gnus-summary-kill-menu
325     gnus-summary-article-menu gnus-summary-thread-menu
326     gnus-summary-post-menu ))
327
328 (defun gnus-xmas-article-menu-add ()
329   (gnus-xmas-menu-add article
330     gnus-article-article-menu gnus-article-treatment-menu))
331
332 (defun gnus-xmas-score-menu-add ()
333   (gnus-xmas-menu-add score
334     gnus-score-menu))
335
336 (defun gnus-xmas-pick-menu-add ()
337   (gnus-xmas-menu-add pick
338     gnus-pick-menu))
339
340 (defun gnus-xmas-topic-menu-add ()
341   (gnus-xmas-menu-add topic
342     gnus-topic-menu))
343
344 (defun gnus-xmas-binary-menu-add ()
345   (gnus-xmas-menu-add binary
346     gnus-binary-menu))
347
348 (defun gnus-xmas-agent-summary-menu-add ()
349   (gnus-xmas-menu-add agent-summary
350     gnus-agent-summary-menu))
351
352 (defun gnus-xmas-agent-group-menu-add ()
353   (gnus-xmas-menu-add agent-group
354     gnus-agent-group-menu))
355
356 (defun gnus-xmas-agent-server-menu-add ()
357   (gnus-xmas-menu-add agent-server
358     gnus-agent-server-menu))
359
360 (defun gnus-xmas-tree-menu-add ()
361   (gnus-xmas-menu-add tree
362     gnus-tree-menu))
363
364 (defun gnus-xmas-draft-menu-add ()
365   (gnus-xmas-menu-add draft
366     gnus-draft-menu))
367
368 (defun gnus-xmas-server-menu-add ()
369   (gnus-xmas-menu-add menu
370     gnus-server-server-menu gnus-server-connections-menu))
371
372 (defun gnus-xmas-browse-menu-add ()
373   (gnus-xmas-menu-add browse
374     gnus-browse-menu))
375
376 (defun gnus-xmas-grouplens-menu-add ()
377   (gnus-xmas-menu-add grouplens
378     gnus-grouplens-menu))
379
380 (defun gnus-xmas-read-event-char ()
381   "Get the next event."
382   (let ((event (next-command-event)))
383     (sit-for 0)
384     ;; We junk all non-key events.  Is this naughty?
385     (while (not (or (key-press-event-p event)
386                     (button-press-event-p event)))
387       (dispatch-event event)
388       (setq event (next-command-event)))
389     (cons (and (key-press-event-p event)
390                (event-to-character event))
391           event)))
392
393 (defun gnus-xmas-define ()
394   (setq gnus-mouse-2 [button2])
395   (setq gnus-mouse-3 [button3])
396   (setq gnus-widget-button-keymap widget-button-keymap)
397
398   (unless (memq 'underline (face-list))
399     (and (fboundp 'make-face)
400          (funcall (intern "make-face") 'underline)))
401   ;; Must avoid calling set-face-underline-p directly, because it
402   ;; is a defsubst in emacs19, and will make the .elc files non
403   ;; portable!
404   (unless (face-differs-from-default-p 'underline)
405     (funcall (intern "set-face-underline-p") 'underline t))
406
407   (cond
408    ((fboundp 'char-or-char-int-p)
409     ;; Handle both types of marks for XEmacs-20.x.
410     (fset 'gnus-characterp 'char-or-char-int-p))
411    ;; V19 of XEmacs, probably.
412    (t
413     (fset 'gnus-characterp 'characterp)))
414
415   (fset 'gnus-make-overlay 'make-extent)
416   (fset 'gnus-delete-overlay 'delete-extent)
417   (fset 'gnus-overlay-put 'set-extent-property)
418   (fset 'gnus-move-overlay 'gnus-xmas-move-overlay)
419   (fset 'gnus-overlay-end 'extent-end-position)
420   (fset 'gnus-kill-all-overlays 'gnus-xmas-kill-all-overlays)
421   (fset 'gnus-extent-detached-p 'extent-detached-p)
422   (fset 'gnus-add-text-properties 'gnus-xmas-add-text-properties)
423   (fset 'gnus-put-text-property 'gnus-xmas-put-text-property)
424   (fset 'gnus-deactivate-mark 'ignore)
425   (fset 'gnus-window-edges 'window-pixel-edges)
426
427   (if (and (<= emacs-major-version 19)
428            (< emacs-minor-version 14))
429       (fset 'gnus-set-text-properties 'gnus-xmas-set-text-properties))
430
431   (when (fboundp 'turn-off-scroll-in-place)
432     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
433
434   (unless (boundp 'standard-display-table)
435     (setq standard-display-table nil))
436
437   (defvar gnus-mouse-face-prop 'highlight)
438
439   (defun gnus-byte-code (func)
440     "Return a form that can be `eval'ed based on FUNC."
441     (let ((fval (indirect-function func)))
442       (if (compiled-function-p fval)
443           (list 'funcall fval)
444         (cons 'progn (cdr (cdr fval))))))
445
446   (fset 'gnus-x-color-values
447         (if (fboundp 'x-color-values)
448             'x-color-values
449           (lambda (color)
450             (color-instance-rgb-components
451              (make-color-instance color))))))
452
453 (defun gnus-xmas-redefine ()
454   "Redefine lots of Gnus functions for XEmacs."
455   (fset 'gnus-summary-set-display-table 'gnus-xmas-summary-set-display-table)
456   (fset 'gnus-visual-turn-off-edit-menu 'identity)
457   (fset 'gnus-summary-recenter 'gnus-xmas-summary-recenter)
458   (fset 'gnus-extent-start-open 'gnus-xmas-extent-start-open)
459   (fset 'gnus-article-push-button 'gnus-xmas-article-push-button)
460   (fset 'gnus-window-top-edge 'gnus-xmas-window-top-edge)
461   (fset 'gnus-read-event-char 'gnus-xmas-read-event-char)
462   (fset 'gnus-group-startup-message 'gnus-xmas-group-startup-message)
463   (fset 'gnus-tree-minimize 'gnus-xmas-tree-minimize)
464   (fset 'gnus-appt-select-lowest-window
465         'gnus-xmas-appt-select-lowest-window)
466   (fset 'gnus-mail-strip-quoted-names 'gnus-xmas-mail-strip-quoted-names)
467   (fset 'gnus-character-to-event 'character-to-event)
468   (fset 'gnus-mode-line-buffer-identification
469         'gnus-xmas-mode-line-buffer-identification)
470   (fset 'gnus-key-press-event-p 'key-press-event-p)
471   (fset 'gnus-region-active-p 'region-active-p)
472   (fset 'gnus-annotation-in-region-p 'gnus-xmas-annotation-in-region-p)
473   (fset 'gnus-mime-button-menu 'gnus-xmas-mime-button-menu)
474
475   (add-hook 'gnus-group-mode-hook 'gnus-xmas-group-menu-add)
476   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
477   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add)
478   (add-hook 'gnus-score-mode-hook 'gnus-xmas-score-menu-add)
479
480   (add-hook 'gnus-pick-mode-hook 'gnus-xmas-pick-menu-add)
481   (add-hook 'gnus-topic-mode-hook 'gnus-xmas-topic-menu-add)
482   (add-hook 'gnus-tree-mode-hook 'gnus-xmas-tree-menu-add)
483   (add-hook 'gnus-binary-mode-hook 'gnus-xmas-binary-menu-add)
484   (add-hook 'gnus-grouplens-mode-hook 'gnus-xmas-grouplens-menu-add)
485   (add-hook 'gnus-server-mode-hook 'gnus-xmas-server-menu-add)
486   (add-hook 'gnus-browse-mode-hook 'gnus-xmas-browse-menu-add)
487
488   (add-hook 'gnus-group-mode-hook 'gnus-xmas-setup-group-toolbar)
489   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
490
491   (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add)
492   (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add)
493   (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add)
494
495   (add-hook 'gnus-draft-mode-hook 'gnus-xmas-draft-menu-add)
496   (add-hook 'gnus-summary-mode-hook
497             'gnus-xmas-switch-horizontal-scrollbar-off)
498   (add-hook 'gnus-tree-mode-hook 'gnus-xmas-switch-horizontal-scrollbar-off)
499
500   (when (featurep 'mule)
501     (defun gnus-truncate-string (str end-column &optional start-column padding)
502       "Truncate string STR to end at column END-COLUMN.
503 The optional 2nd arg START-COLUMN, if non-nil, specifies
504 the starting column; that means to return the characters occupying
505 columns START-COLUMN ... END-COLUMN of STR.
506
507 The optional 3rd arg PADDING, if non-nil, specifies a padding character
508 to add at the end of the result if STR doesn't reach column END-COLUMN,
509 or if END-COLUMN comes in the middle of a character in STR.
510 PADDING is also added at the beginning of the result
511 if column START-COLUMN appears in the middle of a character in STR.
512
513 If PADDING is nil, no padding is added in these cases, so
514 the resulting string may be narrower than END-COLUMN.
515 \[Emacs 20.3 emulating function]"
516       (or start-column
517           (setq start-column 0))
518       (let ((len (length str))
519             (idx 0)
520             (column 0)
521             (head-padding "") (tail-padding "")
522             ch last-column last-idx from-idx)
523         (condition-case nil
524             (while (< column start-column)
525               (setq ch (aref str idx)
526                     column (+ column (char-width ch))
527                     idx (1+ idx)))
528           (args-out-of-range (setq idx len)))
529         (if (< column start-column)
530             (if padding (make-string end-column padding) "")
531           (if (and padding (> column start-column))
532               (setq head-padding
533                     (make-string (- column start-column) padding)))
534           (setq from-idx idx)
535           (if (< end-column column)
536               (setq idx from-idx)
537             (condition-case nil
538                 (while (< column end-column)
539                   (setq last-column column
540                         last-idx idx
541                         ch (aref str idx)
542                         column (+ column (char-width ch))
543                         idx (1+ idx)))
544               (args-out-of-range (setq idx len)))
545             (if (> column end-column)
546                 (setq column last-column idx last-idx))
547             (if (and padding (< column end-column))
548                 (setq tail-padding
549                       (make-string (- end-column column) padding))))
550           (setq str (substring str from-idx idx))
551           (if padding
552               (concat head-padding str tail-padding)
553             str))))
554
555     (defun gnus-tilde-pad-form (el pad-width)
556       "Return a form that pads EL to PAD-WIDTH."
557       (let ((pad (abs pad-width)))
558         (if (symbolp el)
559             (if (< pad-width 0)
560                 `(let ((val (format "%s" ,el)))
561                    (concat val (make-string
562                                 (max 0 (- ,pad (string-width val))) ?\ )))
563               `(let ((val (format "%s" ,el)))
564                  (concat (make-string
565                           (max 0 (- ,pad (string-width val))) ?\ )
566                          val)))
567           (if (< pad-width 0)
568               `(let ((val (eval ,el)))
569                  (concat val (make-string
570                               (max 0 (- ,pad (string-width val))) ?\ )))
571             `(let ((val (eval ,el)))
572                (concat (make-string
573                         (max 0 (- ,pad (string-width val))) ?\ )
574                        val))))))
575     ))
576
577 ;;; XEmacs logo and toolbar.
578
579 (defun gnus-xmas-group-startup-message (&optional x y)
580   "Insert startup message in current buffer."
581   ;; Insert the message.
582   (setq gnus-xmas-glyph-directory (message-xmas-find-glyph-directory "gnus"))
583   (erase-buffer)
584   (cond
585    ((and (console-on-window-system-p)
586          (or (featurep 'xpm)
587              (featurep 'xbm)))
588     (let* ((logo-xpm (expand-file-name "gnus.xpm" gnus-xmas-glyph-directory))
589            (logo-xbm (expand-file-name "gnus.xbm" gnus-xmas-glyph-directory))
590            (glyph (make-glyph
591                    (cond ((featurep 'xpm)
592                           `[xpm
593                             :file ,logo-xpm
594                             :color-symbols
595                             (("thing" . ,(car gnus-xmas-logo-colors))
596                              ("shadow" . ,(cadr gnus-xmas-logo-colors))
597                              ("background" . ,(face-background 'default)))])
598                          ((featurep 'xbm)
599                           `[xbm :file ,logo-xbm])
600                          (t [nothing]))))
601            (wpheight (window-pixel-height))
602            (rest (max 0 (1- (/ (* (- wpheight (glyph-height glyph))
603                                   (window-height))
604                                wpheight 2)))))
605       (insert " ")
606       (set-extent-begin-glyph (make-extent (point) (point)) glyph)
607       (goto-char (point-min))
608       (while (not (eobp))
609         (insert (make-string (/ (max (- (window-width) (or x 35)) 0) 2)
610                              ?\ ))
611         (forward-line 1))
612       (setq gnus-simple-splash nil)
613       (goto-char (point-min))
614       (insert gnus-product-name " " gnus-version-number
615               (if (zerop (string-to-number gnus-revision-number))
616                   ""
617                 (concat " (r" gnus-revision-number ")"))
618               " based on " gnus-original-product-name " v"
619               gnus-original-version-number "\n")
620       (goto-char (point-min))
621       (put-text-property (point) (gnus-point-at-eol) 'face 'gnus-splash-face)
622       (insert-char ?\ ; space
623                    (max 0 (/ (- (window-width) (gnus-point-at-eol)) 2)))
624       (forward-line 1)
625       (insert-char ?\n rest)
626       (set-window-start (selected-window) (point-min))))
627    (t
628     (insert "
629           _    ___ _             _
630           _ ___ __ ___  __    _ ___
631           __   _     ___    __  ___
632               _           ___     _
633              _  _ __             _
634              ___   __            _
635                    __           _
636                     _      _   _
637                    _      _    _
638                       _  _    _
639                   __  ___
640                  _   _ _     _
641                 _   _
642               _    _
643              _    _
644             _
645           __
646
647 "
648             )
649     (goto-char (point-min))
650     (insert gnus-product-name " " gnus-version-number
651             (if (zerop (string-to-number gnus-revision-number))
652                 ""
653               (concat " (r" gnus-revision-number ")"))
654             " based on " gnus-original-product-name " v"
655             gnus-original-version-number)
656     (goto-char (point-min))
657     (insert-char ?\ ; space
658                  (max 0 (/ (- (window-width) (gnus-point-at-eol)) 2)))
659     (forward-line 1)
660     ;; And then hack it.
661     (gnus-indent-rigidly (point) (point-max)
662                          (/ (max (- (window-width) (or x 46)) 0) 2))
663     (goto-char (point-min))
664     (forward-line 1)
665     (let* ((pheight (count-lines (point-min) (point-max)))
666            (wheight (window-height))
667            (rest (- wheight pheight)))
668       (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
669     ;; Paint it.
670     (put-text-property (point-min) (point-max) 'face 'gnus-splash-face)))
671   (setq modeline-buffer-identification
672         (list (concat gnus-version ": *Group*")))
673   (set-buffer-modified-p t))
674
675
676 ;;; The toolbar.
677
678 (defcustom gnus-use-toolbar (if (featurep 'toolbar)
679                                 'default-toolbar
680                               nil)
681   "*If nil, do not use a toolbar.
682 If it is non-nil, it must be a toolbar.  The five valid values are
683 `default-toolbar', `top-toolbar', `bottom-toolbar',
684 `right-toolbar', and `left-toolbar'."
685   :type '(choice (const default-toolbar)
686                  (const top-toolbar) (const bottom-toolbar)
687                  (const left-toolbar) (const right-toolbar)
688                  (const :tag "no toolbar" nil))
689   :group 'gnus-xmas)
690
691 (defvar gnus-group-toolbar
692   '([gnus-group-get-new-news gnus-group-get-new-news t "Get new news"]
693     [gnus-group-get-new-news-this-group
694      gnus-group-get-new-news-this-group t "Get new news in this group"]
695     [gnus-group-catchup-current
696      gnus-group-catchup-current t "Catchup group"]
697     [gnus-group-describe-group
698      gnus-group-describe-group t "Describe group"]
699     [gnus-group-unsubscribe gnus-group-unsubscribe t "Unsubscribe group"]
700     [gnus-group-subscribe gnus-group-subscribe t "Subscribe group"]
701     [gnus-group-kill-group gnus-group-kill-group t "Kill group"]
702     [gnus-group-exit gnus-group-exit t "Exit Gnus"])
703   "The group buffer toolbar.")
704
705 (defvar gnus-summary-toolbar
706   '([gnus-summary-prev-unread
707      gnus-summary-prev-page-or-article t "Page up"]
708     [gnus-summary-next-unread
709      gnus-summary-next-page t "Page down"]
710     [gnus-summary-post-news
711      gnus-summary-post-news t "Post an article"]
712     [gnus-summary-followup-with-original
713      gnus-summary-followup-with-original t
714      "Post a followup and yank the original"]
715     [gnus-summary-followup
716      gnus-summary-followup t "Post a followup"]
717     [gnus-summary-reply-with-original
718      gnus-summary-reply-with-original t "Mail a reply and yank the original"]
719     [gnus-summary-reply
720      gnus-summary-reply t "Mail a reply"]
721     [gnus-summary-caesar-message
722      gnus-summary-caesar-message t "Rot 13"]
723     [gnus-uu-decode-uu
724      gnus-uu-decode-uu t "Decode uuencoded articles"]
725     [gnus-summary-save-article-file
726      gnus-summary-save-article-file t "Save article in file"]
727     [gnus-summary-save-article
728      gnus-summary-save-article t "Save article"]
729     [gnus-uu-post-news
730      gnus-uu-post-news t "Post a uuencoded article"]
731     [gnus-summary-cancel-article
732      gnus-summary-cancel-article t "Cancel article"]
733     [gnus-summary-catchup
734      gnus-summary-catchup t "Catchup"]
735     [gnus-summary-catchup-and-exit
736      gnus-summary-catchup-and-exit t "Catchup and exit"]
737     [gnus-summary-exit gnus-summary-exit t "Exit this summary"])
738   "The summary buffer toolbar.")
739
740 (defvar gnus-summary-mail-toolbar
741   '(
742     [gnus-summary-prev-unread
743      gnus-summary-prev-unread-article t "Prev unread article"]
744     [gnus-summary-next-unread
745      gnus-summary-next-unread-article t "Next unread article"]
746     [gnus-summary-mail-reply gnus-summary-reply t "Reply"]
747     [gnus-summary-mail-originate gnus-summary-post-news t "Originate"]
748     [gnus-summary-mail-save gnus-summary-save-article t "Save"]
749     [gnus-summary-mail-copy gnus-summary-copy-article t "Copy message"]
750     [gnus-summary-mail-forward gnus-summary-mail-forward t "Forward message"]
751     [gnus-summary-caesar-message
752      gnus-summary-caesar-message t "Rot 13"]
753     [gnus-uu-decode-uu
754      gnus-uu-decode-uu t "Decode uuencoded articles"]
755     [gnus-summary-save-article-file
756      gnus-summary-save-article-file t "Save article in file"]
757     [gnus-summary-save-article
758      gnus-summary-save-article t "Save article"]
759     [gnus-summary-catchup
760      gnus-summary-catchup t "Catchup"]
761     [gnus-summary-catchup-and-exit
762      gnus-summary-catchup-and-exit t "Catchup and exit"]
763     [gnus-summary-exit gnus-summary-exit t "Exit this summary"])
764   "The summary buffer mail toolbar.")
765
766 (defun gnus-xmas-setup-group-toolbar ()
767   (and gnus-use-toolbar
768        (message-xmas-setup-toolbar gnus-group-toolbar nil "gnus")
769        (set-specifier (symbol-value gnus-use-toolbar)
770                       (cons (current-buffer) gnus-group-toolbar))))
771
772 (defun gnus-xmas-setup-summary-toolbar ()
773   (let ((bar (if (gnus-news-group-p gnus-newsgroup-name)
774                  gnus-summary-toolbar gnus-summary-mail-toolbar)))
775     (and gnus-use-toolbar
776          (message-xmas-setup-toolbar bar nil "gnus")
777          (set-specifier (symbol-value gnus-use-toolbar)
778                         (cons (current-buffer) bar)))))
779
780 (defun gnus-xmas-mail-strip-quoted-names (address)
781   "Protect mail-strip-quoted-names from NIL input.
782 XEmacs compatibility workaround."
783   (if (null address)
784       nil
785     (mail-strip-quoted-names address)))
786
787 (defun gnus-xmas-call-region (command &rest args)
788   (apply
789    'call-process-region (point-min) (point-max) command t '(t nil) nil
790    args))
791
792 (defface gnus-x-face '((t (:foreground "black" :background "white")))
793   "Face to show X face"
794   :group 'gnus-xmas)
795
796 (defun gnus-xmas-article-display-xface (beg end)
797   "Display any XFace headers in the current article."
798   (save-excursion
799     (let ((xface-glyph
800            (cond
801             ((featurep 'xface)
802              (make-glyph (vector 'xface :data
803                                  (concat "X-Face: "
804                                          (buffer-substring beg end)))))
805             ((featurep 'xpm)
806              (let ((cur (current-buffer)))
807                (save-excursion
808                  (gnus-set-work-buffer)
809                  (insert (format "%s" (buffer-substring beg end cur)))
810                  (gnus-xmas-call-region "uncompface")
811                  (goto-char (point-min))
812                  (insert "/* Width=48, Height=48 */\n")
813                  (gnus-xmas-call-region "icontopbm")
814                  (gnus-xmas-call-region "ppmtoxpm")
815                  (make-glyph
816                   (vector 'xpm :data (buffer-string))))))
817             (t
818              (make-glyph [nothing]))))
819           (ext (make-extent (progn
820                               (goto-char (point-min))
821                               (re-search-forward "^From:" nil t)
822                               (point))
823                             (1+ (point)))))
824       (set-glyph-face xface-glyph 'gnus-x-face)
825       (set-extent-begin-glyph ext xface-glyph)
826       (set-extent-property ext 'duplicable t))))
827
828 (defvar gnus-xmas-modeline-left-extent
829   (let ((ext (copy-extent modeline-buffer-id-left-extent)))
830     ext))
831
832 (defvar gnus-xmas-modeline-right-extent
833   (let ((ext (copy-extent modeline-buffer-id-right-extent)))
834     ext))
835
836 (defvar gnus-xmas-modeline-glyph
837   (progn
838     (setq gnus-xmas-glyph-directory (message-xmas-find-glyph-directory "gnus"))
839     (let* ((file-xpm (expand-file-name "gnus-pointer.xpm"
840                                        gnus-xmas-glyph-directory))
841            (file-xbm (expand-file-name "gnus-pointer.xbm"
842                                        gnus-xmas-glyph-directory))
843            (glyph (make-glyph
844                    ;; Gag gag gag.
845                    (cond ((featurep 'xpm)
846                           ;; Let's try a nifty XPM
847                           `[xpm :file ,file-xpm])
848                          ((featurep 'xbm)
849                           ;; Then a not-so-nifty XBM
850                           `[xbm :file ,file-xbm])
851                          ;; Then the simple string
852                          (t [string :data "Gnus:"])))))
853       (set-glyph-face glyph 'modeline-buffer-id)
854       glyph)))
855
856 (defun gnus-xmas-mode-line-buffer-identification (line)
857   (let ((line (car line))
858         chop)
859     (cond
860      ;; This is some weird type of id.
861      ((not (stringp line))
862       (list line))
863      ;; This is non-standard, so we just pass it through.
864      ((not (string-match "^Gnus:" line))
865       (list line))
866      ;; We have a standard line, so we colorize and glyphize it a bit.
867      (t
868       (setq chop (match-end 0))
869       (list
870        (if gnus-xmas-modeline-glyph
871            (cons gnus-xmas-modeline-left-extent gnus-xmas-modeline-glyph)
872          (cons gnus-xmas-modeline-left-extent (substring line 0 chop)))
873        (cons gnus-xmas-modeline-right-extent (substring line chop)))))))
874
875 (defun gnus-xmas-splash ()
876   (when (eq (device-type) 'x)
877     (gnus-splash)))
878
879 (defun gnus-xmas-annotation-in-region-p (b e)
880   (or (map-extents (lambda (e u) t) nil b e nil nil 'mm t)
881       (if (= b e)
882           (eq (cadr (memq 'gnus-undeletable (text-properties-at b))) t)
883         (text-property-any b e 'gnus-undeletable t))))
884
885 (defun gnus-xmas-mime-button-menu (event)
886   "Construct a context-sensitive menu of MIME commands."
887   (interactive "e")
888   (let ((response (get-popup-menu-response
889                    `("MIME Part"
890                      ,@(mapcar (lambda (c) `[,(caddr c) ,(car c) t])
891                                gnus-mime-button-commands)))))
892     (set-buffer (event-buffer event))
893     (goto-char (event-point event))
894     (funcall (event-function response) (event-object response))))
895
896 (defun gnus-group-add-icon ()
897   "Add an icon to the current line according to `gnus-group-icon-list'."
898   (let* ((p (point))
899          (end (progn (end-of-line) (point)))
900          ;; now find out where the line starts and leave point there.
901          (beg (progn (beginning-of-line) (point))))
902     (save-restriction
903       (narrow-to-region beg end)
904       (goto-char beg)
905       (when (search-forward "==&&==" nil t)
906         (let* ((group (gnus-group-group-name))
907                (entry (gnus-group-entry group))
908                (unread (if (numberp (car entry)) (car entry) 0))
909                (active (gnus-active group))
910                (total (if active (1+ (- (cdr active) (car active))) 0))
911                (info (nth 2 entry))
912                (method (gnus-server-get-method group (gnus-info-method info)))
913                (marked (gnus-info-marks info))
914                (mailp (memq 'mail (assoc (symbol-name
915                                           (car (or method gnus-select-method)))
916                                          gnus-valid-select-methods)))
917                (level (or (gnus-info-level info) gnus-level-killed))
918                (score (or (gnus-info-score info) 0))
919                (ticked (gnus-range-length (cdr (assq 'tick marked))))
920                (group-age (gnus-group-timestamp-delta group))
921                (inhibit-read-only t)
922                (list gnus-group-icon-list)
923                (mystart (match-beginning 0))
924                (myend (match-end 0)))
925           (goto-char (point-min))
926           (while (and list
927                       (not (eval (caar list))))
928             (setq list (cdr list)))
929           (if list
930               (let* ((file (cdar list))
931                      (glyph (gnus-group-icon-create-glyph
932                              (buffer-substring mystart myend)
933                              file)))
934                 (if glyph
935                     (progn
936                       (mapcar 'delete-annotation (annotations-at myend))
937                       (let ((ext (make-extent mystart myend))
938                             (ant (make-annotation glyph myend 'text)))
939                         ;; set text extent params
940                         (set-extent-property ext 'end-open t)
941                         (set-extent-property ext 'start-open t)
942                         (set-extent-property ext 'invisible t)))
943                   (delete-region mystart myend)))
944             (delete-region mystart myend))))
945       (widen))
946     (goto-char p)))
947
948 (defun gnus-group-icon-create-glyph (substring pixmap)
949   "Create a glyph for insertion into a group line."
950   (and
951    gnus-group-running-xemacs
952    (or
953     (cdr-safe (assoc pixmap gnus-group-icon-cache))
954     (let* ((glyph (make-glyph
955                    (list
956                     (cons 'x
957                           (expand-file-name pixmap gnus-xmas-glyph-directory))
958                     (cons 'mswindows
959                           (expand-file-name pixmap gnus-xmas-glyph-directory))
960                     (cons 'tty substring)))))
961       (setq gnus-group-icon-cache
962             (cons (cons pixmap glyph) gnus-group-icon-cache))
963       (set-glyph-face glyph 'default)
964       glyph))))
965
966 (provide 'gnus-xmas)
967
968 ;;; gnus-xmas.el ends here