This file is indexed.

/usr/share/emacs/site-lisp/debian-el/apt-utils.el is in debian-el 36.3+nmu1.

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
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
;;; apt-utils.el --- Emacs interface to APT (Debian package management)

;;; Copyright (C) 2002-2010 Matthew P. Hodges

;; Author: Matthew P. Hodges <MPHodges@member.fsf.org>
;;	$Id: apt-utils.el,v 1.23 2016/11/05 22:18:48 psg Exp $

;; apt-utils.el 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.

;; apt-utils.el 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.

;;; Commentary:
;;
;; Package to interface Emacs with APT.  Start things off using e.g.:
;; M-x apt-utils-show-package RET emacs21 RET
;;
;; Other packages (dependencies, conflicts etc.) can be navigated
;; using apt-utils-{next,previous}-package,
;; apt-utils-choose-package-link or apt-utils-follow-link.  Return to
;; the previous package with apt-utils-view-previous-package.
;; ChangeLog and README files for the current package can easily be
;; accessed with, for example, apt-utils-view-changelog.
;;
;; For normal (i.e., not virtual) packages, the information can be
;; toggled between `package' and `showpkg' displays using
;; apt-utils-toggle-package-info; the latter is useful for the
;; "Reverse Depends".
;;
;; View the key bindings with describe-mode (bound to ? by default).

;;; Code:

(defconst apt-utils-version "2.12.0"
  "Version number of this package.")

