Some repo admin -- .gitignore updates
[packages] / xemacs-packages / hyperbole / hsmail.el
1 ;;; hsmail.el --- Support for Hyperbole buttons in mail composer: mail and mh-letter.
2
3 ;; Copyright (C) 1991-1995 Free Software Foundation, Inc.
4 ;; Developed with support from Motorola Inc.
5
6 ;; Author: Bob Weiner, Brown U.
7 ;; Maintainer: Mats Lidell <matsl@contactor.se>
8 ;; Keywords: hypermedia, mail
9
10 ;; This file is part of GNU Hyperbole.
11
12 ;; GNU Hyperbole is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 3, or (at
15 ;; your option) any later version.
16
17 ;; GNU Hyperbole is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 ;;;
32 ;;; Other required Elisp libraries
33 ;;;
34
35 (require 'sendmail)
36
37 ;;;
38 ;;; Public variables
39 ;;;
40
41
42 (defvar smail:comment '(format
43                         "Comments: Hyperbole mail buttons accepted, v%s.\n"
44                         hyperb:version)
45   "Default comment form to evaluate and add to outgoing mail.
46 Set to the empty string, \"\", for no comment.")
47
48 ;;; Used by 'mail-send' in Emacs "sendmail.el".
49 (if (boundp 'send-mail-function)
50     (or (if (listp send-mail-function)
51             (if (equal (nth 2 send-mail-function) '(smail:widen))
52                 nil
53               (error
54                 "(hsmail): Set 'send-mail-function' to a symbol-name, not a list, before load.")))
55         (setq send-mail-function
56               (list 'lambda nil '(smail:widen) (list send-mail-function))))
57   (error "(hsmail): Install an Emacs \"sendmail.el\" which includes 'send-mail-function'."))
58
59 (if (fboundp 'mail-prefix-region)
60     ;;
61     ;; For compatibility with rsw-modified sendmail.el.
62     (defvar mail-yank-hook
63       (function
64         (lambda ()
65           ;; Set off original message.
66           (mail-prefix-region (hypb:mark t) (point))))
67       "*Hook to run mail yank preface function.
68 Expects point and mark to be set to the region to preface.")
69   ;;
70   ;; Else for compatibility with Supercite and Emacs V19.
71   ;; If you create your own yank hook, set this variable rather than
72   ;; 'mail-yank-hook' from above.
73   (defvar mail-citation-hook nil
74     "*Hook for modifying a citation just inserted in the mail buffer.
75 Each hook function can find the citation between (point) and (mark t).
76 And each hook function should leave point and mark around the citation
77 text as modified.
78
79 If this hook is entirely empty (nil), a default action is taken
80 instead of no action.")
81   (defvar mail-yank-hooks '(mail-indent-citation)
82     "*Obsolete hook to run mail yank citation function.  Use mail-citation-hook instead.
83 Expects point and mark to be set to the region to cite."))
84
85 ;; For compatibility with Supercite and Emacs V19.
86 (defvar mail-yank-prefix nil
87   "*Prefix insert on lines of yanked message being replied to.
88 nil means use indentation.")
89 (defvar mail-indentation-spaces 3
90   "*Number of spaces to insert at the beginning of each cited line.")
91
92 ;;;
93 ;;; Public functions
94 ;;;
95
96 (defun smail:comment-add (&optional comment-form)
97   "Adds a comment to the current outgoing message if Hyperbole has been loaded.
98 Optional COMMENT-FORM is evaluated to obtain the string to add to the
99 message.  If not given, 'smail:comment' is evaluated by default."
100   (let ((comment (eval (or comment-form smail:comment))))
101     (if (and comment (featurep 'hsite))
102         (save-excursion
103           (goto-char (point-min))
104           (and (or (search-forward mail-header-separator nil t)
105                    (if (eq major-mode 'mh-letter-mode)
106                        (search-forward "\n--------" nil t)))
107                (not (search-backward comment nil t))
108                (progn (beginning-of-line) (insert comment)))))))
109
110 (defun smail:widen ()
111   "Widens outgoing mail buffer to include Hyperbole button data."
112   (if (fboundp 'mail+narrow) (mail+narrow) (widen)))
113
114 ;; Overlay this function from V19 "sendmail.el" to work with V18.
115 (defun mail-indent-citation ()
116   "Modify text just inserted from a message to be cited.
117 The inserted text should be the region.
118 When this function returns, the region is again around the modified text.
119
120 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
121 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
122   (let ((start (point)))
123     ;; Don't ever remove headers if user uses Supercite package,
124     ;; since he can set an option in that package to do
125     ;; the removal.
126     (or (hypb:supercite-p)
127         (mail-yank-clear-headers start (hypb:mark t)))
128     (if (null mail-yank-prefix)
129         (indent-rigidly start (hypb:mark t) mail-indentation-spaces)
130       (save-excursion
131         (goto-char start)
132         (while (< (point) (hypb:mark t))
133           (insert mail-yank-prefix)
134           (forward-line 1))))))
135
136 ;; Overlay this function from "sendmail.el" to include Hyperbole button
137 ;; data when yanking in a message and to highlight buttons if possible.
138 (defun mail-yank-original (arg)
139   "Insert the message being replied to, if any.
140 Puts point before the text and mark after.
141 Applies 'mail-citation-hook', 'mail-yank-hook' or 'mail-yank-hooks'
142 to text (in decreasing order of precedence).
143 Just \\[universal-argument] as argument means don't apply hooks
144 and don't delete any header fields.
145
146 If supercite is in use, header fields are never deleted.
147 Use (setq sc-nuke-mail-headers-p t) to have them removed."
148   (interactive "P")
149   (if mail-reply-buffer
150       (let ((start (point)) opoint)
151         (delete-windows-on mail-reply-buffer)
152         (unwind-protect
153             (progn
154               (save-excursion
155                 (set-buffer mail-reply-buffer)
156                 ;; Might be called from newsreader before any
157                 ;; Hyperbole mail reader support has been autoloaded.
158                 (cond ((fboundp 'rmail:msg-widen) (rmail:msg-widen))
159                       ((eq major-mode 'news-reply-mode) (widen))))
160               (setq opoint (point))
161               (insert-buffer mail-reply-buffer)
162               (hmail:msg-narrow)
163               (if (fboundp 'hproperty:but-create) (hproperty:but-create))
164               (if (consp arg)
165                   nil
166                 ;; Don't ever remove headers if user uses Supercite package,
167                 ;; since he can set an option in that package to do
168                 ;; the removal.
169                 (or (hypb:supercite-p)
170                     (mail-yank-clear-headers
171                       start (marker-position (hypb:mark-marker t))))
172                 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
173                                                  mail-indentation-spaces)))
174                   (cond ((and (boundp 'mail-citation-hook) mail-citation-hook)
175                          (run-hooks 'mail-citation-hook))
176                         ((and (boundp 'mail-yank-hook) mail-yank-hook)
177                          (run-hooks 'mail-yank-hook))
178                         ((and (boundp 'mail-yank-hooks) mail-yank-hooks)
179                          (run-hooks 'mail-yank-hooks))
180                         (t (mail-indent-citation))))
181                 (goto-char (min (point-max) (hypb:mark t)))
182                 (set-mark opoint)
183                 (delete-region (point)  ; Remove trailing blank lines.
184                                (progn (re-search-backward "[^ \^I\^L\n]")
185                                       (end-of-line)
186                                       (point))))
187               (or (eq major-mode 'news-reply-mode)
188                   ;; This is like exchange-point-and-mark, but doesn't activate the mark.
189                   ;; It is cleaner to avoid activation, even though the command
190                   ;; loop would deactivate the mark because we inserted text.
191                   (goto-char (prog1 (hypb:mark t)
192                                (set-marker (hypb:mark-marker t)
193                                            (point) (current-buffer)))))
194               (if (not (eolp)) (insert ?\n))
195               )
196           (save-excursion
197             (set-buffer mail-reply-buffer)
198             (hmail:msg-narrow))))))
199
200 ;;;
201 ;;; Private variables
202 ;;;
203
204 ;;; Try to setup comment addition as the first element of these hooks.
205 (if (fboundp 'add-hook)
206     (progn
207       (add-hook 'mail-setup-hook     'smail:comment-add)
208       (add-hook 'mh-letter-mode-hook 'smail:comment-add))
209   (var:append 'mail-setup-hook     '(smail:comment-add))
210   (var:append 'mh-letter-mode-hook '(smail:comment-add)))
211
212 (provide 'hsmail)
213
214 ;;; hsmail.el ends here