Source

edict / edict.el.096

   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
;;;
;;; Copyright (C) 1991, 1992 Per Hammarlund (perham@nada.kth.se)
;;;

;;;
;;; Some code that looks for translations of english and japanese using the
;;; EDICTJ Public Domain japanese/english dictionary.
;;;
;;; Written by Per Hammarlund <perham@nada.kth.se>
;;; Morphology and private dictionary handling/editing by Bob Kerns <rwk@crl.dec.com>
;;; Helpful remarks from Ken-Ichi Handa <handa@etl.go.jp>.
;;; The EDICTJ PD dictionary is maintained by Jim Breen <jwb@monu6.cc.monash.edu.au>
;;;

;;;
;;;   This program 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 1, or (at your option)
;;;   any later version.
;;; 
;;;   This program 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 this program; if not, write to the Free Software
;;;   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;;; 

;;;
;;; Short getting started guide, this assumes that you have not used
;;; the install script and that you understand the "technical" words
;;; used, if you don't, please read the documentation in edict.doc:
;;;
;;; 1. Make sure that you have placed edict.el in a directory that is included
;;;    in the nemacs's search path, look at the variable "load-path" to make sure
;;;    that the directory is in that list.
;;;
;;; 2. Add something like this to your .emacs (or .nemacs) file:
;;;	(autoload 'edict-search-english "edict" "Search for a translation of an English word")
;;; 	(global-set-key "\e*" 'edict-search-english)
;;; 	(autoload 'edict-search-kanji "edict" "Search for a translation of a Kanji sequence")
;;; 	(global-set-key "\e_" 'edict-search-kanji)
;;; 	(autoload 'edict-insert "edict" "Insert the last translation")
;;;     (global-set-key "\e+" 'edict-insert)
;;; Note that you can change the key binding to whatever you like, these are only "examples".
;;;
;;; 3. The variable *edict-files* should be a list of filenames of
;;;    edict dictionary files that you want edict to load and search
;;;    in.  The real dictionary EDICTJ should be one of these files,
;;;    you may also have have some local file(s) there.  Something 
;;;    like this *may* be appropriate to:
;;;	(setq *edict-files*  '("edictj"
;;;                            "~my-friend-the-user/.edict"
;;;                            "~my-other-friend-the-user/.edict"))
;;;    By default, it searches the load path (the same directories that are searched
;;;    when you do m-X load-file<return>edict<return>), for a file named "edictj".
;;;
;;; 4. Set the name of your *own* local edictj file.  (Note that this file should
;;;    not be included in the list above!)  Edict will include the additions that
;;;    you do in this file.  The variable *edict-private-file* defaults to "~/.edict",
;;;    if you want something else do a:
;;;	  (setq *edict-private-file* "~/somewhere/somethingelse/")
;;; (Don't forget to submit your useful words to Jim Breen once in a
;;; while! His address is jwb@monu6.cc.monash.edu.au)
;;;
;;; You are done.  Report errors and comments to perham@nada.kth.se.
;;;

;;;cl.el is part of gnuemacs, so it should be no problem to require
;;;  these Common Lisp extensions.
(require 'cl)

;;; This should exist, but doesn't.  See edict.install for the
;;; compiler half of this.  You should be sure to load the same
;;; hacks into your compiler if you compile this by hand, or you
;;; won't get it byte compiled.

(defmacro eval-when (when &rest forms)
  (and (or (member 'eval when)
	    (member ':execute when))
       (mapcar (function eval) forms))
  (and (or (member 'load when)
	   (member ':load-toplevel when))
       (cons 'progn forms)))

(defvar *edict-private-file* "~/.edict"
  "*This is the edict dictionary where the user's entries will be added.")

;;*edict-files* should contain a list of filenames for the files that
;; should be read up into the *edict* buffer.
(defvar *edict-files* '("edictj")
"*This is a list of edict files that are loaded into the *edict* buffer
and searched. You will probably want at least one of them to be the real
EDICT file.")

;;The edict buffer where the data base, of sorts, is and the buffer
;; variable.
(defvar *edict-buffer-name* "*edict*")
(defvar *edict-buffer* nil)

;;The edict matches buffer and the name of it
(defvar *edict-match-buffer-name* "*edict matches*")
(defvar *edict-match-buffer* nil)

(defvar *edict-version-date* "920423 [ʿ����ǯ�������(��)]"
  "The variable *edict-version-date* contains a string with the
date when this version was released.  In both Swedish and Japanese
standards")

(defvar *edict-version* "0.9.6"
 "The variable *edict-version* contains a string that describes
 what version of the edict software that you are running.")

(defun edict-version ()
  "The function edict-version simply displays (as a message in the
mini-buffer) the version of the edict software that you are running
at the moment.  The same string is also returned from the function."
   (interactive)
   (message (concat "Edict version " *edict-version*  " of " *edict-version-date*)))

;;; Marker so we can find the individual files in the buffer.
(defvar *edict-file-begin-marker* "<<<<<<<<<<<<<<<<")
(defvar *edict-file-end-marker* ">>>>>>>>>>>>>>>>")

;;; This is the set of characters to be ignored in the middle of kanji
;;; words being looked up.
;;; The �� below should be ��, but there seems to be an off-by-one error
;;; in the regexp code.

(defvar *edict-kanji-whitespace* "��-����-�� \n\t>;!:#?,.\"/@��-��")

;;; This is the set of characters to be ignored in the middle of english
;;; words being looked up.
;;; The �� below should be ��, but there seems to be an off-by-one error
;;; in the regexp code.

(defvar *edict-eigo-whitespace* "��-����-�� \n\t>;!:#?,.\"/@��-��")

(defvar *edict-eigo-characters* "[A-Za-z��-�ڣ�-��]"
  "These are the characters that eigo is made up of.")

(defvar *edict-unreadable-error* "While loading edict files: \"%s\" isn't readable!")

(defvar *edict-non-existent-error* "While loading edict files: \"%s\" doesn't exist!")



;;;
;;;Reads the edict files (the ones in the list *edict-files*) into a buffer
;;; called what the string *edict-buffer-name* is set to.
;;;
(defun edict-init ()
  "Reads the edict file into a buffer called *edict*.

This is done only once and the *edict-buffer* is created.
Use the function edict-force-init to reread the edict files."

  ;;create a match buffer.
  (if (not (get-buffer *edict-match-buffer-name*))
    (setq *edict-match-buffer* (get-buffer-create
				*edict-match-buffer-name*)))

  ;;Check that we have a list of strings, we will check that they are readable below.
  (if (not (listp *edict-files*))
    ;;report an error and fail...
    (error "The variable *edict-files* should be a list of paths to edict files!")
    ;;Check for strings if it was a list.
    (if (notevery 'stringp *edict-files*)
      (error "Something in the list *edict-files* is not a string (path)!")))

  ;;Create and read the edict files.
  (if (not (get-buffer *edict-buffer-name*))
    (progn
      (save-window-excursion
	;;First create the buffer and make it the current one
	(setq *edict-buffer* (get-buffer-create *edict-buffer-name*))
	(set-buffer *edict-buffer*)

	;;Read in the files from the list.
	(mapcar (function (lambda (filename)
			    (catch 'found-file
			      (dolist (dir load-path)
				(let ((file (expand-file-name filename dir)))
				  (if (file-exists-p file)
				      (if (file-readable-p file)
					  (throw 'found-file (edict-add-file file))
					(message (format *edict-unreadable-error* filename)))
				    (message (format *edict-non-existent-error*
						     filename))))
				;; If it's an absolute pathname, no need for a search.
				(when (or (equal (substring filename 0 1) "/")
					  (equal (substring filename 0 1) "~"))
				  (throw 'found-file nil))))))
		(if *edict-private-file*
		    (cons *edict-private-file* *edict-files*)
		  *edict-files*))

	;;If none of the files were readable 
	(if (= 0 (buffer-size))
	  (progn
	    (kill-buffer *edict-buffer*)
	    (error "No edict files found! Check value of *edict-files*.")))
	)))
  t)

;;;
;;;
;;;
(defun edict-force-init ()
  "This function always rereads the edict files even if there is a edict buffer, named
by the variable *edict-buffer-name*.

Usefule when you have updated the *edict-files* variable or corrupted the
edict buffer."
  (interactive)
  (kill-buffer *edict-buffer*)
  (edict-init))

;;;
;;; Add file filename to the current buffer with the begin end markers around that file...
;;;
(defun edict-add-file (filename)
  "This function adds a file, filename, to the current buffer.  A
*edict-file-begin-marker* and *edict-file-end-marker* are placed around
the file contents."
  (goto-char (point-max))
  (insert (format "%s %s\n" *edict-file-begin-marker* filename))
  (let ((pos (point)))
    (insert-file-contents filename)
    (goto-char (point-max))
    (insert (format "%s %s\n" *edict-file-end-marker* filename))
    (goto-char pos)
    (when (looking-at "�������� /\\([ -.0-\177]+\\)/")
      (message "Loaded dictionary %s."
	       (buffer-substring (match-beginning 1) (match-end 1))))
    (goto-char (point-max))))

;;;
;;; Remove any leading, trailing, or imbedded whitespace or other noise characters
;;; (such as the inserted ">" etc. used to denote inserted quotations in mail and
;;; news)
;;;
(defun edict-clean-up-kanji (key)
  (let ((start 0)
	(loc 0)
	(end (length key))
	(result "")
	(pattern (concat "[" *edict-kanji-whitespace* "]+")))
    (while (and (< start end) (setq start (string-match pattern key start)))
      (setq result (concat result (substring key loc start)))
      (setq loc (setq start (match-end 0))))
    (concat result (substring key loc))))

(defvar *edict-romaji-remaps* nil)
(setq *edict-romaji-remaps* 
      '(("��" . "a") ("��" . "b") ("��" . "c") ("��" . "d") ("��" . "e") ("��" . "f") ("��" . "g")
	("��" . "h") ("��" . "i") ("��" . "j") ("��" . "k") ("��" . "l") ("��" . "m")
	("��" . "n") ("��" . "o") ("��" . "p") ("��" . "q") ("��" . "r") ("��" . "s") ("��" . "t")
	("��" . "u") ("��" . "v") ("��" . "w") ("��" . "x") ("��" . "y") ("��" . "z")
	("��" . "A") ("��" . "B") ("��" . "C") ("��" . "D") ("��" . "E") ("��" . "F") ("��" . "G")
	("��" . "H") ("��" . "I") ("��" . "J") ("��" . "K") ("��" . "L") ("��" . "M")
	("��" . "N") ("��" . "O") ("��" . "P") ("��" . "Q") ("��" . "R") ("��" . "S") ("��" . "T")
	("��" . "U") ("��" . "V") ("��" . "W") ("��" . "X") ("��" . "Y") ("��" . "Z")))

;;;
;;; Lookup a mapping for zenkaku roman characters to ASCII.
;;;
(defun edict-in-remap-list (item list)
  "Look for ITEM in LIST; return first link in LIST whose car is `equal' to ITEM."
  (let ((ptr list)
        (done nil)
        (result '()))
    (while (not (or done (endp ptr)))
      (cond ((string= item (car (car ptr)))
             (setq done t)
             (setq result ptr)))
      (setq ptr (cdr ptr)))
    result))

;;;
;;; Remap zenkaku roman characters to ASCII.
;;;
(defun edict-remap-romaji (eigo-string)
  (let ((stop (length eigo-string))
	(current 0)
	(match nil)
	(result ""))
    (while (< current stop)
      (if (<  (+ 1 current) stop)
	(setq match (edict-in-remap-list (substring eigo-string current (+ 2 current)) *edict-romaji-remaps*))
	(setq match nil))
      (if match
	(progn
	  (setq result (concat result (cdr (car match))))
	  (setq current (+ 2 current)))
	(progn
	  (setq result (concat result (substring eigo-string current (1+ current))))
	  (setq current (1+ current)))))
    result))

;;;
;;;  Eliminate extra whitespace, newlines, punctuation, etc. which would
;;;  interfere with our dictionary lookup.
;;;
(defun edict-clean-up-eigo (key)
  (let ((start 0)
	(loc 0)
	(end (length key))
	(result "")
	(pattern (concat "[" *edict-eigo-whitespace* "]+")))
    (while (and (< start end)
		(setq start (string-match pattern key start)))
      (setq result (concat result (substring key loc start) " "))
      (setq loc  (setq start (match-end 0))))

    (setf result (concat result (substring key loc)))

    (edict-remap-romaji result)))

;;;
;;;  slightly specialized function to be changed when the real backward word things are included.
;;;
(defun edict-eigo-one-word (direction)
  "The function edict-eigo-one-word goes one word forward (direction > 0)
or backward (direction <= 0).  It assumes that is is looking at a word
when invoked.  It returns the point either at the beginning of a word or
at the whitespace after a word."
  (let ((stop-point (point))
	(stop nil))
    (if (> direction 0)
      ;;forward
      (progn
	(while (not stop)
	  (setq stop-point (point))
	  (if (< (point) (point-max))
	    (if (looking-at *edict-eigo-characters*)
	      (forward-char 1)
 	      (setq stop t))
	    (setq stop t))))
      ;;backward
      (progn
	(while (not stop)
	  (setq stop-point (point))
	  (if (> (point) (point-min))
	    (if (looking-at *edict-eigo-characters*)
	      (backward-char 1)
	      (progn
		(setq stop t)
		(forward-char 1)
		(setq stop-point (point))))
	    (setq stop t )))))
    stop-point))
    

;;;
;;; perham
;;;
(defun edict-find-word-at-point ()
  "Find-word-at-point tries to find an English word close to or behind
point.

If it does not find any word it reports an error."
  (let (start end)

    ;; Move backward for word if not already on one.
    (if (not (looking-at *edict-eigo-characters*))
      (re-search-backward *edict-eigo-characters* (point-min) 'stay))

    (if (looking-at *edict-eigo-characters*)
      (progn
	(setq start (edict-eigo-one-word -1))
	(setq end   (edict-eigo-one-word 1))
	
	(edict-clean-up-eigo (buffer-substring start end)))
      (error "Can't find English word!")
      )))

;;;
;;;
;;;
(defun edict-search-english (arg)
  "Attempts to translate the english word we are looking at. Picks the word 
in the same way as ispell, ie backs up from whitespace, and then expands.

Result is presented in a window that is not selected. Clear the window by
using a negative prefix argument.

If given an argument, adds an english word to the private dictionary."

  (interactive "P")
  (if arg
    (if (< (prefix-numeric-value arg) 0)
	(edict-restore-display)
      (edict-add-english))
    (let ((word (edict-get-english-word)))
      ;;Search if there is a word.
      (when word
	(edict-search-and-display word 'english)))))

;;; Return the english word, or nil
(defun edict-get-english-word ()
  (let (word real-word)

    ;;Find the word
    (setq word (edict-find-word-at-point))

    ;;ask the user if this is really the word that is interesting.
    (setq real-word (read-string
		     (format "Translate word (default \"%s\"): "
			     word)))
    (setq real-word (edict-clean-up-eigo real-word))
    (if (equal real-word "")
	(if (equal word "")
	    nil
	  word)
      real-word)))

;;;
;;;
;;;
(defun edict-search-kanji (arg min max)
  "Attempts to translate the Kanji sequence between mark and point.

Result is presented in a window that is not selected. Clear the window
with for instance C-X 1

Given a numeric argument, this adds the Kanji sequence to the user's
private dictionary."

  ;;Interactive, with a region as argument
  (interactive "P
r")

  ;;make sure that the dictionary is read
  (edict-init)

  (if arg
      (if (< (prefix-numeric-value arg) 0)
	  (edict-restore-display)
	(edict-add-kanji min max))
    (let ((word (edict-clean-up-kanji (buffer-substring min max))))
      (if (equal word "")
	  (error "No word to search for!")
	(edict-search-and-display word '���ܸ�))))
  t)

;;;
;;;
;;;
(defun edict-copy-of-current-line ()
  "Copy-of-current-line creates and returns a copy of the line
where point is. It does not affect the buffer it is working on,
except for moving the point around.

It leaves the point at the end of the line, which is fine for this
application."

  ;;Find the start and end of the current line
  (let ((line-start (progn (beginning-of-line) (point)))
	(line-end   (progn (end-of-line) (point))))

    ;;return a copy of his line, perham, is there something that
    ;; should be tested here?
    (buffer-substring line-start line-end)))


;;;
;;;
;;;
(defun edict-search (key buffer)
  "Searches the *edict-buffer* and returns a list of strings that are
the matches.

If there are no matches this string will be nil."

  ;;perham, should this really go here? Or what should we have? Look
  ;;at ispell.el...
  (save-window-excursion
    (message (format "Searching for word \"%s\"..." key))
    (let ((match-list nil))
      ;;select the database and goto to the first char
      (set-buffer buffer)
      (goto-char (point-min))
      ;;Search for lines that match the key and copy the over to the
      ;; match buffer.
      (while (edict-search-key key)
	(setq match-list (edict-union match-list (list (edict-copy-of-current-line)))))
      match-list)))

(defun edict-search-key (key)
  (search-forward			;Ken-ichi says that one cannot
					;use the re-search-forward
					;function with actually having
					;some reg exp in the starget string.
					;(concat "[\[/ 
					;]" key "[\]/ ]")
   key nil t))

;;;
;;;
;;;

(defvar *edict-previous-configuration* nil)

(defun edict-note-windows ()
  (or *edict-previous-configuration*
      (setq *edict-previous-configuration* (current-window-configuration))))

;;; This doesn't work yet; leave it set to 'top!
(defvar *edict-window-location* 'top
  "*Location to place edict matches window.  top or bottom.
Doesn't work yet.")

(defun edict-display (key-list match-list)
  "Edict-display displayes the strings in a separate window that is
not selected."
  (let* ((text-window (get-buffer-window (current-buffer)))
	 (edict-window (get-buffer-window *edict-match-buffer*))
	 ;; We have available some of this window's height plus any we've already
	 ;; already gotten.
	 (avail-height (+ (window-height text-window)
			  (if edict-window
			      (window-height edict-window)
			    0)))
	 ;; We limit the height to half of what's available, but no more than we need,
	 ;; and no less than window-min-height.  We must remember to include 1 line for
	 ;; the mode-line in our minimum figure.
	 (height (min (max window-min-height (+ (length match-list) 1))
		      (/ avail-height 2))))
    (if (not edict-window)
	(progn
	  ;; We don't have a window, so remember our existing configuration,
	  ;; and either find an acceptable window to split, or use the current
	  ;; window.
	  (edict-note-windows)
	  (let ((use-window (edict-find-acceptable-window text-window)))
	    (if use-window
		(setq edict-window use-window
		      text-window (split-window text-window height))
	      (setq edict-window text-window))))
      ;; We have a window already.  Just adjust its size appropriately.
      (unless (equal height (window-height edict-window))
	(let ((selected (selected-window)))
	  (select-window edict-window)
	  (enlarge-window (- height (window-height edict-window))))))
    (set-buffer *edict-match-buffer*)
    (let ((min (point-min)))
      ;; Replace everything.
      (erase-buffer)
      (mapcar (function (lambda (string-item)
			  (insert string-item)
			  (newline)))
	      match-list)
      (when (eq *edict-window-location* 'bottom)
	(let ((w text-window)
	      (setq text-window edict-window
		    edict-window text-window))))
      ;; OK, now let's move the exact matches to the top.
      (goto-char min)
      ;; Be careful to preserve the order.
      ;; An exact match is any of "^key ", "[key]", "/key/", or "/to key/".
      (dolist (key (reverse key-list))
	(let* ((pattern (concat "^" key " \\|\\[" key "\\]\\|\\/" key
				"\\/\\|\\/to " key "\\/" ))
	       (top-lines nil))
	  ;; First pull them out of the buffer into a list (top-lines).
	  ;; Then re-insert them at the top.
	  (while (re-search-forward pattern nil t)
	    (forward-line 0)
	    (let ((p (point)))
	      (forward-line 1)
	      (push (buffer-substring p (point)) top-lines)
	      (delete-region p (point))))
	  (goto-char min)
	  (mapcar 'insert top-lines)))
      ;; OK, display it all.
      (select-window text-window)
      (set-window-buffer edict-window *edict-match-buffer*)
      (set-window-start edict-window min)))
  t)

;;; Find a window which is of acceptable size to split.
;;; It must be at least twice window-min-height.
(defun edict-find-acceptable-window (window)
  (catch 'no-window
    (let ((new-window window))
      (while (< (window-height new-window) (* 2 window-min-height))
	(setq new-window (next-window new-window))
	(when (eq new-window window)
	  (throw 'no-window nil)))
      new-window)))

;;; Try to put the display back the way it was before showing matches.
(defun edict-restore-display ()
  "Remove the edict windows."
  (when *edict-previous-configuration*
    (set-window-configuration *edict-previous-configuration*))
  (setq *edict-previous-configuration* nil)
  t)

;;; Variables to remember the last insertion of a match into our
;;; buffer, for later replacement.

(defvar edict-last-language nil)
(defvar edict-insert-last-start)
(defvar edict-insert-last-end)

;;;
;;;
;;;
(defun edict-search-and-display (key &optional from-language)
  "Edict-search-and-display searches for matches to the argument key.
If there are any matches these are displayed in a window that is not
selected. This window can be removed with C-X 1."
  (edict-init)
  ;; Remember the last language looked up, so edict-insert can pick the
  ;; right one.
  (setq edict-last-language from-language)
  (save-excursion
    (let ((match-list nil)
	  (one-char-keys nil)
	  (key-list (edict-expand-string key () () (or from-language '���ܸ�))))
      ;; Sort them into the order we'd like exact matches to appear.
      (setq key-list (sort key-list (function (lambda (x y)
						(let ((lx (length x))
						      (ly (length y)))
						  (if (= lx ly)
						      (string-lessp x y)
						    (> lx ly)))))))
      ;; For all the possibilities
      (dolist (key key-list)
	;; Search for matches.  We exlude any one-character keys on the theory that they're
	;; likely to be uninteresting fragments.
	(if (string-match "^[��-�]$" key) ;1 char
	    (push key one-char-keys)
	  (setq match-list (edict-union match-list (edict-search key *edict-buffer*)))))
      ;; If we didn't get anything, we can try including the one-char keys.
      (or match-list
	  (dolist (key one-char-keys)
	    (setq match-list (edict-union match-list (edict-search key *edict-buffer*)))))
      (if (not match-list)
	  (edict-delete-matches-window))
      (edict-display key-list match-list))
    (message "Found it!")))

(defun edict-insert (arg)
  "Insert the last value looked up at the current position.  If repeated,
replace with the next possibility.  If given an argument N, use the
Nth possibility.  Inserts in the opposite language from what was looked up,
unless the argument is negative."
  (interactive "P")
  ;; If we were given a negative argument, we need to switch languages.
  (cond ((null arg))
	((> (prefix-numeric-value arg) 0))
	(t (case arg
	     (- (setq arg nil))
	     (otherwise (setq arg (list (- (prefix-numeric-value arg))))))
	   (setq edict-last-language
		 (ecase edict-last-language
		   (english '���ܸ�)
		   (���ܸ� 'english)))))
  (ecase edict-last-language
    (english (edict-insert-���ܸ� arg))
    (���ܸ� (edict-insert-english arg))))

(defun edict-insert-english (arg)
  "Insert the last english word looked up at the current position.
If repeated, replace with the next possibility.  If given an argument N,
use the Nth possibility."
  (interactive "P")
  (or *edict-match-buffer*
      (error "You must first look up a word."))
  (let ((value nil))
    (save-excursion
      (set-buffer *edict-match-buffer*)
      ;; If we're going to a specific one, always count from the beginning.
      (when arg
	(goto-char (point-min)))
      ;; If the last command was this, then we're going on to the next possibility.
      ;; Otherwise, start at the beginning.
      (case last-command
	(edict-insert-english)
	(t (goto-char (point-min))))
      ;; Seach forward for /<definitition>/  If we don't find one, start over from the
      ;; beginning.
      (unless (re-search-forward "/\\([^/\n]+\\)/" (point-max) t (prefix-numeric-value arg))
	(goto-char (point-min))
	(unless (or arg
		    (re-search-forward "/\\([^/\n]+\\)/" (point-max) t))
	  (error "No match numbered %d found." (prefix-numeric-value arg))))
      ;; Extract the match.  Leave ourselves just before the final /,
      ;; so if it starts a new definition, we'll find it.
      (goto-char (match-end 1))
      (setq value (buffer-substring (match-beginning 1) (match-end 1))))
    ;; If we inserted one of our languages, then we should delete the old
    ;; one first.
    (case last-command
      ((edict-insert-english edict-insert-���ܸ�)
       (delete-region edict-insert-last-start edict-insert-last-end)))
    ;; Insert, remembering where we did it, so it can be replaced if we
    ;; repeat the command.
    (setq edict-insert-last-start (point-marker))
    (insert value)
    (setq edict-insert-last-end (point-marker)))
  ;; Remember this as the last command, not edict-insert.
  (setq this-command 'edict-insert-english)
  t)

(defun edict-insert-���ܸ� (arg)
  "Insert the last ���ܸ� word looked up at the current position.
If repeated, replace with the next possibility.  If given an argument N,
use the Nth possibility."
  (interactive "P")
  (or *edict-match-buffer*
      (error "You must first look up a word."))
  (let ((value nil))
    (save-excursion
      (set-buffer *edict-match-buffer*)
      ;; If we're going to a specific one, always count from the beginning.
      (when arg
	(goto-char (point-min)))
      ;; If the last command was this, then we're going on to the next possibility.
      ;; Otherwise, start at the beginning.
      (case last-command
	(edict-insert-���ܸ�)
	(t (goto-char (point-min))))
      ;; Seach forward for a word at the start of a line.  If we don't find one,
      ;; start over from the beginning.
      (unless (re-search-forward "^\\(\\(\\ch\\|\\ck\\|\\cK\\|\\cc\\|\\cC\\)+\\)[ \t]" 
				 (point-max) t (prefix-numeric-value arg))
	(goto-char (point-min))
	(unless (or arg
		    (re-search-forward "^\\(\\(\\ch\\|\\ck\\|\\cK\\|\\cc\\|\\cC\\)+\\)[ \t]"
				       (point-max) t))
	  (error "No match numbered %d found." (prefix-numeric-value arg))))
      (goto-char (match-end 1))
      (setq value (buffer-substring (match-beginning 1) (match-end 1))))
    ;; If we inserted one of our languages, then we should delete the old
    ;; one first.
    (case last-command
      ((edict-insert-���ܸ� edict-insert-english)
       (delete-region edict-insert-last-start edict-insert-last-end)))
    ;; Insert, remembering where we did it, so it can be replaced if we
    ;; repeat the command.
    (setq edict-insert-last-start (point-marker))
    (insert value)
    (setq edict-insert-last-end (point-marker)))
  ;; Remember this as the last command, not edict-insert.
  (setq this-command 'edict-insert-���ܸ�)
  t)

;;; Remove the matches window from the screen.
;;; This is harder than you'd think.
(defun edict-delete-matches-window ()
  (interactive)
  (let ((window (get-buffer-window *edict-match-buffer*)))
    (when window
      (let* ((selected (selected-window))
	     (next (previous-window window))
	     (height (window-height window))
	     (nedges (window-edges next))
	     (tedges (window-edges window)))
	(delete-window window)
	;; The following is sheer magic.  Deleting a window is not
	;; an inverse to splitting a window.  The space is returned
	;; not to the window below, OR to the window above, but
	;; rather is divided between them.
	(when (and (equal (car nedges) (car tedges))
		   (< (car (cdr nedges)) (car (cdr tedges))))
	  (select-window next)
	  (shrink-window (/ (- height 1) 2))
	  (select-window selected))))
    (error "No matches for key \"%s\"." key)))

;;; The previous configuration before adding an entry to a private dictionary.
(defvar edict-previous-window-configuration nil)
;;; The previously-selected buffer before adding an entry.
(defvar edict-previous-buffer nil)
;;; The filename of the file read in to add an entry to.
(defvar edict-filename nil)


;;; Add an entry to a particular file, and update *edict-buffer*.
;;; Any of kanji/yomi/eigo may be omitted.  The user will be given
;;; an oportunity to edit and then it will be saved.

(defun edict-add-entry-to-file (filename kanji yomi eigo)
  (edict-init)
  (setq filename (expand-file-name filename))
  (let* ((previous-buffer (current-buffer))
	 (buffer (find-file-noselect filename))
	 (window (get-buffer-window buffer)))
    (set-buffer buffer)
    ;; If it's a new file, give it a version string to print on loadup.
    (when (equal (point-min) (point-max))
      (insert (format "�������� /%s's private dictionary/\n"
		      (user-login-name))))
    ;;  Unless it's already in edict-edit mode, put it in that mode.
    ;; This gives us our fancy electric-dictionary editing.
    (unless (eq major-mode 'edict-edit-mode)
      (edict-edit-mode))
    ;; Unless we already have a configuration to go back to, remember
    ;; this one.
    (unless edict-previous-window-configuration
      (setq edict-previous-window-configuration
	    (current-window-configuration)))
    (unless edict-previous-buffer
      (setq edict-previous-buffer previous-buffer))
    ;; Remember the filename, so we can update it in the *edict* buffer
    ;; when we finish.
    (setq edict-filename filename)
    (if window
	(select-window window)
      (split-window nil 4))
    (goto-char (point-max))
    (edict-insert-entry kanji yomi eigo)
    ;; Go into henkan mode if appropriate
    (switch-to-buffer buffer)
    (edict-set-henkan (or (null kanji) (null yomi)))))


;;; Turn on or off henkan
(defun edict-set-henkan (henkan-flag)
  (cond
    ;;EGG
    ((fboundp 'egg:mode-line-display)
     (setq egg:*mode-on* henkan-flag
	   egg:*input-mode* t)
     (egg:mode-line-display)
     )
    ;;SKK
    ((fboundp 'skk-version)
     ;;This is a crude way of doing it, but it should give no secondary effects.
     (skk-mode (if henkan-flag 1 -1))
     )
    ))

;;; Insert a dictionary entry at point.
(defun edict-insert-entry (kanji yomi eigo)
  ;; Make sure this is on a line of its own.
  (let ((p (point)))
    (beginning-of-line)
    (unless (equal p (point))
      (end-of-line)
      (newline)))
  ;; Now insert a standard entry.
  (let ((start (point))
	(p nil))
    ;; Insert a new entry, leaving out any items which are nil,
    ;; and also leaving out the yomi if the entry consists of only kana.
    ;; "���ܸ�"
    (if kanji
	(insert kanji)
      (setq p (point)))
    (when yomi
      (unless (string-match "^\\(\\ch\\|\\cH\\|\\ck\\|\\cK\\)+$" yomi)
	(error "yomi must be in kana: %s." yomi)))
    ;; "���ܸ� [�ˤۤ�]"
    (cond ((and kanji
		(string-match "^\\(\\ch\\|\\cH\\|\\ck\\|\\cK\\)+$" kanji)))
	  (t (insert " [")
	     (if yomi
		 (insert yomi)
	       (if (not p)
		   (setq p (point))))
	     (insert "]")))
    ;; "���ܸ� [�ˤۤ�] /Japanese language/"
    (cond ((null eigo)
	   (insert " /")
	   (unless p (setq p (point))))
	  ((stringp eigo)
	   (insert " /" eigo))
	  ((consp eigo)
	   (insert " ")
	   (dolist (def eigo)
	     (insert "/")
	     (insert def)))
	  (t (error "not a string or list of strings: %s" eigo)))
    (insert "/\n")
    ;; Go to the first un-filled-in field.
    (goto-char (or p start))))

;;; Inverse of edict-insert-entry.  Parse an entry.
;;; (multiple-value-bind (kanji yomi english) (edict-parse-entry)
;;;    (edict-insert-entry kanji yomi english))
;;; duplicates the current line's entry.

(defun edict-parse-entry ()
  (let ((kanji nil)
	(yomi nil)
	(english nil)
	(start nil)
	(p nil)
	(end nil))
    (save-excursion
      (end-of-line)
      (setq end (point))
      (beginning-of-line)
      (setq start (point))
      (search-forward " " end)
      (setq p (1- (point)))
      (when (> p start)
	(setq kanji (buffer-substring start p)))
      ;; Pick up the [yomi] if there are any.
      (when (re-search-forward "\\[\\(\\ch\\|\\cH\\|\\ck\\|\\cK\\)+\\]" end t)
	(setq yomi (buffer-substring (match-beginning 1) (match-end 1)))
	(goto-char (match-end 0)))
      ;; Collect up all the definitions.
      (while (re-search-forward "/\\([^/\n]+\\)/" end t)
	(goto-char (match-end 1))
	(push (buffer-substring (match-beginning 1) (match-end 1)) english)))
    (values kanji yomi english)))

(defvar edict-edit-mode-map ()
  "Mode map used by edict-add-english/kanji.")

;;; Initialize our mode map.
(unless edict-edit-mode-map
  (setq edict-edit-mode-map (make-keymap))
  (dotimes (i 128)
    ;; I don't know how to invoke multi-char commands, so don't hook
    ;; those.
    (unless (consp (aref edict-edit-mode-map i))
      (setf (aref edict-edit-mode-map i) 'edict-standin)))
  (setf (aref edict-edit-mode-map 3) nil
	(aref edict-edit-mode-map 24) nil
	(aref edict-edit-mode-map 27) nil)
  (define-key edict-edit-mode-map "\C-c\C-c" 'edict-exit)
  (define-key edict-edit-mode-map "\C-x\C-s" 'edict-exit)
  (define-key edict-edit-mode-map "\t" 'edict-tab)
  (define-key edict-edit-mode-map "\r" 'edict-new-entry)
  (define-key edict-edit-mode-map "\C-A" 'edict-beginning-of-line)
  (define-key edict-edit-mode-map "\C-E" 'edict-end-of-line)
  (define-key edict-edit-mode-map "\C-x\C-s" 'edict-exit)
  (define-key edict-edit-mode-map "\C-x\C-s" 'edict-exit)
  (define-key edict-edit-mode-map "\C-x\C-s" 'edict-exit)
  (define-key edict-edit-mode-map "[" 'edict-open-bracket)
  (define-key edict-edit-mode-map "]" 'edict-close-bracket)
  (define-key edict-edit-mode-map "/" 'edict-slash))

(defun edict-edit-mode ()
  "Major mode for editing edict entries.
TAB      Tab to next field in this entry.
RETURN   Start a new entry on the next line.
c-A	 Edit the kanji field, and start entering kanji.
c-E	 Go to the end, and start editing english.
C-c C-c	 Install the edited changes & save the file.
C-x C-s  Install the edited changes & save the file.
"
  (interactive)
  (kill-all-local-variables)
  ;; Associate these with the buffer.
  (make-local-variable 'edict-previous-window-configuration)
  (make-local-variable 'edict-previous-bufffer)
  (make-local-variable 'edict-filename)
  (set-syntax-table text-mode-syntax-table)
  (use-local-map edict-edit-mode-map)
  (setq local-abbrev-table text-mode-abbrev-table)
  (setq major-mode 'edict-edit-mode)
  (setq mode-name "Edict")
  (setq paragraph-start "^\\|$")
  (setq paragraph-separate "^\\|$")
  (run-hooks 'text-mode-hook))

;;; Automagically pick the right mode, based on where we are in the string.
;;; That's henkan mode when we're in the entry or yomi sections, and english
;;; in the translation section.
(defun edict-auto-set-henkan ()
  (save-excursion
    (let ((x (point))
	  (end nil))
      (end-of-line)
      (setq end (point))
      (beginning-of-line)
      (edict-set-henkan
       (or (looking-at "$")
	   (when (re-search-forward "[]/]" end t)
	     (<= x (match-beginning 0))))))))

(defun edict-standin ()
  "Invoke the command we would otherwise have invoked, after being sure
we're in the right mode."
  (interactive)
  (setq this-command (aref global-map last-command-char))
  (edict-execute-dictionary-command (function (lambda ()
						(command-execute this-command)))))

(defun edict-execute-dictionary-command (function)
  (edict-auto-set-henkan)
  (let ((buffer (current-buffer)))
    ;; Canonicalize the end to end in exactly one slash.
    (unless (<= (point) (point-min))
      (save-excursion
	(backward-char 1)
	(when (looking-at "//\n")
	  (forward-char 1)
	  (delete-char 1))))
    (funcall function)
    ;; Canonicalize the end of the line to end in exactly one slash.
    (save-excursion
      (end-of-line)
      (delete-horizontal-space)
      (unless (<= (point) (point-min))
	(backward-char 2)
	(while (looking-at "//")
	  ;; Two in a row; delete the second.
	  (forward-char 1)
	  (delete-char 1)
	  (backward-char 2))
	(forward-char 1)
	(unless (looking-at "\n")
	  (unless (looking-at "[/\n]")
	    (end-of-line)
	    (unless (edict-line-has-english)
	      (insert " /"))
	    (insert ?/)))))
    ;; Then if we are at the end, make it end in two, for the sake of visual feedback.
    ;; Except if we're on a blank line, don't add anything.
    (unless (<= (point) (point-min))
      (unless (save-excursion
		(end-of-line)
		(backward-char 1)
		(looking-at "\n"))
	(when (looking-at "\n")
	  (insert "/")
	  (backward-char 1))
	(save-excursion
	  (end-of-line)
	  ;; Make sure there's a trailing newline.
	  (when (>= (point) (point-max))
	    (newline)
	    (backward-char 1))
	  (let ((end (point)))
	    (beginning-of-line)
	    (when (search-forward "/" end t)
	      (when (looking-at "\n")
		(insert "/")))))))
    ;; Only set the henkan if we're still in the same buffer.
    (when (eq buffer (current-buffer))
      (edict-auto-set-henkan))))

(defun edict-line-has-english (&optional complete)
  (save-excursion
    (let ((p (point)))
      (end-of-line)
      (let ((end (point)))
	(goto-char p)
	(beginning-of-line)
	(if complete
	    (re-search-forward "/[^/\n]+/" end t)
	  (re-search-forward "/" end t))))))

(defvar *brackets-allowed-in-english* nil
  "*Allow brackets in the english section of dictionary entries, if non-null.")

(defun edict-open-bracket ()
  "Begin editing the yomi section of the entry, at the beginning of the entry.
Self-inserts if in the english section."
  (interactive)
  (edict-execute-dictionary-command (function (lambda ()
						(edict-char-bracket t)))))

(defun edict-close-bracket ()
  "Begin editing the yomi section of the entry, at the end of the entry.
Self-inserts if in the english section.."
  (interactive)
  (edict-execute-dictionary-command (function (lambda ()
						(if (looking-at "\\]")
						    (edict-tab)
						  (edict-char-bracket nil))))))

(defun edict-char-bracket (open-p)
  (let ((p (point)))
    (end-of-line)
    (let ((end (point)))
      (beginning-of-line)
      (cond ((and *brackets-allowed-in-english*
		  (save-excursion
		    (re-search-forward "/[^\n/]*/" end t))
		  (<= (match-beginning 0) p))
	     (goto-char p)
	     (edict-standin))
	    ((re-search-forward "\\[\\(\\ch\\|\\cH\\|\\ck\\|\\cK\\)*\\]" end t)
	     (goto-char (or (if open-p
				(match-beginning 1)
			      (match-end 1))
			    ;; Empty
			    (1+ (match-beginning 0)))))
	    ((re-search-forward "[ \t]" end t)
	     (goto-char (match-beginning 0))
	     (insert " []")
	     (backward-char 1))
	    (t (goto-char p)
	       (edict-standin))))))

(defun edict-slash ()
  "Begin editing the english section of the entry, at the start of the entry.
Self-inserts if in the english section."
  (interactive)
  (edict-execute-dictionary-command (function edict-slash-internal)))

(defun edict-slash-internal ()
  (if (looking-at "/\n")
      (forward-char)
    (let ((p (point)))
      (end-of-line)
      (let ((end (point)))
	(beginning-of-line)
	(cond ((and (save-excursion
		      (re-search-forward "/[^/\n]*/" end t))
		    (<= (match-beginning 0) p))
	       (goto-char p)
	       (edict-standin))
	      ((search-forward "/" end t))
	      ;; On an empty line, just insert a definition.
	      ((looking-at "$")
	       (insert " //")
	       (backward-char 1))
	      ;; Otherwise, this line has no english, go to the end and add one.
	      (t (end-of-line)
		 (backward-char 1)
		 (unless (looking-at " ")
		   (insert " "))
		 (insert "//")
		 (backward-char 1)))))))

(defun edict-tab ()
  "Tab to the next edict field in this entry.
At the end, wraps back to the beginning.."
  (interactive)
  (edict-execute-dictionary-command (function edict-tab-internal)))

(defun edict-tab-internal ()
  (let ((p (point))
	(end nil))
    (end-of-line)
    (setq end (point))
    (goto-char p)
    (cond ((re-search-forward "[ \t]\\(\\[\\)\\|\\(/\\)" end t)
	   (let ((f-begin (or (match-beginning 1) (match-beginning 2)))
		 (f-end (or (match-end 1) (match-end 2))))
	     (goto-char f-begin)
	     (edict-set-henkan (looking-at "\\["))
	     (goto-char f-end)))
	  (t (beginning-of-line)
	     (edict-set-henkan t)))))

(defun edict-beginning-of-line ()
  "Go to the beginning of the edict entry."
  (interactive)
  (edict-execute-dictionary-command (function (lambda ()
						(beginning-of-line)
						(edict-set-henkan t)))))

(defun edict-end-of-line ()
  "Go to the beginning of the edict entry."
  (interactive)
  (edict-execute-dictionary-command (function (lambda ()
						(end-of-line)
						(edict-set-henkan nil)))))

(defun edict-new-entry (arg)
  "Start a new edict entry on the next line.
If given an argument, copies the word but not the yomi or english.
If given an argument > 4 (i.e. c-U c-U), copies the word and definition,
but not the yomi."
  (interactive "P")
  (edict-execute-dictionary-command (function (lambda ()
						(edict-new-entry-internal arg)))))

(defun edict-new-entry-internal (arg)
  (end-of-line)
  ;;clean up in the dictionary to save space.
  (delete-horizontal-space)
  ;;first check that the last thing on this line is a '/', otherwise add one.
  (unless (<= (point) (point-min))
    (backward-char)
    (unless (looking-at "/")
      (end-of-line)
      (insert "/"))
    (multiple-value-bind (kanji yomi english)
	(edict-parse-entry)
      (end-of-line)
      (if (>= (point) (point-max))
	  (newline)
	(forward-char 1))
      (cond ((null arg)
	     (edict-insert-entry nil nil nil))
	    ((<= (prefix-numeric-value arg) 4)
	     (edict-insert-entry kanji nil nil))
	    (t (edict-insert-entry kanji nil english))))))

(defun edict-exit ()
  "Exit the editing of a private edict file, saving the buffer and updating the
running copy of the dictionary, and restoring the window configuration."
  (interactive)
  (save-buffer)
  (let* ((buffer (current-buffer))
	 (edict-private-buffer (find-file-noselect (expand-file-name *edict-private-file*)))
	 (filename (or edict-filename (buffer-file-name edict-private-buffer)))
	 (configuration edict-previous-window-configuration)
	 (previous-buffer edict-previous-buffer))
    (setq edict-previous-window-configuration nil
	  edict-previous-buffer nil)
    (set-buffer *edict-buffer*)
    (goto-char (point-min))
    (search-forward (format "%s %s" *edict-file-begin-marker* filename))
    (forward-line)
    (let ((loc (point)))
      (search-forward (format "%s %s" *edict-file-end-marker* filename))
      (forward-line 0)
      (delete-region loc (point))
      (goto-char loc)
      (insert-buffer buffer)
      (when configuration
	(set-window-configuration configuration))
      (when previous-buffer
	(switch-to-buffer previous-buffer)))))

(defun edict-add-word ()
  "Add any word to the private dictionary."
  (interactive)
  (edict-add-entry-to-file *edict-private-file* nil nil nil))

(defun edict-add-english ()
  "Add the english word at point to the dictionary."
  (interactive)
  (let ((word (edict-get-english-word)))
    (when word
      (edict-add-entry-to-file *edict-private-file* nil nil word))))

(defun edict-add-kanji (min max)
  "Add the region as a kanji entry in the dictionary."
  (interactive "r")
  (edict-add-entry-to-file *edict-private-file*
			   (edict-clean-up-kanji (buffer-substring min max))
			   nil nil))

;;; Table of morphological rules.
(defvar *edict-syntax-types* nil)

;;; defstruct's defsetfs should expand into this; sigh.

(eval-when (eval load compile)
(defstruct edict-syntax-type
  name
  rules)
)

(defun get-edict-syntax-type (name)
  (if (symbolp name)
      (catch 'found-it
	(dolist (s *edict-syntax-types*)
	  (when (eq (edict-syntax-type-name s) name)
	    (throw 'found-it s)))
	(let ((new (make-edict-syntax-type :name name :rules ())))
	  (push new *edict-syntax-types*)
	  new))
    name))

(eval-when (eval load compile)
(defstruct edict-rule
  name
  pattern				;Pattern which it must match
  filter				;Syntactic filter on previous form
  function				;Function to transform the input
  additional-args			;Arguments to transform function
  from-syntax-types			;Syntaxes for which this is vali
  to-syntax-types)			;Syntaxes to consider after this rule.
)

;;; Delete all occurrances of a rule from the rule base.
(defun edict-delete-rule (name)
  (dolist (s *edict-syntax-types*)
    (let ((old (edict-get-rule-from-syntax-type name s)))
      (when old
	(setf (edict-syntax-type-rules s)
	      (delq old (edict-syntax-type-rules s)))))))

;(defun edict-decircularize-rules ()
;  (interactive)
;  (dolist (s *edict-syntax-types*)
;    (dolist (r (edict-syntax-type-rules s))
;      (setf (edict-rule-from-syntax-types r)
;	    (mapcar (function (lambda (type)
;				(if (symbolp type)
;				    type
;				  (edict-syntax-type-name type))))
;		    (edict-rule-from-syntax-types r)))
;      (setf (edict-rule-to-syntax-types r)
;	    (mapcar (function (lambda (type)
;				(if (symbolp type)
;				    type
;				  (edict-syntax-type-name type))))
;		    (edict-rule-to-syntax-types r))))))
;
;(defun edict-circularize-rules ()
;  (interactive)
;  (dolist (s *edict-syntax-types*)
;    (dolist (r (edict-syntax-type-rules s))
;      (setf (edict-rule-from-syntax-types r)
;	    (mapcar (function (lambda (type)
;				(if (symbolp type)
;				    (get-edict-syntax-type type)
;				  type)))
;		    (edict-rule-from-syntax-types r)))
;      (setf (edict-rule-to-syntax-types r)
;	    (mapcar (function (lambda (type)
;				(if (symbolp type)
;				    (get-edict-syntax-type type)
;				  type)))
;		    (edict-rule-to-syntax-types r))))))

(defun edict-add-rule (name rule)
  (edict-delete-rule name)
  (dolist (s (edict-rule-from-syntax-types rule))
    (push rule (edict-syntax-type-rules s))))

(defun edict-get-rule-from-syntax-type (name syntax-type)
  (catch 'edict-get-rule
    (dolist (rule (edict-syntax-type-rules syntax-type))
      (if (eq name (edict-rule-name rule))
	  (throw 'edict-get-rule rule)))))

(defmacro define-edict-rule (name pattern fromto function &rest additional-args)
  ;; First, some compatibility stuff.
  (let ((filter nil)
	(from nil)
	(to nil))
    (when (stringp fromto)
      (setq filter fromto
	    fromto nil))
    (when (null fromto)
      (setq fromto '(���ܸ� ���ܸ�)))
    (setq from (first fromto)
	  to (second fromto))
    (unless (listp from)
      (setq from (list from)))
    (unless (listp to)
      (setq to (list to)))
    (unless (string-match "^\\^\\|\\$$" pattern)
      (error "Rule %s: pattern must start with ^ or end with $: \"%s\""
	     name pattern))
    (when filter
      (unless (stringp filter)
	(error "Rule %s: filter must be a regexp"
	       name)))
    (` (define-edict-rule-internal '(, name) '(, pattern) '(, filter)
	 '(, from) '(, to)
	 (function (, function)) (function (, additional-args))))))

(defun define-edict-rule-internal (name pattern filter 
					from-syntax-types to-syntax-types
					function additional-args)
  (unless (string-match "^\\^\\|\\$$" pattern)
    (error "Rule %s: pattern must start with ^ or end with $: \"%s\""
	   name pattern))
  (when filter
    (unless (stringp filter)
      (error "Rule %s: filter must be a regexp"
	     name)))
  (let ((from-types nil)
	(to-types nil))
    (dolist (f from-syntax-types)
      (push (get-edict-syntax-type f) from-types))
    (dolist (to to-syntax-types)
      (push (get-edict-syntax-type to) to-types))
    (edict-add-rule name 
		    (make-edict-rule :name name
				     :pattern pattern
				     :filter filter
				     :from-syntax-types from-types
				     :to-syntax-types to-types
				     :function function
				     :additional-args additional-args))
    name))

(defun edict-subst-affix (string &rest affixes)
  (let ((x nil)
	(i 1)
	(prev -1)
	(result ""))
    (dolist (x affixes)
      (let ((pos (match-beginning i)))
	(cond ((eq x 'edict-identity))
	      ((eq x 'edict-ignore)
	       (setq result (concat result
				    (substring string
					       (max prev 0) (match-beginning i)))
		     prev (match-end i)))
	      ((and (symbolp x) (fboundp x))
	       (setq result
		     (concat result
			     (substring string (max prev 0) (match-beginning i))
			     (funcall x (substring string
						   (match-beginning i)
						   (match-end i))))))
	      ((not (stringp x))
	       (error "%s is not a string or function name in edict-subst-affix"
		      x))
	      ((and pos (>= pos prev))
	       (setq result (concat result
				    (substring string
					       (max prev 0)
					       (match-beginning i))
				    x))
	       (setq prev (match-end i))))
	(incf i)))
    (concat result (substring string (max prev 0)))))

;;; Takes a series of alternating pairs of substitution functions
;;; and arguments for those substitution functions.  This can be
;;; used to algorithmically replace certain parts (typically involving
;;; changing an ���� to ���� final character.

(defun edict-subst-modified-affix (string &rest affixes)
  (let ((fun nil)
	(args nil)
	(i 1)
	(prev -1)
	(result ""))
    (while affixes
      (setq fun (car affixes)
	    args (car (cdr affixes))
	    affixes (cdr (cdr affixes)))
      (let ((pos (match-beginning i)))
	(cond ((eq fun 'edict-identity))
	      ((eq fun 'edict-ignore)
	       (setq result (concat result
				    (substring string
					       (max prev 0)
					       (match-beginning i)))
		     prev (match-end i)))
	      ((not (or (stringp fun) (and (symbolp fun) (fboundp fun))))
	       (error "%s is not a string or function name in %s" 
		      'edict-subst-modified-affix
		      x))
	      ((and pos (>= pos prev))
	       (setq result (concat result
				    (substring string (max prev 0) pos)
				    (apply fun (substring string 
							  (match-beginning i)
							  (match-end i))
					   args)))
	       (setq prev (max prev (match-end i)))))
	(incf i)))
    (concat result (substring string (max prev 0)))))

;;; Ignore this piece
(defun edict-ignore (affix) "")

;;; Keep this piece
(defun edict-identity (affix) affix)

;;; Substitute for this piece
(defun edict-subst (affix data)
  data)

;;; More or less a guon table, for converting doshi suffixes.
(defvar *edict-doshi-suffix*
  '(["��" "��" "��" "��" "��"];; u -> wa; kau->kawanai
    ["��" "��" "��" "��" "��"]
    ["��" "��" "��" "��" "��"]
    ["��" "��" "��" "��" "��"]
    ["��" "��" "��" "��" "��"]
    ["��" "��" "��" "��" "��"]
    ["��" "��" "��" "��" "��"]
    ["��" "��" "��" "��" "��"]
    ["��" "��" "��" "��" "��"]
    ["��" "��" "��" "��" "��"]
    ["��" "��" "��" "��" "��"]
    ["��" "��" "��" "��" "��"]
    ["��" "��" "��" "��" "��"]))

(defun edict-modify-verb (suffix from to)
  (catch 'exit
    (dolist (b *edict-doshi-suffix*)
      (if (equal suffix (aref b from))
	  (throw 'exit (aref b to))))
    (throw 'skip-rule nil)))

;;; Set this to true for debugging.
(defvar *edict-expand-string-trace* nil)  

;;; This returns a list of the results of applying all rules whose
;;; patterns match, to all levels of recursion.
(defun edict-expand-string (string &optional others previous syntax)
  (let* ((result nil)
	 (syntax (or syntax '���ܸ�))
	 (stype (get-edict-syntax-type syntax)))
    (dolist (rule (edict-syntax-type-rules stype))
      (when (string-match (edict-rule-pattern rule) string)
	(catch 'skip-rule
	  (unless (and previous
		       (edict-rule-filter rule)
		       (edict-filter-rule rule previous))
	    (let ((temp (apply (edict-rule-function rule) string
			       (edict-rule-additional-args rule))))
	      (unless (or (equal temp string)
			  (edict-find temp others)
			  (edict-find temp result))
		(when *edict-expand-string-trace*
		  (read-string (format "%s: %s -> %s -:" 
				       (edict-rule-name rule)
				       string temp)))
		(setq result
		      (edict-union (edict-expand-string-recurse
				    temp (cons string (append result others))
				    string rule)
				   result))))))))
    (if (edict-find string result)
	result
      (cons string result))))

(defun edict-expand-string-recurse (string others previous rule)
  (edict-expand-string-syntaxes string others previous 
				(edict-rule-to-syntax-types rule)))

(defun edict-expand-string-syntaxes (string others previous syntaxes)
  (let ((result nil))
    (dolist (syntax syntaxes)
      (setq result
	    (edict-union (edict-expand-string string
					      (append result others)
					      previous
					      syntax)
			 result)))
    result))


;;; Returns T if the rule should not be run, because of the past
;;; history of expansions.  I.e. if something started out with ��
;;; on the end, and we've made it into an adjective, we should disable
;;; any expansions based on it being a the conjunctive/stem form of a verb.
;;; This is done purely based on the most immediately preceding expansion,
;;; because that is what determined the sense of the word.

(defun edict-filter-rule (rule previous)
  (let ((filter (edict-rule-filter rule)))
    (cond ((null filter) nil)
	  ((null previous) nil)
	  ((stringp filter)
	   (string-match filter previous))
	  ((symbolp filter)
	   (funcall filter frob))
	  ((consp filter)
	   (apply (car filter) frob (cdr filter)))
	  (t (error "Bogus filter in rule %s: %s" (edict-rule-name rule) filter)))))


(defun edict-find (elt list)
  (catch 'edict-find
    (dolist (test list)
      (when (equal elt test)
	(throw 'edict-find test)))))

(defun edict-union (set1 set2)
  (let ((result set2))
    (dolist (frob set1)
      (unless (edict-find frob set2)
	(setq result (cons frob result))))
    result))

;;; The syntax of the rules is:
;;; (define-edict-rule name <pattern> <conversion-function> <conversion-data>).
;;; 
;;;  <pattern> is a regular expression, with the parts to be substituted
;;;  being denoted by \\(<subpattern>\\).
;;; 
;;;  <conversion function> is a funtion responsible for determining the replacements.
;;;  The current choices are edict-subst-modified-affix and edict-subst-affix.
;;;  These functions are called just after doing match-string, so the regexp variables
;;;  are set up.  They are applied to the string, and <conversion-data>.  These functions
;;;  are responsible for determining and performing the substitutions to be made, and
;;;  returning a list of possiblities.
;;; 
;;;  edict-subst-affix is the simpler case.  It takes as conversion data one string
;;;  for each subpattern in the pattern.  This string will be used in place of the
;;;  original.
;;; 
;;;  edict-subst-modified-affix takes as conversion data, an alternating list of
;;;  functions and lists of additional arguments for those functions.  Each function
;;;  is applied to the substring being replaced and its additional arguments.
;;;  Likely functions to use include edict-modify-verb, edict-ignore, and edict-subst.

;;; Strip "���ޤ�"
(define-edict-rule �֤��ޤ��פ�������
  "\\(\\cc\\|\\ch\\)\\([�����������ˤӤߤ�]\\)\\(��\\(��\\|����\\)\\)$"
  "�ޤ���$"
  edict-subst-modified-affix
  edict-identity ()
  edict-modify-verb (1 2)
  edict-ignore ())

(define-edict-rule �֤ޤ��פ�������
  "\\(\\cc\\|[�����������ˤӤߤꤨ�������Ƥͤ٤��]\\)\\(��\\(��\\|����\\)\\)$"
  "�ޤ���$"
  edict-subst-affix edict-identity "��")

(define-edict-rule ����ޤ��פ����̥롼��
  "\\(���\\(��\\|����\\)\\)$"
  ()
  edict-subst-affix "���")

(define-edict-rule �֤��ޤ��פ����̥롼��
  "\\(^\\|��\\|���\\)\\(����\\(��\\|����\\)\\)$"
  "�ޤ���$"
  edict-subst-modified-affix
  edict-identity ()
  edict-subst ("����"))

(define-edict-rule �֤��ޤ��פ����̥롼��
  "\\(����\\(��\\|����\\)\\)$"
  ()
  edict-subst-affix "����")

;;; The several cases of �ơ��ä�.
;;;  Note either pattern may generate multiple possibilities.
;;; Also, ��.
(define-edict-rule �֤ơ����פ���֤��פޤ��Ѵ�����
  "\\(��\\(��\\|��[��]?\\)\\)$" 
  ()
  edict-subst-affix "��")

(define-edict-rule �֤ơ����פ���֤ġפޤ��Ѵ�����
  "\\(��\\(��\\|��[��]?\\)\\)$" 
  ()
  edict-subst-affix "��")
(define-edict-rule �֤ơ����פ���֤�פޤ��Ѵ�����
  "\\(��\\(��\\|��[��]?\\)\\)$" 
  ()
  edict-subst-affix "��")
(define-edict-rule ���ʤΡ֤ơ����פ���֤�פޤ��Ѵ�����
  "\\(\\cc\\|[�����������ˤӤߤꤨ�������Ƥͤ٤��]\\)\\(\\(��\\|��[��]?\\)\\)$" 
  ()
  edict-subst-affix edict-identity "��")
(define-edict-rule �֤ơ����פ���֤��פޤ��Ѵ�����
  "\\(��\\(��\\|��[��]?\\)\\)$" 
  ()
  edict-subst-affix "��")
(define-edict-rule �֤ơ����פ���֤��פޤ��Ѵ�����
  "\\(��\\(��\\|��[��]?\\)\\)$" 
  ()
  edict-subst-affix "��")
(define-edict-rule �֤ơ����פ���֤��פޤ��Ѵ�����
  "\\(��[�Ǥ�]\\)$" 
  ()
  edict-subst-affix "��")
(define-edict-rule �֤ơ����פ���֤֡פޤ��Ѵ�����
  "\\(��\\(��\\|��[��]?\\)\\)$" 
  ()
  edict-subst-affix "��")
(define-edict-rule �֤ơ����פ���֤�פޤ��Ѵ�����
  "\\(��\\(��\\|��[��]?\\)\\)$" 
  ()
  edict-subst-affix "��")
(define-edict-rule �֤ơ����פ���̡֤פޤ��Ѵ�����
  "\\(��\\(��\\|��[��]?\\)\\)$" 
  ()
  edict-subst-affix "��")

;;; �Ԥ� is an irregular verb.
(define-edict-rule �Ԥ������̥롼��
  "��\\(��\\(��\\|��[��]?\\)\\)$"
  ()
  edict-subst-affix "��")

(define-edict-rule ����ơפ����̥롼��
  "��\\(��\\|��[��]?\\)$"
  ()
  edict-subst-affix "���")

(define-edict-rule �֤��ơפ����̥롼��
  "\\(����\\|����[��]?\\)$"
  ()
  edict-subst-affix "����")

(define-edict-rule �֤��ơפ����̥롼��
  "\\(����\\|����[��]?\\)$"
  ()
  edict-subst-affix "����")

;;; Potential form.
;;; The filters here are due to �ְ��ʤΡ֤ơ����פ���֤�פޤ��Ѵ������

(define-edict-rule ���  "\\(\\cc\\|\\ch\\)\\(���\\)$"
  "���$"
  edict-subst-affix edict-identity "��")
(define-edict-rule ���� "\\(\\cc\\|\\ch\\)\\(����\\)$"
  "����$"
  edict-subst-affix edict-identity "��")
(define-edict-rule ���� "\\(\\cc\\|\\ch\\)\\(����\\)$"
  "����$"
  edict-subst-affix edict-identity "��")
(define-edict-rule �Ƥ� "\\(\\cc\\|\\ch\\)\\(�Ƥ�\\)$"
  "\\(��\\|�Ƥ���\\)$"
  edict-subst-affix edict-identity "��")
(define-edict-rule �ͤ� "\\(\\cc\\|\\ch\\)\\(�ͤ�\\)$"
  "�ͤ�"
  edict-subst-affix edict-identity "��")
(define-edict-rule ��� "\\(\\cc\\|\\ch\\)\\(���\\)$"
  "���"
  edict-subst-affix edict-identity "��")
(define-edict-rule �� "\\(\\cc\\|\\ch\\)\\(����\\)$"
  "����"
  edict-subst-affix edict-identity "��")
(define-edict-rule ���� "\\(\\cc\\|\\ch\\)\\(����\\)$"
  "����"
  edict-subst-affix edict-identity "��")
(define-edict-rule �٤� "\\(\\cc\\|\\ch\\)\\(�٤�\\)$"
  "�٤�"
  edict-subst-affix edict-identity "��")

;;; ����ư�졣 Also serves for the passive.
(define-edict-rule ����
  "\\(\\cc\\|[�����������ˤӤߤꤨ�������Ƥͤ٤��]\\)\\(����\\)$"
  ()
  edict-subst-affix edict-identity "��")

;;; Passive
(define-edict-rule ����ư��Ρ֤����פ��Ѵ����� 
  "\\([�狼�������ʤޤФ�]\\)\\(���\\)$"
  ()
  edict-subst-modified-affix
  edict-modify-verb (0 2)
  edict-ignore ())

(define-edict-rule �����Υ롼��
  "��\\(����\\)$"
  ()
  edict-subst-affix "��")

(define-edict-rule �����Υ롼��
  "\\(�����\\)$"
  ()
  edict-subst-affix "����")

;;; Causitive
(define-edict-rule ����ư��Ρ֤�����פ��Ѵ����� 
  "\\([�狼�������ʤޤФ�]\\)\\(����\\)$"
  ()
  edict-subst-modified-affix
  edict-modify-verb (0 2)
  edict-ignore ())

(define-edict-rule ����ư��Ρ֤�����פ��Ѵ����� 
  "\\(\\cc\\|[�����������ˤӤߤꤨ�������Ƥͤ٤��]\\)\\(������\\)$"
  ()
  edict-subst-affix edict-identity "��")

(define-edict-rule ������Υ롼��
  "\\(������\\)$"
  ()
  edict-subst-affix "����")

;;; eba conditional form.
(define-edict-rule �֤��Сפ��Ѵ����� "\\([���������Ƥͤ٤��]\\)\\(��\\)$"
  ()
  edict-subst-modified-affix
  edict-modify-verb (3 2)
  edict-ignore ())

;;; tara conditional form is handled as part of the �ơ��������� rules.

;;; The informal negative form.
(define-edict-rule �֤ʤ��פ��Ѵ����� "\\([�狼�������ʤޤФ�]\\)\\(�ʤ�\\|��\\)$"
  ()
  edict-subst-modified-affix
  edict-modify-verb (0 2)
  edict-ignore ())

(define-edict-rule ���ʤΡ֤ʤ��פ��Ѵ�����
  "\\(\\cc\\|[�����������ˤӤߤꤨ�������Ƥͤ٤��]\\)\\(�ʤ�\\|��\\)$"
  ()
  edict-subst-affix edict-identity "��")

(define-edict-rule �֤��ʤ��פ����̥롼��
  "\\(���ʤ�\\|����\\)$"
  ()
  edict-subst-affix "����")

(define-edict-rule �֤ʤ��פ����̥롼��
  "^\\(�ʤ�\\)$"
  ()
  edict-subst-affix "����")

;;; Conjunctive form

(define-edict-rule ���ʤ�conjunctive
  "\\(\\cc\\|\\ch\\)[�����������ˤӤߤꤨ�������Ƥͤ٤��]\\(\\)$"
  "��$\\|���ä�$\\|����$\\|�����$\\|��������$\\|������$\\|�夲��$\\|���ޤ�$\\|����$\\|���ʤ�$\\|�����$\\|����$\\|���餺$\\|���ޤ�$\\|����$\\|�ߤ�$\\|������$\\|�ʤ���$\\|���$\\|��餦$"
  edict-subst-modified-affix
  edict-identity ()
  edict-subst ("��"))

(define-edict-rule ���ʤ�conjunctive
  "\\(\\cc\\|\\ch\\)\\([�����������ˤӤߤ�]\\)$"
  "��$\\|���ä�$\\|����$\\|�����$\\|��������$\\|������$\\|�夲��$\\|���ޤ�$\\|����$\\|���ʤ�$\\|�����$\\|����$\\|���餺$\\|���ޤ�$\\|����$\\|�ߤ�$\\|������$\\|�ʤ���$\\|���$\\|��餦$"
  edict-subst-modified-affix
  edict-identity ()
  edict-modify-verb (1 2))

(define-edict-rule �֤���פ�����conjunctive
  "\\(\\cc\\|\\ch\\|\\ck\\|\\cK\\)\\(��\\)$"
  "��$"
  edict-subst-affix edict-identity "����")

(define-edict-rule �֤���פ�����conjunctive
  "\\(\\cc\\|\\ch\\)\\(��\\)$"
  ()
  edict-subst-affix edict-identity "����")

(define-edict-rule �֤���פ�����conjunctive
  "\\(\\cc\\|\\ch\\)\\(��\\)$"
  ()
  edict-subst-affix edict-identity "����")

;;; The informal imperative form, ����ư��
(define-edict-rule �֤�פθ���ư����Ѵ����� 
  "\\(\\cc\\|\\ch\\)\\([���������Ƥͤ٤��]\\)$"
  ()
  edict-subst-modified-affix
  edict-identity ()
  edict-modify-verb (3 2))

;;; The informal imperative form, ����ư��
(define-edict-rule �֤�פΰ���ư����Ѵ�����
  "\\(\\cc\\|[�����������ˤӤߤꤨ�������Ƥͤ٤��]\\)\\(��\\)$"
  ()
  edict-subst-affix edict-identity "��")

;;; Irregulars
(define-edict-rule ���褤�פ����̥롼��
  "^\\(�褤\\)$"
  ()
  edict-subst-affix "���")
(define-edict-rule �֤����פ����̥롼��
  "^\\(����\\)$"
  "��$"
  edict-subst-affix "����")

(define-edict-rule �֤���פ����̥롼��
  "^\\(����\\)$"
  ()
  edict-subst-affix "����")

;;; The plain desiderative
(define-edict-rule �֤����פ������� 
  "\\(\\cc\\|\\ch\\)\\([�����������ˤӤߤ�]\\)\\(����\\|������\\)$"
  ()
  edict-subst-modified-affix
  edict-identity ()
  edict-modify-verb (1 2)
  edict-ignore ())

(define-edict-rule ���ʤΡ֤����פ�������
  "\\(\\cc\\|[�����������ˤӤߤꤨ�������Ƥͤ٤��]\\)\\(����\\|������\\)$"
  ()
  edict-subst-affix edict-identity "��")

(define-edict-rule �֤������פ����̥롼��
  "^\\(������\\|��������\\)$"
  ()
  edict-subst-affix "����")

(define-edict-rule ���褿���פ����̥롼��
  "^\\(�褿��\\|�褿����\\)$"
  ()
  edict-subst-affix "���")
(define-edict-rule �֤������פ����̥롼��
  "^\\(������\\|��������\\)$"
  ()
  edict-subst-affix "����")

;;; Flush auxilliary verbs after te form.
(define-edict-rule ��ư�졼��
  "\\(\\cc\\|\\ch\\)\\(��\\|��\\|���\\)\\(����\\|����\\|���ޤ�\\|����\\|����\\|�ߤ�\\)$"
  ()
  edict-subst-modified-affix
  edict-identity ()
  edict-identity ()
  edict-ignore ())

(define-edict-rule ��ư�졼����
  "\\(\\cc\\|\\ch\\)\\(��\\|���\\)\\(��\\)$"
  ()
  edict-subst-modified-affix
  edict-identity ()
  edict-identity ()
  edict-ignore ())

(define-edict-rule ��ư�졼��
  "\\(\\cc\\|\\ch\\)\\(��\\|��\\|���\\)\\(������\\|��������\\|�ʤ���\\|����\\|�Ԥ�\\|����\\|���\\)$"
  ()
  edict-subst-modified-affix
  edict-identity ()
  edict-identity ()
  edict-ignore ())

(define-edict-rule ��ư�졼��
  "\\(\\cc\\|\\ch\\)\\(��\\|��\\|���\\)\\(\\([����]��\\)?[����]����\\|���\\|��餦\\|��������\\|ĺ��\\|�����\\|��������\\)$"
  ()
  edict-subst-modified-affix
  edict-identity ()
  edict-identity ()
  edict-ignore ())

(define-edict-rule ��ư�졼��
  "\\(\\cc\\|\\ch\\)\\(��\\|��\\|���\\)\\(����\\|����\\|�ʤ�\\|���ޤ�\\)$"
  ()
  edict-subst-modified-affix
  edict-identity ()
  edict-identity ()
  edict-ignore ())

(define-edict-rule modifiers
  "\\(\\cc\\|\\ch\\)[�������������Ĥ̤֤��]\\(�餷��\\|����\\|�褦\\)$"
  ()
  edict-subst-affix edict-identity "")

(define-edict-rule humble
  "\\(��\\)\\(\\cc\\|\\ch\\)+\\([�����������ˤӤߤ�]\\)\\(������\\|�ˤʤ�\\|����\\|������\\|�����夲��\\|�⤦��������\\)$"
  ()
  edict-subst-modified-affix
  edict-ignore ()
  edict-identity ()
  edict-modify-verb (1 2)
  edict-ignore ())

;;; Volitional
(define-edict-rule ���ʤΡ֤�����
  "\\(\\cc\\|\\ch\\)\\([���������ȤΤܤ��]\\)\\(��\\)$"
  ()
  edict-subst-modified-affix
  edict-identity ()
  edict-modify-verb (4 2)
  edict-ignore ())

(define-edict-rule ���ʤΡ֤褦��
  "\\(\\cc\\|[�����������ˤӤߤꤨ�������Ƥͤ٤��]\\)\\(�褦\\)$"
  ()
  edict-subst-affix edict-identity "��")

(define-edict-rule ����褦�פ����̥롼��
  "\\(��褦\\)$"
  ()
  edict-subst-affix "���")
(define-edict-rule �֤��褦�פ����̥롼��
  "\\(���褦\\)$"
  ()
  edict-subst-affix "����")
(define-edict-rule �֤��褦�פ����̥롼��
  "\\(���褦\\)$"
  ()
  edict-subst-affix "����")

(define-edict-rule �Ƥ��ޤ�
  "[^��]\\(���㤦\\)$"
  ()
  edict-subst-affix "�Ƥ��ޤ�")

(define-edict-rule �Ǥ��ޤ�
  "��\\(���㤦\\)$"
  ()
  edict-subst-affix "�Ǥ��ޤ�")

;; Honorific prefixes
(define-edict-rule �ɸ����Ƭ��
  "^\\(��\\|��\\|��\\)"
  ()
  edict-subst-affix "")

;; Various forms of adjectives.
(define-edict-rule ���ƻ졼��
  "\\(\\cc\\|\\ch\\)\\(��\\)$"
  "\\(��\\(���\\|����\\|�ʤ�\\|��\\)\\|��\\(�ޤ�\\|�ޤ���\\|����\\|�ʤ���\\|�Ĥ�\\|�䤵��\\|�ˤ���\\|������\\)\\|��\\(��\\|\\|��\\)\\|����\\|��\\(��\\|����\\|����\\|���\\|��\\|�Ƥ���\\)\\)$"
  edict-subst-affix edict-identity "��")
(define-edict-rule ���ƻ졼����
  "\\(\\cc\\|\\ch\\)\\(����\\)$"
  ()
  edict-subst-affix edict-identity "��")
(define-edict-rule ���ƻ졼���ʤ�
  "\\(\\cc\\|\\ch\\)\\(���ʤ�\\)$"
  ()
  edict-subst-affix edict-identity "��")
(define-edict-rule ���ƻ졼���餺
  "\\(\\cc\\|\\ch\\)\\(���餺\\)$"
  ()
  edict-subst-affix edict-identity "��")
(define-edict-rule ���ƻ졼���ä�
  "\\(\\cc\\|\\ch\\)\\(���ä�\\)$"
  ()
  edict-subst-affix edict-identity "��")
(define-edict-rule ���ƻ졼�ʤ�
  "\\(\\cc\\|\\ch\\)\\(\\(����\\|�Ǥ�\\)\\(�ʤ�\\|����ޤ���\\)\\)$"
  ()
  edict-subst-affix edict-identity "")
(define-edict-rule ���ƻ졼�����
  "\\(\\cc\\|\\ch\\)\\(�����\\)$"
  ()
  edict-subst-affix edict-identity "��")

;;; Other affixes

(define-edict-rule other-suffixes
  "\\(\\cc\\|\\ch\\)\\(Ū\\|�Ƥ�\\|���\\|ʪ\\|��\\|��\\|��\\|��\\|����\\|����\\|����\\|��\\|����\\|�����\\|��\\|����\\|��\\)$"
  ()
  edict-subst-affix edict-identity "")

(define-edict-rule other-prefixes
  "^\\(��\\|��\\|��\\|Ⱦ\\|��\\)\\cc"
  ()
  edict-subst-affix "")

;;; Canonicalize number expressions
(define-edict-rule numbers
  "^\\([0-9��-�����󻰻͸�ϻ��Ȭ�彽ɴ������]+\\)\\(\\cc\\|\\ch\\)"
  ()
  edict-subst-affix "��" edict-identity )

(define-edict-rule ��ʤ�
  "^\\([0-9��-�����󻰻͸�ϻ��Ȭ�彽ɴ������]+\\)\\(\\cc\\|\\ch\\)"
  ()
  edict-subst-affix edict-ignore edict-identity )

(define-edict-rule ��
  "\\(����ʤ�\\|�ǤϤʤ�\\|���ä�\\|���\\)$"
  ()
  edict-subst-affix "��")

(define-edict-rule �Ǥ�
  "\\(���㤢��ޤ���\\|�ǤϤ���ޤ���\\|�Ǥ��礦\\)$"
  ()
  edict-subst-affix "�Ǥ�")

(define-edict-rule �Ǥ�/��
  "\\(�Ǥ�\\)$"
  ()
  edict-subst-affix "��")

(define-edict-rule cupola
  "\\(\\cc\\|\\ch\\)\\(��\\|�Ǥ�\\)$"
  ()
  edict-subst-affix edict-identity edict-ignore)

(define-edict-rule english-plural
  "\\([^i][^e]\\|i[^e]\\|[^i]e\\)\\(s\\)$"
  (english english-noun)
  edict-subst-affix edict-ignore)

(define-edict-rule english-plural-ies
  "\\(ies\\)$"
  (english english-noun)
  edict-subst-affix "y")

(provide 'edict)