summaryrefslogtreecommitdiff
path: root/lisp/mail/mh-utils.el
blob: a6501fede672323519f2ed7e9fd9ee258d3c22a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
;;; mh-utils.el --- mh-e code needed for both sending and reading

;; Copyright (C) 1993, 1995, 1997, 2000, 2001, 2002 Free Software Foundation, Inc.

;; Author: Bill Wohler <wohler@newt.com>
;; Maintainer: Bill Wohler <wohler@newt.com>
;; Keywords: mail
;; See: mh-e.el

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; Internal support for mh-e package.

;;; Change Log:

;; $Id: mh-utils.el,v 1.79 2002/04/07 19:20:56 wohler Exp $

;;; Code:

(load "executable" t t)                 ; Non-fatal dependency on
					; executable-find

;;; Autoload mh-seq

(autoload 'mh-add-to-sequence "mh-seq")
(autoload 'mh-notate-seq "mh-seq")
(autoload 'mh-read-seq-default "mh-seq")
(autoload 'mh-map-to-seq-msgs "mh-seq")

;;; Other Autoloads

(autoload 'mail-header-end "sendmail")

;;; Set for local environment:
;;; mh-progs and mh-lib used to be set in paths.el, which tried to
;;; figure out at build time which of several possible directories MH
;;; was installed into.  But if you installed MH after building Emacs,
;;; this would almost certainly be wrong, so now we do it at run time.

(defvar mh-progs nil
  "Directory containing MH commands, such as inc, repl, and rmm.")

(defvar mh-lib nil
  "Directory containing the MH library.
This directory contains, among other things,
the components file.")

(defvar mh-lib-progs nil
  "Directory containing MH helper programs.
This directory contains, among other things,
the mhl program.")

(defvar mh-nmh-p nil
  "Non-nil if nmh is installed on this system instead of MH.")

;;;###autoload
(put 'mh-progs 'risky-local-variable t)
;;;###autoload
(put 'mh-lib 'risky-local-variable t)
;;;###autoload
(put 'mh-lib-progs 'risky-local-variable t)
;;;###autoload
(put 'mh-nmh-p 'risky-local-variable t)

;;; User preferences:

(defgroup mh-buffer nil
  "Layout of MH-E buffers"
  :prefix "mh-"
  :group 'mh)


(defcustom mh-auto-folder-collect t
  "*Whether to start collecting MH folder names immediately in the background.
Non-nil means start a background process collecting the names of all
folders as soon as mh-e is loaded."
  :type 'boolean
  :group 'mh)

(defcustom mh-recursive-folders nil
  "*If non-nil, then commands which operate on folders do so recursively."
  :type 'boolean
  :group 'mh)

(defcustom mh-clean-message-header t
  "*Non-nil means clean headers of messages that are displayed or inserted.
The variables `mh-visible-headers' and `mh-invisible-headers' control what
is removed."
  :type 'boolean
  :group 'mh-buffer)

(defcustom mh-visible-headers nil
  "*If non-nil, contains a regexp specifying the headers to keep when cleaning.
Only used if `mh-clean-message-header' is non-nil.  Setting this variable
overrides `mh-invisible-headers'."
  :type '(choice (const nil) regexp)
  :group 'mh-buffer)

(defvar mh-invisible-headers
  (concat
   "^"
   (let ((max-specpdl-size 1000))	;workaround for insufficient default
     (regexp-opt
      '( ;; RFC 822
	"Received: " "Message-Id: " "Return-Path: "
	;; RFC 2045
	"Mime-Version" "Content-"
	;; sendmail
	"X-Authentication-Warning: " "X-MIME-Autoconverted: " "From "
	"Status: "
	;; X400
	"X400-" "P1-Message-Id: " "Original-Encoded-Information-Types: "
	"P1-Recipient: " "P1-Content-Type: " "Ua-Content-Id: "
	;; MH
	"Resent" "Prev-Resent" "Forwarded: " "Replied: " "Delivery-Date: "
	"In-Reply-To: " "Remailed-" "Via: " "Mail-from: "
	;; gnus
	"X-Gnus-Mail-Source: "
	;; MS Outlook
	"X-Priority: " "X-Msmail-" "X-MimeOLE: " "X-Apparently-From: "
	"Importance: " "Sensitivity: " "X-MS-TNEF-Correlator: "
	;; Juno
	"X-Juno-"
	;; Hotmail
	"X-OriginalArrivalTime: " "X-Originating-IP: "
	;; Netscape/Mozilla
	"X-Accept-Language: " "X-Mozilla-Status: "
	;; NTMail
	"X-Info: " "X-VSMLoop: "
	;; News
	"NNTP-" "X-News: "
	;; Mailman mailing list manager
	"List-" "X-Beenthere: " "X-Mailman-Version: "
	;; Egroups/yahoogroups mailing list manager
	"X-eGroups-" "X-Apparently-To: " "Mailing-List: " "Delivered-To: "
	;; SourceForge mailing list manager
	"X-Original-Date: "
	;; Unknown mailing list managers
	"X-Mailing-List: " "X-Loop: "
	"List-Subscribe: " "List-Unsubscribe: "
	"X-List-Subscribe: " "X-List-Unsubscribe: "
	"X-Listserver: " "List-" "X-List-Host: "
	;; Sieve filtering
	"X-Sieve: "
	;; Worldtalk gateways
	"X-Wss-Id: "
	;; User added
	"X-Face: " "X-Qotd-"
	;; Miscellaneous
	"X-Sender: " "X-Ack: " "Errors-To: " "Precedence: " "X-Message-Id"
	"X-From-Line" "X-Cron-Env: " "Delivery: " "X-Delivered"
	"X-Received: " "X-Vms-To: " "Xref: " "X-Request-" "X-UIDL: "
	"X-Orcl-Content-Type: " "X-Server-Uuid: " "X-Envelope-Sender: "
	"X-Envelope-To: " "Encoding: " "Old-Return-Path: " "Path: "
	"References: " "Lines: " "Autoforwarded: " "Bestservhost: "
	"X-pgp: " "X-Accept-Language: " "Priority: " "User-Agent: "
	"X-MIMETrack: " "X-Abuse-Info: " "X-Complaints-To: "
	"X-No-Archive: " "X-Original-Complaints-To: "
	"X-Original-Trace: " "X-Received-Date: " "X-Server-Date: "
	"X-Trace: " "X-UserInfo1: " "X-submission-address: ")
      t)))
   "*Regexp matching lines in a message header that are not to be shown.
If `mh-visible-headers' is non-nil, it is used instead to specify what
to keep.")

;;; Additional header fields that might someday be added:
;;; "Sender: " "Reply-to: "

(defcustom mh-bury-show-buffer t
  "*Non-nil means that the displayed show buffer for a folder is buried."
  :type 'boolean
  :group 'mh-buffer)

(defcustom mh-summary-height (or (and (fboundp 'frame-height)
                                      (> (frame-height) 24)
                                      (min 10 (/ (frame-height) 6)))
                                 4)
  "*Number of lines in MH-Folder window (including the mode line)."
  :type 'integer
  :group 'mh-buffer)

;; Use goto-addr if it was already loaded (which probably sets this
;; variable to t), or if this variable is otherwise set to t.
(defcustom mh-show-use-goto-addr (and (boundp 'goto-address-highlight-p)
                                      goto-address-highlight-p)
  "*Non-nil means URLs and e-mail addresses are highlighted using goto-addr while in mh-show-mode."
  :type 'boolean
  :group 'mh-buffer)

(defvar mh-scan-msg-number-regexp "^ *\\([0-9]+\\)"
  "Regexp to find the number of a message in a scan line.
The message's number must be surrounded with \\( \\)")

(defvar mh-scan-msg-search-regexp "^[^0-9]*%d[^0-9]"
  "Format string containing a regexp matching the scan listing for a message.
The desired message's number will be an argument to format.")

(defcustom mhl-formfile nil
  "*Name of format file to be used by mhl to show and print messages.
A value of T means use the default format file.
Nil means don't use mhl to format messages when showing; mhl is still used,
with the default format file, to format messages when printing them.
The format used should specify a non-zero value for overflowoffset so
the message continues to conform to RFC 822 and mh-e can parse the headers."
  :type '(choice (const nil) (const t) string)
  :group 'mh)
(put 'mhl-formfile 'info-file "mh-e")

(defvar mh-decode-quoted-printable-have-mimedecode
  (not (null (and (fboundp 'executable-find)(executable-find "mimedecode"))))
  "Whether the mimedecode command is installed on the system.
This sets the default value of variable `mh-decode-quoted-printable' to
determine whether quoted-printable MIME parts are decode when viewed in
`mh-show'.  The source code for mimedecode can be obtained from
http://www.freesoft.org/CIE/FAQ/mimedeco.c")

(defcustom mh-decode-quoted-printable
  mh-decode-quoted-printable-have-mimedecode
  "Whether to decode quoted-printable MIME parts in `mh-show'.
This can only be done if the 'mimedecode' command is available in the
executable path on the system (the mh-decode-quoted-printable-have-mimedecode
variable is set if the command was found).  That program is used as a helper
program to achieve this.  The source code for mimedecode can usually be
obtained from http://www.freesoft.org/CIE/FAQ/mimedeco.c"
  :type 'boolean
  :group 'mh-buffer)

(defcustom mh-update-sequences-after-mh-show t
  "Whether to call `mh-update-sequence' in `mh-show-mode'.
If set, `mh-update-sequence' is run every time a message is shown, telling
MH or nmh that this is your current message.  It's useful, for example, to
display MIME content using \"M-! mhshow RET\""
  :type 'boolean
  :group 'mh-buffer)

(defcustom mh-highlight-citation-p 'gnus
  "How to highlight citations in show buffers.
The gnus method uses a different color for each indentation."
  :type '(choice (const :tag "Use gnus" gnus)
                 (const :tag "Use font-lock" font-lock)
                 (const :tag "Don't fontify" nil))
  :group 'mh-buffer)

(defvar mh-default-folder-for-message-function nil
  "Function to select a default folder for refiling or Fcc.
If set to a function, that function is called with no arguments by
`\\[mh-refile-msg]' and `\\[mh-to-fcc]' to get a default when
prompting the user for a folder.  The function is called from within a
`save-excursion', with point at the start of the message.  It should
return the folder to offer as the refile or Fcc folder, as a string
with a leading `+' sign.  It can also return an empty string to use no
default, or NIL to calculate the default the usual way.
NOTE: This variable is not an ordinary hook;
It may not be a list of functions.")

(defvar mh-find-path-hook nil
  "Invoked by `mh-find-path' while reading the user's MH profile.")

(defvar mh-folder-list-change-hook nil
  "Invoked whenever the cached folder list `mh-folder-list' is changed.")

(defvar mh-show-buffer-mode-line-buffer-id "{show-%s} %d"
  "Format string to produce `mode-line-buffer-identification' for show buffers.
First argument is folder name.  Second is message number.")

(defvar mh-cmd-note 4
  "Offset to insert notation.")

(defvar mh-note-seq "%"
  "String whose first character is used to notate messages in a sequence.")

(defvar mh-mail-header-separator "--------"
  "*Line used by MH to separate headers from text in messages being composed.
This variable should not be used directly in programs. Programs should use
`mail-header-separator' instead. `mail-header-separator' is initialized to
`mh-mail-header-separator' in `mh-letter-mode'; in other contexts, you may
have to perform this initialization yourself.

Do not make this a regexp as it may be the argument to `insert' and it is
passed through `regexp-quote' before being used by functions like
`re-search-forward'.")

(defun mh-in-header-p ()
  ;; Return non-nil if the point is in the header of a draft message.
  (< (point) (mail-header-end)))

(defun mh-header-field-end ()
  ;; Move to the end of the current header field.
  ;; Handles RFC 822 continuation lines.
  (forward-line 1)
  (while (looking-at "^[ \t]")
    (forward-line 1))
  (backward-char 1))		;to end of previous line

(defun mh-letter-header-font-lock (limit)
  "Return the entire mail header to font-lock.
Argument LIMIT limits search."
  (if (= (point) limit)
      nil
    (let* ((mail-header-end (save-match-data (mail-header-end)))
           (lesser-limit (if (< mail-header-end limit) mail-header-end limit)))
      (when (mh-in-header-p)
        (set-match-data (list 1 lesser-limit))
        (goto-char lesser-limit)
        t))))

(defun mh-header-field-font-lock (field limit)
  "Return the value of a header field FIELD to font-lock.
Argument LIMIT limits search."
  (if (= (point) limit)
      nil
    (let* ((mail-header-end (mail-header-end))
           (lesser-limit (if (< mail-header-end limit) mail-header-end limit))
           (case-fold-search t))
      (when (and (< (point) mail-header-end)   ;Only within header
                 (re-search-forward (format "^%s" field) lesser-limit t))
        (let ((match-one-b (match-beginning 0))
              (match-one-e (match-end 0)))
          (mh-header-field-end)
          (if (> (point) limit)           ;Don't search for end beyond limit
              (goto-char limit))
          (set-match-data (list match-one-b match-one-e
                                (1+ match-one-e) (point)))
          t)))))

(defun mh-header-to-font-lock (limit)
  (mh-header-field-font-lock "To:" limit))

(defun mh-header-cc-font-lock (limit)
  (mh-header-field-font-lock "cc:" limit))

(defun mh-header-subject-font-lock (limit)
  (mh-header-field-font-lock "Subject:" limit))

(defvar mh-show-to-face 'mh-show-to-face
  "Face for highlighting the To: header field.")
(if (boundp 'facemenu-unlisted-faces)
    (add-to-list 'facemenu-unlisted-faces "^mh-show"))
(defface mh-show-to-face
  '((((class grayscale) (background light))
     (:foreground "DimGray" :underline t))
    (((class grayscale) (background dark))
     (:foreground "LightGray" :underline t))
    (((class color) (background light)) (:foreground "SaddleBrown"))
    (((class color) (background dark))  (:foreground "burlywood"))
    (t (:underline t)))
  "Face for highlighting the To: header field."
  :group 'mh-buffer)

(defvar mh-show-from-face 'mh-show-from-face
  "Face for highlighting the From: header field.")
(defface mh-show-from-face
  '((((class color) (background light))
     (:foreground "red3"))
    (((class color) (background dark))
     (:foreground "cyan"))
    (t
     (:bold t)))
  "Face for highlighting the From: header field."
  :group 'mh-buffer)

(defvar mh-folder-subject-face 'mh-folder-subject-face
  "Face for highlighting subject text in MH-Folder buffers.")
(if (boundp 'facemenu-unlisted-faces)
    (add-to-list 'facemenu-unlisted-faces "^mh-folder"))
(defface mh-folder-subject-face
  '((((class color) (background light))
     (:foreground "blue4"))
    (((class color) (background dark))
     (:foreground "yellow"))
    (t
     (:bold t)))
  "Face for highlighting subject text in MH-Folder buffers."
  :group 'mh)
(defvar mh-show-subject-face 'mh-show-subject-face
  "Face for highlighting the Subject header field.")
(copy-face 'mh-folder-subject-face 'mh-show-subject-face)

(eval-after-load "font-lock"
  '(progn
     (defvar mh-show-cc-face 'mh-show-cc-face
       "Face for highlighting cc header fields.")
     (copy-face 'font-lock-variable-name-face 'mh-show-cc-face)
     (defvar mh-show-date-face 'mh-show-date-face
       "Face for highlighting the Date header field.")
     (copy-face 'font-lock-type-face 'mh-show-date-face)
     (defvar mh-show-header-face 'mh-show-header-face
       "Face used to deemphasize unspecified header fields.")
     (copy-face 'font-lock-string-face 'mh-show-header-face)
     
     (defvar mh-show-font-lock-keywords
       '(("^\\(From:\\|Sender:\\)\\(.*\\)"
          (1 'default) (2 mh-show-from-face))
         (mh-header-to-font-lock
          (0 'default) (1 mh-show-to-face))
         (mh-header-cc-font-lock
	  (0 'default) (1 mh-show-cc-face))
         ("^\\(Reply-To:\\|Return-Path:\\)\\(.*\\)$"
          (1 'default) (2 mh-show-from-face))
         (mh-header-subject-font-lock
          (0 'default) (1 mh-show-subject-face))
         ("^\\(Apparently-To:\\|Newsgroups:\\)\\(.*\\)"
          (1 'default) (2 mh-show-cc-face))
         ("^\\(In-reply-to\\|Date\\):\\(.*\\)$"
          (1 'default) (2 mh-show-date-face))
         (mh-letter-header-font-lock (0 mh-show-header-face append t)))
       "Additional expressions to highlight in MH-show mode.")
     
     (defvar mh-show-font-lock-keywords-with-cite
       (eval-when-compile
         (let* ((cite-chars "[>|}]")
                (cite-prefix "A-Za-z")
                (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
           (append
            mh-show-font-lock-keywords
            (list
             ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
             `(,cite-chars
               (,(concat "\\=[ \t]*"
                         "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
                         "\\(" cite-chars "[ \t]*\\)\\)+"
                         "\\(.*\\)")
                (beginning-of-line) (end-of-line)
                (2 font-lock-constant-face nil t)
                (4 font-lock-comment-face nil t)))))))
       "Additional expressions to highlight in MH-show mode.")
     ))

(defun mh-gnus-article-highlight-citation ()
  "Highlight cited text in current buffer using gnus."
  (interactive)
  (require 'gnus-cite)
  (let ((modified (buffer-modified-p))
        (gnus-article-buffer (buffer-name))
        (gnus-cite-face-list
         '(gnus-cite-face-2 gnus-cite-face-3 gnus-cite-face-4 gnus-cite-face-5
           gnus-cite-face-6 gnus-cite-face-7 gnus-cite-face-8 gnus-cite-face-9
           gnus-cite-face-10 gnus-cite-face-11 gnus-cite-face-1)))
    (gnus-article-highlight-citation t)
    (set-buffer-modified-p modified)))

;;; Internal bookkeeping variables:

;; The value of `mh-folder-list-change-hook' is called whenever
;; mh-folder-list variable is set.
;; List of folder names for completion.
(defvar mh-folder-list nil)

;; Cached value of the `Path:' component in the user's MH profile.
;; User's mail folder directory.
(defvar mh-user-path nil)

;; An mh-draft-folder of NIL means do not use a draft folder.
;; Cached value of the `Draft-Folder:' component in the user's MH profile.
;; Name of folder containing draft messages.
(defvar mh-draft-folder nil)

;; Cached value of the `Unseen-Sequence:' component in the user's MH profile.
;; Name of the Unseen sequence.
(defvar mh-unseen-seq nil)

;; Cached value of the `Previous-Sequence:' component in the user's MH
;; profile.
;; Name of the Previous sequence.
(defvar mh-previous-seq nil)

;; Cached value of the `Inbox:' component in the user's MH profile,
;; or "+inbox" if no such component.
;; Name of the Inbox folder.
(defvar mh-inbox nil)

;; Name of mh-e scratch buffer.
(defconst mh-temp-buffer " *mh-temp*")

;; Name of the mh-e folder list buffer.
(defconst mh-temp-folders-buffer "*Folders*")

;; Name of the mh-e sequences list buffer.
(defconst mh-temp-sequences-buffer "*Sequences*")

;; Window configuration before mh-e command.
(defvar mh-previous-window-config nil)

;;Non-nil means next SPC or whatever goes to next undeleted message.
(defvar mh-page-to-next-msg-p nil)

;;; Internal variables local to a folder.

;; Name of current folder, a string.
(defvar mh-current-folder nil)

;; Buffer that displays message for this folder.
(defvar mh-show-buffer nil)

;; Full path of directory for this folder.
(defvar mh-folder-filename nil)
  
;;Number of msgs in buffer.
(defvar mh-msg-count nil)

;; If non-nil, show the message in a separate window.
(defvar mh-showing-mode nil)

;;; This holds a documentation string used by describe-mode.
(defun mh-showing-mode (&optional arg)
  "Change whether messages should be displayed.
With arg, display messages iff ARG is positive."
  (setq mh-showing-mode
	(if (null arg)
	    (not mh-showing-mode)
	  (> (prefix-numeric-value arg) 0))))

;; The sequences of this folder.  An alist of (seq . msgs).
(defvar mh-seq-list nil)

;; List of displayed messages to be removed from the Unseen sequence.
(defvar mh-seen-list nil)

;; If non-nil, show buffer contains message with all headers.
;; If nil, show buffer contains message processed normally.
;; Showing message with headers or normally.
(defvar mh-showing-with-headers nil)


;;; mh-e macros

(defmacro with-mh-folder-updating (save-modification-flag-p &rest body)
  ;; Format is (with-mh-folder-updating (SAVE-MODIFICATION-FLAG-P) &body BODY).
  ;; Execute BODY, which can modify the folder buffer without having to
  ;; worry about file locking or the read-only flag, and return its result.
  ;; If SAVE-MODIFICATION-FLAG-P is non-nil, the buffer's modification
  ;; flag is unchanged, otherwise it is cleared.
  (setq save-modification-flag-p (car save-modification-flag-p)) ; CL style
  `(prog1
       (let ((mh-folder-updating-mod-flag (buffer-modified-p))
	     (buffer-read-only nil)
	     (buffer-file-name nil))	;don't let the buffer get locked
	 (prog1
	     (progn
	       ,@body)
	   (mh-set-folder-modified-p mh-folder-updating-mod-flag)))
     ,@(if (not save-modification-flag-p)
	   '((mh-set-folder-modified-p nil)))))

(put 'with-mh-folder-updating 'lisp-indent-hook 1)

(defmacro mh-in-show-buffer (show-buffer &rest body)
  ;; Format is (mh-in-show-buffer (SHOW-BUFFER) &body BODY).
  ;; Display buffer SHOW-BUFFER in other window and execute BODY in it.
  ;; Stronger than save-excursion, weaker than save-window-excursion.
  (setq show-buffer (car show-buffer))	; CL style
  `(let ((mh-in-show-buffer-saved-window (selected-window)))
     (switch-to-buffer-other-window ,show-buffer)
     (if mh-bury-show-buffer (bury-buffer (current-buffer)))
     (unwind-protect
	 (progn
           ,@body)
       (select-window mh-in-show-buffer-saved-window))))

(put 'mh-in-show-buffer 'lisp-indent-hook 1)

(defmacro mh-make-seq (name msgs) (list 'cons name msgs))

(defmacro mh-seq-name (pair) (list 'car pair))

(defmacro mh-seq-msgs (pair) (list 'cdr pair))


;;; Ensure new buffers won't get this mode if default-major-mode is nil.
(put 'mh-show-mode 'mode-class 'special)

(define-derived-mode mh-show-mode text-mode "MH-Show"
  "Major mode for showing messages in mh-e.
The value of `mh-show-mode-hook' is called when a new message is displayed."
  (set (make-local-variable 'mail-header-separator) mh-mail-header-separator)
  (mh-show-unquote-From)
  (when mh-show-use-goto-addr
    (if (not (featurep 'goto-addr))
        (load "goto-addr" t t))
    (if (fboundp 'goto-address)
        (goto-address)))
  (make-local-variable 'font-lock-defaults)
  (set (make-local-variable 'font-lock-support-mode) nil)
  (cond
   ((equal mh-highlight-citation-p 'font-lock)
    (setq font-lock-defaults '(mh-show-font-lock-keywords-with-cite t)))
   ((equal mh-highlight-citation-p 'gnus)
    (setq font-lock-defaults '(mh-show-font-lock-keywords t))
    (mh-gnus-article-highlight-citation))
   (t
    (setq font-lock-defaults '(mh-show-font-lock-keywords t)))))

(defun mh-maybe-show (&optional msg)
  ;; If in showing mode, then display the message pointed to by the cursor.
  (if mh-showing-mode (mh-show msg)))

(defun mh-show (&optional message)
  "Show MESSAGE (default: message at cursor).
Force a two-window display with the folder window on top (size
`mh-summary-height') and the show buffer below it.
If the message is already visible, display the start of the message.

Display of the message is controlled by setting the variables
`mh-clean-message-header' and `mhl-formfile'.  The default behavior is
to scroll uninteresting headers off the top of the window.
Type \"\\[mh-header-display]\" to see the message with all its headers."
  (interactive)
  (and mh-showing-with-headers
       (or mhl-formfile mh-clean-message-header)
       (mh-invalidate-show-buffer))
  (mh-show-msg message))

(defun mh-show-mouse (EVENT)
  "Move point to mouse EVENT and show message."
  (interactive "e")
  (mouse-set-point EVENT)
  (mh-show))

(defun mh-show-msg (msg)
  (if (not msg)
      (setq msg (mh-get-msg-num t)))
  (mh-showing-mode t)
  (setq mh-page-to-next-msg-p nil)
  (let ((folder mh-current-folder)
	(clean-message-header mh-clean-message-header)
	(show-window (get-buffer-window mh-show-buffer)))
    (if (not (eq (next-window (minibuffer-window)) (selected-window)))
	(delete-other-windows))		; force ourself to the top window
    (mh-in-show-buffer (mh-show-buffer)
      (if (and show-window
	       (equal (mh-msg-filename msg folder) buffer-file-name))
	  (progn			;just back up to start
	    (goto-char (point-min))
	    (if (not clean-message-header)
		(mh-start-of-uncleaned-message)))
	(mh-display-msg msg folder))))
  (if (not (= (1+ (window-height)) (frame-height))) ;not horizontally split
      (shrink-window (- (window-height) mh-summary-height)))
  (mh-recenter nil)
  (if (not (memq msg mh-seen-list)) (setq mh-seen-list (cons msg mh-seen-list)))
  (when mh-update-sequences-after-mh-show
    (mh-update-sequences))
  (run-hooks 'mh-show-hook))


(defun mh-decode-quoted-printable ()
  ;; Run mimedecode commmand on current buffer, replacing it contents.
  (let ((case-fold-search t))
    (goto-char (point-min))
    (when (and (re-search-forward
                "^content-transfer-encoding:[ \t]*quoted-printable"
                nil t)
               (search-forward "\n\n" nil t))
      (message "Converting quoted-printable characters...")
      (let ((modified (buffer-modified-p))
            (command "mimedecode"))
        (shell-command-on-region (point-min) (point-max) command t t)
        (if (fboundp 'deactivate-mark)
            (deactivate-mark))
        (set-buffer-modified-p modified))
      (message "Converting quoted-printable characters... done."))))


(defun mh-show-unquote-From ()
  ;; Decode >From at beginning of lines for mh-show-mode
  (save-excursion
    (let ((modified (buffer-modified-p))
          (case-fold-search nil))
      (goto-char (mail-header-end))
      (while (re-search-forward "^>From" nil t)
        (replace-match "From"))
      (set-buffer-modified-p modified))))

(defun mh-display-msg (msg-num folder)
  ;; Display message NUMBER of FOLDER.
  ;; Sets the current buffer to the show buffer.
  (set-buffer folder)
  ;; Bind variables in folder buffer in case they are local
  (let ((formfile mhl-formfile)
	(clean-message-header mh-clean-message-header)
	(invisible-headers mh-invisible-headers)
	(visible-headers mh-visible-headers)
	(msg-filename (mh-msg-filename msg-num))
	(show-buffer mh-show-buffer))
    (if (not (file-exists-p msg-filename))
	(error "Message %d does not exist" msg-num))
    (set-buffer show-buffer)
    (cond ((not (equal msg-filename buffer-file-name))
	   (mh-unvisit-file)
	   (erase-buffer)
	   ;; Changing contents, so this hook needs to be reinitialized.
	   ;; pgp.el uses this.
	   (if (boundp 'write-contents-hooks) ;Emacs 19
	       (kill-local-variable 'write-contents-hooks))
	   (if formfile
	       (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
				       (if (stringp formfile)
					   (list "-form" formfile))
				       msg-filename)
	     (insert-file-contents msg-filename))
           (if mh-decode-quoted-printable
               (mh-decode-quoted-printable))
	   (goto-char (point-min))
	   (cond (clean-message-header
		  (mh-clean-msg-header (point-min)
				       invisible-headers
				       visible-headers)
		  (goto-char (point-min)))
		 (t
		  (mh-start-of-uncleaned-message)))
	   ;; the parts of visiting we want to do (no locking)
	   (or (eq buffer-undo-list t)	;don't save undo info for prev msgs
	       (setq buffer-undo-list nil))
	   (set-buffer-modified-p nil)
	   (set-buffer-auto-saved)
	   ;; the parts of set-visited-file-name we want to do (no locking)
	   (setq buffer-file-name msg-filename)
	   (setq buffer-backed-up nil)
	   (auto-save-mode 1)
	   (set-mark nil)
	   (mh-show-mode)
	   (setq mode-line-buffer-identification
		 (list (format mh-show-buffer-mode-line-buffer-id
			       folder msg-num)))
	   (set-buffer folder)
	   (setq mh-showing-with-headers nil)))))

(defun mh-start-of-uncleaned-message ()
  ;; position uninteresting headers off the top of the window
  (let ((case-fold-search t))
    (re-search-forward
     "^To:\\|^Cc:\\|^From:\\|^Subject:\\|^Date:" nil t)
    (beginning-of-line)
    (mh-recenter 0)))


(defun mh-invalidate-show-buffer ()
  ;; Invalidate the show buffer so we must update it to use it.
  (if (get-buffer mh-show-buffer)
      (save-excursion
	(set-buffer mh-show-buffer)
	(mh-unvisit-file))))


(defun mh-unvisit-file ()
  ;; Separate current buffer from the message file it was visiting.
  (or (not (buffer-modified-p))
      (null buffer-file-name)		;we've been here before
      (yes-or-no-p (format "Message %s modified; flush changes? "
			   (file-name-nondirectory buffer-file-name)))
      (error "Flushing changes not confirmed"))
  (clear-visited-file-modtime)
  (unlock-buffer)
  (setq buffer-file-name nil))

  
(defun mh-get-msg-num (error-if-no-message)
  ;; Return the message number of the displayed message.  If the argument
  ;; ERROR-IF-NO-MESSAGE is non-nil, then complain if the cursor is not
  ;; pointing to a message.
  (save-excursion
    (beginning-of-line)
    (cond ((looking-at mh-scan-msg-number-regexp)
	   (string-to-int (buffer-substring (match-beginning 1)
					    (match-end 1))))
	  (error-if-no-message
	   (error "Cursor not pointing to message"))
	  (t nil))))


(defun mh-msg-filename (msg &optional folder)
  ;; Return the file name of MESSAGE in FOLDER (default current folder).
  (expand-file-name (int-to-string msg)
		    (if folder
			(mh-expand-file-name folder)
			mh-folder-filename)))


(defun mh-clean-msg-header (start invisible-headers visible-headers)
  ;; Flush extraneous lines in a message header, from the given POINT to the
  ;; end of the message header.  If VISIBLE-HEADERS is non-nil, it contains a
  ;; regular expression specifying the lines to display, otherwise
  ;; INVISIBLE-HEADERS contains a regular expression specifying lines to
  ;; delete from the header.
  (let ((case-fold-search t)
        (after-change-functions nil))   ;Work around emacs-20 font-lock bug
					;causing an endless loop.
    (save-restriction
      (goto-char start)
      (if (search-forward "\n\n" nil 'move)
	  (backward-char 1))
      (narrow-to-region start (point))
      (goto-char (point-min))
      (if visible-headers
	  (while (< (point) (point-max))
	    (cond ((looking-at visible-headers)
		   (forward-line 1)
		   (while (looking-at "[ \t]") (forward-line 1)))
		  (t
		    (mh-delete-line 1)
		    (while (looking-at "[ \t]")
		      (mh-delete-line 1)))))
	  (while (re-search-forward invisible-headers nil t)
	    (beginning-of-line)
	    (mh-delete-line 1)
	    (while (looking-at "[ \t]")
	      (mh-delete-line 1))))
      (unlock-buffer))))


(defun mh-recenter (arg)
  ;; Like recenter but with two improvements:
  ;;  - only does anything if the current buffer is in the selected
  ;;    window.  (Commands like save-some-buffers can make this false.)
  ;;  - nil arg means recenter as with C-u prefix
  (if (eq (get-buffer-window (current-buffer))
	  (selected-window))
      ;; '(4) is the same as C-u prefix argument.
      (recenter (if arg arg '(4)))))


(defun mh-delete-line (lines)
  ;; Delete version of kill-line.
  (delete-region (point) (progn (forward-line lines) (point))))

(defun mh-notate (msg notation offset)
  ;; Marks MESSAGE with the character NOTATION at position OFFSET.
  ;; Null MESSAGE means the message that the cursor points to.
  (save-excursion
    (if (or (null msg)
	    (mh-goto-msg msg t t))
	(with-mh-folder-updating (t)
	  (beginning-of-line)
	  (forward-char offset)
	  (delete-char 1)
	  (insert notation)))))


(defun mh-find-msg-get-num (step)
  ;; Return the message number of the message on the current scan line
  ;; or one nearby.  Jumps over non-message lines, such as inc errors.
  ;; STEP tells whether to search forward or backward if we have to search.
  (or (mh-get-msg-num nil)
      (let ((msg-num nil)
	    (nreverses 0))
	(while (and (not msg-num)
		    (< nreverses 2))
	  (cond ((eobp)
		 (setq step -1)
		 (setq nreverses (1+ nreverses)))
		((bobp)
		 (setq step 1)
		 (setq nreverses (1+ nreverses))))
	  (forward-line step)
	  (setq msg-num (mh-get-msg-num nil)))
	msg-num)))

(defun mh-goto-msg (number &optional no-error-if-no-message dont-show)
  "Position the cursor at message NUMBER.
Optional non-nil second argument NO-ERROR-IF-NO-MESSAGE means return nil
instead of signaling an error if message does not exist; in this case, the
cursor is positioned near where the message would have been.
Non-nil third argument DONT-SHOW means not to show the message."
  (interactive "NGo to message: ")
  (setq number (prefix-numeric-value number)) ;Emacs 19
  ;; This basic routine tries to be as fast as possible,
  ;; using a binary search and minimal regexps.
  (let ((cur-msg (mh-find-msg-get-num -1))
	(jump-size mh-msg-count))
    (while (and (> jump-size 1)
		cur-msg
		(not (eq cur-msg number)))
      (cond ((< cur-msg number)
	     (setq jump-size (min (- number cur-msg)
				  (ash (1+ jump-size) -1)))
	     (forward-line jump-size)
	     (setq cur-msg (mh-find-msg-get-num 1)))
	    (t
	     (setq jump-size (min (- cur-msg number)
				  (ash (1+ jump-size) -1)))
	     (forward-line (- jump-size))
	     (setq cur-msg (mh-find-msg-get-num -1)))))
    (if (eq cur-msg number)
	(progn
	  (beginning-of-line)
	  (or dont-show
	      (mh-maybe-show number)
	      t))
      (if (not no-error-if-no-message)
	  (error "No message %d" number)))))


(defun mh-msg-search-pat (n)
  ;; Return a search pattern for message N in the scan listing.
  (format mh-scan-msg-search-regexp n))


(defun mh-get-profile-field (field)
  ;; Find and return the value of FIELD in the current buffer.
  ;; Returns NIL if the field is not in the buffer.
  (let ((case-fold-search t))
    (goto-char (point-min))
    (cond ((not (re-search-forward (format "^%s" field) nil t)) nil)
	  ((looking-at "[\t ]*$") nil)
	  (t
	   (re-search-forward "[\t ]*\\([^\t \n].*\\)$" nil t)
	   (let ((start (match-beginning 1)))
	     (end-of-line)
	     (buffer-substring start (point)))))))

(defvar mail-user-agent)
(defvar read-mail-command)

(defvar mh-find-path-run nil
  "Non-nil if `mh-find-path' has been run already.")

(defun mh-find-path ()
  ;; Set mh-progs, mh-lib, and mh-libs-progs
  ;; (This step is necessary if MH was installed after this Emacs was dumped.)
  ;; From profile file, set mh-user-path, mh-draft-folder,
  ;; mh-unseen-seq, mh-previous-seq, mh-inbox.
  (mh-find-progs)
  (unless mh-find-path-run
    (setq mh-find-path-run t)
    (setq read-mail-command 'mh-rmail)
    (setq mail-user-agent 'mh-e-user-agent))
  (save-excursion
    ;; Be sure profile is fully expanded before switching buffers
    (let ((profile (expand-file-name (or (getenv "MH") "~/.mh_profile"))))
      (set-buffer (get-buffer-create mh-temp-buffer))
      (setq buffer-offer-save nil)	;for people who set default to t
      (erase-buffer)
      (condition-case err
	  (insert-file-contents profile)
	(file-error
	 (mh-install profile err)))
      (setq mh-user-path (mh-get-profile-field "Path:"))
      (if (not mh-user-path)
	  (setq mh-user-path "Mail"))
      (setq mh-user-path
	    (file-name-as-directory
	     (expand-file-name mh-user-path (expand-file-name "~"))))
      (setq mh-draft-folder (mh-get-profile-field "Draft-Folder:"))
      (if mh-draft-folder
	  (progn
	    (if (not (mh-folder-name-p mh-draft-folder))
		(setq mh-draft-folder (format "+%s" mh-draft-folder)))
	    (if (not (file-exists-p (mh-expand-file-name mh-draft-folder)))
		(error "Draft folder \"%s\" not found.  Create it and try again"
		       (mh-expand-file-name mh-draft-folder)))))
      (setq mh-inbox (mh-get-profile-field "Inbox:"))
      (cond ((not mh-inbox)
	     (setq mh-inbox "+inbox"))
	    ((not (mh-folder-name-p mh-inbox))
	     (setq mh-inbox (format "+%s" mh-inbox))))
      (setq mh-unseen-seq (mh-get-profile-field "Unseen-Sequence:"))
      (if mh-unseen-seq
	  (setq mh-unseen-seq (intern mh-unseen-seq))
	(setq mh-unseen-seq 'unseen))	;old MH default?
      (setq mh-previous-seq (mh-get-profile-field "Previous-Sequence:"))
      (if mh-previous-seq
	  (setq mh-previous-seq (intern mh-previous-seq)))
      (run-hooks 'mh-find-path-hook)))
  (and mh-auto-folder-collect
       (let ((mh-no-install t))		;only get folders if MH installed
	 (condition-case err
	     (mh-make-folder-list-background)
	   (file-error)))))		;so don't complain if not installed

(defun mh-file-command-p (file)
  "Return t if file FILE is the name of a executable regular file."
  (and (file-regular-p file) (file-executable-p file)))

(defun mh-find-progs ()
  "Find the directories for the installed MH/nmh binaries and config files.
Set the `mh-progs' and `mh-lib', and `mh-lib-progs' variables to the
directory names and set `mh-nmh-p' if we detect nmh instead of MH."
  (let ((path (or (mh-path-search exec-path "mhparam")
		 (mh-path-search '("/usr/local/nmh/bin" ; nmh default
				  "/usr/local/bin/mh/"
				  "/usr/local/mh/"
				  "/usr/bin/mh/" ;Ultrix 4.2
				  "/usr/new/mh/" ;Ultrix <4.2
				  "/usr/contrib/mh/bin/" ;BSDI
				  "/usr/pkg/bin/"	; NetBSD
				  "/usr/local/bin/"
				  )
				 "mhparam"))))
    (if (not path)
      (error "Unable to find the `mhparam' command"))
    (save-excursion
      (let ((tmp-buffer (get-buffer-create mh-temp-buffer)))
	(set-buffer tmp-buffer)
	(unwind-protect
	    (progn
	      (call-process (expand-file-name "mhparam" path)
			    nil '(t nil) nil "libdir" "etcdir")
	      (goto-char (point-min))
	      (if (search-forward-regexp "^libdir:\\s-\\(\\S-+\\)\\s-*$" nil t)
		  (setq mh-lib-progs (match-string 1)
			mh-lib mh-lib-progs
			mh-progs path))
	      (goto-char (point-min))
	      (if (search-forward-regexp "^etcdir:\\s-\\(\\S-+\\)\\s-*$" nil t)
		  (setq mh-lib (match-string 1)
			mh-nmh-p t)))
	  (kill-buffer tmp-buffer))))
    (unless (and mh-progs mh-lib mh-lib-progs)
	(error "Unable to determine paths from `mhparam' command"))))

(defun mh-path-search (path file &optional func-p)
  ;; Search PATH, a list of directory names, for FILE.
  ;; Returns the element of PATH that contains FILE, or nil if not found.
  (while (and path
	      (not (funcall (or func-p 'mh-file-command-p)
			    (expand-file-name file (car path)))))
    (setq path (cdr path)))
  (car path))

(defvar mh-no-install nil)		;do not run install-mh

(defun mh-install (profile error-val)
  ;; Called to do error recovery if we fail to read the profile file.
  ;; If possible, initialize the MH environment.
  (if (or (getenv "MH")
	  (file-exists-p profile)
	  mh-no-install)
      (signal (car error-val)
	      (list (format "Cannot read MH profile \"%s\"" profile)
		    (car (cdr (cdr error-val))))))
  ;; The "install-mh" command will output a short note which
  ;; mh-exec-cmd will display to the user.
  ;; The MH 5 version of install-mh might try prompt the user
  ;; for information, which would fail here.
  (mh-exec-cmd (expand-file-name "install-mh" mh-lib-progs) "-auto")
  ;; now try again to read the profile file
  (erase-buffer)
  (condition-case err
      (insert-file-contents profile)
    (file-error
     (signal (car err)			;re-signal with more specific msg
	     (list (format "Cannot read MH profile \"%s\"" profile)
		   (car (cdr (cdr err))))))))


(defun mh-set-folder-modified-p (flag)
  ;; Mark current folder as modified or unmodified according to FLAG.
  (set-buffer-modified-p flag))


(defun mh-find-seq (name) (assoc name mh-seq-list))

(defun mh-seq-to-msgs (seq)
  ;; Return a list of the messages in SEQUENCE.
  (mh-seq-msgs (mh-find-seq seq)))


(defun mh-add-msgs-to-seq (msgs seq &optional internal-flag)
  ;; Add MESSAGE(s) to the SEQUENCE.  If optional FLAG is non-nil, do not mark
  ;; the message in the scan listing or inform MH of the addition.
  (let ((entry (mh-find-seq seq)))
    (if (and msgs (atom msgs)) (setq msgs (list msgs)))
    (if (null entry)
	(setq mh-seq-list (cons (mh-make-seq seq msgs) mh-seq-list))
	(if msgs (setcdr entry (append msgs (mh-seq-msgs entry)))))
    (cond ((not internal-flag)
	   (mh-add-to-sequence seq msgs)
	   (mh-notate-seq seq mh-note-seq (1+ mh-cmd-note))))))

(defvar mh-folder-hist nil)

(defun mh-prompt-for-folder (prompt default can-create)
  ;; Prompt for a folder name with PROMPT.  Returns the folder's name as a
  ;; string.  DEFAULT is used if the folder exists and the user types return.
  ;; If the CAN-CREATE flag is t, then a non-existent folder is made.
  (if (null default)
      (setq default ""))
  (let* ((prompt (format "%s folder%s" prompt
			 (if (equal "" default)
			     "? "
			     (format " [%s]? " default))))
	 read-name folder-name)
    (if (null mh-folder-list)
	(mh-set-folder-list))
    (while (and (setq read-name (completing-read prompt mh-folder-list nil nil
						 "+" 'mh-folder-hist default))
		(equal read-name "")
		(equal default "")))
    (cond ((or (equal read-name "") (equal read-name "+"))
	   (setq read-name default))
	  ((not (mh-folder-name-p read-name))
	   (setq read-name (format "+%s" read-name))))
    (if (or (not read-name) (equal "" read-name))
        (error "No folder specified"))
    (setq folder-name read-name)
    (cond ((and (> (length folder-name) 0)
		(eq (aref folder-name (1- (length folder-name))) ?/))
	   (setq folder-name (substring folder-name 0 -1))))
    (let ((new-file-p (not (file-exists-p (mh-expand-file-name folder-name)))))
      (cond ((and new-file-p
		  (y-or-n-p
		   (format "Folder %s does not exist.  Create it? " folder-name)))
	     (message "Creating %s" folder-name)
	     (call-process "mkdir" nil nil nil (mh-expand-file-name folder-name))
	     (message "Creating %s...done" folder-name)
	     (setq mh-folder-list (cons (list read-name) mh-folder-list))
	     (run-hooks 'mh-folder-list-change-hook))
	    (new-file-p
	     (error "Folder %s is not created" folder-name))
	    ((not (file-directory-p (mh-expand-file-name folder-name)))
	     (error "\"%s\" is not a directory"
		    (mh-expand-file-name folder-name)))
	    ((and (null (assoc read-name mh-folder-list))
		  (null (assoc (concat read-name "/") mh-folder-list)))
	     (setq mh-folder-list (cons (list read-name) mh-folder-list))
	     (run-hooks 'mh-folder-list-change-hook))))
    folder-name))


(defvar mh-make-folder-list-process nil) ;The background process collecting the folder list.

(defvar mh-folder-list-temp nil)	;mh-folder-list as it is being built.

(defvar mh-folder-list-partial-line "")	;Start of last incomplete line from folder process.

(defun mh-set-folder-list ()
  ;; Sets mh-folder-list correctly.
  ;; A useful function for the command line or for when you need to
  ;; sync by hand.  Format is in a form suitable for completing read.
  (message "Collecting folder names...")
  (if (not mh-make-folder-list-process)
      (mh-make-folder-list-background))
  (while (eq (process-status mh-make-folder-list-process) 'run)
    (accept-process-output mh-make-folder-list-process))
  (setq mh-folder-list mh-folder-list-temp)
  (run-hooks 'mh-folder-list-change-hook)
  (setq mh-folder-list-temp nil)
  (delete-process mh-make-folder-list-process)
  (setq mh-make-folder-list-process nil)
  (message "Collecting folder names...done"))

(defun mh-make-folder-list-background ()
  ;; Start a background process to compute a list of the user's folders.
  ;; Call mh-set-folder-list to wait for the result.
  (cond
   ((not mh-make-folder-list-process)
    (unless mh-inbox
      (mh-find-path))
    (let ((process-connection-type nil))
      (setq mh-make-folder-list-process
	    (start-process "folders" nil (expand-file-name "folders" mh-progs)
			   "-fast"
			   (if mh-recursive-folders
			       "-recurse"
			     "-norecurse")))
      (set-process-filter mh-make-folder-list-process
			  'mh-make-folder-list-filter)
      (process-kill-without-query mh-make-folder-list-process)))))

(defun mh-make-folder-list-filter (process output)
  ;; parse output from "folders -fast"
  (let ((position 0)
	line-end
	new-folder
	(prevailing-match-data (match-data)))
    (unwind-protect
	;; make sure got complete line
	(while (setq line-end (string-match "\n" output position))
	  (setq new-folder (format "+%s%s"
				   mh-folder-list-partial-line
				   (substring output position line-end)))
	  (setq mh-folder-list-partial-line "")
	  ;; is new folder a subfolder of previous?
	  (if (and mh-folder-list-temp
		   (string-match
		    (regexp-quote
		     (concat (car (car mh-folder-list-temp)) "/"))
		    new-folder))
	      ;; append slash to parent folder for better completion
	      ;; (undone by mh-prompt-for-folder)
	      (setq mh-folder-list-temp
		    (cons
		     (list new-folder)
		     (cons
		      (list (concat (car (car mh-folder-list-temp)) "/"))
		      (cdr mh-folder-list-temp))))
	    (setq mh-folder-list-temp
		  (cons (list new-folder)
			mh-folder-list-temp)))
	  (setq position (1+ line-end)))
      (set-match-data prevailing-match-data))
    (setq mh-folder-list-partial-line (substring output position))))


(defun mh-folder-name-p (name)
  ;; Return non-NIL if NAME is possibly the name of a folder.
  ;; A name (a string or symbol) can be a folder name if it begins with "+".
  (if (symbolp name)
      (eq (aref (symbol-name name) 0) ?+)
    (and (> (length name) 0)
	 (eq (aref name 0) ?+))))


;;; Issue commands to MH.


(defun mh-exec-cmd (command &rest args)
  ;; Execute mh-command COMMAND with ARGS.
  ;; The side effects are what is desired.
  ;; Any output is assumed to be an error and is shown to the user.
  ;; The output is not read or parsed by mh-e.
  (save-excursion
    (set-buffer (get-buffer-create mh-temp-buffer))
    (erase-buffer)
    (apply 'call-process
	   (expand-file-name command mh-progs) nil t nil
	   (mh-list-to-string args))
    (if (> (buffer-size) 0)
	(save-window-excursion
	  (switch-to-buffer-other-window mh-temp-buffer)
	  (sit-for 5)))))


(defun mh-exec-cmd-error (env command &rest args)
  ;; In environment ENV, execute mh-command COMMAND with args ARGS.
  ;; ENV is nil or a string of space-separated "var=value" elements.
  ;; Signals an error if process does not complete successfully.
  (save-excursion
    (set-buffer (get-buffer-create mh-temp-buffer))
    (erase-buffer)
    (let ((status
	   (if env
	       ;; the shell hacks necessary here shows just how broken Unix is
	       (apply 'call-process "/bin/sh" nil t nil "-c"
		      (format "%s %s ${1+\"$@\"}"
			      env
			      (expand-file-name command mh-progs))
		      command
		      (mh-list-to-string args))
	       (apply 'call-process
		      (expand-file-name command mh-progs) nil t nil
		      (mh-list-to-string args)))))
      (mh-handle-process-error command status))))


(defun mh-exec-cmd-daemon (command &rest args)
  ;; Execute MH command COMMAND with ARGS in the background.
  ;; Any output from command is displayed in an asynchronous pop-up window.
  (save-excursion
    (set-buffer (get-buffer-create mh-temp-buffer))
    (erase-buffer))
  (let* ((process-connection-type nil)
	 (process (apply 'start-process
			 command nil
			 (expand-file-name command mh-progs)
			 (mh-list-to-string args))))
    (set-process-filter process 'mh-process-daemon)))

(defun mh-process-daemon (process output)
  ;; Process daemon that puts output into a temporary buffer.
  (set-buffer (get-buffer-create mh-temp-buffer))
  (insert-before-markers output)
  (display-buffer mh-temp-buffer))


(defun mh-exec-cmd-quiet (raise-error command &rest args)
  ;; Args are RAISE-ERROR, COMMANDS, ARGS....
  ;; Execute MH command COMMAND with ARGS.  ARGS is a list of strings.
  ;; Return at start of mh-temp buffer, where output can be parsed and used.
  ;; Returns value of call-process, which is 0 for success,
  ;; unless RAISE-ERROR is non-nil, in which case an error is signaled
  ;; if call-process returns non-0.
  (set-buffer (get-buffer-create mh-temp-buffer))
  (erase-buffer)
  (let ((value
	 (apply 'call-process
		(expand-file-name command mh-progs) nil t nil
		args)))
    (goto-char (point-min))
    (if raise-error
	(mh-handle-process-error command value)
      value)))


(defun mh-exec-cmd-output (command display &rest args)
  ;; Execute MH command COMMAND with DISPLAY flag and ARGS.
  ;; Put the output into buffer after point.  Set mark after inserted text.
  ;; Output is expected to be shown to user, not parsed by mh-e.
  (push-mark (point) t)
  (apply 'call-process
	 (expand-file-name command mh-progs) nil t display
	 (mh-list-to-string args))
  (exchange-point-and-mark))


(defun mh-exec-lib-cmd-output (command &rest args)
  ;; Execute MH library command COMMAND with ARGS.
  ;; Put the output into buffer after point.  Set mark after inserted text.
  (apply 'mh-exec-cmd-output (expand-file-name command mh-lib-progs) nil args))


(defun mh-handle-process-error (command status)
  ;; Raise error if COMMAND returned non-0 STATUS, otherwise return STATUS.
  ;; STATUS is return value from call-process.
  ;; Program output is in current buffer.
  ;; If output is too long to include in error message, display the buffer.
  (cond ((eq status 0)			;success
	 status)
	((stringp status)		;kill string
	 (error "%s: %s" command status))
	(t				;exit code
	 (cond
	  ((= (buffer-size) 0)		;program produced no error message
	   (error "%s: exit code %d" command status))
	  (t
	   ;; will error message fit on one line?
	   (goto-line 2)
	   (if (and (< (buffer-size) (frame-width))
		    (eobp))
	       (error "%s"
		      (buffer-substring 1 (progn (goto-char 1)
						 (end-of-line)
						 (point))))
	     (display-buffer (current-buffer))
	     (error "%s failed with status %d.  See error message in other window"
		    command status)))))))


(defun mh-expand-file-name (filename &optional default)
  ;; Just like `expand-file-name', but also handles MH folder names.
  ;; Assumes that any filename that starts with '+' is a folder name.
   (if (mh-folder-name-p filename)
       (expand-file-name (substring filename 1) mh-user-path)
     (expand-file-name filename default)))


(defun mh-list-to-string (l)
  ;; Flattens the list L and makes every element of the new list into a string.
  (nreverse (mh-list-to-string-1 l)))

(defun mh-list-to-string-1 (l)
  (let ((new-list nil))
    (while l
      (cond ((null (car l)))
	    ((symbolp (car l))
	     (setq new-list (cons (symbol-name (car l)) new-list)))
	    ((numberp (car l))
	     (setq new-list (cons (int-to-string (car l)) new-list)))
	    ((equal (car l) ""))
	    ((stringp (car l)) (setq new-list (cons (car l) new-list)))
	    ((listp (car l))
	     (setq new-list (nconc (mh-list-to-string-1 (car l))
				   new-list)))
	    (t (error "Bad element in mh-list-to-string: %s" (car l))))
      (setq l (cdr l)))
    new-list))

(provide 'mh-utils)

;;; mh-utils.el ends here