Synch with Gnus.
[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     (defalias 'gnus-characterp 'char-or-char-int-p))
411    ;; V19 of XEmacs, probably.
412    (t
413     (defalias 'gnus-characterp 'characterp)))
414
415   (defalias 'gnus-make-overlay 'make-extent)
416   (defalias 'gnus-delete-overlay 'delete-extent)
417   (defalias 'gnus-overlay-put 'set-extent-property)
418   (defalias 'gnus-move-overlay 'gnus-xmas-move-overlay)
419   (defalias 'gnus-overlay-buffer 'extent-object)
420   (defalias 'gnus-overlay-start 'extent-start-position)
421   (defalias 'gnus-overlay-end 'extent-end-position)
422   (defalias 'gnus-kill-all-overlays 'gnus-xmas-kill-all-overlays)
423   (defalias 'gnus-extent-detached-p 'extent-detached-p)
424   (defalias 'gnus-add-text-properties 'gnus-xmas-add-text-properties)
425   (defalias 'gnus-put-text-property 'gnus-xmas-put-text-property)
426   (defalias 'gnus-deactivate-mark 'ignore)
427   (defalias 'gnus-window-edges 'window-pixel-edges)
428
429   (if (and (<= emacs-major-version 19)
430            (< emacs-minor-version 14))
431       (defalias 'gnus-set-text-properties 'gnus-xmas-set-text-properties))
432
433   (when (fboundp 'turn-off-scroll-in-place)
434     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
435
436   (unless (boundp 'standard-display-table)
437     (setq standard-display-table nil))
438
439   (defvar gnus-mouse-face-prop 'highlight)
440
441   (defun gnus-byte-code (func)
442     "Return a form that can be `eval'ed based on FUNC."
443     (let ((fval (indirect-function func)))
444       (if (compiled-function-p fval)
445           (list 'funcall fval)
446         (cons 'progn (cdr (cdr fval))))))
447
448   (defalias 'gnus-x-color-values
449     (if (fboundp 'x-color-values)
450         'x-color-values
451       (lambda (color)
452         (color-instance-rgb-components
453          (make-color-instance color))))))
454
455 (defun gnus-xmas-redefine ()
456   "Redefine lots of Gnus functions for XEmacs."
457   (defalias 'gnus-summary-set-display-table 'gnus-xmas-summary-set-display-table)
458   (defalias 'gnus-visual-turn-off-edit-menu 'identity)
459   (defalias 'gnus-summary-recenter 'gnus-xmas-summary-recenter)
460   (defalias 'gnus-extent-start-open 'gnus-xmas-extent-start-open)
461   (defalias 'gnus-article-push-button 'gnus-xmas-article-push-button)
462   (defalias 'gnus-window-top-edge 'gnus-xmas-window-top-edge)
463   (defalias 'gnus-read-event-char 'gnus-xmas-read-event-char)
464   (defalias 'gnus-group-startup-message 'gnus-xmas-group-startup-message)
465   (defalias 'gnus-tree-minimize 'gnus-xmas-tree-minimize)
466   (defalias 'gnus-appt-select-lowest-window
467         'gnus-xmas-appt-select-lowest-window)
468   (defalias 'gnus-mail-strip-quoted-names 'gnus-xmas-mail-strip-quoted-names)
469   (defalias 'gnus-character-to-event 'character-to-event)
470   (defalias 'gnus-mode-line-buffer-identification
471         'gnus-xmas-mode-line-buffer-identification)
472   (defalias 'gnus-key-press-event-p 'key-press-event-p)
473   (defalias 'gnus-region-active-p 'region-active-p)
474   (defalias 'gnus-annotation-in-region-p 'gnus-xmas-annotation-in-region-p)
475   (defalias 'gnus-mime-button-menu 'gnus-xmas-mime-button-menu)
476
477   (add-hook 'gnus-group-mode-hook 'gnus-xmas-group-menu-add)
478   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
479   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add)
480   (add-hook 'gnus-score-mode-hook 'gnus-xmas-score-menu-add)
481
482   (add-hook 'gnus-pick-mode-hook 'gnus-xmas-pick-menu-add)
483   (add-hook 'gnus-topic-mode-hook 'gnus-xmas-topic-menu-add)
484   (add-hook 'gnus-tree-mode-hook 'gnus-xmas-tree-menu-add)
485   (add-hook 'gnus-binary-mode-hook 'gnus-xmas-binary-menu-add)
486   (add-hook 'gnus-grouplens-mode-hook 'gnus-xmas-grouplens-menu-add)
487   (add-hook 'gnus-server-mode-hook 'gnus-xmas-server-menu-add)
488   (add-hook 'gnus-browse-mode-hook 'gnus-xmas-browse-menu-add)
489
490   (add-hook 'gnus-group-mode-hook 'gnus-xmas-setup-group-toolbar)
491   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
492
493   (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add)
494   (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add)
495   (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add)
496
497   (add-hook 'gnus-draft-mode-hook 'gnus-xmas-draft-menu-add)
498   (add-hook 'gnus-summary-mode-hook
499             'gnus-xmas-switch-horizontal-scrollbar-off)
500   (add-hook 'gnus-tree-mode-hook 'gnus-xmas-switch-horizontal-scrollbar-off)
501
502   (when (featurep 'mule)
503     (defun gnus-truncate-string (str end-column &optional start-column padding)
504       "Truncate string STR to end at column END-COLUMN.
505 The optional 2nd arg START-COLUMN, if non-nil, specifies
506 the starting column; that means to return the characters occupying
507 columns START-COLUMN ... END-COLUMN of STR.
508
509 The optional 3rd arg PADDING, if non-nil, specifies a padding character
510 to add at the end of the result if STR doesn't reach column END-COLUMN,
511 or if END-COLUMN comes in the middle of a character in STR.
512 PADDING is also added at the beginning of the result
513 if column START-COLUMN appears in the middle of a character in STR.
514
515 If PADDING is nil, no padding is added in these cases, so
516 the resulting string may be narrower than END-COLUMN.
517 \[Emacs 20.3 emulating function]"
518       (or start-column
519           (setq start-column 0))
520       (let ((len (length str))
521             (idx 0)
522             (column 0)
523             (head-padding "") (tail-padding "")
524             ch last-column last-idx from-idx)
525         (condition-case nil
526             (while (< column start-column)
527               (setq ch (aref str idx)
528                     column (+ column (char-width ch))
529                     idx (1+ idx)))
530           (args-out-of-range (setq idx len)))
531         (if (< column start-column)
532             (if padding (make-string end-column padding) "")
533           (if (and padding (> column start-column))
534               (setq head-padding
535                     (make-string (- column start-column) padding)))
536           (setq from-idx idx)
537           (if (< end-column column)
538               (setq idx from-idx)
539             (condition-case nil
540                 (while (< column end-column)
541                   (setq last-column column
542                         last-idx idx
543                         ch (aref str idx)
544                         column (+ column (char-width ch))
545                         idx (1+ idx)))
546               (args-out-of-range (setq idx len)))
547             (if (> column end-column)
548                 (setq column last-column idx last-idx))
549             (if (and padding (< column end-column))
550                 (setq tail-padding
551                       (make-string (- end-column column) padding))))
552           (setq str (substring str from-idx idx))
553           (if padding
554               (concat head-padding str tail-padding)
555             str))))
556
557     (defun gnus-tilde-pad-form (el pad-width)
558       "Return a form that pads EL to PAD-WIDTH."
559       (let ((pad (abs pad-width)))
560         (if (symbolp el)
561             (if (< pad-width 0)
562                 `(let ((val (format "%s" ,el)))
563                    (concat val (make-string
564                                 (max 0 (- ,pad (string-width val))) ?\ )))
565               `(let ((val (format "%s" ,el)))
566                  (concat (make-string
567                           (max 0 (- ,pad (string-width val))) ?\ )
568                          val)))
569           (if (< pad-width 0)
570               `(let ((val (eval ,el)))
571                  (concat val (make-string
572                               (max 0 (- ,pad (string-width val))) ?\ )))
573             `(let ((val (eval ,el)))
574                (concat (make-string
575                         (max 0 (- ,pad (string-width val))) ?\ )
576                        val))))))
577     ))
578
579 ;;; XEmacs logo and toolbar.
580
581 (defun gnus-xmas-group-startup-message (&optional x y)
582   "Insert startup message in current buffer."
583   ;; Insert the message.
584   (setq gnus-xmas-glyph-directory (message-xmas-find-glyph-directory "gnus"))
585   (erase-buffer)
586   (cond
587    ((and (console-on-window-system-p)
588          (or (featurep 'xpm)
589              (featurep 'xbm)))
590     (let* ((logo-xpm (expand-file-name "gnus.xpm" gnus-xmas-glyph-directory))
591            (logo-xbm (expand-file-name "gnus.xbm" gnus-xmas-glyph-directory))
592            (glyph (make-glyph
593                    (cond ((featurep 'xpm)
594                           `[xpm
595                             :file ,logo-xpm
596                             :color-symbols
597                             (("thing" . ,(car gnus-xmas-logo-colors))
598                              ("shadow" . ,(cadr gnus-xmas-logo-colors))
599                              ("background" . ,(face-background 'default)))])
600                          ((featurep 'xbm)
601                           `[xbm :file ,logo-xbm])
602                          (t [nothing]))))
603            (wpheight (window-pixel-height))
604            (rest (max 0 (1- (/ (* (- wpheight (glyph-height glyph))
605                                   (window-height))
606                                wpheight 2)))))
607       (insert " ")
608       (set-extent-begin-glyph (make-extent (point) (point)) glyph)
609       (goto-char (point-min))
610       (while (not (eobp))
611         (insert (make-string (/ (max (- (window-width) (or x 35)) 0) 2)
612                              ?\ ))
613         (forward-line 1))
614       (setq gnus-simple-splash nil)
615       (goto-char (point-min))
616       (insert gnus-product-name " " gnus-version-number
617               (if (zerop (string-to-number gnus-revision-number))
618                   ""
619                 (concat " (r" gnus-revision-number ")"))
620               " based on " gnus-original-product-name " v"
621               gnus-original-version-number "\n")
622       (end-of-line 0)
623       (put-text-property (point-min) (point) 'face 'gnus-splash-face)
624       (insert-char ?\  (prog1
625                            (max 0 (/ (- (window-width) (point)) 2))
626                          (goto-char (point-min))))
627       (forward-line 1)
628       (insert-char ?\n rest)
629       (set-window-start (selected-window) (point-min))))
630    (t
631     (insert "
632           _    ___ _             _
633           _ ___ __ ___  __    _ ___
634           __   _     ___    __  ___
635               _           ___     _
636              _  _ __             _
637              ___   __            _
638                    __           _
639                     _      _   _
640                    _      _    _
641                       _  _    _
642                   __  ___
643                  _   _ _     _
644                 _   _
645               _    _
646              _    _
647             _
648           __
649
650 "
651             )
652     (goto-char (point-min))
653     (insert gnus-product-name " " gnus-version-number
654             (if (zerop (string-to-number gnus-revision-number))
655                 ""
656               (concat " (r" gnus-revision-number ")"))
657             " based on " gnus-original-product-name " v"
658             gnus-original-version-number)
659     (insert-char ?\  (prog1
660                          (max 0 (/ (- (window-width) (point)) 2))
661                        (goto-char (point-min))))
662     (forward-line 1)
663     ;; And then hack it.
664     (gnus-indent-rigidly (point) (point-max)
665                          (/ (max (- (window-width) (or x 46)) 0) 2))
666     (goto-char (point-min))
667     (forward-line 1)
668     (let* ((pheight (count-lines (point-min) (point-max)))
669            (wheight (window-height))
670            (rest (- wheight pheight)))
671       (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
672     ;; Paint it.
673     (put-text-property (point-min) (point-max) 'face 'gnus-splash-face)))
674   (setq modeline-buffer-identification
675         (list (concat gnus-version ": *Group*")))
676   (set-buffer-modified-p t))
677
678
679 ;;; The toolbar.
680
681 (defcustom gnus-use-toolbar (if (featurep 'toolbar)
682                                 'default-toolbar
683                               nil)
684   "*If nil, do not use a toolbar.
685 If it is non-nil, it must be a toolbar.  The five valid values are
686 `default-toolbar', `top-toolbar', `bottom-toolbar',
687 `right-toolbar', and `left-toolbar'."
688   :type '(choice (const default-toolbar)
689                  (const top-toolbar) (const bottom-toolbar)
690                  (const left-toolbar) (const right-toolbar)
691                  (const :tag "no toolbar" nil))
692   :group 'gnus-xmas)
693
694 (defvar gnus-group-toolbar
695   '([gnus-group-get-new-news gnus-group-get-new-news t "Get new news"]
696     [gnus-group-get-new-news-this-group
697      gnus-group-get-new-news-this-group t "Get new news in this group"]
698     [gnus-group-catchup-current
699      gnus-group-catchup-current t "Catchup group"]
700     [gnus-group-describe-group
701      gnus-group-describe-group t "Describe group"]
702     [gnus-group-unsubscribe gnus-group-unsubscribe t "Unsubscribe group"]
703     [gnus-group-subscribe gnus-group-subscribe t "Subscribe group"]
704     [gnus-group-kill-group gnus-group-kill-group t "Kill group"]
705     [gnus-group-exit gnus-group-exit t "Exit Gnus"])
706   "The group buffer toolbar.")
707
708 (defvar gnus-summary-toolbar
709   '([gnus-summary-prev-unread
710      gnus-summary-prev-page-or-article t "Page up"]
711     [gnus-summary-next-unread
712      gnus-summary-next-page t "Page down"]
713     [gnus-summary-post-news
714      gnus-summary-post-news t "Post an article"]
715     [gnus-summary-followup-with-original
716      gnus-summary-followup-with-original t
717      "Post a followup and yank the original"]
718     [gnus-summary-followup
719      gnus-summary-followup t "Post a followup"]
720     [gnus-summary-reply-with-original
721      gnus-summary-reply-with-original t "Mail a reply and yank the original"]
722     [gnus-summary-reply
723      gnus-summary-reply t "Mail a reply"]
724     [gnus-summary-caesar-message
725      gnus-summary-caesar-message t "Rot 13"]
726     [gnus-uu-decode-uu
727      gnus-uu-decode-uu t "Decode uuencoded articles"]
728     [gnus-summary-save-article-file
729      gnus-summary-save-article-file t "Save article in file"]
730     [gnus-summary-save-article
731      gnus-summary-save-article t "Save article"]
732     [gnus-uu-post-news
733      gnus-uu-post-news t "Post a uuencoded article"]
734     [gnus-summary-cancel-article
735      gnus-summary-cancel-article t "Cancel article"]
736     [gnus-summary-catchup
737      gnus-summary-catchup t "Catchup"]
738     [gnus-summary-catchup-and-exit
739      gnus-summary-catchup-and-exit t "Catchup and exit"]
740     [gnus-summary-exit gnus-summary-exit t "Exit this summary"])
741   "The summary buffer toolbar.")
742
743 (defvar gnus-summary-mail-toolbar
744   '(
745     [gnus-summary-prev-unread
746      gnus-summary-prev-unread-article t "Prev unread article"]
747     [gnus-summary-next-unread
748      gnus-summary-next-unread-article t "Next unread article"]
749     [gnus-summary-mail-reply gnus-summary-reply t "Reply"]
750     [gnus-summary-mail-originate gnus-summary-post-news t "Originate"]
751     [gnus-summary-mail-save gnus-summary-save-article t "Save"]
752     [gnus-summary-mail-copy gnus-summary-copy-article t "Copy message"]
753     [gnus-summary-mail-forward gnus-summary-mail-forward t "Forward message"]
754     [gnus-summary-caesar-message
755      gnus-summary-caesar-message t "Rot 13"]
756     [gnus-uu-decode-uu
757      gnus-uu-decode-uu t "Decode uuencoded articles"]
758     [gnus-summary-save-article-file
759      gnus-summary-save-article-file t "Save article in file"]
760     [gnus-summary-save-article
761      gnus-summary-save-article t "Save article"]
762     [gnus-summary-catchup
763      gnus-summary-catchup t "Catchup"]
764     [gnus-summary-catchup-and-exit
765      gnus-summary-catchup-and-exit t "Catchup and exit"]
766     [gnus-summary-exit gnus-summary-exit t "Exit this summary"])
767   "The summary buffer mail toolbar.")
768
769 (defun gnus-xmas-setup-group-toolbar ()
770   (and gnus-use-toolbar
771        (message-xmas-setup-toolbar gnus-group-toolbar nil "gnus")
772        (set-specifier (symbol-value gnus-use-toolbar)
773                       (cons (current-buffer) gnus-group-toolbar))))
774
775 (defun gnus-xmas-setup-summary-toolbar ()
776   (let ((bar (if (gnus-news-group-p gnus-newsgroup-name)
777                  gnus-summary-toolbar gnus-summary-mail-toolbar)))
778     (and gnus-use-toolbar
779          (message-xmas-setup-toolbar bar nil "gnus")
780          (set-specifier (symbol-value gnus-use-toolbar)
781                         (cons (current-buffer) bar)))))
782
783 (defun gnus-xmas-mail-strip-quoted-names (address)
784   "Protect mail-strip-quoted-names from NIL input.
785 XEmacs compatibility workaround."
786   (if (null address)
787       nil
788     (mail-strip-quoted-names address)))
789
790 (defun gnus-xmas-call-region (command &rest args)
791   (apply
792    'call-process-region (point-min) (point-max) command t '(t nil) nil
793    args))
794
795 (defface gnus-x-face '((t (:foreground "black" :background "white")))
796   "Face to show X face"
797   :group 'gnus-xmas)
798
799 (defun gnus-xmas-article-display-xface (beg end)
800   "Display any XFace headers in the current article."
801   (save-excursion
802     (let ((xface-glyph
803            (cond
804             ((featurep 'xface)
805              (make-glyph (vector 'xface :data
806                                  (concat "X-Face: "
807                                          (buffer-substring beg end)))))
808             ((featurep 'xpm)
809              (let ((cur (current-buffer)))
810                (save-excursion
811                  (gnus-set-work-buffer)
812                  (insert (format "%s" (buffer-substring beg end cur)))
813                  (gnus-xmas-call-region "uncompface")
814                  (goto-char (point-min))
815                  (insert "/* Width=48, Height=48 */\n")
816                  (gnus-xmas-call-region "icontopbm")
817                  (gnus-xmas-call-region "ppmtoxpm")
818                  (make-glyph
819                   (vector 'xpm :data (buffer-string))))))
820             (t
821              (make-glyph [nothing]))))
822           (ext (make-extent (progn
823                               (goto-char (point-min))
824                               (re-search-forward "^From:" nil t)
825                               (point))
826                             (1+ (point)))))
827       (set-glyph-face xface-glyph 'gnus-x-face)
828       (set-extent-begin-glyph ext xface-glyph)
829       (set-extent-property ext 'duplicable t))))
830
831 (defvar gnus-xmas-modeline-left-extent
832   (let ((ext (copy-extent modeline-buffer-id-left-extent)))
833     ext))
834
835 (defvar gnus-xmas-modeline-right-extent
836   (let ((ext (copy-extent modeline-buffer-id-right-extent)))
837     ext))
838
839 (defvar gnus-xmas-modeline-glyph
840   (progn
841     (setq gnus-xmas-glyph-directory (message-xmas-find-glyph-directory "gnus"))
842     (let* ((file-xpm (expand-file-name "gnus-pointer.xpm"
843                                        gnus-xmas-glyph-directory))
844            (file-xbm (expand-file-name "gnus-pointer.xbm"
845                                        gnus-xmas-glyph-directory))
846            (glyph (make-glyph
847                    ;; Gag gag gag.
848                    (cond ((featurep 'xpm)
849                           ;; Let's try a nifty XPM
850                           `[xpm :file ,file-xpm])
851                          ((featurep 'xbm)
852                           ;; Then a not-so-nifty XBM
853                           `[xbm :file ,file-xbm])
854                          ;; Then the simple string
855                          (t [string :data "Gnus:"])))))
856       (set-glyph-face glyph 'modeline-buffer-id)
857       glyph)))
858
859 (defun gnus-xmas-mode-line-buffer-identification (line)
860   (let ((line (car line))
861         chop)
862     (cond
863      ;; This is some weird type of id.
864      ((not (stringp line))
865       (list line))
866      ;; This is non-standard, so we just pass it through.
867      ((not (string-match "^Gnus:" line))
868       (list line))
869      ;; We have a standard line, so we colorize and glyphize it a bit.
870      (t
871       (setq chop (match-end 0))
872       (list
873        (if gnus-xmas-modeline-glyph
874            (cons gnus-xmas-modeline-left-extent gnus-xmas-modeline-glyph)
875          (cons gnus-xmas-modeline-left-extent (substring line 0 chop)))
876        (cons gnus-xmas-modeline-right-extent (substring line chop)))))))
877
878 (defun gnus-xmas-splash ()
879   (when (eq (device-type) 'x)
880     (gnus-splash)))
881
882 (defun gnus-xmas-annotation-in-region-p (b e)
883   (or (map-extents (lambda (e u) t) nil b e nil nil 'mm t)
884       (if (= b e)
885           (eq (cadr (memq 'gnus-undeletable (text-properties-at b))) t)
886         (text-property-any b e 'gnus-undeletable t))))
887
888 (defun gnus-xmas-mime-button-menu (event)
889   "Construct a context-sensitive menu of MIME commands."
890   (interactive "e")
891   (let ((response (get-popup-menu-response
892                    `("MIME Part"
893                      ,@(mapcar (lambda (c) `[,(caddr c) ,(car c) t])
894                                gnus-mime-button-commands)))))
895     (set-buffer (event-buffer event))
896     (goto-char (event-point event))
897     (funcall (event-function response) (event-object response))))
898
899 (defun gnus-group-add-icon ()
900   "Add an icon to the current line according to `gnus-group-icon-list'."
901   (let* ((p (point))
902          (end (progn (end-of-line) (point)))
903          ;; now find out where the line starts and leave point there.
904          (beg (progn (beginning-of-line) (point))))
905     (save-restriction
906       (narrow-to-region beg end)
907       (goto-char beg)
908       (when (search-forward "==&&==" nil t)
909         (let* ((group (gnus-group-group-name))
910                (entry (gnus-group-entry group))
911                (unread (if (numberp (car entry)) (car entry) 0))
912                (active (gnus-active group))
913                (total (if active (1+ (- (cdr active) (car active))) 0))
914                (info (nth 2 entry))
915                (method (gnus-server-get-method group (gnus-info-method info)))
916                (marked (gnus-info-marks info))
917                (mailp (memq 'mail (assoc (symbol-name
918                                           (car (or method gnus-select-method)))
919                                          gnus-valid-select-methods)))
920                (level (or (gnus-info-level info) gnus-level-killed))
921                (score (or (gnus-info-score info) 0))
922                (ticked (gnus-range-length (cdr (assq 'tick marked))))
923                (group-age (gnus-group-timestamp-delta group))
924                (inhibit-read-only t)
925                (list gnus-group-icon-list)
926                (mystart (match-beginning 0))
927                (myend (match-end 0)))
928           (goto-char (point-min))
929           (while (and list
930                       (not (eval (caar list))))
931             (setq list (cdr list)))
932           (if list
933               (let* ((file (cdar list))
934                      (glyph (gnus-group-icon-create-glyph
935                              (buffer-substring mystart myend)
936                              file)))
937                 (if glyph
938                     (progn
939                       (mapcar 'delete-annotation (annotations-at myend))
940                       (let ((ext (make-extent mystart myend))
941                             (ant (make-annotation glyph myend 'text)))
942                         ;; set text extent params
943                         (set-extent-property ext 'end-open t)
944                         (set-extent-property ext 'start-open t)
945                         (set-extent-property ext 'invisible t)))
946                   (delete-region mystart myend)))
947             (delete-region mystart myend))))
948       (widen))
949     (goto-char p)))
950
951 (defun gnus-group-icon-create-glyph (substring pixmap)
952   "Create a glyph for insertion into a group line."
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 (defun gnus-xmas-mailing-list-menu-add ()
968   (gnus-xmas-menu-add mailing-list
969                       gnus-mailing-list-menu))
970
971 (add-hook 'gnus-mailing-list-mode-hook 'gnus-xmas-mailing-list-menu-add)
972
973 (provide 'gnus-xmas)
974
975 ;;; gnus-xmas.el ends here