This file is indexed.

/usr/share/emacs/site-lisp/w3m/w3m-form.el is in w3m-el-snapshot 1.4.569+0.20170110-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
;;; w3m-form.el --- Stuffs to handle <form> tag

;; Copyright (C) 2001-2014 TSUCHIYA Masatoshi <tsuchiya@namazu.org>

;; Authors: TSUCHIYA Masatoshi <tsuchiya@namazu.org>,
;;          Yuuichi Teranishi  <teranisi@gohome.org>,
;;          Hideyuki SHIRAI    <shirai@meadowy.org>,
;;          Shun-ichi GOTO     <gotoh@taiyo.co.jp>,
;;          Akihiro Arisawa    <ari@mbf.sphere.ne.jp>
;; Keywords: w3m, WWW, hypermedia

;; This file is a part of emacs-w3m.

;; 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 2, 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; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.


;;; Commentary:

;; This file contains the stuffs to handle <form> tag on emacs-w3m.
;; For more detail about emacs-w3m, see:
;;
;;    http://emacs-w3m.namazu.org/


;;; Code:

(eval-when-compile
  (require 'cl))

(require 'w3m-util)
(require 'w3m)

(eval-when-compile
  (defvar w3m-current-forms))

(defcustom w3m-form-use-fancy-faces t
  "*Use fancy faces to fontify <form> tags."
  :group 'w3m
  :type 'boolean)

(defcustom w3m-form-treat-textarea-size t
  "*Non-nil means to process textarea size (treat textarea rows)."
  :group 'w3m
  :type 'boolean)

(defcustom w3m-form-use-textarea-backup t
  "*Non-nil means save and restore text that you wrote last in the textarea.
Files to save text are stored in the directory specified by the
`w3m-form-textarea-directory' variable."
  :group 'w3m
  :type 'boolean)

(defcustom w3m-form-textarea-file-expire-date 3
  "*Date to expire of the file for textarea's backup."
  :group 'w3m
  :type '(choice (integer :tag "Expire date")
		 (const :tag "Remove when emacs-w3m exit" t)
		 (const :tag "No expire" nil)))

(defcustom w3m-form-textarea-file-coding-system
  (cond ((or (featurep 'un-define)
	     (fboundp 'utf-translate-cjk-mode))
	 'utf-8)
	((equal "Japanese" w3m-language)
	 'iso-2022-7bit-ss2)
	((w3m-find-coding-system 'utf-8)
	 'utf-8)
	(t
	 'iso-2022-7bit-ss2))
  "Coding system for textarea's backup file."
  :group 'w3m
  :type '(coding-system :size 0))

(defcustom w3m-form-textarea-directory
  (expand-file-name ".textarea" w3m-profile-directory)
  "*Name of the directory to save the file of textarea input."
  :group 'w3m
  :type '(directory :size 0))

(defcustom w3m-form-textarea-edit-mode 'text-mode
  "*Major mode to edit textarea."
  :group 'w3m
  :type '(choice
	  (function :tag "Major mode")
	  (repeat
	   :tag "Rules to select major modes for the current page"
	   (cons (choice (regexp :tag "Regexp matches the current page")
			 (function :tag "Predicate checks the current page")
			 (sexp :tag "Expression checks the current page"))
		 (function :tag "Major mode")))))

(defface w3m-form
  '((((class color) (background light)) (:foreground "cyan" :underline t))
    (((class color) (background dark)) (:foreground "red" :underline t))
    (t (:underline t)))
  "*Face to fontify forms."
  :group 'w3m-face)
;; backward-compatibility alias
(put 'w3m-form-face 'face-alias 'w3m-form)

(defface w3m-form-inactive
  ;; A copy of the `shadow' face, plus `underline'.
  '((((class color grayscale) (min-colors 88) (background light))
     (:foreground "grey50" :underline t))
    (((class color grayscale) (min-colors 88) (background dark))
     (:foreground "grey70" :underline t))
    (((class color) (min-colors 8) (background light))
     (:foreground "green" :underline t))
    (((class color) (min-colors 8) (background dark))
     (:foreground "yellow" :underline t)))
  "Face used to dim inactive forms."
  :group 'w3m-face)

;;; Local variables
(defvar w3m-form-input-textarea-buffer nil)
(defvar w3m-form-input-textarea-form nil)
(defvar w3m-form-input-textarea-hseq nil)
(defvar w3m-form-input-textarea-point nil)
(defvar w3m-form-input-textarea-wincfg nil)
(defvar w3m-form-input-textarea-file nil)
(defvar w3m-form-input-textarea-coding-system nil)
(defvar w3m-form-use-textarea-backup-p nil)
(make-variable-buffer-local 'w3m-form-input-textarea-buffer)
(make-variable-buffer-local 'w3m-form-input-textarea-form)
(make-variable-buffer-local 'w3m-form-input-textarea-hseq)
(make-variable-buffer-local 'w3m-form-input-textarea-point)
(make-variable-buffer-local 'w3m-form-input-textarea-wincfg)
(make-variable-buffer-local 'w3m-form-input-textarea-file)
(make-variable-buffer-local 'w3m-form-input-textarea-coding-system)
(make-variable-buffer-local 'w3m-form-use-textarea-backup-p)

(defvar w3m-form-textarea-files nil)
(make-variable-buffer-local 'w3m-form-textarea-files)

(defvar w3m-form-textarea-post-files nil)
(make-variable-buffer-local 'w3m-form-textarea-post-files)

(defvar w3m-form-input-textarea-mode nil
  "Non-nil if w3m textarea minor mode is enabled.")
(make-variable-buffer-local 'w3m-form-input-textarea-mode)

(defvar w3m-form-input-select-buffer nil)
(defvar w3m-form-input-select-form nil)
(defvar w3m-form-input-select-name nil)
(defvar w3m-form-input-select-id nil)
(defvar w3m-form-input-select-point nil)
(defvar w3m-form-input-select-candidates nil)
(defvar w3m-form-input-select-wincfg nil)
(defvar w3m-form-input-select-urlid nil)
(make-variable-buffer-local 'w3m-form-input-select-buffer)
(make-variable-buffer-local 'w3m-form-input-select-form)
(make-variable-buffer-local 'w3m-form-input-select-name)
(make-variable-buffer-local 'w3m-form-input-select-id)
(make-variable-buffer-local 'w3m-form-input-select-point)
(make-variable-buffer-local 'w3m-form-input-select-candidates)
(make-variable-buffer-local 'w3m-form-input-select-wincfg)
(make-variable-buffer-local 'w3m-form-input-select-urlid)

(defvar w3m-form-input-map-buffer nil)
(defvar w3m-form-input-map-wincfg nil)
(defvar w3m-form-input-map-point nil)
(defvar w3m-form-input-map-urlname nil)
(make-variable-buffer-local 'w3m-form-input-map-buffer)
(make-variable-buffer-local 'w3m-form-input-map-wincfg)
(make-variable-buffer-local 'w3m-form-input-map-point)
(make-variable-buffer-local 'w3m-form-input-map-urlname)

(defvar w3m-form-new-session nil
  "Specify non-nil value to create a new session after sending form.
It is useful to bind this variable with `let', but do not set it globally.")

(defvar w3m-form-download nil
  "Specify non-nil value to download contents after sending form.
It is useful to bind this variable with `let', but do not set it globally.")

;;; w3m-form structure:

(defun w3m-form-normalize-action (action url)
  "Normalize the ACTION using URL as a current URL."
  ;; "!CURRENT_URL!" is magic string of w3m.
  (if (and action (not (string= action "!CURRENT_URL!")))
      (w3m-expand-url action url)
    (when url
      (w3m-string-match-url-components url)
      (substring url 0 (or (match-beginning 6)
			   (match-beginning 8))))))

(defun w3m-form-new (method action &optional baseurl charlst enctype)
  "Return new form object."
  (vector 'w3m-form-object
	  (if (stringp method)
	      (intern method)
	    method)
	  action
	  charlst
	  (or enctype 'application/x-www-form-urlencoded)
	  nil))

(defun w3m-form-p (obj)
  "Return t if OBJ is a form object."
  (and (vectorp obj)
       (symbolp (aref 0 obj))
       (eq (aref 0 obj) 'w3m-form-object)))

(defun w3m-form-set-method (form method)
  (aset form 1 (if (stringp method)
		   (intern method)
		 method)))
(defsetf w3m-form-method w3m-form-set-method)

(defmacro w3m-form-method (form)
  `(aref ,form 1))
(defmacro w3m-form-action (form)
  `(aref ,form 2))
(defmacro w3m-form-charlst (form)
  `(aref ,form 3))
(defmacro w3m-form-enctype (form)
  `(aref ,form 4))
(defmacro w3m-form-plist (form)
  `(aref ,form 5))
(defun w3m-form-put-property (form name property value)
  (aset form 5
	(plist-put (w3m-form-plist form)
		   (setq name (if (stringp name) (intern name) name))
		   (plist-put (plist-get (w3m-form-plist form) name)
			      property value)))
  value)
(defmacro w3m-form-get-property (form name property)
  `(plist-get (plist-get (w3m-form-plist ,form)
			 (if (stringp ,name)
			     (intern ,name)
			   ,name))
	      ,property))
(defmacro w3m-form-put (form id name value)
  `(w3m-form-put-property ,form ,id :value (cons ,name ,value)))
(defmacro w3m-form-get (form id)
  `(cdr (w3m-form-get-property ,form ,id :value)))
(defun w3m-form-get-by-name (form id name)
  (let ((plist (w3m-form-plist form))
	pair value)
    (while plist
      (when (eq id (car plist))
	(setq pair (plist-get (cadr plist) :value))
	(when (and pair
		   (string= (car pair) name))
	  (setq value (cdr pair)
		plist nil)))
      (setq plist (cddr plist)))
    value))
(defun w3m-form-put-by-name (form id name value)
  (let ((plist (w3m-form-plist form))
	pair found)
    (while plist
      (setq pair (plist-get (cadr plist) :value))
      (when (and pair
		 (string= (car pair) name))
	(setcar plist id)
	(setcdr pair value)
	(setq found t
	      plist nil))
      (setq plist (cddr plist)))
    (unless found
      (w3m-form-put form id name value))))

(defun w3m-form-goto-next-field ()
  "Move to next form field and return the point.
If no field in forward, return nil without moving."
  (let* ((id (get-text-property (point) 'w3m-form-field-id))
	 (beg (if id
		  (next-single-property-change (point) 'w3m-form-field-id)
		(point)))
	 (next (next-single-property-change beg 'w3m-form-field-id)))
    (if next
	(goto-char next)
      nil)))

(defun w3m-form-get-coding-system (coding)
  (or (catch 'det
	(while coding
	  (if (w3m-charset-to-coding-system (car coding))
	      (throw 'det (w3m-charset-to-coding-system (car coding)))
	    (setq coding (cdr coding)))))
      w3m-current-coding-system
      (w3m-charset-to-coding-system
       (w3m-content-charset w3m-current-url))
      w3m-default-coding-system))

(defun w3m-form-make-form-data (form)
  (let ((plist (w3m-form-plist form))
	(coding (w3m-form-charlst form))
	buf bufs)
    (setq coding (w3m-form-get-coding-system coding))
    (while plist
      (let* ((number (car plist))
	     (pair (plist-get (cadr plist) :value))
	     (name (car pair))
	     (value (cdr pair)))
	(cond
	 ((and (consp value)
	       (eq (car value) 'file))
	  (setq bufs (cons (cons number (cons name value)) bufs)))
	 ((and (consp value)
	       (consp (cdr value))
	       (consp (cadr value)))	; select.
	  (setq bufs (cons (cons number (cons name (car value))) bufs)))
	 ((consp value)			; checkbox
	  (setq bufs (append (mapcar (lambda (x) (cons number (cons name x))) value)
			     bufs)))
	 (value
	  (setq bufs (cons (cons number (cons name value)) bufs))))
	(setq plist (cddr plist))))
    (when bufs
      (setq bufs (sort bufs #'car-less-than-car))
      (if (eq (w3m-form-enctype form) 'multipart/form-data)
	  (let ((boundary (apply 'format "--_%d_%d_%d" (current-time)))
		file type)
	    ;; (setq buf (nreverse buf))
	    (cons
	     (concat "multipart/form-data; boundary=" boundary)
	     (with-temp-buffer
	       (set-buffer-multibyte nil)
	       (while (setq buf (cdr (car bufs)))
		 (if (and (consp (cdr buf))
			  (eq (car (cdr buf)) 'file))
		     (progn
		       (setq file (expand-file-name (cdr (cdr buf))))
		       (if (string= (setq type (w3m-local-content-type file))
				    "unknown")
			   (setq type "application/octet-stream"))
		       (insert "--" boundary "\r\n"
			       "Content-Disposition: form-data; name=\""
			       (car buf)
			       "\"; filename=\"" file "\"\r\n"
			       "Content-Type: " type "\r\n"
			       "Content-Transfer-Encoding: binary\r\n\r\n")
		       (when (file-exists-p file)
			 (insert-file-contents-literally file)
			 (goto-char (point-max)))
		       (insert "\r\n"))
		   (insert "--" boundary "\r\n"
			   "Content-Disposition: form-data; name=\""
			   (car buf)
			   "\"\r\n\r\n"
			   (if (cdr buf)
			       (encode-coding-string (cdr buf) coding)
			     "")
			   "\r\n"))
		 (setq bufs (cdr bufs)))
	       (insert "--" boundary "--\r\n")
	       (buffer-string))))
	(mapconcat (lambda (elem)
		     (setq elem (cdr elem))
		     (format "%s=%s"
			     (w3m-url-encode-string (car elem) coding t)
			     (w3m-url-encode-string (if (stringp (cdr elem))
							(cdr elem)
						      "")
						    coding
						    t)))
		   bufs "&")))))

(defun w3m-form-resume (forms)
  "Resume content of all forms in the current buffer using FORMS."
  (when forms
    (if (eq (car forms) t)
	(setq forms (cdr forms)))
    (save-excursion
      (goto-char (point-min))
      (let (textareas)
	(while (w3m-form-goto-next-field)
	  (let ((fid (get-text-property (point) 'w3m-form-field-id)))
	    (when (and fid
		       (string-match "\
fid=\\([^/]+\\)/type=\\([^/]+\\)/name=\\([^/]*\\)/id=\\(.*\\)$"
				     fid))
	      (let ((form (nth (string-to-number (match-string 1 fid))
			       forms))
		    (cform (nth (string-to-number (match-string 1 fid))
				w3m-current-forms))
		    (type (match-string 2 fid))
		    (name (match-string 3 fid))
		    (id (string-to-number (match-string 4 fid))))
		(when form
		  (cond
		   ((or (string= type "submit")
			(string= type "image"))
		    ;; Remove status to support forms containing multiple
		    ;; submit buttons.
		    (w3m-form-put cform id name nil))
		   ((or (string= type "reset")
			(string= type "hidden")
			;; Do nothing.
			))
		   ((string= type "password")
		    (w3m-form-replace (w3m-form-get form id)
				      'invisible)
		    (unless (eq form cform)
		      (w3m-form-put cform id name (w3m-form-get form id))))
		   ((string= type "radio")
		    (let ((value (w3m-form-get-by-name form id name)))
		      (when value
			(w3m-form-replace
			 (if (string= value (nth 4 (w3m-action (point))))
			     "*" " ")))
		      (unless (eq form cform)
			(w3m-form-put-by-name cform id name value))))
		   ((string= type "checkbox")
		    (let ((value (w3m-form-get form id)))
		      (when value
			(w3m-form-replace
			 (if (member (nth 4 (w3m-action (point))) value)
			     "*" " ")))
		      (unless (eq form cform)
			(w3m-form-put cform id name value))))
		   ((string= type "select")
		    (let ((selects (w3m-form-get form id)))
		      (when (and (consp selects) (car selects))
			(w3m-form-replace (cdr (assoc (car selects)
						      (cdr selects)))))
		      (unless (eq form cform)
			(w3m-form-put cform id name selects))))
		   ((string= type "textarea")
		    (let ((hseq (nth 2 (w3m-action (point))))
			  (value (w3m-form-get form id)))
		      (when (> hseq 0)
			(setq textareas (cons (cons hseq value) textareas)))
		      (unless (eq form cform)
			(w3m-form-put cform id name value))))
		   ((string= type "file")
		    (let ((value (w3m-form-get form id)))
		      (when (and value
				 (consp value))
			(w3m-form-replace (cdr value)))
		      (unless (eq form cform)
			(w3m-form-put cform id name value))))
		   (t
		    (let ((value (w3m-form-get form id)))
		      (when (stringp value)
			(w3m-form-replace value))
		      (unless (eq form cform)
			(w3m-form-put cform id name value))))))))))
	(unless w3m-form-treat-textarea-size
	  (dolist (textarea textareas)
	    (when (cdr textarea)
	      (w3m-form-textarea-replace (car textarea) (cdr textarea)))))))))

;;;###autoload
(defun w3m-fontify-forms ()
  "Process half-dumped data and fontify forms in this buffer."
  ;; If `w3m-current-forms' is resumed from history, reuse it.
  (w3m-form-parse-and-fontify
   (when (eq t (car w3m-current-forms))
     (setq w3m-current-forms (cdr w3m-current-forms)))))

(eval-and-compile
  (unless (fboundp 'w3m-form-make-button)
    (defun w3m-form-make-button (start end properties &optional readonly)
      "Make button on the region from START to END with PROPERTIES."
      (w3m-add-text-properties
       start end (append (if readonly
			     '(face w3m-form-inactive w3m-form-readonly t)
			   '(face w3m-form)
			   properties))))))

;;; w3mmee
;;
(eval-and-compile
  (defalias 'w3m-char-to-int (if (fboundp 'char-to-int)
				 'char-to-int
			       'identity))
  (defalias 'w3m-string-to-char-list (if (fboundp 'string-to-list)
					 'string-to-list
				       (lambda (str)
					 (mapcar 'identity str))))
  (defalias 'w3m-int-to-char (if (fboundp 'int-to-char)
				 'int-to-char
			       'identity)))

(defmacro w3m-form-mee-attr-unquote (x)
  "Unquote form attribute of w3mmee."
  '(let (attr)
     (when (eq (car x) ?T)
       (setq x (cdr x))
       (while (and x (not (eq (w3m-char-to-int (car x)) 0)))
	 (setq attr (concat attr (char-to-string (car x))))
	 (setq x (cdr x))))
     attr))

(defun w3m-form-mee-new (x)
  "Decode form information of w3mmee."
  (setq x (w3m-string-to-char-list
	   (w3m-url-decode-string x w3m-current-coding-system)))
  (let (method enctype action charset target name)
    (setq method (case (/ (w3m-char-to-int (car x)) 16)
		   (0 "get")
		   (1 "post")
		   (2 "internal")
		   (3 "head"))
	  enctype (case (% (w3m-char-to-int (car x)) 16)
		    (0 'application/x-www-form-urlencoded)
		    (1 'multipart/form-data)))
    (setq x (cdr x))
    (setq action (w3m-form-normalize-action (w3m-form-mee-attr-unquote x)
					    w3m-current-url))
    (setq x (cdr x))
    (if (member "lang=many" w3m-compile-options)
	(setq charset (w3m-form-mee-attr-unquote x))
      (setq charset (case (car x)
		      (?e "euc-jp")
		      (?s "shift-jis")
		      (?n "iso-2022-7bit"))))
    (setq x (cdr x))
    (setq target (w3m-form-mee-attr-unquote x))	; not used.
    (setq x (cdr x))
    (setq name (w3m-form-mee-attr-unquote x)) ; not used.
    (w3m-form-new method action nil (and charset (list charset)) enctype)))

(defun w3m-form-mee-select-value (value)
  "Decode select form information of w3mmee."
  (let ((clist (w3m-string-to-char-list
		(w3m-url-decode-string value w3m-current-coding-system)))
	label val s selected candidates)
    (while clist
      (setq s (eq (car clist) (w3m-int-to-char 1))
	    label nil
	    val nil)
      (setq clist (cdr clist))
      (while (not (eq (car clist) (w3m-int-to-char 0)))
	(setq label (concat label (char-to-string (car clist))))
	(setq clist (cdr clist)))
      (if label
	  (setq label (decode-coding-string label w3m-output-coding-system)))
      (setq clist (cdr clist))
      (while (not (eq (car clist) (w3m-int-to-char 0)))
	(setq val (concat val (char-to-string (car clist))))
	(setq clist (cdr clist)))
      (if val
	  (setq val (decode-coding-string val w3m-output-coding-system)))
      (if s (setq selected val))
      (push (cons val label) candidates)
      (setq clist (cdr clist)))
    (cons selected (nreverse candidates))))

(defun w3m-fontify-textareas ()
  "Process and fontify textareas in this buffer."
  (when w3m-form-treat-textarea-size
    (save-excursion
      (goto-char (point-min))
      (let ((inhibit-read-only t)
	    form fid start end type name rows start-column end-column
	    hseq abs-hseq text id filename readonly)
	(while (w3m-form-goto-next-field)
	  (setq fid (get-text-property (point) 'w3m-form-field-id))
	  (setq filename (get-text-property (point) 'w3m-form-file-name))
	  (when
	      (and
	       fid
	       (string-match
		"fid=\\([^/]+\\)/type=\\([^/]+\\)/name=\\([^/]*\\)/id=\\(.*\\)$"
		fid))
	    (setq form (nth (string-to-number (match-string 1 fid))
			    w3m-current-forms)
		  type (match-string 2 fid)
		  name (match-string 3 fid)
		  id (string-to-number (match-string 4 fid)))
	    (when (string= type "textarea")
	      (setq rows (get-text-property (point) 'w3m-textarea-rows)
		    hseq (get-text-property (point) 'w3m-form-hseq)
		    readonly (get-text-property (point) 'w3m-form-readonly)
		    abs-hseq (w3m-anchor-sequence))
	      (setq start-column (- (current-column) 1))
	      (goto-char (next-single-property-change (point)
						      'w3m-form-hseq))
	      (setq end-column (current-column))
	      (save-excursion
		(dotimes (i (- rows 1))
		  (forward-line -1)
		  (save-excursion
		    (move-to-column start-column)
		    (delete-char 1)
		    (insert "[")
		    (setq start (point))
		    (move-to-column end-column)
		    (delete-char 1)
		    (setq end (point))
		    (insert "]"))
		  (w3m-add-face-property
		   start end (if readonly 'w3m-form-inactive 'w3m-form))
		  (add-text-properties
		   start end
		   `(w3m-form-field-id
		     ,(format "fid=%s/type=%s/name=%s/id=%d" fid type name id)
		     w3m-action (w3m-form-input-textarea ,form ,hseq)
		     w3m-submit (w3m-form-submit ,form ,id ,name
						 (w3m-form-get ,form ,id)
						 w3m-form-new-session
						 w3m-form-download)
		     w3m-form-hseq ,hseq
		     w3m-anchor-sequence ,abs-hseq
		     w3m-form-id ,id
		     w3m-form-name ,name
		     w3m-form-file-name ,filename
		     w3m-form-readonly ,readonly)))
		(when (setq text (w3m-form-get form id))
		  (w3m-form-textarea-replace hseq text))))))))))

(defun w3m-form-parse-and-fontify (&optional reuse-forms)
  "Parse forms of the half-dumped data in this buffer and fontify them.
Result form structure is saved to the local variable `w3m-current-forms'.
If optional REUSE-FORMS is non-nil, reuse it as `w3m-current-form'."
  (let ((case-fold-search t)
	(id 0)
	tag start end internal-start textareas selects forms maps mapval
	form filename)
    (setq w3m-form-textarea-files nil)
    (setq w3m-form-use-textarea-backup-p nil)
    (goto-char (point-min))
    (while (if (eq w3m-type 'w3mmee)
	       (w3m-search-tag "_f" "map" "img_alt" "input_alt"
			       "/input_alt")
	     (w3m-search-tag "form_int" "map" "img_alt" "input_alt"
			     "/input_alt"))
      (setq tag (downcase (match-string 1)))
      (goto-char (match-end 1))
      (setq start (match-end 0))
      (cond
       ((string= tag (if (eq w3m-type 'w3mmee) "_f" "form_int"))
	(if (eq w3m-type 'w3mmee)
	    (w3m-parse-attributes (_x)
	      (setq forms (nconc forms (list (w3m-form-mee-new _x)))))
	  (w3m-parse-attributes (action (method :case-ignore)
					(fid :integer)
					(accept-charset :case-ignore)
					(enctype :case-ignore)
					(charset :case-ignore))
	    (when action
	      (setq action (w3m-url-transfer-encode-string
			    (w3m-decode-anchor-string action)
			    (if charset
				(w3m-charset-to-coding-system charset)))))
	    (if (setq form (cdr (assq fid forms)))
		(progn
		  (setf (w3m-form-method form) (or method "get"))
		  (setf (w3m-form-action form)
			(w3m-form-normalize-action action w3m-current-url))
		  (setf (w3m-form-charlst form)
			(if accept-charset
			    (setq accept-charset
				  (split-string accept-charset ","))))
		  (setf (w3m-form-enctype form)
			(if enctype
			    (intern enctype)
			  'application/x-www-form-urlencoded)))
	      (setq form (w3m-form-new
			  (or method "get")
			  (w3m-form-normalize-action
			   action (or w3m-current-base-url w3m-current-url))
			  nil
			  (if accept-charset
			      (setq accept-charset
				    (split-string accept-charset ",")))
			  (if enctype
			      (intern enctype)
			    'application/x-www-form-urlencoded)))
	      (setq forms (cons (cons fid form) forms))))))
       ((string= tag "map")
	(let (candidates)
	  (w3m-parse-attributes (name)
	    (while (and (w3m-search-tag "area" "/map")
			(not (char-equal
			      (char-after (match-beginning 1))
			      ?/)))
	      (goto-char (match-end 1))
	      (w3m-parse-attributes (href alt)
		(when href
		  (setq candidates (cons (cons href (or alt href))
					 candidates)))))
	    (unless maps (setq maps (w3m-form-new "map" ".")))
	    (when candidates
	      (w3m-form-put maps
			    (incf id)
			    name
			    (nreverse candidates))))))
       ((string= tag "img_alt")
	(w3m-parse-attributes (usemap)
	  (w3m-search-tag "/img_alt")
	  (when (or usemap mapval)
	    (unless maps (setq maps (w3m-form-new "map" ".")))
	    (unless usemap (setq usemap mapval))
	    (when mapval (setq mapval nil))
	    (w3m-add-face-property start end 'w3m-form)
	    (add-text-properties
	     start (match-beginning 0)
	     `(w3m-action (w3m-form-input-map ,maps ,id ,usemap))))))
       ((string= tag "/input_alt")
	(replace-match ""))
       ((string= tag "input_alt")
	(w3m-parse-attributes ((fid :integer)
			       (type :case-ignore)
			       (width :integer)
			       (maxlength :integer)
			       (hseq :integer)
			       (selectnumber :integer)	 ; select
			       (textareanumber :integer) ; textarea
			       (size :integer)		 ; textarea
			       (rows :integer)		 ; textarea
			       (top_mergin :integer)	 ; textarea
			       (checked :bool) ; checkbox, radio
			       (readonly :bool)
			       no_effect       ; map
			       name value)
	  (incf id)
	  (when value
	    (setq value (w3m-decode-entities-string value)))
	  (save-excursion
	    (search-forward "</input_alt>")
	    (setq end (match-beginning 0)))
	  (let ((abs-hseq (or (and (null hseq) 0) (abs hseq)))
		(face (if readonly 'w3m-form-inactive 'w3m-form)))
	    (setq w3m-max-anchor-sequence
		  (max abs-hseq w3m-max-anchor-sequence))
	    (if (eq w3m-type 'w3mmee)
		(setq form (nth fid forms))
	      (setq form (cdr (assq fid forms))))
	    (unless form
	      (setq forms (cons (cons fid (setq form
						(w3m-form-new nil nil)))
				forms)))
	    (cond
	     ((and (string= type "hidden")
		   (string= name "link"))
	      (setq mapval value))
	     ((or (string= type "submit")
		  (string= type "image"))
	      (unless (string= no_effect "true")
		(w3m-form-make-button
		 start end
		 `(w3m-form-field-id
		   ,(format "fid=%d/type=%s/name=%s/id=%d" fid type name id)
		   w3m-action (w3m-form-submit ,form ,id ,name ,value
					       w3m-form-new-session
					       w3m-form-download)
		   w3m-submit (w3m-form-submit ,form ,id ,name
					       (w3m-form-get ,form ,id)
					       w3m-form-new-session
					       w3m-form-download)
		   w3m-anchor-sequence ,abs-hseq)
		 readonly)))
	     ((string= type "reset")
	      (w3m-form-make-button
	       start end
	       `(w3m-form-field-id
		 ,(format "fid=%d/type=%s/name=%s/id=%d" fid type name id)
		 w3m-action (w3m-form-reset ,form)
		 w3m-anchor-sequence ,abs-hseq)
	       readonly))
	     ((string= type "textarea")
	      (if (eq w3m-type 'w3mmee)
		  (w3m-form-put form id
				name
				(decode-coding-string
				 (w3m-url-decode-string
				  value	w3m-current-coding-system)
				 w3m-output-coding-system))
		(setq textareas (cons (list textareanumber form id
					    name readonly)
				      textareas)))
	      (when w3m-current-url
		(setq filename (expand-file-name
				(w3m-form-input-textarea-filename
				 w3m-current-url
				 (format "fid=%d/type=%s/name=%s/id=%d"
					 fid type name id))
				w3m-form-textarea-directory))
		(setq w3m-form-textarea-files
		      (cons filename w3m-form-textarea-files)))
	      (w3m-add-face-property start end face)
	      (add-text-properties
	       start end
	       `(w3m-form-field-id
		 ,(format "fid=%d/type=%s/name=%s/id=%d" fid type name id)
		 w3m-action (w3m-form-input-textarea ,form ,hseq)
		 w3m-submit (w3m-form-submit ,form ,id ,name
					     (w3m-form-get ,form ,id)
					     w3m-form-new-session
					     w3m-form-download)
		 w3m-textarea-rows ,rows
		 w3m-form-hseq ,hseq
		 w3m-anchor-sequence ,abs-hseq
		 w3m-form-id ,id
		 w3m-form-name ,name
		 w3m-form-file-name ,filename
		 w3m-form-readonly ,readonly)))
	     ((string= type "select")
	      (when (if (eq w3m-type 'w3mmee)
			(when value
			  (w3m-form-put form id name
					(w3m-form-mee-select-value value))
			  t)
		      (setq selects (cons (list selectnumber form id name)
					  selects)))
		(w3m-add-face-property start end face)
		(add-text-properties
		 start end
		 `(w3m-form-field-id
		   ,(format "fid=%d/type=%s/name=%s/id=%d" fid type name id)
		   w3m-action (w3m-form-input-select ,form ,id ,name)
		   w3m-submit (w3m-form-submit ,form ,id ,name
					       (w3m-form-get ,form ,id)
					       w3m-form-new-session
					       w3m-form-download)
		   w3m-anchor-sequence ,abs-hseq
		   w3m-form-readonly ,readonly))))
	     ((string= type "password")
	      (w3m-add-face-property start end face)
	      (add-text-properties
	       start end
	       `(w3m-form-field-id
		 ,(format "fid=%d/type=%s/name=%s/id=%d" fid type name id)
		 w3m-action (w3m-form-input-password ,form ,id ,name)
		 w3m-submit (w3m-form-submit ,form ,id ,name
					     (w3m-form-get ,form ,id)
					     w3m-form-new-session
					     w3m-form-download)
		 w3m-anchor-sequence ,abs-hseq
		 w3m-form-readonly ,readonly)))
	     ((string= type "checkbox")
	      (let ((cvalue (w3m-form-get form id)))
		(w3m-form-put form id name
			      (if checked
				  (cons value cvalue)
				cvalue)))
	      (w3m-add-face-property start end face)
	      (add-text-properties
	       start end
	       `(w3m-form-field-id
		 ,(format "fid=%d/type=%s/name=%s/id=%d" fid type name id)
		 w3m-action (w3m-form-input-checkbox ,form ,id ,name ,value)
		 w3m-submit (w3m-form-submit ,form ,id ,name
					     (w3m-form-get ,form ,id)
					     w3m-form-new-session
					     w3m-form-download)
		 w3m-anchor-sequence ,abs-hseq
		 w3m-form-readonly ,readonly)))
	     ((string= type "radio")
	      ;; Radio button input, one name has one value
	      (if checked
		  (w3m-form-put-by-name form id name value))
	      (w3m-add-face-property start end face)
	      (add-text-properties
	       start end
	       `(w3m-form-field-id
		 ,(format "fid=%d/type=%s/name=%s/id=%d" fid type name id)
		 w3m-action (w3m-form-input-radio ,form ,id ,name ,value)
		 w3m-submit (w3m-form-submit ,form ,id ,name
					     (w3m-form-get-by-name
					      ,form ,id ,name)
					     w3m-form-new-session
					     w3m-form-download)
		 w3m-anchor-sequence ,abs-hseq
		 w3m-form-readonly ,readonly)))
	     ((string= type "file")
	      (w3m-add-face-property start end face)
	      (add-text-properties
	       start end
	       `(w3m-form-field-id
		 ,(format "fid=%d/type=%s/name=%s/id=%d" fid type name id)
		 w3m-action (w3m-form-input-file ,form ,id ,name ,value)
		 w3m-submit (w3m-form-submit ,form ,id ,name
					     (w3m-form-get ,form ,id)
					     w3m-form-new-session
					     w3m-form-download)
		 w3m-anchor-sequence ,abs-hseq
		 w3m-form-readonly ,readonly)))
	     (t
	      (w3m-form-put form
			    id
			    name
			    (or value (w3m-form-get form id)))
	      (w3m-add-face-property start end face)
	      (add-text-properties
	       start end
	       `(w3m-form-field-id
		 ,(format "fid=%d/type=%s/name=%s/id=%d" fid type name id)
		 w3m-action (w3m-form-input ,form ,id ,name ,type
					    ,width ,maxlength ,value)
		 w3m-submit (w3m-form-submit ,form ,id ,name
					     (w3m-form-get ,form ,id)
					     w3m-form-new-session
					     w3m-form-download)
		 w3m-anchor-sequence ,abs-hseq
		 w3m-form-readonly ,readonly)))))))))
    ;; Process <internal> tag.
    (when (search-forward "<internal>" nil t)
      (setq internal-start (match-beginning 0))
      (while (and (null reuse-forms)
		  (re-search-forward "<\\([a-z]+\\)_int" nil t))
	(incf id)
	(cond
	 ((string= (match-string 1) "select")
	  (w3m-parse-attributes ((selectnumber :integer))
	    (let ((selectinfo (cdr (assq selectnumber selects)))
		  current candidates)
	      (when selectinfo
		;; Parse FORM SELECT fields until </SELECT> (or </FORM>)
		(while (and (w3m-search-tag "option_int" "/select_int")
			    (not (char-equal (char-after (match-beginning 1))
					     ?/)))
		  ;; <option_int> is found
		  (goto-char (match-end 1))
		  (w3m-parse-attributes ((value :decode-entity)
					 (label :decode-entity)
					 (selected :bool))
		    (push (cons value label) candidates)
		    (if selected (setq current value))
		    (skip-chars-forward ">\n")))
		(setq candidates (nreverse candidates))
		(w3m-form-put (nth 0 selectinfo)
			      (nth 1 selectinfo)
			      (nth 2 selectinfo)
			      (cons (or current	; current value
					(caar candidates))
				    candidates))))))
	 ((string= (match-string 1) "textarea")
	  (w3m-parse-attributes ((textareanumber :integer))
	    (forward-char 1)		; skip newline character.
	    (let ((textareainfo (cdr (assq textareanumber textareas)))
		  (buffer (current-buffer))
		  end text)
	      (when textareainfo
		(setq start (point))
		(skip-chars-forward "^<")
		(setq text (buffer-substring-no-properties start (point)))
		(w3m-form-put
		 (nth 0 textareainfo) (nth 1 textareainfo) (nth 2 textareainfo)
		 (with-temp-buffer
		   (insert text)
		   (w3m-decode-entities)
		   (goto-char (point-min))
		   (while (search-forward "\r\n" nil t) (replace-match "\n"))
		   (buffer-string)))))))))
      (when (search-forward "</internal>" nil t)
	(delete-region internal-start (match-end 0))))
    (setq w3m-current-forms (if (eq w3m-type 'w3mmee)
				forms
			      (mapcar 'cdr
				      (sort forms #'car-less-than-car))))
    (w3m-form-resume (or reuse-forms w3m-current-forms))))

(defun w3m-form-replace (string &optional invisible)
  (let* ((start (text-property-any (point-min) (point-max)
				   'w3m-action (w3m-action (point))))
	 (width (string-width
		 (buffer-substring
		  start
		  (next-single-property-change start 'w3m-action))))
	 (prop (text-properties-at start))
	 (p (point))
	 (inhibit-read-only t))
    (goto-char start)
    (insert (setq string
		  (if invisible
		      (make-string (length string) ?.)
		    (mapconcat 'identity
			       (split-string
				(w3m-truncate-string (or string "")
						     width) "\n")
			       "")))
	    (make-string (max (- width (string-width string)) 0) ?\ ))
    (delete-region (point)
		   (next-single-property-change (point) 'w3m-action))
    (add-text-properties start (point) prop)
    (set-buffer-modified-p nil)
    (prog1 (point)
      (goto-char p))))

(defun w3m-form-input (form id name type width maxlength value)
  (let ((fvalue (w3m-form-get form id)))
    (if (get-text-property (point) 'w3m-form-readonly)
	(w3m-message "READONLY %s: %s" (upcase type) fvalue)
      (save-excursion
	(let ((input (save-excursion
		       (read-from-minibuffer (concat (upcase type) ": ") fvalue)))
	      (coding (w3m-form-get-coding-system (w3m-form-charlst form))))
	  (when (with-temp-buffer
		  (insert input)
		  (w3m-form-coding-system-accept-region-p nil nil coding))
	    (w3m-form-put form id name input)
	    (w3m-form-replace input)))))))

(defun w3m-form-input-password (form id name)
  (if (get-text-property (point) 'w3m-form-readonly)
      (message "This input box is read-only.")
    (let* ((fvalue (w3m-form-get form id))
	   (input (save-excursion
		    (read-passwd (concat "PASSWORD"
					 (if fvalue
					     " (default is no change)")
					 ": ")
				 nil
				 fvalue))))
      (w3m-form-put form id name input)
      (w3m-form-replace input 'invisible))))

(defun w3m-form-input-checkbox (form id name value)
  (if (get-text-property (point) 'w3m-form-readonly)
      (w3m-message "This form is currently inactive")
    (let ((fvalue (w3m-form-get form id)))
      (if (member value fvalue)		; already checked
	  (progn
	    (w3m-form-put form id name (delete value fvalue))
	    (w3m-form-replace " "))
	(w3m-form-put form id name (cons value fvalue))
	(w3m-form-replace "*")))))

(defun w3m-form-field-parse (fid)
  (when (and fid
	     (string-match
	      "fid=\\([^/]+\\)/type=\\([^/]+\\)/name=\\([^/]*\\)/id=\\(.*\\)$"
	      fid))
    (list (match-string 1 fid)
	  (match-string 2 fid)
	  (match-string 3 fid)
	  (match-string 4 fid))))

(defun w3m-form-input-radio (form id name value)
  (if (get-text-property (point) 'w3m-form-readonly)
      (w3m-message "This form is currently inactive")
    (save-excursion
      (let ((fid (w3m-form-field-parse
		  (get-text-property (point) 'w3m-form-field-id)))
	    cur-fid)
	(when fid
	  ;; Uncheck all RADIO input having same NAME
	  (goto-char 1)
	  (while (w3m-form-goto-next-field)
	    (setq cur-fid (w3m-form-field-parse
			   (get-text-property (point)
					      'w3m-form-field-id)))
	    (when (and (string= (nth 0 fid) (nth 0 cur-fid))
		       (string= (nth 1 fid) (nth 1 cur-fid))
		       (string= (nth 2 fid) (nth 2 cur-fid)))
	      (w3m-form-put-by-name
	       form (string-to-number (nth 3 fid)) (nth 2 fid) nil)
	      (w3m-form-replace " ")))))) ; erase check
    ;; Then set this field as checked.
    (w3m-form-put-by-name form id name value)
    (w3m-form-replace "*")))

(defun w3m-form-input-file (form id name value)
  (if (get-text-property (point) 'w3m-form-readonly)
      (w3m-message "This form is currently inactive")
    (let ((input (save-excursion
		   (read-file-name "File name: "
				   (or (cdr (w3m-form-get form id))
				       "~/")))))
      (w3m-form-put form id name (cons 'file input))
      (w3m-form-replace input))))

;;; TEXTAREA

(defcustom w3m-form-input-textarea-buffer-lines 10
  "*Buffer lines for form textarea buffer."
  :group 'w3m
  :type '(integer :size 0))

(defcustom w3m-form-input-textarea-mode-hook nil
  "*A hook called after w3m-form-input-textarea-mode."
  :group 'w3m
  :type 'hook)

(defcustom w3m-form-input-textarea-set-hook nil
  "*A Hook called before w3m-form-input-textarea-set."
  :group 'w3m
  :type 'hook)

(defun w3m-form-text-chop (text)
  "Return a list of substrings of TEXT which are separated by newline
character."
  (when text
    (let ((start 0) parts)
      (while (string-match "\n" text start)
	(setq parts (cons (substring text start (match-beginning 0)) parts)
	      start (match-end 0)))
      (nreverse (cons (substring text start) parts)))))

(defun w3m-form-search-textarea (hseq direction)
  (let ((point (point))
	(next-single-property-change-function
	 (if (eq direction 'forward)
	     'next-single-property-change
	   'previous-single-property-change))
	found)
    (if (get-text-property point 'w3m-form-hseq)
	(setq point (funcall next-single-property-change-function point
			     'w3m-form-hseq)))
    (when point
      (while (and (not found)
		  (setq point (funcall next-single-property-change-function
				       point 'w3m-form-hseq)))
	(when (eq (get-text-property point 'w3m-form-hseq) hseq)
	  (setq found t)))
      (if point (goto-char point)))))

(defun w3m-form-textarea-replace (hseq string)
  (let ((chopped (w3m-form-text-chop string))
	(p (point)))
    (goto-char (point-min))
    (while (w3m-form-search-textarea hseq 'forward)
      (w3m-form-replace (or (car chopped) ""))
      (setq chopped (cdr chopped)))
    (goto-char p)))

(defun w3m-form-textarea-info ()
  "Return a list of (ID NAME LINE READONLY) for current text area."
  (let ((s (get-text-property (point) 'w3m-form-hseq))
	(lines 1))
    (save-excursion
      (while (w3m-form-search-textarea s 'backward)
	(incf lines))
      (list (get-text-property (point) 'w3m-form-id)
	    (get-text-property (point) 'w3m-form-name)
	    lines
	    (get-text-property (point) 'w3m-form-readonly)))))

(defvar w3m-form-input-textarea-map nil)
(unless w3m-form-input-textarea-map
  (setq w3m-form-input-textarea-map (make-sparse-keymap))
  (define-key w3m-form-input-textarea-map "\C-c\C-c"
    'w3m-form-input-textarea-set)
  (define-key w3m-form-input-textarea-map "\C-c\C-q"
    'w3m-form-input-textarea-exit)
  (define-key w3m-form-input-textarea-map "\C-c\C-k"
    'w3m-form-input-textarea-exit)
  (define-key w3m-form-input-textarea-map "\C-x\C-s"
    'w3m-form-input-textarea-save))

(defvar w3m-form-input-textarea-org-mode-map nil)
(unless w3m-form-input-textarea-org-mode-map
  (setq w3m-form-input-textarea-org-mode-map (make-sparse-keymap))
  (define-key w3m-form-input-textarea-org-mode-map "\M-#c"
    'w3m-form-input-textarea-set)
  (define-key w3m-form-input-textarea-org-mode-map "\M-#q"
    'w3m-form-input-textarea-exit)
  (define-key w3m-form-input-textarea-org-mode-map "\M-#k"
    'w3m-form-input-textarea-exit)
  (define-key w3m-form-input-textarea-org-mode-map "\M-#s"
    'w3m-form-input-textarea-save))

(defun w3m-form-input-textarea-filename (url id)
  (condition-case nil
      (concat (md5 (concat url id) nil nil w3m-current-coding-system) ".txt")
    (error
     (let ((file "")
	   ;; Interdit chars of Windows
	   (replace (regexp-opt '("\\" "/" ":" "*" "?" "\"" "<" ">" "|")))
	   (max-file-path 254))
       (while (string-match replace url)
	 (setq file (concat file (substring url 0 (match-beginning 0)) "_"))
	 (setq url (substring url (match-end 0))))
       (setq file (concat file url "-"))
       (while (string-match replace id)
	 (setq file (concat file (substring id 0 (match-beginning 0)) "_"))
	 (setq id (substring id (match-end 0))))
       (if (< (- max-file-path 4) (length file))
	   (setq file (substring file 0 (- max-file-path 4 (length id)))))
       (setq file (concat file id ".txt"))
       (convert-standard-filename file)))))

(defun w3m-form-input-textarea-save (&optional buffer file no-check)
  "Save textarea buffer."
  (interactive)
  (setq buffer (or buffer (current-buffer)))
  (setq file (or file w3m-form-input-textarea-file))
  (with-current-buffer buffer
    (if (/= (buffer-size) 0)
	(when (and
	       (or w3m-form-use-textarea-backup-p
		   (and (eq this-command 'w3m-form-input-textarea-save)
			(y-or-n-p "Really save this buffer? ")))
	       (or no-check
		   (w3m-form-coding-system-accept-region-p)))
	  (let ((buffer-file-coding-system
		 w3m-form-textarea-file-coding-system)
		(coding-system-for-write
		 w3m-form-textarea-file-coding-system))
	    (write-region (point-min) (point-max) file nil 'nomsg)))
      (when w3m-form-use-textarea-backup-p
	(when (file-exists-p file)
	  (delete-file file))
	(when (file-exists-p (make-backup-file-name file))
	  (delete-file (make-backup-file-name file)))
	(set-buffer-modified-p nil)))))

(defun w3m-form-input-textarea-set ()
  "Save and exit from w3m form textarea mode."
  (interactive)
  (run-hooks 'w3m-form-input-textarea-set-hook)
  (let ((input (buffer-string))
	(buffer (current-buffer))
	(hseq w3m-form-input-textarea-hseq)
	(form w3m-form-input-textarea-form)
	(point w3m-form-input-textarea-point)
	(w3mbuffer w3m-form-input-textarea-buffer)
	(wincfg w3m-form-input-textarea-wincfg)
	(file w3m-form-input-textarea-file)
	info)
    (when (w3m-form-coding-system-accept-region-p)
      (w3m-form-input-textarea-save buffer file t)
      (or (one-window-p) (delete-window))
      (kill-buffer buffer)
      (if (not (buffer-live-p w3mbuffer))
	  (and (eq this-command 'w3m-form-input-textarea-set)
	       (message "No current w3m buffer"))
	(pop-to-buffer w3mbuffer)
	(set-window-configuration wincfg)
	(when (and form point)
	  (goto-char point)
	  (setq info (w3m-form-textarea-info))
	  (w3m-form-put form (nth 0 info) (nth 1 info) input)
	  (w3m-form-textarea-replace hseq input))))))

(defun w3m-form-input-textarea-exit ()
  "Exit from w3m form textarea mode."
  (interactive)
  (let ((buffer (current-buffer))
	(point w3m-form-input-textarea-point)
	(w3mbuffer w3m-form-input-textarea-buffer)
	(wincfg w3m-form-input-textarea-wincfg)
	(file w3m-form-input-textarea-file))
    (w3m-form-input-textarea-save buffer file)
    (or (one-window-p) (delete-window))
    (kill-buffer buffer)
    (if (not (buffer-live-p w3mbuffer))
	(and (eq this-command 'w3m-form-input-textarea-exit)
	     (message "No current w3m buffer"))
      (pop-to-buffer w3mbuffer)
      (set-window-configuration wincfg)
      (when point (goto-char point)))))

(unless (assq 'w3m-form-input-textarea-mode minor-mode-alist)
  (push (list 'w3m-form-input-textarea-mode " w3m form textarea")
	minor-mode-alist))
(unless (assq 'w3m-form-input-textarea-mode minor-mode-map-alist)
  (push (cons 'w3m-form-input-textarea-mode w3m-form-input-textarea-map)
	minor-mode-map-alist))

(defun w3m-form-input-textarea-mode (&optional arg)
  "\\<w3m-form-input-textarea-map>
Minor mode to edit form textareas of w3m.

\\[w3m-form-input-textarea-set]\
	Set the value and exit from this textarea.
\\[w3m-form-input-textarea-exit]\
	Exit from this textarea without setting the value.
\\[w3m-form-input-textarea-save]\
	Save editing data in this textarea.
"
  (interactive "P")
  (when (setq w3m-form-input-textarea-mode
	      (if arg
		  (> (prefix-numeric-value arg) 0)
		(not w3m-form-input-textarea-mode)))
    (run-hooks 'w3m-form-input-textarea-mode-hook))
  (when (eq major-mode 'org-mode)
    (add-to-list 'minor-mode-overriding-map-alist
		 (cons 'w3m-form-input-textarea-mode
		       w3m-form-input-textarea-org-mode-map))))

(defvar w3m-form-textarea-use-org-mode-p nil
  "Use org-mode for editing textareas when non-nil")

(defun w3m-form-textarea-toggle-major-mode (&optional arg)
  "Toggle editing textareas in Org-mode.
When off, textareas are edited in text-mode, otherwise in
org-mode. With prefix argument ARG, use org-mode if ARG is
positive, otherwise text-mode."
  (interactive "P")
  (setq w3m-form-textarea-use-org-mode-p
	(if (null arg)
	    (not w3m-form-textarea-use-org-mode-p)
	  (> (prefix-numeric-value arg) 0)))
  (message "Edit textarea in Org-mode %s"
	   (if w3m-form-textarea-use-org-mode-p "enabled" "disabled")))

(autoload 'show-all "outline" nil t)

(add-hook 'w3m-form-input-textarea-mode-hook
	  (lambda ()
	    (and (eq major-mode 'org-mode))
	    (show-all)))

(defvar view-mode-map)
(defun w3m-form-input-textarea-mode-setup (caller-buffer readonly)
  (funcall (or (and readonly
		    'view-mode)
	       (and w3m-form-textarea-use-org-mode-p
		    'org-mode)
	       (and (functionp w3m-form-textarea-edit-mode)
		    w3m-form-textarea-edit-mode)
	       (when (buffer-live-p caller-buffer)
		 (with-current-buffer caller-buffer
		   (save-match-data
		     (catch 'found-mode
		       (dolist (elem w3m-form-textarea-edit-mode)
			 (when (if (stringp (car elem))
				   (string-match (car elem)
						 w3m-current-url)
				 (if (functionp (car elem))
				     (funcall (car elem))
				   (eval (car elem))))
			   (throw 'found-mode (cdr elem))))))))
	       'text-mode))
  (if readonly
      (let ((keymap (copy-keymap view-mode-map)))
	(set (make-local-variable 'minor-mode-map-alist)
	     (list (cons 'view-mode keymap)))
	(dolist (key (nconc (where-is-internal 'w3m-form-input-textarea-set
					       w3m-form-input-textarea-map)
			    (where-is-internal 'w3m-form-input-textarea-exit
					       w3m-form-input-textarea-map)))
	  (define-key keymap key 'w3m-form-input-textarea-exit))
	(dolist (command '(View-exit View-exit-and-edit View-kill-and-leave
				     View-leave View-quit View-quit-all))
	  (substitute-key-definition command
				     'w3m-form-input-textarea-exit keymap))
	(w3m-message (substitute-command-keys "READONLY TEXT; type \
`\\<w3m-form-input-textarea-map>\\[w3m-form-input-textarea-exit]' \
to quit textarea")))
    (w3m-form-input-textarea-mode 1)
    (w3m-message "%s"
		 (if readonly "\
This text is not editable because the form has disabled or readonly attribute"
		   (substitute-command-keys "Type \
`\\<w3m-form-input-textarea-map>\\[w3m-form-input-textarea-set]' to exit \
textarea, or type \
`\\<w3m-form-input-textarea-map>\\[w3m-form-input-textarea-exit]' to quit \
textarea")))))

(eval-and-compile
  (defalias 'w3m-same-window-p
    (if (featurep 'xemacs)
	(lambda (buffer-name)
	  "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
This function returns non-nil if `display-buffer' or
`pop-to-buffer' would show a buffer named BUFFER-NAME in the
selected rather than \(as usual\) some other window.  See
`same-window-buffer-names' and `same-window-regexps'."
	  (cond
	   ((not (stringp buffer-name)))
	   ;; The elements of `same-window-buffer-names' can be buffer
	   ;; names or cons cells whose cars are buffer names.
	   ((and (boundp 'same-window-buffer-names)
		 (member buffer-name same-window-buffer-names)))
	   ((and (boundp 'same-window-buffer-names)
		 (assoc buffer-name same-window-buffer-names)))
	   ((and (boundp 'same-window-regexps)
		 (save-match-data
		   (catch 'found
		     (dolist (regexp same-window-regexps)
		       ;; The elements of `same-window-regexps' can be regexps
		       ;; or cons cells whose cars are regexps.
		       (when (or (and (stringp regexp)
				      (string-match regexp buffer-name))
				 (and (consp regexp) (stringp (car regexp))
				      (string-match (car regexp) buffer-name)))
			 (throw 'found t)))))))))
      'same-window-p)))

(eval-when-compile
  (unless (fboundp 'split-window-sensibly)
    (defalias 'split-window-sensibly 'ignore)))

(defun w3m-form-input-textarea (form hseq)
  (let* ((info  (w3m-form-textarea-info))
	 (value (w3m-form-get form (car info)))
	 (cur-win (selected-window))
	 (wincfg (current-window-configuration))
	 (w3mbuffer (current-buffer))
	 (point (point))
	 (size (- (window-height cur-win)
		  (1+ (max window-min-height
			   w3m-form-input-textarea-buffer-lines))))
	 (file (get-text-property (point) 'w3m-form-file-name))
	 (coding (w3m-form-get-coding-system (w3m-form-charlst form)))
	 (readonly (nth 3 info))
	 (backup-p (and (not readonly)
			(w3m-form-use-textarea-backup-p)))
	 buffer)
    (setq w3m-form-use-textarea-backup-p backup-p)
    (when backup-p
      (add-hook 'kill-emacs-hook 'w3m-form-textarea-file-cleanup)
      (let ((dir (file-chase-links
		  (expand-file-name w3m-form-textarea-directory))))
	(unless (and (file-exists-p dir) (file-directory-p dir))
	  (make-directory dir))))
    (setq buffer
	  (catch 'detect-buffer
	    (save-current-buffer
	      (dolist (buffer (buffer-list))
		(set-buffer buffer)
		(when (and w3m-form-input-textarea-mode
			   (eq w3m-form-input-textarea-buffer w3mbuffer)
			   (string= w3m-form-input-textarea-file file))
		  (throw 'detect-buffer (cons t buffer)))))
	    (generate-new-buffer "*w3m form textarea*")))
    (unless (consp buffer)
      (when (and backup-p (file-exists-p file) (file-readable-p file))
	(with-temp-buffer
	  (let ((buffer-file-coding-system w3m-form-textarea-file-coding-system)
		(coding-system-for-read w3m-form-textarea-file-coding-system))

	    (insert-file-contents file))
	  (let ((before (buffer-string)))
	    (when (unless (w3m-form-textarea-same-check value before)
		    (save-window-excursion
		      (set-window-buffer (selected-window) (current-buffer))
		      (goto-char (abs (w3m-compare-strings
				       before 0 (length before)
				       value 0 (length value))))
		      (condition-case nil
			  (y-or-n-p
			   "The saved text for this form exists. Use it? ")
			(quit
			 (kill-buffer buffer)
			 (error "Abort textarea editing")))))
	      (setq value before)))))
      (with-current-buffer buffer
	(insert value)
	(set-buffer-modified-p nil)
	(when readonly
	  (set-buffer-modified-p nil)
	  (setq buffer-read-only t))
	(goto-char (point-min))
	(forward-line (1- (nth 2 info)))
	(w3m-form-input-textarea-mode-setup w3mbuffer readonly)
	(setq w3m-form-input-textarea-form form
	      w3m-form-input-textarea-hseq hseq
	      w3m-form-input-textarea-buffer w3mbuffer
	      w3m-form-input-textarea-point point
	      w3m-form-input-textarea-wincfg wincfg
	      w3m-form-input-textarea-file file
	      w3m-form-input-textarea-coding-system coding
	      w3m-form-use-textarea-backup-p backup-p)))
    (if (and (consp buffer)
	     (get-buffer-window (cdr buffer)))
	;; same frame only
	(select-window (get-buffer-window (cdr buffer)))
      ;; Use the whole current window for the textarea when a user added
      ;; the buffer name "*w3m form textarea*" to `same-window-buffer-names'
      ;; (that is available only in Emacs).
      ;; cf.
      ;; <https://lists.gnu.org/archive/html/help-gnu-emacs/2008-11/msg00551.html>
      (unless (w3m-same-window-p (buffer-name (if (consp buffer)
						  (cdr buffer)
						buffer)))
	(condition-case nil
	    (if (and w3m-form-textarea-use-org-mode-p
		     (fboundp 'split-window-sensibly) ;; Emacs >= 23
		     (not (eq (symbol-function 'split-window-sensibly)
			      'ignore)))
		(split-window-sensibly cur-win)
	      (split-window cur-win (if (> size 0) size window-min-height)))
	  (error
	   (delete-other-windows)
	   (split-window cur-win (- (window-height cur-win)
				    w3m-form-input-textarea-buffer-lines))))
	(select-window (next-window)))
      (let ((pop-up-windows nil))
	(switch-to-buffer (if (consp buffer) (cdr buffer) buffer))))))

(defun w3m-form-use-textarea-backup-p ()
  (and w3m-form-use-textarea-backup
       (let ((cbuf (current-buffer))
	     (curl w3m-current-url))
	 (catch 'loop
	   (save-current-buffer
	     (dolist (buf (w3m-list-buffers))
	       (when (eq buf cbuf)
		 (throw 'loop t))
	       (set-buffer buf)
	       (when (string= w3m-current-url curl)
		 (throw 'loop nil)))
	     t)))))

(defun w3m-form-textarea-same-check (str1 str2)
  "Compare STR1 and STR2 without tailed whitespace."
  (when (string-match "[ \t\n\r]+$" str1)
    (setq str1 (substring str1 0 (match-beginning 0))))
  (when (string-match "[ \t\n\r]+$" str2)
    (setq str2 (substring str2 0 (match-beginning 0))))
  (string= str1 str2))

(defun w3m-form-textarea-file-cleanup ()
  "Remove all textarea files."
  (remove-hook 'kill-emacs-hook 'w3m-form-textarea-file-cleanup)
  (let ((dir (file-chase-links
	      (expand-file-name w3m-form-textarea-directory)))
	(checktime t)
	files file time)
    (when (and w3m-form-textarea-file-expire-date
	       (file-directory-p dir))
      (when (integerp w3m-form-textarea-file-expire-date)
	(setq checktime (decode-time (current-time)))
	(setq checktime (encode-time (nth 0 checktime) ;; seconds
				     (nth 1 checktime) ;; minutes
				     (nth 2 checktime) ;; hour
				     (- (nth 3 checktime) ;; day
					w3m-form-textarea-file-expire-date)
				     (nth 4 checktime) ;; month
				     (nth 5 checktime) ;; year
				     (nth 6 checktime) ;; dow
				     (nth 7 checktime) ;; dst
				     (nth 8 checktime)))) ;; zone
      (setq files (directory-files dir 'full "[^.]" 'nosort))
      (while (setq file (car files))
	(setq files (cdr files))
	(when (file-writable-p file)
	  (if (eq checktime t)
	      (delete-file file)
	    (setq time (nth 5 (file-attributes file)))
	    (when (w3m-time-newer-p checktime time)
	      (delete-file file))))))))

(defun w3m-form-textarea-files-remove ()
  "Remove used files of textarea."
  (let (file)
    (while (setq file (car w3m-form-textarea-post-files))
      (setq w3m-form-textarea-post-files (cdr w3m-form-textarea-post-files))
      (when (and (member file w3m-form-textarea-files)
		 (file-exists-p file)
		 (file-writable-p file))
	(delete-file file)
	(setq file (make-backup-file-name file))
	(when (and (file-exists-p file)
		   (file-writable-p file))
	  (delete-file file))))))

(defun w3m-form-set-number (w3mbuf newname)
  "Change parent w3m buffer in form buffers"
  (save-current-buffer
    (let ((newbuff (get-buffer newname)))
      (when newbuff
	(dolist (buffer (buffer-list))
	  (set-buffer buffer)
	  (cond
	   ((and w3m-form-input-textarea-mode
		 (eq w3m-form-input-textarea-buffer w3mbuf))
	    (setq w3m-form-input-textarea-buffer newbuff))
	   ((and (eq major-mode 'w3m-form-input-select-mode)
		 (eq w3m-form-input-select-buffer w3mbuf))
	    (setq w3m-form-input-select-buffer newbuff))
	   ((and (eq major-mode 'w3m-form-input-map-mode)
		 (eq w3m-form-input-map-buffer w3mbuf))
	    (setq w3m-form-input-map-buffer newbuff))))))))

(defun w3m-form-kill-buffer (w3mbuf)
  "Kill form buffers"
  (save-current-buffer
    (dolist (buffer (buffer-list))
      (set-buffer buffer)
      (cond
       ((and w3m-form-input-textarea-mode
	     (eq w3m-form-input-textarea-buffer w3mbuf))
	(w3m-form-input-textarea-exit))
       ((and (eq major-mode 'w3m-form-input-select-mode)
	     (eq w3m-form-input-select-buffer w3mbuf))
	(w3m-form-input-select-exit))
       ((and (eq major-mode 'w3m-form-input-map-mode)
	     (eq w3m-form-input-map-buffer w3mbuf))
	(w3m-form-input-map-exit))))))

;;; SELECT

(defcustom w3m-form-input-select-buffer-lines 10
  "*Buffer lines for form select buffer."
  :group 'w3m
  :type '(integer :size 0))

(defcustom w3m-form-input-select-mode-hook nil
  "*A hook called after w3m-form-input-select-mode."
  :group 'w3m
  :type 'hook)

(defcustom w3m-form-input-select-set-hook nil
  "*A Hook called before w3m-form-input-select-set."
  :group 'w3m
  :type 'hook)

(defcustom w3m-form-mouse-face 'highlight
  "*Mouse face to highlight selected value."
  :group 'w3m
  :type 'face)

(defvar w3m-form-input-select-keymap nil)
(unless w3m-form-input-select-keymap
  (setq w3m-form-input-select-keymap (make-sparse-keymap))
  (define-key w3m-form-input-select-keymap "\C-c\C-c"
    'w3m-form-input-select-set)
  (define-key w3m-form-input-select-keymap "\r"
    'w3m-form-input-select-set)
  (define-key w3m-form-input-select-keymap "\C-m"
    'w3m-form-input-select-set)
  (define-key w3m-form-input-select-keymap "\C-c\C-q"
    'w3m-form-input-select-exit)
  (define-key w3m-form-input-select-keymap "\C-c\C-k"
    'w3m-form-input-select-exit)
  (define-key w3m-form-input-select-keymap "q"
    'w3m-form-input-select-exit)
  (define-key w3m-form-input-select-keymap "\C-g"
    'w3m-form-input-select-exit)
  (define-key w3m-form-input-select-keymap "h" 'backward-char)
  (define-key w3m-form-input-select-keymap "j" 'next-line)
  (define-key w3m-form-input-select-keymap "k" 'previous-line)
  (define-key w3m-form-input-select-keymap "l" 'forward-char)
  (if (featurep 'xemacs)
      (define-key w3m-form-input-select-keymap [(button2)]
	'w3m-form-input-select-set-mouse)
    (define-key w3m-form-input-select-keymap [mouse-2]
      'w3m-form-input-select-set-mouse)))

(defun w3m-form-input-select-set-mouse (event)
  "Save and exit from w3m form select mode with mouse."
  (interactive "e")
  (mouse-set-point event)
  (w3m-form-input-select-set))

(defun w3m-form-input-select-set ()
  "Save and exit from w3m form select mode."
  (interactive)
  (run-hooks 'w3m-form-input-select-set-hook)
  (let* ((cur (get-text-property (point)
				 'w3m-form-select-value))
	 (buffer (current-buffer))
	 (name w3m-form-input-select-name)
	 (id w3m-form-input-select-id)
	 (form w3m-form-input-select-form)
	 (point w3m-form-input-select-point)
	 (w3mbuffer w3m-form-input-select-buffer)
	 (wincfg w3m-form-input-select-wincfg)
	 input)
    (setcar w3m-form-input-select-candidates cur)
    (setq input w3m-form-input-select-candidates)
    (or (one-window-p) (delete-window))
    (kill-buffer buffer)
    (when (buffer-live-p w3mbuffer)
      (pop-to-buffer w3mbuffer)
      (set-window-configuration wincfg)
      (when (and form point)
	(goto-char point)
	(w3m-form-put form id name input)
	(w3m-form-replace (cdr (assoc cur (cdr input))))))))

(defun w3m-form-input-select-exit ()
  "Exit from w3m form select mode."
  (interactive)
  (let* ((buffer (current-buffer))
	 (point w3m-form-input-select-point)
	 (w3mbuffer w3m-form-input-select-buffer)
	 (wincfg w3m-form-input-select-wincfg))
    (or (one-window-p) (delete-window))
    (kill-buffer buffer)
    (when (buffer-live-p w3mbuffer)
      (pop-to-buffer w3mbuffer)
      (set-window-configuration wincfg)
      (when point (goto-char point)))))

(defun w3m-form-input-select-mode ()
  "\\<w3m-form-input-select-keymap>
   Major mode for w3m form select.

\\[w3m-form-input-select-set]\
	Save and exit from w3m form select mode.
\\[w3m-form-input-select-exit]\
	Exit from w3m form select mode.
\\[w3m-form-input-select-set-mouse]\
	Save and exit from w3m form select mode with mouse.
"
  (setq mode-name "w3m form select"
	major-mode 'w3m-form-input-select-mode)
  (setq buffer-read-only t)
  (use-local-map w3m-form-input-select-keymap)
  (w3m-run-mode-hooks 'w3m-form-input-select-mode-hook))

(defun w3m-form-input-select (form id name)
  (if (get-text-property (point) 'w3m-form-readonly)
      (w3m-message "This form is currently inactive")
    (let* ((value (w3m-form-get form id))
	   (cur-win (selected-window))
	   (wincfg (current-window-configuration))
	   (urlid (format "%s:%s:%d" w3m-current-url name id))
	   (w3mbuffer (current-buffer))
	   (point (point))
	   (size (min
		  (- (window-height cur-win)
		     window-min-height 1)
		  (- (window-height cur-win)
		     (max window-min-height
			  (1+ w3m-form-input-select-buffer-lines)))))
	   buffer cur pos)
      (setq buffer
	    (catch 'detect-buffer
	      (save-current-buffer
		(dolist (buffer (buffer-list))
		  (set-buffer buffer)
		  (when (and w3m-form-input-select-buffer
			     (eq w3m-form-input-select-buffer w3mbuffer)
			     (string= w3m-form-input-select-urlid urlid))
		    (throw 'detect-buffer (cons t buffer)))))
	      (generate-new-buffer "*w3m form select*")))
      (unless (consp buffer)
	(with-current-buffer buffer
	  (setq w3m-form-input-select-form form)
	  (setq w3m-form-input-select-name name)
	  (setq w3m-form-input-select-id id)
	  (setq w3m-form-input-select-buffer w3mbuffer)
	  (setq w3m-form-input-select-point point)
	  (setq w3m-form-input-select-candidates value)
	  (setq w3m-form-input-select-wincfg wincfg)
	  (setq w3m-form-input-select-urlid urlid)
	  (when value
	    (setq cur (car value))
	    (setq value (cdr value))
	    (dolist (candidate value)
	      (setq pos (point))
	      (insert (if (zerop (length (cdr candidate)))
			  " "		; "" -> " "
			(cdr candidate)))
	      (add-text-properties pos (point)
				   (list 'w3m-form-select-value (car candidate)
					 'mouse-face w3m-form-mouse-face))
	      (insert "\n")))
	  (goto-char (point-min))
	  (while (and (not (eobp))
		      (not (equal cur
				  (get-text-property (point)
						     'w3m-form-select-value))))
	    (goto-char (next-single-property-change (point)
						    'w3m-form-select-value)))
	  (set-buffer-modified-p nil)
	  (beginning-of-line)
	  (w3m-form-input-select-mode)))
      (if (and (consp buffer)
	       (get-buffer-window (cdr buffer)))
	  ;; same frame only
	  (select-window (get-buffer-window (cdr buffer)))
	(condition-case nil
	    (split-window cur-win (if (> size 0) size window-min-height))
	  (error
	   (delete-other-windows)
	   (split-window cur-win (- (window-height cur-win)
				    w3m-form-input-select-buffer-lines))))
	(select-window (next-window))
	(let ((pop-up-windows nil))
	  (switch-to-buffer (if (consp buffer) (cdr buffer) buffer)))))))

;;; MAP

(defcustom w3m-form-input-map-buffer-lines 10
  "*Buffer lines for form select map buffer."
  :group 'w3m
  :type '(integer :size 0))

(defcustom w3m-form-input-map-mode-hook nil
  "*A hook called after w3m-form-input-map-mode."
  :group 'w3m
  :type 'hook)

(defcustom w3m-form-input-map-set-hook nil
  "*A Hook called before w3m-form-input-map-set."
  :group 'w3m
  :type 'hook)

(defvar w3m-form-input-map-keymap nil)
(unless w3m-form-input-map-keymap
  (setq w3m-form-input-map-keymap (make-sparse-keymap))
  (define-key w3m-form-input-map-keymap "\C-c\C-c"
    'w3m-form-input-map-set)
  (define-key w3m-form-input-map-keymap "\r"
    'w3m-form-input-map-set)
  (define-key w3m-form-input-map-keymap "\C-m"
    'w3m-form-input-map-set)
  (define-key w3m-form-input-map-keymap "\C-c\C-q"
    'w3m-form-input-map-exit)
  (define-key w3m-form-input-map-keymap "\C-c\C-k"
    'w3m-form-input-map-exit)
  (define-key w3m-form-input-map-keymap "q"
    'w3m-form-input-map-exit)
  (define-key w3m-form-input-map-keymap "\C-g"
    'w3m-form-input-map-exit)
  (define-key w3m-form-input-map-keymap "h" 'backward-char)
  (define-key w3m-form-input-map-keymap "j" 'next-line)
  (define-key w3m-form-input-map-keymap "k" 'previous-line)
  (define-key w3m-form-input-map-keymap "l" 'forward-char)
  (if (featurep 'xemacs)
      (define-key w3m-form-input-map-keymap [(button2)]
	'w3m-form-input-map-set-mouse)
    (define-key w3m-form-input-map-keymap [mouse-2]
      'w3m-form-input-map-set-mouse)))

(defun w3m-form-input-map-set-mouse (event)
  "Save and exit from w3m form select map mode with mouse."
  (interactive "e")
  (mouse-set-point event)
  (w3m-form-input-map-set))

(defun w3m-form-input-map-set ()
  "Save and exit from w3m form select map mode."
  (interactive)
  (run-hooks 'w3m-form-input-map-set-hook)
  (let* ((map (get-text-property (point) 'w3m-form-map-value))
	 (buffer (current-buffer))
	 (w3mbuffer w3m-form-input-map-buffer)
	 (wincfg w3m-form-input-map-wincfg)
	 (point w3m-form-input-map-point))
    (or (one-window-p) (delete-window))
    (kill-buffer buffer)
    (when (buffer-live-p w3mbuffer)
      (pop-to-buffer w3mbuffer)
      (set-window-configuration wincfg)
      (when point (goto-char point))
      (w3m-goto-url (w3m-expand-url map)))))

(defun w3m-form-input-map-exit ()
  "Exit from w3m form select map mode."
  (interactive)
  (let* ((buffer (current-buffer))
	 (w3mbuffer w3m-form-input-map-buffer)
	 (wincfg w3m-form-input-map-wincfg)
	 (point w3m-form-input-map-point))
    (or (one-window-p) (delete-window))
    (kill-buffer buffer)
    (when (buffer-live-p w3mbuffer)
      (pop-to-buffer w3mbuffer)
      (set-window-configuration wincfg)
      (when point (goto-char point)))))

(defun w3m-form-input-map-mode ()
  "\\<w3m-form-input-map-keymap>
   Major mode for w3m map select.

\\[w3m-form-input-map-set]\
	Save and exit from w3m form select map mode.
\\[w3m-form-input-map-exit]\
	Exit from w3m form select map mode.
\\[w3m-form-input-map-set-mouse]\
	Save and exit from w3m form select map mode with mouse.
"
  (setq mode-name "w3m map select"
	major-mode 'w3m-form-input-map-mode)
  (setq buffer-read-only t)
  (use-local-map w3m-form-input-map-keymap)
  (w3m-run-mode-hooks 'w3m-form-input-map-mode-hook))

(defun w3m-form-input-map (form id name)
  (let* ((value (w3m-form-get-by-name form id name))
	 (urlname (format "%s:%s" w3m-current-url name))
	 (cur-win (selected-window))
	 (wincfg (current-window-configuration))
	 (w3mbuffer (current-buffer))
	 (point (point))
	 (size (min
		(- (window-height cur-win)
		   window-min-height 1)
		(- (window-height cur-win)
		   (max window-min-height
			(1+ w3m-form-input-map-buffer-lines)))))
	 buffer pos)
    (setq buffer
	  (catch 'detect-buffer
	    (save-current-buffer
	      (dolist (buffer (buffer-list))
		(set-buffer buffer)
		(when (and w3m-form-input-map-buffer
			   (eq w3m-form-input-map-buffer w3mbuffer)
			   (string= w3m-form-input-map-urlname urlname))
		  (throw 'detect-buffer (cons t buffer)))))
	    (generate-new-buffer "*w3m map select*")))
    (unless (consp buffer)
      (with-current-buffer buffer
	(setq w3m-form-input-map-buffer w3mbuffer)
	(setq w3m-form-input-map-wincfg wincfg)
	(setq w3m-form-input-map-point point)
	(setq w3m-form-input-map-urlname urlname)
	(when value
	  (dolist (candidate value)
	    (setq pos (point))
	    (insert (if (zerop (length (cdr candidate)))
			(car candidate)
		      (cdr candidate)))
	    (add-text-properties pos (point)
				 (list 'w3m-form-map-value (car candidate)
				       'mouse-face w3m-form-mouse-face))
	    (insert "\n")))
	(goto-char (point-min))
	(set-buffer-modified-p nil)
	(beginning-of-line)
	(w3m-form-input-map-mode)))
    (if (and (consp buffer)
	     (get-buffer-window (cdr buffer)))
	;; same frame only
	(select-window (get-buffer-window (cdr buffer)))
      (condition-case nil
	  (split-window cur-win (if (> size 0) size window-min-height))
	(error
	 (delete-other-windows)
	 (split-window cur-win (- (window-height cur-win)
				  w3m-form-input-map-buffer-lines))))
      (select-window (next-window))
      (let ((pop-up-windows nil))
	(switch-to-buffer (if (consp buffer) (cdr buffer) buffer))))))

;;;
(defun w3m-form-submit-get-textarea-files (form)
  (when w3m-form-use-textarea-backup-p
    (let ((plist (w3m-form-plist form))
	  pos id file files)
      (while plist
	(setq id (car plist))
	(setq plist (cddr plist))
	(setq pos (text-property-any (point-min) (point-max) 'w3m-form-id id))
	(when (and pos
		   (setq file (get-text-property pos 'w3m-form-file-name)))
	  (setq files (cons file files))))
      files)))

(defvar w3m-form-last-position nil)

(defun w3m-form-restore-last-position ()
  "Restore positions after fontifying a page containing forms."
  (when (> (point-max) (nth 0 w3m-form-last-position))
    (set-window-start nil (goto-char (nth 0 w3m-form-last-position)))
    (set-window-hscroll nil (nth 1 w3m-form-last-position))
    (forward-line (max 0 (1- (nth 2 w3m-form-last-position))))
    (move-to-column (nth 3 w3m-form-last-position)))
  (remove-hook 'w3m-fontify-after-hook 'w3m-form-restore-last-position))

(defun w3m-form-submit (form &optional id name value new-session download)
  (if (w3m-anchor (point))
      ;; cf SA17565
      (w3m-goto-url (w3m-anchor (point)))
    (when (and id name
	       (> (length name) 0))
      (w3m-form-put form id name value))
    (let ((orig-url w3m-current-url)
	  (url (or (w3m-form-action form)
		   (if (string-match "\\?" w3m-current-url)
		       (substring w3m-current-url 0 (match-beginning 0))
		     w3m-current-url))))
      (setq w3m-form-last-position (list (window-start) (window-hscroll)
					 (count-lines (window-start) (point))
					 (current-column)))
      (setq w3m-form-textarea-post-files
	    (w3m-form-submit-get-textarea-files form))
      (cond ((and (not (string= url orig-url))
		  (string-match "^https://" orig-url)
		  (string-match "^http://" url)
		  (not (y-or-n-p (format "Send POST data to '%s'?" url))))
	     (ding))
	    ((or (eq 'post (w3m-form-method form))
		 ;; While some sites, e.g., emacswiki.org, specify the
		 ;; `get' method for the enctype `multipart/form-data',
		 ;; we use the `post' method according to the proposal
		 ;; of RFC2070.
		 (eq 'multipart/form-data (w3m-form-enctype form)))
	     (if download
		 (funcall 'w3m-download
			  url nil nil nil
			  (w3m-form-make-form-data form))
	       (add-hook 'w3m-fontify-after-hook
			 'w3m-form-restore-last-position)
	       (funcall (if new-session
			    'w3m-goto-url-new-session
			  'w3m-goto-url)
			url 'reload nil
			(w3m-form-make-form-data form)
			w3m-current-url)))
	    ((eq 'get (w3m-form-method form))
	     (funcall (if download
			  'w3m-download
			(add-hook 'w3m-fontify-after-hook
				  'w3m-form-restore-last-position)
			(if new-session
			    'w3m-goto-url-new-session
			  'w3m-goto-url))
		      (let ((query (w3m-form-make-form-data form)))
			(if (zerop (length query))
			    url
			  (concat (w3m-url-strip-query url) "?" query)))))
	    (t
	     (w3m-message "This form's method has not been supported: %s"
			  (let (print-level print-length)
			    (prin1-to-string (w3m-form-method form)))))))))

(defun w3m-form-real-reset (form sexp)
  (and (eq 'w3m-form-input (car sexp))
       (eq form (nth 1 sexp))
       (w3m-form-put form (nth 2 sexp) (nth 3 sexp) (nth 7 sexp))
       (w3m-form-replace (nth 7 sexp))))

(defun w3m-form-reset (form)
  (save-excursion
    (let (pos prop)
      (when (setq prop (w3m-action (goto-char (point-min))))
	(goto-char (or (w3m-form-real-reset form prop)
		       (next-single-property-change pos 'w3m-action))))
      (while (setq pos (next-single-property-change (point) 'w3m-action))
	(goto-char pos)
	(goto-char (or (w3m-form-real-reset form (w3m-action pos))
		       (next-single-property-change pos 'w3m-action)))))))


(provide 'w3m-form)

;;; w3m-form.el ends here