-
Notifications
You must be signed in to change notification settings - Fork 0
/
love22.el
1463 lines (1233 loc) · 50.9 KB
/
love22.el
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
;;; -*- Mode: Emacs-Lisp -*-
;;; LOVE22.EL
;;; Michael D. Ernst <[email protected]>
;;; 10/2/90
;;; last modified 10/18/90
;; LCD Archive Entry:
;; love22|Michael D. Ernst|[email protected]|
;; Support for writing and recognizing text in the style of Love 22.|
;; 90-10-18|1.0|~/games/love22.el.Z|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Description of love22.el
;;;
;; This file helps you write text in the style of Love 22, a peripatetic
;; jester and perennial presidential candidate (on a platform of "FOOD",
;; "CLOTH", and "RENTS") who looks exactly like Uncle Sam, and dresses the
;; part. He notices words such as "LOVE22" whose letters add up to 22 on
;; the ABC chart.
;;
;; The ABC Chart
;; A B C D E F G H I
;; J K L M N O P Q R
;; S T U V W X Y Z
;; 1 2 3 4 5 6 7 8 9
;;
;; In conversations, he points them out in real time, whether he's talking
;; or listening, and in writing he emphasizes them with capitalization and
;; quotation marks, sometimes substituting homonyms to make the addition
;; work out. This program lets you do the same, automatically. The effect
;; can be quite humorous in text -- some people mistake it for
;; Zippification. Try it on some text you have lying around to see the
;; result. (I have my mail-before-send-hook do this automatically on
;; certain mail, depending on the recipient.)
;;
;; You can get $22 bills, a copy of Love 22's platform, and other fun stuff
;; by sending a self-addressed stamped envelope (with a small donation, if
;; you're feeling generous; I sent $2.22 last time) to
;; Love 22
;; PO Box 4022
;; Key West, FL 33040
;; The commands of interest are:
;;
;; show-abc-chart
;; Displays the ABC chart in the *Help* buffer.
;; abc-chart-word
;; Reports the ABC chart value of the word at point.
;; In Love22 mode, bound to C-x =.
;; abc-chart-region
;; Reports the ABC chart value of the current region.
;; In Love22 mode, bound to M-=.
;; love22-buffer
;; Attempts to find words whose ABC chart values are 22 (or its multiples).
;; Will group words together, substitute homonyms, look for roots of words,
;; and try various other tricks. The function 22-hook is called on each
;; such word (or group of words) found; its default action is to upcase the
;; word(s), surround it by quotes, and change spaces to hyphens if
;; substitution (e.g. "2" for "too") occurred.
;; love22-region
;; Like love22-buffer; acts on the current region (between point and mark).
;; love22-mode
;; A minor mode to support writing of Love22 text. Love22 mode causes the
;; (almost) continual display of the ABC chart value value of the current
;; word, as well as emphasizing words whose value is a multiple of 22, if
;; love22-emphasize is non-nil. (This mode's emphasis functionality is
;; considerably less than that of love22-buffer, but it operates in real
;; time.)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; How to use this file
;;;
;;; Save this file as love22.el somewhere on your load-path.
;; *****
;; Don't byte-compile this file; it only works interpreted.
;; *****
;;; Add to your .emacs file (without the leading semicolons):
;;; (autoload 'show-abc-chart "love22"
;;; "Show the ABC chart in the *Help* window." t)
;;; (autoload 'abc-chart-word "love22"
;;; "Compute and display the ABC chart value of the word at point." t)
;;; (autoload 'abc-chart-region "love22"
;;; "Compute and display the ABC chart value of the current region." t)
;;; (autoload 'love22-buffer "love22"
;;; "Process a buffer to look like Love22 wrote it." t)
;;; (autoload 'love22-region "love22"
;;; "Process a region to look like Love22 wrote it." t)
;;; (autoload 'love22-mode "love22"
;;; "Minor mode for writing Love22 text." t)
;;; If love22.el is not on your load-path, you may need to specify a full
;;; path for the filename instead of just "love22" (eg "~/emacs/love22", if
;;; "~/emacs/" isn't on your load-path but that's where love22.el is).
;;; Now in future editing sessions you'll be ready to use the love22
;;; commands (for instance, M-x love22-region). To use them now, first do
;;; M-x load-file RET love22.el
;;; Changing values in the "Global variables" section modifies the behavior
;;; of the program; the experienced user may wish to play with these.
;;; Yes, you could use this as a filter from the command line by running emacs
;;; in batch mode. This exercise is left to the reader.
;;; Enjoy!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Contents
;;;
;;; Description of love22.el
;;; How to use this file
;;; Contents
;;; To do
;;; Global variables
;;; Datatypes
;;; ABC Chart manipulation
;;; "Love22" a region or buffer
;;; Word roots
;;; 22-hook
;;; Love22 minor mode
;;; Love22-new-command
;;; Overlay and restore by modifying function cell binding
;;; Overlay and restore by replacing in keymap
;;; Overlay and restore -- top-level functions
;;; Utilities
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; To do
;;;
;;; Bug: After turning love22 mode on and off, C-= is still bound to
;;; abc-chart-word.
;;; Make this usable in batch mode, and describe how to do it.
;;; Figure out why this won't byte-compile, fix it, add the following
;;; to the instructions.
;; ;;; Type
;; ;;; M-x byte-compile-file RET love22.el
;; ;;; to byte-compile the file (it's too slow when it runs interpreted).
;; ;;; (M-x = Meta-x (hold down meta, press x), RET = the return key)
;;; Perhaps interactively ask each time that a love22 word is about to be
;;; 22-hook'ed. (Only problem is that the current solution doesn't permit
;;; overlapping 22 words, since when something qualifies the rest of the
;;; history is thrown away. This shouldn't be overly difficult to fix.)
;;; Instead of building lists front-to-back, I ought to build them in
;;; reverse order, then reverse them later. nconc costs time to walk down
;;; the list to the end...
;;; Make the tex stuff settable, so I could get, say, bold instead of
;;; quotation with uppercasing.
;;; Make suffix-checking more efficient.
;;; * rewrite suffixp better
;;; * reverse the word and then compare from the front in word-root
(provide 'love22)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Global variables
;;;
;;; These variables are grouped into behavior modifiers, root exceptions,
;;; conversion list, and syntax table. The asterisk at the beginning of
;;; the documentation string means that the user is permitted to change
;;; the variable.
;;; BEHAVIOR MODIFIERS
(defvar love22-emphasize t
"*If non-nil, then in Love22 mode 22-words are automatically emphasized.")
(defvar love22-explicit-sum nil
"*If non-nil, when a word with ABC chart value > 22 is emphasized, the actual
value is noted afterward. This is done by love22-buffer and love22-region,
not by love22-mode, which never notes the value.")
;;; ROOT EXCEPTIONS
;;; These variables enumerate exceptions to the usual rules for finding
;;; roots of words. The strings should be lowercase.
;; Consider adding "est" to the list of suffixes. It might be too
;; complicated, too many exceptions, though.
(defvar love22-ly-exceptions
'("belly" "only" "really")
"*Words whose roots can't be found by removing the \"ly\".")
(defvar love22-nt-exceptions
'("ain't" "can't" "don't" "shan't" "won't")
"*Words whose roots can't be found by removing the \"n't\".
I believe that this list is exhaustive.")
(defvar love22-es-exceptions
'("james" "molasses" "rhodes" "ulysses" "yes")
"*Words ending in \"es\" whose singulars cannot be found by removing the \"es\" or \"s\".")
(defvar love22-es-plural-suffixes
'("ch" "sh" "th" "o" "ss" "x" "ay" "ey" "iy" "oy" "uy")
"*Words ending with one of these suffixes add \"es\" to make the plural.")
(defvar love22-es-s-exceptions
'("shoes" "horseshoes")
"*Words ending in \"es\" that look like \"es\" was added to make the plural,
but actually only \"s\" was. In other words, these words have some member of
love22-es-plural-suffixes immediately before the \"es\".")
(defvar love22-s-exceptions
'("as" "carlos" "Emacs" "has" "its" "plus" "towards" "was" "wumpus")
"*Words ending in \"s\" whose singulars can't be found by removing the \"s\".
Words ending in \"es\" should appear in love22-es-exceptions, not here.")
(defvar love22-ous-exceptions
'("bayous" "bijous" "cachous" "carcajous" "caribous" "chous" "congous"
"froufrous" "ious" "kinkajous" "manitous" "marabous" "sous" "tinamous")
"*Words ending in \"ous\" which are plural nouns, not adjectives.
I believe that this list is exhaustive.
\"loups-garous\" is omitted since two s's were added to make the plural.")
(defvar love22-ism-exceptions
'("schism")
"*Words whose roots can't be found by removing the \"ism\".")
(defvar love22-ist-exceptions
'("exist" "fist" "twist")
"*Words whose roots can't be found by removing the \"ist\".")
;;; CONVERSION LIST
;;; These could go both directions, and possibly among homonyms (eg to => too)
;;; as well, but those seem less in the spirit of the thing.
;;; The inputs and results are lowercase for uniformity.
;;; None of the conversion inputs is a 22-word, else it shouldn't have been
;;; here to begin with. (This is not required, however.)
;;; I should make sure none of these has the same ABC chart value before and
;;; after, though the code works correctly (ie, uses the original value)
;;; regardless.
(defvar love22-conversion-alist
'(
;; letter homonyms
("be" . "B")
("see" . "C")
("sea" . "C")
("gee" . "G")
("aye" . "I")
("eye" . "I")
("jay" . "J")
("ell" . "L")
("oh" . "O")
("pea" . "P")
("pee" . "P")
("cue" . "Q")
("queue" . "Q")
("are" . "R")
("tee" . "T")
("tea" . "T")
("you" . "U")
("why" . "Y")
;; number homonyms
("won" . "1")
("to" . "2")
("too" . "2")
("for" . "4")
("fore" . "4")
("ate" . "8")
("nein" . "9")
;; numbers
("one" . "1")
("two" . "2")
("three" . "3")
("four" . "4")
("five" . "5")
("six" . "6")
("seven" . "7")
("eight" . "8")
("nine" . "9")
("ten" . "10")
;; synonyms
("can't" . "cannot") ;; cannot is a 22-word
;; How to deal with multi-word substitutions? These could be especially
;; troublesome if only one of the two words is part (or all) of a 22-word.
;; ("won't" . "will not")
;; likewise with ("ain't" "can't" "don't" "shan't" "won't")
;; Don't want to just treat "will not" as a single word because of 22-hook.
)
"*Alist of word conversions used to give phrases a 22 ABC chart value."
)
;;; SYNTAX TABLE
(defvar love22-syntax-table nil
"*Syntax table used by love22 to find word boundaries.")
;; What if the syntax table changes? What if I change buffers or modes?
;; What if I invoke this from a buffer with a weird syntax table?
;; Solution: use the text mode syntax table as a base and hope for the best.
(if (null love22-syntax-table)
(progn
(setq love22-syntax-table (copy-syntax-table text-mode-syntax-table))
;; Make - into inter-word punctuation.
(modify-syntax-entry ?- "'" love22-syntax-table)
;; Make ' a word constituent.
(modify-syntax-entry ?' "w" love22-syntax-table)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Datatypes
;;;
;;; DESCRIPTION
;; A description represents a word unit (a word, a converted word, or the
;; root of a word) via a vector of:
;; ABC-value (a number)
;; start (a point)
;; length (nil if whole word, else number of characters in root)
;; text (t if the text is what actually appears in the buffer)
(defmacro make-description (abc-value start length text)
(list 'vector abc-value start length text))
(defmacro description-abc-value (desc)
(list 'aref desc 0))
(defmacro description-start (desc)
(list 'aref desc 1))
(defmacro description-length (desc)
(list 'aref desc 2))
(defmacro description-text (desc)
(list 'aref desc 3))
;; For use by mapcar
(defun description-text-fn (desc)
(description-text desc))
;; If the length is nil, it's no root. If the length is a number, it is.
(defmacro description-rootp (desc)
(list 'description-length desc))
;;; TRACE
;; A trace is a view of the previous few words in the buffer.
;; Several traces may apply, if a recent word can be converted.
;; A trace is a list of descriptions, in reverse order (ie, most recent first).
(defvar love22-traces nil
"A list of traces, most preferred first.")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ABC Chart manipulation
;;;
(defun show-abc-chart ()
"Display the ABC chart relating letters to their numeric values."
(interactive)
(with-output-to-temp-buffer "*Help*"
(princ " The ABC Chart\n\n")
(princ "A B C D E F G H I\n")
(princ "J K L M N O P Q R\n")
(princ "S T U V W X Y Z \n")
(princ "1 2 3 4 5 6 7 8 9\n")
(print-help-return-message)))
;; perhaps do this more elegantly.
(defvar abc-chart
(let ((chart (make-vector 256 0)))
(setq ch ?1)
(setq val 1)
(while (<= ch ?9)
(aset chart ch val)
(setq val (1+ val))
(setq ch (1+ ch)))
(setq ch ?a)
(setq val 1)
(while (<= ch ?z)
(aset chart ch val)
(aset chart (upcase ch) val)
(setq val (if (= val 9) 1 (1+ val)))
(setq ch (1+ ch)))
chart)
"The ABC chart assigning numeric values to characters.")
(defmacro abc-chart-char (char)
(` (aref abc-chart (, char))))
;; For use by mapcar
(defun abc-chart-char-fn (char)
(abc-chart-char char))
;; This does NOT do clever substitutions.
(defun abc-chart-string (string)
(apply '+ (mapcar 'abc-chart-char-fn string)))
;; This is adapted from ispell.el.
(defun love22-current-word (&optional noerr)
"Returns a cons of (start . end) for the current word.
Errs if there is no current word, or returns nil if NOERR is true."
(with-love22-syntax-table
(save-excursion
(if (not (looking-at "\\w"))
;; Move backward for word if not already on one
(re-search-backward "\\w" (point-min) 'stay))
;; Move to start of word
(re-search-backward "\\W" (point-min) 'stay)
;; Find start and end of word
(if (re-search-forward "\\w+" nil t)
(cons (match-beginning 0) (match-end 0))
(if (not noerr)
(error "No word to check."))))))
(defun abc-chart-word (&optional noerr terse)
"Check word at or before point on the ABC chart.
If NOERR is non-nil, then doesn't err if there is no current word.
If TERSE is non-nil, the output is abbreviated."
(interactive)
(let ((region (love22-current-word noerr)))
(if region
(abc-chart-region (car region) (cdr region) terse))))
(defun abc-chart-region (start end &optional terse)
"Print the sum of the ABC chart values of the characters in the region.
If TERSE is non-nil, the output is abbreviated."
(interactive "r")
(let ((region-string (buffer-substring start end)))
(message (if terse
"%s = %d"
"%s = %d on the ABC chart.")
region-string (abc-chart-string region-string))))
(defun abc-chart-word-emphasize-maybe (&optional noerr terse)
"Check word at or before point on the ABC chart. If = 22, then emphasize it.
This needs to be interactive because it gets bound to keys."
(interactive)
(let ((region (love22-current-word noerr)))
(if region
(abc-chart-region-emphasize-maybe (car region) (cdr region) terse))))
;; 'Twould be nice if this did the hyphenization hacks, too, but it DOESN'T!
;; (and probably shouldn't, given its calling pattern -- too hard to undo).
(defun abc-chart-region-emphasize-maybe (start end &optional terse)
"Print the sum of the ABC chart values of the characters in the region; if
= 22, then emphazise it."
;; This doesn't really need to be interactive.
;; (interactive "r")
(let* ((region-string (buffer-substring start end))
(region-abc-value (abc-chart-string region-string)))
(if (word-22-value-p region-abc-value)
;; could also add quotes
(upcase-region start end))
(message (if terse
"%s = %d"
"%s = %d on the ABC chart.")
region-string (abc-chart-string region-string))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; "Love22" a region or buffer
;;;
;;; Beware adding a hypen on the front or end of a hyphenized word, though
;;; that isn't so great a tragedy.
;;; algorithm:
;; maintain a point before which nothing will be done.
;; maintain a list of lists of ABC values of the following words,
;; and the words themselves (perhaps in reverse order?), and points.
;; * sublists in reverse order: easier to look at bigger & bigger prefixes
;; * but we shouldn't be looking at more than a couple of words that way, and
;; the "correct" direction is more intuitive.
;; can use mapcar to compute sums: expensive but easy. (no, hard to look
;; at smaller prefixes first).
;;
;; get next word, compute ABC value(s).
;; * if any is 22 or multiple thereof, win
;; * check each with prefixes of the lists; if any add up to 22 or mult, win.
;; * prune the lists
(defun love22-region (start end)
"Call 22-hook on the words in the region whose ABC chart values are 22."
(interactive "r")
;; get rid of the buffer, if it's there.
(debug-form
(if (get-buffer "*debug*")
(debug-form (kill-buffer (get-buffer "*debug*")))))
(with-love22-syntax-table
(save-excursion
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(setq love22-traces nil)
;; while we can find the start of a new word
(while (re-search-forward "\\<" nil t)
(setq word-start (point))
(forward-word 1)
(setq word-end (point))
(setq word (buffer-substring word-start word-end))
(check-for-22 word word-start)
)
))))
(defun love22-buffer ()
"Call 22-hook on the words in the buffer whose ABC chart values are 22.
If narrowing is in effect, only affects the accessible region."
(interactive)
(love22-region (point-min) (point-max)))
;; There are two distinct criteria: one for individual words, and another
;; for sequences of words. For instance, by default we let words have any
;; multiple of 22, but avoid long sequences by prohibiting multi-word
;; sequences from having such very large multiples.
(defun word-22-value-p (value)
(and (zerop (mod value 22))
(not (zerop value))))
(defun trace-22-value-p (value)
(or (= 22 value) (= 44 value)))
(defun trace-22-too-large (value)
"Returns t if the value is larger than any of the acceptable 22 values."
(> value 44))
;; I should probably downcase the word first of all, because of suffix
;; checking later. As it is, I'm doing that twice: once for homonym
;; (conversion) checking and once for root checking.
(defun check-for-22 (word word-start)
"Checks WORD (and combinations ending with WORD) for ABC value of 22.
Uses homonyms, checks for roots, searches backward through traces.
WORD-START is the start of the word; point is on the end of the word.
Calls 22-hook on any 22-word or sequence ending with word.
If none is found, updates love22-traces.
Doesn't return a useful value."
(debug-form
(prin1 "check-for-22:") (terpri)
(prin1 " ") (prin1 word) (prin1 " ") (prin1 word-start) (terpri)
(prin1 " ") (prin1 love22-traces) (terpri))
;; possibilities is a list of (continuep . description) pairs.
;; found-22 is a cons of (abc-value . trace)
(let* ((possibilities nil)
(found-22
(catch 'found-22
(let* ((permit-continuation
(not (or
;; end of sentence
(looking-at (sentence-end))
;; two newlines before next word
(looking-at "\\W*\n\\W*\n")
;; It would be nice to permit this if the whole quote
;; or parenthesis were included, but not otherwise...
;; end of a quote or parenthesis, or colon
(looking-at "\"\\|''\\|)\\|:")
;; beginning of a quote or parenthesis
(looking-at ",?\\s *\\(\"\\|``\\|(\\)")
;; Possibly prohibit coalescing after commas,
;; but that doesn't seem like such a tragedy.
)))
;; ORIGINAL WORD
(word-abc-value (abc-chart-string word))
(word-description (make-description
word-abc-value word-start nil nil)))
(if (word-22-value-p word-abc-value)
(throw 'found-22 (cons word-abc-value (list word-description))))
(if (not (trace-22-too-large word-abc-value))
(setq possibilities (list (cons permit-continuation
word-description))))
;; CONVERSION
(let ((cword (cdr (assoc (downcase word) love22-conversion-alist))))
(if cword
(let* ((cword-abc-value (abc-chart-string cword))
(cword-description (make-description
cword-abc-value word-start nil
cword)))
(if (word-22-value-p cword-abc-value)
(throw 'found-22 (cons cword-abc-value
(list cword-description))))
(if (not (trace-22-too-large cword-abc-value))
(nconc possibilities
(list (cons permit-continuation
cword-description))))))))
;; ROOT
(let ((root (word-root word)))
(if root
(let* ((root-abc-value (abc-chart-string root))
(root-description (make-description
root-abc-value word-start
(length root) nil)))
(if (word-22-value-p root-abc-value)
(throw 'found-22 (cons root-abc-value
(list root-description))))
;; Perhaps don't allow coalescing with roots at all.
(if (not (trace-22-too-large root-abc-value))
(nconc possibilities
(list (cons nil root-description)))))))
(debug-form (prin1 " final possibilities: ")
(prin1 possibilities) (terpri))
;; now the word, its conversion, and its root are in possibilities
(let ((old-traces love22-traces))
(setq love22-traces nil)
(while possibilities
(love22-coalesce (car possibilities) old-traces)
(setq possibilities (cdr possibilities))))
;; No 22 was found (else we would have thrown out to 'found-22).
;; ACTION: I should now prune love22-traces of duplicates.
;; end of catch body; default is nil (no 22 found)
nil))
)
;; body of (let found-22 ...)
(debug-form (prin1 " found-22 = ") (prin1 found-22) (terpri))
(if found-22
(progn (22-hook found-22)
(setq love22-traces nil)))))
(defun love22-coalesce (cont-desc old-traces)
"Try to combine words in the traces into groups with 22-valued ABC values.
CONT-DESC is a (continuep . description) cons; TRACES is a list of traces."
(debug-form
(prin1 "love22-coalesce:") (terpri)
(prin1 " ") (prin1 cont-desc) (terpri)
(prin1 " ") (prin1 old-traces) (terpri))
(let ((continuep (car cont-desc))
(description (cdr cont-desc)))
(if (null old-traces)
;; There were no qualifying previous words.
;; Start a new trace with only the current description.
;; love22-traces might not be nil here if this is a transformation
;; and the original word has already been added.
(if continuep
(setq love22-traces
(nconc love22-traces (list (list description)))))
;; There were previous words.
(let (new-traces
this-trace
tt-remainder ; poor variable name choice
tt-sum)
(while old-traces
;; this-trace exists to be modified and returned (thrown to 'found-22).
;; I should be able to do this more efficiently
;; (eg only copy what's needed, not everything).
;; This copy is because of the setcdr below, for truncating traces.
;; (Alternately, I could always simply add to traces, and lose them
;; only after a 22 or at the end of a sentence. Hmmmm... seems fishy.)
(setq this-trace (cons description (copy-sequence (car old-traces)))
tt-remainder (cons nil this-trace)
tt-sum 0)
;; tt-remainder lags behind those elements of this-trace actually
;; examined, so this while loop's condition is "while this-trace has
;; unexamined elements". This won't bail out on the first time
;; through because we wouldn't have gotten here if the ABC value of
;; the first word were too large.
(while (cdr tt-remainder)
(debug-form
(prin1 " tt-remainder = ") (prin1 tt-remainder) (terpri))
;; Add in the next description's ABC value.
(setq tt-sum
(+ tt-sum (description-abc-value (car (cdr tt-remainder)))))
(if (trace-22-too-large tt-sum)
;; If this value is too large, then we will never need to look
;; beyond the current value, so ditch everything after it.
(setcdr tt-remainder nil)
(progn
(setq tt-remainder (cdr tt-remainder))
;; Now tt-remainder is the last description summed into tt-sum.
(if (trace-22-value-p tt-sum)
(progn
;; Delete everything not contributing to tt-sum.
(setcdr tt-remainder nil)
;; Return the (modified) trace.
(throw 'found-22 (cons tt-sum this-trace))))))
;; this was neither a 22-value nor too large; try again
) ; end (while (cdr tt-remainder) ...)
;; no 22-value found; save away this-trace
(if continuep
(setq new-traces (nconc new-traces (list this-trace))))
(setq old-traces (cdr old-traces))
) ; end (while old-traces ...)
(if continuep
(setq love22-traces (nconc love22-traces new-traces)))
) ; end (let new-traces ...)
)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Word roots
;;;
;;; Finding roots may backfire if a root gets connected with something
;;; previous instead of the whole word getting connected with something later.
;;; That is, "FOO BARism baz" might be less desirable than foo BARISM BAZ". Of
;;; course, BARISM is always checked before BARism; likewise with coalescing.
;;; Finding word roots seems like a hard problem. I'm giving up on most of
;;; it for now.
;;; A future version could return a number of progressively smaller roots.
;;; ACTION: This suffix checking is VERY inefficient. Fix it later.
(defun word-root (word)
"Returns a root for WORD, or nil if none is found.
Nasty heuristic."
(setq word (downcase word))
(setq suffix-characters
(cond
((and (suffixp word "ly")
;; b: all "ble"
;; f: last syllable is "fly"
;; i: roots end in "y"
(not (memq (elt word (- (length word) 3)) '(?b ?f ?i)))
(not (string-member word love22-ly-exceptions)))
2)
((and (suffixp word "n't")
(not (string-member word love22-nt-exceptions)))
3)
((suffixp word "'s")
2)
;; Maybe I don't need any silent-e check (eg consecutive consonants).
((suffixp word "es")
(debug-form (prin1 "ES-check: ") (prin1 word)
(prin1 (string-member word love22-es-exceptions))
(terpri))
(if (string-member word love22-es-exceptions)
nil
;; otherwise:
(let ((sans-es (substring word 0 (- (length word) 2))))
(if (suffixp sans-es "i")
nil
(if (and (apply 'or-fn
(mapcar
(function
(lambda (suffix) (suffixp sans-es suffix)))
love22-es-plural-suffixes))
(not (string-member word love22-es-s-exceptions)))
2
1)))))
((and (suffixp word "s")
;; avoid double-counting possessives
(not (suffixp word "'s"))
;; i: root could end in "y"
;; s: "mess", "watercress"
;; v: root could end in "f" or "fe"
(not (memq (elt word (- (length word) 2)) '(?i ?s ?v)))
(not (string-member word love22-s-exceptions))
;; bweare of "ous" adjectives
(or (not (suffixp word "ous"))
(string-member word love22-ous-exceptions)))
1)
((and (suffixp word "ism")
(not (string-member word love22-ism-exceptions)))
3)
((and (suffixp word "ist")
(not (string-member word love22-ist-exceptions)))
3)
;; ;; these might be too erratic for use.
;; ((suffixp word "ing")
;; ;; beware doubled letter: travelling vs traveling (both OK)
;; ;; beware "baking"
;; (setq word-root (allbutlastthreechars word)))
;; ((past-form word)
;; ;;; for instance, "<doubled consonant>ed"
;; (foo))
) ; end of cond
)
(debug-form (princ " suffix-chars: ") (prin1 suffix-characters) (terpri))
(if suffix-characters
(substring word 0 (- (length word) suffix-characters))))
(defun suffixp (word suffix)
"Returns t if SUFFIX is a suffix of WORD, nil otherwise."
(let ((suffix-len (length suffix))
(word-len (length word)))
(and (< suffix-len word-len)
(string-equal suffix (substring word (- word-len suffix-len))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 22-hook
;;;
;; What a mess!
;; ACTION: problem with redoing when there's an (= 44) there already which
;; isn't noticed or is coalesced with something else.
;; This should act intelligently depending on intervening text (eg newlines).
;; "Can insert quotes, upcase, change spaces to hyphens, etc."
;; * upcase
;; * surround with "" (add extra space if already "")
;; * spaces to hyphens, if any substitutions done
;; . be thoughtful about punctuation (eg don't, if colon or hyphen)
;; . remember about newlines, reinsert elsewhere (need a global var)
;; work from end back to avoid screwing up points?
;; just use word-motion commands to find next word? No, because of roots (?).
;; perhaps to deal with roots we need another slot in the description.
;; if the latter, beware roots getting messed up.
(defun 22-hook (abc-val-trace)
"The argument is an (abc-value . trace) cons."
(debug-form
(prin1 "22-hook:") (terpri)
(prin1 " ") (prin1 abc-val-trace) (terpri))
(with-love22-syntax-table
(let* ((abc-value (car abc-val-trace))
;; Put 22-trace in forward order.
(22-trace (nreverse (cdr abc-val-trace)))
(convert-spaces
;; at least two words in this trace, and some word was transformed.
(and (cdr 22-trace)
(apply 'or-fn (mapcar 'description-text-fn 22-trace))))
;; (region-start (description-start (car 22-trace)))
;; (region-end (description-end (car (cdr (last 22-trace)))))
(missing-newline nil)
(trace-start (description-start (car 22-trace))))
(debug-form
(prin1 " convert = ") (prin1 convert-spaces) (terpri))
;; Remember where I was.
;; Is this not very useful, in the case in which characters are inserted
;; or deleted? Perhaps the best thing is just to leave point at the end
;; of the 22-region, or at the beginning of the first word after, or at
;; the end of the last word even partially contained in it
;; (save-excursion ...)
;; go to beginning of region.
(goto-char trace-start)
;; invariant: at the beginning/end of this loop, I'm at the beginning
;; of the word corresponding to the first description.
(while (cdr 22-trace)
(if (description-text (car 22-trace))
(progn (kill-word 1)
(insert (upcase (description-text (car 22-trace)))))
(upcase-word 1))
;; (setq end-of-prev-word (point))
;; (re-search-forward "\\<")
;; (setq start-of-next-word (point))
;; Deal with interword stuff.
(if convert-spaces
(cond ((looking-at "\\s *\\<")
;; only whitespace before beginning of next word, no
;; punctuation: convert to a hyphen with no surrounding
;; space
(let ((end-of-prev-word (point)))
(re-search-forward "\\<")
(setq missing-newline
(or missing-newline
(string-match "\n" (buffer-substring
end-of-prev-word
(point)))))
(delete-region end-of-prev-word (point))
(insert "-")))
;; could have other cases here
;; default -- leave punctuation as is.
(t
(re-search-forward "\\<")))
;; Leave interword space alone, but move to beginning of next word.
;; Here perhaps I should close up too-large spaces, etc. Nah, that's
;; for the typist to do.
(re-search-forward "\\<"))
(setq 22-trace (cdr 22-trace))
)
;; Upcase last word, which may be a root.
;; This special code isn't necessary if we prohibit coalescing with roots.
(let ((last-desc (car 22-trace)))
(if (description-rootp last-desc)
(let ((word-start (point)))
(forward-char (description-length last-desc))
(debug-form
(prin1 " forward ") (prin1 (description-length last-desc))
(prin1 " from ") (prin1 (char-to-string (preceding-char)))
(prin1 (char-to-string (following-char))) (terpri))
(upcase-region word-start (point)))
(if (description-text (car 22-trace))
(progn (kill-word 1)
(insert (upcase (description-text (car 22-trace)))))
(upcase-word 1))))
;; Add surrounding quotes at trace-start and at point.
;; If the region is already surrounded by quotes, don't add any more.
(if (not (quoted-region-p trace-start (point)))
(let ((inserted-characters 0)
(trace-end (point)))
(goto-char trace-start)
(setq inserted-characters (insert-after-point
(if (TeX-mode-p) "``" "\"")))
(setq inserted-characters (+ inserted-characters
(add-quote-separation-maybe)))
(goto-char trace-end)
(forward-char inserted-characters)
(insert (if (TeX-mode-p) "''" "\""))))
;; Add sum
(if (and (not (= 22 abc-value))
love22-explicit-sum)
(progn
(if (not (looking-at "\\w"))
(insert " "))
;; could use "n * 22" instead of "44" "66", etc.
(insert "(= " (int-to-string abc-value) ")")))
;; Add newline
(if missing-newline
(progn (insert "\n")
(fixup-whitespace)))
;; Add quote separation
(add-quote-separation-maybe)
;; Skip over the rest of word if this was a prefix.
(if (description-rootp (car 22-trace))
(re-search-forward "\\>"))
;; if sum wasn't 22 (eg was 44), then point this out: insert
;; " (= 2*22)"
)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Love22 minor mode
;;;
;; problem: minibuffer seems to use map which causes it to annoyingly
;; write over minibuffer input. (Well, it goes away after a second.)
;; Soln: (1) fix this (2) put info in mode line
;; consider putting info in mode line instead.
;;; I should change the current syntax table into a full syntax table if
;;; it's currently sparse, else I'll spend too much time looking up characters
;;; in it (and describe-bindings will be ugly).
(defun love22-mode (arg)
"Minor mode to support writing of Love22 text.
Toggle love22-mode, or turn it on if optional ARG is positive.
Love22 mode causes the display of the ABC chart value value of the current
word, as well as emphasizing words whose value is a multiple of 22, if
love22-emphasize is non-nil. (This mode's emphasis functionality is
considerably less than that of love22-buffer, but it operates in real time."
(interactive "P")
(if love22-mode
(love22-restore))
(setq love22-mode
(if (null arg) (not love22-mode)
(> (prefix-numeric-value arg) 0)))