summaryrefslogtreecommitdiff
path: root/lisp/=superyank.el
blob: f76e6c7c3bf9a1fb9da7c0a63368a8daba0602d4 (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
;;; superyank.el --- smart message-yanking code for GNUS

;; Copyright (C) 1992 Free Software Foundation, Inc.

;; Author: Barry A. Warsaw <warsaw@cme.nist.gov>
;; Version: 1.1
;; Adapted-By: ESR
;; Keywords: news

;; 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, 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Commentary:

;; Inserts the message being replied to with various user controlled
;; citation styles.
;;

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY.  No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all,
;; unless he says so in writing.  Refer to the GNU Emacs General Public
;; License for full details.

;; Everyone is granted permission to copy, modify and redistribute
;; this file, but only under the conditions described in the
;; GNU Emacs General Public License.   A copy of this license is
;; supposed to have been given to you along with GNU Emacs so you
;; can know your rights and responsibilities.  It should be in a
;; file named COPYING.  Among other things, the copyright notice
;; and this notice must be preserved on all copies.

;; NAME: Barry A. Warsaw                USMAIL: National Institute of Standards
;; TELE: (301) 975-3460                         and Technology (formerly NBS)
;; UUCP: {...}!uunet!cme-durer!warsaw           Rm. B-124, Bldg. 220
;; ARPA: warsaw@cme.nist.gov                    Gaithersburg, MD 20899

;; Modification history:
;;
;; modified: 14-Jun-1989 baw (better keymap set procedure, rewrite-headers)
;; modified: 12-Jun-1989 baw (added defvar for sy-use-only-preference-p)
;; modified:  6-Jun-1989 baw (better sy-rewrite-headers, no kill/yank)
;; modified:  5-Jun-1989 baw (requires rnewspost.el)
;; modified:  1-Jun-1989 baw (persistent attribution, sy-open-line)
;; modified: 31-May-1989 baw (fixed some gnus problems, id'd another)
;; modified: 22-May-1989 baw (documentation)
;; modified:  8-May-1989 baw (auto filling of regions)
;; modified:  1-May-1989 baw (documentation)
;; modified: 27-Apr-1989 baw (new preference scheme)
;; modified: 24-Apr-1989 baw (remove gnus headers, attrib scheme, cite lines)
;; modified: 19-Apr-1989 baw (cite key, fill p, yank region, naming scheme)
;; modified: 12-Apr-1989 baw (incorp other mail yank features seen on net)
;; created : 16-Feb-1989 baw (mod vanilla fn indent-rigidly mail-yank-original)

;; Though I wrote this package basically from scratch, as an Emacs Lisp
;; learning exercise, it was inspired by postings of similar packages to
;; the gnu.emacs newsgroup over the past month or so.
;;
;; Here's a brief history of how this package developed:
;;
;; I as well as others on the net were pretty unhappy about the way emacs
;; cited replies with the tab or 4 spaces.  It looked ugly and made it hard
;; to distinguish between original and cited lines.  I hacked on the function
;; yank-original to at least give the user the ability to define the citation
;; character.  I posted this simple hack, and others did as well.  The main
;; difference between mine and others was that a space was put after the
;; citation string on on new citations, but not after previously cited lines:
;;
;; >> John wrote this originally
;; > Jane replied to that
;;
;; Then Martin Neitzel posted some code that he developed, derived in part
;; from code that Ashwin Ram posted previous to that.  In Martin's
;; posting, he introduced a new, and (IMHO) superior, citation style,
;; eliminating nested citations.  Yes, I wanted to join the Small-But-
;; Growing-Help-Stamp-Out-Nested-Citation-Movement! You should too.
;;
;; But Martin's code simply asks the user for the citation string (here
;; after called the `attribution' string), and I got to thinking, it wouldn't
;; be that difficult to automate that part.  So I started hacking this out.
;; It proved to be not as simple as I first thought.  But anyway here it
;; is.  See the wish list below for future plans (if I have time).
;;
;; Type "C-h f mail-yank-original" after this package is loaded to get a
;; description of what it does and the variables that control it.
;;
;; ======================================================================
;;
;; Changes wish list
;;
;; 1) C-x C-s yanks a region from the RMAIL buffer instead of the
;;    whole buffer
;;
;; 2) reparse nested citations to try to recast as non-nested citations
;;    perhaps by checking the References: line
;;

;;; Code:

;; ======================================================================
;;
;; require and provide features
;;
(require 'sendmail)
;;
;; ======================================================================
;;
;; don't need rnewspost.el to rewrite the header.  This only works
;; with diffs to rnewspost.el that I posted with the original
;; superyank code.
;;
(setq news-reply-header-hook nil)

;; **********************************************************************
;; start of user defined variables
;; **********************************************************************
;;
;; this section defines variables that control the operation of
;; super-mail-yank.  Most of these are described in the comment section
;; as well as the DOCSTRING.
;;

;;
;; ----------------------------------------------------------------------
;;
;; this variable holds the default author's name for citations
;;
(defvar sy-default-attribution "Anon"
  "String that describes attribution to unknown person. This string
should not contain the citation string.")

;;
;; ----------------------------------------------------------------------
;;
;; string used as an end delimiter for both nested and non-nested citations
;;
(defvar sy-citation-string ">"
  "String to use as an end-delimiter for citations.  This string is
used in both nested and non-nested citations.  For best results, use a
single character with no trailing space.  Most commonly used string
is: \">\.")

;;
;; ----------------------------------------------------------------------
;;
;; variable controlling citation type, nested or non-nested
;;
(defvar sy-nested-citation-p nil
  "Non-nil uses nested citations, nil uses non-nested citations.
Nested citations are of the style:

I wrote this
> He wrote this
>> She replied to something he wrote

Non-nested citations are of the style:

I wrote this
John> He wrote this
Jane> She originally wrote this")


;;
;; ----------------------------------------------------------------------
;;
;; regular expression that matches existing citations
;;
(defvar sy-cite-regexp "[a-zA-Z0-9]*>"
  "Regular expression that describes how an already cited line in an
article begins.  The regexp is only used at the beginning of a line,
so it doesn't need to begin with a '^'.")

;;
;; ----------------------------------------------------------------------
;;
;; regular expression that delimits names from titles in the field that
;; looks like:   (John X. Doe -- Computer Hacker Extraordinaire)
;;
(defvar sy-titlecue-regexp "\\s +-+\\s +"
  
  "Regular expression that delineates names from titles in the name
field.  Often, people will set up their name field to look like this:

(John Xavier Doe -- Computer Hacker Extraordinaire)

Set to nil to treat entire field as a name.")

;;
;; ----------------------------------------------------------------------
;;
;;
(defvar sy-preferred-attribution 2
  
  "This is an integer indicating what the user's preference is in
attribution style, based on the following key:

0: email address name is preferred
1: initials are preferred
2: first name is preferred
3: last name is preferred

The value of this variable may also be greater than 3, which would
allow you to prefer the 2nd through nth - 1 name.  If the preferred
attribution is nil or the empty string, then the secondary preferrence
will be the first name.  After that, the entire name alist is search
until a non-empty, non-nil name is found.  If no such name is found,
then the user is either queried or the default attribution string is
used depending on the value of sy-confirm-always-p.

Examples:

assume the from: line looks like this:

from: doe@computer.some.where.com (John Xavier Doe)

The following preferences would return these strings:

0: \"doe\"
1: \"JXD\"
2: \"John\"
3: \"Doe\"
4: \"Xavier\"

anything else would return \"John\".")

;;
;; ----------------------------------------------------------------------
;;
(defvar sy-confirm-always-p t
  "If t, always confirm attribution string before inserting into
buffer.")


;;
;; ----------------------------------------------------------------------
;;
;; informative header hook
;;
(defvar sy-rewrite-header-hook 'sy-header-on-said
  "Hook for inserting informative header at the top of the yanked
message. Set to nil for no header.  Here is a list of predefined
header styles; you can use these as a model to write you own:

sy-header-on-said [default]:   On 14-Jun-1989 GMT,
                               John Xavier Doe said:

sy-header-inarticle-writes:    In article <123456789> John Xavier Doe writes:

sy-header-regarding-writes:    Regarding RE: superyank; John Xavier Doe adds:

sy-header-verbose:             On 14-Jun-1989 GMT, John Xavier Doe
                               from the organization Great Company
                               has this to say about article <123456789>
                               in newsgroups misc.misc
                               concerning RE: superyank
                               referring to previous articles <987654321>

You can use the following variables as information strings in your header:

sy-reply-yank-date:         the date field              [ex: 14-Jun-1989 GMT]
sy-reply-yank-from:         the from field              [ex: John Xavier Doe]
sy-reply-yank-message-id:   the message id              [ex: <123456789>]
sy-reply-yank-subject:      the subject line            [ex: RE: superyank]
sy-reply-yank-newsgroup:    the newsgroup name for GNUS [ex: misc.misc]
sy-reply-yank-references:   the article references      [ex: <987654321>]
sy-reply-yank-organization: the author's organization   [ex: Great Company]

If a field can't be found, because it doesn't exist or is not being
shown, perhaps because of toggle-headers, the corresponding field
variable will contain the string \"mumble mumble\".")

;;
;; ----------------------------------------------------------------------
;;
;; non-nil means downcase the author's name string
;;
(defvar sy-downcase-p nil
  "Non-nil means downcase the author's name string.")

;;
;; ----------------------------------------------------------------------
;;
;; controls removal of leading white spaces
;;
(defvar sy-left-justify-p nil
  "If non-nil, delete all leading white space before citing.")

;;
;; ----------------------------------------------------------------------
;;
;; controls auto filling of region
;;
(defvar sy-auto-fill-region-p nil
  "If non-nil, automatically fill each paragraph that is cited.  If
nil, do not auto fill each paragraph.")


;;
;; ----------------------------------------------------------------------
;;
;; controls use of preferred attribution only, or use of attribution search
;; scheme if the preferred attrib can't be found.
;;
(defvar sy-use-only-preference-p nil
  
  "If non-nil, then only the preferred attribution string will be
used.  If the preferred attribution string can not be found, then the
sy-default-attribution will be used.  If nil, and the preferred
attribution string is not found, then some secondary scheme will be
employed to find a suitable attribution string.")

;; **********************************************************************
;; end of user defined variables
;; **********************************************************************

;;
;; ----------------------------------------------------------------------
;;
;; The new citation style means we can clean out other headers in addition
;; to those previously cleaned out.  Anyway, we create our own headers.
;; Also, we want to clean out any headers that gnus puts in.  Add to this
;; for other mail or news readers you may be using.
;;
(setq mail-yank-ignored-headers "^via:\\|^origin:\\|^status:\\|^re\\(mail\\|ceiv\\)ed\\|^[a-z-]*message-id:\\|^\\(summary-\\)?line[s]?:\\|^cc:\\|^subject:\\|^\\(\\(in-\\)?reply-\\)?to:\\|^\\(\\(return\\|reply\\)-\\)?path:\\|^\\(posted-\\)?date:\\|^\\(mail-\\)?from:\\|^newsgroup[s]?:\\|^organization:\\|^keywords:\\|^distribution:\\|^references:")

;;
;; ----------------------------------------------------------------------
;;
;; global variables, not user accessable
;;
(setq sy-persist-attribution (concat sy-default-attribution "> "))
(setq sy-reply-yank-date "")
(setq sy-reply-yank-from "")
(setq sy-reply-yank-message-id "")
(setq sy-reply-yank-subject "")
(setq sy-reply-yank-newsgroups "")
(setq sy-reply-yank-references "")
(setq sy-reply-yank-organization "")

;;
;; ======================================================================
;;
;; This section contains primitive functions used in the schemes.  They
;; extract name fields from various parts of the "from:" field based on
;; the control variables described above.
;;
;; Some will use recursion to pick out the correct namefield in the namestring
;; or the list of initials.  These functions all scan a string that contains
;; the name, ie:  "John Xavier Doe".  There is no limit on the number of names
;; in the string.  Also note that all white spaces are basically ignored and
;; are stripped from the returned strings, and titles are ignored if 
;; sy-titlecue-regexp is set to non-nil.
;;
;; Others will use methods to try to extract the name from the email
;; address of the originator.  The types of addresses readable are
;; described above.

;;
;; ----------------------------------------------------------------------
;;
;; try to extract the name from an email address of the form
;; name%[stuff]
;;
;; Unlike the get-name functions above, these functions operate on the
;; buffer instead of a supplied name-string.
;;
(defun sy-%-style-address ()
  (beginning-of-line)
  (buffer-substring
   (progn (re-search-forward "%" (point-max) t)
	  (if (not (bolp)) (forward-char -1))
	  (point))
   (progn (re-search-backward "^\\|[^a-zA-Z0-9]")
	  (point))))

;;
;; ----------------------------------------------------------------------
;;
;; try to extract names from addresses with the form:
;; [stuff]name@[stuff]
;;
(defun sy-@-style-address ()
  (beginning-of-line)
  (buffer-substring
   (progn (re-search-forward "@" (point-max) t)
	  (if (not (bolp)) (forward-char -1))
	  (point))
   (progn (re-search-backward "^\\|[^a-zA-Z0-0]")
	  (if (not (bolp)) (forward-char 1))
	  (point))))

;;
;; ----------------------------------------------------------------------
;;
;; try to extract the name from addresses with the form:
;; [stuff]![stuff]...!name[stuff]
;;
(defun sy-!-style-address ()
  (beginning-of-line)
  (buffer-substring
   (progn (while (re-search-forward "!" (point-max) t))
	  (point))
   (progn (re-search-forward "[^a-zA-Z0-9]\\|$")
	  (if (not (eolp)) (forward-char -1))
	  (point))))

;;
;; ----------------------------------------------------------------------
;;
;; using the different email name schemes, try each one until you get a
;; non-nil entry
;;
(defun sy-get-emailname ()
  (let ((en1 (sy-%-style-address))
	(en2 (sy-@-style-address))
	(en3 (sy-!-style-address)))
    (cond
     ((not (string-equal en1 "")) en1)
     ((not (string-equal en2 "")) en2)
     ((not (string-equal en3 "")) en3)
     (t ""))))

;;
;; ----------------------------------------------------------------------
;;
;; returns the "car" of the namestring, really the first namefield
;;
;; (sy-string-car "John Xavier Doe")
;; => "John"
;;
(defun sy-string-car (namestring)
  (substring namestring
	     (progn (string-match "\\s *" namestring) (match-end 0))
	     (progn (string-match "\\s *\\S +" namestring) (match-end 0))))

;;
;; ----------------------------------------------------------------------
;;
;; returns the "cdr" of the namestring, really the whole string from
;; after the first name field to the end of the string.
;;
;; (sy-string-cdr "John Xavier Doe")
;; => "Xavier Doe"
;;
(defun sy-string-cdr (namestring)
  (substring namestring
	     (progn (string-match "\\s *\\S +\\s *" namestring)
		    (match-end 0))))

;;
;; ----------------------------------------------------------------------
;;
;; convert a namestring to a list of namefields
;;
;; (sy-namestring-to-list "John Xavier Doe")
;; => ("John" "Xavier" "Doe")
;;
(defun sy-namestring-to-list (namestring)
  (if (not (string-match namestring ""))
      (append (list (sy-string-car namestring))
	      (sy-namestring-to-list (sy-string-cdr namestring)))))

;;
;; ----------------------------------------------------------------------
;;
;; strip the initials from each item in the list and return a string
;; that is the concatenation of the initials
;;
(defun sy-strip-initials (raw-nlist)
  (if (not raw-nlist)
      nil
    (concat (substring (car raw-nlist) 0 1)
	    (sy-strip-initials (cdr raw-nlist)))))


;;
;; ----------------------------------------------------------------------
;;
;; using the namestring, build a list which is in the following order
;;
;; (email, initials, firstname, lastname, name1, name2, name3 ... nameN-1)
;;
(defun sy-build-ordered-namelist (namestring)
  (let* ((raw-nlist (sy-namestring-to-list namestring))
	 (initials (sy-strip-initials raw-nlist))
	 (firstname (car raw-nlist))
	 (revnames (reverse (cdr raw-nlist)))
	 (lastname (car revnames))
	 (midnames (reverse (cdr revnames)))
	 (emailnames (sy-get-emailname)))
    (append (list emailnames)
	    (list initials)
	    (list firstname)
	    (list lastname)
	    midnames)))

;;
;; ----------------------------------------------------------------------
;;
;; Query the user for the attribution string.  Supply sy-default-attribution
;; as the default choice.
;;
(defun sy-query-for-attribution ()
  (concat 
   (let* ((prompt (concat "Enter attribution string: (default "
			  sy-default-attribution
			  ") "))
	  (query (read-input prompt))
	  (attribution (if (string-equal query "")
			   sy-default-attribution
			 query)))
     (if sy-downcase-p
	 (downcase attribution)
       attribution))
   sy-citation-string))


;;
;; ----------------------------------------------------------------------
;;
;; parse the current line for the namestring
;;
(defun sy-get-namestring ()
  (save-restriction
    (beginning-of-line)
    (if (re-search-forward "(.*)" (point-max) t)
	(let ((start (progn
		       (beginning-of-line)
		       (re-search-forward "\\((\\s *\\)\\|$" (point-max) t)
		       (point)))
	      (end (progn
		     (re-search-forward
		      (concat "\\(\\s *\\()\\|" sy-titlecue-regexp "\\)\\)\\|$")
		      (point-max) t)
		     (point))))
	  (narrow-to-region start end)
	  (let ((start (progn
			 (beginning-of-line)
			 (point)))
		(end (progn
		       (end-of-line)
		       (re-search-backward
			(concat "\\s *\\()\\|" sy-titlecue-regexp "\\)$")
			(point-min) t)
		       (point))))
	    (buffer-substring start end)))
      (let ((start (progn
		     (beginning-of-line)
		     (re-search-forward "^\"*")
		     (point)))
	    (end (progn
		   (re-search-forward "\\(\\s *[a-zA-Z0-9\\.]+\\)*"
				      (point-max) t)
		   (point))))
	(buffer-substring start end)))))


;;
;; ----------------------------------------------------------------------
;;
;; scan the nlist and return the integer pointing to the first legal
;; non-empty namestring.  Returns the integer pointing to the index
;; in the nlist of the preferred namestring, or nil if no legal
;; non-empty namestring could be found.
;;
(defun sy-return-preference-n (nlist)
  (let ((p sy-preferred-attribution)
	(exception nil))
    ;;
    ;; check to be sure the index is not out-of-bounds
    ;;
    (cond
     ((< p 0) (setq p 2) (setq exception t))
     ((not (nth p nlist)) (setq p 2) (setq exception t)))
    ;;
    ;; check to be sure that the explicit preference is not empty
    ;;
    (if (string-equal (nth p nlist) "")
	(progn (setq p 0)
	       (setq exception t)))
    ;;
    ;; find the first non-empty namestring
    ;;
    (while (and (nth p nlist)
		(string-equal (nth p nlist) ""))
      (setq exception t)
      (setq p (+ p 1)))
    ;;
    ;; return the preference index if non-nil, otherwise nil
    ;;
    (if (or (and exception sy-use-only-preference-p)
	    (not (nth p nlist)))
	nil
      p)))

;;
;;
;; ----------------------------------------------------------------------
;;
;; rebuild the nlist into an alist for completing-read.  Use as a guide
;; the index of the preferred name field.  Get the actual preferred
;; name field base on other factors (see above).  If no actual preferred
;; name field is found, then query the user for the attribution string.
;;
;; also note that the nlist is guaranteed to be non-empty.  At the very
;; least it will consist of 4 empty strings ("" "" "" "")
;; 
(defun sy-nlist-to-alist (nlist)
  (let ((preference (sy-return-preference-n nlist))
	alist
	(n 0))
    ;;
    ;; check to be sure preference is not nil
    ;;
    (if (not preference)
	(setq alist (list (cons (sy-query-for-attribution) nil)))
      ;;
      ;; preference is non-nil
      ;;
      (setq alist (list (cons (nth preference nlist) nil)))
      (while (nth n nlist)
	(if (= n preference) nil
	  (setq alist (append alist (list (cons (nth n nlist) nil)))))
	(setq n (+ n 1))))
    alist))



;;
;; ----------------------------------------------------------------------
;;
;; confirm if desired after the alist has been built
;;
(defun sy-get-attribution (alist)
  (concat
   ;;
   ;; check to see if nested citations are to be used
   ;;
   (if sy-nested-citation-p
       ""
     ;;
     ;; check to see if confirmation is needed
     ;; if not, just return the preference (first element in alist)
     ;;
     (if (not sy-confirm-always-p)
	 (car (car alist))
       ;;
       ;; confirmation is requested so build the prompt, confirm
       ;; and return the chosen string
       ;;
       (let* (ignore
	      (prompt (concat "Complete attribution string: (default "
			      (car (car alist))
			      ") "))
	      ;;
	      ;; set up the local completion keymap
	      ;;
	      (minibuffer-local-must-match-map
	       (let ((map (make-sparse-keymap)))
		 (define-key map "?"    'minibuffer-completion-help)
		 (define-key map " "    'minibuffer-complete-word)
		 (define-key map "\t"   'minibuffer-complete)
		 (define-key map "\00A" 'exit-minibuffer)
		 (define-key map "\00D" 'exit-minibuffer)
		 (define-key map "\007"
		   '(lambda ()
		      (interactive)
		      (beep)
		      (exit-minibuffer)))
		 map))
	      ;;
	      ;; read the completion
	      ;;
	      (attribution (completing-read prompt alist))
	      ;;
	      ;; check attribution string for emptyness
	      ;;
	      (choice (if (or (not attribution)
			      (string-equal attribution ""))
			  (car (car alist))
			attribution)))
	 
	 (if sy-downcase-p
	     (downcase choice)
	   choice))))
   sy-citation-string))


;;
;; ----------------------------------------------------------------------
;;
;; this function will scan the current rmail buffer, narrowing it to the
;; from: line, then using this, it will try to decipher some names from
;; that line.  It will then build the name alist and try to confirm
;; its choice of attribution strings.  It returns the chosen attribution
;; string.
;;
(defun sy-scan-rmail-for-names (rmailbuffer)
  (save-excursion
    (let ((case-fold-search t)
	  alist
	  attribution)
      (switch-to-buffer rmailbuffer)
      (goto-char (point-min))
      ;;
      ;; be sure there is a from: line
      ;;
      (if (not (re-search-forward "^from:\\s *" (point-max) t))
	  (setq attribution (sy-query-for-attribution))
	;;
	;; if there is a from: line, then scan the narrow the buffer,
	;; grab the namestring, and build the alist, then using this
	;; get the attribution string.
	;;
	(save-restriction
	  (narrow-to-region (point)
			    (progn (end-of-line) (point)))
	  (let* ((namestring (sy-get-namestring))
		 (nlist (sy-build-ordered-namelist namestring)))
	    (setq alist (sy-nlist-to-alist nlist))))
	;;
	;; we've built the alist, now confirm the attribution choice
	;; if appropriate
	;;
	(setq attribution (sy-get-attribution alist)))
      attribution)))


;;
;; ======================================================================
;;
;; the following function insert of citations, writing of headers, filling
;; paragraphs and general higher level operations
;;

;;
;; ----------------------------------------------------------------------
;;
;; insert a nested citation
;;
(defun sy-insert-citation (start end cite-string)
  (save-excursion
    (goto-char end)
    (setq end (point-marker))
    (goto-char start)
    (or (bolp)
	(forward-line 1))
    
    (let ((fill-prefix (concat cite-string " "))
	  (fstart (point))
	  (fend (point)))
      
      (while (< (point) end)
	;;
	;; remove leading tabs if desired
	;;
	(if sy-left-justify-p
	    (delete-region (point)
			   (progn (skip-chars-forward " \t") (point))))
	;;
	;; check to see if the current line should be cited
	;;
	(if (or (eolp)
		(looking-at sy-cite-regexp))
	    ;;
	    ;; do not cite this line unless nested-citations are to be 
	    ;; used
	    ;;
	    (progn
	      (or (eolp)
		  (if sy-nested-citation-p
		      (insert cite-string)))
	      
	      ;; set fill start and end points
	      ;;
	      (or (= fstart  fend)
		  (not sy-auto-fill-region-p)
		  (progn (goto-char fend)
			 (or (not (eolp))
			     (setq fend (+ fend 1)))
			 (fill-region-as-paragraph fstart fend)))
	      (setq fstart (point))
	      (setq fend (point)))
	  
	  ;; else
	  ;;
	  (insert fill-prefix)
	  (end-of-line)
	  (setq fend (point)))
	
	(forward-line 1)))
    (move-marker end nil)))

;;
;; ----------------------------------------------------------------------
;;
;; yank a particular field into a holding variable
;;
(defun sy-yank-fields (start)
  (save-excursion
    (goto-char start)
    (setq sy-reply-yank-date         (mail-fetch-field "date")
	  sy-reply-yank-from         (mail-fetch-field "from")
	  sy-reply-yank-subject      (mail-fetch-field "subject")
	  sy-reply-yank-newsgroups   (mail-fetch-field "newsgroups")
	  sy-reply-yank-references   (mail-fetch-field "references")
	  sy-reply-yank-message-id   (mail-fetch-field "message-id")
	  sy-reply-yank-organization (mail-fetch-field "organization"))
    (or sy-reply-yank-date
	(setq sy-reply-yank-date "mumble mumble"))
    (or sy-reply-yank-from
	(setq sy-reply-yank-from "mumble mumble"))
    (or sy-reply-yank-subject
	(setq sy-reply-yank-subject "mumble mumble"))
    (or sy-reply-yank-newsgroups
	(setq sy-reply-yank-newsgroups "mumble mumble"))
    (or sy-reply-yank-references
	(setq sy-reply-yank-references "mumble mumble"))
    (or sy-reply-yank-message-id
	(setq sy-reply-yank-message-id "mumble mumble"))
    (or sy-reply-yank-organization
	(setq sy-reply-yank-organization "mumble mumble"))))

;;
;; ----------------------------------------------------------------------
;;
;; rewrite the header to be more conversational
;;
(defun sy-rewrite-headers (start)
  (goto-char start)
  (run-hooks 'sy-rewrite-header-hook))

;;
;; ----------------------------------------------------------------------
;;
;; some different styles of headers
;;
(defun sy-header-on-said ()
  (insert-string "\nOn " sy-reply-yank-date ",\n"
		 sy-reply-yank-from " said:\n"))

(defun sy-header-inarticle-writes ()
  (insert-string "\nIn article " sy-reply-yank-message-id
		 " " sy-reply-yank-from " writes:\n"))

(defun sy-header-regarding-writes ()
  (insert-string "\nRegarding " sy-reply-yank-subject
		 "; " sy-reply-yank-from " adds:\n"))

(defun sy-header-verbose ()
  (insert-string "\nOn " sy-reply-yank-date ",\n"
		 sy-reply-yank-from "\nfrom the organization "
		 sy-reply-yank-organization "\nhad this to say about article "
		 sy-reply-yank-message-id "\nin newsgroups "
		 sy-reply-yank-newsgroups "\nconcerning "
		 sy-reply-yank-subject "\nreferring to previous articles "
		 sy-reply-yank-references "\n"))

;;
;; ----------------------------------------------------------------------
;;
;; yank the original article in and attribute
;;
(defun sy-yank-original (arg)
  
  "Insert the message being replied to, if any (in rmail/gnus). Puts
point before the text and mark after. Calls generalized citation
function sy-insert-citation to cite all allowable lines."
  
  (interactive "P")
  (if mail-reply-buffer
      (let* ((sy-confirm-always-p (if (consp arg)
				      t
				    sy-confirm-always-p))
	     (attribution (sy-scan-rmail-for-names mail-reply-buffer))
	     (top (point))
	     (start (point))
	     (end (progn (delete-windows-on mail-reply-buffer)
			 (insert-buffer mail-reply-buffer)
			 (mark))))
	
	(sy-yank-fields start)
	(sy-rewrite-headers start)
	(setq start (point))
	(mail-yank-clear-headers top (mark))
	(setq sy-persist-attribution (concat attribution " "))
	(sy-insert-citation start end attribution))
    
    (goto-char top)
    (exchange-point-and-mark)))


;;
;; ----------------------------------------------------------------------
;;
;; this is here for compatibility with existing mail/news yankers
;; overloads the default mail-yank-original
;;
(defun mail-yank-original (arg)
  
  "Yank original message buffer into the reply buffer, citing as per
user preferences.  Numeric Argument forces confirmation.

Here is a description of the superyank.el package, what it does and
what variables control its operation.  This was written by Barry
Warsaw (warsaw@cme.nist.gov, {...}!uunet!cme-durer!warsaw).

A 'Citation' is the acknowledgement of the original author of a mail
message.  There are two general forms of citation. In 'nested
citations', indication is made that the cited line was written by
someone *other* that the current message author (or by that author at
an earlier time).  No indication is made as to the identity of the
original author.  Thus, a nested citation after multiple replies would
look like this (this is after my reply to a previous message):

>>John originally wrote this
>>and this as well
> Jane said that John didn't know
> what he was talking about
And that's what I think as well.

In non-nested citations, you won't see multiple \">\" characters at
the beginning of the line.  Non-nested citations will insert an
informative string at the beginning of a cited line, attributing that
line to an author.  The same message described above might look like
this if non-nested citations were used:

John> John originally wrote this
John> and this as well
Jane> Jane said that John didn't know
Jane> what he was talking about
And that's what I think as well.

Notice that my inclusion of Jane's inclusion of John's original
message did not result in a cited line of the form: Jane>John>.  Thus
no nested citations.  The style of citation is controlled by the
variable `sy-nested-citation-p'.  Nil uses non-nested citations and
non-nil uses old style, nested citations.

The variable `sy-citation-string' is the string to use as a marker for
a citation, either nested or non-nested.  For best results, this
string should be a single character with no trailing space and is
typically the character \">\".  In non-nested citations this string is
appended to the attribution string (author's name), along with a
trailing space.  In nested citations, a trailing space is only added
to a first level citation.

Another important variable is `sy-cite-regexp' which describes strings
that indicate a previously cited line.  This regular expression is
always used at the beginning of a line so it doesn't need to begin
with a \"^\" character.  Change this variable if you change
`sy-citation-string'.

The following section only applies to non-nested citations.

This package has a fair amount of intellegence related to deciphering
the author's name based on information provided by the original
message buffer.  In normal operation, the program will pick out the
author's first and last names, initials, terminal email address and
any other names it can find.  It will then pick an attribution string
from this list based on a user defined preference and it will ask for
confirmation if the user specifies.  This package gathers its
information from the `From:' line of the original message buffer.  It
recognizes From: lines with the following forms:

From: John Xavier Doe <doe@speedy.computer.com>
From: \"John Xavier Doe\" <doe@speedy.computer.com>
From: doe@speedy.computer.com (John Xavier Doe)
From: computer!speedy!doe (John Xavier Doe)
From: computer!speedy!doe (John Xavier Doe)
From: doe%speedy@computer.com (John Xavier Doe)

In this case, if confirmation is requested, the following strings will
be made available for completion and confirmation:

\"John\"
\"Xavier\"
\"Doe\"
\"JXD\"
\"doe\"

Note that completion is case sensitive.  If there was a problem
picking out a From: line, or any other problem getting even a single
name, then the user will be queried for an attribution string.  The
default attribution string is set in the variable
`sy-default-attribution'.

Sometimes people set their name fields so that it also includes a
title of the form:

From: doe@speedy.computer.com (John Doe -- Hacker Extraordinaire)

To avoid the inclusion of the string \"-- Hacker Extraordinaire\" in
the name list, the variable `sy-titlecue-regexp' is provided.  Its
default setting will still properly recognize names of the form:

From: xdoe@speedy.computer.com (John Xavier-Doe -- Crazed Hacker)

The variable `sy-preferred-attribution' contains an integer that
indicates which name field the user prefers to use as the attribution
string, based on the following key:

0: email address name is preferred
1: initials are preferred
2: first name is preferred
3: last name is preferred

The value can be greater than 3, in which case, you would be
preferring the 2nd throught nth -1 name.  In any case, if the
preferred name can't be found, then one of two actions will be taken
depending on the value of the variable `sy-use-only-preference-p'.  If
this is non-nil, then the `sy-default-attribution will be used.  If it
is nil, then a secondary scheme will be employed to find a suitable
attribution scheme.  First, the author's first name will be used.  If
that can't be found than the name list is searched for the first
non-nil, non-empty name string.  If still no name can be found, then
the user is either queried, or the `sy-default-attribution' is used,
depending on the value of `sy-confirm-always-p'.

If the variable `sy-confirm-always-p' is non-nil, superyank will always
confirm the attribution string with the user before inserting it into
the reply buffer.  Confirmation is with completion, but the completion
list is merely a suggestion; the user can override the list by typing
in a string of their choice.

The variable `sy-rewrite-header-hook' is a hook that contains a lambda
expression which rewrites the informative header at the top of the
yanked message.  Set to nil to avoid writing any header.

You can make superyank autofill each paragraph it cites by setting the
variable `sy-auto-fill-region-p' to non-nil.  Or set the variable to nil
and fill the paragraphs manually with sy-fill-paragraph-manually (see
below).

Finally, `sy-downcase-p' if non-nil, indicates that you always want to
downcase the attribution string before insertion, and
`sy-left-justify-p', if non-nil, indicates that you want to delete all
leading white space before citing.

Since the almost all yanking in other modes (RMAIL, GNUS) is done
through the function `mail-yank-original', and since superyank
overloads this function, cited yanking is automatically bound to the
C-c C-y key.  There are three other smaller functions that are
provided with superyank and they are bound as below.  Try C-h f on
each function to get more information on these functions.

Key Bindings:

C-c C-y     mail-yank-original (superyank's version)
C-c q       sy-fill-paragraph-manually
C-c C-q     sy-fill-paragraph-manually
C-c i       sy-insert-persist-attribution
C-c C-i     sy-insert-persist-attribution
C-c C-o     sy-open-line


Summary of variables, with their default values:

sy-default-attribution     (default: \"Anon\")
           Attribution to use if no attribution string can be deciphered
           from the original message buffer.

sy-citation-string         (default: \">\")
           String to append to the attribution string for citation, for
           best results, it should be one character with no trailing space.

sy-nested-citation-p       (default: nil)
           Nil means use non-nested citations, non-nil means use old style
           nested citations.

sy-cite-regexp             (default: \"[a-zA-Z0-9]*>\")
           Regular expression that matches the beginning of a previously
           cited line.  Always used at the beginning of a line so it does
           not need to start with a \"^\" character.

sy-titlecue-regexp         (default: \"\\s +-+\\s +\")
           Regular expression that matches a title delimiter in the name
           field.

sy-preferred-attribution   (default: 2)
           Integer indicating user's preferred attribution field.

sy-confirm-always-p        (default: t)
           Non-nil says always confirm with completion before inserting
           attribution.

sy-rewrite-header-hook     (default: 'sy-header-on-said)
           Hook for inserting informative header at the top of the yanked
           message.

sy-downcase-p              (default: nil)
           Non-nil says downcase the attribution string before insertion.

sy-left-justify-p          (default: nil)
           Non-nil says delete leading white space before citing.
  
sy-auto-fill-region-p      (default: nil)
           Non-nil says don't auto fill the region.  T says auto fill the
           paragraph.

sy-use-only-preference-p   (default: nil)
           If nil, use backup scheme when preferred attribution string
           can't be found.  If non-nil and preferred attribution string
           can't be found, then use sy-default-attribution."
  
  (interactive "P")
  
  (local-set-key "\C-cq"    'sy-fill-paragraph-manually)
  (local-set-key "\C-c\C-q" 'sy-fill-paragraph-manually)
  (local-set-key "\C-c\i"   'sy-insert-persist-attribution)
  (local-set-key "\C-c\C-i" 'sy-insert-persist-attribution)
  (local-set-key "\C-c\C-o" 'sy-open-line)
  
  (sy-yank-original arg))


;;
;; ----------------------------------------------------------------------
;;
;; based on Bruce Israel's "fill-paragraph-properly", and modified from
;; code posted by David C. Lawrence.  Modified to use the persistant
;; attribution if none could be found from the paragraph.
;;
(defun sy-fill-paragraph-manually (arg)
  "Fill paragraph containing or following point.
This automatically finds the sy-cite-regexp and uses it as the prefix.
If the sy-cite-regexp is not in the first line of the paragraph, it
makes a guess at what the fill-prefix for the paragraph should be by
looking at the first line and taking anything up to the first
alphanumeric character.

Prefix arg means justify both sides of paragraph as well.

This function just does fill-paragraph if the fill-prefix is set. If
what it deduces to be the paragraph prefix (based on the first line)
does not precede each line in the region, then the persistant
attribution is used.  The persistant attribution is just the last
attribution string used to cite lines."
  
  (interactive "P")
  (save-excursion
    (forward-paragraph)
    (or (bolp)
	(newline 1))
    
    (let ((end (point))
	  st
	  (fill-prefix fill-prefix))
      (backward-paragraph)
      (if (looking-at "\n")
	  (forward-char 1))
      (setq st (point))
      (if fill-prefix
	  nil
	(untabify st end) ;; die, scurvy tabs!
	;;
        ;; untabify might have made the paragraph longer character-wise,
        ;; make sure end reflects the correct location of eop.
	;;
        (forward-paragraph)
	(setq end (point))
        (goto-char st)
        (if (looking-at sy-cite-regexp)
            (setq fill-prefix (concat
			       (buffer-substring
				st (progn (re-search-forward sy-cite-regexp)
					  (point)))
			       " "))
	  ;;
          ;; this regexp is is convenient because paragraphs quoted by simple
          ;; indentation must still yield to us  <evil laugh>
	  ;;
          (while (looking-at "[^a-zA-Z0-9]")
	    (forward-char 1))
          (setq fill-prefix (buffer-substring st (point))))
        (next-line 1) (beginning-of-line)
        (while (and (< (point) end)
		    (not (string-equal fill-prefix "")))
	  ;;
          ;; if what we decided was the fill-prefix does not precede all
          ;; of the lines in the paragraph, we probably goofed.  In this
	  ;; case set it to the persistant attribution.
	  ;;
          (if (looking-at (regexp-quote fill-prefix))
	      ()
	    (setq fill-prefix sy-persist-attribution))
          (next-line 1)
	  (beginning-of-line)))
      (fill-region-as-paragraph st end arg))))

;;
;; ----------------------------------------------------------------------
;;
;; insert the persistant attribution at point
;;
(defun sy-insert-persist-attribution ()
  "Insert the persistant attribution.
This inserts the peristant attribution at the beginning of the line that
point is on.  This string is the last attribution confirmed and used
in the yanked reply buffer."
  (interactive)
  (save-excursion
    (beginning-of-line)
    (insert-string sy-persist-attribution)))


;;
;; ----------------------------------------------------------------------
;;
;; open a line putting the attribution at the beginning

(defun sy-open-line (arg)
  "Insert a newline and leave point before it.
Also inserts the persistant attribution at the beginning of the line.
With argument, inserts ARG newlines."
  (interactive "p")
  (save-excursion
    (let ((start (point)))
      (open-line arg)
      (goto-char start)
      (forward-line)
      (while (< 0 arg)
	(sy-insert-persist-attribution)
	(forward-line 1)
	(setq arg (- arg 1))))))

(provide 'superyank)

;;; superyank.el ends here