(require 'browse-url)
(require 'jka-compr)
(require 'thingatpt)

(defalias 'apt-utils-puthash 'puthash)

;; Customizable variables

(defgroup apt-utils nil
  "Emacs interface to APT (Debian package management)."
  :group 'tools
  :link '(url-link "http://mph-emacs-pkgs.alioth.debian.org/AptUtilsEl.html"))

(defcustom apt-utils-fill-packages t
  "*Fill APT package names if t."
  :group 'apt-utils
  :type 'boolean)

(defcustom apt-utils-show-link-info t
  "*Show APT package descriptions when cycling through links if t."
  :group 'apt-utils
  :type 'boolean)

(defcustom apt-utils-show-all-versions nil
  "*Show APT descriptions for multiple package versions if t."
  :group 'apt-utils
  :type 'boolean)

(defcustom apt-utils-automatic-update 'ask
  "*Controls automatic rebuilding of APT package lists.

If t always rebuilt when `apt-utils-timestamped-file' is newer
than the timestamp stored in `apt-utils-package-list-built'.  If
equal to the symbol ask, ask the user about the update.  If nil,
never update automatically."
  :group 'apt-utils
  :type '(choice (const :tag "Always update automatically" t)
                 (const :tag "Ask user about update" ask)
                 (const :tag "Never update automatically" nil)))

(defcustom apt-utils-grep-dctrl-args '("-e")
  "*List of arguments to pass to `apt-utils-grep-dctrl-program'."
  :group 'apt-utils
  :type '(repeat string))

(defcustom apt-utils-kill-buffer-confirmation-function 'yes-or-no-p
  "Function called before killing any buffers.
The function is called with one argument, which is a prompt.
Suitable non-nil values include `yes-or-no-p', `y-or-n-p' and
`ignore'."
  :group 'apt-utils
  :type '(choice (const :tag "Kill buffers only after yes or no query" yes-or-no-p)
                 (const :tag "Kill buffers only after y or n query" y-or-n-p)
                 (const :tag "Never kill buffers" ignore)
                 (const :tag "Kill buffers without confirmation" nil)))

(defcustom apt-utils-search-split-regexp "\\s-*&&\\s-*"
  "Regular expression used to split multiple search terms.
See `apt-utils-search' and `apt-utils-search-names-only'."
  :group 'apt-utils
  :type 'regexp)

(defcustom apt-utils-web-browse-debian-changelog-url
  "http://packages.debian.org/changelogs/pool/main/%d/%s/%s_%v/changelog"
  "Template URL for Debian ChangeLog files.
See `apt-utils-web-format-url'."
  :group 'apt-utils
  :type 'string)

(defcustom apt-utils-web-browse-bug-reports-url
  "http://bugs.debian.org/%p"
  "Template URL for Debian bug reports.
See `apt-utils-web-format-url'."
  :group 'apt-utils
  :type 'string)

(defcustom apt-utils-web-browse-copyright-url
  "http://packages.debian.org/changelogs/pool/main/%d/%s/%s_%v/%p.copyright"
  "Template URL for Debian copyright files.
See `apt-utils-web-format-url'."
  :group 'apt-utils
  :type 'string)

(defcustom apt-utils-web-browse-versions-url
  "http://packages.debian.org/%p"
  "Template URL for Debian version information.
See `apt-utils-web-format-url'."
  :group 'apt-utils
  :type 'string)

(defcustom apt-utils-show-package-hooks nil
  "Hooks to be run after presenting package information."
  :group 'apt-utils
  :type 'hook)

(defcustom apt-utils-use-current-window nil
  "If non-nil always display APT utils buffers in the current window.
In this case `switch-to-buffer' is used to select the APT utils
buffer.  If nil, `display-buffer' is used, and the precise
behaviour depends on the value of `pop-up-windows'."
  :group 'apt-utils
  :type 'boolean)

(defcustom apt-utils-dpkg-program "/usr/bin/dpkg"
  "Location of the dpkg program.
This can be set to dlocate, which has the advantage of better
performance, but uses cached data that may be out of date."
  :group 'apt-utils
  :type '(choice (const :tag "dpkg" "/usr/bin/dpkg")
                 (const : tag "dlocate" "/usr/bin/dlocate")
                 (file :must-match t)))

(defcustom apt-utils-display-installed-status t
  "If non-nil display the installed status of the current package."
  :group 'apt-utils
  :type 'boolean)

;; Faces

(defface apt-utils-normal-package-face
  '((((class color) (background light))
     (:foreground "blue"))
    (((class color) (background dark))
     (:foreground "yellow")))
  "Face used for APT normal package hyperlinks."
  :group 'apt-utils)

(defface apt-utils-normal-installed-package-face
  '((((class color))
     (:inherit apt-utils-normal-package-face :bold t)))
  "Face used for APT installed package hyperlinks."
  :group 'apt-utils)

(defface apt-utils-virtual-package-face
  '((((class color) (background light))
     (:foreground "green4"))
    (((class color) (background dark))
     (:foreground "green")))
  "Face used for APT virtual package hyperlinks."
  :group 'apt-utils)

(defface apt-utils-field-keyword-face
  '((((class color) (background light))
     (:foreground "purple" :bold t))
    (((class color) (background dark))
     (:foreground "purple" :bold t)))
  "Face used for APT field keywords."
  :group 'apt-utils)

(defface apt-utils-field-contents-face
  '((((class color) (background light))
     (:foreground "orchid"))
    (((class color) (background dark))
     (:foreground "orange")))
  "Face used for APT field contents."
  :group 'apt-utils)

(defface apt-utils-description-face
  '((((class color))
     (:foreground "cadet blue")))
  "Face used for APT package description."
  :group 'apt-utils)

(defface apt-utils-version-face
  '((((class color))
     (:italic t)))
  "Face used for APT package versions."
  :group 'apt-utils)

(defface apt-utils-broken-face
  '((((class color))
     (:foreground "red")))
  "Face used for unknown APT package."
  :group 'apt-utils)

(defface apt-utils-file-face
  '((((class color))
     (:foreground "brown")))
  "Face used for files."
  :group 'apt-utils)

(defface apt-utils-installed-status-face
  '((((class color))
     (:italic t)))
  "Face used for installed status."
  :group 'apt-utils)

;; Other variables

(defvar apt-utils-apt-cache-program "/usr/bin/apt-cache"
  "Location of the apt-cache program.")

(defvar apt-utils-grep-dctrl-program "/usr/bin/grep-dctrl"
  "Location of the grep-dctrl program.")

(defvar apt-utils-grep-dctrl-file-directory "/var/lib/apt/lists"
  "Directory used by `apt-utils-search-grep-dctrl'.
See also `apt-utils-grep-dctrl-file-list'.")

(defvar apt-utils-grep-dctrl-file-list nil
  "List of files searched by `apt-utils-search-grep-dctrl'.
If no list is specified, this is computed on demand from files in
`apt-utils-grep-dctrl-file-directory'.")

(defvar apt-utils-package-list nil
  "Hash table containing APT packages types.")

(defvar apt-utils-package-list-built nil
  "If non-nil, a timestamp for the APT package list data.")

(defvar apt-utils-package-history nil
  "History of packages for each `apt-utils-mode' buffer.")
(make-variable-buffer-local 'apt-utils-package-history)

(defvar apt-utils-current-links nil
  "Package links associated with the `apt-utils-mode' buffer.")
(make-variable-buffer-local 'apt-utils-current-links)

(defvar apt-utils-buffer-positions nil
  "Cache of positions associated with package history.
These are stored in a hash table.  See also
`apt-utils-package-history'")
(make-variable-buffer-local 'apt-utils-buffer-positions)

(defvar apt-utils-dired-buffer nil
  "Keep track of dired buffer.")

(defvar apt-utils-automatic-update-asked nil
  "Non-nil if user already asked about updating package lists.")

(defvar apt-utils-timestamped-file "/var/cache/apt/pkgcache.bin"
  "File to check timestamp of (see `apt-utils-automatic-update').")

;; XEmacs support

(defconst apt-utils-xemacs-p
  (or (featurep 'xemacs)
      (string-match "XEmacs\\|Lucid" (emacs-version)))
  "True if we are using apt-utils under XEmacs.")

;; Other version-dependent configuration

(defalias 'apt-utils-line-end-position
  (cond
   ((fboundp 'line-end-position) 'line-end-position)
   ((fboundp 'point-at-eol) 'point-at-eol)))

(defalias 'apt-utils-line-beginning-position
  (cond
   ((fboundp 'line-beginning-position) 'line-beginning-position)
   ((fboundp 'point-at-bol) 'point-at-bol)))

(defconst apt-utils-completing-read-hashtable-p
  ;; I think this is a valid way to check this feature...
  (condition-case nil
      (or (all-completions "" (make-hash-table)) t)
    (error nil))
  "Non-nil if `completing-read' supports hash table as input.")

(defconst apt-utils-face-property
  (if (with-temp-buffer
        ;; We have to rename to something without a leading space,
        ;; otherwise font-lock-mode won't get activated.
        (rename-buffer "*test-font-lock*")
        (font-lock-mode 1)
        (and (boundp 'char-property-alias-alist)
             (member 'font-lock-face
                     (assoc 'face char-property-alias-alist))))
      'font-lock-face
    'face)
  "Use font-lock-face if `add-text-properties' supports it.
Otherwise, just use face.")

(cond
 ;; Emacs 21
 ((fboundp 'replace-regexp-in-string)
  (defalias 'apt-utils-replace-regexp-in-string 'replace-regexp-in-string))
 ;; Emacs 20
 ((and (require 'dired)
       (fboundp 'dired-replace-in-string))
  (defalias 'apt-utils-replace-regexp-in-string 'dired-replace-in-string))
 ;; XEmacs
 ((fboundp 'replace-in-string)
  (defun apt-utils-replace-regexp-in-string (regexp rep string)
    (replace-in-string string regexp rep)))
 ;; Bail out
 (t
  (error "No replace in string function found")))

;; Commands and functions

;;;###autoload
(defun apt-utils-show-package (&optional new-session)
  "Show information for a Debian package.
A selection of known packages is presented.  See `apt-utils-mode'
for more detailed help.  If NEW-SESSION is non-nil, generate a
new `apt-utils-mode' buffer."
  (interactive "P")
  (let ((package (apt-utils-choose-package)))
    (when (> (length package) 0)
      (apt-utils-show-package-1 package t new-session))))

(defun apt-utils-show-package-1 (package-spec &optional interactive new-session)
  "Present Debian package information in a dedicated buffer.

PACKAGE-SPEC can be either a string (the name of the package) or
a list, where the car of the list is the name of the package, and
the cdr is the package type.

If INTERACTIVE is non-nil, then we have been called
interactively (or from a keyboard macro) via
`apt-utils-show-package'.  Hence, reset the history of visited
packages.

If NEW-SESSION is non-nil, generate a new `apt-utils-mode'
buffer."
  (apt-utils-check-package-lists)
  (let (package type)
    (cond ((and package-spec (listp package-spec))
           (setq package (car package-spec))
           (setq type (cdr package-spec)))
          ((stringp package-spec)
           (setq package package-spec
                 type (apt-utils-package-type package))))
    ;; Set up the buffer
    (cond
     (new-session
      (set-buffer (generate-new-buffer "*APT package info*"))
      (apt-utils-mode)
      (apt-utils-update-mode-name))
     ((eq major-mode 'apt-utils-mode)
      ;; do nothing
      )
     (t
      (set-buffer (get-buffer-create "*APT package info*"))
      (apt-utils-mode)))
    ;; If called interactively, initialize apt-utils-package-history
    (when (or interactive new-session)
      (setq apt-utils-package-history (cons (cons package type) nil))
      (if (hash-table-p apt-utils-buffer-positions)
          (clrhash apt-utils-buffer-positions)
        (setq apt-utils-buffer-positions (make-hash-table :test 'equal))))
    (let ((inhibit-read-only t))
      (erase-buffer)
      (cond
       ((memq type '(normal normal-installed))
        (call-process apt-utils-apt-cache-program nil '(t nil) nil "show" package)
        ;; Remove old versions if not wanted
        (unless apt-utils-show-all-versions
          (goto-char (point-min))
          (re-search-forward "^$")
          (unless (eobp)
            (delete-region (point) (point-max))))
        (apt-utils-add-package-links))
       ;; Virtual package or normal package w/ showpkg
       ((memq type '(virtual normal-showpkg))
        (call-process apt-utils-apt-cache-program nil '(t nil) nil "showpkg" package)
        (apt-utils-add-showpkg-links package))
       ;; Normal search
       ((equal type 'search)
        (insert (format "Debian package search for %s\n\n" package))
        (apply 'call-process apt-utils-apt-cache-program nil '(t nil) nil
               "search" "--"
               (split-string package apt-utils-search-split-regexp))
        (apt-utils-sort-result)
        (apt-utils-add-search-links 'search))
       ;; Search for names only
       ((equal type 'search-names-only)
        (insert (format "Debian package search (names only) for %s\n\n" package))
        (apply 'call-process apt-utils-apt-cache-program nil '(t nil) nil
               "search" "--names-only" "--"
               (split-string package apt-utils-search-split-regexp))
        (apt-utils-sort-result)
        (apt-utils-add-search-links 'search-names-only))
       ;; Search for file names
       ((equal type 'search-file-names)
        (insert (format "Debian package search (file names) for %s\n\n" package))
        (apply 'call-process apt-utils-dpkg-program nil t nil
               "-S" (list package))
        (apt-utils-sort-result)
        (apt-utils-add-search-links 'search-file-names))
       ;; grep-dctrl search
       ((equal type 'search-grep-dctrl)
        (insert (format "grep-dctrl search for %s\n\n"
                        (concat (format "\"%s\" " (car package))
                                (mapconcat 'identity (cdr package) " "))))
        (apply 'call-process apt-utils-grep-dctrl-program nil t nil package)
        (apt-utils-sort-result)
        ;; Don't check installed status; may take forever
        (let ((apt-utils-display-installed-status nil))
          (apt-utils-add-package-links))))
      (if apt-utils-use-current-window
          (switch-to-buffer (current-buffer))
        (select-window (display-buffer (current-buffer))))
      ;; Point only needs setting for new sessions or when choosing
      ;; new packages with apt-utils-follow-link or
      ;; apt-utils-choose-package-link.
      (goto-char (point-min))
      (run-hooks 'apt-utils-show-package-hooks)))
  (set-buffer-modified-p nil)
  (setq buffer-read-only t))

(defun apt-utils-list-package-files ()
  "List the files associated with the current package.
The list appears in a `dired-mode' buffer.  Only works for
installed packages; uses `apt-utils-dpkg-program'."
  (interactive)
  (let ((package (caar apt-utils-package-history))
        (type (cdar apt-utils-package-history))
        files)
    (setq files (apt-utils-get-package-files package))
    ;; Some meta packages contain only directories, so
    ;; apt-utils-get-package-files returns '("/."); however, we don't
    ;; want to list /.
    (when (equal files '("/."))
      (setq files nil))
    (cond
     ((memq type '(normal normal-showpkg normal-installed))
      (if files
          (progn
            ;; Some versions of Emacs won't update dired for the same
            ;; directory name if it already exists
            (if (buffer-live-p apt-utils-dired-buffer)
                (kill-buffer apt-utils-dired-buffer))
            (setq apt-utils-dired-buffer (dired-noselect files))
            (display-buffer apt-utils-dired-buffer))
        (message "Package does not contain any files/is not installed.")))
     (t
      (message "No files associated for type: %s." type)))))

(defalias 'apt-utils-view-package-files 'apt-utils-list-package-files)

(defun apt-utils-get-package-files (package &optional filter installed)
  "Return a list of files belonging to package PACKAGE.
With optional argument FILTER, return files matching this regular
expression.

With non-nil INSTALLED, return t if package is installed,
otherwise nil."
  (let (files)
    (catch 'installed
      (with-temp-buffer
        (call-process apt-utils-dpkg-program nil t nil "-L" package)
        ;; Check for files
        (cond
         ((or (search-backward "does not contain any files" nil t)
              (search-backward "not installed" nil t)
              ;; dlocate returns nothing for uninstalled packages
              (or (zerop (buffer-size))))
          (when installed
            (throw 'installed nil)))
         (installed
          (throw 'installed t))
         (t
          (setq files (split-string (buffer-string) "\n"))
          ;; Keep regular files or top directory (for dired)
          (setq files
                (delq nil
                      (mapcar (lambda (elt)
                                (if (and (or (file-regular-p elt)
                                             (string-equal "/." elt))
                                         (string-match (or filter ".") elt))
                                    elt
                                  nil))
                              files))))))
      files)))

(defun apt-utils-current-package-installed-p ()
  "Return non-nil if the current-package is installed."
  (apt-utils-get-package-files (caar apt-utils-package-history) nil t))

;;;###autoload
(defun apt-utils-search ()
  "Search Debian packages for regular expression.
To search for multiple patterns use a string like \"foo && bar\".
The regular expression used to split the
terms (`apt-utils-search-split-regexp') is customisable."
  (interactive)
  (apt-utils-search-internal 'search
                             "Search packages for regexp: "))

(defun apt-utils-search-names-only ()
  "Search Debian package names for regular expression.
To search for multiple patterns use a string like \"foo && bar\".
The regular expression used to split the
terms (`apt-utils-search-split-regexp') is customisable."
  (interactive)
  (apt-utils-search-internal 'search-names-only
                             "Search package names for regexp: "))

(defun apt-utils-search-file-names ()
  "Search Debian file names for string."
  (interactive)
  (apt-utils-search-internal 'search-file-names
                             "Search file names for string: "))

(defun apt-utils-search-internal (type prompt)
  "Search Debian packages for regular expression or string.
The type of search is specified by TYPE, the prompt for the
search is specified by PROMPT."
  (apt-utils-check-package-lists)
  (let ((regexp (read-from-minibuffer prompt)))
    ;; Set up the buffer
    (cond
     ((eq major-mode 'apt-utils-mode)
      ;; do nothing
      )
     (t
      (set-buffer (get-buffer-create "*APT package info*"))
      (apt-utils-mode)))
    (let ((inhibit-read-only t)
          result)
      (erase-buffer)
      ;; Can't search for string starting with "-" because the "--"
      ;; option isn't understood by dpkg or dlocate
      (when (and (eq type 'search-file-names)
                 (string-match "^-" regexp))
        (setq regexp (apt-utils-replace-regexp-in-string "^-+" "" regexp)))
      (insert (format "Debian package search%s for %s\n\n"
                      (cond ((eq type 'search-names-only) " (names only)")
                            ((eq type 'search-file-names) " (file names)")
                            (t ""))
                      regexp))
      (setq result
            (cond
             ((eq type 'search)
              (setq apt-utils-package-history (cons (cons regexp 'search) nil))
              (apply 'call-process apt-utils-apt-cache-program nil '(t nil) nil
                     "search" "--"
                     (split-string regexp apt-utils-search-split-regexp)))
             ((eq type 'search-names-only)
              (setq apt-utils-package-history (cons (cons regexp 'search-names-only) nil))
              (apply 'call-process apt-utils-apt-cache-program nil '(t nil) nil
                     "search" "--names-only" "--"
                     (split-string regexp apt-utils-search-split-regexp)))

             ((eq type 'search-file-names)
              (setq apt-utils-package-history (cons (cons regexp 'search-file-names) nil))
              (apply 'call-process apt-utils-dpkg-program nil t nil
                     "-S" (list regexp)))))
      (if (hash-table-p apt-utils-buffer-positions)
          (clrhash apt-utils-buffer-positions)
        (setq apt-utils-buffer-positions (make-hash-table :test 'equal)))
      (if (eq result 0)
          (apt-utils-add-search-links type)
        (if (hash-table-p apt-utils-current-links)
            (clrhash apt-utils-current-links)))
      (goto-char (point-min))
      ;; Sort results
      (apt-utils-sort-result)
      (set-buffer-modified-p nil)
      (setq buffer-read-only t)
      (display-buffer (current-buffer)))))

(defun apt-utils-search-grep-dctrl ()
  "Search Debian packages for regular expression using grep-dctrl."
  (interactive)
  (apt-utils-check-package-lists)
  (let (args
        (fields (apt-utils-read-fields "Search package fields: "))
        (show (apt-utils-read-fields "Show package fields: "))
        (regexp (read-from-minibuffer "Search regexp: ")))
    ;; Check args
    (cond
     ((equal (length fields) 0)
      (error "No fields selected for search"))
     ((equal (length show) 0)
      (error "No fields selected for show"))
     ((equal (length regexp) 0)
      (error "No regexp selected")))
    (setq fields (concat "-F" fields))
    (setq show (concat "-s" show))
    (cond
     ((eq major-mode 'apt-utils-mode)
      ;; do nothing
      )
     (t
      (set-buffer (get-buffer-create "*APT package info*"))
      (apt-utils-mode)))
    (let ((inhibit-read-only t)
          result)
      (erase-buffer)
      ;; Construct argument list (need to keep this)
      (setq args (append (list regexp fields show) apt-utils-grep-dctrl-args
                         (or apt-utils-grep-dctrl-file-list
                             (directory-files apt-utils-grep-dctrl-file-directory
                                              t "_Packages$"))))
      (insert (format "grep-dctrl search for %s\n\n"
                      (mapconcat
                       (lambda (elt)
                         (if (string-equal regexp elt)
                             (format "\"%s\"" regexp)
                           elt))
                       args " ")))
      (setq result
            (apply 'call-process
                   apt-utils-grep-dctrl-program nil t nil args))
      (setq apt-utils-package-history (cons (cons args 'search-grep-dctrl) nil))
      (if (hash-table-p apt-utils-buffer-positions)
          (clrhash apt-utils-buffer-positions)
        (setq apt-utils-buffer-positions (make-hash-table :test 'equal)))
      (if (eq result 0)
          (apt-utils-add-package-links)
        (if (hash-table-p apt-utils-current-links)
            (clrhash apt-utils-current-links)))
      (goto-char (point-min))
      (set-buffer-modified-p nil)
      (setq buffer-read-only t)
      (display-buffer (current-buffer)))))

(defun apt-utils-read-fields (prompt)
  "Read fields for `apt-utils-search-grep-dctrl'.
Use PROMPT for `completing-read'."
  (let ((chosen "foo")
        (completion-ignore-case t)
        ;; Why can't I use '(...) for the list?
        (keywords (list "Architecture" "Bugs" "Conffiles" "Conflicts"
                        "Depends" "Description" "Enhances" "Essential"
                        "Filename" "Installed-Size" "MD5sum" "Maintainer"
                        "Origin" "Package" "Pre-Depends" "Priority"
                        "Provides" "Recommends" "Replaces" "Section"
                        "Size" "Source" "Suggests" "Tag" "Task" "Version"
                        "url"))
        fields)
    (while (> (length chosen) 0)
      (setq chosen
            (completing-read prompt
                             (mapcar (lambda (elt)
                                       (list elt elt))
                                     keywords)
                             nil
                             t))
      (setq keywords (delete chosen keywords))
      (if (stringp fields)
          (progn
            (when (> (length chosen) 0)
              (setq fields (concat fields "," chosen))))
        (setq fields chosen)))
    fields))

(defun apt-utils-toggle-package-info ()
  "Toggle between package and showpkg info for normal packages."
  (interactive)
  (unless (equal major-mode 'apt-utils-mode)
    (error "Not in APT utils buffer"))
  (let ((package (caar apt-utils-package-history))
        (type (cdar apt-utils-package-history))
        posns)
    (cond
     ((memq type '(normal normal-installed))
      (setq posns (apt-utils-update-buffer-positions 'toggle))
      (setq apt-utils-package-history
            (cons (cons package 'normal-showpkg)
                  (cdr apt-utils-package-history)))
      (apt-utils-show-package-1 (car apt-utils-package-history) nil)
      (goto-char (car posns))
      (set-window-start (selected-window) (cadr posns)))
     ((equal type 'normal-showpkg)
      (setq posns (apt-utils-update-buffer-positions 'toggle))
      (setq apt-utils-package-history
            (cons (cons package 'normal)
                  (cdr apt-utils-package-history)))
      (apt-utils-show-package-1 (car apt-utils-package-history) nil)
      (goto-char (car posns))
      (set-window-start (selected-window) (cadr posns)))
     ((equal type 'virtual)
      (message "Cannot toggle info for virtual packages."))
     ((memq type '(search search-names-only
                          search-file-names
                          search-grep-dctrl))
      (message "Cannot toggle info for searches.")))))

(defun apt-utils-normal-package-p ()
  "Return non-nil if the current package is a normal package.
That is, not a normal-showpkg, search or a virtual package."
  (eq (cdar apt-utils-package-history) 'normal))

(defun apt-utils-toggle-package-p ()
  "Return non-nil if we can toggle between package and showpkg.
See also `apt-utils-toggle-package-info'."
  (memq (cdar apt-utils-package-history) '(normal normal-showpkg normal-installed)))

(defun apt-utils-check-package-lists ()
  "Determine whether package lists need rebuilding."
  (apt-utils-update-mode-name)
  (cond
   ((null apt-utils-package-list-built)
    (apt-utils-build-package-list))
   ((and (apt-utils-packages-needs-update)
         ;; Only act for non-nil apt-utils-automatic-update
         apt-utils-automatic-update
         (cond
          ((eq apt-utils-automatic-update t))
          ((eq apt-utils-automatic-update 'ask)
           (unless apt-utils-automatic-update-asked
             (setq apt-utils-automatic-update-asked t)
             (yes-or-no-p
              "APT package lists may be out of date. Update them? ")))))
    (apt-utils-build-package-list t))))

;; Find ChangeLog files

(defun apt-utils-view-changelog ()
  "Find ChangeLog for the current package."
  (interactive)
  (cond
   ((not (equal major-mode 'apt-utils-mode))
    (message "Not in APT utils buffer."))
   ((not (memq (cdar apt-utils-package-history) '(normal normal-showpkg normal-installed)))
    (message "Not a normal package."))
   (t
    (let* ((package (caar apt-utils-package-history))
           (file (apt-utils-changelog-file package)))
      (if file
        (apt-utils-view-file file)
      (message "No ChangeLog file found for %s." package))))))

(defun apt-utils-changelog-file (&optional package)
  "Find ChangeLog file for PACKAGE or the current package."
  (unless package (setq package (caar apt-utils-package-history)))
  (let ((file
         (apt-utils-find-readable-file
          (format "/usr/share/doc/%s/" package)
          '("CHANGELOG" "ChangeLog" "Changelog" "changelog")
          '("" ".gz"))))
        file))

;; Find Debian ChangeLog files

(defun apt-utils-view-debian-changelog ()
  "Find Debian ChangeLog for the current package."
  (interactive)
  (cond
   ((not (equal major-mode 'apt-utils-mode))
    (message "Not in APT utils buffer."))
   ((not (memq (cdar apt-utils-package-history) '(normal normal-showpkg normal-installed)))
    (message "Not a normal package."))
   (t
    (let* ((package (caar apt-utils-package-history))
           (file (apt-utils-debian-changelog-file package)))
      (if file
          (apt-utils-view-file file)
        (message "No Debian ChangeLog file found for %s." package))))))

(defun apt-utils-debian-changelog-file (&optional package)
  "Find Debian ChangeLog file for PACKAGE or the current package."
  (unless package (setq package (caar apt-utils-package-history)))
  (let ((file
         (apt-utils-find-readable-file
          (format "/usr/share/doc/%s/" package)
          '("changelog.Debian")
          '(".gz"))))
        file))

;; Find NEWS files

(defun apt-utils-view-news ()
  "Find NEWS for the current package."
  (interactive)
  (cond
   ((not (equal major-mode 'apt-utils-mode))
    (message "Not in APT utils buffer."))
   ((not (memq (cdar apt-utils-package-history) '(normal normal-showpkg normal-installed)))
    (message "Not a normal package."))
   (t
    (let* ((package (caar apt-utils-package-history))
           (file (apt-utils-news-file package)))
      (if file
          (apt-utils-view-file file)
        (message "No NEWS file found for %s." package))))))

(defun apt-utils-news-file (&optional package)
  "Find NEWS file for PACKAGE or the current package."
  (unless package (setq package (caar apt-utils-package-history)))
  (let ((file
         (apt-utils-find-readable-file
          (format "/usr/share/doc/%s/" package)
          '("NEWS")
          '("" ".gz"))))
    file))

;; Find Debian NEWS files

(defun apt-utils-view-debian-news ()
  "Find Debian NEWS for the current package."
  (interactive)
  (cond
   ((not (equal major-mode 'apt-utils-mode))
    (message "Not in APT utils buffer."))
   ((not (memq (cdar apt-utils-package-history) '(normal normal-showpkg normal-installed)))
    (message "Not a normal package."))
   (t
    (let* ((package (caar apt-utils-package-history))
           (file (apt-utils-debian-news-file package)))
      (if file
          (apt-utils-view-file file)
        (message "No Debian NEWS file found for %s." package))))))

(defun apt-utils-debian-news-file (&optional package)
  "Find Debian NEWS file for PACKAGE or the current package."
  (unless package (setq package (caar apt-utils-package-history)))
  (let ((file
         (apt-utils-find-readable-file
          (format "/usr/share/doc/%s/" package)
          '("NEWS.Debian")
          '(".gz"))))
    file))

;; Find README files

(defun apt-utils-view-readme ()
  "Find README for the current package."
  (interactive)
  (cond
   ((not (equal major-mode 'apt-utils-mode))
    (message "Not in APT utils buffer."))
   ((not (memq (cdar apt-utils-package-history) '(normal normal-showpkg normal-installed)))
    (message "Not a normal package."))
   (t
    (let* ((package (caar apt-utils-package-history))
           (file (apt-utils-readme-file package)))
      (if file
          (apt-utils-view-file file)
        (message "No README file found for %s." package))))))

(defun apt-utils-readme-file (&optional package)
  "Find README file for PACKAGE or the current package."
  (unless package (setq package (caar apt-utils-package-history)))
  (let ((file
         (apt-utils-find-readable-file
          (format "/usr/share/doc/%s/" package)
          '("README" "readme")
          '("" ".gz"))))
    file))

;; Find Debian README files

(defun apt-utils-view-debian-readme ()
  "Find Debian README for the current package."
  (interactive)
  (cond
   ((not (equal major-mode 'apt-utils-mode))
    (message "Not in APT utils buffer."))
   ((not (memq (cdar apt-utils-package-history) '(normal normal-showpkg normal-installed)))
    (message "Not a normal package."))
   (t
    (let* ((package (caar apt-utils-package-history))
           (file (apt-utils-debian-readme-file package)))
      (if file
          (apt-utils-view-file file)
        (message "No Debian README file found for %s." package))))))

(defun apt-utils-debian-readme-file (&optional package)
  "Find Debian README file for PACKAGE or the current package."
  (unless package (setq package (caar apt-utils-package-history)))
  (let ((file
         (apt-utils-find-readable-file
          (format "/usr/share/doc/%s/" package)
          '("README.Debian" "README.debian")
          '("" ".gz"))))
    file))

;; Find copyright files

(defun apt-utils-view-copyright ()
  "Find copyright file for the current package."
  (interactive)
  (cond
   ((not (equal major-mode 'apt-utils-mode))
    (message "Not in APT utils buffer."))
   ((not (memq (cdar apt-utils-package-history) '(normal normal-showpkg normal-installed)))
    (message "Not a normal package."))
   (t
    (let* ((package (caar apt-utils-package-history))
           (file (apt-utils-copyright-file package)))
      (if file
          (apt-utils-view-file file)
        (message "No copyright file found for %s." package))))))

(defun apt-utils-copyright-file (&optional package)
  "Find copyright file for PACKAGE or the current package."
  (unless package (setq package (caar apt-utils-package-history)))
  (let ((file
         (apt-utils-find-readable-file
          (format "/usr/share/doc/%s/copyright" package)
          '("")
          '(""))))
    file))

(defun apt-utils-view-man-page ()
  "View man page for the current package.
If there is more than one man page associated with the package,
offer a choice."
  (interactive)
  (cond
   ((not (equal major-mode 'apt-utils-mode))
    (message "Not in APT utils buffer."))
   ((not (memq (cdar apt-utils-package-history) '(normal normal-showpkg normal-installed)))
    (message "Not a normal package."))
   (t
    (let ((package (caar apt-utils-package-history))
          (regexp
           "^.*/man/\\([a-zA-Z_/.]+\\)?man[0-9]/\\(.*\\)\\.\\([0-9a-z]+\\)\\.gz")
          choice chosen files table)
      (setq files (apt-utils-get-package-files package
                                               "/man/.*\\.gz$"))
      (cond
       ((null files)
        (message "No man pages found for %s." package))
       ((not (cdr files))
        (setq chosen (car files)))
       (t
        (setq table (mapcar
                     (lambda (file)
                       (setq choice
                             (with-temp-buffer
                               (insert file)
                               (when (re-search-backward regexp nil t)
                                 (replace-match "\\2 (\\1\\3)" nil nil))
                               (buffer-string)))
                       (cons choice file))
                     files))
        (setq chosen
              (cdr (assoc
                    (let ((completion-ignore-case t))
                      (completing-read "Choose man page: " table nil t))
                    table)))))
      (when chosen
        (if (fboundp 'woman-find-file)
            (woman-find-file chosen)
          (manual-entry chosen)))))))

(defun apt-utils-view-emacs-startup-file ()
  "View Emacs startup file for the current package.
If there is more than one file associated with the package, offer
a choice."
  (interactive)
  (cond
   ((not (equal major-mode 'apt-utils-mode))
    (message "Not in APT utils buffer."))
   ((not (memq (cdar apt-utils-package-history) '(normal normal-showpkg normal-installed)))
    (message "Not a normal package."))
   (t
    (let ((package (caar apt-utils-package-history))
          chosen files table)
      (setq files
            (or (apt-utils-get-package-files package
                                             "^/etc/emacs/site-start.d/.*")
                (and (boundp 'debian-emacs-flavor)
                     (apt-utils-get-package-files
                      package
                      (format "^/etc/%s/site-start.d/.*"
                              (symbol-name debian-emacs-flavor))))))
      (cond
       ((null files)
        (message "No Emacs startup files found for %s." package))
       ((not (cdr files))
        (setq chosen (car files)))
       (t
        (setq table (mapcar
                     (lambda (file)
                       (cons file file))
                     files))
        (setq chosen
              (cdr (assoc
                    (let ((completion-ignore-case t))
                      (completing-read "Choose Emacs startup file: " table nil t))
                    table)))))
      (when chosen
        (apt-utils-view-file chosen))))))

(defun apt-utils-view-version ()
  "View installed version information for current package."
  (interactive)
  (let ((package (caar apt-utils-package-history))
        (type (cdar apt-utils-package-history)))
    (if (memq type '(normal normal-showpkg normal-installed))
        (let ((info (apt-utils-get-installed-info package)))
          (if info
              (message (apply #'format
                              "%s: version %s (Desired = %s; Status = %s; Error = %s)"
                              package info))
            (message "Not installed; not known to dkpg")))
      (message "Can show version info only for normal packages"))))

(defun apt-utils-get-installed-info (package)
  "Return list of installation information for package PACKAGE."
  (let ((desired-list '((?u "Unknown")
                        (?i "Install")
                        (?r "Remove")
                        (?p "Purge")
                        (?h "Hold")))
        (status-list '((?n "Not installed")
                       (?i "Installed")
                       (?c "Config files")
                       (?u "Unpackage")
                       (?f "Failed config")
                       (?h "Half installed")))
        (err-list '((?  "None")
                    (?h "Hold")
                    (?r "Reinstall required")
                    (?x "Hold + reinstall required")))
        desired status err status-bad err-bad)
    (unless (eq package 'broken)
      (with-temp-buffer
        (let ((process-environment (append '("COLUMNS=200") (copy-alist process-environment))))
          (call-process apt-utils-dpkg-program nil t nil "-l" package))
        (when (re-search-backward
               (format "^\\([a-z ][a-z ][a-z ]\\)\\s-+%s\\s-+\\(\\S-+\\)"
                       (regexp-quote package)) nil t)
          (progn
            (setq desired (aref (match-string 1) 0)
                  status (aref (match-string 1) 1)
                  err (aref (match-string 1) 2)
                  status-bad (not (eq status (downcase status)))
                  err-bad (not (eq err (downcase err))))
            ;; Return list of information
            (list (match-string 2)      ; version
                  (cadr (assoc desired desired-list))
                  (concat (cadr (assoc (downcase status) status-list))
                          (and status-bad " [bad]"))
                  (concat (cadr (assoc (downcase err) err-list))
                          (and err-bad " [bad]")))))))))

(defun apt-utils-insert-installed-info (package)
  "Insert installed information for package PACKAGE at point."
  (let ((posn (point)))
    (insert (format " (%s)" (or (nth 2 (apt-utils-get-installed-info package))
                                "Not installed; not known to dpkg")))
    (add-text-properties (1+ posn)
                         (point)
                         '(face apt-utils-installed-status-face))))

;; File-related utility functions

(defun apt-utils-find-readable-file (dir prefixes suffixes)
  "Find a readable file composed of directory prefix and suffix.
Directory is DIR, prefix is one of PREFIXES and suffix is one of
SUFFIXES."
  (catch 'found
    (dolist (prefix prefixes)
      (dolist (suffix suffixes)
        (when (file-readable-p (concat dir prefix suffix))
          (throw 'found (concat dir prefix suffix)))))
    nil))                               ; Return nil, if no file found

(defun apt-utils-view-file (file)
  "View file FILE in function `view-mode'."
  (cond ((string-match "\\.gz$" file)
         (if (fboundp 'with-auto-compression-mode)
             (with-auto-compression-mode
               (view-file file))
           (auto-compression-mode 1)
           (view-file file)))
        (t
         (view-file file))))

;; Follow hyperlinks

(defun apt-utils-follow-link (new-session)
  "Follow hyperlink at point.
With non-nil NEW-SESSION, follow link in a new buffer."
  (interactive "P")
  (unless (equal major-mode 'apt-utils-mode)
    (error "Not in APT utils buffer"))
  (let ((package
         (cadr
          (member 'apt-package (text-properties-at (point))))))
    (apt-utils-follow-link-internal package new-session)))

(defun apt-utils-mouse-follow-link (event)
  "Follow hyperlink at mouse click.
Argument EVENT is a mouse event."
  (interactive "e")
  (let (package)
    (save-selected-window
      (mouse-set-point event)
      (setq package (apt-utils-package-at-point))
      (apt-utils-follow-link-internal package nil))))

(defun apt-utils-package-at-point ()
  "Return name of package at point, if any."
  (cadr
   (member 'apt-package (text-properties-at
                         (point)))))

(defun apt-utils-follow-link-internal (package new-session)
  "Follow hyperlink for PACKAGE.
With non-nil NEW-SESSION, follow link in a new buffer."
  (cond
   ((equal package 'broken)
    (message "Package name is broken somehow."))
   (package
    (unless new-session
      (apt-utils-update-buffer-positions 'forward))
    (apt-utils-show-package-1 package nil new-session)
    (unless new-session
      (setq apt-utils-package-history
            (cons (cons package (apt-utils-package-type package))
                  apt-utils-package-history))))
   (t
    (message "No known package at point."))))

;; Go to previous package in list

(defun apt-utils-view-previous-package ()
  "Go back to previous package displayed."
  (interactive)
  (unless (equal major-mode 'apt-utils-mode)
    (error "Not in APT utils buffer"))
  (if (cdr apt-utils-package-history)
      (progn
        (let ((posns (apt-utils-update-buffer-positions 'backward)))
          (apt-utils-show-package-1 (cadr apt-utils-package-history) nil)
          (goto-char (car posns))
          (set-window-start (selected-window) (cadr posns)))
        (setq apt-utils-package-history (cdr apt-utils-package-history)))
    (message "No further package history.")))

(defun apt-utils-previous-package-p ()
  "Return non-nil if there is a previous entry in the package history.
See also `apt-utils-package-history'."
  (cdr apt-utils-package-history))

;; Adapted from widget-move

(defun apt-utils-next-package (&optional arg)
  "Move point to the ARG next package.
ARG may be negative to move backward."
  (interactive "p")
  (unless (equal major-mode 'apt-utils-mode)
    (error "Not in APT utils buffer"))
  (cond
   ;; No links
   ((or (null apt-utils-current-links)
        (= (hash-table-count apt-utils-current-links) 0))
    (message "No package links."))
   ;; One link
   ((and (= (hash-table-count apt-utils-current-links) 1)
         (not (eq (cdar apt-utils-package-history) 'search-file-names)))
    (goto-char (point-min))
    (goto-char (next-single-property-change (point)
                                                 'apt-package)))
   (t
    (let ((old (apt-utils-package-at)))
      ;; Forward.
      (while (> arg 0)
        (cond ((eobp)
               (goto-char (point-min)))
              (t
               (goto-char (or (next-single-property-change
                               (point) 'apt-package)
                              (point-max)))))
        (let ((new (apt-utils-package-at)))
          (when new
            (unless (eq new old)
              (setq arg (1- arg))
              (setq old new)))))
      ;; Backward.
      (while (< arg 0)
        (cond ((bobp)
               (goto-char (point-max)))
              (t
               (goto-char (or (previous-single-property-change
                               (point) 'apt-package)
                              (point-min)))))
        (let ((new (apt-utils-package-at)))
          (when new
            (unless (eq new old)
              (setq arg (1+ arg))))))
      ;; Go to beginning of field.
      (let ((new (apt-utils-package-at)))
        (while (eq (apt-utils-package-at) new)
          (backward-char)))
      (forward-char))))
  ;; Echo some info
  (when apt-utils-show-link-info
    (apt-utils-package-at-message)))

(defun apt-utils-previous-package (&optional arg)
  "Move point to the ARG previous package.
ARG may be negative to move forward."
  (interactive "p")
  (apt-utils-next-package (- arg)))

;; Choose a package from the known links

(defun apt-utils-choose-package-link (new-session)
  "Choose a Debian package from a list of links.
With non-nil NEW-SESSION, follow link in a new buffer."
  (interactive "P")
  (apt-utils-choose-package-link-internal new-session))

(defun apt-utils-choose-package-link-internal (new-session)
  "Choose a Debian package from a list of links.
With non-nil NEW-SESSION, follow link in a new buffer."
  (cond
   ((not (equal major-mode 'apt-utils-mode))
    (error "Not in APT utils buffer"))
   ((= (hash-table-count apt-utils-current-links) 0)
    (message "No package links."))
   (t
    (let* ((PC-word-delimiters "-")
           (package
            (completing-read "Choose related Debian package: "
                             (cond
                              (apt-utils-completing-read-hashtable-p
                               apt-utils-current-links)
                              (t
                               (apt-utils-build-completion-table
                                apt-utils-current-links)))
                             nil t)))
      (when (> (length package) 0)
        (unless new-session
          (apt-utils-update-buffer-positions 'forward))
        (apt-utils-show-package-1 package nil new-session)
        (unless new-session
          (setq apt-utils-package-history
                (cons (cons package (apt-utils-package-type package))
                      apt-utils-package-history))))))))

(defun apt-utils-build-package-list (&optional force)
  "Build list of Debian packages known to APT.
With optional argument FORCE, rebuild the packages lists even if
they are defined.  When package lists are not up-to-date, this is
indicated in `mode-name'."
  (when (or force (null apt-utils-package-list-built))
    (unwind-protect
        (progn
          (setq apt-utils-package-list-built nil
                apt-utils-automatic-update-asked nil)
          (message "Building Debian package lists...")
          ;; Hash table listing package types
          (if (hash-table-p apt-utils-package-list)
              (clrhash apt-utils-package-list)
            (setq apt-utils-package-list (make-hash-table :test 'equal)))
          ;; All packages except virtual ones
          (with-temp-buffer
            ;; Virtual and normal packages
            (call-process apt-utils-apt-cache-program nil '(t nil) nil "pkgnames")
            (goto-char (point-min))
            (while (not (eobp))
              (apt-utils-puthash (buffer-substring (apt-utils-line-beginning-position)
                                                   (apt-utils-line-end-position))
                                 'virtual apt-utils-package-list)
              (forward-line 1))
            ;; Normal packages
            (erase-buffer)
            (call-process apt-utils-apt-cache-program nil '(t nil) nil "pkgnames"
                          "-o" "APT::Cache::AllNames=0")
            (goto-char (point-min))
            (while (not (eobp))
              (apt-utils-puthash (buffer-substring (apt-utils-line-beginning-position)
                                                   (apt-utils-line-end-position))
                                 'normal apt-utils-package-list)
              (forward-line 1))
            ;; Installed packages
            (erase-buffer)
            (call-process apt-utils-dpkg-program nil t nil "-l")
            (goto-char (point-min))
            (let (package)
              (while (not (eobp))
                (when (looking-at "^ii")
                  (setq package
                        (nth 1 (split-string (buffer-substring
                                              (apt-utils-line-beginning-position)
                                              (apt-utils-line-end-position))
                                             "\\s-+")))
                  (apt-utils-puthash package
                                     'normal-installed
                                     apt-utils-package-list))
                (forward-line 1))))
          (message "Building Debian package lists...done.")
          (setq apt-utils-package-list-built (current-time))
          (apt-utils-update-mode-name))
      (unless apt-utils-package-list-built
        (message "Building Debian package lists...interrupted.")
        (apt-utils-update-mode-name)
        (if (hash-table-p apt-utils-package-list)
            (clrhash apt-utils-package-list))))))

(defun apt-utils-rebuild-package-lists ()
  "Rebuild the APT package lists."
  (interactive)
  (apt-utils-build-package-list t))

(defun apt-utils-choose-package ()
  "Choose a Debian package name."
  (let ((package
         (and (eq major-mode 'apt-utils-mode)
              (cadr (member 'apt-package
                            (text-properties-at (point))))))
        (PC-word-delimiters "-"))
    (when (not (stringp package))
      (setq package (word-at-point)))
    (completing-read (if package
                         (format "Choose Debian package (%s): " package)
                       "Choose Debian package: ")
                     'apt-utils-choose-package-completion
                     nil t package)))

;; emacs 22 has `dynamic-completion-table' to help construct a
;; function like this, but emacs 21 and xemacs 21) don't
(defun apt-utils-choose-package-completion (str pred all)
  "Apt package name completion handler, for `completing-read'."
  (let ((enable-recursive-minibuffers t))
    (apt-utils-check-package-lists))
  (cond ((null all)
         (try-completion str (if apt-utils-completing-read-hashtable-p
                                 apt-utils-package-list
                               (apt-utils-build-completion-table
                                apt-utils-package-list))
                         pred))
        ((eq all t)
         (all-completions str (if apt-utils-completing-read-hashtable-p
                                  apt-utils-package-list
                                (apt-utils-build-completion-table
                                 apt-utils-package-list))
                          pred))
        ((eq all 'lambda)
         (if (fboundp 'test-completion)
             ;; `test-completion' is new in emacs22, and it takes
             ;; hashtables, so don't really need to test
             ;; apt-utils-completing-read-hashtable-p
             (test-completion str (if apt-utils-completing-read-hashtable-p
                                      apt-utils-package-list
                                    (apt-utils-build-completion-table
                                     apt-utils-package-list))
                              pred)
           (and (gethash str apt-utils-package-list)
                t)))))

(defun apt-utils-build-completion-table (hash)
  "Build completion table for packages using keys of hashtable HASH."
  (let (ret)
    (maphash (lambda (key value)
               (setq ret (cons (list key) ret)))
             hash)
    ret))

;; Add hyperlinks

(defun apt-utils-add-package-links ()
  "Add hyperlinks to related Debian packages."
  (let ((keywords '("Conflicts" "Depends" "Enhances" "Package"
                    "Pre-Depends" "Provides" "Recommends" "Replaces"
                    "Suggests"))
        match)
    (if (hash-table-p apt-utils-current-links)
        (clrhash apt-utils-current-links)
      (setq apt-utils-current-links (make-hash-table :test 'equal)))
    (goto-char (point-min))
    (while (re-search-forward "^\\([^ \n:]+\\):\\( \\|$\\)"
                              (point-max) t)
      (setq match (match-string 1))
      (add-text-properties (if (looking-at "$")
                               (point) ;; Conffiles (also see below)
                             (1- (point)))
                           (save-excursion
                             (beginning-of-line)
                             (point))
                           `(,apt-utils-face-property apt-utils-field-keyword-face))
      (cond
       ((member match keywords)
        ;; Remove newline characters in field
        (let ((end (apt-field-end-position)))
          (subst-char-in-region (point) end ?\n ?\  )
          (canonically-space-region (point) end))
        ;; Find packages
        (let ((packages (apt-utils-current-field-packages))
              (inhibit-read-only t)
              face
              length length-no-version
              package)
          (while packages
            (setq package (car packages))
            (setq length (length package))
            ;; Remove version info (in parenthesis), and whitespace
            (setq package (apt-utils-replace-regexp-in-string
                           "\\((.*)\\|\\s-+\\)" "" package))
            (setq length-no-version (length package))
            ;; Package type
            (cond
             ((equal (apt-utils-package-type package t) 'normal)
              (setq face 'apt-utils-normal-package-face))
             ((equal (apt-utils-package-type package t) 'normal-installed)
              (setq face 'apt-utils-normal-installed-package-face))
             ((equal (apt-utils-package-type package t) 'virtual)
              (setq face 'apt-utils-virtual-package-face))
             (t
              (setq face 'apt-utils-broken-face)
              (setq package 'broken)))
            ;; Store package links
            (apt-utils-current-links-add-package package)
            ;; Add text properties
            (add-text-properties (point) (+ (point) length-no-version)
                                 `(,apt-utils-face-property ,face
                                        mouse-face highlight
                                        apt-package ,package))
            ;; Version?
            (when (> length length-no-version)
              (add-text-properties (+ (point) length-no-version 1)
                                   (+ (point) length)
                                   `(,apt-utils-face-property apt-utils-version-face)))
            ;; Fill package names
            (when (and apt-utils-fill-packages
                       (> (current-column) (+ 2 (length match)))
                       (> (+ (current-column) length) fill-column))
              (when (equal (char-before) ?\ )
                (delete-char -1))          ; trailing whitespace
              (insert "\n" (make-string (+ 2 (length match)) ? )))
            (forward-char length)
            (when (and (equal match "Package")
                       apt-utils-display-installed-status)
              (apt-utils-insert-installed-info package))
            (skip-chars-forward ", |\n")
            (setq packages (cdr packages)))))
       ((string-match-p "Description\\(-..\\)?" match)
        (add-text-properties (point)
                             (save-excursion
                               (or
                                (re-search-forward "^[^ ]" (point-max) t)
                                (point-max)))
                             `(,apt-utils-face-property apt-utils-description-face)))
       ;; Conffiles doesn't have trailing space
       ((looking-at "$")
        nil)
       (t
        (add-text-properties (1- (point))
                             (save-excursion
                               (end-of-line)
                               (point))
                             `(,apt-utils-face-property apt-utils-field-contents-face)))))))

(defun apt-utils-add-showpkg-links (package)
  "Add hyperlinks to related Debian packages for PACKAGE."
  (let ((keywords '("Reverse Depends" "Reverse Provides"))
        (inhibit-read-only t)
        start end regexp face link)
    (if (hash-table-p apt-utils-current-links)
        (clrhash apt-utils-current-links)
      (setq apt-utils-current-links (make-hash-table :test 'equal)))
    (while keywords
      (setq regexp (concat "^" (car keywords) ": "))
      (goto-char (point-min))
      (when (re-search-forward regexp (point-max) t)
        (add-text-properties (match-beginning 0) (1- (match-end 0))
                             `(,apt-utils-face-property
                               apt-utils-field-keyword-face))
        ;; Limits of search
        (setq start (1+ (point)))
        (setq end (or (re-search-forward "[a-z]:" (point-max) t)
                      (point-max)))
        (save-restriction
          (narrow-to-region start end)
          (goto-char (point-min))
          (while (not (eobp))
            (when (or (looking-at "^\\s-+\\(.*\\),")
                      (looking-at "^\\(.*\\) "))
              (setq link (match-string 1))
              (cond
               ((equal (apt-utils-package-type link t) 'normal)
                (setq face 'apt-utils-normal-package-face))
               ((equal (apt-utils-package-type package t) 'normal-installed)
                (setq face 'apt-utils-normal-installed-package-face))
               ((equal (apt-utils-package-type link t) 'virtual)
                (setq face 'apt-utils-virtual-package-face))
               (t
                (setq face 'apt-utils-broken-face)
                (setq link 'broken)))
              ;; Store package links
              (apt-utils-current-links-add-package link)
              (add-text-properties (match-beginning 1) (match-end 1)
                                   `(,apt-utils-face-property ,face
                                          mouse-face highlight
                                          apt-package ,link)))
          (forward-line))))
      (setq keywords (cdr keywords))))
  (when (and apt-utils-display-installed-status
             (memq (apt-utils-package-type package t)
                   '(normal normal-installed)))
    (goto-char (point-min))
    (re-search-forward "Package: .*$")
    (apt-utils-insert-installed-info package)))

(defun apt-utils-add-search-links (type)
  "Add hyperlinks to related Debian packages.
The type of search is specified by TYPE."
  (let ((inhibit-read-only t)
        local-keymap
        face link regexp)
    (when (eq type 'search-file-names)
      (setq local-keymap (make-sparse-keymap))
      (define-key local-keymap (kbd "RET")
        (lambda ()
          (interactive)
          (view-file (or (get-text-property (point) 'apt-package-file)
                         (get-text-property (1- (point)) 'apt-package-file))))))
    (if (hash-table-p apt-utils-current-links)
        (clrhash apt-utils-current-links)
      (setq apt-utils-current-links (make-hash-table :test 'equal)))
    (goto-char (point-min))
    (forward-line 2)                    ; Move past header
    (cond
     ((eq type 'search-file-names)
      ;; Reformat diversion information
      (save-excursion
        (while (re-search-forward "diversion by \\(.*\\) \\(from\\|to\\): \\(.*\\)" nil t)
          (replace-match "\\1: \\3 (diversion \\2)" nil nil)))
      (setq regexp "\\([^:,]+\\)[,:]"))
     (t
      (setq regexp"^\\([^ ]+\\) - ")))
    (while (re-search-forward regexp (point-max) t)
      (setq link (match-string 1))
      (cond
       ((equal (apt-utils-package-type link t) 'normal)
        (setq face 'apt-utils-normal-package-face))
       ((equal (apt-utils-package-type link t) 'normal-installed)
        (setq face 'apt-utils-normal-installed-package-face))
       ((equal (apt-utils-package-type link t) 'virtual)
        (setq face 'apt-utils-virtual-package-face))
       (t
        (setq face 'apt-utils-broken-face)
        (setq link 'broken)))
      ;; Store package links
      (apt-utils-current-links-add-package link)
      (add-text-properties (match-beginning 1) (match-end 1)
                           `(,apt-utils-face-property ,face
                                  mouse-face highlight
                                  apt-package ,link))
      ;; Multiple fields separated by commas
      (when (eq type 'search-file-names)
        (if (eq (char-before) ?\:)
            (progn
              (when local-keymap
                (let ((start (1+ (point)))
                      (end (save-excursion
                             (goto-char (apt-utils-line-end-position))
                             (re-search-backward " (diversion \\(from\\|to\\))"
                                                 (apt-utils-line-beginning-position)
                                                 t)
                             (point))))
                  (add-text-properties start end
                                       `(face apt-utils-file-face
                                              keymap ,local-keymap
                                              ;; Pretend we're a package
                                              ;; so that we can move
                                              ;; here with
                                              ;; apt-utils-next-package
                                              apt-package dummy
                                              apt-package-file
                                              ,(buffer-substring-no-properties start end)
                                              ))))
              (goto-char (1+ (apt-utils-line-end-position))))
          (skip-chars-forward ", "))))))

(defun apt-utils-package-type (package &optional no-error)
  "Return what type of package PACKAGE is.
With optional argument NO-ERROR, don't flag an error for unknown
packages."
  (or (gethash package apt-utils-package-list)
      (cond
       (no-error
        nil)
       (t
        (error
         (substitute-command-keys
          "Package name is broken: rebuild package lists using \\[apt-utils-rebuild-package-lists] may help")
         package)))))

(defun apt-utils-package-at ()
  "Get package at point."
  (get-text-property (point) 'apt-package))

(defun apt-utils-package-at-message ()
  "Emit message describing package at point."
  (let ((package (apt-utils-package-at)))
    (cond
     ((eq package 'dummy)
      ;; Do nothing as this isn't really a package
      )
     ((equal package 'broken)
      (message "Package name is broken somehow."))
     (package
      (with-temp-buffer
        (call-process apt-utils-apt-cache-program nil t nil "show" package)
        (if (re-search-backward "^Description: \\(.*\\)$" (point-min) t)
            (message "%s: %s." package (match-string 1))
          (message "%s: virtual package (no description)."
                   package)))))))

(defun apt-utils-quit (&optional kill-buffer)
  "Quit this `apt-utils-mode' buffer.
With prefix argument KILL-BUFFER, kill the `apt-utils-mode'
buffer."
  (interactive "P")
  (unless (equal major-mode 'apt-utils-mode)
    (error "Not in APT utils buffer"))
  (let ((buffer (current-buffer)))
    (if (fboundp 'quit-window)
        (quit-window)
      (bury-buffer))
    (when kill-buffer
      (kill-buffer buffer)))
  (run-hooks 'apt-utils-quit-hooks))

(defun apt-utils-cleanup ()
  "Clean up lists used by `apt-utils-mode'.
Specifically, nullify `apt-utils-package-list'.  Only do this if
there are no buffers left in `apt-utils-mode'."
  (unless (memq 'apt-utils-mode
                (mapcar (lambda (b)
                          (with-current-buffer b
                            major-mode))
                        (delete (current-buffer) (buffer-list))))
    (clrhash apt-utils-package-list)
    (setq apt-utils-package-list-built nil)))

(defun apt-utils-describe-package ()
  "Describe package at point."
  (interactive)
  (apt-utils-package-at-message))

(defun apt-utils-kill-other-window-buffers ()
  "Kill buffers in other windows and the windows themselves.
See `apt-utils-kill-buffer-confirmation-function' for
customisation options."
  (interactive)
  (cond
   ((not (eq major-mode 'apt-utils-mode))
    (error "Not in APT utils buffer"))
   ((not (cdr (window-list)))
    (message "No other windows to kill"))
   (t
    (when (or (null apt-utils-kill-buffer-confirmation-function)
              (funcall apt-utils-kill-buffer-confirmation-function
                       "Kill buffers in other windows? "))
      (let ((buffer-list
             (delq (current-buffer)
                   (mapcar #'window-buffer (window-list)))))
        (mapc (lambda (b)
                (when (buffer-live-p b)
                  (kill-buffer b)))
              buffer-list))
      (delete-other-windows))
    (message nil))))

;; Track positions

(defun apt-utils-update-buffer-positions (type)
  "Update `apt-utils-buffer-positions'.
TYPE can be forward, backward, or toggle."
  (let (posns)
    (cond
     ((eq type 'forward)
      ;; Make the key unique; we could visit the same package more
      ;; than once
      (apt-utils-puthash (format "%s/%s/%d"
                       (caar apt-utils-package-history)
                       (cdar apt-utils-package-history)
                       (length apt-utils-package-history))
               (list (point) (window-start (selected-window)))
               apt-utils-buffer-positions))
     ((eq type 'backward)
      ;; Remove old values
      (remhash (format "%s/normal/%d"
                       (caar apt-utils-package-history)
                       (length apt-utils-package-history))
               apt-utils-buffer-positions)
      (remhash (format "%s/normal-showpkg/%d"
                       (caar apt-utils-package-history)
                       (length apt-utils-package-history))
               apt-utils-buffer-positions)
      (remhash (format "%s/virtual/%d"
                       (caar apt-utils-package-history)
                       (length apt-utils-package-history))
               apt-utils-buffer-positions)
      ;; Get position for previous package
      (setq posns
            (gethash (format "%s/%s/%d"
                             (car (cadr apt-utils-package-history))
                             (cdr (cadr apt-utils-package-history))
                             (1- (length apt-utils-package-history)))
                     apt-utils-buffer-positions)))
     ((eq type 'toggle)
      ;; new/old package types
      (let ((package (caar apt-utils-package-history))
            (type (cdar apt-utils-package-history))
            new old)
        (if (equal type 'normal)
            (setq old 'normal
                  new 'normal-showpkg)
          (setq old 'normal-showpkg
                new 'normal))
        ;; Set position for old entry
        (apt-utils-puthash (format "%s/%s/%d"
                         package
                         old
                         (length apt-utils-package-history))
                 (list (point) (window-start (selected-window)))
                 apt-utils-buffer-positions)
        ;; Get position for new entry
        (setq posns
              (gethash (format "%s/%s/%d"
                               package
                               new
                               (length apt-utils-package-history))
                       apt-utils-buffer-positions
                       (list 1 1)))     ; default value
        )))
    posns))

(defun apt-utils-current-field-packages ()
  "Return a list of the packages on the current line."
  (let ((keywords '("Conflicts" "Depends" "Enhances" "Package"
                    "Pre-Depends" "Provides" "Recommends" "Replaces"
                    "Suggests"))
        eol match packages posn string)
    (save-excursion
      (end-of-line)
      (setq eol (point))
      (beginning-of-line)
      (cond
       ((eobp)
        (message "Not on package field line.")
        nil)
       ((and (re-search-forward "^\\([^ \n:]+\\): " eol t)
             (setq match (match-string 1))
             (member match keywords))
        (setq posn (point))
        (goto-char (apt-field-end-position))
        (setq string (buffer-substring-no-properties posn (point)))
        (with-temp-buffer
          (insert string)
          (goto-char (point-min))
          (while (re-search-forward "\n *" nil t)
            (replace-match " "))
          (setq packages
                ;; Packages split by commas, or alternatives by vertical
                ;; bars; for Enhances, multiple lines my be spanned
                (split-string (buffer-substring (point-min) (point-max))
                              " ?[,|] ?")))
        packages)
       (t
        (message "Not on package field line.")
        nil)))))

(defun apt-field-end-position ()
  "Move to end of current field."
  (save-excursion
    (re-search-forward "\\(^[^: ]+:\\|^$\\)")
    (beginning-of-line)
    (backward-char)
    (point)))

;; Borrowed from gnus/lisp/time-date.el

(defun apt-utils-time-less-p (t1 t2)
  "Say whether time value T1 is less than time value T2."
  (or (< (car t1) (car t2))
      (and (= (car t1) (car t2))
           (< (nth 1 t1) (nth 1 t2)))))

(defun apt-utils-web-browse-debian-changelog ()
  "Browse web version of Debian ChangeLog file for the current package."
  (interactive)
  (apt-utils-web-browse-url
   apt-utils-web-browse-debian-changelog-url))

(defun apt-utils-web-browse-bug-reports ()
  "Browse Debian bug reports for the current package."
  (interactive)
  (apt-utils-web-browse-url
   apt-utils-web-browse-bug-reports-url))

(defun apt-utils-web-browse-copyright ()
  "Browse web version of Debian copyright file for the current package."
  (interactive)
  (apt-utils-web-browse-url
   apt-utils-web-browse-copyright-url))

(defun apt-utils-web-browse-versions ()
  "Browse web version information for the current package."
  (interactive)
  (apt-utils-web-browse-url
   apt-utils-web-browse-versions-url))

(defun apt-utils-web-browse-url (url)
  "Browse Debian-related URL.
The URL can contain tokens that need formatting (see
`apt-utils-web-format-url')."
  (cond
   ((not (equal major-mode 'apt-utils-mode))
    (message "Not in APT utils buffer."))
   ((not (memq (cdar apt-utils-package-history) '(normal normal-showpkg normal-installed)))
    (message "Not a normal package."))
   (t
    (browse-url (apt-utils-web-format-url url)))))

(defun apt-utils-web-format-url (url)
  "Format and return Debian URL.
The tokens that can be replaced are:
    %d: pool directory
    %s: source package name
    %p: package name
    %v: package version."
  (let ((buffer (current-buffer))
        (package (caar apt-utils-package-history))
        (type (cdar apt-utils-package-history))
        char source-package version)
    (save-excursion                     ; for normal package type
      (with-temp-buffer
        (cond
         ((memq type '(normal normal-installed))
          (set-buffer buffer))
         ((eq type 'normal-showpkg)
          (call-process apt-utils-apt-cache-program nil '(t nil) nil "show" package)))
        (goto-char (point-min))
        (if (re-search-forward "^Source: \\(.*\\)$" (point-max) t)
            (setq source-package (match-string 1))
          (setq source-package package))
        (goto-char (point-min))
        (re-search-forward "^Version: \\([0-9]:\\)?\\(.*\\)$" (point-max))
        (setq version (match-string 2))))
    ;; Format the URL
    (while (string-match "%\\(.\\)" url)
      (setq char (string-to-char (match-string 1 url)))
      (setq url (apt-utils-replace-regexp-in-string
                 (match-string 0 url)
                 (cond
                  ((eq char ?d)
                   (substring source-package 0
                              (if (string-match "^lib[a-z]"
                                                source-package)
                                  4 1)))
                  ((eq char ?s) source-package)
                  ((eq char ?p) package)
                  ((eq char ?v) version)
                  (t
                   (error "Unrecognized token (%%%c) in URL: %s" char url)))
                 url))))
  url)

(defun apt-utils-packages-needs-update ()
  "Return t if `apt-utils' package lists needs updating."
  (or (not apt-utils-package-list-built)
      (apt-utils-time-less-p apt-utils-package-list-built
                             (nth 5 (file-attributes apt-utils-timestamped-file)))))

(defun apt-utils-update-mode-name ()
  "Update `mode-name' for all buffers in `apt-utils-mode'."
  (let* ((need-update (apt-utils-packages-needs-update))
         (update-string
          (and need-update
               (substitute-command-keys
                ": update using \\<apt-utils-mode-map>\\[apt-utils-rebuild-package-lists]")))
         (name (concat "APT utils" update-string)))
    (mapc (lambda (b)
            (with-current-buffer b
              (when (eq major-mode 'apt-utils-mode)
                (setq mode-name name))))
          (buffer-list))))

(defun apt-utils-current-links-add-package (package)
  "Add PACKAGE to `apt-utils-current-links' hashtable."
  (unless (eq package 'broken)
    (apt-utils-puthash package nil apt-utils-current-links)))

;; Mode settings

(defvar apt-utils-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "#")             'apt-utils-rebuild-package-lists)
    (define-key map (kbd "1")             'delete-other-windows)
    (define-key map (kbd "<")             'apt-utils-view-previous-package)
    (define-key map (kbd ">")             'apt-utils-choose-package-link)
    (define-key map (kbd "?")             'describe-mode)
    (define-key map (kbd "DEL")           'scroll-down)
    (define-key map (kbd "M-TAB")         'apt-utils-previous-package)
    (define-key map (kbd "RET")           'apt-utils-follow-link)
    (define-key map (kbd "S s")           'apt-utils-search)
    (define-key map (kbd "S f")           'apt-utils-search-file-names)
    (define-key map (kbd "S g")           'apt-utils-search-grep-dctrl)
    (define-key map (kbd "S n")           'apt-utils-search-names-only)
    (define-key map (kbd "SPC")           'scroll-up)
    (define-key map (kbd "TAB")           'apt-utils-next-package)
    (define-key map (kbd "b C")           'apt-utils-web-browse-debian-changelog)
    (define-key map (kbd "b b")           'apt-utils-web-browse-bug-reports)
    (define-key map (kbd "b l")           'apt-utils-web-browse-copyright)
    (define-key map (kbd "b v")           'apt-utils-web-browse-versions)
    (define-key map (kbd "d")             'apt-utils-describe-package)
    (when (fboundp 'window-list)
      (define-key map (kbd "k")           'apt-utils-kill-other-window-buffers))
    (define-key map (kbd "l")             'apt-utils-list-package-files)
    (define-key map (kbd "o")             'other-window)
    (define-key map (kbd "q")             'apt-utils-quit)
    (define-key map (kbd "s")             'apt-utils-show-package)
    (define-key map (kbd "t")             'apt-utils-toggle-package-info)
    (define-key map (kbd "v C")           'apt-utils-view-debian-changelog)
    (define-key map (kbd "v R")           'apt-utils-view-debian-readme)
    (define-key map (kbd "v N")           'apt-utils-view-debian-news)
    (define-key map (kbd "v c")           'apt-utils-view-changelog)
    (define-key map (kbd "v e")           'apt-utils-view-emacs-startup-file)
    (define-key map (kbd "v f")           'apt-utils-view-package-files)
    (define-key map (kbd "v l")           'apt-utils-view-copyright)
    (define-key map (kbd "v m")           'apt-utils-view-man-page)
    (define-key map (kbd "v n")           'apt-utils-view-news)
    (define-key map (kbd "v r")           'apt-utils-view-readme)
    (define-key map (kbd "v v")           'apt-utils-view-version)
    (define-key map [(shift iso-lefttab)] 'apt-utils-previous-package)
    (define-key map [(shift tab)]         'apt-utils-previous-package)
    (define-key map
      (if apt-utils-xemacs-p '(button2) (kbd "<mouse-2>"))
      'apt-utils-mouse-follow-link)
    map)
  "Keymap for apt-utils mode.")

;; Menus

(defvar apt-utils-menu nil
  "Menu to use for `apt-utils-mode'.")

(when (fboundp 'easy-menu-define)

  (easy-menu-define apt-utils-menu apt-utils-mode-map "Apt Utils Menu"
    `("Apt Utils"
      ["Show Package"               apt-utils-show-package t]
      ["Toggle Package Info"        apt-utils-toggle-package-info
       (apt-utils-toggle-package-p)]
      ["View Previous Package"      apt-utils-view-previous-package
       (apt-utils-previous-package-p)]
      ["Choose Package Link"        apt-utils-choose-package-link
       (> (hash-table-count apt-utils-current-links) 0)]
      ["Next Package Link"          apt-utils-next-package
       (> (hash-table-count apt-utils-current-links) 0)]
      ["Previous Package Link"      apt-utils-previous-package
       (> (hash-table-count apt-utils-current-links) 0)]
      ["Follow Link at Point"       apt-utils-follow-link
       (apt-utils-package-at-point)]
      ["Rebuild Package Lists"      apt-utils-rebuild-package-lists t]
      "---"
      ("Search"
       ["Package Descriptions"      apt-utils-search t]
       ["Package Names"             apt-utils-search-names-only t]
       ["Installed Files"           apt-utils-search-file-names t]
       ["Grep-Dctrl"                apt-utils-search-grep-dctrl t])
      ("View Files"
       ,@(list (if apt-utils-xemacs-p
                   :included
                 :active)
               '(apt-utils-current-package-installed-p))
       ["ChangeLog"                 apt-utils-view-changelog
        (apt-utils-changelog-file)]
       ["Debian ChangeLog"          apt-utils-view-debian-changelog
        (apt-utils-debian-changelog-file)]
       ["README"                    apt-utils-view-readme
        (apt-utils-readme-file)]
       ["Debian README"             apt-utils-view-debian-readme
        (apt-utils-debian-readme-file)]
       ["NEWS"                      apt-utils-view-news
        (apt-utils-news-file)]
       ["Debian NEWS"               apt-utils-view-debian-news
        (apt-utils-debian-news-file)]
       ["Copyright"                 apt-utils-view-copyright
        (apt-utils-copyright-file)]
       "---"
       ["Man Page"                  apt-utils-view-man-page
        (apt-utils-current-package-installed-p)]
       "---"
       ["All Package Files (dired)" apt-utils-view-package-files
        (apt-utils-current-package-installed-p)])
      ("Browse URL"
       ,@(list (if apt-utils-xemacs-p
                   :included
                 :active)
               '(apt-utils-toggle-package-p))
       ["Debian ChangeLog"          apt-utils-web-browse-debian-changelog t]
       ["Bug Reports"               apt-utils-web-browse-bug-reports t]
       ["Copyright"                 apt-utils-web-browse-copyright t]
       ["Package Versions"          apt-utils-web-browse-versions t])
      "---"
      ["Help"                       describe-mode t]
      ["Quit"                       apt-utils-quit t])))

(defun apt-utils-mode ()
  "Major mode to interface Emacs with APT (Debian package management).

Start things off with, for example:

    M-x apt-utils-show-package RET emacs21 RET

Other packages (dependencies, conflicts etc.) can be navigated
using:

    \\[apt-utils-toggle-package-info] toggle package and showpkg information
    \\[apt-utils-view-previous-package] show the previous package from history
    \\[apt-utils-choose-package-link] choose next package from current links
    \\[apt-utils-next-package] move to next package link
    \\[apt-utils-previous-package] move to previous package link
    \\[apt-utils-follow-link] show package for the link at point
    \\[apt-utils-list-package-files] list package files (in a `dired' buffer)

Confirmation will be requested before updating the list of known
packages.  The update can be started at any time with
\\[apt-utils-rebuild-package-lists].

Package searches can be performed using:

    \\[apt-utils-search] search for regular expression in package names and descriptions
    \\[apt-utils-search-names-only] search for regular expression in package names
    \\[apt-utils-search-file-names] search for string in filenames
    \\[apt-utils-search-grep-dctrl] search for regular expression in selected package fields
    (using the grep-dctrl program)

Files associated with installed packages can be accessed using:

    \\[apt-utils-view-changelog] view ChangeLog file
    \\[apt-utils-view-debian-changelog] view Debian ChangeLog file
    \\[apt-utils-view-readme] view README file
    \\[apt-utils-view-debian-readme] view Debian README file
    \\[apt-utils-view-news] view NEWS file
    \\[apt-utils-view-debian-news] view Debian NEWS file
    \\[apt-utils-view-copyright] view copyright (licence) file
    \\[apt-utils-view-man-page] view man page

Web locations can be visited using:

    \\[apt-utils-web-browse-debian-changelog] browse Debian ChangeLog URL
    \\[apt-utils-web-browse-bug-reports] browse bug report URL
    \\[apt-utils-web-browse-copyright] browse copyright (licence) URL
    \\[apt-utils-web-browse-versions] browse package versions URL

A history of navigated packages is maintained when package links
are followed using `apt-utils-choose-package-link' or
`apt-utils-follow-link'.  This history is reset when
`apt-utils-show-package' or any of the search commands is used.

Key definitions:
\\{apt-utils-mode-map}"
  (kill-all-local-variables)
  (use-local-map apt-utils-mode-map)
  (setq major-mode 'apt-utils-mode)
  (setq mode-name "APT utils")
  (setq buffer-undo-list t)
  (setq truncate-lines t)
  ;; XEmacs
  (when (and (fboundp 'easy-menu-add)
             apt-utils-menu)
    (easy-menu-add apt-utils-menu))
  (add-hook 'kill-buffer-hook 'apt-utils-cleanup nil t)
  (run-hooks 'apt-utils-mode-hook))

;; Debugging

(defun apt-utils-trace-all ()
  "Trace all `apt-utils' functions.  For debugging."
  (require 'trace)
  (let ((buffer (get-buffer-create "*APT Utils Trace*")))
    (buffer-disable-undo buffer)
    (all-completions "apt-utils" obarray
                     (lambda (sym)
                       (and (fboundp sym)
                            (not (memq (car-safe (symbol-function sym))
                                       '(autoload macro)))
                            (trace-function-background sym buffer))))))

(defun apt-utils-sort-result ()
  (save-excursion
    (goto-char (point-min))
    (forward-line 2)
    (sort-lines nil (point) (point-max))))

(provide 'apt-utils)

;;; apt-utils.el ends here