/[cmucl]/src/hemlock/mh.lisp
ViewVC logotype

Contents of /src/hemlock/mh.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Mon Oct 31 04:50:12 1994 UTC (19 years, 5 months ago) by ram
Branch: MAIN
CVS Tags: sparc-tramp-assem-base, double-double-array-base, post-merge-intl-branch, release-19b-pre1, release-19b-pre2, merged-unicode-utf16-extfmt-2009-06-11, double-double-init-sparc-2, unicode-utf16-extfmt-2009-03-27, double-double-base, snapshot-2007-09, snapshot-2007-08, snapshot-2008-08, snapshot-2008-09, ppc_gencgc_snap_2006-01-06, sse2-packed-2008-11-12, snapshot-2008-05, snapshot-2008-06, snapshot-2008-07, snapshot-2007-05, snapshot-2008-01, snapshot-2008-02, snapshot-2008-03, intl-branch-working-2010-02-19-1000, snapshot-2006-11, snapshot-2006-10, double-double-init-sparc, snapshot-2006-12, unicode-string-buffer-impl-base, sse2-base, release-20b-pre1, release-20b-pre2, unicode-string-buffer-base, RELEASE_18d, sse2-packed-base, sparc-tramp-assem-2010-07-19, amd64-dd-start, snapshot-2003-10, snapshot-2004-10, release-18e-base, release-19f-pre1, snapshot-2008-12, snapshot-2008-11, intl-2-branch-base, snapshot-2004-08, snapshot-2004-09, remove_negative_zero_not_zero, snapshot-2007-01, snapshot-2007-02, snapshot-2004-05, snapshot-2004-06, snapshot-2004-07, release-19e, release-19d, GIT-CONVERSION, double-double-init-ppc, release-19c, dynamic-extent-base, unicode-utf16-sync-2008-12, LINKAGE_TABLE, release-19c-base, cross-sol-x86-merged, label-2009-03-16, release-19f-base, PRE_LINKAGE_TABLE, merge-sse2-packed, mod-arith-base, sparc_gencgc_merge, merge-with-19f, snapshot-2004-12, snapshot-2004-11, intl-branch-working-2010-02-11-1000, RELEASE_18a, RELEASE_18b, RELEASE_18c, unicode-snapshot-2009-05, unicode-snapshot-2009-06, amd64-merge-start, ppc_gencgc_snap_2005-12-17, double-double-init-%make-sparc, unicode-utf16-sync-2008-07, release-18e-pre2, unicode-utf16-sync-2008-09, unicode-utf16-extfmts-sync-2008-12, prm-before-macosx-merge-tag, cold-pcl-base, RELEASE_20b, snapshot-2008-04, snapshot-2003-11, snapshot-2005-07, unicode-utf16-sync-label-2009-03-16, RELEASE_19f, snapshot-2007-03, release-20a-base, cross-sol-x86-base, unicode-utf16-char-support-2009-03-26, unicode-utf16-char-support-2009-03-25, release-19a-base, unicode-utf16-extfmts-pre-sync-2008-11, snapshot-2008-10, sparc_gencgc, snapshot-2007-04, snapshot-2010-12, snapshot-2010-11, unicode-utf16-sync-2008-11, snapshot-2007-07, snapshot-2011-09, snapshot-2011-06, snapshot-2011-07, snapshot-2011-04, snapshot-2007-06, snapshot-2011-02, snapshot-2011-03, snapshot-2011-01, snapshot-2003-12, release-19a-pre1, release-19a-pre3, release-19a-pre2, pre-merge-intl-branch, release-19a, UNICODE-BASE, double-double-array-checkpoint, double-double-reader-checkpoint-1, release-19d-base, release-19e-pre1, double-double-irrat-end, release-19e-pre2, snapshot-2010-05, snapshot-2010-04, snapshot-2010-07, snapshot-2010-06, snapshot-2010-01, snapshot-2010-03, snapshot-2010-02, release-19d-pre2, release-19d-pre1, snapshot-2010-08, release-18e, double-double-init-checkpoint-1, double-double-reader-base, label-2009-03-25, snapshot-2005-03, release-19b-base, cross-sol-x86-2010-12-20, double-double-init-x86, sse2-checkpoint-2008-10-01, intl-branch-2010-03-18-1300, snapshot-2005-11, double-double-sparc-checkpoint-1, snapshot-2004-04, sse2-merge-with-2008-11, sse2-merge-with-2008-10, snapshot-2005-10, RELEASE_20a, snapshot-2005-12, release-20a-pre1, snapshot-2005-01, snapshot-2009-11, snapshot-2009-12, unicode-utf16-extfmt-2009-06-11, portable-clx-import-2009-06-16, unicode-utf16-string-support, release-19c-pre1, cross-sparc-branch-base, release-19e-base, intl-branch-base, double-double-irrat-start, snapshot-2005-06, snapshot-2005-05, snapshot-2005-04, ppc_gencgc_snap_2005-05-14, snapshot-2005-02, unicode-utf16-base, portable-clx-base, snapshot-2005-09, snapshot-2005-08, lisp-executable-base, snapshot-2009-08, snapshot-2007-12, snapshot-2007-10, snapshot-2007-11, snapshot-2009-02, snapshot-2009-01, snapshot-2009-07, snapshot-2009-05, snapshot-2009-04, snapshot-2006-02, snapshot-2006-03, release-18e-pre1, snapshot-2006-01, snapshot-2006-06, snapshot-2006-07, snapshot-2006-04, snapshot-2006-05, pre-telent-clx, snapshot-2006-08, snapshot-2006-09, HEAD
Branch point for: release-19b-branch, double-double-reader-branch, double-double-array-branch, mod-arith-branch, RELEASE-19F-BRANCH, portable-clx-branch, sparc_gencgc_branch, cross-sparc-branch, RELEASE-20B-BRANCH, RELENG_18, unicode-string-buffer-branch, sparc-tramp-assem-branch, dynamic-extent, UNICODE-BRANCH, release-19d-branch, ppc_gencgc_branch, sse2-packed-branch, lisp-executable, RELEASE-20A-BRANCH, amd64-dd-branch, double-double-branch, unicode-string-buffer-impl-branch, intl-branch, release-18e-branch, cold-pcl, unicode-utf16-branch, cross-sol-x86-branch, release-19e-branch, sse2-branch, release-19a-branch, release-19c-branch, intl-2-branch, unicode-utf16-extfmt-branch
Changes since 1.4: +1 -3 lines
Fix headed boilerplate.
<
1 ram 1.1 ;;; -*- Package: Hemlock; Log: hemlock.log -*-
2 ram 1.4 ;;;
3 ram 1.1 ;;; **********************************************************************
4 ram 1.4 ;;; This code was written as part of the CMU Common Lisp project at
5     ;;; Carnegie Mellon University, and has been placed in the public domain.
6     ;;;
7     (ext:file-comment
8 ram 1.5 "$Header: /tiger/var/lib/cvsroots/cmucl/src/hemlock/mh.lisp,v 1.5 1994/10/31 04:50:12 ram Rel $")
9 ram 1.4 ;;;
10 ram 1.1 ;;; **********************************************************************
11 ram 1.4 ;;;
12 ram 1.1 ;;; This is a mailer interface to MH.
13     ;;;
14     ;;; Written by Bill Chiles.
15     ;;;
16    
17     (in-package "HEMLOCK")
18    
19    
20    
21     ;;;; General stuff.
22    
23     (defvar *new-mail-buffer* nil)
24    
25     (defvar *mh-utility-bit-bucket* (make-broadcast-stream))
26    
27    
28     (defattribute "Digit"
29     "This is just a (mod 2) attribute for base 10 digit characters.")
30     ;;;
31     (dotimes (i 10)
32     (setf (character-attribute :digit (digit-char i)) 1))
33    
34    
35     (defmacro number-string (number)
36     `(let ((*print-base* 10))
37     (prin1-to-string ,number)))
38    
39    
40     (defmacro do-headers-buffers ((buffer-var folder &optional hinfo-var)
41     &rest forms)
42     "The Forms are evaluated with Buffer-Var bound to each buffer containing
43     headers lines for folder. Optionally Hinfo-Var is bound to the
44     headers-information structure."
45     (let ((folder-var (gensym))
46     (hinfo (gensym)))
47     `(let ((,folder-var ,folder))
48     (declare (simple-string ,folder-var))
49     (dolist (,buffer-var *buffer-list*)
50     (when (hemlock-bound-p 'headers-information :buffer ,buffer-var)
51     (let ((,hinfo (variable-value 'headers-information
52     :buffer ,buffer-var)))
53     (when (string= (the simple-string (headers-info-folder ,hinfo))
54     ,folder-var)
55     ,@(if hinfo-var
56     `((let ((,hinfo-var ,hinfo))
57     ,@forms))
58     forms))))))))
59    
60     (defmacro do-headers-lines ((hbuffer &key line-var mark-var) &rest forms)
61     "Forms are evaluated for each non-blank line. When supplied Line-Var and
62     Mark-Var are to the line and a :left-inserting mark at the beginning of the
63     line. This works with DELETE-HEADERS-BUFFER-LINE, but one should be careful
64     using this to modify the hbuffer."
65     (let ((line-var (or line-var (gensym)))
66     (mark-var (or mark-var (gensym)))
67     (id (gensym)))
68     `(with-mark ((,mark-var (buffer-point ,hbuffer) :left-inserting))
69     (buffer-start ,mark-var)
70     (loop
71     (let* ((,line-var (mark-line ,mark-var))
72     (,id (line-message-id ,line-var)))
73     (unless (blank-line-p ,line-var)
74     ,@forms)
75     (if (or (not (eq ,line-var (mark-line ,mark-var)))
76     (string/= ,id (line-message-id ,line-var)))
77     (line-start ,mark-var)
78     (unless (line-offset ,mark-var 1 0) (return))))))))
79    
80     (defmacro with-headers-mark ((mark-var hbuffer msg) &rest forms)
81     "Forms are executed with Mark-Var bound to a :left-inserting mark at the
82     beginning of the headers line representing msg. If no such line exists,
83     no execution occurs."
84     (let ((line (gensym)))
85     `(do-headers-lines (,hbuffer :line-var ,line :mark-var ,mark-var)
86     (when (string= (the simple-string (line-message-id ,line))
87     (the simple-string ,msg))
88     ,@forms
89     (return)))))
90    
91    
92    
93     ;;;; Headers Mode.
94    
95     (defmode "Headers" :major-p t)
96    
97     (defhvar "Headers Information"
98     "This holds the information about the current headers buffer."
99     :value nil)
100    
101     (defstruct (headers-info (:print-function print-headers-info))
102     buffer ;Buffer for these headers.
103     folder ;String name of folder with leading MH "+".
104     msg-seq ;MH sequence of messages in buffer.
105     msg-strings ;List of strings representing msg-seq.
106     other-msg-bufs ;List of message buffers referencing this headers buffer.
107     draft-bufs ;List of draft buffers referencing this headers buffer.
108     msg-buffer)
109    
110     (defun print-headers-info (obj str n)
111     (declare (ignore n))
112     (format str "#<Headers Info ~S>" (headers-info-folder obj)))
113    
114     (defmacro line-message-deleted (line)
115     `(getf (line-plist ,line) 'mh-msg-deleted))
116    
117     (defmacro line-message-id (line)
118     `(getf (line-plist ,line) 'mh-msg-id))
119    
120     (defun headers-current-message (hinfo)
121     (let* ((point (buffer-point (headers-info-buffer hinfo)))
122     (line (mark-line point)))
123     (unless (blank-line-p line)
124     (values (line-message-id line)
125     (copy-mark point)))))
126    
127     (defcommand "Message Headers" (p)
128     "Prompts for a folder and messages, displaying headers in a buffer in the
129     current window. With an argument, prompt for a pick expression."
130     "Show some headers."
131     (let ((folder (prompt-for-folder)))
132     (new-message-headers
133     folder
134     (prompt-for-message :prompt (if p
135     "MH messages to pick from: "
136     "MH messages: ")
137     :folder folder
138     :messages "all")
139     p)))
140    
141     (defcommand "Pick Headers" (p)
142     "Further narrow the selection of this folders headers.
143     Prompts for a pick expression to pick over the headers in the current
144     buffer. Entering an empty expression displays all the headers for that
145     folder."
146     "Prompts for a pick expression to pick over the headers in the current
147     buffer."
148     (declare (ignore p))
149     (let ((hinfo (value headers-information)))
150     (unless hinfo
151     (editor-error "Pick Headers only works in a headers buffer."))
152     (pick-message-headers hinfo)))
153    
154     ;;; PICK-MESSAGE-HEADERS picks messages from info's messages based on an
155     ;;; expression provided by the user. If the expression is empty, we do
156     ;;; headers on all the messages in folder. The buffer's name is changed to
157     ;;; reflect the messages picked over and the expression used.
158     ;;;
159     (defun pick-message-headers (hinfo)
160     (let ((folder (headers-info-folder hinfo))
161     (msgs (headers-info-msg-strings hinfo)))
162     (multiple-value-bind (pick user-pick)
163     (prompt-for-pick-expression)
164     (let* ((hbuffer (headers-info-buffer hinfo))
165     (new-mail-buf-p (eq hbuffer *new-mail-buffer*))
166     (region (cond (pick
167     (message-headers-to-region
168     folder (pick-messages folder msgs pick)))
169     (new-mail-buf-p
170     (maybe-get-new-mail-msg-hdrs folder))
171     (t (message-headers-to-region folder
172     (list "all"))))))
173     (with-writable-buffer (hbuffer)
174     (revamp-headers-buffer hbuffer hinfo)
175     (when region (insert-message-headers hbuffer hinfo region)))
176     (setf (buffer-modified hbuffer) nil)
177     (buffer-start (buffer-point hbuffer))
178     (setf (buffer-name hbuffer)
179     (cond (pick (format nil "Headers ~A ~A ~A" folder msgs user-pick))
180     (new-mail-buf-p (format nil "Unseen Headers ~A" folder))
181     (t (format nil "Headers ~A (all)" folder))))))))
182    
183     ;;; NEW-MESSAGE-HEADERS picks over msgs if pickp is non-nil, or it just scans
184     ;;; msgs. It is important to pick and get the message headers region before
185     ;;; making the buffer and info structures since PICK-MESSAGES and
186     ;;; MESSAGE-HEADERS-TO-REGION will call EDITOR-ERROR if they fail. The buffer
187     ;;; name is chosen based on folder, msgs, and an optional pick expression.
188     ;;;
189     (defun new-message-headers (folder msgs &optional pickp)
190     (multiple-value-bind (pick-exp user-pick)
191     (if pickp (prompt-for-pick-expression))
192     (let* ((pick (if pick-exp (pick-messages folder msgs pick-exp)))
193     (region (message-headers-to-region folder (or pick msgs)))
194     (hbuffer (maybe-make-mh-buffer (format nil "Headers ~A ~A~:[~; ~S~]"
195     folder msgs pick user-pick)
196     :headers))
197     (hinfo (make-headers-info :buffer hbuffer :folder folder)))
198     (insert-message-headers hbuffer hinfo region)
199     (defhvar "Headers Information"
200     "This holds the information about the current headers buffer."
201     :value hinfo :buffer hbuffer)
202     (setf (buffer-modified hbuffer) nil)
203     (setf (buffer-writable hbuffer) nil)
204     (buffer-start (buffer-point hbuffer))
205     (change-to-buffer hbuffer))))
206    
207     (defhvar "MH Scan Line Form"
208     "This is a pathname of a file containing an MH format expression for headers
209     lines."
210 ram 1.4 :value (pathname "library:mh-scan"))
211 ram 1.1
212     ;;; MESSAGE-HEADERS-TO-REGION uses the MH "scan" utility output headers into
213     ;;; buffer for folder and msgs.
214     ;;;
215     ;;; (value fill-column) should really be done as if the buffer were current,
216     ;;; but Hemlock doesn't let you do this without the buffer being current.
217     ;;;
218     (defun message-headers-to-region (folder msgs &optional width)
219     (let ((region (make-empty-region)))
220     (with-output-to-mark (*standard-output* (region-end region) :full)
221     (mh "scan"
222     `(,folder ,@msgs
223 ram 1.4 "-form" ,(namestring (truename (value mh-scan-line-form)))
224 ram 1.1 "-width" ,(number-string (or width (value fill-column)))
225     "-noheader")))
226     region))
227    
228     (defun insert-message-headers (hbuffer hinfo region)
229     (ninsert-region (buffer-point hbuffer) region)
230     (let ((seq (set-message-headers-ids hbuffer :return-seq)))
231     (setf (headers-info-msg-seq hinfo) seq)
232     (setf (headers-info-msg-strings hinfo) (mh-sequence-strings seq)))
233     (when (value virtual-message-deletion)
234     (note-deleted-headers hbuffer
235     (mh-sequence-list (headers-info-folder hinfo)
236     "hemlockdeleted"))))
237    
238     (defun set-message-headers-ids (hbuffer &optional return-seq)
239     (let ((msgs nil))
240     (do-headers-lines (hbuffer :line-var line)
241     (let* ((line-str (line-string line))
242     (num (parse-integer line-str :junk-allowed t)))
243     (declare (simple-string line-str))
244     (unless num
245     (editor-error "MH scan lines must contain the message id as the ~
246     first thing on the line for the Hemlock interface."))
247     (setf (line-message-id line) (number-string num))
248     (when return-seq (setf msgs (mh-sequence-insert num msgs)))))
249     msgs))
250    
251     (defun note-deleted-headers (hbuffer deleted-seq)
252     (when deleted-seq
253     (do-headers-lines (hbuffer :line-var line :mark-var hmark)
254     (if (mh-sequence-member-p (line-message-id line) deleted-seq)
255     (note-deleted-message-at-mark hmark)
256     (setf (line-message-deleted line) nil)))))
257    
258     ;;; PICK-MESSAGES -- Internal Interface.
259     ;;;
260     ;;; This takes a folder (with a + in front of the name), messages to pick
261     ;;; over, and an MH pick expression (in the form returned by
262     ;;; PROMPT-FOR-PICK-EXPRESSION). Sequence is an MH sequence to set to exactly
263     ;;; those messages chosen by the pick when zerop is non-nil; when zerop is nil,
264     ;;; pick adds the messages to the sequence along with whatever messages were
265     ;;; already in the sequence. This returns a list of message specifications.
266     ;;;
267     (defun pick-messages (folder msgs expression &optional sequence (zerop t))
268     (let* ((temp (with-output-to-string (*standard-output*)
269     (unless
270     ;; If someone bound *signal-mh-errors* to nil around this
271     ;; function, MH pick outputs bogus messages (for example,
272     ;; "0"), and MH would return without calling EDITOR-ERROR.
273     (mh "pick" `(,folder
274     ,@msgs
275     ,@(if sequence `("-sequence" ,sequence))
276     ,@(if zerop '("-zero"))
277     "-list" ; -list must follow -sequence.
278     ,@expression))
279     (return-from pick-messages nil))))
280     (len (length temp))
281     (start 0)
282     (result nil))
283     (declare (simple-string temp))
284     (loop
285     (let ((end (position #\newline temp :start start :test #'char=)))
286     (cond ((not end)
287     (return (nreverse (cons (subseq temp start) result))))
288     ((= start end)
289     (return (nreverse result)))
290     (t
291     (push (subseq temp start end) result)
292     (when (>= (setf start (1+ end)) len)
293     (return (nreverse result)))))))))
294    
295    
296     (defcommand "Delete Headers Buffer and Message Buffers" (p &optional buffer)
297     "Prompts for a headers buffer to delete along with its associated message
298     buffers. Any associated draft buffers are left alone, but their associated
299     message buffers will be deleted."
300     "Deletes the current headers buffer and its associated message buffers."
301     (declare (ignore p))
302     (let* ((default (cond ((value headers-information) (current-buffer))
303     ((value message-information) (value headers-buffer))))
304     (buffer (or buffer
305     (prompt-for-buffer :default default
306     :default-string
307     (if default (buffer-name default))))))
308     (unless (hemlock-bound-p 'headers-information :buffer buffer)
309     (editor-error "Not a headers buffer -- ~A" (buffer-name buffer)))
310     (let* ((hinfo (variable-value 'headers-information :buffer buffer))
311     ;; Copy list since buffer cleanup hook is destructive.
312     (other-bufs (copy-list (headers-info-other-msg-bufs hinfo)))
313     (msg-buf (headers-info-msg-buffer hinfo)))
314     (when msg-buf (delete-buffer-if-possible msg-buf))
315     (dolist (b other-bufs) (delete-buffer-if-possible b))
316     (delete-buffer-if-possible (headers-info-buffer hinfo)))))
317    
318     (defhvar "Expunge Messages Confirm"
319     "When set (the default), \"Expunge Messages\" and \"Quit Headers\" will ask
320     for confirmation before expunging messages and packing the folder's message
321     id's."
322     :value t)
323    
324     (defhvar "Temporary Draft Folder"
325     "This is the folder name where MH fcc: messages are kept that are intended
326     to be deleted and expunged when messages are expunged for any other
327     folder -- \"Expunge Messages\" and \"Quit Headers\"."
328     :value nil)
329    
330     ;;; "Quit Headers" doesn't expunge or compact unless there is a deleted
331     ;;; sequence. This collapses other headers buffers into the same folder
332     ;;; differently than "Expunge Messages" since the latter assumes there will
333     ;;; always be one remaining headers buffer. This command folds all headers
334     ;;; buffers into the folder that are not the current buffer or the new mail
335     ;;; buffer into one buffer. When the current buffer is the new mail buffer
336     ;;; we do not check for more unseen headers since we are about to delete
337     ;;; the buffer anyway. The other headers buffers must be deleted before
338     ;;; making the new one due to aliasing the buffer structure and
339     ;;; MAYBE-MAKE-MH-BUFFER.
340     ;;;
341     (defcommand "Quit Headers" (p)
342     "Quit headers buffer possibly expunging deleted messages.
343     This affects the current headers buffer. When there are deleted messages
344     the user is asked for confirmation on expunging the messages and packing the
345     folder's message id's. Then the buffer and all its associated message
346     buffers are deleted. Setting \"Quit Headers Confirm\" to nil inhibits
347     prompting. When \"Temporary Draft Folder\" is bound, this folder's messages
348     are deleted and expunged."
349     "This affects the current headers buffer. When there are deleted messages
350     the user is asked for confirmation on expunging the messages and packing
351     the folder. Then the buffer and all its associated message buffers are
352     deleted."
353     (declare (ignore p))
354     (let* ((hinfo (value headers-information))
355     (minfo (value message-information))
356     (hdrs-buf (cond (hinfo (current-buffer))
357     (minfo (value headers-buffer)))))
358     (unless hdrs-buf
359     (editor-error "Not in or associated with any headers buffer."))
360     (let* ((folder (cond (hinfo (headers-info-folder hinfo))
361     (minfo (message-info-folder minfo))))
362     (deleted-seq (mh-sequence-list folder "hemlockdeleted")))
363     (when (and deleted-seq
364     (or (not (value expunge-messages-confirm))
365     (prompt-for-y-or-n
366     :prompt (list "Expunge messages and pack folder ~A? "
367     folder)
368     :default t
369     :default-string "Y")))
370     (message "Deleting messages ...")
371     (mh "rmm" (list folder "hemlockdeleted"))
372     (let ((*standard-output* *mh-utility-bit-bucket*))
373     (message "Compacting folder ...")
374     (mh "folder" (list folder "-fast" "-pack")))
375     (message "Maintaining consistency ...")
376     (let (hbufs)
377     (declare (list hbufs))
378     (do-headers-buffers (b folder)
379     (unless (or (eq b hdrs-buf) (eq b *new-mail-buffer*))
380     (push b hbufs)))
381     (dolist (b hbufs)
382     (delete-headers-buffer-and-message-buffers-command nil b))
383     (when hbufs
384     (new-message-headers folder (list "all"))))
385     (expunge-messages-fix-draft-buffers folder)
386     (unless (eq hdrs-buf *new-mail-buffer*)
387     (expunge-messages-fix-unseen-headers folder))
388     (delete-and-expunge-temp-drafts)))
389     (delete-headers-buffer-and-message-buffers-command nil hdrs-buf)))
390    
391     ;;; DELETE-AND-EXPUNGE-TEMP-DRAFTS deletes all the messages in the
392     ;;; temporary draft folder if there is one defined. Any headers buffers
393     ;;; into this folder are deleted with their message buffers. We have to
394     ;;; create a list of buffers to delete since buffer deletion destructively
395     ;;; modifies the same list DO-HEADERS-BUFFERS uses. "rmm" is run without
396     ;;; error reporting since it signals an error if there are no messages to
397     ;;; delete. This function must return; for example, "Quit Headers" would
398     ;;; not complete successfully if this ended up calling EDITOR-ERROR.
399     ;;;
400     (defun delete-and-expunge-temp-drafts ()
401     (let ((temp-draft-folder (value temporary-draft-folder)))
402     (when temp-draft-folder
403     (setf temp-draft-folder (coerce-folder-name temp-draft-folder))
404     (message "Deleting and expunging temporary drafts ...")
405     (when (mh "rmm" (list temp-draft-folder "all") :errorp nil)
406     (let (hdrs)
407     (declare (list hdrs))
408     (do-headers-buffers (b temp-draft-folder)
409     (push b hdrs))
410     (dolist (b hdrs)
411     (delete-headers-buffer-and-message-buffers-command nil b)))))))
412    
413    
414    
415     ;;;; Message Mode.
416    
417     (defmode "Message" :major-p t)
418    
419     (defhvar "Message Information"
420     "This holds the information about the current message buffer."
421     :value nil)
422    
423     (defstruct message/draft-info
424     headers-mark) ;Mark pointing to a headers line in a headers buffer.
425    
426     (defstruct (message-info (:include message/draft-info)
427     (:print-function print-message-info))
428     folder ;String name of folder with leading MH "+".
429     msgs ;List of strings representing messages to be shown.
430     draft-buf ;Possible draft buffer reference.
431     keep) ;Whether message buffer may be re-used.
432    
433     (defun print-message-info (obj str n)
434     (declare (ignore n))
435     (format str "#<Message Info ~S ~S>"
436     (message-info-folder obj) (message-info-msgs obj)))
437    
438    
439     (defcommand "Next Message" (p)
440     "Show the next message.
441     When in a message buffer, shows the next message in the associated headers
442     buffer. When in a headers buffer, moves point down a line and shows that
443     message."
444     "When in a message buffer, shows the next message in the associated headers
445     buffer. When in a headers buffer, moves point down a line and shows that
446     message."
447     (declare (ignore p))
448     (show-message-offset 1))
449    
450     (defcommand "Previous Message" (p)
451     "Show the previous message.
452     When in a message buffer, shows the previous message in the associated
453     headers buffer. When in a headers buffer, moves point up a line and shows
454     that message."
455     "When in a message buffer, shows the previous message in the associated
456     headers buffer. When in a headers buffer, moves point up a line and
457     shows that message."
458     (declare (ignore p))
459     (show-message-offset -1))
460    
461     (defcommand "Next Undeleted Message" (p)
462     "Show the next undeleted message.
463     When in a message buffer, shows the next undeleted message in the associated
464     headers buffer. When in a headers buffer, moves point down to a line
465     without a deleted message and shows that message."
466     "When in a message buffer, shows the next undeleted message in the associated
467     headers buffer. When in a headers buffer, moves point down to a line without
468     a deleted message and shows that message."
469     (declare (ignore p))
470     (show-message-offset 1 :undeleted))
471    
472     (defcommand "Previous Undeleted Message" (p)
473     "Show the previous undeleted message.
474     When in a message buffer, shows the previous undeleted message in the
475     associated headers buffer. When in a headers buffer, moves point up a line
476     without a deleted message and shows that message."
477     "When in a message buffer, shows the previous undeleted message in the
478     associated headers buffer. When in a headers buffer, moves point up a line
479     without a deleted message and shows that message."
480     (declare (ignore p))
481     (show-message-offset -1 :undeleted))
482    
483     (defun show-message-offset (offset &optional undeleted)
484     (let ((minfo (value message-information)))
485     (cond
486     ((not minfo)
487     (let ((hinfo (value headers-information)))
488     (unless hinfo (editor-error "Not in a message or headers buffer."))
489     (show-message-offset-hdrs-buf hinfo offset undeleted)))
490     ((message-info-keep minfo)
491     (let ((hbuf (value headers-buffer)))
492     (unless hbuf (editor-error "Not associated with a headers buffer."))
493     (let ((hinfo (variable-value 'headers-information :buffer hbuf))
494     (point (buffer-point hbuf)))
495     (move-mark point (message-info-headers-mark minfo))
496     (show-message-offset-hdrs-buf hinfo offset undeleted))))
497     (t
498     (show-message-offset-msg-buf minfo offset undeleted)))))
499    
500     (defun show-message-offset-hdrs-buf (hinfo offset undeleted)
501     (unless hinfo (editor-error "Not in a message or headers buffer."))
502     (unless (show-message-offset-mark (buffer-point (headers-info-buffer hinfo))
503     offset undeleted)
504     (editor-error "No ~:[previous~;next~] ~:[~;undeleted ~]message."
505     (plusp offset) undeleted))
506     (show-headers-message hinfo))
507    
508     (defun show-message-offset-msg-buf (minfo offset undeleted)
509     (let ((msg-mark (message-info-headers-mark minfo)))
510     (unless msg-mark (editor-error "Not associated with a headers buffer."))
511     (unless (show-message-offset-mark msg-mark offset undeleted)
512     (let ((hbuf (value headers-buffer))
513     (mbuf (current-buffer)))
514     (setf (current-buffer) hbuf)
515     (setf (window-buffer (current-window)) hbuf)
516     (delete-buffer-if-possible mbuf))
517     (editor-error "No ~:[previous~;next~] ~:[~;undeleted ~]message."
518     (plusp offset) undeleted))
519     (move-mark (buffer-point (line-buffer (mark-line msg-mark))) msg-mark)
520     (let* ((next-msg (line-message-id (mark-line msg-mark)))
521     (folder (message-info-folder minfo))
522     (mbuffer (current-buffer)))
523     (with-writable-buffer (mbuffer)
524     (delete-region (buffer-region mbuffer))
525     (setf (buffer-name mbuffer) (get-storable-msg-buf-name folder next-msg))
526     (setf (message-info-msgs minfo) next-msg)
527     (read-mh-file (merge-pathnames next-msg
528     (merge-relative-pathnames
529     (strip-folder-name folder)
530     (mh-directory-pathname)))
531     mbuffer)
532     (let ((unseen-seq (mh-profile-component "unseen-sequence")))
533     (when unseen-seq
534     (mark-one-message folder next-msg unseen-seq :delete))))))
535     (let ((dbuffer (message-info-draft-buf minfo)))
536     (when dbuffer
537     (delete-variable 'message-buffer :buffer dbuffer)
538     (setf (message-info-draft-buf minfo) nil))))
539    
540     (defun get-storable-msg-buf-name (folder msg)
541     (let ((name (format nil "Message ~A ~A" folder msg)))
542     (if (not (getstring name *buffer-names*))
543     name
544     (let ((n 2))
545     (loop
546     (setf name (format nil "Message ~A ~A copy ~D" folder msg n))
547     (unless (getstring name *buffer-names*)
548     (return name))
549     (incf n))))))
550    
551     (defun show-message-offset-mark (msg-mark offset undeleted)
552     (with-mark ((temp msg-mark))
553     (let ((winp
554     (cond (undeleted
555     (loop
556     (unless (and (line-offset temp offset 0)
557     (not (blank-line-p (mark-line temp))))
558     (return nil))
559     (unless (line-message-deleted (mark-line temp))
560     (return t))))
561     ((and (line-offset temp offset 0)
562     (not (blank-line-p (mark-line temp)))))
563     (t nil))))
564     (if winp (move-mark msg-mark temp)))))
565    
566    
567     (defcommand "Show Message" (p)
568     "Shows the current message.
569     Prompts for a folder and message(s), displaying this in the current window.
570     When invoked in a headers buffer, shows the message on the current line."
571     "Show a message."
572     (declare (ignore p))
573     (let ((hinfo (value headers-information)))
574     (if hinfo
575     (show-headers-message hinfo)
576     (let ((folder (prompt-for-folder)))
577     (show-prompted-message folder (prompt-for-message :folder folder))))))
578    
579     ;;; SHOW-HEADERS-MESSAGE shows the current message for hinfo. If there is a
580     ;;; main message buffer, clobber it, and we don't have to deal with kept
581     ;;; messages or draft associations since those operations should have moved
582     ;;; the message buffer into the others list. Remove the message from the
583     ;;; unseen sequence, and make sure the message buffer is displayed in some
584     ;;; window.
585     ;;;
586     (defun show-headers-message (hinfo)
587     (multiple-value-bind (cur-msg cur-mark)
588     (headers-current-message hinfo)
589     (unless cur-msg (editor-error "Not on a header line."))
590     (let* ((mbuffer (headers-info-msg-buffer hinfo))
591     (folder (headers-info-folder hinfo))
592     (buf-name (get-storable-msg-buf-name folder cur-msg))
593     (writable nil))
594     (cond (mbuffer
595     (setf (buffer-name mbuffer) buf-name)
596     (setf writable (buffer-writable mbuffer))
597     (setf (buffer-writable mbuffer) t)
598     (delete-region (buffer-region mbuffer))
599     (let ((minfo (variable-value 'message-information :buffer mbuffer)))
600     (move-mark (message-info-headers-mark minfo) cur-mark)
601     (delete-mark cur-mark)
602     (setf (message-info-msgs minfo) cur-msg)))
603     (t (setf mbuffer (maybe-make-mh-buffer buf-name :message))
604     (setf (headers-info-msg-buffer hinfo) mbuffer)
605     (defhvar "Message Information"
606     "This holds the information about the current headers buffer."
607     :value (make-message-info :folder folder
608     :msgs cur-msg
609     :headers-mark cur-mark)
610     :buffer mbuffer)
611     (defhvar "Headers Buffer"
612     "This is bound in message and draft buffers to their
613     associated headers buffer."
614     :value (headers-info-buffer hinfo) :buffer mbuffer)))
615     (read-mh-file (merge-pathnames
616     cur-msg
617     (merge-relative-pathnames (strip-folder-name folder)
618     (mh-directory-pathname)))
619     mbuffer)
620     (setf (buffer-writable mbuffer) writable)
621     (let ((unseen-seq (mh-profile-component "unseen-sequence")))
622     (when unseen-seq (mark-one-message folder cur-msg unseen-seq :delete)))
623     (get-message-buffer-window mbuffer))))
624    
625     ;;; SHOW-PROMPTED-MESSAGE takes an arbitrary message spec and blasts those
626     ;;; messages into a message buffer. First we pick the message to get them
627     ;;; individually specified as normalized message ID's -- all integers and
628     ;;; no funny names such as "last".
629     ;;;
630     (defun show-prompted-message (folder msgs)
631     (let* ((msgs (pick-messages folder msgs nil))
632     (mbuffer (maybe-make-mh-buffer (format nil "Message ~A ~A" folder msgs)
633     :message)))
634     (defhvar "Message Information"
635     "This holds the information about the current headers buffer."
636     :value (make-message-info :folder folder :msgs msgs)
637     :buffer mbuffer)
638     (let ((*standard-output* (make-hemlock-output-stream (buffer-point mbuffer)
639     :full)))
640     (mh "show" `(,folder ,@msgs "-noshowproc" "-noheader"))
641     (setf (buffer-modified mbuffer) nil))
642     (buffer-start (buffer-point mbuffer))
643     (setf (buffer-writable mbuffer) nil)
644     (get-message-buffer-window mbuffer)))
645    
646     ;;; GET-MESSAGE-BUFFER-WINDOW currently just changes to buffer, unless buffer
647     ;;; has any windows, in which case it uses the first one. It could prompt for
648     ;;; a window, split the current window, split the current window or use the
649     ;;; next one if there is one, funcall an Hvar. It could take a couple
650     ;;; arguments to control its behaviour. Whatever.
651     ;;;
652     (defun get-message-buffer-window (mbuffer)
653     (let ((wins (buffer-windows mbuffer)))
654     (cond (wins
655     (setf (current-buffer) mbuffer)
656     (setf (current-window) (car wins)))
657     (t (change-to-buffer mbuffer)))))
658    
659    
660     (defhvar "Scroll Message Showing Next"
661     "When this is set, \"Scroll Message\" shows the next message when the end
662     of the current message is visible."
663     :value t)
664    
665     (defcommand "Scroll Message" (p)
666     "Scroll the current window down through the current message.
667     If the end of the message is visible, then show the next undeleted message
668     if \"Scroll Message Showing Next\" is non-nil."
669     "Scroll the current window down through the current message."
670     (if (and (not p)
671     (displayed-p (buffer-end-mark (current-buffer)) (current-window))
672     (value scroll-message-showing-next))
673     (show-message-offset 1 :undeleted)
674     (scroll-window-down-command p)))
675    
676    
677     (defcommand "Keep Message" (p)
678     "Keeps the current message buffer from being re-used. Also, if the buffer
679     would be deleted due to a draft completion, it will not be."
680     "Keeps the current message buffer from being re-used. Also, if the buffer
681     would be deleted due to a draft completion, it will not be."
682     (declare (ignore p))
683     (let ((minfo (value message-information)))
684     (unless minfo (editor-error "Not in a message buffer."))
685     (let ((hbuf (value headers-buffer)))
686     (when hbuf
687     (let ((mbuf (current-buffer))
688     (hinfo (variable-value 'headers-information :buffer hbuf)))
689     (when (eq (headers-info-msg-buffer hinfo) mbuf)
690     (setf (headers-info-msg-buffer hinfo) nil)
691     (push mbuf (headers-info-other-msg-bufs hinfo))))))
692     (setf (message-info-keep minfo) t)))
693    
694     (defcommand "Edit Message Buffer" (p)
695     "Recursively edit message buffer.
696     Puts the current message buffer into \"Text\" mode allowing modifications in
697     a recursive edit. While in this state, the buffer is associated with the
698     pathname of the message, so saving the file is possible."
699     "Puts the current message buffer into \"Text\" mode allowing modifications in
700     a recursive edit. While in this state, the buffer is associated with the
701     pathname of the message, so saving the file is possible."
702     (declare (ignore p))
703     (let* ((minfo (value message-information)))
704     (unless minfo (editor-error "Not in a message buffer."))
705     (let* ((msgs (message-info-msgs minfo))
706     (mbuf (current-buffer))
707     (mbuf-name (buffer-name mbuf))
708     (writable (buffer-writable mbuf))
709     (abortp t))
710     (when (consp msgs)
711     (editor-error
712     "There appears to be more than one message in this buffer."))
713     (unwind-protect
714     (progn
715     (setf (buffer-writable mbuf) t)
716     (setf (buffer-pathname mbuf)
717     (merge-pathnames
718     msgs
719     (merge-relative-pathnames
720     (strip-folder-name (message-info-folder minfo))
721     (mh-directory-pathname))))
722     (setf (buffer-major-mode mbuf) "Text")
723     (do-recursive-edit)
724     (setf abortp nil))
725     (when (and (not abortp)
726     (buffer-modified mbuf)
727     (prompt-for-y-or-n
728     :prompt "Message buffer modified, save it? "
729     :default t))
730     (save-file-command nil mbuf))
731     (setf (buffer-modified mbuf) nil)
732     ;; "Save File", which the user may have used, changes the buffer's name.
733     (unless (getstring mbuf-name *buffer-names*)
734     (setf (buffer-name mbuf) mbuf-name))
735     (setf (buffer-writable mbuf) writable)
736     (setf (buffer-pathname mbuf) nil)
737     (setf (buffer-major-mode mbuf) "Message")))))
738    
739    
740    
741     ;;;; Draft Mode.
742    
743     (defmode "Draft")
744    
745     (defhvar "Draft Information"
746     "This holds the information about the current draft buffer."
747     :value nil)
748    
749     (defstruct (draft-info (:include message/draft-info)
750     (:print-function print-draft-info))
751     folder ;String name of draft folder with leading MH "+".
752     message ;String id of draft folder message.
753     pathname ;Pathname of draft in the draft folder directory.
754     delivered ;This is set when the draft was really sent.
755     replied-to-folder ;Folder of message draft is in reply to.
756     replied-to-msg) ;Message draft is in reply to.
757    
758     (defun print-draft-info (obj str n)
759     (declare (ignore n))
760     (format str "#<Draft Info ~A>" (draft-info-message obj)))
761    
762    
763     (defhvar "Reply to Message Prefix Action"
764     "This is one of :cc-all, :no-cc-all, or nil. When an argument is supplied to
765     \"Reply to Message\", this value determines how arguments passed to the
766     MH utility."
767     :value nil)
768    
769     (defcommand "Reply to Message" (p)
770     "Sets up a draft in reply to the current message.
771     Prompts for a folder and message to reply to. When in a headers buffer,
772     replies to the message on the current line. When in a message buffer,
773     replies to that message. With an argument, regard \"Reply to Message Prefix
774     Action\" for carbon copy arguments to the MH utility."
775     "Prompts for a folder and message to reply to. When in a headers buffer,
776     replies to the message on the current line. When in a message buffer,
777     replies to that message."
778     (let ((hinfo (value headers-information))
779     (minfo (value message-information)))
780     (cond (hinfo
781     (multiple-value-bind (cur-msg cur-mark)
782     (headers-current-message hinfo)
783     (unless cur-msg (editor-error "Not on a header line."))
784     (setup-reply-draft (headers-info-folder hinfo)
785     cur-msg hinfo cur-mark p)))
786     (minfo
787     (setup-message-buffer-draft (current-buffer) minfo :reply p))
788     (t
789     (let ((folder (prompt-for-folder)))
790     (setup-reply-draft folder
791     (car (prompt-for-message :folder folder))
792     nil nil p))))))
793    
794     ;;; SETUP-REPLY-DRAFT takes a folder and msg to draft a reply to. Optionally,
795     ;;; a headers buffer and mark are associated with the draft. First, the draft
796     ;;; buffer is associated with the headers buffer if there is one. Then the
797     ;;; message buffer is created and associated with the drafter buffer and
798     ;;; headers buffer. Argument may be used to pass in the argument from the
799     ;;; command.
800     ;;;
801     (defun setup-reply-draft (folder msg &optional hinfo hmark argument)
802     (let* ((dbuffer (sub-setup-message-draft
803     "repl" :end-of-buffer
804     `(,folder ,msg
805     ,@(if argument
806     (case (value reply-to-message-prefix-action)
807     (:no-cc-all '("-nocc" "all"))
808     (:cc-all '("-cc" "all")))))))
809     (dinfo (variable-value 'draft-information :buffer dbuffer))
810     (h-buf (if hinfo (headers-info-buffer hinfo))))
811     (setf (draft-info-replied-to-folder dinfo) folder)
812     (setf (draft-info-replied-to-msg dinfo) msg)
813     (when h-buf
814     (defhvar "Headers Buffer"
815     "This is bound in message and draft buffers to their associated
816     headers buffer."
817     :value h-buf :buffer dbuffer)
818     (setf (draft-info-headers-mark dinfo) hmark)
819     (push dbuffer (headers-info-draft-bufs hinfo)))
820     (let ((msg-buf (maybe-make-mh-buffer (format nil "Message ~A ~A" folder msg)
821     :message)))
822     (defhvar "Message Information"
823     "This holds the information about the current headers buffer."
824     :value (make-message-info :folder folder :msgs msg
825     :headers-mark
826     (if h-buf (copy-mark hmark) hmark)
827     :draft-buf dbuffer)
828     :buffer msg-buf)
829     (when h-buf
830     (defhvar "Headers Buffer"
831     "This is bound in message and draft buffers to their associated
832     headers buffer."
833     :value h-buf :buffer msg-buf)
834     (push msg-buf (headers-info-other-msg-bufs hinfo)))
835     (read-mh-file (merge-pathnames
836     msg
837     (merge-relative-pathnames (strip-folder-name folder)
838     (mh-directory-pathname)))
839     msg-buf)
840     (setf (buffer-writable msg-buf) nil)
841     (defhvar "Message Buffer"
842     "This is bound in draft buffers to their associated message buffer."
843     :value msg-buf :buffer dbuffer))
844     (get-draft-buffer-window dbuffer)))
845    
846    
847     (defcommand "Forward Message" (p)
848     "Forward current message.
849     Prompts for a folder and message to forward. When in a headers buffer,
850     forwards the message on the current line. When in a message buffer,
851     forwards that message."
852     "Prompts for a folder and message to reply to. When in a headers buffer,
853     replies to the message on the current line. When in a message buffer,
854     replies to that message."
855     (declare (ignore p))
856     (let ((hinfo (value headers-information))
857     (minfo (value message-information)))
858     (cond (hinfo
859     (multiple-value-bind (cur-msg cur-mark)
860     (headers-current-message hinfo)
861     (unless cur-msg (editor-error "Not on a header line."))
862     (setup-forward-draft (headers-info-folder hinfo)
863     cur-msg hinfo cur-mark)))
864     (minfo
865     (setup-message-buffer-draft (current-buffer) minfo :forward))
866     (t
867     (let ((folder (prompt-for-folder)))
868     (setup-forward-draft folder
869     (car (prompt-for-message :folder folder))))))))
870    
871     ;;; SETUP-FORWARD-DRAFT sets up a draft forwarding folder's msg. When there
872     ;;; is a headers buffer involved (hinfo and hmark), the draft is associated
873     ;;; with it.
874     ;;;
875     ;;; This function is like SETUP-REPLY-DRAFT (in addition to "forw" and
876     ;;; :to-field), but it does not setup a message buffer. If this is added as
877     ;;; something forward drafts want, then SETUP-REPLY-DRAFT should be
878     ;;; parameterized and renamed.
879     ;;;
880     (defun setup-forward-draft (folder msg &optional hinfo hmark)
881     (let* ((dbuffer (sub-setup-message-draft "forw" :to-field
882     (list folder msg)))
883     (dinfo (variable-value 'draft-information :buffer dbuffer))
884     (h-buf (if hinfo (headers-info-buffer hinfo))))
885     (when h-buf
886     (defhvar "Headers Buffer"
887     "This is bound in message and draft buffers to their associated
888     headers buffer."
889     :value h-buf :buffer dbuffer)
890     (setf (draft-info-headers-mark dinfo) hmark)
891     (push dbuffer (headers-info-draft-bufs hinfo)))
892     (get-draft-buffer-window dbuffer)))
893    
894    
895     (defcommand "Send Message" (p)
896     "Setup a draft buffer.
897     Setup a draft buffer, reserving a draft folder message. When invoked in a
898     headers buffer, the current message is available in an associated message
899     buffer."
900     "Setup a draft buffer, reserving a draft folder message. When invoked in
901     a headers buffer, the current message is available in an associated
902     message buffer."
903     (declare (ignore p))
904     (let ((hinfo (value headers-information))
905     (minfo (value message-information)))
906     (cond (hinfo (setup-headers-message-draft hinfo))
907     (minfo (setup-message-buffer-draft (current-buffer) minfo :compose))
908     (t (setup-message-draft)))))
909    
910     (defun setup-message-draft ()
911     (get-draft-buffer-window (sub-setup-message-draft "comp" :to-field)))
912    
913     ;;; SETUP-HEADERS-MESSAGE-DRAFT sets up a draft buffer associated with a
914     ;;; headers buffer and a message buffer. The headers current message is
915     ;;; inserted in the message buffer which is also associated with the headers
916     ;;; buffer. The draft buffer is associated with the message buffer.
917     ;;;
918     (defun setup-headers-message-draft (hinfo)
919     (multiple-value-bind (cur-msg cur-mark)
920     (headers-current-message hinfo)
921     (unless cur-msg (message "Draft not associated with any message."))
922     (let* ((dbuffer (sub-setup-message-draft "comp" :to-field))
923     (dinfo (variable-value 'draft-information :buffer dbuffer))
924     (h-buf (headers-info-buffer hinfo)))
925     (when cur-msg
926     (defhvar "Headers Buffer"
927     "This is bound in message and draft buffers to their associated headers
928     buffer."
929     :value h-buf :buffer dbuffer)
930     (push dbuffer (headers-info-draft-bufs hinfo)))
931     (when cur-msg
932     (setf (draft-info-headers-mark dinfo) cur-mark)
933     (let* ((folder (headers-info-folder hinfo))
934     (msg-buf (maybe-make-mh-buffer
935     (format nil "Message ~A ~A" folder cur-msg)
936     :message)))
937     (defhvar "Message Information"
938     "This holds the information about the current headers buffer."
939     :value (make-message-info :folder folder :msgs cur-msg
940     :headers-mark (copy-mark cur-mark)
941     :draft-buf dbuffer)
942     :buffer msg-buf)
943     (defhvar "Headers Buffer"
944     "This is bound in message and draft buffers to their associated
945     headers buffer."
946     :value h-buf :buffer msg-buf)
947     (push msg-buf (headers-info-other-msg-bufs hinfo))
948     (read-mh-file (merge-pathnames
949     cur-msg
950     (merge-relative-pathnames (strip-folder-name folder)
951     (mh-directory-pathname)))
952     msg-buf)
953     (setf (buffer-writable msg-buf) nil)
954     (defhvar "Message Buffer"
955     "This is bound in draft buffers to their associated message buffer."
956     :value msg-buf :buffer dbuffer)))
957     (get-draft-buffer-window dbuffer))))
958    
959     ;;; SETUP-MESSAGE-BUFFER-DRAFT takes a message buffer and its message
960     ;;; information. A draft buffer is created according to type, and the two
961     ;;; buffers are associated. Any previous association of the message buffer and
962     ;;; a draft buffer is dropped. Any association between the message buffer and
963     ;;; a headers buffer is propagated to the draft buffer, and if the message
964     ;;; buffer is the headers buffer's main message buffer, it is moved to "other"
965     ;;; status. Argument may be used to pass in the argument from the command.
966     ;;;
967     (defun setup-message-buffer-draft (msg-buf minfo type &optional argument)
968     (let* ((msgs (message-info-msgs minfo))
969     (cur-msg (if (consp msgs) (car msgs) msgs))
970     (folder (message-info-folder minfo))
971     (dbuffer
972     (ecase type
973     (:reply
974     (sub-setup-message-draft
975     "repl" :end-of-buffer
976     `(,folder ,cur-msg
977     ,@(if argument
978     (case (value reply-to-message-prefix-action)
979     (:no-cc-all '("-nocc" "all"))
980     (:cc-all '("-cc" "all")))))))
981     (:compose
982     (sub-setup-message-draft "comp" :to-field))
983     (:forward
984     (sub-setup-message-draft "forw" :to-field
985     (list folder cur-msg)))))
986     (dinfo (variable-value 'draft-information :buffer dbuffer)))
987     (when (message-info-draft-buf minfo)
988     (delete-variable 'message-buffer :buffer (message-info-draft-buf minfo)))
989     (setf (message-info-draft-buf minfo) dbuffer)
990     (when (eq type :reply)
991     (setf (draft-info-replied-to-folder dinfo) folder)
992     (setf (draft-info-replied-to-msg dinfo) cur-msg))
993     (when (hemlock-bound-p 'headers-buffer :buffer msg-buf)
994     (let* ((hbuf (variable-value 'headers-buffer :buffer msg-buf))
995     (hinfo (variable-value 'headers-information :buffer hbuf)))
996     (defhvar "Headers Buffer"
997     "This is bound in message and draft buffers to their associated
998     headers buffer."
999     :value hbuf :buffer dbuffer)
1000     (setf (draft-info-headers-mark dinfo)
1001     (copy-mark (message-info-headers-mark minfo)))
1002     (push dbuffer (headers-info-draft-bufs hinfo))
1003     (when (eq (headers-info-msg-buffer hinfo) msg-buf)
1004     (setf (headers-info-msg-buffer hinfo) nil)
1005     (push msg-buf (headers-info-other-msg-bufs hinfo)))))
1006     (defhvar "Message Buffer"
1007     "This is bound in draft buffers to their associated message buffer."
1008     :value msg-buf :buffer dbuffer)
1009     (get-draft-buffer-window dbuffer)))
1010    
1011     (defvar *draft-to-pattern*
1012     (new-search-pattern :string-insensitive :forward "To:"))
1013    
1014     (defun sub-setup-message-draft (utility point-action &optional args)
1015     (mh utility `(,@args "-nowhatnowproc"))
1016     (let* ((folder (mh-draft-folder))
1017     (draft-msg (mh-current-message folder))
1018     (msg-pn (merge-pathnames draft-msg (mh-draft-folder-pathname)))
1019     (dbuffer (maybe-make-mh-buffer (format nil "Draft ~A" draft-msg)
1020     :draft)))
1021     (read-mh-file msg-pn dbuffer)
1022     (setf (buffer-pathname dbuffer) msg-pn)
1023     (defhvar "Draft Information"
1024     "This holds the information about the current draft buffer."
1025     :value (make-draft-info :folder (coerce-folder-name folder)
1026     :message draft-msg
1027     :pathname msg-pn)
1028     :buffer dbuffer)
1029     (let ((point (buffer-point dbuffer)))
1030     (ecase point-action
1031     (:to-field
1032     (when (find-pattern point *draft-to-pattern*)
1033     (line-end point)))
1034     (:end-of-buffer (buffer-end point))))
1035     dbuffer))
1036    
1037     (defun read-mh-file (pathname buffer)
1038     (unless (probe-file pathname)
1039     (editor-error "No such message -- ~A" (namestring pathname)))
1040     (read-file pathname (buffer-point buffer))
1041     (setf (buffer-write-date buffer) (file-write-date pathname))
1042     (buffer-start (buffer-point buffer))
1043     (setf (buffer-modified buffer) nil))
1044    
1045    
1046     (defvar *draft-buffer-window-fun* 'change-to-buffer
1047     "This is called by GET-DRAFT-BUFFER-WINDOW to display a new draft buffer.
1048     The default is CHANGE-TO-BUFFER which uses the current window.")
1049    
1050     ;;; GET-DRAFT-BUFFER-WINDOW is called to display a new draft buffer.
1051     ;;;
1052     (defun get-draft-buffer-window (dbuffer)
1053     (funcall *draft-buffer-window-fun* dbuffer))
1054    
1055    
1056     (defcommand "Reply to Message in Other Window" (p)
1057     "Reply to message, creating another window for draft buffer.
1058     Prompts for a folder and message to reply to. When in a headers buffer,
1059     replies to the message on the current line. When in a message buffer,
1060     replies to that message. The current window is split displaying the draft
1061     buffer in the new window and the message buffer in the current."
1062     "Prompts for a folder and message to reply to. When in a headers buffer,
1063     replies to the message on the current line. When in a message buffer,
1064     replies to that message."
1065     (let ((*draft-buffer-window-fun* #'draft-buffer-in-other-window))
1066     (reply-to-message-command p)))
1067    
1068     (defun draft-buffer-in-other-window (dbuffer)
1069     (when (hemlock-bound-p 'message-buffer :buffer dbuffer)
1070     (let ((mbuf (variable-value 'message-buffer :buffer dbuffer)))
1071     (when (not (eq (current-buffer) mbuf))
1072     (change-to-buffer mbuf))))
1073     (setf (current-buffer) dbuffer)
1074     (setf (current-window) (make-window (buffer-start-mark dbuffer)))
1075     (defhvar "Split Window Draft"
1076     "Indicates window needs to be cleaned up for draft."
1077     :value t :buffer dbuffer))
1078    
1079     (defhvar "Deliver Message Confirm"
1080     "When set, \"Deliver Message\" will ask for confirmation before sending the
1081     draft. This is off by default since \"Deliver Message\" is not bound to
1082     any key by default."
1083     :value t)
1084    
1085     (defcommand "Deliver Message" (p)
1086     "Save and deliver the current draft buffer.
1087     When in a draft buffer, this saves the file and uses SEND to deliver the
1088     draft. Otherwise, this prompts for a draft message id, invoking SEND."
1089     "When in a draft buffer, this saves the file and uses SEND to deliver the
1090     draft. Otherwise, this prompts for a draft message id, invoking SEND."
1091     (declare (ignore p))
1092     (let ((dinfo (value draft-information)))
1093     (cond (dinfo
1094     (deliver-draft-buffer-message dinfo))
1095     (t
1096     (let* ((folder (coerce-folder-name (mh-draft-folder)))
1097     (msg (prompt-for-message :folder folder)))
1098     (mh "send" `("-draftfolder" ,folder "-draftmessage" ,@msg)))))))
1099    
1100     (defun deliver-draft-buffer-message (dinfo)
1101     (when (draft-info-delivered dinfo)
1102     (editor-error "This draft has already been delivered."))
1103     (when (or (not (value deliver-message-confirm))
1104     (prompt-for-y-or-n :prompt "Deliver message? " :default t))
1105     (let ((dbuffer (current-buffer)))
1106     (when (buffer-modified dbuffer)
1107     (write-buffer-file dbuffer (buffer-pathname dbuffer)))
1108     (message "Delivering draft ...")
1109     (mh "send" `("-draftfolder" ,(draft-info-folder dinfo)
1110     "-draftmessage" ,(draft-info-message dinfo)))
1111     (setf (draft-info-delivered dinfo) t)
1112     (let ((replied-folder (draft-info-replied-to-folder dinfo))
1113     (replied-msg (draft-info-replied-to-msg dinfo)))
1114     (when replied-folder
1115     (message "Annotating message being replied to ...")
1116     (mh "anno" `(,replied-folder ,replied-msg "-component" "replied"))
1117     (do-headers-buffers (hbuf replied-folder)
1118     (with-headers-mark (hmark hbuf replied-msg)
1119     (mark-to-note-replied-msg hmark)
1120     (with-writable-buffer (hbuf)
1121     (setf (next-character hmark) #\A))))
1122     (dolist (b *buffer-list*)
1123     (when (and (hemlock-bound-p 'message-information :buffer b)
1124     (buffer-modeline-field-p b :replied-to-message))
1125     (dolist (w (buffer-windows b))
1126     (update-modeline-field b w :replied-to-message))))))
1127     (maybe-delete-extra-draft-window dbuffer (current-window))
1128 ram 1.4 (let ((mbuf (value message-buffer)))
1129     (when (and mbuf
1130     (not (hemlock-bound-p 'netnews-message-info :buffer mbuf)))
1131     (let ((minfo (variable-value 'message-information :buffer mbuf)))
1132     (when (and minfo (not (message-info-keep minfo)))
1133     (delete-buffer-if-possible mbuf)))))
1134 ram 1.1 (delete-buffer-if-possible dbuffer))))
1135    
1136     (defcommand "Delete Draft and Buffer" (p)
1137 ram 1.4 "Delete the current draft and associated message and buffer."
1138     "Delete the current draft and associated message and buffer."
1139 ram 1.1 (declare (ignore p))
1140     (let ((dinfo (value draft-information))
1141     (dbuffer (current-buffer)))
1142     (unless dinfo (editor-error "No draft associated with buffer."))
1143     (maybe-delete-extra-draft-window dbuffer (current-window))
1144     (delete-file (draft-info-pathname dinfo))
1145 ram 1.4 (let ((mbuf (value message-buffer)))
1146     (when (and mbuf
1147     (not (hemlock-bound-p 'netnews-message-info :buffer mbuf)))
1148     (let ((minfo (variable-value 'message-information :buffer mbuf)))
1149     (when (and minfo (not (message-info-keep minfo)))
1150     (delete-buffer-if-possible mbuf)))))
1151     (delete-buffer-if-possible dbuffer)))
1152 ram 1.1
1153 ram 1.4 ;;; MAYBE-DELETE-EXTRA-DRAFT-WINDOW -- Internal.
1154 ram 1.1 ;;;
1155 ram 1.4 ;;; This takes a draft buffer and a window into it that should not be deleted.
1156     ;;; If "Split Window Draft" is bound in the buffer, and there are at least two
1157     ;;; windows in dbuffer-window's group, then we delete some window. Blow away
1158     ;;; the variable, so we don't think this is still a split window draft buffer.
1159     ;;;
1160 ram 1.1 (defun maybe-delete-extra-draft-window (dbuffer dbuffer-window)
1161     (when (and (hemlock-bound-p 'split-window-draft :buffer dbuffer)
1162 ram 1.4 ;; Since we know bitmap devices have window groups, this loop is
1163 ram 1.3 ;; more correct than testing the length of *window-list* and
1164     ;; accounting for *echo-area-window* being in there.
1165     (do ((start dbuffer-window)
1166     (count 1 (1+ count))
1167     (w (next-window dbuffer-window) (next-window w)))
1168     ((eq start w) (> count 1))))
1169     (delete-window (next-window dbuffer-window))
1170 ram 1.1 (delete-variable 'split-window-draft :buffer dbuffer)))
1171    
1172     (defcommand "Remail Message" (p)
1173     "Prompts for a folder and message to remail. Prompts for a resend-to
1174     address string and resend-cc address string. When in a headers buffer,
1175     remails the message on the current line. When in a message buffer,
1176     remails that message."
1177     "Prompts for a folder and message to remail. Prompts for a resend-to
1178     address string and resend-cc address string. When in a headers buffer,
1179     remails the message on the current line. When in a message buffer,
1180     remails that message."
1181     (declare (ignore p))
1182     (let ((hinfo (value headers-information))
1183     (minfo (value message-information)))
1184     (cond (hinfo
1185     (multiple-value-bind (cur-msg cur-mark)
1186     (headers-current-message hinfo)
1187     (unless cur-msg (editor-error "Not on a header line."))
1188     (delete-mark cur-mark)
1189     (remail-message (headers-info-folder hinfo) cur-msg
1190     (prompt-for-string :prompt "Resend To: ")
1191     (prompt-for-string :prompt "Resend Cc: "))))
1192     (minfo
1193     (remail-message (message-info-folder minfo)
1194     (message-info-msgs minfo)
1195     (prompt-for-string :prompt "Resend To: ")
1196     (prompt-for-string :prompt "Resend Cc: ")))
1197     (t
1198     (let ((folder (prompt-for-folder)))
1199     (remail-message folder
1200     (car (prompt-for-message :folder folder))
1201     (prompt-for-string :prompt "Resend To: ")
1202     (prompt-for-string :prompt "Resend Cc: "))))))
1203     (message "Message remailed."))
1204    
1205    
1206     ;;; REMAIL-MESSAGE claims a draft folder message with "dist". This is then
1207     ;;; sucked into a buffer and modified by inserting the supplied addresses.
1208     ;;; "send" is used to deliver the draft, but it requires certain evironment
1209     ;;; variables to make it do the right thing. "mhdist" says the draft is only
1210     ;;; remailing information, and "mhaltmsg" is the message to send. "mhannotate"
1211     ;;; must be set due to a bug in MH's "send"; it will not notice the "mhdist"
1212     ;;; flag unless there is some message to be annotated. This command does not
1213     ;;; provide for annotation of the remailed message.
1214     ;;;
1215     (defun remail-message (folder msg resend-to resend-cc)
1216     (mh "dist" `(,folder ,msg "-nowhatnowproc"))
1217     (let* ((draft-folder (mh-draft-folder))
1218     (draft-msg (mh-current-message draft-folder)))
1219     (setup-remail-draft-message draft-msg resend-to resend-cc)
1220     (mh "send" `("-draftfolder" ,draft-folder "-draftmessage" ,draft-msg)
1221     :environment
1222     `((:|mhdist| . "1")
1223     (:|mhannotate| . "1")
1224     (:|mhaltmsg| . ,(namestring
1225     (merge-pathnames msg (merge-relative-pathnames
1226     (strip-folder-name folder)
1227     (mh-directory-pathname)))))))))
1228    
1229     ;;; SETUP-REMAIL-DRAFT-MESSAGE takes a draft folder and message that have been
1230     ;;; created with the MH "dist" utility. A buffer is created with this
1231     ;;; message's pathname, searching for "resent-to:" and "resent-cc:", filling in
1232     ;;; the supplied argument values. After writing out the results, the buffer
1233     ;;; is deleted.
1234     ;;;
1235     (defvar *draft-resent-to-pattern*
1236     (new-search-pattern :string-insensitive :forward "resent-to:"))
1237     (defvar *draft-resent-cc-pattern*
1238     (new-search-pattern :string-insensitive :forward "resent-cc:"))
1239    
1240     (defun setup-remail-draft-message (msg resend-to resend-cc)
1241     (let* ((msg-pn (merge-pathnames msg (mh-draft-folder-pathname)))
1242     (dbuffer (maybe-make-mh-buffer (format nil "Draft ~A" msg)
1243     :draft))
1244     (point (buffer-point dbuffer)))
1245     (read-mh-file msg-pn dbuffer)
1246     (when (find-pattern point *draft-resent-to-pattern*)
1247     (line-end point)
1248     (insert-string point resend-to))
1249     (buffer-start point)
1250     (when (find-pattern point *draft-resent-cc-pattern*)
1251     (line-end point)
1252     (insert-string point resend-cc))
1253     (write-file (buffer-region dbuffer) msg-pn :keep-backup nil)
1254     ;; The draft buffer delete hook expects this to be bound.
1255     (defhvar "Draft Information"
1256     "This holds the information about the current draft buffer."
1257     :value :ignore
1258     :buffer dbuffer)
1259     (delete-buffer dbuffer)))
1260    
1261    
1262    
1263     ;;;; Message and Draft Stuff.
1264    
1265     (defhvar "Headers Buffer"
1266     "This is bound in message and draft buffers to their associated headers
1267     buffer."
1268     :value nil)
1269    
1270     (defcommand "Goto Headers Buffer" (p)
1271     "Selects associated headers buffer if it exists.
1272     The headers buffer's point is moved to the appropriate line, pushing a
1273     buffer mark where point was."
1274     "Selects associated headers buffer if it exists."
1275     (declare (ignore p))
1276     (let ((h-buf (value headers-buffer)))
1277     (unless h-buf (editor-error "No associated headers buffer."))
1278     (let ((info (or (value message-information) (value draft-information))))
1279     (change-to-buffer h-buf)
1280     (push-buffer-mark (copy-mark (current-point)))
1281     (move-mark (current-point) (message/draft-info-headers-mark info)))))
1282    
1283     (defhvar "Message Buffer"
1284     "This is bound in draft buffers to their associated message buffer."
1285     :value nil)
1286    
1287     (defcommand "Goto Message Buffer" (p)
1288     "Selects associated message buffer if it exists."
1289     "Selects associated message buffer if it exists."
1290     (declare (ignore p))
1291     (let ((msg-buf (value message-buffer)))
1292     (unless msg-buf (editor-error "No associated message buffer."))
1293     (change-to-buffer msg-buf)))
1294    
1295    
1296     (defhvar "Message Insertion Prefix"
1297     "This is a fill prefix that is used when inserting text from a message buffer
1298     into a draft buffer by \"Insert Message Region\". It defaults to three
1299     spaces."
1300     :value " ")
1301    
1302     (defhvar "Message Insertion Column"
1303     "This is a fill column that is used when inserting text from a message buffer
1304     into a draft buffer by \"Insert Message Region\"."
1305     :value 75)
1306    
1307     (defcommand "Insert Message Region" (p)
1308 ram 1.4 "Copy the current region into the associated draft or post buffer. When
1309     in a message buffer that has an associated draft or post buffer, the
1310     current active region is copied into the draft or post buffer. It is
1311     filled using \"Message Insertion Prefix\" and \"Message Insertion
1312     Column\". If an argument is supplied, the filling is inhibited.
1313     If both a draft buffer and post buffer are associated with this, then it
1314     is inserted into the draft buffer."
1315     "When in a message buffer that has an associated draft or post buffer,
1316     the current active region is copied into the post or draft buffer. It is
1317     filled using \"Message Insertion Prefix\" and \"Message Insertion
1318     Column\". If an argument is supplied, the filling is inhibited."
1319     (let* ((minfo (value message-information))
1320     (nm-info (if (hemlock-bound-p 'netnews-message-info)
1321     (value netnews-message-info)))
1322     (post-buffer (and nm-info (nm-info-post-buffer nm-info)))
1323     (post-info (and post-buffer
1324     (variable-value 'post-info :buffer post-buffer)))
1325     dbuf kind)
1326     (cond (minfo
1327     (setf kind :mail)
1328     (setf dbuf (message-info-draft-buf minfo)))
1329     (nm-info
1330     (setf kind :netnews)
1331     (setf dbuf (or (nm-info-draft-buffer nm-info)
1332     (nm-info-post-buffer nm-info))))
1333     (t (editor-error "Not in a netnews message or message buffer.")))
1334     (unless dbuf
1335     (editor-error "Message buffer not associated with any draft or post ~
1336     buffer."))
1337     (let* ((region (copy-region (current-region)))
1338     (dbuf-point (buffer-point dbuf))
1339     (dbuf-mark (copy-mark dbuf-point)))
1340     (cond ((and (eq kind :mail)
1341     (hemlock-bound-p 'split-window-draft :buffer dbuf)
1342     (> (length (the list *window-list*)) 2)
1343     (buffer-windows dbuf))
1344     (setf (current-buffer) dbuf
1345     (current-window) (car (buffer-windows dbuf))))
1346     ((and (eq kind :netnews)
1347     (and (member (post-info-message-window post-info)
1348     *window-list*)
1349     (member (post-info-reply-window post-info)
1350     *window-list*)))
1351     (setf (current-buffer) dbuf
1352     (current-window) (post-info-reply-window post-info)))
1353     (t (change-to-buffer dbuf)))
1354     (push-buffer-mark dbuf-mark)
1355     (ninsert-region dbuf-point region)
1356     (unless p
1357     (fill-region-by-paragraphs (region dbuf-mark dbuf-point)
1358     (value message-insertion-prefix)
1359     (value message-insertion-column)))))
1360 ram 1.1 (setf (last-command-type) :ephemerally-active))
1361    
1362    
1363     (defhvar "Message Buffer Insertion Prefix"
1364     "This is a line prefix that is inserted at the beginning of every line in
1365     a message buffer when inserting those lines into a draft buffer with
1366     \"Insert Message Buffer\". It defaults to four spaces."
1367     :value " ")
1368    
1369     (defcommand "Insert Message Buffer" (p)
1370 ram 1.4 "Insert entire (associated) message buffer into (associated) draft or
1371     post buffer. When in a draft or post buffer with an associated message
1372     buffer, or when in a message buffer that has an associated draft or post
1373     buffer, the message buffer is inserted into the draft buffer. When
1374     there are both an associated draft and post buffer, the text is inserted
1375     into the draft buffer. Each inserted line is modified by prefixing it
1376     with \"Message Buffer Insertion Prefix\". If an argument is supplied
1377     the prefixing is inhibited."
1378     "When in a draft or post buffer with an associated message buffer, or
1379     when in a message buffer that has an associated draft or post buffer, the
1380     message buffer is inserted into the draft buffer. Each inserted line is
1381     modified by prefixing it with \"Message Buffer Insertion Prefix\". If an
1382     argument is supplied the prefixing is inhibited."
1383 ram 1.1 (let ((minfo (value message-information))
1384     (dinfo (value draft-information))
1385 ram 1.4 mbuf dbuf message-kind)
1386 ram 1.1 (cond (minfo
1387 ram 1.4 (setf message-kind :mail)
1388 ram 1.1 (setf dbuf (message-info-draft-buf minfo))
1389     (unless dbuf
1390     (editor-error
1391     "Message buffer not associated with any draft buffer."))
1392     (setf mbuf (current-buffer))
1393     (change-to-buffer dbuf))
1394     (dinfo
1395 ram 1.4 (setf message-kind :mail)
1396 ram 1.1 (setf mbuf (value message-buffer))
1397     (unless mbuf
1398     (editor-error
1399     "Draft buffer not associated with any message buffer."))
1400     (setf dbuf (current-buffer)))
1401 ram 1.4 ((hemlock-bound-p 'netnews-message-info)
1402     (setf message-kind :netnews)
1403     (setf mbuf (current-buffer))
1404     (let ((nm-info (value netnews-message-info)))
1405     (setf dbuf (or (nm-info-draft-buffer nm-info)
1406     (nm-info-post-buffer nm-info)))
1407     (unless dbuf
1408     (editor-error "Message buffer not associated with any draft ~
1409     or post buffer.")))
1410     (change-to-buffer dbuf))
1411     ((hemlock-bound-p 'post-info)
1412     (setf message-kind :netnews)
1413     (let ((post-info (value post-info)))
1414     (setf mbuf (post-info-message-buffer post-info))
1415     (unless mbuf
1416     (editor-error "Post buffer not associated with any message ~
1417     buffer.")))
1418     (setf dbuf (current-buffer)))
1419     (t (editor-error "Not in a draft, message, news-message, or post ~
1420     buffer.")))
1421 ram 1.1 (let* ((dbuf-point (buffer-point dbuf))
1422     (dbuf-mark (copy-mark dbuf-point)))
1423     (push-buffer-mark dbuf-mark)
1424     (insert-region dbuf-point (buffer-region mbuf))
1425     (unless p
1426     (let ((prefix (value message-buffer-insertion-prefix)))
1427     (with-mark ((temp dbuf-mark :left-inserting))
1428     (loop
1429     (when (mark>= temp dbuf-point) (return))
1430     (insert-string temp prefix)
1431     (unless (line-offset temp 1 0) (return)))))))
1432 ram 1.4 (ecase message-kind
1433     (:mail
1434     (insert-message-buffer-cleanup-split-draft dbuf mbuf))
1435     (:netnews
1436     (nn-reply-cleanup-split-windows dbuf))))
1437 ram 1.1 (setf (last-command-type) :ephemerally-active))
1438    
1439     ;;; INSERT-MESSAGE-BUFFER-CLEANUP-SPLIT-DRAFT tries to delete an extra window
1440     ;;; due to "Reply to Message in Other Window". Since we just inserted the
1441     ;;; message buffer in the draft buffer, we don't need the other window into
1442     ;;; the message buffer.
1443     ;;;
1444     (defun insert-message-buffer-cleanup-split-draft (dbuf mbuf)
1445     (when (and (hemlock-bound-p 'split-window-draft :buffer dbuf)
1446     (> (length (the list *window-list*)) 2))
1447     (let ((win (car (buffer-windows mbuf))))
1448     (cond
1449     (win
1450     (when (eq win (current-window))
1451     (let ((dwin (car (buffer-windows dbuf))))
1452     (unless dwin
1453     (editor-error "Couldn't fix windows for split window draft."))
1454     (setf (current-buffer) dbuf)
1455     (setf (current-window) dwin)))
1456     (delete-window win))
1457     (t ;; This happens when invoked with the message buffer current.
1458     (let ((dwins (buffer-windows dbuf)))
1459     (when (> (length (the list dwins)) 1)
1460     (delete-window (find-if #'(lambda (w)
1461     (not (eq w (current-window))))
1462     dwins)))))))
1463     (delete-variable 'split-window-draft :buffer dbuf)))
1464    
1465    
1466     ;;; CLEANUP-MESSAGE-BUFFER is called when a buffer gets deleted. It cleans
1467     ;;; up references to a message buffer.
1468     ;;;
1469     (defun cleanup-message-buffer (buffer)
1470     (let ((minfo (variable-value 'message-information :buffer buffer)))
1471     (when (hemlock-bound-p 'headers-buffer :buffer buffer)
1472     (let* ((hinfo (variable-value 'headers-information
1473     :buffer (variable-value 'headers-buffer
1474     :buffer buffer)))
1475     (msg-buf (headers-info-msg-buffer hinfo)))
1476     (if (eq msg-buf buffer)
1477     (setf (headers-info-msg-buffer hinfo) nil)
1478     (setf (headers-info-other-msg-bufs hinfo)
1479     (delete buffer (headers-info-other-msg-bufs hinfo)
1480     :test #'eq))))
1481     (delete-mark (message-info-headers-mark minfo))
1482     ;;
1483     ;; Do this for MAYBE-MAKE-MH-BUFFER since it isn't necessary for GC.
1484     (delete-variable 'headers-buffer :buffer buffer))
1485     (when (message-info-draft-buf minfo)
1486     (delete-variable 'message-buffer
1487     :buffer (message-info-draft-buf minfo)))))
1488    
1489     ;;; CLEANUP-DRAFT-BUFFER is called when a buffer gets deleted. It cleans
1490     ;;; up references to a draft buffer.
1491     ;;;
1492     (defun cleanup-draft-buffer (buffer)
1493     (let ((dinfo (variable-value 'draft-information :buffer buffer)))
1494     (when (hemlock-bound-p 'headers-buffer :buffer buffer)
1495     (let* ((hinfo (variable-value 'headers-information
1496     :buffer (variable-value 'headers-buffer
1497     :buffer buffer))))
1498     (setf (headers-info-draft-bufs hinfo)
1499     (delete buffer (headers-info-draft-bufs hinfo) :test #'eq))
1500     (delete-mark (draft-info-headers-mark dinfo))))
1501     (when (hemlock-bound-p 'message-buffer :buffer buffer)
1502     (setf (message-info-draft-buf
1503     (variable-value 'message-information
1504     :buffer (variable-value 'message-buffer
1505     :buffer buffer)))
1506     nil))))
1507    
1508     ;;; CLEANUP-HEADERS-BUFFER is called when a buffer gets deleted. It cleans
1509     ;;; up references to a headers buffer.
1510     ;;;
1511     (defun cleanup-headers-buffer (buffer)
1512     (let* ((hinfo (variable-value 'headers-information :buffer buffer))
1513     (msg-buf (headers-info-msg-buffer hinfo)))
1514     (when msg-buf
1515     (cleanup-headers-reference
1516     msg-buf (variable-value 'message-information :buffer msg-buf)))
1517     (dolist (b (headers-info-other-msg-bufs hinfo))
1518     (cleanup-headers-reference
1519     b (variable-value 'message-information :buffer b)))
1520     (dolist (b (headers-info-draft-bufs hinfo))
1521     (cleanup-headers-reference
1522     b (variable-value 'draft-information :buffer b)))))
1523    
1524     (defun cleanup-headers-reference (buffer info)
1525     (delete-mark (message/draft-info-headers-mark info))
1526     (setf (message/draft-info-headers-mark info) nil)
1527     (delete-variable 'headers-buffer :buffer buffer)
1528     (when (typep info 'draft-info)
1529     (setf (draft-info-replied-to-folder info) nil)
1530     (setf (draft-info-replied-to-msg info) nil)))
1531    
1532     ;;; REVAMP-HEADERS-BUFFER cleans up a headers buffer for immediate re-use.
1533     ;;; After deleting the buffer's region, there will be one line in the buffer
1534     ;;; because of how Hemlock regions work, so we have to delete that line's
1535     ;;; plist. Then we clean up any references to the buffer and delete the
1536     ;;; main message buffer. The other message buffers are left alone assuming
1537     ;;; they are on the "others" list because they are being used in some
1538     ;;; particular way (for example, a draft buffer refers to one or the user has
1539     ;;; kept it). Then some slots of the info structure are set to nil.
1540     ;;;
1541     (defun revamp-headers-buffer (hbuffer hinfo)
1542     (delete-region (buffer-region hbuffer))
1543     (setf (line-plist (mark-line (buffer-point hbuffer))) nil)
1544     (let ((msg-buf (headers-info-msg-buffer hinfo)))
1545     ;; Deleting the buffer sets the slot to nil.
1546     (when msg-buf (delete-buffer-if-possible msg-buf))
1547     (cleanup-headers-buffer hbuffer))
1548     (setf (headers-info-other-msg-bufs hinfo) nil)
1549     (setf (headers-info-draft-bufs hinfo) nil)
1550     (setf (headers-info-msg-seq hinfo) nil)
1551     (setf (headers-info-msg-strings hinfo) nil))
1552    
1553    
1554    
1555     ;;;; Incorporating new mail.
1556    
1557     (defhvar "New Mail Folder"
1558     "This is the folder new mail is incorporated into."
1559     :value "+inbox")
1560    
1561     (defcommand "Incorporate New Mail" (p)
1562     "Incorporates new mail into \"New Mail Folder\", displaying INC output in
1563     a pop-up window."
1564     "Incorporates new mail into \"New Mail Folder\", displaying INC output in
1565     a pop-up window."
1566     (declare (ignore p))
1567     (with-pop-up-display (s)
1568     (incorporate-new-mail s)))
1569    
1570     (defhvar "Unseen Headers Message Spec"
1571     "This is an MH message spec suitable any message prompt. It is used to
1572     supply headers for the unseen headers buffer, in addition to the
1573     unseen-sequence name that is taken from the user's MH profile, when
1574     incorporating new mail and after expunging. This value is a string."
1575     :value nil)
1576    
1577     (defcommand "Incorporate and Read New Mail" (p)
1578     "Incorporates new mail and generates a headers buffer.
1579     Incorporates new mail into \"New Mail Folder\", and creates a headers buffer
1580     with the new messages. To use this, you must define an unseen- sequence in
1581     your profile. Each time this is invoked the unseen-sequence is SCAN'ed, and
1582     the headers buffer's contents are replaced."
1583     "Incorporates new mail into \"New Mail Folder\", and creates a headers
1584     buffer with the new messages. This buffer will be appended to with
1585     successive uses of this command."
1586     (declare (ignore p))
1587     (let ((unseen-seq (mh-profile-component "unseen-sequence")))
1588     (unless unseen-seq
1589     (editor-error "No unseen-sequence defined in MH profile."))
1590     (incorporate-new-mail)
1591     (let* ((folder (value new-mail-folder))
1592     ;; Stash current message before fetching unseen headers.
1593     (cur-msg (mh-current-message folder))
1594     (region (get-new-mail-msg-hdrs folder unseen-seq)))
1595     ;; Fetch message headers before possibly making buffer in case we error.
1596     (when (not (and *new-mail-buffer*
1597     (member *new-mail-buffer* *buffer-list* :test #'eq)))
1598     (let ((name (format nil "Unseen Headers ~A" folder)))
1599     (when (getstring name *buffer-names*)
1600     (editor-error "There already is a buffer named ~S!" name))
1601     (setf *new-mail-buffer*
1602     (make-buffer name :modes (list "Headers")
1603     :delete-hook '(new-mail-buf-delete-hook)))
1604     (setf (buffer-writable *new-mail-buffer*) nil)))
1605     (cond ((hemlock-bound-p 'headers-information
1606     :buffer *new-mail-buffer*)
1607     (let ((hinfo (variable-value 'headers-information
1608     :buffer *new-mail-buffer*)))
1609     (unless (string= (headers-info-folder hinfo) folder)
1610     (editor-error
1611     "An unseen headers buffer already exists but into another ~
1612     folder. Your mail has already been incorporated into the ~
1613     specified folder."))
1614     (with-writable-buffer (*new-mail-buffer*)
1615     (revamp-headers-buffer *new-mail-buffer* hinfo))
1616     ;; Restore the name in case someone used "Pick Headers".
1617     (setf (buffer-name *new-mail-buffer*)
1618     (format nil "Unseen Headers ~A" folder))
1619     (insert-new-mail-message-headers hinfo region cur-msg)))
1620     (t
1621     (let ((hinfo (make-headers-info :buffer *new-mail-buffer*
1622     :folder folder)))
1623     (defhvar "Headers Information"
1624     "This holds the information about the current headers buffer."
1625     :value hinfo :buffer *new-mail-buffer*)
1626     (insert-new-mail-message-headers hinfo region cur-msg)))))))
1627    
1628     ;;; NEW-MAIL-BUF-DELETE-HOOK is invoked whenever the new mail buffer is
1629     ;;; deleted.
1630     ;;;
1631     (defun new-mail-buf-delete-hook (buffer)
1632     (declare (ignore buffer))
1633     (setf *new-mail-buffer* nil))
1634    
1635     ;;; GET-NEW-MAIL-MSG-HDRS takes a folder and the unseen-sequence name. It
1636     ;;; returns a region with the unseen message headers and any headers due to
1637     ;;; the "Unseen Headers Message Spec" variable.
1638     ;;;
1639     (defun get-new-mail-msg-hdrs (folder unseen-seq)
1640     (let* ((unseen-headers-message-spec (value unseen-headers-message-spec))
1641     (other-msgs (if unseen-headers-message-spec
1642     (breakup-message-spec
1643     (string-trim '(#\space #\tab)
1644     unseen-headers-message-spec))))
1645     (msg-spec (cond ((null other-msgs)
1646     (list unseen-seq))
1647     ((member unseen-seq other-msgs :test #'string=)
1648     other-msgs)
1649     (t (cons unseen-seq other-msgs)))))
1650     (message-headers-to-region folder msg-spec)))
1651    
1652     ;;; INSERT-NEW-MAIL-MESSAGE-HEADERS inserts region in the new mail buffer.
1653     ;;; Then we look for the header line with cur-msg id, moving point there.
1654     ;;; There may have been unseen messages before incorporating new mail, and
1655     ;;; cur-msg should be the first new message. Then we either switch to the
1656     ;;; new mail headers, or show the current message.
1657     ;;;
1658     (defun insert-new-mail-message-headers (hinfo region cur-msg)
1659     (declare (simple-string cur-msg))
1660     (with-writable-buffer (*new-mail-buffer*)
1661     (insert-message-headers *new-mail-buffer* hinfo region))
1662     (let ((point (buffer-point *new-mail-buffer*)))
1663     (buffer-start point)
1664     (with-headers-mark (cur-mark *new-mail-buffer* cur-msg)
1665     (move-mark point cur-mark)))
1666     (change-to-buffer *new-mail-buffer*))
1667    
1668    
1669     (defhvar "Incorporate New Mail Hook"
1670     "Functions on this hook are invoked immediately after new mail is
1671     incorporated."
1672     :value nil)
1673    
1674     (defun incorporate-new-mail (&optional stream)
1675     "Incorporates new mail, passing INC's output to stream. When stream is
1676     nil, output is flushed."
1677     (unless (new-mail-p) (editor-error "No new mail."))
1678     (let ((args `(,(coerce-folder-name (value new-mail-folder))
1679     ,@(if stream nil '("-silent"))
1680 ram 1.4 "-form" ,(namestring (truename (value mh-scan-line-form)))
1681 ram 1.1 "-width" ,(number-string (value fill-column)))))
1682 ram 1.4 (message "Incorporating new mail ...")
1683     (mh "inc" args))
1684 ram 1.1 (when (value incorporate-new-mail-hook)
1685     (message "Invoking new mail hooks ..."))
1686     (invoke-hook incorporate-new-mail-hook))
1687    
1688    
1689    
1690     ;;;; Deletion.
1691    
1692     (defhvar "Virtual Message Deletion"
1693     "When set, \"Delete Message\" merely MARK's a message into the
1694     \"hemlockdeleted\" sequence; otherwise, RMM is invoked."
1695     :value t)
1696    
1697     (defcommand "Delete Message and Show Next" (p)
1698     "Delete message and show next undeleted message.
1699     This command is only valid in a headers buffer or a message buffer
1700     associated with some headers buffer. The current message is deleted, and
1701     the next undeleted one is shown."
1702     "Delete the current message and show the next undeleted one."
1703     (declare (ignore p))
1704     (let ((hinfo (value headers-information))
1705     (minfo (value message-information)))
1706     (cond (hinfo
1707     (multiple-value-bind (cur-msg cur-mark)
1708     (headers-current-message hinfo)
1709     (unless cur-msg (editor-error "Not on a header line."))
1710     (delete-mark cur-mark)
1711     (delete-message (headers-info-folder hinfo) cur-msg)))
1712     (minfo
1713     (delete-message (message-info-folder minfo)
1714     (message-info-msgs minfo)))
1715     (t
1716     (editor-error "Not in a headers or message buffer."))))
1717     (show-message-offset 1 :undeleted))
1718    
1719     (defcommand "Delete Message and Down Line" (p)
1720     "Deletes the current message, moving point to the next line.
1721     When in a headers buffer, deletes the message on the current line. Then it
1722     moves point to the next non-blank line."
1723     "Deletes current message and moves point down a line."
1724     (declare (ignore p))
1725     (let ((hinfo (value headers-information)))
1726     (unless hinfo (editor-error "Not in a headers buffer."))
1727     (multiple-value-bind (cur-msg cur-mark)
1728     (headers-current-message hinfo)
1729     (unless cur-msg (editor-error "Not on a header line."))
1730     (delete-message (headers-info-folder hinfo) cur-msg)
1731     (when (line-offset cur-mark 1)
1732     (unless (blank-line-p (mark-line cur-mark))
1733     (move-mark (current-point) cur-mark)))
1734     (delete-mark cur-mark))))
1735    
1736     ;;; "Delete Message" unlike "Headers Delete Message" cannot know for sure
1737     ;;; which message id's have been deleted, so when virtual message deletion
1738     ;;; is not used, we cannot use DELETE-HEADERS-BUFFER-LINE to keep headers
1739     ;;; buffers consistent. However, the message id's in the buffer (if deleted)
1740     ;;; will generate MH errors if operations are attempted with them, and
1741     ;;; if the user ever packs the folder with "Expunge Messages", the headers
1742     ;;; buffer will be updated.
1743     ;;;
1744     (defcommand "Delete Message" (p)
1745     "Prompts for a folder, messages to delete, and pick expression. When in
1746     a headers buffer into the same folder specified, the messages prompt
1747     defaults to those messages in the buffer; \"all\" may be entered if this is
1748     not what is desired. When \"Virtual Message Deletion\" is set, messages are
1749     only MARK'ed for deletion. See \"Expunge Messages\". When this feature is
1750     not used, headers and message buffers message id's my not be consistent
1751     with MH."
1752     "Prompts for a folder and message to delete. When \"Virtual Message
1753     Deletion\" is set, messages are only MARK'ed for deletion. See \"Expunge
1754     Messages\"."
1755     (declare (ignore p))
1756     (let* ((folder (prompt-for-folder))
1757     (hinfo (value headers-information))
1758     (temp-msgs (prompt-for-message
1759     :folder folder
1760     :messages
1761     (if (and hinfo
1762     (string= folder
1763     (the simple-string
1764     (headers-info-folder hinfo))))
1765     (headers-info-msg-strings hinfo))
1766     :prompt "MH messages to pick from: "))
1767     (pick-exp (prompt-for-pick-expression))
1768     (msgs (pick-messages folder temp-msgs pick-exp))
1769     (virtually (value virtual-message-deletion)))
1770     (declare (simple-string folder))
1771     (if virtually
1772     (mh "mark" `(,folder ,@msgs "-sequence" "hemlockdeleted" "-add"))
1773     (mh "rmm" `(,folder ,@msgs)))
1774     (if virtually
1775     (let ((deleted-seq (mh-sequence-list folder "hemlockdeleted")))
1776     (when deleted-seq
1777     (do-headers-buffers (hbuf folder)
1778     (with-writable-buffer (hbuf)
1779     (note-deleted-headers hbuf deleted-seq)))))
1780     (do-headers-buffers (hbuf folder hinfo)
1781     (do-headers-lines (hbuf :line-var line :mark-var hmark)
1782     (when (member (line-message-id line) msgs :test #'string=)
1783     (delete-headers-buffer-line hinfo hmark)))))))
1784    
1785     (defcommand "Headers Delete Message" (p)
1786     "Delete current message.
1787     When in a headers buffer, deletes the message on the current line. When
1788     in a message buffer, deletes that message. When \"Virtual Message
1789     Deletion\" is set, messages are only MARK'ed for deletion. See \"Expunge
1790     Messages\"."
1791     "When in a headers buffer, deletes the message on the current line. When
1792     in a message buffer, deletes that message. When \"Virtual Message
1793     Deletion\" is set, messages are only MARK'ed for deletion. See \"Expunge
1794     Messages\"."
1795     (declare (ignore p))
1796     (let ((hinfo (value headers-information))
1797     (minfo (value message-information)))
1798     (cond (hinfo
1799     (multiple-value-bind (cur-msg cur-mark)
1800     (headers-current-message hinfo)
1801     (unless cur-msg (editor-error "Not on a header line."))
1802     (delete-mark cur-mark)
1803     (delete-message (headers-info-folder hinfo) cur-msg)))
1804     (minfo
1805     (let ((msgs (message-info-msgs minfo)))
1806     (delete-message (message-info-folder minfo)
1807     (if (consp msgs) (car msgs) msgs)))
1808     (message "Message deleted."))
1809     (t (editor-error "Not in a headers or message buffer.")))))
1810    
1811     ;;; DELETE-MESSAGE takes a folder and message id and either flags this message
1812     ;;; for deletion or deletes it. All headers buffers into folder are updated,
1813     ;;; either by flagging a headers line or deleting it.
1814     ;;;
1815     (defun delete-message (folder msg)
1816     (cond ((value virtual-message-deletion)
1817     (mark-one-message folder msg "hemlockdeleted" :add)
1818     (do-headers-buffers (hbuf folder)
1819     (with-headers-mark (hmark hbuf msg)
1820     (with-writable-buffer (hbuf)
1821     (note-deleted-message-at-mark hmark)))))
1822     (t (mh "rmm" (list folder msg))
1823     (do-headers-buffers (hbuf folder hinfo)
1824     (with-headers-mark (hmark hbuf msg)
1825     (delete-headers-buffer-line hinfo hmark)))))
1826     (dolist (b *buffer-list*)
1827     (when (and (hemlock-bound-p 'message-information :buffer b)
1828     (buffer-modeline-field-p b :deleted-message))
1829     (dolist (w (buffer-windows b))
1830     (update-modeline-field b w :deleted-message)))))
1831    
1832     ;;; NOTE-DELETED-MESSAGE-AT-MARK takes a mark at the beginning of a valid
1833     ;;; headers line, sticks a "D" on the line, and frobs the line's deleted
1834     ;;; property. This assumes the headers buffer is modifiable.
1835     ;;;
1836     (defun note-deleted-message-at-mark (mark)
1837     (find-attribute mark :digit)
1838     (find-attribute mark :digit #'zerop)
1839     (character-offset mark 2)
1840     (setf (next-character mark) #\D)
1841     (setf (line-message-deleted (mark-line mark)) t))
1842    
1843     ;;; DELETE-HEADERS-BUFFER-LINE takes a headers information and a mark on the
1844     ;;; line to be deleted. Before deleting the line, we check to see if any
1845     ;;; message or draft buffers refer to the buffer because of the line. Due
1846     ;;; to how regions are deleted, line plists get messed up, so they have to
1847     ;;; be regenerated. We regenerate them for the whole buffer, so we don't have
1848     ;;; to hack the code to know which lines got messed up.
1849     ;;;
1850     (defun delete-headers-buffer-line (hinfo hmark)
1851     (delete-headers-line-references hinfo hmark)
1852     (let ((id (line-message-id (mark-line hmark)))
1853     (hbuf (headers-info-buffer hinfo)))
1854     (with-writable-buffer (hbuf)
1855     (with-mark ((end (line-start hmark) :left-inserting))
1856     (unless (line-offset end 1 0) (buffer-end end))
1857     (delete-region (region hmark end))))
1858     (let ((seq (mh-sequence-delete id (headers-info-msg-seq hinfo))))
1859     (setf (headers-info-msg-seq hinfo) seq)
1860     (setf (headers-info-msg-strings hinfo) (mh-sequence-strings seq)))
1861     (set-message-headers-ids hbuf)
1862     (when (value virtual-message-deletion)
1863     (let ((deleted-seq (mh-sequence-list (headers-info-folder hinfo)
1864     "hemlockdeleted")))
1865     (do-headers-lines (hbuf :line-var line)
1866     (setf (line-message-deleted line)
1867     (mh-sequence-member-p (line-message-id line) deleted-seq)))))))
1868    
1869    
1870     ;;; DELETE-HEADERS-LINE-REFERENCES removes any message buffer or draft buffer
1871     ;;; pointers to a headers buffer or marks into the headers buffer. Currently
1872     ;;; message buffers and draft buffers are identified differently for no good
1873     ;;; reason; probably message buffers should be located in the same way draft
1874     ;;; buffers are. Also, we currently assume only one of other-msg-bufs could
1875     ;;; refer to the line (similarly for draft-bufs), but this might be bug
1876     ;;; prone. The message buffer case couldn't happen since the buffer name
1877     ;;; would cause MAYBE-MAKE-MH-BUFFER to re-use the buffer, but you could reply
1878     ;;; to the same message twice simultaneously.
1879     ;;;
1880     (defun delete-headers-line-references (hinfo hmark)
1881     (let ((msg-id (line-message-id (mark-line hmark)))
1882     (main-msg-buf (headers-info-msg-buffer hinfo)))
1883     (declare (simple-string msg-id))
1884     (when main-msg-buf
1885     (let ((minfo (variable-value 'message-information :buffer main-msg-buf)))
1886     (when (string= (the simple-string (message-info-msgs minfo))
1887     msg-id)
1888     (cond ((message-info-draft-buf minfo)
1889     (cleanup-headers-reference main-msg-buf minfo)
1890     (setf (headers-info-msg-buffer hinfo) nil))
1891     (t (delete-buffer-if-possible main-msg-buf))))))
1892     (dolist (mbuf (headers-info-other-msg-bufs hinfo))
1893     (let ((minfo (variable-value 'message-information :buffer mbuf)))
1894     (when (string= (the simple-string (message-info-msgs minfo))
1895     msg-id)
1896     (cond ((message-info-draft-buf minfo)
1897     (cleanup-headers-reference mbuf minfo)
1898     (setf (headers-info-other-msg-bufs hinfo)
1899     (delete mbuf (headers-info-other-msg-bufs hinfo)
1900     :test #'eq)))
1901     (t (delete-buffer-if-possible mbuf)))
1902     (return)))))
1903     (dolist (dbuf (headers-info-draft-bufs hinfo))
1904     (let ((dinfo (variable-value 'draft-information :buffer dbuf)))
1905     (when (same-line-p (draft-info-headers-mark dinfo) hmark)
1906     (cleanup-headers-reference dbuf dinfo)
1907     (setf (headers-info-draft-bufs hinfo)
1908     (delete dbuf (headers-info-draft-bufs hinfo) :test #'eq))
1909     (return)))))
1910    
1911    
1912     (defcommand "Undelete Message" (p)
1913     "Prompts for a folder, messages to undelete, and pick expression. When in
1914     a headers buffer into the same folder specified, the messages prompt
1915     defaults to those messages in the buffer; \"all\" may be entered if this is
1916     not what is desired. This command is only meaningful if you have
1917     \"Virtual Message Deletion\" set."
1918     "Prompts for a folder, messages to undelete, and pick expression. When in
1919     a headers buffer into the same folder specified, the messages prompt
1920     defaults to those messages in the buffer; \"all\" may be entered if this is
1921     not what is desired. This command is only meaningful if you have
1922     \"Virtual Message Deletion\" set."
1923     (declare (ignore p))
1924     (unless (value virtual-message-deletion)
1925     (editor-error "You don't use virtual message deletion."))
1926     (let* ((folder (prompt-for-folder))
1927     (hinfo (value headers-information))
1928     (temp-msgs (prompt-for-message
1929     :folder folder
1930     :messages
1931     (if (and hinfo
1932     (string= folder
1933     (the simple-string
1934     (headers-info-folder hinfo))))
1935     (headers-info-msg-strings hinfo))
1936     :prompt "MH messages to pick from: "))
1937     (pick-exp (prompt-for-pick-expression))
1938     (msgs (if pick-exp
1939     (or (pick-messages folder temp-msgs pick-exp) temp-msgs)
1940     temp-msgs)))
1941     (declare (simple-string folder))
1942     (mh "mark" `(,folder ,@msgs "-sequence" "hemlockdeleted" "-delete"))
1943     (let ((deleted-seq (mh-sequence-list folder "hemlockdeleted")))
1944     (do-headers-buffers (hbuf folder)
1945     (with-writable-buffer (hbuf)
1946     (do-headers-lines (hbuf :line-var line :mark-var hmark)
1947     (when (and (line-message-deleted line)
1948     (not (mh-sequence-member-p (line-message-id line)
1949     deleted-seq)))
1950     (note-undeleted-message-at-mark hmark))))))))
1951    
1952     (defcommand "Headers Undelete Message" (p)
1953     "Undelete the current message.
1954     When in a headers buffer, undeletes the message on the current line. When
1955     in a message buffer, undeletes that message. This command is only
1956     meaningful if you have \"Virtual Message Deletion\" set."
1957     "When in a headers buffer, undeletes the message on the current line. When
1958     in a message buffer, undeletes that message. This command is only
1959     meaningful if you have \"Virtual Message Deletion\" set."
1960     (declare (ignore p))
1961     (unless (value virtual-message-deletion)
1962     (editor-error "You don't use virtual message deletion."))
1963     (let ((hinfo (value headers-information))
1964     (minfo (value message-information)))
1965     (cond (hinfo
1966     (multiple-value-bind (cur-msg cur-mark)
1967     (headers-current-message hinfo)
1968     (unless cur-msg (editor-error "Not on a header line."))
1969     (delete-mark cur-mark)
1970     (undelete-message (headers-info-folder hinfo) cur-msg)))
1971     (minfo
1972     (undelete-message (message-info-folder minfo)
1973     (message-info-msgs minfo))
1974     (message "Message undeleted."))
1975     (t (editor-error "Not in a headers or message buffer.")))))
1976    
1977     ;;; UNDELETE-MESSAGE takes a folder and a message id. All headers buffers into
1978     ;;; folder are updated.
1979     ;;;
1980     (defun undelete-message (folder msg)
1981     (mark-one-message folder msg "hemlockdeleted" :delete)
1982     (do-headers-buffers (hbuf folder)
1983     (with-headers-mark (hmark hbuf msg)
1984     (with-writable-buffer (hbuf)
1985     (note-undeleted-message-at-mark hmark))))
1986     (dolist (b *buffer-list*)
1987     (when (and (hemlock-bound-p 'message-information :buffer b)
1988     (buffer-modeline-field-p b :deleted-message))
1989     (dolist (w (buffer-windows b))
1990     (update-modeline-field b w :deleted-message)))))
1991    
1992     ;;; NOTE-UNDELETED-MESSAGE-AT-MARK takes a mark at the beginning of a valid
1993     ;;; headers line, sticks a space on the line in place of a "D", and frobs the
1994     ;;; line's deleted property. This assumes the headers buffer is modifiable.
1995     ;;;
1996     (defun note-undeleted-message-at-mark (hmark)
1997     (find-attribute hmark :digit)
1998     (find-attribute hmark :digit #'zerop)
1999     (character-offset hmark 2)
2000     (setf (next-character hmark) #\space)
2001     (setf (line-message-deleted (mark-line hmark)) nil))
2002    
2003    
2004     (defcommand "Expunge Messages" (p)
2005     "Expunges messages marked for deletion.
2006     This command prompts for a folder, invoking RMM on the \"hemlockdeleted\"
2007     sequence after asking the user for confirmation. Setting \"Quit Headers
2008     Confirm\" to nil inhibits prompting. The folder's message id's are packed
2009     with FOLDER -pack. When in a headers buffer, uses that folder. When in a
2010     message buffer, uses its folder, updating any associated headers buffer.
2011     When \"Temporary Draft Folder\" is bound, this folder's messages are deleted
2012     and expunged."
2013     "Prompts for a folder, invoking RMM on the \"hemlockdeleted\" sequence and
2014     packing the message id's with FOLDER -pack. When in a headers buffer,
2015     uses that folder."
2016     (declare (ignore p))
2017     (let* ((hinfo (value headers-information))
2018     (minfo (value message-information))
2019     (folder (cond (hinfo (headers-info-folder hinfo))
2020     (minfo (message-info-folder minfo))
2021     (t (prompt-for-folder))))
2022     (deleted-seq (mh-sequence-list folder "hemlockdeleted")))
2023     ;;
2024     ;; Delete the messages if there are any.
2025     ;; This deletes "hemlockdeleted" from sequence file; we don't have to.
2026     (when (and deleted-seq
2027     (or (not (value expunge-messages-confirm))
2028     (prompt-for-y-or-n
2029     :prompt (list "Expunge messages and pack folder ~A? "
2030     folder)
2031     :default t
2032     :default-string "Y")))
2033     (message "Deleting messages ...")
2034     (mh "rmm" (list folder "hemlockdeleted"))
2035     ;;
2036     ;; Compact the message id's after deletion.
2037     (let ((*standard-output* *mh-utility-bit-bucket*))
2038     (message "Compacting folder ...")
2039     (mh "folder" (list folder "-fast" "-pack")))
2040     ;;
2041     ;; Do a bunch of consistency maintenance.
2042     (let ((new-buf-p (eq (current-buffer) *new-mail-buffer*)))
2043     (message "Maintaining consistency ...")
2044     (expunge-messages-fold-headers-buffers folder)
2045     (expunge-messages-fix-draft-buffers folder)
2046     (expunge-messages-fix-unseen-headers folder)
2047     (when new-buf-p (change-to-buffer *new-mail-buffer*)))
2048     (delete-and-expunge-temp-drafts))))
2049    
2050     ;;; EXPUNGE-MESSAGES-FOLD-HEADERS-BUFFERS deletes all headers buffers into the
2051     ;;; compacted folder. We can only update the headers buffers by installing all
2052     ;;; headers, so there may as well be only one such buffer. First we get a list
2053     ;;; of the buffers since DO-HEADERS-BUFFERS is trying to iterate over a list
2054     ;;; being destructively modified by buffer deletions.
2055     ;;;
2056     (defun expunge-messages-fold-headers-buffers (folder)
2057     (let (hbufs)
2058     (declare (list hbufs))
2059     (do-headers-buffers (b folder)
2060     (unless (eq b *new-mail-buffer*)
2061     (push b hbufs)))
2062     (unless (zerop (length hbufs))
2063     (dolist (b hbufs)
2064     (delete-headers-buffer-and-message-buffers-command nil b))
2065     (new-message-headers folder (list "all")))))
2066    
2067     ;;; EXPUNGE-MESSAGES-FIX-DRAFT-BUFFERS finds any draft buffer that was set up
2068     ;;; as a reply to some message in folder, removing this relationship in case
2069     ;;; that message id does not exist after expunge folder compaction.
2070     ;;;
2071     (defun expunge-messages-fix-draft-buffers (folder)
2072     (declare (simple-string folder))
2073     (dolist (b *buffer-list*)
2074     (when (hemlock-bound-p 'draft-information :buffer b)
2075     (let* ((dinfo (variable-value 'draft-information :buffer b))
2076     (reply-folder (draft-info-replied-to-folder dinfo)))
2077     (when (and reply-folder
2078     (string= (the simple-string reply-folder) folder))
2079     (setf (draft-info-replied-to-folder dinfo) nil)
2080     (setf (draft-info-replied-to-msg dinfo) nil))))))
2081    
2082     ;;; EXPUNGE-MESSAGES-FIX-UNSEEN-HEADERS specially handles the unseen headers
2083     ;;; buffer apart from the other headers buffers into the same folder when
2084     ;;; messages have been expunged. We must delete the associated message buffers
2085     ;;; since REVAMP-HEADERS-BUFFER does not, and these potentially reference bad
2086     ;;; message id's. When doing this we must copy the other-msg-bufs list since
2087     ;;; the delete buffer cleanup hook for them is destructive. Then we check for
2088     ;;; more unseen messages.
2089     ;;;
2090     (defun expunge-messages-fix-unseen-headers (folder)
2091     (declare (simple-string folder))
2092     (when *new-mail-buffer*
2093     (let ((hinfo (variable-value 'headers-information
2094     :buffer *new-mail-buffer*)))
2095     (when (string= (the simple-string (headers-info-folder hinfo))
2096     folder)
2097     (let ((other-bufs (copy-list (headers-info-other-msg-bufs hinfo))))
2098     (dolist (b other-bufs) (delete-buffer-if-possible b)))
2099     (with-writable-buffer (*new-mail-buffer*)
2100     (revamp-headers-buffer *new-mail-buffer* hinfo)
2101     ;; Restore the name in case someone used "Pick Headers".
2102     (setf (buffer-name *new-mail-buffer*)
2103     (format nil "Unseen Headers ~A" folder))
2104     (let ((region (maybe-get-new-mail-msg-hdrs folder)))
2105     (when region
2106     (insert-message-headers *new-mail-buffer* hinfo region))))))))
2107    
2108     ;;; MAYBE-GET-NEW-MAIL-MSG-HDRS returns a region suitable for a new mail buffer
2109     ;;; or nil. Folder is probed for unseen headers, and if there are some, then
2110     ;;; we call GET-NEW-MAIL-MSG-HDRS which also uses "Unseen Headers Message Spec".
2111     ;;; If there are no unseen headers, we only look for "Unseen Headers Message
2112     ;;; Spec" messages. We go through these contortions to keep MH from outputting
2113     ;;; errors.
2114     ;;;
2115     (defun maybe-get-new-mail-msg-hdrs (folder)
2116     (let ((unseen-seq-name (mh-profile-component "unseen-sequence")))
2117     (multiple-value-bind (unseen-seq foundp)
2118     (mh-sequence-list folder unseen-seq-name)
2119     (if (and foundp unseen-seq)
2120     (get-new-mail-msg-hdrs folder unseen-seq-name)
2121     (let ((spec (value unseen-headers-message-spec)))
2122     (when spec
2123     (message-headers-to-region
2124     folder
2125     (breakup-message-spec (string-trim '(#\space #\tab) spec)))))))))
2126    
2127    
2128    
2129     ;;;; Folders.
2130    
2131     (defvar *folder-name-table* nil)
2132    
2133     (defun check-folder-name-table ()
2134     (unless *folder-name-table*
2135     (message "Finding folder names ...")
2136     (setf *folder-name-table* (make-string-table))
2137     (let* ((output (with-output-to-string (*standard-output*)
2138     (mh "folders" '("-fast"))))
2139     (length (length output))
2140     (start 0))
2141     (declare (simple-string output))
2142     (loop
2143     (when (> start length) (return))
2144     (let ((nl (position #\newline output :start start)))
2145     (unless nl (return))
2146     (unless (= start nl)
2147     (setf (getstring (subseq output start nl) *folder-name-table*) t))