Indent.
[gnus] / lisp / messagexmas.el
1 ;;; messagexmas.el --- XEmacs extensions to message
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2003, 2004,
4 ;;   2005, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: mail, news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31 (require 'nnheader)
32
33 (defvar message-xmas-dont-activate-region t
34   "If t, don't activate region after yanking.")
35
36 (defvar message-xmas-glyph-directory nil
37   "*Directory where Message logos and icons are located.
38 If this variable is nil, Message will try to locate the directory
39 automatically.")
40
41 (defvar message-use-toolbar (if (featurep 'toolbar) 'default)
42   "*Position to display the toolbar.  Nil means do not use a toolbar.
43 If it is non-nil, it should be one of the symbols `default', `top',
44 `bottom', `right', and `left'.  `default' means to use the default
45 toolbar, the rest mean to display the toolbar on the place which those
46 names show.")
47
48 (defvar message-toolbar-thickness
49   (if (featurep 'toolbar)
50       (cons (specifier-instance default-toolbar-height)
51             (specifier-instance default-toolbar-width)))
52   "*Cons of the height and the width specifying the thickness of a toolbar.
53 The height is used for the toolbar displayed on the top or the bottom,
54 the width is used for the toolbar displayed on the right or the left.")
55
56 (defvar message-toolbar
57   '([message-spell ispell-message t "Spell"]
58     [message-help (Info-goto-node "(Message)Top") t "Message help"])
59   "The message buffer toolbar.")
60
61 (defun message-xmas-find-glyph-directory (&optional package)
62   (setq package (or package "message"))
63   (let ((dir (symbol-value
64               (intern-soft (concat package "-xmas-glyph-directory")))))
65     (if (and (stringp dir) (file-directory-p dir))
66         dir
67       (nnheader-find-etc-directory package))))
68
69 (defun message-xmas-setup-toolbar (bar &optional force package)
70   (let ((dir (or (message-xmas-find-glyph-directory package)
71                  (message-xmas-find-glyph-directory "gnus")))
72         (xpm (if (featurep 'xpm) "xpm" "xbm"))
73         icon up down disabled name)
74     (unless package
75       (setq message-xmas-glyph-directory dir))
76     (when dir
77       (while bar
78         (setq icon (aref (car bar) 0)
79               name (symbol-name icon)
80               bar (cdr bar))
81         (when (or force
82                   (not (boundp icon)))
83           (setq up (concat dir name "-up." xpm))
84           (setq down (concat dir name "-down." xpm))
85           (setq disabled (concat dir name "-disabled." xpm))
86           (if (not (file-exists-p up))
87               (setq bar nil
88                     dir nil)
89             (set icon (toolbar-make-button-list
90                        up (and (file-exists-p down) down)
91                        (and (file-exists-p disabled) disabled)))))))
92     dir))
93
94 (defun message-setup-toolbar ()
95   (when (featurep 'toolbar)
96     (if (and message-use-toolbar
97              (message-xmas-setup-toolbar message-toolbar))
98         (let ((bar (or (intern-soft (format "%s-toolbar" message-use-toolbar))
99                        'default-toolbar))
100               (height (car message-toolbar-thickness))
101               (width (cdr message-toolbar-thickness))
102               (cur (current-buffer))
103               bars)
104           (set-specifier (symbol-value bar) message-toolbar cur)
105           (set-specifier default-toolbar-height height cur)
106           (set-specifier default-toolbar-width width cur)
107           (set-specifier top-toolbar-height height cur)
108           (set-specifier bottom-toolbar-height height cur)
109           (set-specifier right-toolbar-width width cur)
110           (set-specifier left-toolbar-width width cur)
111           (if (eq bar 'default-toolbar)
112               (progn
113                 (remove-specifier default-toolbar-visible-p cur)
114                 (remove-specifier top-toolbar cur)
115                 (remove-specifier top-toolbar-visible-p cur)
116                 (remove-specifier bottom-toolbar cur)
117                 (remove-specifier bottom-toolbar-visible-p cur)
118                 (remove-specifier right-toolbar cur)
119                 (remove-specifier right-toolbar-visible-p cur)
120                 (remove-specifier left-toolbar cur)
121                 (remove-specifier left-toolbar-visible-p cur))
122             (set-specifier (symbol-value (intern (format "%s-visible-p" bar)))
123                            t cur)
124             (setq bars (delq bar (list 'default-toolbar
125                                        'bottom-toolbar 'top-toolbar
126                                        'right-toolbar 'left-toolbar)))
127             (while bars
128               (set-specifier (symbol-value (intern (format "%s-visible-p"
129                                                            (pop bars))))
130                              nil cur))))
131       (let ((cur (current-buffer)))
132         (set-specifier default-toolbar-visible-p nil cur)
133         (set-specifier top-toolbar-visible-p nil cur)
134         (set-specifier bottom-toolbar-visible-p nil cur)
135         (set-specifier right-toolbar-visible-p nil cur)
136         (set-specifier left-toolbar-visible-p nil cur)))))
137
138 (defun message-xmas-exchange-point-and-mark ()
139   "Exchange point and mark, but allow for XEmacs' optional argument."
140   (exchange-point-and-mark message-xmas-dont-activate-region))
141
142 (defun message-xmas-maybe-fontify ()
143   (when (featurep 'font-lock)
144     (font-lock-set-defaults)))
145
146 (defun message-xmas-make-caesar-translation-table (n)
147   "Create a rot table with offset N."
148   (let ((i -1)
149         (table (make-string 256 0))
150         (a (mm-char-int ?a))
151         (A (mm-char-int ?A)))
152     (while (< (incf i) 256)
153       (aset table i i))
154     (concat
155      (substring table 0 A)
156      (substring table (+ A n) (+ A n (- 26 n)))
157      (substring table A (+ A n))
158      (substring table (+ A 26) a)
159      (substring table (+ a n) (+ a n (- 26 n)))
160      (substring table a (+ a n))
161      (substring table (+ a 26) 255))))
162
163 (defun message-xmas-make-date (&optional now)
164   "Make a valid data header.
165 If NOW, use that time instead."
166   (let ((zone (car (current-time-zone)))
167         sign)
168     (if (>= zone 0)
169         (setq sign "+")
170       (setq sign "-"
171             zone (- zone)))
172     (format "%s %s%02d%02d"
173             (format-time-string "%a, %d %b %Y %T" now)
174             sign
175             (/ zone 3600)
176             (/ (% zone 3600) 60))))
177
178 (add-hook 'message-mode-hook 'message-xmas-maybe-fontify)
179
180 (defun message-xmas-redefine ()
181   "Redefine message functions for XEmacs."
182   (defalias 'message-exchange-point-and-mark
183     'message-xmas-exchange-point-and-mark)
184   (defalias 'message-mark-active-p
185     'region-exists-p)
186   (defalias 'message-make-caesar-translation-table
187     'message-xmas-make-caesar-translation-table)
188   (defalias 'message-make-overlay 'make-extent)
189   (defalias 'message-delete-overlay 'delete-extent)
190   (defalias 'message-overlay-put 'set-extent-property)
191   (defalias 'message-make-date 'message-xmas-make-date))
192
193 (message-xmas-redefine)
194
195 (provide 'messagexmas)
196
197 ;;; messagexmas.el ends here