dc01c2882d7571b25aa89cfbb549e9c689ca767c
[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       (end-of-line 0)
621       (put-text-property (point-min) (point) 'face 'gnus-splash-face)
622       (insert-char ?\  (prog1
623                            (max 0 (/ (- (window-width) (point)) 2))
624                          (goto-char (point-min))))
625       (forward-line 1)
626       (insert-char ?\n rest)
627       (set-window-start (selected-window) (point-min))))
628    (t
629     (insert "
630           _    ___ _             _
631           _ ___ __ ___  __    _ ___
632           __   _     ___    __  ___
633               _           ___     _
634              _  _ __             _
635              ___   __            _
636                    __           _
637                     _      _   _
638                    _      _    _
639                       _  _    _
640                   __  ___
641                  _   _ _     _
642                 _   _
643               _    _
644              _    _
645             _
646           __
647
648 "
649             )
650     (goto-char (point-min))
651     (insert gnus-product-name " " gnus-version-number
652             (if (zerop (string-to-number gnus-revision-number))
653                 ""
654               (concat " (r" gnus-revision-number ")"))
655             " based on " gnus-original-product-name " v"
656             gnus-original-version-number)
657     (insert-char ?\  (prog1
658                          (max 0 (/ (- (window-width) (point)) 2))
659                        (goto-char (point-min))))
660     (forward-line 1)
661     ;; And then hack it.
662     (gnus-indent-rigidly (point) (point-max)
663                          (/ (max (- (window-width) (or x 46)) 0) 2))
664     (goto-char (point-min))
665     (forward-line 1)
666     (let* ((pheight (count-lines (point-min) (point-max)))
667            (wheight (window-height))
668            (rest (- wheight pheight)))
669       (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
670     ;; Paint it.
671     (put-text-property (point-min) (point-max) 'face 'gnus-splash-face)))
672   (setq modeline-buffer-identification
673         (list (concat gnus-version ": *Group*")))
674   (set-buffer-modified-p t))
675
676
677 ;;; The toolbar.
678
679 (defcustom gnus-use-toolbar (if (featurep 'toolbar)
680                                 'default-toolbar
681                               nil)
682   "*If nil, do not use a toolbar.
683 If it is non-nil, it must be a toolbar.  The five valid values are
684 `default-toolbar', `top-toolbar', `bottom-toolbar',
685 `right-toolbar', and `left-toolbar'."
686   :type '(choice (const default-toolbar)
687                  (const top-toolbar) (const bottom-toolbar)
688                  (const left-toolbar) (const right-toolbar)
689                  (const :tag "no toolbar" nil))
690   :group 'gnus-xmas)
691
692 (defvar gnus-group-toolbar
693   '([gnus-group-get-new-news gnus-group-get-new-news t "Get new news"]
694     [gnus-group-get-new-news-this-group
695      gnus-group-get-new-news-this-group t "Get new news in this group"]
696     [gnus-group-catchup-current
697      gnus-group-catchup-current t "Catchup group"]
698     [gnus-group-describe-group
699      gnus-group-describe-group t "Describe group"]
700     [gnus-group-unsubscribe gnus-group-unsubscribe t "Unsubscribe group"]
701     [gnus-group-subscribe gnus-group-subscribe t "Subscribe group"]
702     [gnus-group-kill-group gnus-group-kill-group t "Kill group"]
703     [gnus-group-exit gnus-group-exit t "Exit Gnus"])
704   "The group buffer toolbar.")
705
706 (defvar gnus-summary-toolbar
707   '([gnus-summary-prev-unread
708      gnus-summary-prev-page-or-article t "Page up"]
709     [gnus-summary-next-unread
710      gnus-summary-next-page t "Page down"]
711     [gnus-summary-post-news
712      gnus-summary-post-news t "Post an article"]
713     [gnus-summary-followup-with-original
714      gnus-summary-followup-with-original t
715      "Post a followup and yank the original"]
716     [gnus-summary-followup
717      gnus-summary-followup t "Post a followup"]
718     [gnus-summary-reply-with-original
719      gnus-summary-reply-with-original t "Mail a reply and yank the original"]
720     [gnus-summary-reply
721      gnus-summary-reply t "Mail a reply"]
722     [gnus-summary-caesar-message
723      gnus-summary-caesar-message t "Rot 13"]
724     [gnus-uu-decode-uu
725      gnus-uu-decode-uu t "Decode uuencoded articles"]
726     [gnus-summary-save-article-file
727      gnus-summary-save-article-file t "Save article in file"]
728     [gnus-summary-save-article
729      gnus-summary-save-article t "Save article"]
730     [gnus-uu-post-news
731      gnus-uu-post-news t "Post a uuencoded article"]
732     [gnus-summary-cancel-article
733      gnus-summary-cancel-article t "Cancel article"]
734     [gnus-summary-catchup
735      gnus-summary-catchup t "Catchup"]
736     [gnus-summary-catchup-and-exit
737      gnus-summary-catchup-and-exit t "Catchup and exit"]
738     [gnus-summary-exit gnus-summary-exit t "Exit this summary"])
739   "The summary buffer toolbar.")
740
741 (defvar gnus-summary-mail-toolbar
742   '(
743     [gnus-summary-prev-unread
744      gnus-summary-prev-unread-article t "Prev unread article"]
745     [gnus-summary-next-unread
746      gnus-summary-next-unread-article t "Next unread article"]
747     [gnus-summary-mail-reply gnus-summary-reply t "Reply"]
748     [gnus-summary-mail-originate gnus-summary-post-news t "Originate"]
749     [gnus-summary-mail-save gnus-summary-save-article t "Save"]
750     [gnus-summary-mail-copy gnus-summary-copy-article t "Copy message"]
751     [gnus-summary-mail-forward gnus-summary-mail-forward t "Forward message"]
752     [gnus-summary-caesar-message
753      gnus-summary-caesar-message t "Rot 13"]
754     [gnus-uu-decode-uu
755      gnus-uu-decode-uu t "Decode uuencoded articles"]
756     [gnus-summary-save-article-file
757      gnus-summary-save-article-file t "Save article in file"]
758     [gnus-summary-save-article
759      gnus-summary-save-article t "Save article"]
760     [gnus-summary-catchup
761      gnus-summary-catchup t "Catchup"]
762     [gnus-summary-catchup-and-exit
763      gnus-summary-catchup-and-exit t "Catchup and exit"]
764     [gnus-summary-exit gnus-summary-exit t "Exit this summary"])
765   "The summary buffer mail toolbar.")
766
767 (defun gnus-xmas-setup-group-toolbar ()
768   (and gnus-use-toolbar
769        (message-xmas-setup-toolbar gnus-group-toolbar nil "gnus")
770        (set-specifier (symbol-value gnus-use-toolbar)
771                       (cons (current-buffer) gnus-group-toolbar))))
772
773 (defun gnus-xmas-setup-summary-toolbar ()
774   (let ((bar (if (gnus-news-group-p gnus-newsgroup-name)
775                  gnus-summary-toolbar gnus-summary-mail-toolbar)))
776     (and gnus-use-toolbar
777          (message-xmas-setup-toolbar bar nil "gnus")
778          (set-specifier (symbol-value gnus-use-toolbar)
779                         (cons (current-buffer) bar)))))
780
781 (defun gnus-xmas-mail-strip-quoted-names (address)
782   "Protect mail-strip-quoted-names from NIL input.
783 XEmacs compatibility workaround."
784   (if (null address)
785       nil
786     (mail-strip-quoted-names address)))
787
788 (defun gnus-xmas-call-region (command &rest args)
789   (apply
790    'call-process-region (point-min) (point-max) command t '(t nil) nil
791    args))
792
793 (defface gnus-x-face '((t (:foreground "black" :background "white")))
794   "Face to show X face"
795   :group 'gnus-xmas)
796
797 (defun gnus-xmas-article-display-xface (beg end)
798   "Display any XFace headers in the current article."
799   (save-excursion
800     (let ((xface-glyph
801            (cond
802             ((featurep 'xface)
803              (make-glyph (vector 'xface :data
804                                  (concat "X-Face: "
805                                          (buffer-substring beg end)))))
806             ((featurep 'xpm)
807              (let ((cur (current-buffer)))
808                (save-excursion
809                  (gnus-set-work-buffer)
810                  (insert (format "%s" (buffer-substring beg end cur)))
811                  (gnus-xmas-call-region "uncompface")
812                  (goto-char (point-min))
813                  (insert "/* Width=48, Height=48 */\n")
814                  (gnus-xmas-call-region "icontopbm")
815                  (gnus-xmas-call-region "ppmtoxpm")
816                  (make-glyph
817                   (vector 'xpm :data (buffer-string))))))
818             (t
819              (make-glyph [nothing]))))
820           (ext (make-extent (progn
821                               (goto-char (point-min))
822                               (re-search-forward "^From:" nil t)
823                               (point))
824                             (1+ (point)))))
825       (set-glyph-face xface-glyph 'gnus-x-face)
826       (set-extent-begin-glyph ext xface-glyph)
827       (set-extent-property ext 'duplicable t))))
828
829 (defvar gnus-xmas-modeline-left-extent
830   (let ((ext (copy-extent modeline-buffer-id-left-extent)))
831     ext))
832
833 (defvar gnus-xmas-modeline-right-extent
834   (let ((ext (copy-extent modeline-buffer-id-right-extent)))
835     ext))
836
837 (defvar gnus-xmas-modeline-glyph
838   (progn
839     (setq gnus-xmas-glyph-directory (message-xmas-find-glyph-directory "gnus"))
840     (let* ((file-xpm (expand-file-name "gnus-pointer.xpm"
841                                        gnus-xmas-glyph-directory))
842            (file-xbm (expand-file-name "gnus-pointer.xbm"
843                                        gnus-xmas-glyph-directory))
844            (glyph (make-glyph
845                    ;; Gag gag gag.
846                    (cond ((featurep 'xpm)
847                           ;; Let's try a nifty XPM
848                           `[xpm :file ,file-xpm])
849                          ((featurep 'xbm)
850                           ;; Then a not-so-nifty XBM
851                           `[xbm :file ,file-xbm])
852                          ;; Then the simple string
853                          (t [string :data "Gnus:"])))))
854       (set-glyph-face glyph 'modeline-buffer-id)
855       glyph)))
856
857 (defun gnus-xmas-mode-line-buffer-identification (line)
858   (let ((line (car line))
859         chop)
860     (cond
861      ;; This is some weird type of id.
862      ((not (stringp line))
863       (list line))
864      ;; This is non-standard, so we just pass it through.
865      ((not (string-match "^Gnus:" line))
866       (list line))
867      ;; We have a standard line, so we colorize and glyphize it a bit.
868      (t
869       (setq chop (match-end 0))
870       (list
871        (if gnus-xmas-modeline-glyph
872            (cons gnus-xmas-modeline-left-extent gnus-xmas-modeline-glyph)
873          (cons gnus-xmas-modeline-left-extent (substring line 0 chop)))
874        (cons gnus-xmas-modeline-right-extent (substring line chop)))))))
875
876 (defun gnus-xmas-splash ()
877   (when (eq (device-type) 'x)
878     (gnus-splash)))
879
880 (defun gnus-xmas-annotation-in-region-p (b e)
881   (or (map-extents (lambda (e u) t) nil b e nil nil 'mm t)
882       (if (= b e)
883           (eq (cadr (memq 'gnus-undeletable (text-properties-at b))) t)
884         (text-property-any b e 'gnus-undeletable t))))
885
886 (defun gnus-xmas-mime-button-menu (event)
887   "Construct a context-sensitive menu of MIME commands."
888   (interactive "e")
889   (let ((response (get-popup-menu-response
890                    `("MIME Part"
891                      ,@(mapcar (lambda (c) `[,(caddr c) ,(car c) t])
892                                gnus-mime-button-commands)))))
893     (set-buffer (event-buffer event))
894     (goto-char (event-point event))
895     (funcall (event-function response) (event-object response))))
896
897 (defun gnus-group-add-icon ()
898   "Add an icon to the current line according to `gnus-group-icon-list'."
899   (let* ((p (point))
900          (end (progn (end-of-line) (point)))
901          ;; now find out where the line starts and leave point there.
902          (beg (progn (beginning-of-line) (point))))
903     (save-restriction
904       (narrow-to-region beg end)
905       (goto-char beg)
906       (when (search-forward "==&&==" nil t)
907         (let* ((group (gnus-group-group-name))
908                (entry (gnus-group-entry group))
909                (unread (if (numberp (car entry)) (car entry) 0))
910                (active (gnus-active group))
911                (total (if active (1+ (- (cdr active) (car active))) 0))
912                (info (nth 2 entry))
913                (method (gnus-server-get-method group (gnus-info-method info)))
914                (marked (gnus-info-marks info))
915                (mailp (memq 'mail (assoc (symbol-name
916                                           (car (or method gnus-select-method)))
917                                          gnus-valid-select-methods)))
918                (level (or (gnus-info-level info) gnus-level-killed))
919                (score (or (gnus-info-score info) 0))
920                (ticked (gnus-range-length (cdr (assq 'tick marked))))
921                (group-age (gnus-group-timestamp-delta group))
922                (inhibit-read-only t)
923                (list gnus-group-icon-list)
924                (mystart (match-beginning 0))
925                (myend (match-end 0)))
926           (goto-char (point-min))
927           (while (and list
928                       (not (eval (caar list))))
929             (setq list (cdr list)))
930           (if list
931               (let* ((file (cdar list))
932                      (glyph (gnus-group-icon-create-glyph
933                              (buffer-substring mystart myend)
934                              file)))
935                 (if glyph
936                     (progn
937                       (mapcar 'delete-annotation (annotations-at myend))
938                       (let ((ext (make-extent mystart myend))
939                             (ant (make-annotation glyph myend 'text)))
940                         ;; set text extent params
941                         (set-extent-property ext 'end-open t)
942                         (set-extent-property ext 'start-open t)
943                         (set-extent-property ext 'invisible t)))
944                   (delete-region mystart myend)))
945             (delete-region mystart myend))))
946       (widen))
947     (goto-char p)))
948
949 (defun gnus-group-icon-create-glyph (substring pixmap)
950   "Create a glyph for insertion into a group line."
951   (and
952    gnus-group-running-xemacs
953    (or
954     (cdr-safe (assoc pixmap gnus-group-icon-cache))
955     (let* ((glyph (make-glyph
956                    (list
957                     (cons 'x
958                           (expand-file-name pixmap gnus-xmas-glyph-directory))
959                     (cons 'mswindows
960                           (expand-file-name pixmap gnus-xmas-glyph-directory))
961                     (cons 'tty substring)))))
962       (setq gnus-group-icon-cache
963             (cons (cons pixmap glyph) gnus-group-icon-cache))
964       (set-glyph-face glyph 'default)
965       glyph))))
966
967 (provide 'gnus-xmas)
968
969 ;;; gnus-xmas.el ends here