summaryrefslogtreecommitdiff
path: root/lisp/calendar/diary-lib.el
blob: ec02900f49571511333bec75cc4d11c705306a72 (about) (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
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
;;; diary-lib.el --- diary functions

;; Copyright (C) 1989-1990, 1992-1995, 2001-2015 Free Software
;; Foundation, Inc.

;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
;; Maintainer: Glenn Morris <rgm@gnu.org>
;; Keywords: calendar

;; 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 3 of the License, 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.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; See calendar.el.

;;; Code:

(require 'calendar)
(eval-and-compile (load "diary-loaddefs" nil t))

(defgroup diary nil
  "Emacs diary."
  :prefix "diary-"
  :group 'calendar)

(defcustom diary-include-string "#include"
  "The string indicating inclusion of another file of diary entries.
See the documentation for the function `diary-include-other-diary-files'."
  :type 'string
  :group 'diary)

(defcustom diary-list-include-blanks nil
  "If nil, do not include days with no diary entry in the list of diary entries.
Such days will then not be shown in the fancy diary buffer, even if they
are holidays."
  :type 'boolean
  :group 'diary)

(defface diary-anniversary '((t :inherit font-lock-keyword-face))
  "Face used for anniversaries in the fancy diary display."
  :version "22.1"
  :group 'calendar-faces)

(defface diary-time '((t :inherit font-lock-variable-name-face))
  "Face used for times of day in the fancy diary display."
  :version "22.1"
  :group 'calendar-faces)

(defface diary-button '((((type pc) (class color))
                         (:foreground "lightblue")))
  "Face used for buttons in the fancy diary display."
  :version "22.1"
  :group 'calendar-faces)

;; Face markup of calendar and diary displays: Any entry line that
;; ends with [foo:value] where foo is a face attribute (except :box
;; :stipple) or with [face:blah] tags, will have these values applied
;; to the calendar and fancy diary displays.  These attributes "stack"
;; on calendar displays.  File-wide attributes can be defined as
;; follows: the first line matching "^# [tag:value]" defines the value
;; for that particular tag.
(defcustom diary-face-attrs
  '((" *\\[foreground:\\([-a-z]+\\)\\]$" 1 :foreground string)
    (" *\\[background:\\([-a-z]+\\)\\]$" 1 :background string)
    (" *\\[width:\\([-a-z]+\\)\\]$" 1 :width symbol)
    (" *\\[height:\\([.0-9]+\\)\\]$" 1 :height int)
    (" *\\[weight:\\([-a-z]+\\)\\]$" 1 :weight symbol)
    (" *\\[slant:\\([-a-z]+\\)\\]$" 1 :slant symbol)
    (" *\\[underline:\\([-a-z]+\\)\\]$" 1 :underline stringtnil)
    (" *\\[overline:\\([-a-z]+\\)\\]$" 1 :overline stringtnil)
    (" *\\[strike-through:\\([-a-z]+\\)\\]$" 1 :strike-through stringtnil)
    (" *\\[inverse-video:\\([-a-z]+\\)\\]$" 1 :inverse-video tnil)
    (" *\\[face:\\([-0-9a-z]+\\)\\]$" 1 :face string)
    (" *\\[font:\\([-a-z0-9]+\\)\\]$" 1 :font string)
    ;; Unsupported.
;;;    (" *\\[box:\\([-a-z]+\\)\\]$" 1 :box)
;;;    (" *\\[stipple:\\([-a-z]+\\)\\]$" 1 :stipple)
    )
  "Alist of (REGEXP SUBEXP ATTRIBUTE TYPE) elements.
This is used by `diary-pull-attrs' to fontify certain diary
elements.  REGEXP is a regular expression to for, and SUBEXP is
the numbered sub-expression to extract.  `diary-glob-file-regexp-prefix'
is pre-pended to REGEXP for file-wide specifiers.  ATTRIBUTE
specifies which face attribute (e.g. `:foreground') to modify, or
that this is a face (`:face') to apply.  TYPE is the type of
attribute being applied.  Available TYPES (see `diary-attrtype-convert')
are: `string', `symbol', `int', `tnil', `stringtnil.'"
  :type '(repeat (list (string :tag "Regular expression")
                       (integer :tag "Sub-expression")
                       (symbol :tag "Attribute (e.g. :foreground)")
                       (choice (const string :tag "A string")
                               (const symbol :tag "A symbol")
                               (const int :tag "An integer")
                               (const tnil :tag "t or nil")
                               (const stringtnil
                                      :tag "A string, t, or nil"))))
  :group 'diary)

(defcustom diary-glob-file-regexp-prefix "^\\#"
  "Regular expression pre-pended to `diary-face-attrs' for file-wide specifiers."
  :type 'regexp
  :group 'diary)

(defcustom diary-file-name-prefix nil
  "Non-nil means prefix each diary entry with the name of the file defining it."
  :type 'boolean
  :group 'diary)

(defcustom diary-file-name-prefix-function 'identity
  "The function that will take a diary file name and return the desired prefix."
  :type 'function
  :group 'diary)

(defcustom diary-sexp-entry-symbol "%%"
  "The string used to indicate a sexp diary entry in `diary-file'.
See the documentation for the function `diary-list-sexp-entries'."
  :type 'string
  :group 'diary)

(defcustom diary-comment-start nil
  "String marking the start of a comment in the diary, or nil.
Nil means there are no comments.  The diary does not display
parts of entries that are inside comments.  You can use comments
for whatever you like, e.g. for meta-data that packages such as
`appt.el' can use.  Comments may not span multiple lines, and there
can be only one comment on any line.
See also `diary-comment-end'."
  :version "24.1"
  :type '(choice (const :tag "No comment" nil) string)
  :group 'diary)

(defcustom diary-comment-end ""
  "String marking the end of a comment in the diary.
The empty string means comments finish at the end of a line.
See also `diary-comment-start'."
  :version "24.1"
  :type 'string
  :group 'diary)

(defcustom diary-hook nil
  "List of functions called after the display of the diary.
Used for example by the appointment package - see `appt-activate'."
  :type 'hook
  :group 'diary)

(defcustom diary-display-function 'diary-fancy-display
  "Function used to display the diary.
The two standard options are `diary-fancy-display' and `diary-simple-display'.

When this function is called, the variable `diary-entries-list'
is a list, in order by date, of all relevant diary entries in the
form of ((MONTH DAY YEAR) STRING), where string is the diary
entry for the given date.  This can be used, for example, to
produce a different buffer for display (perhaps combined with
holidays), or hard copy output."
  :type '(choice (const diary-fancy-display :tag "Fancy display")
                 (const diary-simple-display :tag "Basic display")
                 (const :tag "No display" ignore)
                 (function :tag "User-specified function"))
  :initialize 'custom-initialize-default
  :set 'diary-set-maybe-redraw
  :version "23.2"                       ; simple->fancy
  :group 'diary)

(defcustom diary-list-entries-hook nil
  "Hook run after diary file is culled for relevant entries.

If you add `diary-include-other-diary-files' to this hook, you
will probably also want to add `diary-mark-included-diary-files'
to `diary-mark-entries-hook'.  For example, to cause the fancy
diary buffer to be displayed with diary entries from various
included files, each day's entries sorted into lexicographic
order, add the following to your init file:

     (setq diary-display-function \\='diary-fancy-display)
     (add-hook \\='diary-list-entries-hook \\='diary-include-other-diary-files)
     (add-hook \\='diary-list-entries-hook \\='diary-sort-entries t)

Note how the sort function is placed last, so that it can sort
the entries included from other files.

This hook runs after `diary-nongregorian-listing-hook'.  These two hooks
differ only if you are using included diary files.  In that case,
`diary-nongregorian-listing-hook' runs for each file, whereas
`diary-list-entries-hook' only runs once, for the main diary file.
So for example, to sort the complete list of diary entries you would
use the list-entries hook, whereas to process e.g. Islamic entries in
the main file and all included files, you would use the nongregorian hook."
  :type 'hook
  :options '(diary-include-other-diary-files diary-sort-entries)
  :group 'diary)

(defcustom diary-mark-entries-hook nil
  "List of functions called after marking diary entries in the calendar.
You might wish to add `diary-mark-included-diary-files', in which case
you will probably also want to add `diary-include-other-diary-files' to
`diary-list-entries-hook'.

This hook runs after `diary-nongregorian-marking-hook'.  These two hooks
differ only if you are using included diary files.  In that case,
`diary-nongregorian-marking-hook' runs for each file, whereas
`diary-mark-entries-hook' only runs once, for the main diary file."
  :type 'hook
  :options '(diary-mark-included-diary-files)
  :group 'diary)

(defcustom diary-nongregorian-listing-hook nil
  "List of functions called for listing diary file and included files.
As the files are processed for diary entries, these functions are used
to cull relevant entries.  You can use any or all of
`diary-bahai-list-entries', `diary-hebrew-list-entries', and
`diary-islamic-list-entries'.  The documentation for these functions
describes the style of such diary entries.

You can use this hook for other functions as well, if you want them to
be run on the main diary file and any included diary files.  Otherwise,
use `diary-list-entries-hook', which runs only for the main diary file."
  :type 'hook
  :options '(diary-bahai-list-entries
             diary-hebrew-list-entries
             diary-islamic-list-entries)
  :group 'diary)

(defcustom diary-nongregorian-marking-hook nil
  "List of functions called for marking diary file and included files.
As the files are processed for diary entries, these functions are used
to cull relevant entries.  You can use any or all of
`diary-bahai-mark-entries', `diary-hebrew-mark-entries' and
`diary-islamic-mark-entries'.  The documentation for these functions
describes the style of such diary entries.

You can use this hook for other functions as well, if you want them to
be run on the main diary file and any included diary files.  Otherwise,
use `diary-mark-entries-hook', which runs only for the main diary file."
  :type 'hook
  :options '(diary-bahai-mark-entries
             diary-hebrew-mark-entries
             diary-islamic-mark-entries)
  :group 'diary)

(defcustom diary-print-entries-hook 'lpr-buffer
  "Run by `diary-print-entries' after preparing a temporary diary buffer.
The buffer shows only the diary entries currently visible in the
diary buffer.  The default just does the printing.  Other uses
might include, for example, rearranging the lines into order by
day and time, saving the buffer instead of deleting it, or
changing the function used to do the printing."
  :type 'hook
  :group 'diary)

(defcustom diary-unknown-time -9999
  "Value returned by `diary-entry-time' when no time is found.
The default value -9999 causes entries with no recognizable time
to be placed before those with times; 9999 would place entries
with no recognizable time after those with times."
  :type 'integer
  :group 'diary
  :version "20.3")

(defcustom diary-mail-addr
  (or (bound-and-true-p user-mail-address) "")
  "Email address that `diary-mail-entries' will send email to."
  :group 'diary
  :type  'string
  :version "20.3")

(defcustom diary-mail-days 7
  "Default number of days for `diary-mail-entries' to check."
  :group 'diary
  :type 'integer
  :version "20.3")

(defcustom diary-remind-message
  '("Reminder: Only "
    (if (zerop (% days 7))
        (format "%d week%s" (/ days 7) (if (= 7 days) "" "s"))
      (format "%d day%s" days (if (= 1 days) "" "s")))
    " until "
    diary-entry)
  "Pseudo-pattern giving form of reminder messages in the fancy diary display.

Used by the function `diary-remind', a pseudo-pattern is a list of
expressions that can involve the keywords `days' (a number), `date'
\(a list of month, day, year), and `diary-entry' (a string)."
  :type 'sexp
  :group 'diary)

(defcustom diary-abbreviated-year-flag t
  "Interpret a two-digit year DD in a diary entry as either 19DD or 20DD.
This applies to the Gregorian, Hebrew, Islamic, and Bahá'í calendars.
When the current century is added to a two-digit year, if the result
is more than 50 years in the future, the previous century is assumed.
If the result is more than 50 years in the past, the next century is assumed.
If this variable is nil, years must be written in full."
  :type 'boolean
  :group 'diary)

(defun diary-outlook-format-1 (body)
  "Return a replace-match template for an element of `diary-outlook-formats'.
Returns a string using match elements 1-5, where:
1 = month name, 2 = day, 3 = year, 4 = time, 5 = location; also uses
%s = message subject.  BODY is the string from which the matches derive."
  (let* ((monthname (match-string 1 body))
        (day (match-string 2 body))
        (year (match-string 3 body))
        ;; Blech.
        (month (catch 'found
                 (dotimes (i (length calendar-month-name-array))
                   (if (string-equal (aref calendar-month-name-array i)
                                     monthname)
                       (throw 'found (1+ i))))
                 nil)))
    ;; If we could convert the monthname to a numeric month, we can
    ;; use the standard function calendar-date-string.
    (concat (if month
                (calendar-date-string (list month (string-to-number day)
                                            (string-to-number year)) nil t)
              (cond ((eq calendar-date-style 'iso) "\\3 \\1 \\2") ; YMD
                    ((eq calendar-date-style 'european) "\\2 \\1 \\3") ; DMY
                    (t "\\1 \\2 \\3"))) ; MDY
            "\n \\4 %s, \\5")))
;; TODO Sometimes the time is in a different time-zone to the one you
;; are in.  Eg in PST, you might still get an email referring to:
;; "7:00 PM-8:00 PM. Greenwich Standard Time".
;; Note that it doesn't use a standard abbreviation for the timezone,
;; or anything helpful like that.
;; Sigh, this could cause the meeting to even be on a different day
;; to that given in the When: string.
;; These things seem to come in a multipart mail with a calendar part,
;; it's probably better to use that rather than this whole thing.
;; So this is unlikely to get improved.

;; TODO Is the format of these messages actually documented anywhere?
(defcustom diary-outlook-formats
  '(;; When: Tuesday, November 9, 2010 7:00 PM-8:00 PM. Greenwich Standard Time
    ;; Where: Meeting room B
    ("[ \t\n]*When: [[:alpha:]]+, \\([[:alpha:]]+\\) \\([0-9][0-9]*\\), \
\\([0-9]\\{4\\}\\),? \\(.+\\)\n\
\\(?:Where: \\(.+\n\\)\\)?" . diary-outlook-format-1))
  "Alist of regexps matching message text and replacement text.

The regexp must match the start of the message text containing an
appointment, but need not include a leading `^'.  If it matches the
current message, a diary entry is made from the corresponding
template.  If the template is a string, it should be suitable for
passing to `replace-match', and so will have occurrences of `\\D' to
substitute the match for the Dth subexpression.  It must also contain
a single `%s' which will be replaced with the text of the message's
Subject field.  Any other `%' characters must be doubled, so that the
template can be passed to `format'.

If the template is actually a function, it is called with the message
body text as argument, and may use `match-string' etc. to make a
template following the rules above."
  :type '(alist :key-type (regexp :tag "Regexp matching time/place")
                :value-type (choice
                             (string :tag "Template for entry")
                             (function :tag
                                       "Unary function providing template")))
  :version "22.1"
  :group 'diary)

(defvar diary-header-line-flag)
(defvar diary-header-line-format)

(defun diary-set-header (symbol value)
  "Set SYMBOL's value to VALUE, and redraw the diary header if necessary."
  (let ((oldvalue (symbol-value symbol))
        (dbuff (and diary-file (find-buffer-visiting diary-file))))
    (custom-set-default symbol value)
    (and dbuff
         (not (equal value oldvalue))
         (with-current-buffer dbuff
           (if (eq major-mode 'diary-mode)
               (setq header-line-format (and diary-header-line-flag
                                             diary-header-line-format)))))))

;; This can be removed once the kill/yank treatment of invisible text
;; (see etc/TODO) is fixed. -- gm
(defcustom diary-header-line-flag t
  "Non-nil means `diary-simple-display' will show a header line.
The format of the header is specified by `diary-header-line-format'."
  :group   'diary
  :type    'boolean
  :initialize 'custom-initialize-default
  :set 'diary-set-header
  :version "22.1")

(defvar diary-selective-display nil
  "Internal diary variable; non-nil if some diary text is hidden.")

(defcustom diary-header-line-format
  '(:eval (calendar-string-spread
           (list (if diary-selective-display
                     "Some text is hidden - press \"C-c C-s\" before edit/copy"
                   "Diary"))
           ?\s (window-width)))
  "Format of the header line displayed by `diary-simple-display'.
Only used if `diary-header-line-flag' is non-nil."
  :group 'diary
  :type 'sexp
  :initialize 'custom-initialize-default
  :set 'diary-set-header
  :version "23.3")                      ; frame-width -> window-width

;; The first version of this also checked for diary-selective-display
;; in the non-fancy case. This was an attempt to distinguish between
;; displaying the diary and just visiting the diary file. However,
;; when using fancy diary, calling diary when there are no entries to
;; display does not create the fancy buffer, nor does it set
;; diary-selective-display in the diary buffer. This means some
;; customizations will not take effect, eg:
;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00466.html
;; So the check for diary-selective-display was dropped. This means the
;; diary will be displayed if one customizes a diary variable while
;; just visiting the diary-file. This is i) unlikely, and ii) no great loss.
;;;###cal-autoload
(defun diary-live-p ()
  "Return non-nil if the diary is being displayed.
The actual return value is a diary buffer."
  (or (get-buffer diary-fancy-buffer)
      (and diary-file (find-buffer-visiting diary-file))))

;;;###cal-autoload
(defun diary-set-maybe-redraw (symbol value)
  "Set SYMBOL's value to VALUE, and redraw the diary if necessary.
Redraws the diary if it is being displayed (note this is not the same as
just visiting the `diary-file'), and SYMBOL's value is to be changed."
  (let ((oldvalue (symbol-value symbol)))
    (custom-set-default symbol value)
    (and (not (equal value oldvalue))
         (diary-live-p)
         ;; Note this assumes diary was called without prefix arg.
         (diary))))

(defcustom diary-number-of-entries 1
  "Specifies how many days of diary entries are to be displayed initially.
This variable affects the diary display when the command \\[diary] is
used, or if the value of the variable `calendar-view-diary-initially-flag'
is non-nil.  For example, if the default value 1 is used, then only the
current day's diary entries will be displayed.  If the value 2 is used,
then both the current day's and the next day's entries will be displayed.

The value can also be a vector such as [0 2 2 2 2 4 1]; this value says
to display no diary entries on Sunday, the entries for the current date
and the day after on Monday through Thursday, Friday through Monday's
entries on Friday, and only Saturday's entries on Saturday.

This variable does not affect the diary display with the `d' command
from the calendar; in that case, the prefix argument controls the number
of days of diary entries displayed."
  :type '(choice (integer :tag "Entries")
                 (vector :value [0 0 0 0 0 0 0]
                         (integer :tag "Sunday")
                         (integer :tag "Monday")
                         (integer :tag "Tuesday")
                         (integer :tag "Wednesday")
                         (integer :tag "Thursday")
                         (integer :tag "Friday")
                         (integer :tag "Saturday")))
  :initialize 'custom-initialize-default
  :set 'diary-set-maybe-redraw
  :group 'diary)

;;; More user options in calendar.el, holidays.el.


(defun diary-check-diary-file ()
  "Check that the file specified by `diary-file' exists and is readable.
If so, return the expanded file name, otherwise signal an error."
  (if (and diary-file (file-exists-p diary-file))
      (if (file-readable-p diary-file)
          diary-file
        (error "Diary file ‘%s’ is not readable" diary-file))
    (error "Diary file ‘%s’ does not exist" diary-file)))

;;;###autoload
(defun diary (&optional arg)
  "Generate the diary window for ARG days starting with the current date.
If no argument is provided, the number of days of diary entries is governed
by the variable `diary-number-of-entries'.  A value of ARG less than 1
does nothing.  This function is suitable for execution in an init file."
  (interactive "P")
  (diary-check-diary-file)
  (diary-list-entries (calendar-current-date)
                      (if arg (prefix-numeric-value arg))))

;;;###cal-autoload
(defun diary-view-entries (&optional arg)
  "Prepare and display a buffer with diary entries.
Searches the file named in `diary-file' for entries that match
ARG days starting with the date indicated by the cursor position
in the displayed three-month calendar."
  (interactive "p")
  (diary-check-diary-file)
  (diary-list-entries (calendar-cursor-to-date t) arg))


;;;###cal-autoload
(defun diary-view-other-diary-entries (arg dfile)
  "Prepare and display buffer of diary entries from an alternative diary file.
Searches for entries that match ARG days, starting with the date indicated
by the cursor position in the displayed three-month calendar.
DFILE specifies the file to use as the diary file."
  (interactive
   (list (prefix-numeric-value current-prefix-arg)
         (read-file-name "Enter diary file name: " default-directory nil t)))
  (let ((diary-file dfile))
    (diary-view-entries arg)))

(defvar diary-syntax-table
  (let ((st (copy-syntax-table (standard-syntax-table))))
    (modify-syntax-entry ?* "w" st)
    (modify-syntax-entry ?: "w" st)
    st)
  "The syntax table used when parsing dates in the diary file.
It is the standard syntax table used in Fundamental mode, but with the
syntax of `*' and `:' changed to be word constituents.")

(defun diary-attrtype-convert (attrvalue type)
  "Convert string ATTRVALUE to TYPE appropriate for a face description.
Valid TYPEs are: string, symbol, int, stringtnil, tnil."
  (cond ((eq type 'string) attrvalue)
        ((eq type 'symbol) (intern-soft attrvalue))
        ((eq type 'int) (string-to-number attrvalue))
        ((eq type 'stringtnil)
         (cond ((string-equal "t" attrvalue) t)
               ((string-equal "nil" attrvalue) nil)
               (t attrvalue)))
        ((eq type 'tnil) (string-equal "t" attrvalue))))

(defun diary-pull-attrs (entry fileglobattrs)
  "Search for matches for regexps from `diary-face-attrs'.
If ENTRY is nil, searches from the start of the current buffer, and
prepends all regexps with `diary-glob-file-regexp-prefix'.
If ENTRY is a string, search for matches in that string, and remove them.
Returns a list of ENTRY followed by (ATTRIBUTE VALUE) pairs.
When ENTRY is non-nil, FILEGLOBATTRS forms the start of the (ATTRIBUTE VALUE)
pairs."
  (let (regexp regnum attrname attrname attrvalue type ret-attr)
    (if (null entry)
        (save-excursion
          (dolist (attr diary-face-attrs)
            ;; FIXME inefficient searching.
            (goto-char (point-min))
            (setq regexp (concat diary-glob-file-regexp-prefix (car attr))
                  regnum (cadr attr)
                  attrname (nth 2 attr)
                  type (nth 3 attr)
                  attrvalue (if (re-search-forward regexp nil t)
                                (match-string-no-properties regnum)))
            (and attrvalue
                 (setq attrvalue (diary-attrtype-convert attrvalue type))
                 (setq ret-attr (append ret-attr
                                        (list attrname attrvalue))))))
      (setq ret-attr fileglobattrs)
      (dolist (attr diary-face-attrs)
        (setq regexp (car attr)
              regnum (cadr attr)
              attrname (nth 2 attr)
              type (nth 3 attr)
              attrvalue nil)
        ;; If multiple matches, replace all, use the last (which may
        ;; be the first instance in the line, if the regexp is
        ;; anchored with $).
        (while (string-match regexp entry)
          (setq attrvalue (match-string-no-properties regnum entry)
                entry (replace-match "" t t entry)))
        (and attrvalue
             (setq attrvalue (diary-attrtype-convert attrvalue type))
             (setq ret-attr (append ret-attr (list attrname attrvalue))))))
    (list entry ret-attr)))



(defvar diary-modify-entry-list-string-function nil
  "Function applied to entry string before putting it into the entries list.
Can be used by programs integrating a diary list into other buffers (e.g.
org.el and planner.el) to modify the string or add properties to it.
The function takes a string argument and must return a string.")

(defvar diary-entries-list)             ; bound in diary-list-entries

(defun diary-add-to-list (date string specifier &optional marker
                               globcolor literal)
  "Add an entry to `diary-entries-list'.
Do nothing if DATE or STRING are nil.  DATE is the (MONTH DAY
YEAR) for which the entry applies; STRING is the text of the
entry as it will appear in the diary (i.e. with any format
strings such as \"%d\" expanded); SPECIFIER is the date part of
the entry as it appears in the diary-file; LITERAL is the entry
as it appears in the diary-file (i.e. before expansion).
If LITERAL is nil, it is taken to be the same as STRING.

The entry is added to the list as (DATE STRING SPECIFIER LOCATOR
GLOBCOLOR), where LOCATOR has the form (MARKER FILENAME LITERAL),
FILENAME being the file containing the diary entry.

Modifies STRING using `diary-modify-entry-list-string-function', if non-nil.
Also removes the region between `diary-comment-start' and
`diary-comment-end', if the former is non-nil."
  (when (and date string)
    ;; b-f-n is nil if we are visiting an include file in a temp-buffer.
    (let ((dfile (or (buffer-file-name) diary-file))
          cstart)
      (if diary-file-name-prefix
          (let ((prefix (funcall diary-file-name-prefix-function dfile)))
            (or (string-equal prefix "")
                (setq string (format "[%s] %s" prefix string)))))
      (and diary-modify-entry-list-string-function
           (setq string (funcall diary-modify-entry-list-string-function
                                 string)))
      (when (and diary-comment-start
                 (string-match (setq cstart (regexp-quote diary-comment-start))
                               string))
        ;; Preserve the value with the comments.
        (or literal (setq literal string))
        ;; Handles multiple comments per entry, so long as each is on
        ;; a single line, and each line has no more than one comment.
        (setq string (replace-regexp-in-string
                      (format "%s.*%s" cstart (regexp-quote diary-comment-end))
                      "" string)))
      (setq diary-entries-list
            (append diary-entries-list
                    (list (list date string specifier
                                (list marker dfile literal)
                                globcolor)))))))

(defun diary-list-entries-2 (date mark globattr list-only
                                  &optional months symbol gdate)
  "Internal subroutine of `diary-list-entries'.
Find diary entries applying to DATE, by searching from point-min for
each element of `diary-date-forms'.  MARK indicates an entry is non-marking.
GLOBATTR is the list of global file attributes.  If LIST-ONLY is
non-nil, don't change the buffer, only return a list of entries.
Optional array MONTHS replaces `calendar-month-name-array', and
means months cannot be abbreviated.  Optional string SYMBOL marks diary
entries of the desired type.  If DATE is not Gregorian, then the
Gregorian equivalent should be provided via GDATE.  Returns non-nil if
any entries were found."
  (let* ((month (calendar-extract-month date))
         (day (calendar-extract-day date))
         (year (calendar-extract-year date))
         (dayname (format "%s\\|%s\\.?" (calendar-day-name date)
                          (calendar-day-name date 'abbrev)))
         (calendar-month-name-array (or months calendar-month-name-array))
         (monthname (format "\\*\\|%s%s" (calendar-month-name month)
                            (if months ""
                              (format "\\|%s\\.?"
                                      (calendar-month-name month 'abbrev)))))
         (month (format "\\*\\|0*%d" month))
         (day (format "\\*\\|0*%d" day))
         (year (format "\\*\\|0*%d%s" year
                       (if diary-abbreviated-year-flag
                           (format "\\|%02d" (% year 100))
                         "")))
        (case-fold-search t)
        entry-found)
    (dolist (date-form diary-date-forms)
      (let ((backup (when (eq (car date-form) 'backup)
                      (setq date-form (cdr date-form))
                      t))
            ;; date-form uses day etc as set above.
            (regexp (format "^%s?%s\\(%s\\)" (regexp-quote mark)
                            (if symbol (regexp-quote symbol) "")
                            (mapconcat 'eval date-form "\\)\\(?:")))
            entry-start date-start temp)
        (goto-char (point-min))
        (while (re-search-forward regexp nil t)
          (if backup (re-search-backward "\\<" nil t))
          ;; regexp moves us past the end of date, onto the next line.
          ;; Trailing whitespace after date not allowed (see diary-file).
          (if (and (bolp) (not (looking-at "[ \t]")))
              ;; Diary entry that consists only of date.
              (backward-char 1)
            ;; Found a nonempty diary entry--make it
            ;; visible and add it to the list.
            (setq date-start (line-end-position 0))
            ;; Actual entry starts on the next-line?
            (if (looking-at "[ \t]*\n[ \t]") (forward-line 1))
            (setq entry-found t
                  entry-start (point))
            (forward-line 1)
            (while (looking-at "[ \t]") ; continued entry
              (forward-line 1))
            (unless (and (eobp) (not (bolp)))
              (backward-char 1))
            (unless list-only
              (remove-overlays date-start (point) 'invisible 'diary))
            (setq temp (diary-pull-attrs
                        (buffer-substring-no-properties
                         entry-start (point)) globattr))
            (diary-add-to-list
             (or gdate date) (car temp)
             (buffer-substring-no-properties (1+ date-start) (1- entry-start))
             (copy-marker entry-start) (cadr temp))))))
    entry-found))

(defvar original-date)                  ; from diary-list-entries
(defvar file-glob-attrs)
(defvar list-only)
(defvar number)

(defun diary-list-entries-1 (months symbol absfunc)
  "List diary entries of a certain type.
MONTHS is an array of month names.  SYMBOL marks diary entries of the type
in question.  ABSFUNC is a function that converts absolute dates to dates
of the appropriate type."
  (let ((gdate original-date))
    (dotimes (_idummy number)
      (diary-list-entries-2
       (funcall absfunc (calendar-absolute-from-gregorian gdate))
       diary-nonmarking-symbol file-glob-attrs list-only months symbol gdate)
      (setq gdate
            (calendar-gregorian-from-absolute
             (1+ (calendar-absolute-from-gregorian gdate))))))
  (goto-char (point-min)))

(defvar diary-included-files nil
  "List of any diary files included in the last call to `diary-list-entries'.
Or to `diary-mark-entries'.")

(defun diary-list-entries (date number &optional list-only)
  "Create and display a buffer containing the relevant lines in `diary-file'.
Selects entries for NUMBER days starting with date DATE.  Hides any
other entries using overlays.  If NUMBER is less than 1, this function
does nothing.

Returns a list of all relevant diary entries found.
The list entries have the form ((MONTH DAY YEAR) STRING SPECIFIER) where
\(MONTH DAY YEAR) is the date of the entry, STRING is the entry text, and
SPECIFIER is the applicability.  If the variable `diary-list-include-blanks'
is non-nil, this list includes a dummy diary entry consisting of the empty
string for a date with no diary entries.

If producing entries for multiple dates (i.e., NUMBER > 1), then
this function normally returns the entries from any given diary
file in date order.  The entries for any given day are in the
order in which they were found in the file, not necessarily in
time-of-day order.  Note that any functions present on the
hooks (see below) may add entries, or change the order.  For
example, `diary-include-other-diary-files' adds entries from any
include files that it finds to the end of the original list.  The
entries from each file will be in date order, but the overall
list will not be.  If you want the entire list to be in time
order, add `diary-sort-entries' to the end of `diary-list-entries-hook'.

After preparing the initial list, hooks run in this order:

  `diary-nongregorian-listing-hook' runs for the main diary file,
      and each included file.  For example, this is the appropriate hook
      to process Islamic entries in all diary files.

  `diary-list-entries-hook' runs once only, for the main diary file.
      For example, this is appropriate for sorting all the entries.
      If not using include files, there is no difference from the previous
      hook.

  `diary-hook' runs last, after the diary is displayed.
      This is used e.g. by `appt-check'.

Functions called by these hooks may use the variables ORIGINAL-DATE
and NUMBER, which are the arguments with which this function was called.
Note that hook functions should _not_ use DATE, but ORIGINAL-DATE.
\(Sexp diary entries may use DATE - see `diary-list-sexp-entries'.)

This function displays the list using `diary-display-function', unless
LIST-ONLY is non-nil, in which case it just returns the list."
  (unless number
    (setq number (if (vectorp diary-number-of-entries)
                     (aref diary-number-of-entries (calendar-day-of-week date))
                   diary-number-of-entries)))
  (when (> number 0)
    (let* ((original-date date)    ; save for possible use in the hooks
           (date-string (calendar-date-string date))
           (diary-buffer (find-buffer-visiting diary-file))
           ;; Dynamically bound in diary-include-files.
           (d-incp (and (boundp 'diary-including) diary-including))
           diary-entries-list file-glob-attrs temp-buff)
      (unless d-incp
        (setq diary-included-files nil)
        (message "Preparing diary..."))
      (unwind-protect
          (with-current-buffer (or diary-buffer
                                   (if list-only
                                       (setq temp-buff (generate-new-buffer
                                                        " *diary-temp*"))
                                     (find-file-noselect diary-file t)))
            (if diary-buffer
                (or (verify-visited-file-modtime diary-buffer)
                    (revert-buffer t t)))
            (if temp-buff
                ;; If including, caller has already verified it is readable.
                (insert-file-contents diary-file)
              ;; Setup things like the header-line-format and invisibility-spec.
              (if (eq major-mode (default-value 'major-mode))
                  (diary-mode)
                ;; This kludge is to make customizations to
                ;; diary-header-line-flag after diary has been displayed
                ;; take effect. Unconditionally calling (diary-mode)
                ;; clobbers file local variables.
                ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00363.html
                ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-04/msg00404.html
                (if (eq major-mode 'diary-mode)
                    (setq header-line-format (and diary-header-line-flag
                                                  diary-header-line-format)))))
            ;; d-s-p is passed to the diary display function.
            (let ((diary-saved-point (point)))
              (save-excursion
                (save-restriction
                  (widen)                   ; bug#5093
                  (setq file-glob-attrs (cadr (diary-pull-attrs nil "")))
                  (with-syntax-table diary-syntax-table
                    (goto-char (point-min))
                    (unless list-only
                      (let ((ol (make-overlay (point-min) (point-max) nil t nil)))
                        (set (make-local-variable 'diary-selective-display) t)
                        (overlay-put ol 'invisible 'diary)
                        (overlay-put ol 'evaporate t)))
                    (dotimes (_idummy number)
                      (let ((sexp-found (diary-list-sexp-entries date))
                            (entry-found (diary-list-entries-2
                                          date diary-nonmarking-symbol
                                          file-glob-attrs list-only)))
                        (if diary-list-include-blanks
                            (or sexp-found entry-found
                                (diary-add-to-list date "" "" "" "")))
                        (setq date
                              (calendar-gregorian-from-absolute
                               (1+ (calendar-absolute-from-gregorian date)))))))
                  (goto-char (point-min))
                  ;; Although it looks like list-entries-hook runs
                  ;; every time, diary-include-other-diary-files
                  ;; binds it to nil (essentially) when it runs
                  ;; in included files.
                  (run-hooks 'diary-nongregorian-listing-hook
                             'diary-list-entries-hook)
                  ;; We could make this explicit:
                  ;;; (run-hooks 'diary-nongregorian-listing-hook)
                  ;;; (if d-incp
                  ;;;     (diary-include-other-diary-files) ; recurse
                  ;;;   (run-hooks 'diary-list-entries-hook))
                  (unless list-only
                    ;; Avoid M-x diary; M-x calendar; M-x diary
                    ;; clobbering the calendar window.
                    ;; FIXME this is not the right solution.
                    (let ((display-buffer-fallback-action
                           (list (delq
                                  'display-buffer-in-previous-window
                                  (copy-sequence
                                   (car display-buffer-fallback-action))))))
                      (funcall diary-display-function)))
                  (run-hooks 'diary-hook)))))
        (and temp-buff (buffer-name temp-buff) (kill-buffer temp-buff)))
      (or d-incp (message "Preparing diary...done"))
      diary-entries-list)))

(defun diary-unhide-everything ()
  "Show all invisible text in the diary."
  (kill-local-variable 'diary-selective-display)
  (save-restriction                     ; bug#5477
    (widen)
    (remove-overlays (point-min) (point-max) 'invisible 'diary))
  (kill-local-variable 'mode-line-format))

(defvar original-date)                  ; bound in diary-list-entries
;(defvar number)                         ; already declared above

(defun diary-include-files (&optional mark)
  "Process diary entries from included diary files.
By default, lists included entries, but if optional argument MARK is non-nil
marks entries instead.
For example, this enables you to share common diary files.
Specify include files using lines matching `diary-include-string', e.g.
    #include \"filename\"
This is recursive; that is, included files may include other files."
  (goto-char (point-min))
  (while (re-search-forward
          (format "^%s \"\\([^\"]*\\)\"" (regexp-quote diary-include-string))
          nil t)
    (let ((diary-file (match-string-no-properties 1))
          (diary-mark-entries-hook 'diary-mark-included-diary-files)
          (diary-list-entries-hook 'diary-include-other-diary-files)
          (diary-including t)
          diary-hook diary-list-include-blanks efile)
      (if (file-exists-p diary-file)
          (if (file-readable-p diary-file)
              (if (member (setq efile (expand-file-name diary-file))
                          diary-included-files)
                  (error "Recursive diary include for %s" diary-file)
                (setq diary-included-files
                      (append diary-included-files (list efile)))
                (if mark
                    (diary-mark-entries)
                  (setq diary-entries-list
                        (append diary-entries-list
                                (diary-list-entries original-date number t)))))
            (display-warning
             :error
             (format "Can't read included diary file %s\n" diary-file)))
        (display-warning
         :error
         (format "Can't find included diary file %s\n" diary-file)))))
  (goto-char (point-min)))

(defun diary-include-other-diary-files ()
  "Add diary entries from included diary files to `diary-entries-list'.
To use, add this function to `diary-list-entries-hook'.
For details, see `diary-include-files'.
See also `diary-mark-included-diary-files'."
  (diary-include-files))

(defvar date-string)                    ; bound in diary-list-entries

(defun diary-display-no-entries ()
  "Common subroutine of `diary-simple-display' and `diary-fancy-display'.
Handles the case where there are no diary entries.
Returns a cons (NOENTRIES . HOLIDAY-STRING)."
    (let* ((holiday-list (if diary-show-holidays-flag
                             (calendar-check-holidays original-date)))
           (hol-string (format "%s%s%s"
                               date-string
                               (if holiday-list ": " "")
                               (mapconcat 'identity holiday-list "; ")))
           (msg (format "No diary entries for %s" hol-string))
           ;; Empty list, or single item with no text.
           ;; FIXME multiple items with no text?
           (noentries (or (not diary-entries-list)
                          (and (not (cdr diary-entries-list))
                               (string-equal "" (cadr
                                                 (car diary-entries-list)))))))
      ;; Inconsistency: whether or not the holidays are displayed in a
      ;; separate buffer depends on if there are diary entries.
      (when noentries
        (if (or (< (length msg) (frame-width))
                (not holiday-list))
            (message "%s" msg)
          ;; holiday-list which is too wide for a message gets a buffer.
          (calendar-in-read-only-buffer holiday-buffer
            (calendar-set-mode-line (format "Holidays for %s" date-string))
            (insert (mapconcat 'identity holiday-list "\n")))
          (message "No diary entries for %s" date-string)))
      (cons noentries hol-string)))


(defvar diary-saved-point)              ; bound in diary-list-entries

(defun diary-simple-display ()
  "Display the diary buffer if there are any relevant entries or holidays.
Entries that do not apply are made invisible.  Holidays are shown
in the mode line.  This is an option for `diary-display-function'."
  ;; If selected window is dedicated (to the calendar), need a new one
  ;; to display the diary.
  (let* ((pop-up-frames (or pop-up-frames (window-dedicated-p)))
         (dbuff (find-buffer-visiting diary-file))
         (empty (diary-display-no-entries)))
    ;; This may be too wide, but when simple diary is used there is
    ;; nowhere else for the holidays to go.  Also, it is documented in
    ;; diary-show-holidays-flag that the holidays go in the mode-line.
    ;; FIXME however if there are no diary entries a separate buffer
    ;; is displayed - this is inconsistent.
    (with-current-buffer dbuff
      (calendar-set-mode-line (format "Diary for %s" (cdr empty))))
    (unless (car empty)                 ; no entries
      (with-current-buffer dbuff
        (let ((window (display-buffer (current-buffer))))
          ;; d-s-p is passed from diary-list-entries.
          (set-window-point window diary-saved-point)
          (set-window-start window (point-min)))))))

(defvar diary-goto-entry-function 'diary-goto-entry
  "Function called to jump to a diary entry.
Modes that require special handling of the included file
containing the diary entry can assign a suitable function to this
variable.")

(define-button-type 'diary-entry
  'action (lambda (button) (funcall diary-goto-entry-function button))
  'face 'diary-button 'help-echo "Find this diary entry"
  'follow-link t)

(defun diary-goto-entry (button)
  "Jump to the diary entry for the BUTTON at point."
  (let* ((locator (button-get button 'locator))
         (marker (car locator))
         markbuf file)
    ;; If marker pointing to diary location is valid, use that.
    (if (and marker (setq markbuf (marker-buffer marker)))
        (progn
          (pop-to-buffer markbuf)
          (goto-char (marker-position marker)))
      ;; Marker is invalid (eg buffer has been killed).
      (or (and (setq file (cadr locator))
               (file-exists-p file)
               (find-file-other-window file)
               (progn
                 (when (eq major-mode (default-value 'major-mode)) (diary-mode))
                 (goto-char (point-min))
                 (if (re-search-forward (format "%s.*\\(%s\\)"
                                                (regexp-quote (nth 2 locator))
                                                (regexp-quote (nth 3 locator)))
                                        nil t)
                     (goto-char (match-beginning 1)))))
          (message "Unable to locate this diary entry")))))

(defun diary-fancy-display ()
  "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
Holidays are shown unless `diary-show-holidays-flag' is nil.
Days with no diary entries are not shown (even if that day is a
holiday), unless `diary-list-include-blanks' is non-nil.

This is an option for `diary-display-function'."
  ;; Turn off selective-display in the diary file's buffer.
  (with-current-buffer (find-buffer-visiting diary-file)
    (diary-unhide-everything))
  (unless (car (diary-display-no-entries)) ; no entries
    ;; Prepare the fancy diary buffer.
    (calendar-in-read-only-buffer diary-fancy-buffer
      (calendar-set-mode-line "Diary Entries")
      (let ((holiday-list-last-month 1)
            (holiday-list-last-year 1)
            (date (list 0 0 0))
            holiday-list)
        (dolist (entry diary-entries-list)
          (unless (calendar-date-equal date (car entry))
            (setq date (car entry))
            (and diary-show-holidays-flag
                 (calendar-date-compare
                  (list (list holiday-list-last-month
                              (calendar-last-day-of-month
                               holiday-list-last-month
                               holiday-list-last-year)
                              holiday-list-last-year))
                  (list date))
                 ;; We need to get the holidays for the next 3 months.
                 (setq holiday-list-last-month
                       (calendar-extract-month date)
                       holiday-list-last-year
                       (calendar-extract-year date))
                 (progn
                   (calendar-increment-month
                    holiday-list-last-month holiday-list-last-year 1)
                   t)
                 (setq holiday-list
                       (let ((displayed-month holiday-list-last-month)
                             (displayed-year holiday-list-last-year))
                         (calendar-holiday-list)))
                 (calendar-increment-month
                  holiday-list-last-month holiday-list-last-year 1))
            (let ((longest 0)
                  date-holiday-list cc)
              ;; Make a list of all holidays for date.
              (dolist (h holiday-list)
                (if (calendar-date-equal date (car h))
                    (setq date-holiday-list (append date-holiday-list
                                                    (cdr h)))))
              (insert (if (bobp) "" ?\n) (calendar-date-string date))
              (if date-holiday-list (insert ":  "))
              (setq cc (current-column))
              (insert (mapconcat (lambda (x)
                                   (setq longest (max longest (length x)))
                                   x)
                                 date-holiday-list
                                 (concat "\n" (make-string cc ?\s))))
              (insert ?\n (make-string (+ cc longest) ?=) ?\n)))
          (let ((this-entry (cadr entry))
                this-loc marks temp-face)
            (unless (zerop (length this-entry))
              (if (setq this-loc (nth 3 entry))
                  (insert-button this-entry
                                 ;; (MARKER FILENAME SPECIFIER LITERAL)
                                 'locator (list (car this-loc)
                                                (cadr this-loc)
                                                (nth 2 entry)
                                                (or (nth 2 this-loc)
                                                    (nth 1 entry)))
                                 :type 'diary-entry)
                (insert this-entry))
              (insert ?\n)
              ;; Doesn't make sense to check font-lock-mode - see
              ;; comments above diary-entry-marker in calendar.el.
              (and ; font-lock-mode
                   (setq marks (nth 4 entry))
                   (save-excursion
                     (setq temp-face (calendar-make-temp-face marks))
                     (search-backward this-entry)
                     (overlay-put
                      (make-overlay (match-beginning 0) (match-end 0))
                      'face temp-face)))))))
      ;; FIXME can't remember what this check was for.
      ;; To prevent something looping, or a minor optimization?
      (if (eq major-mode 'diary-fancy-display-mode)
          (run-hooks 'diary-fancy-display-mode-hook)
        (diary-fancy-display-mode))
      (calendar-set-mode-line date-string))))

;; FIXME modernize?
(defun diary-print-entries ()
  "Print a hard copy of the diary display.

If the simple diary display is being used, prepare a temp buffer with the
visible lines of the diary buffer, add a heading line composed from the mode
line, print the temp buffer, and destroy it.

If the fancy diary display is being used, just print the buffer.

The hooks given by the variable `diary-print-entries-hook' are called to do
the actual printing."
  (interactive)
  (let ((diary-buffer (get-buffer diary-fancy-buffer))
        temp-buffer heading start end)
    (if diary-buffer
        (with-current-buffer diary-buffer
          (run-hooks 'diary-print-entries-hook))
      (or (setq diary-buffer (find-buffer-visiting diary-file))
          (error "You don't have a diary buffer!"))
      ;; Name affects printing?
      (setq temp-buffer (get-buffer-create " *Printable Diary Entries*"))
      (with-current-buffer diary-buffer
        (setq heading
              (if (not (stringp mode-line-format))
                  "All Diary Entries"
                (string-match "^-*\\([^-].*[^-]\\)-*$" mode-line-format)
                (match-string 1 mode-line-format))
              start (point-min))
        (while
            (progn
              (setq end (next-single-char-property-change start 'invisible))
              (unless (get-char-property start 'invisible)
                (with-current-buffer temp-buffer
                  (insert-buffer-substring diary-buffer start end)))
              (setq start end)
              (and end (< end (point-max))))))
      (set-buffer temp-buffer)
      (goto-char (point-min))
      (insert heading "\n"
              (make-string (length heading) ?=) "\n")
      (run-hooks 'diary-print-entries-hook)
      (kill-buffer temp-buffer))))

;;;###cal-autoload
(defun diary-show-all-entries ()
  "Show all of the diary entries in the diary file.
This function gets rid of the selective display of the diary file so that
all entries, not just some, are visible.  If there is no diary buffer, one
is created."
  (interactive)
  (let* ((d-file (diary-check-diary-file))
         (pop-up-frames (or pop-up-frames (window-dedicated-p)))
         (win (selected-window))
         (height (window-height)))
    (with-current-buffer (or (find-buffer-visiting d-file)
                             (find-file-noselect d-file t))
      (when (eq major-mode (default-value 'major-mode)) (diary-mode))
      (diary-unhide-everything)
      (display-buffer (current-buffer))
      (when (and (/= height (window-height win))
                 (with-current-buffer (window-buffer win)
                   (derived-mode-p 'calendar-mode)))
        (fit-window-to-buffer win)))))

;;;###autoload
(defun diary-mail-entries (&optional ndays)
  "Send a mail message showing diary entries for next NDAYS days.
If no prefix argument is given, NDAYS is set to `diary-mail-days'.
Mail is sent to the address specified by `diary-mail-addr'.

Here is an example of a script to call `diary-mail-entries',
suitable for regular scheduling using cron (or at).  Note that
since `emacs -script' does not load your init file, you should
ensure that all relevant variables are set.

#!/usr/bin/emacs -script
;; diary-rem.el - run the Emacs diary-reminder

\(setq diary-mail-days 3
      diary-file \"/path/to/diary.file\"
      calendar-date-style 'european
      diary-mail-addr \"user@host.name\")

\(diary-mail-entries)

# diary-rem.el ends here
"
  (interactive "P")
  (if (string-equal diary-mail-addr "")
      (user-error "You must set ‘diary-mail-addr’ to use this command")
    (let ((diary-display-function 'diary-fancy-display))
      (diary-list-entries (calendar-current-date) (or ndays diary-mail-days)))
    (compose-mail diary-mail-addr
                  (concat "Diary entries generated "
                          (calendar-date-string (calendar-current-date))))
    (insert
     (if (get-buffer diary-fancy-buffer)
         (with-current-buffer diary-fancy-buffer (buffer-string))
       "No entries found"))
    (call-interactively (get mail-user-agent 'sendfunc))))

(defun diary-name-pattern (string-array &optional abbrev-array paren)
  "Return a regexp matching the strings in the array STRING-ARRAY.
If the optional argument ABBREV-ARRAY is present, the regexp
also matches the supplied abbreviations, with or without final `.'
characters.  If the optional argument PAREN is non-nil, surrounds
the regexp with parentheses."
  (regexp-opt (append string-array
                      abbrev-array
                      (if abbrev-array
                          (mapcar (lambda (e) (format "%s." e))
                                  abbrev-array))
                      nil)
              paren))

(defvar diary-marking-entries-flag nil
  "True during the marking of diary entries, nil otherwise.")

(defvar diary-marking-entry-flag nil
  "True during the marking of diary entries, if current entry is marking.")

;; file-glob-attrs bound in diary-mark-entries.
(defun diary-mark-entries-1 (markfunc &optional months symbol absfunc)
  "Mark diary entries of a certain type.
MARKFUNC is a function that marks entries of the appropriate type
matching a given date pattern.  MONTHS is an array of month names.
SYMBOL marks diary entries of the type in question.  ABSFUNC is a
function that converts absolute dates to dates of the appropriate type.  "
  (let ((dayname (diary-name-pattern calendar-day-name-array
                                     calendar-day-abbrev-array))
        (monthname (format "%s\\|\\*"
                           (if months
                               (diary-name-pattern months)
                             (diary-name-pattern calendar-month-name-array
                                                 calendar-month-abbrev-array))))
        (month "[0-9]+\\|\\*")
        (day "[0-9]+\\|\\*")
        (year "[0-9]+\\|\\*")
        (case-fold-search t)
        marks)
    (dolist (date-form diary-date-forms)
      (if (eq (car date-form) 'backup)  ; ignore 'backup directive
          (setq date-form (cdr date-form)))
      (let* ((l (length date-form))
             (d-name-pos (- l (length (memq 'dayname date-form))))
             (d-name-pos (if (/= l d-name-pos) (1+ d-name-pos)))
             (m-name-pos (- l (length (memq 'monthname date-form))))
             (m-name-pos (if (/= l m-name-pos) (1+ m-name-pos)))
             (d-pos (- l (length (memq 'day date-form))))
             (d-pos (if (/= l d-pos) (1+ d-pos)))
             (m-pos (- l (length (memq 'month date-form))))
             (m-pos (if (/= l m-pos) (1+ m-pos)))
             (y-pos (- l (length (memq 'year date-form))))
             (y-pos (if (/= l y-pos) (1+ y-pos)))
             (regexp (format "^%s\\(%s\\)"
                             (if symbol (regexp-quote symbol) "")
                             (mapconcat 'eval date-form "\\)\\("))))
        (goto-char (point-min))
        (while (re-search-forward regexp nil t)
          (let* ((dd-name
                  (if d-name-pos
                      (match-string-no-properties d-name-pos)))
                 (mm-name
                  (if m-name-pos
                      (match-string-no-properties m-name-pos)))
                 (mm (string-to-number
                      (if m-pos
                          (match-string-no-properties m-pos)
                        "")))
                 (dd (string-to-number
                      (if d-pos
                          (match-string-no-properties d-pos)
                        "")))
                 (y-str (if y-pos
                            (match-string-no-properties y-pos)))
                 (yy (if (not y-str)
                         0
                       (if (and (= (length y-str) 2)
                                diary-abbreviated-year-flag)
                           (let* ((current-y
                                   (calendar-extract-year
                                    (if absfunc
                                        (funcall
                                         absfunc
                                         (calendar-absolute-from-gregorian
                                          (calendar-current-date)))
                                      (calendar-current-date))))
                                  (y (+ (string-to-number y-str)
                                        ;; Current century, eg 2000.
                                        (* 100 (/ current-y 100))))
                                  (offset (- y current-y)))
                             ;; Add 2-digit year to current century.
                             ;; If more than 50 years in the future,
                             ;; assume last century. If more than 50
                             ;; years in the past, assume next century.
                             (if (> offset 50)
                                 (- y 100)
                               (if (< offset -50)
                                   (+ y 100)
                                 y)))
                         (string-to-number y-str)))))
            (setq marks (cadr (diary-pull-attrs
                               (buffer-substring-no-properties
                                (point) (line-end-position))
                               file-glob-attrs)))
            ;; Only mark all days of a given name if the pattern
            ;; contains no more specific elements.
            (if (and dd-name (not (or d-pos m-pos y-pos)))
                (calendar-mark-days-named
                 (cdr (assoc-string dd-name
                                    (calendar-make-alist
                                     calendar-day-name-array
                                     0 nil calendar-day-abbrev-array
                                     (mapcar (lambda (e)
                                               (format "%s." e))
                                             calendar-day-abbrev-array))
                                    t)) marks)
              (if mm-name
                  (setq mm
                        (if (string-equal mm-name "*") 0
                          (cdr (assoc-string
                                mm-name
                                (if months (calendar-make-alist months)
                                  (calendar-make-alist
                                   calendar-month-name-array
                                   1 nil calendar-month-abbrev-array
                                   (mapcar (lambda (e)
                                             (format "%s." e))
                                           calendar-month-abbrev-array)))
                                t)))))
              (funcall markfunc mm dd yy marks))))))))

;;;###cal-autoload
(defun diary-mark-entries (&optional redraw)
  "Mark days in the calendar window that have diary entries.
Marks each entry in the diary that is visible in the calendar window.

After marking the entries, runs `diary-nongregorian-marking-hook'
for the main diary file, and each included file.  For example,
this is the appropriate hook to process Islamic entries in all
diary files.  Next `diary-mark-entries-hook' runs, for the main diary
file only.  If not using include files, there is no difference between
these two hooks.

If the optional argument REDRAW is non-nil (which is the case
interactively, for example) then this first removes any existing diary
marks.  This is intended to deal with deleted diary entries."
  (interactive "p")
  ;; To remove any deleted diary entries. Do not redraw when:
  ;; i) processing #include diary files (else only get the marks from
  ;; the last #include file processed).
  ;; ii) called via calendar-redraw (since calendar has already been
  ;; erased).
  ;; Use of REDRAW handles both of these cases.
  (when (and redraw calendar-mark-diary-entries-flag)
    (setq calendar-mark-diary-entries-flag nil)
    (calendar-redraw))
  (let ((diary-marking-entries-flag t)
        (diary-buffer (find-buffer-visiting diary-file))
        ;; Dynamically bound in diary-include-files.
        (d-incp (and (boundp 'diary-including) diary-including))
        file-glob-attrs temp-buff)
    (unless d-incp
      (setq diary-included-files nil)
      (message "Marking diary entries..."))
    (unwind-protect
        (with-current-buffer (or diary-buffer
                                 (if d-incp
                                     (setq temp-buff (generate-new-buffer
                                                        " *diary-temp*"))
                                   (find-file-noselect
                                    (diary-check-diary-file) t)))
          (if temp-buff
              ;; If including, caller has already verified it is readable.
              (insert-file-contents diary-file)
            (if (eq major-mode (default-value 'major-mode)) (diary-mode)))
          (setq calendar-mark-diary-entries-flag t)
          (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
          (with-syntax-table diary-syntax-table
            (save-excursion
              (diary-mark-entries-1 'calendar-mark-date-pattern)
              (diary-mark-sexp-entries)
              ;; Although it looks like mark-entries-hook runs every time,
              ;; diary-mark-included-diary-files binds it to nil
              ;; (essentially) when it runs in included files.
              (run-hooks 'diary-nongregorian-marking-hook
                         'diary-mark-entries-hook))))
      (and temp-buff (buffer-name temp-buff) (kill-buffer temp-buff)))
    (or d-incp (message "Marking diary entries...done"))))

(defun diary-sexp-entry (sexp entry date)
  "Process a SEXP diary ENTRY for DATE."
  (let ((result (if calendar-debug-sexp
                    (let ((debug-on-error t))
                      (eval (car (read-from-string sexp))))
                  (let (err)
                    (condition-case err
                        (eval (car (read-from-string sexp)))
                      (error
                       (display-warning
                        :error
                        (format "Bad diary sexp at line %d in %s:\n%s\n\
Error: %s\n"
                                (count-lines (point-min) (point))
                                diary-file sexp err))
                       nil))))))
    (cond ((stringp result) result)
          ((and (consp result)
                (stringp (cdr result))) result)
          (result entry)
          (t nil))))

(defvar displayed-year)                 ; bound in calendar-generate
(defvar displayed-month)

(defun diary-mark-sexp-entries ()
  "Mark days in the calendar window that have sexp diary entries.
Each entry in the diary file (or included files) visible in the calendar window
is marked.  See the documentation for the function `diary-list-sexp-entries'."
  (let* ((sexp-mark (regexp-quote diary-sexp-entry-symbol))
         (s-entry (format "^\\(%s(\\)\\|\\(%s%s(diary-remind\\)" sexp-mark
                          (regexp-quote diary-nonmarking-symbol)
                          sexp-mark))
         (file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
         m y first-date last-date date mark file-glob-attrs
         sexp-start sexp entry entry-start)
    (with-current-buffer calendar-buffer
      (setq m displayed-month
            y displayed-year))
    (calendar-increment-month m y -1)
    (setq first-date (calendar-absolute-from-gregorian (list m 1 y)))
    (calendar-increment-month m y 2)
    (setq last-date
          (calendar-absolute-from-gregorian
           (list m (calendar-last-day-of-month m y) y)))
    (goto-char (point-min))
    (while (re-search-forward s-entry nil t)
      (setq diary-marking-entry-flag (char-equal (preceding-char) ?\())
      (re-search-backward "(")
      (setq sexp-start (point))
      (forward-sexp)
      (setq sexp (buffer-substring-no-properties sexp-start (point)))
      (forward-char 1)
      (if (and (bolp) (not (looking-at "[ \t]")))
          ;; Diary entry consists only of the sexp.
          (progn
            (backward-char 1)
            (setq entry ""))
        (setq entry-start (point))
        ;; Find end of entry.
        (forward-line 1)
        (while (looking-at "[ \t]")
          (forward-line 1))
        (if (bolp) (backward-char 1))
        (setq entry (buffer-substring-no-properties entry-start (point))))
      (setq date (1- first-date))
      ;; FIXME this loops over all visible dates.
      ;; Could be optimized in many cases. Depends on whether t or * present.
      (while (<= (setq date (1+ date)) last-date)
        (when (setq mark (diary-sexp-entry
                          sexp entry
                          (calendar-gregorian-from-absolute date)))
          (calendar-mark-visible-date
           (calendar-gregorian-from-absolute date)
           (or (cadr (diary-pull-attrs entry file-glob-attrs))
               (if (consp mark) (car mark)))))))))

(defun diary-mark-included-diary-files ()
  "Mark diary entries from included diary files.
To use, add this function to `diary-mark-entries-hook'.
For details, see `diary-include-files'.
See also `diary-include-other-diary-files'."
  (diary-include-files t))

(defun calendar-mark-days-named (dayname &optional color)
  "Mark all dates in the calendar window that are day DAYNAME of the week.
0 means all Sundays, 1 means all Mondays, and so on.
Optional argument COLOR is passed to `calendar-mark-visible-date' as MARK."
  (with-current-buffer calendar-buffer
    (let ((prev-month displayed-month)
          (prev-year displayed-year)
          (succ-month displayed-month)
          (succ-year displayed-year)
          (last-day)
          (day))
      (calendar-increment-month succ-month succ-year 1)
      (calendar-increment-month prev-month prev-year -1)
      (setq day (calendar-absolute-from-gregorian
                 (calendar-nth-named-day 1 dayname prev-month prev-year))
            last-day (calendar-absolute-from-gregorian
                      (calendar-nth-named-day -1 dayname succ-month succ-year)))
      (while (<= day last-day)
        (calendar-mark-visible-date (calendar-gregorian-from-absolute day)
                                    color)
        (setq day (+ day 7))))))

(defun calendar-mark-month (month year p-month p-day p-year &optional color)
  "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P-DAY/P-YEAR.
A value of 0 in any position of the pattern is a wildcard.
Optional argument COLOR is passed to `calendar-mark-visible-date' as MARK."
  (if (or (and (= month p-month)
               (or (zerop p-year) (= year p-year)))
          (and (zerop p-month)
               (or (zerop p-year) (= year p-year))))
      (if (zerop p-day)
          (dotimes (i (calendar-last-day-of-month month year))
            (calendar-mark-visible-date (list month (1+ i) year) color))
        (calendar-mark-visible-date (list month p-day year) color))))

(defun calendar-mark-date-pattern (month day year &optional color)
  "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
A value of 0 in any position is a wildcard.  Optional argument COLOR is
passed to `calendar-mark-visible-date' as MARK."
  (with-current-buffer calendar-buffer
    (let ((m displayed-month)
          (y displayed-year))
      (calendar-increment-month m y -1)
      (dotimes (_idummy 3)
        (calendar-mark-month m y month day year color)
        (calendar-increment-month m y 1)))))

;; Bahai, Hebrew, Islamic.
(defun calendar-mark-complex (month day year fromabs &optional color)
  "Mark dates in the calendar conforming to MONTH DAY YEAR of some system.
The function FROMABS converts absolute dates to the appropriate date system.
Optional argument COLOR is passed to `calendar-mark-visible-date' as MARK."
  ;; Not one of the simple cases--check all visible dates for match.
  ;; Actually, the following code takes care of ALL of the cases, but
  ;; it's much too slow to be used for the simple (common) cases.
  (let* ((m displayed-month)
         (y displayed-year)
         (first-date (progn
                       (calendar-increment-month m y -1)
                       (calendar-absolute-from-gregorian (list m 1 y))))
         (last-date (progn
                      (calendar-increment-month m y 2)
                      (calendar-absolute-from-gregorian
                       (list m (calendar-last-day-of-month m y) y))))
         (date (1- first-date))
         local-date)
    (while (<= (setq date (1+ date)) last-date)
      (setq local-date (funcall fromabs date))
      (and (or (zerop month)
               (= month (calendar-extract-month local-date)))
           (or (zerop day)
               (= day (calendar-extract-day local-date)))
           (or (zerop year)
               (= year (calendar-extract-year local-date)))
           (calendar-mark-visible-date
            (calendar-gregorian-from-absolute date) color)))))

;; Bahai, Islamic.
(defun calendar-mark-1 (month day year fromabs toabs &optional color)
  "Mark dates in the calendar conforming to MONTH DAY YEAR of some system.
The function FROMABS converts absolute dates to the appropriate date system.
The function TOABS carries out the inverse operation.  Optional argument
COLOR is passed to `calendar-mark-visible-date' as MARK."
  (with-current-buffer calendar-buffer
    (if (and (not (zerop month)) (not (zerop day)))
        (if (not (zerop year))
            ;; Fully specified date.
            (let ((date (calendar-gregorian-from-absolute
                         (funcall toabs (list month day year)))))
              (if (calendar-date-is-visible-p date)
                  (calendar-mark-visible-date date color)))
          ;; Month and day in any year--this taken from the holiday stuff.
          (let* ((i-date (funcall fromabs
                                  (calendar-absolute-from-gregorian
                                   (list displayed-month 15 displayed-year))))
                 (m (calendar-extract-month i-date))
                 (y (calendar-extract-year i-date))
                 date)
            (unless (< m 1)             ; calendar doesn't apply
              (calendar-increment-month m y (- 10 month))
              (and (> m 7)              ; date might be visible
                   (calendar-date-is-visible-p
                    (setq date (calendar-gregorian-from-absolute
                                (funcall toabs (list month day y)))))
                   (calendar-mark-visible-date date color)))))
      (calendar-mark-complex month day year fromabs color))))


(defun diary-entry-time (s)
  "Return time at the beginning of the string S as a military-style integer.
For example, returns 1325 for 1:25pm.

Returns `diary-unknown-time' (default value -9999) if no time is recognized.
The recognized forms are XXXX, X:XX, or XX:XX (military time), and XXam,
XXAM, XXpm, XXPM, XX:XXam, XX:XXAM, XX:XXpm, or XX:XXPM.  A period (.) can
be used instead of a colon (:) to separate the hour and minute parts."
  (let (case-fold-search)
    (cond ((string-match                ; military time
            "\\`[ \t\n]*\\([0-9]?[0-9]\\)[:.]?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)"
            s)
           (+ (* 100 (string-to-number (match-string 1 s)))
              (string-to-number (match-string 2 s))))
          ((string-match                ; hour only (XXam or XXpm)
            "\\`[ \t\n]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s)
           (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
              (if (equal ?a (downcase (aref s (match-beginning 2))))
                  0 1200)))
          ((string-match        ; hour and minute (XX:XXam or XX:XXpm)
            "\\`[ \t\n]*\\([0-9]?[0-9]\\)[:.]\\([0-9][0-9]\\)\\([ap]\\)m\\>" s)
           (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
              (string-to-number (match-string 2 s))
              (if (equal ?a (downcase (aref s (match-beginning 3))))
                  0 1200)))
          (t diary-unknown-time))))     ; unrecognizable

(defun diary-entry-compare (e1 e2)
  "Return t if E1 is earlier than E2."
  (or (calendar-date-compare e1 e2)
      (and (calendar-date-equal (car e1) (car e2))
           (let* ((ts1 (cadr e1)) (t1 (diary-entry-time ts1))
                  (ts2 (cadr e2)) (t2 (diary-entry-time ts2)))
             (or (< t1 t2)
                 (and (= t1 t2)
                      (string-lessp ts1 ts2)))))))

(defun diary-sort-entries ()
  "Sort the list of diary entries by time of day.
If you add this function to `diary-list-entries-hook', it should
be the last item in the hook, in case earlier items add diary
entries, or change the order."
  (setq diary-entries-list (sort diary-entries-list 'diary-entry-compare)))


(defun diary-list-sexp-entries (date)
  "Add sexp entries for DATE from the diary file to `diary-entries-list'.
Also, make them visible in the diary.  Returns t if any entries are found.

Sexp diary entries must be prefaced by a `diary-sexp-entry-symbol'
\(normally `%%').  The form of a sexp diary entry is

                  %%(SEXP) ENTRY

Both ENTRY and DATE are available when the SEXP is evaluated.  If
the SEXP returns nil, the diary entry does not apply.  If it
returns a non-nil value, ENTRY will be taken to apply to DATE; if
the value is a string, that string will be the diary entry in the
fancy diary display.

For example, the following diary entry will apply to the 21st of
the month if it is a weekday and the Friday before if the 21st is
on a weekend:

      &%%(let ((dayname (calendar-day-of-week date))
               (day (calendar-extract-day date)))
           (or
             (and (= day 21) (memq dayname '(1 2 3 4 5)))
             (and (memq day '(19 20)) (= dayname 5)))
         ) UIUC pay checks deposited

A number of built-in functions are available for this type of
diary entry.  In the following, the optional parameter MARK
specifies a face or single-character string to use when
highlighting the day in the calendar.  For those functions that
take MONTH, DAY, and YEAR as arguments, the order of the input
parameters changes according to `calendar-date-style' (e.g. to
DAY MONTH YEAR in the European style).

  %%(diary-date MONTH DAY YEAR &optional MARK) text
    Entry applies if date is MONTH, DAY, YEAR.  DAY, MONTH, and YEAR can
    be a list of integers, t (meaning all values), or an integer.

  %%(diary-float MONTH DAYNAME N &optional DAY MARK) text
    Entry will appear on the Nth DAYNAME after/before MONTH DAY.
    DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
    If N>0, use the Nth DAYNAME after MONTH DAY.
    If N<0, use the Nth DAYNAME before MONTH DAY.
    DAY defaults to 1 if N>0, and MONTH's last day otherwise.
    MONTH can be a list of months, a single month, or t to
    specify all months.

  %%(diary-block M1 D1 Y1 M2 D2 Y2 &optional MARK) text
    Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
    inclusive.

  %%(diary-anniversary MONTH DAY YEAR &optional MARK) text
    Entry will appear on anniversary dates of MONTH DAY, YEAR.
    Text can contain `%d' or `%d%s'; `%d' will be replaced by the
    number of years since the MONTH DAY, YEAR, and `%s' by the
    ordinal ending of that number (i.e. `st', `nd', `rd' or `th',
    as appropriate).  The anniversary of February 29 is
    considered to be March 1 in a non-leap year.

  %%(diary-cyclic N MONTH DAY YEAR &optional MARK) text
    Entry will appear every N days, starting MONTH DAY, YEAR.
    Text can contain `%d' or `%d%s'; `%d' will be replaced by the
    number of repetitions since the MONTH DAY, YEAR and `%s' by
    the ordinal ending of that number (i.e. `st', `nd', `rd' or
    `th', as appropriate).

  %%(diary-remind SEXP DAYS &optional MARKING) text
    Entry is a reminder for diary sexp SEXP.  DAYS is either a
    single number or a list of numbers indicating the number(s)
    of days before the event that the warning(s) should occur.
    A negative number -DAYS has the same meaning as a list (1 2 ... DAYS).
    If the current date is (one of) DAYS before the event indicated
    by EXPR, then a suitable message (as specified by
    `diary-remind-message') appears.  In addition to the
    reminders beforehand, the diary entry also appears on the
    date itself.  If optional MARKING is non-nil then the
    *reminders* are marked on the calendar.  Marking of reminders
    is independent of whether the entry *itself* is a marking or
    non-marking one.

  %%(diary-hebrew-yahrzeit MONTH DAY YEAR) text
    Text is assumed to be the name of the person; the date is the
    date of death on the *civil* calendar.  The diary entry will
    appear on the proper Hebrew-date anniversary and on the day
    before.

All the remaining functions do not accept any text, and so only
make sense with `diary-fancy-display'.  Most produce output every day.

`diary-day-of-year'      - day of year and number of days remaining
`diary-iso-date'         - ISO commercial date
`diary-astro-day-number' - astronomical (Julian) day number
`diary-sunrise-sunset'   - local times of sunrise and sunset

These functions give the date in alternative calendrical systems:

`diary-bahai-date', `diary-chinese-date', `diary-coptic-date',
`diary-ethiopic-date', `diary-french-date', `diary-hebrew-date',
`diary-islamic-date', `diary-julian-date', `diary-mayan-date',
`diary-persian-date'

Theses functions only produce output on certain dates:

`diary-lunar-phases'           - phases of moon (on the appropriate days)
`diary-hebrew-omer'            - Omer count, within 50 days after Passover
`diary-hebrew-parasha'         - weekly parasha, every Saturday
`diary-hebrew-rosh-hodesh'     - Rosh Hodesh, or the day or Saturday before
`diary-hebrew-sabbath-candles' - local time of candle lighting, on Fridays


Marking these entries is *extremely* time consuming, so it is
best if they are non-marking."
  (let ((s-entry (format "^%s?%s(" (regexp-quote diary-nonmarking-symbol)
                         (regexp-quote diary-sexp-entry-symbol)))
        entry-found file-glob-attrs marks
        sexp-start sexp entry specifier entry-start line-start
        diary-entry temp literal)
    (goto-char (point-min))
    (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
    (while (re-search-forward s-entry nil t)
      (backward-char 1)
      (setq sexp-start (point))
      (forward-sexp)
      (setq sexp (buffer-substring-no-properties sexp-start (point))
            line-start (line-end-position 0)
            specifier
            (buffer-substring-no-properties (1+ line-start) (point))
            entry-start (1+ line-start))
      (forward-char 1)
      (if (and (bolp) (not (looking-at "[ \t]")))
          ;; Diary entry consists only of the sexp.
          (progn
            (backward-char 1)
            (setq entry ""))
        (setq entry-start (point))
        (forward-line 1)
        (while (looking-at "[ \t]")
          (forward-line 1))
        (if (bolp) (backward-char 1))
        (setq entry (buffer-substring-no-properties entry-start (point))))
      (setq diary-entry (diary-sexp-entry sexp entry date)
            literal entry               ; before evaluation
            entry (if (consp diary-entry)
                      (cdr diary-entry)
                    diary-entry))
      (when diary-entry
        (remove-overlays line-start (point) 'invisible 'diary)
        (if (< 0 (length entry))
            (setq temp (diary-pull-attrs entry file-glob-attrs)
                  entry (nth 0 temp)
                  marks (nth 1 temp))))
      (diary-add-to-list date entry specifier
                         (if entry-start (copy-marker entry-start))
                         marks literal)
      (setq entry-found (or entry-found diary-entry)))
    entry-found))

(defun diary-make-date (a b c)
  "Convert A B C into the internal calendar date form.
The expected order of the inputs depends on `calendar-date-style',
e.g. in the European case, A = day, B = month, C = year.  Returns
a list (MONTH DAY YEAR), i.e. the American style, which is the
form used internally by the calendar and diary."
  (cond ((eq calendar-date-style 'iso)  ; YMD
         (list b c a))
        ((eq calendar-date-style 'european) ; DMY
         (list b a c))
        (t (list a b c))))


;;; Sexp diary functions.

(defvar date)
(defvar entry)

;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
(defun diary-date (month day year &optional mark)
  "Specific date(s) diary entry.
Entry applies if date is MONTH, DAY, YEAR.  Each parameter can be a
list of integers, t (meaning all values), or an integer.  The order
of the input parameters changes according to `calendar-date-style'
\(e.g. to DAY MONTH YEAR in the European style).

An optional parameter MARK specifies a face or single-character string
to use when highlighting the day in the calendar."
  (let* ((ddate (diary-make-date month day year))
         (dd (calendar-extract-day ddate))
         (mm (calendar-extract-month ddate))
         (yy (calendar-extract-year ddate))
         (m (calendar-extract-month date))
         (y (calendar-extract-year date))
         (d (calendar-extract-day date)))
    (and
     (or (and (listp dd) (memq d dd))
         (equal d dd)
         (eq dd t))
     (or (and (listp mm) (memq m mm))
         (equal m mm)
         (eq mm t))
     (or (and (listp yy) (memq y yy))
         (equal y yy)
         (eq yy t))
     (cons mark entry))))

;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
(defun diary-block (m1 d1 y1 m2 d2 y2 &optional mark)
  "Block diary entry.
Entry applies if date is between, or on one of, two dates.  The order
of the input parameters changes according to `calendar-date-style'
\(e.g. to D1, M1, Y1, D2, M2, Y2 in the European style).

An optional parameter MARK specifies a face or single-character string
to use when highlighting the day in the calendar."
  (let ((date1 (calendar-absolute-from-gregorian
                (diary-make-date m1 d1 y1)))
        (date2 (calendar-absolute-from-gregorian
                (diary-make-date m2 d2 y2)))
        (d (calendar-absolute-from-gregorian date)))
    (and (<= date1 d) (<= d date2)
         (cons mark entry))))

;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
(defun diary-float (month dayname n &optional day mark)
  "Diary entry for the Nth DAYNAME after/before MONTH DAY.
DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
If N>0, use the Nth DAYNAME after MONTH DAY.
If N<0, use the Nth DAYNAME before MONTH DAY.
DAY defaults to 1 if N>0, and MONTH's last day otherwise.
MONTH can be a list of months, an integer, or t (meaning all months).
Optional MARK specifies a face or single-character string to use when
highlighting the day in the calendar."
  ;; This is messy because the diary entry may apply, but the date on which it
  ;; is based can be in a different month/year.  For example, asking for the
  ;; first Monday after December 30.  For large values of |n| the problem is
  ;; more grotesque.
  (and (= dayname (calendar-day-of-week date))
       (let* ((m (calendar-extract-month date))
              (d (calendar-extract-day date))
              (y (calendar-extract-year date))
              ;; Last (n>0) or first (n<0) possible base date for entry.
              (limit
               (calendar-nth-named-absday (- n) dayname m y d))
              (last-abs (if (> n 0) limit (+ limit 6)))
              (first-abs (if (> n 0) (- limit 6) limit))
              (last (calendar-gregorian-from-absolute last-abs))
              (first (calendar-gregorian-from-absolute first-abs))
              ;; m1, d1 is first possible base date.
              (m1 (calendar-extract-month first))
              (d1 (calendar-extract-day first))
              (y1 (calendar-extract-year first))
              ;; m2, d2 is last possible base date.
              (m2 (calendar-extract-month last))
              (d2 (calendar-extract-day last))
              (y2 (calendar-extract-year last)))
         (if (or (and (= m1 m2) ; only possible base dates in one month
                      (or (eq month t)
                          (if (listp month)
                              (memq m1 month)
                            (= m1 month)))
                      (let ((d (or day (if (> n 0)
                                           1
                                         (calendar-last-day-of-month m1 y1)))))
                        (and (<= d1 d) (<= d d2))))
                 ;; Only possible base dates straddle two months.
                 (and (or (< y1 y2)
                          (and (= y1 y2) (< m1 m2)))
                      (or
                       ;; m1, d1 works as a base date.
                       (and
                        (or (eq month t)
                            (if (listp month)
                                (memq m1 month)
                              (= m1 month)))
                        (<= d1 (or day (if (> n 0)
                                           1
                                         (calendar-last-day-of-month m1 y1)))))
                       ;; m2, d2 works as a base date.
                       (and (or (eq month t)
                                (if (listp month)
                                    (memq m2 month)
                                  (= m2 month)))
                            (<= (or day (if (> n 0)
                                            1
                                          (calendar-last-day-of-month m2 y2)))
                                d2)))))
             (cons mark entry)))))

(defun diary-ordinal-suffix (n)
  "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
  (if (or (memq (% n 100) '(11 12 13))
          (< 3 (% n 10)))
      "th"
    (aref ["th" "st" "nd" "rd"] (% n 10))))

;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
(defun diary-anniversary (month day &optional year mark)
  "Anniversary diary entry.
Entry applies if date is the anniversary of MONTH, DAY, YEAR.
The order of the input parameters changes according to
`calendar-date-style' (e.g. to DAY MONTH YEAR in the European style).

The diary entry can contain `%d' or `%d%s'; the %d will be replaced
by the number of years since the MONTH, DAY, YEAR, and the %s will
be replaced by the ordinal ending of that number (that is, `st',
`nd', `rd' or `th', as appropriate).  The anniversary of February 29
is considered to be March 1 in non-leap years.

An optional parameter MARK specifies a face or single-character
string to use when highlighting the day in the calendar."
  (let* ((ddate (diary-make-date month day year))
         (dd (calendar-extract-day ddate))
         (mm (calendar-extract-month ddate))
         (yy (calendar-extract-year ddate))
         (y (calendar-extract-year date))
         (diff (if yy (- y yy) 100)))
    (and (= mm 2) (= dd 29) (not (calendar-leap-year-p y))
         (setq mm 3
               dd 1))
    (and (> diff 0) (calendar-date-equal (list mm dd y) date)
         (cons mark (format entry diff (diary-ordinal-suffix diff))))))

;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
(defun diary-cyclic (n month day year &optional mark)
  "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
The order of the input parameters changes according to
`calendar-date-style' (e.g. to N DAY MONTH YEAR in the European
style).  The entry can contain `%d' or `%d%s'; the %d will be
replaced by the number of repetitions since the MONTH DAY YEAR,
and %s by the ordinal ending of that number (that is, `st', `nd',
`rd' or `th', as appropriate).

An optional parameter MARK specifies a face or single-character
string to use when highlighting the day in the calendar."
  (or (> n 0)
      (user-error "Day count must be positive"))
  (let* ((diff (- (calendar-absolute-from-gregorian date)
                  (calendar-absolute-from-gregorian
                   (diary-make-date month day year))))
         (cycle (/ diff n)))
    (and (>= diff 0) (zerop (% diff n))
         (cons mark (format entry cycle (diary-ordinal-suffix cycle))))))

(defun diary-day-of-year ()
  "Day of year and number of days remaining in the year of date diary entry."
  (calendar-day-of-year-string date))

(defun diary-remind (sexp days &optional marking)
  "Provide a reminder of a diary entry.
SEXP is a diary-sexp.  DAYS is either a single number or a list
of numbers indicating the number(s) of days before the event that
the warning(s) should occur on.  A negative number -DAYS has the
same meaning as a list (1 2 ... DAYS).  If the current date
is (one of) DAYS before the event indicated by SEXP, then this function
returns a suitable message (as specified by `diary-remind-message').

In addition to the reminders beforehand, the diary entry also
appears on the date itself.

A `diary-nonmarking-symbol' at the beginning of the line of the
`diary-remind' entry specifies that the diary entry (not the
reminder) is non-marking.  Marking of reminders is independent of
whether the entry itself is a marking or nonmarking; if optional
parameter MARKING is non-nil then the reminders are marked on the
calendar."
  ;; `date' has a value at this point, from diary-sexp-entry.
  ;; Convert a negative number to a list of days.
  (and (integerp days)
       (< days 0)
       (setq days (number-sequence 1 (- days))))
  (let ((diary-entry (eval sexp)))
    (cond
     ;; Diary entry applies on date.
     ((and diary-entry
           (or (not diary-marking-entries-flag) diary-marking-entry-flag))
      diary-entry)
     ;; Diary entry may apply to `days' before date.
     ((and (integerp days)
           (not diary-entry)      ; diary entry does not apply to date
           (or (not diary-marking-entries-flag) marking))
      ;; Adjust date, and re-evaluate.
      (let ((date (calendar-gregorian-from-absolute
                   (+ (calendar-absolute-from-gregorian date) days))))
        (when (setq diary-entry (eval sexp))
          ;; Discard any mark portion from diary-anniversary, etc.
          (if (consp diary-entry) (setq diary-entry (cdr diary-entry)))
          (mapconcat 'eval diary-remind-message ""))))
     ;; Diary entry may apply to one of a list of days before date.
     ((and (listp days) days)
      (or (diary-remind sexp (car days) marking)
          (diary-remind sexp (cdr days) marking))))))


;;; Diary insertion functions.

;;;###cal-autoload
(defun diary-make-entry (string &optional nonmarking file)
  "Insert a diary entry STRING which may be NONMARKING in FILE.
If omitted, NONMARKING defaults to nil and FILE defaults to
`diary-file'."
  (let ((pop-up-frames (or pop-up-frames (window-dedicated-p))))
    (find-file-other-window (or file diary-file)))
  (when (eq major-mode (default-value 'major-mode)) (diary-mode))
  (widen)
  (diary-unhide-everything)
  (goto-char (point-max))
  (when (let ((case-fold-search t))
          (search-backward "Local Variables:"
                           (max (- (point-max) 3000) (point-min))
                           t))
    (beginning-of-line)
    (insert "\n")
    (forward-line -1))
  (insert
   (if (bolp) "" "\n")
   (if nonmarking diary-nonmarking-symbol "")
   string " "))

;;;###cal-autoload
(defun diary-insert-entry (arg &optional event)
  "Insert a diary entry for the date indicated by point.
Prefix argument ARG makes the entry nonmarking."
  (interactive
   (list current-prefix-arg last-nonmenu-event))
  (diary-make-entry (calendar-date-string (calendar-cursor-to-date t event) t t)
                    arg))

;;;###cal-autoload
(defun diary-insert-weekly-entry (arg)
  "Insert a weekly diary entry for the day of the week indicated by point.
Prefix argument ARG makes the entry nonmarking."
  (interactive "P")
  (diary-make-entry (calendar-day-name (calendar-cursor-to-date t))
                    arg))

(defun diary-date-display-form (&optional type)
  "Return value for `calendar-date-display-form' using `calendar-date-style'.
Optional symbol TYPE is either `monthly' or `yearly'."
  (cond ((eq type 'monthly) (cond ((eq calendar-date-style 'iso)
                                   '((format "*-*-%.2d"
                                             (string-to-number day))))
                                  ((eq calendar-date-style 'european)
                                   '(day " * "))
                                  (t '("* " day ))))
        ((eq type 'yearly) (cond ((eq calendar-date-style 'iso)
                                  '((format "*-%.2d-%.2d"
                                            (string-to-number month)
                                            (string-to-number day))))
                                 ((eq calendar-date-style 'european)
                                  '(day " " monthname))
                                 (t '(monthname " " day))))
        ;; Iso cannot contain "-", because this form used eg by
        ;; diary-insert-anniversary-entry.
        (t (cond ((eq calendar-date-style 'iso)
                 '((format "%s %.2d %.2d" year
                           (string-to-number month) (string-to-number day))))
                 ((eq calendar-date-style 'european)
                  '(day " " month " " year))
                 (t '(month " " day " " year))))))

(defun diary-insert-entry-1 (&optional type nomark months symbol absfunc)
  "Subroutine to insert a diary entry related to the date at point.
TYPE is the type of entry (`monthly' or `yearly').  NOMARK non-nil
means make the entry non-marking.  Array MONTHS is used in place
of `calendar-month-name-array'.  String SYMBOL marks the type of
diary entry.  Function ABSFUNC converts absolute dates to dates of
the appropriate type."
  (let ((calendar-date-display-form (if type
                                        (diary-date-display-form type)
                                      calendar-date-display-form))
        (calendar-month-name-array (or months calendar-month-name-array))
        (date (calendar-cursor-to-date t)))
    (diary-make-entry
     (format "%s%s" (or symbol "")
             (calendar-date-string
              (if absfunc
                  (funcall absfunc (calendar-absolute-from-gregorian date))
                date)
              (not absfunc)
              (not type)))
     nomark)))

;;;###cal-autoload
(defun diary-insert-monthly-entry (arg)
  "Insert a monthly diary entry for the day of the month indicated by point.
Prefix argument ARG makes the entry nonmarking."
  (interactive "P")
  (diary-insert-entry-1 'monthly arg))

;;;###cal-autoload
(defun diary-insert-yearly-entry (arg)
  "Insert an annual diary entry for the day of the year indicated by point.
Prefix argument ARG makes the entry nonmarking."
  (interactive "P")
  (diary-insert-entry-1 'yearly arg))

;;;###cal-autoload
(defun diary-insert-anniversary-entry (arg)
  "Insert an anniversary diary entry for the date given by point.
Prefix argument ARG makes the entry nonmarking."
  (interactive "P")
  (let ((calendar-date-display-form (diary-date-display-form)))
    (diary-make-entry
     (format "%s(diary-anniversary %s)"
             diary-sexp-entry-symbol
             (calendar-date-string (calendar-cursor-to-date t) nil t))
     arg)))

;;;###cal-autoload
(defun diary-insert-block-entry (arg)
  "Insert a block diary entry for the days between the point and marked date.
Prefix argument ARG makes the entry nonmarking."
  (interactive "P")
  (let ((calendar-date-display-form (diary-date-display-form))
        (cursor (calendar-cursor-to-date t))
        (mark (or (car calendar-mark-ring)
                  (error "No mark set in this buffer")))
        start end)
    (if (< (calendar-absolute-from-gregorian mark)
           (calendar-absolute-from-gregorian cursor))
        (setq start mark
              end cursor)
      (setq start cursor
            end mark))
    (diary-make-entry
     (format "%s(diary-block %s %s)"
             diary-sexp-entry-symbol
             (calendar-date-string start nil t)
             (calendar-date-string end nil t))
     arg)))

;;;###cal-autoload
(defun diary-insert-cyclic-entry (arg)
  "Insert a cyclic diary entry starting at the date given by point.
Prefix argument ARG makes the entry nonmarking."
  (interactive "P")
  (let ((calendar-date-display-form (diary-date-display-form)))
    (diary-make-entry
     (format "%s(diary-cyclic %d %s)"
             diary-sexp-entry-symbol
             (calendar-read "Repeat every how many days: "
                            (lambda (x) (> x 0)))
             (calendar-date-string (calendar-cursor-to-date t) nil t))
     arg)))

;;; Diary mode.

(defun diary-redraw-calendar ()
  "If `calendar-buffer' is live and diary entries are marked, redraw it."
  (and calendar-mark-diary-entries-flag
       (save-excursion
         (calendar-redraw)))
  ;; Return value suitable for `write-contents-functions'.
  nil)

(defvar diary-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "\C-c\C-s" 'diary-show-all-entries)
    (define-key map "\C-c\C-q" 'quit-window)
    map)
  "Keymap for `diary-mode'.")

(defun diary-font-lock-sexps (limit)
  "Recognize sexp diary entry up to LIMIT for font-locking."
  (if (re-search-forward
       (format "^%s?\\(%s\\)" (regexp-quote diary-nonmarking-symbol)
               (regexp-quote diary-sexp-entry-symbol))
       limit t)
      (condition-case nil
          (save-restriction
            (narrow-to-region (point-min) limit)
            (let ((start (point)))
              (forward-sexp 1)
              (store-match-data (list start (point)))
              t))
        (error t))))

(defun diary-font-lock-date-forms (month-array &optional symbol abbrev-array)
  "Create font-lock patterns for `diary-date-forms' using MONTH-ARRAY.
If given, optional SYMBOL must be a prefix to entries.  If
optional ABBREV-ARRAY is present, also matches the abbreviations
from this array (with or without a final `.'), in addition to the
full month names."
  (let ((dayname (diary-name-pattern calendar-day-name-array
                                     calendar-day-abbrev-array t))
        (monthname (format "\\(%s\\|\\*\\)"
                           (diary-name-pattern month-array abbrev-array)))
        (month "\\([0-9]+\\|\\*\\)")
        (day "\\([0-9]+\\|\\*\\)")
        (year "-?\\([0-9]+\\|\\*\\)"))
    (mapcar (lambda (x)
              (cons
               (concat "^" (regexp-quote diary-nonmarking-symbol) "?"
                       (if symbol (regexp-quote symbol) "") "\\("
                       (mapconcat 'eval
                                  ;; If backup, omit first item (backup)
                                  ;; and last item (not part of date).
                                  (if (equal (car x) 'backup)
                                      (nreverse (cdr (reverse (cdr x))))
                                    x)
                                  "")
                       ;; With backup, last item is not part of date.
                       (if (equal (car x) 'backup)
                           (concat "\\)" (eval (car (reverse x))))
                         "\\)"))
               '(1 'diary)))
            diary-date-forms)))

(defmacro diary-font-lock-keywords-1 (markfunc listfunc feature months symbol)
  "Subroutine of the function `diary-font-lock-keywords'.
If MARKFUNC is a member of `diary-nongregorian-marking-hook', or
LISTFUNC of `diary-nongregorian-listing-hook', then require FEATURE and
return a font-lock pattern matching array of MONTHS and marking SYMBOL."
  `(when (or (memq ',markfunc diary-nongregorian-marking-hook)
             (memq ',listfunc diary-nongregorian-listing-hook))
     (require ',feature)
     (diary-font-lock-date-forms ,months ,symbol)))

(defconst diary-time-regexp
  ;; Accepted formats: 10:00 10.00 10h00 10h 10am 10:00am 10.00am
  ;; Use of "." as a separator annoyingly matches numbers, eg "123.45".
  ;; Hence often prefix this with "\\(^\\|\\s-\\)."
  (concat "[0-9]?[0-9]\\([AaPp][mM]\\|\\("
          "[Hh]\\([0-9][0-9]\\)?\\|[:.][0-9][0-9]"
          "\\)\\([AaPp][Mm]\\)?\\)")
  "Regular expression matching a time of day.")

(defvar calendar-hebrew-month-name-array-leap-year)
(defvar calendar-islamic-month-name-array)
(defvar calendar-bahai-month-name-array)
(defvar calendar-chinese-month-name-array)

;;;###cal-autoload
(defun diary-font-lock-keywords ()
  "Return a value for the variable `diary-font-lock-keywords'."
  (append
   (diary-font-lock-date-forms calendar-month-name-array
                               nil calendar-month-abbrev-array)
   (diary-font-lock-keywords-1 diary-hebrew-mark-entries
                               diary-hebrew-list-entries
                               cal-hebrew
                               calendar-hebrew-month-name-array-leap-year
                               diary-hebrew-entry-symbol)
   (diary-font-lock-keywords-1 diary-islamic-mark-entries
                               diary-islamic-list-entries
                               cal-islam
                               calendar-islamic-month-name-array
                               diary-islamic-entry-symbol)
   (diary-font-lock-keywords-1 diary-bahai-mark-entries
                               diary-bahai-list-entries
                               cal-bahai
                               calendar-bahai-month-name-array
                               diary-bahai-entry-symbol)
   (diary-font-lock-keywords-1 diary-chinese-mark-entries
                               diary-chinese-list-entries
                               cal-china
                               calendar-chinese-month-name-array
                               diary-chinese-entry-symbol)
   (list
    (cons
     (format "^%s.*$" (regexp-quote diary-include-string))
     'font-lock-keyword-face)
    (cons
     (format "^%s?\\(%s\\)" (regexp-quote diary-nonmarking-symbol)
             (regexp-quote diary-sexp-entry-symbol))
     '(1 font-lock-constant-face))
    (cons
     (format "^%s" (regexp-quote diary-nonmarking-symbol))
     'font-lock-constant-face)
    (cons
     (format "^%s?%s" (regexp-quote diary-nonmarking-symbol)
             (regexp-opt (mapcar 'regexp-quote
                                 (list diary-hebrew-entry-symbol
                                       diary-islamic-entry-symbol
                                       diary-bahai-entry-symbol
                                       diary-chinese-entry-symbol))
                         t))
     '(1 font-lock-constant-face))
    '(diary-font-lock-sexps . font-lock-keyword-face)
    ;; Don't need to worry about space around "-" because the first
    ;; match takes care of that.  It does mean the "-" itself may or
    ;; may not be fontified though.
    ;; diary-date-forms often include a final character that is not
    ;; part of the date (eg a non-digit to mark the end of the year).
    ;; This can use up the only space char between a date and time (b#7891).
    ;; Hence we use OVERRIDE, which can only override whitespace.
    ;; FIXME it's probably better to tighten up the diary-time-regexp
    ;; and drop the whitespace requirement below.
    `(,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
               diary-time-regexp)
      . (0 'diary-time t)))))
;      . 'diary-time))))

(defvar diary-font-lock-keywords (diary-font-lock-keywords)
  "Forms to highlight in `diary-mode'.")

;;;###autoload
(define-derived-mode diary-mode fundamental-mode "Diary"
  "Major mode for editing the diary file."
  (set (make-local-variable 'font-lock-defaults)
       '(diary-font-lock-keywords t))
  (set (make-local-variable 'comment-start) diary-comment-start)
  (set (make-local-variable 'comment-end) diary-comment-end)
  (add-to-invisibility-spec '(diary . nil))
  (add-hook 'after-save-hook 'diary-redraw-calendar nil t)
  ;; In case the file was modified externally, refresh the calendar
  ;; after refreshing the diary buffer.
  (add-hook 'after-revert-hook 'diary-redraw-calendar nil t)
  (if diary-header-line-flag
      (setq header-line-format diary-header-line-format)))


;;; Fancy Diary Mode.

(defun diary-fancy-date-pattern ()
  "Return a regexp matching the first line of a fancy diary date header.
This depends on the calendar date style."
  (concat
   (let ((dayname (diary-name-pattern calendar-day-name-array nil t))
         (monthname (diary-name-pattern calendar-month-name-array nil t))
         (day "1")
         (month "2")
         ;; FIXME? This used to be "-?[0-9]+" - what was the "-?" for?
         (year "3"))
     ;; This is ugly.  c-d-d-form expects `day' etc to be "numbers in
     ;; string form"; eg the iso version calls string-to-number on some.
     ;; Therefore we cannot eg just let day = "[0-9]+".  (Bug#8583).
     ;; Assumes no integers in c-day/month-name-array.
     (replace-regexp-in-string "[0-9]+" "[0-9]+"
                               (mapconcat 'eval calendar-date-display-form "")
                               nil t))
   ;; Optional ": holiday name" after the date.
   "\\(: .*\\)?"))

(defun diary-fancy-date-matcher (limit)
  "Search for a fancy diary data header, up to LIMIT."
  ;; Any number of " other holiday name" lines, followed by "==" line.
  (when (re-search-forward
         (format "%s\\(\n +.*\\)*\n=+$" (diary-fancy-date-pattern)) limit t)
    (put-text-property (match-beginning 0) (match-end 0) 'font-lock-multiline t)
    t))

(defvar diary-fancy-font-lock-keywords
  `((diary-fancy-date-matcher . 'diary)
    ("^.*\\([aA]nniversary\\|[bB]irthday\\).*$" . 'diary-anniversary)
    ("^.*Yahrzeit.*$" . font-lock-constant-face)
    ("^\\(Erev \\)?Rosh Hodesh.*" . font-lock-function-name-face)
    ("^Day.*omer.*$" . font-lock-builtin-face)
    ("^Parashat.*$" . font-lock-comment-face)
    (,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
              diary-time-regexp) . 'diary-time))
  "Keywords to highlight in fancy diary display.")

;; If region looks like it might start or end in the middle of a
;; multiline pattern, extend the region to encompass the whole pattern.
(defun diary-fancy-font-lock-fontify-region-function (beg end &optional verbose)
  "Function to use for `font-lock-fontify-region-function' in Fancy Diary.
Needed to handle multiline keyword in `diary-fancy-font-lock-keywords'.
Fontify the region between BEG and END, quietly unless VERBOSE is non-nil."
  (goto-char beg)
  (forward-line 0)
  (if (looking-at "=+$") (forward-line -1))
  (while (and (looking-at " +[^ ]")
              (zerop (forward-line -1))))
  ;; This check not essential.
  (if (looking-at (diary-fancy-date-pattern))
      (setq beg (line-beginning-position)))
  (goto-char end)
  (forward-line 0)
  (while (and (looking-at " +[^ ]")
              (zerop (forward-line 1))))
  (if (looking-at "=+$")
      (setq end (line-beginning-position 2)))
  (font-lock-default-fontify-region beg end verbose))

(defvar diary-fancy-overriding-map (make-sparse-keymap)
  "Keymap overriding minor-mode maps in `diary-fancy-display-mode'.")

(define-derived-mode diary-fancy-display-mode special-mode
  "Diary"
  "Major mode used while displaying diary entries using Fancy Display."
  (set (make-local-variable 'font-lock-defaults)
       '(diary-fancy-font-lock-keywords
         t nil nil nil
         (font-lock-fontify-region-function
          . diary-fancy-font-lock-fontify-region-function)))
  (set (make-local-variable 'minor-mode-overriding-map-alist)
       (list (cons t diary-fancy-overriding-map)))
  (view-mode 1))

;; Following code from Dave Love <fx@gnu.org>.
;; Import Outlook-format appointments from mail messages in Gnus or
;; Rmail using command `diary-from-outlook'.  This, or the specialized
;; functions `diary-from-outlook-gnus' and `diary-from-outlook-rmail',
;; could be run from hooks to notice appointments automatically (in
;; which case they will prompt about adding to the diary).  The
;; message formats recognized are customizable through `diary-outlook-formats'.

(defun diary-from-outlook-internal (subject body &optional test-only)
  "Snarf a diary entry from a message assumed to be from MS Outlook.
SUBJECT and BODY are strings giving the message subject and body.
Arg TEST-ONLY non-nil means return non-nil if and only if the
message contains an appointment, don't make a diary entry."
  (catch 'finished
    (let (format-string)
      (dolist (fmt diary-outlook-formats)
        (when (eq 0 (string-match (car fmt) body))
          (unless test-only
            (setq format-string (cdr fmt))
            (save-excursion
              (save-window-excursion
                (diary-make-entry
                 (format (replace-match (if (functionp format-string)
                                            (funcall format-string body)
                                          format-string)
                                        t nil (match-string 0 body))
                         subject)))))
          (throw 'finished t))))
    nil))

(defvar gnus-article-mime-handles)
(defvar gnus-article-buffer)

(autoload 'gnus-fetch-field "gnus-util")
(autoload 'gnus-narrow-to-body "gnus")
(autoload 'mm-get-part "mm-decode")

(defun diary-from-outlook-gnus (&optional noconfirm)
  "Maybe snarf diary entry from Outlook-generated message in Gnus.
Unless the optional argument NOCONFIRM is non-nil (which is the case when
this function is called interactively), then if an entry is found the
user is asked to confirm its addition.
Add this function to `gnus-article-prepare-hook' to notice appointments
automatically."
  (interactive "p")
  (with-current-buffer gnus-article-buffer
    (let ((subject (gnus-fetch-field "subject"))
          (body (if gnus-article-mime-handles
                    ;; We're multipart.  Don't get confused by part
                    ;; buttons &c.  Assume info is in first part.
                    (mm-get-part (nth 1 gnus-article-mime-handles))
                  (save-restriction
                    (gnus-narrow-to-body)
                    (buffer-string)))))
      (when (diary-from-outlook-internal subject body t)
        (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
          (diary-from-outlook-internal subject body)
          (message "Diary entry added"))))))

(custom-add-option 'gnus-article-prepare-hook 'diary-from-outlook-gnus)

(defvar rmail-buffer)

(defun diary-from-outlook-rmail (&optional noconfirm)
  "Maybe snarf diary entry from Outlook-generated message in Rmail.
Unless the optional argument NOCONFIRM is non-nil (which is the case when
this function is called interactively), then if an entry is found the
user is asked to confirm its addition."
  (interactive "p")
  ;; FIXME maybe the body needs rmail-mm decoding, in which case
  ;; there is no single buffer with both body and subject, sigh.
  (with-current-buffer rmail-buffer
    (let ((subject (mail-fetch-field "subject"))
          (body (buffer-substring (save-excursion
                                    (rfc822-goto-eoh)
                                    (point))
                                  (point-max))))
      (when (diary-from-outlook-internal subject body t)
        (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
          (diary-from-outlook-internal subject body)
          (message "Diary entry added"))))))

(defvar diary-from-outlook-function nil
  "If non-nil, a function of one argument for `diary-from-outlook' to call.
If the current buffer contains an Outlook-style appointment message,
this function should extract it into a diary entry.  If the argument is
nil, it should ask for confirmation before adding this entry to the diary.
For examples, see `diary-from-outlook-rmail' and `diary-from-outlook-gnus'.")

(defun diary-from-outlook (&optional noconfirm)
  "Maybe snarf diary entry from current Outlook-generated message.
Uses `diary-from-outlook-function' if that is non-nil, else
`diary-from-outlook-rmail' for Rmail or `diary-from-outlook-gnus' for Gnus.
Unless the optional argument NOCONFIRM is non-nil (which is the
case when this function is called interactively), then if an
entry is found the user is asked to confirm its addition."
  (interactive "p")
  (let ((func (cond
               (diary-from-outlook-function)
               ((eq major-mode 'rmail-mode)
                #'diary-from-outlook-rmail)
               ((memq major-mode '(gnus-summary-mode gnus-article-mode))
                #'diary-from-outlook-gnus)
               (t (error "Don't know how to snarf in ‘%s’" major-mode)))))
    (funcall func noconfirm)))

(provide 'diary-lib)

;; Local Variables:
;; coding: utf-8
;; End:

;;; diary-lib.el ends here